Skip to main content

Astrodetection

Astrodetection is a Python library designed for detecting astroturfing clues from lists of posts (mainly on X up to now, but not exclusively)

Installation

Pip

pip install "astrodetection[standard]"

or

pip install "astrodetection[light]"

Conda

  1. Use the YAML file to configure the environment with conda:

    conda create -n astrodetection_env
    conda activate astrodetection_env
    conda env update -f environment_standard.yml
    

Note: the environment_standard.yml configuration file uses FAISS and Fasttext libraries for VIGINUM D3LTA implementation

**If you have compatibility issues, prefer environment_light.yml and use astrodetection_light module

Usage

You can import directly the main functions:

from astrodetection import semantic_faiss, prepare_input_data, compute_bot_likelihood_metrics, create_network, copypasta_score_hub

Or use them directly:

import glob
import pandas as pd
import os
import numpy as np
import astrodetection

# Load a single JSON file into a DataFrame
file = "file_path"  # Select the first file
df = pd.read_json(file)
df.index = df.index.astype(str)  # Compatibility with d3lta

# Preprocess the DataFrame
df = df[df['tweet'].str.len() > 100]
df = df[df['username'] != 'grok']
df.index = df.index.astype(str)

# Compute matches and scores
df_filtered, df_emb = astrodetection.prepare_input_data(df, embeddings=df['emb'])

matches, df_cluster = astrodetection.semantic_faiss(
    df_filtered.rename(columns={'tweet': 'original'}),
    min_size_txt=0,
    df_embeddings_use=df_emb,
    threshold_grapheme=0.8,
    threshold_language=0.715,
    threshold_semantic=0.9
) #function taken from D3LTA 

# Create the raw post-similarity network and measure its largest component
network = astrodetection.create_network(matches, df, return_sigma=False)
copypasta_hub = astrodetection.copypasta_score_hub(network, df)

# The same indicator is also available in the combined metrics
scores = astrodetection.compute_bot_likelihood_metrics(
    df,
    matches=matches,
    G_copypasta=network,
)

# Create the interactive visualization when needed
network_viz = astrodetection.create_network(matches, df)

Inspecting individual indicators

Call an indicator directly with inspection=True to get its usual result and a JSON-serializable details dictionary for reporting.

from astrodetection.utils import check_recent_account

score, details = check_recent_account(
    df,
    account_creation_col="createdDate",
    tweet_date_col="tweet_date",
    age_days_threshold=30,
    deduplicate_accounts=True,
    inspection=True,
)
recent_accounts = details["rows"]  # account, created_at, activity_at, age_days

Each row describes an account selected using its earliest recorded activity. The numerator and denominator give the exact counts used for the percentage. Creation-week tables list row IDs, or account IDs when deduplication uses an account column. The window_end of a temporal entropy window is exclusive. Undefined indices include the available rows and a reason. When an empty input would raise a division-by-zero error in copypasta_score or get_top_users, inspection reports None for the percentage and includes the zero denominator. Existing result pairs stay together as the first element, including the pair from get_similarity_hub_score(..., return_communities=True).

Measuring domain age at sharing time

Both astrodetection and astrodetection_light expose the same functions for measuring domain age. Pass a column of expanded HTTP(S) URL lists and a column of sharing dates. The extraction keeps one observation per post and registrable domain, using the registry section of the bundled Public Suffix List.

from astrodetection import compute_domain_age_metrics, extract_domain_observations

observations = extract_domain_observations(
    df,
    url_col="expanded_urls",
    date_col="tweet_date",
    exclude_domains=("t.co",),
)
scores = compute_domain_age_metrics(observations, registration_dates)

Supply registration_dates as a mapping from the normalized ASCII domains in observations["domain"] to registration dates. Both functions work offline. The result includes the mean of log1p(age_in_days), the number of candidate pairs, and the number of valid pairs. Missing or invalid dates and negative ages reduce coverage. The mean is None if no pair has a valid age.

New changes

  1. semantic_faiss function can now take detect only copypastas based on levenshtein distance, ignoring embeddings, if "skip" is passed as argument in df_embeddings_use field.

  2. compute_bot_likelihood_metrics function can now take columns' names as arguments for more customization

  3. compute_bot_likelihood_metrics now returns high_following_followers_ratio (%), the share of rows whose following/followers ratio exceeds fw_ratio_threshold (default 10).

  4. copypasta_score_hub returns the percentage of all original posts belonging to the largest connected component of a post-similarity network. By default it retains every duplicate type and every edge already present in the network.

Metadata

Release files for astrodetection 0.3.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astrodetection 0.3.9.1
File Size Uploaded Provenance Actions
astrodetection-0.3.9.1.tar.gz 78.3 kB Without provenance Details for astrodetection-0.3.9.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for astrodetection 0.3.9.1
File Interpreter ABI Platform Actions
astrodetection-0.3.9.1-py3-none-any.whl Python 3 none any Details for astrodetection-0.3.9.1-py3-none-any.whl

Total release size: 153.1 kB

Release files / astrodetection-0.3.9.1.tar.gz

Download URL astrodetection-0.3.9.1.tar.gz
Size 78.3 kB
Tags Source
SHA-256 checksum
How to use checksums
036e921b1533309e0e646977a8cfacd7df7fcd06da881b00ecfef25adb6e2fe4
BLAKE2b-256 checksum
How to use checksums
26b578bf3200af04657856f19f3d1b04d3715e0c9eb0f4790d08cd259092a6b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / astrodetection-0.3.9.1-py3-none-any.whl

Download URL astrodetection-0.3.9.1-py3-none-any.whl
Size 74.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0421e984231e9a40574cbb67ff86ff4637c0ac986274dd655c53a8cb5fe00bde
BLAKE2b-256 checksum
How to use checksums
bc3f11895a75f2eb5c519038de3d1ea0236f60b14e17cb3a6557f22082eda86b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Security for astrodetection 0.3.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page