Skip to content

Large Conversation

Open In Colab

Notebook Preparation

Screenshot of final results

For fastest processing

  1. Click Edit > Notebook settings
  2. Select T4 GPU and save.

The runtime will restart, and the pacmap and localmap steps should run much quicker :)

import os

if os.environ.get("IS_GENERATING_DOCS", None):
    from itables import init_notebook_mode
    init_notebook_mode(all_interactive=True) # (1)!
  1. renders interactive tables for dataframes, which look really nice on the documentation website

Installation & Import

%pip install -qqq git+https://github.com/patcon/valency-anndata@main
import valency_anndata as val

Loading Data

# Loads dataset of 33k german-speaking participants,
# run by Aufstehen political party in 2018
adata = val.datasets.aufstehen(translate_to="en")
Downloading bytes:           |  0.00B



Reconstructing (incomplete total...): |          |  0.00B /  0.00B



Fetching 7 files:   0%|          | 0/7 [00:00<?, ?it/s]
adata.var
content participant_id_authored created_date moderation_state is_seed is_meta language_original language_current is_translated
comment-id
0I am afraid of war and conflict in the world.01536009294110<NA>enTrue
1I have existential fears.01536009306110<NA>enTrue
2The world is becoming more and more complicated and I feel overwhelmed by it.01536009361110<NA>enTrue
3I feel like I don't have my future in my own hands. My origins still define me.01536009375110<NA>enTrue
5The compatibility of family and work needs to be improved. Free daycare places should be normal and there should be enough available01536009393110<NA>enTrue
6I'm afraid of old age and retirement.01536009412110<NA>enTrue
7We now have to think about what will happen if certain jobs are taken over by robots and machines.01536009432110<NA>enTrue
8We need politicians with a connection to reality.01536009442110<NA>enTrue
10I actually feel very safe in Germany.01536009458110<NA>enTrue
11The shift to the right in society scares me.01536009466110<NA>enTrue
(773 more rows not shown)

Running Vanilla Polis Pipeline

# Run the Polis steps and generate a "visual diff" for before and after (green = new)
with val.viz.schematic_diagram(diff_from=adata):
  # Takes ~1 minute
  val.tools.recipe_polis(adata)

svg

val.viz.embedding(adata, basis="pca_polis", color="kmeans_polis")

png

Running Additional Projections: PaCMAP, LocalMAP, UMAP

# Takes ~1 min (~4 min without GPU)
val.tools.pacmap(adata, layer="X_masked_imputed_mean")
val.viz.embedding(adata, basis="pacmap", color="kmeans_polis")

png

# Takes ~3 min (~10 min without GPU)
val.tools.localmap(adata, layer="X_masked_imputed_mean")
val.viz.embedding(adata, basis="localmap", color="kmeans_polis")

png

# Takes ~2 min
val.preprocessing.neighbors(adata, use_rep="X_pca_polis")
val.tools.umap(adata)
/home/runner/work/valency-anndata/valency-anndata/.venv/lib/python3.11/site-packages/umap/spectral.py:548: UserWarning: Spectral initialisation failed! The eigenvector solver
failed. This is likely due to too small an eigengap. Consider
adding some noise or jitter to your data.

Falling back to random initialisation!
  warn(
val.viz.embedding(adata, basis="umap", color="kmeans_polis")

png

Animating 33k grouped participants

# Explore the differences between each projection based on the
# results of k-means clustering on the basic Polis PCA projections.
val.viz.jscatter(
    adata,
    use_reps=[
        "X_pca_polis",
        "X_pacmap",
        "X_localmap",
        "X_umap",
    ],
    color="kmeans_polis",
)

Coloring with data

# Calculate general vote metrics on all participants and statements.
with val.viz.schematic_diagram(diff_from=adata):
  val.preprocessing.calculate_qc_metrics(adata, inplace=True)

svg

# Explore the differences between each projection based on these metrics.
val.viz.jscatter(
    adata,
    use_reps=[
        "X_pca_polis",
        "X_pacmap",
        "X_localmap",
        "X_umap",
    ],
    color="pct_seen", # Groups from k-means clustering of PCA projections
)