allotaxonometer-ui 0.1.20 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,13 +1,17 @@
1
1
 
2
+
2
3
  # Allotaxonometer UI
3
4
 
5
+
4
6
  <img width="50%" alt="pipeline" src="https://github.com/user-attachments/assets/41375b5f-e942-499f-aea4-bca004767e52">
5
7
 
6
- Headless UI components for allotaxonometer visualizations built with **Svelte 5**.
8
+ <p>
7
9
 
8
10
  - Visit our [single-page web application](https://vermont-complex-systems.github.io/complex-stories/allotaxonometry) to try it out online.
9
- - Use [allotaxonometer-core](https://github.com/jstonge/allotaxonometer-core) for Rust/Python server-side computation.
10
- - See the original [py-allotax](https://github.com/compstorylab/py-allotax) for the Python-only implementation.
11
+ - Use [py-allotax](https://github.com/compstorylab/py-allotax) to use allotaxonometry programmatically ([:octocat: github](https://github.com/compstorylab/py-allotax))
12
+ - We also maintain a [matlab version](https://gitlab.com/compstorylab/allotaxonometer) to use allotaxonometry at scale.
13
+
14
+ Headless UI components for allotaxonometer visualizations built with Svelte 5.
11
15
 
12
16
  ## Installation
13
17
 
@@ -15,112 +19,146 @@ Headless UI components for allotaxonometer visualizations built with **Svelte 5*
15
19
  npm install allotaxonometer-ui
16
20
  ```
17
21
 
18
- ## Usage
22
+ ## Use cases and install
19
23
 
20
- ### Client-side pipeline (Allotaxonograph class)
24
+ - Explore systems and formulate research questions: we recommend starting with the web app...
25
+ - Running multiple or larger scale analyses: we recommend using the py-allotax, which details its install instructions, details the required data format, and provides examples in its repo.
21
26
 
22
- Compute everything in the browser using the built-in JS/WASM pipeline:
23
-
24
- ```svelte
25
- <script>
26
- import { Allotaxonograph, Dashboard } from 'allotaxonometer-ui';
27
+ ## Data Input
27
28
 
28
- const instance = new Allotaxonograph(elem1, elem2, {
29
- alpha: 0.58,
30
- title: ['System 1', 'System 2']
31
- });
29
+ The allotaxonometer expects 2 datasets with `types` and `counts`. The `totalunique` and `probs` fields are **optional** and will be calculated automatically if not provided.
32
30
 
33
- let dat = $derived(instance?.dat);
34
- let barData = $derived(instance?.barData);
35
- let balanceData = $derived(instance?.balanceData);
36
- </script>
31
+ ### Minimal Format (Recommended)
32
+ ```javascript
33
+ const data = [{
34
+ types: ['John', 'William', 'James', 'George', 'Charles'],
35
+ counts: [8502, 7494, 5097, 4458, 4061]
36
+ // totalunique and probs calculated automatically!
37
+ }];
38
+ ```
37
39
 
38
- <Dashboard {dat} {barData} {balanceData}
39
- divnorm={instance.rtd.normalization}
40
- alpha={0.58}
41
- maxlog10={instance.maxlog10}
42
- title={['System 1', 'System 2']}
43
- />
40
+ ### Full Format (All Fields)
41
+ | | types | counts | totalunique | probs |
42
+ |----|---------|----------|---------------|---------|
43
+ | 0 | John | 8502 | 1161 | 0.0766 |
44
+ | 1 | William | 7494 | 1161 | 0.0675 |
45
+ | 2 | James | 5097 | 1161 | 0.0459 |
46
+ | 3 | George | 4458 | 1161 | 0.0402 |
47
+ | 4 | Charles | 4061 | 1161 | 0.0366 |
48
+
49
+
50
+ ## Paper data
51
+
52
+ #### Babynames data
53
+
54
+ The original babyname dataset for boys and girls can be found on the [catalog.data.gov](https://catalog.data.gov/dataset?tags=baby-names) website. But we use the dataset [here](http://pdodds.w3.uvm.edu/permanent-share/pocs-babynames.zip) to replicate the original paper. You can find a 5-years aggregated version used in the `Observable` version in `data/`. The original dataset includes each year from 1880–2018, which have 5 or more applications. You can convert the original folder into the formatted `.json` file using R with the following command:
55
+
56
+ ```R
57
+ read_and_write_babyname_dat <- function(fname, gender) {
58
+ d <- readr::read_csv(fname,
59
+ col_names = c("types", "gender", "counts"),
60
+ col_select = c("types", "counts"),
61
+ col_types = c("c", "i"))
62
+
63
+ d$probs <- d$counts / sum(d$counts)
64
+ d$total_unique <- nrow(d)
65
+ return(d)
66
+ }
67
+ # You need to be in the folder above `data/`, which is the unzip folder contained in
68
+ # http://pdodds.w3.uvm.edu/permanent-share/pocs-babynames.zip
69
+ purrr::map(
70
+ list.files("data/", pattern = "names-boys*"),
71
+ ~read_and_write_babyname_dat(paste("data", .x, sep = "/"), "boys")
72
+ )
73
+
74
+ purrr::map(
75
+ list.files("data/", pattern = "names-girls*"),
76
+ ~read_and_write_babyname_dat(paste("data", .x, sep = "/"), "girls")
77
+ )
44
78
  ```
45
79
 
46
- ### Server-computed data (API adapter)
80
+ #### Twitter data
47
81
 
48
- When using a backend that computes allotax results (e.g. via `allotaxonometer-core`), use `adaptDisplayResult` to map the API response to component-ready props:
82
+ We access the Twitter data from the Comptuational Story Lab [storywrangling](https://gitlab.com/compstorylab/storywrangling)' API. Unfortunately, the API only work when you are connected on the University of Vermont's VPN. Follow the instructions [here](https://www.uvm.edu/it/kb/article/install-cisco-vpn/) to get the VPN working. Once this is done, run the following lines from the command line:
49
83
 
50
- ```js
51
- import { adaptDisplayResult } from 'allotaxonometer-ui';
84
+ ```shell
85
+ git clone https://gitlab.com/compstorylab/storywrangling.git
86
+ cd storywrangling
87
+ pip install -e .
88
+ ```
52
89
 
53
- const apiResult = await fetch('/allotax?...').then(r => r.json());
54
- const display = adaptDisplayResult(apiResult);
55
- // display.dat, display.barData, display.balanceData, display.divnorm, display.maxlog10, etc.
90
+ Then from `python` you can get the top ngram count with rank data for any given day with the following:
91
+
92
+ ```python
93
+ from storywrangling import Storywrangler
94
+ from datetime import datetime
95
+ import json
96
+ from pathlib import Path
97
+
98
+ def get_ngram(yr, month, day, fname=False):
99
+ storywrangler = Storywrangler()
100
+ ngram_zipf = storywrangler.get_zipf_dist(
101
+ date=datetime(yr, month, day),
102
+ lang="en", ngrams="1grams",
103
+ max_rank=10000, rt=False
104
+ ).reset_index()\
105
+ .rename(columns={
106
+ "ngram":"types", "count":"counts", "count_no_rt":"counts_no_rt",
107
+ "rank":"rank", "rank_no_rt":"rank_no_rt", "freq":"probs", "freq_no_rt":"probs_no_rt"
108
+ })\
109
+ .dropna()\
110
+ .assign(totalunique = lambda x: x.shape[0])\
111
+ .loc[:, ["types", "counts", "totalunique", "probs"]]\
112
+ .to_dict(orient="index")
113
+
114
+ ngram_zipf = { f"{yr}_{month}_{day}": [_ for _ in ngram_zipf.values()] }
115
+
116
+ if fname:
117
+ if Path(fname).exists():
118
+ with open(fname) as f:
119
+ old_dat = json.load(f)
120
+
121
+ ngram_zipf.update(old_dat)
122
+
123
+ with open(fname, 'w') as f:
124
+ json.dump(ngram_zipf, f)
125
+ else:
126
+ return ngram_zipf
56
127
  ```
57
128
 
58
- ### Multi-alpha (recommended for API usage)
129
+ Note that this solution is a bit clunky. At some point we would prefer to have a sql DB that we can interact with.
59
130
 
60
- Fetch all alpha values at once and switch instantly on the client:
131
+ #### Species Abundance Data
61
132
 
62
- ```svelte
63
- <script>
64
- // Fetch once with all alphas — no refetch when alpha slider changes
65
- const data = await fetch('/allotax?alphas=0,0.08,0.17,...').then(r => r.json());
66
- // data.balance — shared across alphas
67
- // data.alpha_results[i] — { diamond_counts, wordshift, normalization, alpha }
68
- </script>
133
+ We access the species abundance data from https://datadryad.org/stash/dataset/doi:10.15146/5xcp-0d46, downloading the full dataset, unzipping it, and then loading bci.tree\<i\>.rdata for i in (1-8), as well as bci.spptable.rdata. We then run the following code to subset the full census represented by each of the bci.tree\<i\>.rdata to get the counts of the species of the trees alive during that census, combine merge that with the species name database to get the full name, and then put it in the format that our allotaxonometer code expects:
69
134
 
70
- {@const slice = data.alpha_results[alphaIndex]}
71
- {@const dat = { counts: slice.diamond_counts, deltas: [] }}
135
+ ```r
136
+ library(Sys)
137
+ library(dplyr)
138
+ library("rlist")
139
+ library(jsonlite)
72
140
 
73
- <Dashboard
74
- {dat}
75
- barData={slice.wordshift}
76
- balanceData={data.balance}
77
- divnorm={slice.normalization}
78
- alpha={slice.alpha}
79
- />
80
- ```
141
+ tree_data <- vector("list", length=8)
81
142
 
82
- ## Components
83
143
 
84
- | Component | Description |
85
- |-----------|-------------|
86
- | `Dashboard` | Main orchestrating component combining all visualizations |
87
- | `Diamond` | Rank-rank scatter plot showing relationships between systems |
88
- | `Wordshift` | Horizontal bar chart showing type contributions to divergence |
89
- | `DivergingBarChart` | Bar chart for positive/negative shifts |
90
- | `Legend` | Color and size legends |
91
144
 
92
- ## Data Input
145
+ dfs = list(bci.tree1, bci.tree2, bci.tree3, bci.tree4, bci.tree5, bci.tree6, bci.tree7, bci.tree8)
93
146
 
94
- The allotaxonometer expects 2 datasets with `types` and `counts`. The `totalunique` and `probs` fields are **optional** and will be calculated automatically if not provided.
95
147
 
96
- ### Minimal Format (Recommended)
97
- ```javascript
98
- const data = [{
99
- types: ['John', 'William', 'James', 'George', 'Charles'],
100
- counts: [8502, 7494, 5097, 4458, 4061]
101
- // totalunique and probs calculated automatically!
102
- }];
103
- ```
148
+ for (i in seq_along(dfs)) {
149
+ print(i)
150
+ full_census <- merge(dfs[[i]], bci.spptable, by='sp')
151
+ alive_census <-full_census[full_census$status %in% c('A','AD'),] # A='Alive', AD='A seldom-used code, applied when a tree was noted as dead in one census but was found alive in a later census. For most purposes, this code should be interpreted the same as code A for alive.'
152
+ count_df <- dplyr::count(alive_census, Latin, sort = TRUE)
153
+ names(count_df)[names(count_df) == 'Latin'] <- 'types'
154
+ names(count_df)[names(count_df) == 'n'] <- 'counts'
155
+ count_df['totalunique'] <- nrow(count_df)
156
+ count_df['probs']<-count_df['counts'] / nrow(alive_census)
157
+ tree_data[[i]] <- count_df
158
+ }
104
159
 
105
- ### Full Format
106
- ```javascript
107
- const data = [{
108
- types: ['John', 'William', 'James', 'George', 'Charles'],
109
- counts: [8502, 7494, 5097, 4458, 4061],
110
- totalunique: 1161,
111
- probs: [0.0766, 0.0675, 0.0459, 0.0402, 0.0366]
112
- }];
113
- ```
160
+ names(tree_data) <- c("1981-1983", "1985", "1991-1992", "1995-1996", "2000-2001", "2005-2006", "2010-2011", "2013-2015")
114
161
 
115
- ## Development
116
-
117
- ```bash
118
- npm run build # Build library (includes WASM)
119
- npm run build:wasm # Build Rust/WASM module only
120
- npm run test:run # Run all tests once
121
- npm test # Run tests in watch mode
162
+ exportJson <- toJSON(tree_data)
163
+ write(exportJson, "tree_species_counts.json")
122
164
  ```
123
-
124
- ## WASM Acceleration
125
-
126
- The `rank_turbulence_divergence` computation is implemented in Rust/WASM for 3-4x speedup on datasets with 10K+ types. WASM loads eagerly but falls back to JavaScript if unavailable. See [WASM_IMPLEMENTATION.md](WASM_IMPLEMENTATION.md) for details.