Metadata-Version: 2.5
Name: citation-topics
Version: 0.3.1
Summary: Research topics and areas from the citation links of any list of works, with Leiden clustering as CWTS does for OpenAlex.
Project-URL: Homepage, https://github.com/restrepo/citation-topics
Project-URL: Repository, https://github.com/restrepo/citation-topics
Project-URL: Issues, https://github.com/restrepo/citation-topics/issues
Project-URL: Changelog, https://github.com/restrepo/citation-topics/blob/main/CHANGELOG.md
Author-email: Diego Restrepo <restrepo@udea.edu.co>
License-Expression: MIT
License-File: LICENSE
Keywords: bibliometrics,citation network,community detection,leiden,scientometrics,topics
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: igraph>=0.11
Requires-Dist: leidenalg>=0.10
Requires-Dist: numpy>=1.24
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: pytest-cov>=5; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff==0.17.3; extra == 'dev'
Requires-Dist: twine>=5; extra == 'dev'
Description-Content-Type: text/markdown

# citation-topics

[![CI](https://github.com/restrepo/citation-topics/actions/workflows/ci.yml/badge.svg)](https://github.com/restrepo/citation-topics/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/citation-topics.svg)](https://pypi.org/project/citation-topics/)
[![Python](https://img.shields.io/pypi/pyversions/citation-topics.svg)](https://pypi.org/project/citation-topics/)

Research topics and broad areas for any list of works (papers, theses, projects, patents) from their citation
links, with Leiden clustering of the direct-citation network as CWTS does for the OpenAlex topics. It also gives
each work a second and a third topic, describes every topic from whatever the input carries (titles, abstracts,
keywords, journals, categories, countries), and ships Claude skills and an agent that name the topics.

It was extracted from [restrepo/inspireauth10](https://github.com/restrepo/inspireauth10), where it groups 1.9
million INSPIRE-HEP papers into 304 topics and 6 areas.

- [How the topics are made](https://restrepo.github.io/citation-topics/leiden.html): the method, step by step.
- [Sample board](https://restrepo.github.io/citation-topics/examples/inspire-991924/): the citation community of
  the INSPIRE-HEP author 991924, and the same network of works
  [in VOSviewer Online](https://app.vosviewer.com/?json=https%3A%2F%2Frestrepo.github.io%2Fcitation-topics%2Fexamples%2Finspire-991924%2Fworks_vosviewer.json).
- [OpenAlex sample board](https://restrepo.github.io/citation-topics/examples/openalex-A5010397600/): the same for
  the OpenAlex author A5010397600, in marine ecology.

## Install

```bash
pip install citation-topics
citation-topics --help
```

## Input

A JSON array or JSON Lines file (optionally gzipped), one object per work:

```json
{"id": "W1", "references": ["W7", "W9"], "year": 2019, "title": "...", "abstract": "...",
 "keywords": ["..."], "journal": "...", "categories": ["..."], "countries": ["..."],
 "authors": ["..."], "citations": 41, "url": "https://..."}
```

- `id` (unique; strings or integers, compared as strings) and `references` (ids of other works) are required.
  References to ids outside the list and self-citations are ignored and counted.
- Every other field is optional. Other names are read with `--id-field`, `--refs-field` and
  `--map NAME=FIELD` (for example `--map year=publication_year`).

| Optional field | Adds |
|---|---|
| `year` | years of each topic, time windows (`--from-year`, `--to-year`), comparisons between epochs |
| `title`, `abstract`, `keywords`, `journal`, `categories` | the description of each topic (`describe`), hence its name |
| `citations` | most cited works by that count (otherwise by citations inside the list) |
| `countries` | countries of each topic in the description |
| `authors` | the Authors tab of the board, and the authors of each work in the Network tab and in `works_vosviewer.json` (with `author_count` and `collaborations`, when present, for long author lists) |
| `url` | links from the board and the description to each work |
| any true/false field, e.g. `own` | a tab of the board that follows those works (`board --highlight own`) |

Naming needs at least one of `title`, `abstract`, `keywords`, `journal` or `categories`: `describe` lists the
ones missing in `dossier.json` (`missing`), and the `topic-namer` skill stops and says so when none is there. No
field is fetched or inferred from elsewhere. Fields the package does not know are kept in the input and ignored,
so a list can carry them from the start for a later board.

## Commands

```bash
citation-topics run works.jsonl.gz out/            # network, topics, areas, second and third topics
citation-topics describe works.jsonl.gz out/       # out/dossier.json: what each topic is about
citation-topics names out/ names.json              # put names into topics.json and areas.json
citation-topics export out/                        # VOSviewer files of the topics, CSVs
citation-topics export out/ --works works.jsonl.gz # also the network of works for VOSviewer (up to 10,000 works)
citation-topics board works.jsonl.gz out/          # out/board.html: tabs for what the fields allow
citation-topics agreement out_s1/ out_s2/ ...      # NMI/ARI between seeds, and the medoid to keep
citation-topics compare old/ new/ cmp/             # where earlier topics went; names carried over
citation-topics skills install                     # Claude skills and the topic-namer agent into ./.claude
```

`run` options and defaults (those of the INSPIRE run): `--resolution 16` (higher = more, smaller topics),
`--min-size 100` (smaller topics are merged into the topic they share the most links with), `--passes 20`
(stops early when the quality stops rising), `--seed 1`, `--area-resolution 0.3`, `--area-seeds 50` (the best
of that many runs). Tune them for other lists; the skill `topics-run` explains how.

## Example: the citation community of an INSPIRE-HEP profile

The topics of an author's papers together with every paper citing them. Tried on the INSPIRE-HEP author
[991924](https://inspirehep.net/authors/991924): 79 papers and 1,888 citing papers, 1,967 works, 11 topics in 3
areas, in about a minute; the result is the [sample board](https://restrepo.github.io/citation-topics/examples/inspire-991924/).

1. **Install** (Python 3.10 or newer):

   ```bash
   pip install citation-topics
   curl -O https://raw.githubusercontent.com/restrepo/citation-topics/main/examples/inspire_profile.py
   ```

2. **Get the works** from INSPIRE, with the number of the profile (`https://inspirehep.net/authors/991924`)
   or its INSPIRE BAI:

   ```bash
   python inspire_profile.py 991924 works.jsonl
   ```

   It writes, for each paper, its references, year, title, abstract, keywords, journal, arXiv categories,
   authors (the first 10, with the author count and collaboration), citation count and link, and `"own": true`
   for the author's papers.

3. **Topics.** A list of about 2,000 works needs smaller topics than the defaults, which suit millions of works:

   ```bash
   citation-topics run works.jsonl out/ --resolution 1 --min-size 20
   ```

   Try two or three resolutions (for example 1, 2, 4) and keep the one whose topics are solid (`share_inside` in
   `out/topics.json`) and readable in number. Optionally run seeds 1 to 4 into different folders and keep the
   one `citation-topics agreement` calls the medoid.

4. **Describe and export:**

   ```bash
   citation-topics describe works.jsonl out/ --abstracts
   citation-topics export out/ --works works.jsonl --highlight own --highlight-label "The author's papers"
   ```

5. **Name the topics**, with or without Claude:

   - *With Claude Desktop (optional; the Code tab, or Claude Code):* install the skills in the folder, open the
     folder in the Code tab and ask, for example, "name the topics in out/ with the topic-namer skill". Claude reads
     `out/dossier.json`, writes `names.json` with a name and a reason for every topic and area, and applies it.

     ```bash
     citation-topics skills install
     ```

   - *Without Claude:* read `out/dossier.json` (distinctive terms, most cited titles, journals and categories of
     each topic), write `names.json` yourself in the format of the `names` command, and apply it:

     ```bash
     citation-topics names out/ names.json
     citation-topics export out/ --works works.jsonl --highlight own --highlight-label "The author's papers"
     ```

     Unnamed topics are shown as "Topic N".

6. **Board:** one page with the map and table of topics, works per year, a searchable list of works, the network
   of works with one labelled cluster per topic (click a work to see only its citation links, magenta to the works it
   cites and amber to the works citing it, its first author beside it, and its authors, year, citations and
   INSPIRE link below the graph), and a tab with the author's own papers by topic:

   ```bash
   citation-topics board works.jsonl out/ --highlight own --highlight-label "Your papers" --title "Your name: citation topics"
   ```

   Open `out/board.html` in a browser, or ask Claude to publish it (skill `topics-board`).

7. **VOSviewer (optional):** `out/works_vosviewer.json` is the network of works (one item per work, its topic as
   the cluster, the board's positions) and `out/topics_vosviewer.json` the graph of topics. Open either in
   [VOSviewer Online](https://app.vosviewer.com/) (Open → the file) or in VOSviewer. To share a link, put the file
   at a public address that allows cross-site reads (GitHub Pages does) and open
   `https://app.vosviewer.com/?json=` followed by its URL; `board --vosviewer-url URL` adds that link to the
   board's Network tab. The sample:

   ```text
   https://app.vosviewer.com/?json=https%3A%2F%2Frestrepo.github.io%2Fcitation-topics%2Fexamples%2Finspire-991924%2Fworks_vosviewer.json
   ```

**From an OpenAlex profile.** `examples/openalex_profile.py` does step 2 for any field: given an OpenAlex author id
or ORCID, it writes the author's works and every work citing them, with OpenAlex fields and subfields as
categories (standard library only; it uses `OPENALEX_API_KEY` when set, and works without it). Tried on the author
[A5010397600](https://openalex.org/A5010397600) (marine and coral-reef ecology): 90 works and 2,136 citing works,
12 topics in 6 areas at resolution 1; the result is the
[OpenAlex sample board](https://restrepo.github.io/citation-topics/examples/openalex-A5010397600/).

```bash
curl -O https://raw.githubusercontent.com/restrepo/citation-topics/main/examples/openalex_profile.py
python openalex_profile.py A5010397600 works.jsonl
```

## Method

1. **Network**: one node per work, an edge for each reference to another work of the list; undirected, each
   pair of works once.
2. **Topics**: Leiden maximizing modularity with a resolution parameter (the RB configuration model), with
   igraph's `community_leiden`, pass after pass until the quality stops rising. Works with no link stay
   unassigned; topics under `--min-size` are merged into their most linked neighbour; topics are numbered by
   size.
3. **Areas**: Leiden (leidenalg) on the graph of topics weighted by the links between them, with the links
   inside each topic as self-loops, the best of `--area-seeds` runs. Every topic is in one area.
4. **Second and third topics**: a work's links are counted by the topic at the other end; the second and third
   topics are the most linked other topics, scored as links to them / links to the main topic, capped at 1.
   Works the clustering left out take as main topic the one they link to most (`inferred`).
5. **Description** (`describe`): per topic, its distinctive terms (shares in the topic against the rest of the
   list), journals, categories, keywords, countries, years, linked topics and most cited works.
6. **Network of works** (`board`, `export --works`, up to `--network-max` works, 10,000 by default): each topic
   is a disc with area proportional to its works, placed by the links between topics and pushed apart until none
   overlap; inside it, the topic's works are placed by the links among them (Fruchterman-Reingold, igraph).
   Comfortable up to 10,000 works and about 50 topics (the default limit); usable to about 20,000 works
   (`--network-max 20000`), where zooming gets slower and the board weighs about 5 MB. Above 150,000 links the
   board starts with the links hidden. Measured sizes and times are in the `community-graph` skill.
7. **Names**: written by Claude from the description (skill `topic-namer`), never by the package. `compare`
   carries names over from an earlier run: a topic sharing 60% of its works both ways with an earlier one keeps
   its name.

The same input, parameters and versions of igraph and leidenalg give the same topics; `meta.json` records them
with the MD5 of the input.

**Checked against inspireauth10.** On its 1,872,221 INSPIRE papers (44 million citation links) with the
defaults, `run` gives the same network, the same 304 topics and 6 areas, the same Leiden quality and the same
second and third topics as inspireauth10's own scripts, array for array (igraph 1.0.0, leidenalg 0.12.0, numpy
2.5.4, Python 3.13). It took 45 minutes and more than 4 GB of memory; `describe` with titles, keywords,
journals, categories and countries took 10 minutes and 4.4 GB.

## Output

| File | Content |
|---|---|
| `works.jsonl.gz` | per work: `id`, `topic`, `area` (null if none), `topics` (three, with `score` and `links`), `n_links`, `inferred` |
| `topics.json` | per topic: area, name, works, links inside and out, `share_inside`, years, most cited works |
| `areas.json` | per area: name, works, topics, links, `share_inside` |
| `meta.json` | parameters, versions, input MD5, network counts, optional fields present, quality |
| `dossier.json` | description of each topic and area (`describe`) |
| `topics_vosviewer.json`, `topics_map.txt`, `topics_network.txt` | graph of topics for VOSviewer (`export`) |
| `works_vosviewer.json` | network of works for VOSviewer: one item per work with a link, cluster = its topic, the board's positions (`export --works`) |
| `topics.csv`, `works_topics.csv.gz` | the same as tables (`export`) |
| `board.html` | self-contained board: Overview always, then Timeline, Works, Network (up to 10,000 works), highlight, Countries and Authors as the fields allow, and About (`board`) |
| `network.npz`, `clusters.npz` | arrays reused by the other commands |

## Claude skills

`citation-topics skills install` copies into `./.claude` (or `~/.claude` with `--user`):

- `topics-run`: checks the input, chooses the parameters, runs, describes and exports, and explains the result;
- `topic-namer`: names topics and areas from `dossier.json` and applies them, after carrying over earlier names;
- `topics-compare`: seeds (which run to keep) and earlier runs or time windows (fates, flows, names);
- `community-graph`: the whole path from an INSPIRE-HEP profile or any list of works to the community graph
  (topics as labelled clusters of real works, the board, the VOSviewer file and link), with the size limits;
- `topics-board`: builds the board with the tabs the fields allow and says which fields would add the others;
- the agent `topic-namer`, which names a batch of topics when the skill hands it one.

In Claude Desktop or Claude Code, ask for topics of a list of works and the skills take it from there.

## Development

```bash
python -m pip install -e ".[dev]"
python -m pytest
ruff check . && ruff format --check .
```

The site (`docs/`: the method page, the home page and the sample board with its VOSviewer files) is copied to
the `gh-pages` branch, served at <https://restrepo.github.io/citation-topics/>, by `.github/workflows/pages.yml` on
every merge to `main` that changes it.

Releases are published to PyPI from GitHub Actions when a `v*` tag is pushed; see [PUBLISHING.md](PUBLISHING.md)
and [CHANGELOG.md](CHANGELOG.md).
