Skip to content

API reference

Corpus

Bases: Model

Documents from a DataFrame (data) or a file (data_file), as a tomotopy corpus.

load()

Tokenize the documents (and concatenate n-grams) into a tomotopy corpus.

TopicModel

Bases: Model

A topic model whose artifacts live under output_dir.

File names start with model_id (<MODEL_TYPE>_<name>_k(<k>)), so models that differ in name or k can share one directory.

num_topics property

Number of topics in the model

model_summary_file property

One JSON line per trained model, shared by every model in output_dir.

train(corpus=None)

Train on corpus (or the one already set) and evaluate coherence.

Nothing is written; call :meth:save, :meth:summarize, and :meth:plot for artifacts.

save()

Write the model file, log-likelihood trace, distributions, and top words.

summarize()

Write the tomotopy summary and append this model's metrics to the summary jsonl.

plot()

Write the log-likelihood plot, topic word clouds, and LDAvis (if installed).

load()

Load the model file and saved distributions written by :meth:save for model_id.

infer(corpus, output_file=None, iterations=100, tolerance=-1, num_workers=0, together=False)

Infer topic distributions for corpus and save them (with log-likelihoods).

LdaConfig

Bases: Model

Keyword arguments for tomotopy.LDAModel.

TrainSummaryConfig

Bases: Model

Keyword arguments for tomotopy.LDAModel.summary.

WordPrior

Bases: Model

Seed words per topic, from prior_data and/or a YAML or JSON data_file.

add(id_, prior)

Add a prior to the word prior

load(missing_ok=False)

Load words from data_file and prior_data.

A missing data_file raises FileNotFoundError unless missing_ok (used by :meth:save to create the file).

save()

Save words to data_file: JSON for a .json file, YAML otherwise.

WordCloud

Bases: Model

Keyword arguments for wordcloud.WordCloud; mask may be an image path.

save_image_pages(images, output_dir, output_file_format, num_images_per_page, num_cols, num_rows=None, titles=None, title_fontsize=10, title_color='black', figsize=None, width_multiple=4, height_multiple=2, dpi=100)

Lay images out in grids of num_images_per_page and save one file per page.

Raises ValueError when an explicit num_rows x num_cols grid cannot hold a page.

Workflow steps: small functions that chain a load, corpus, and model through HyFI steps:.

Each step returns what the next one takes as its first argument (HyFI pipe). They work the same when called from plain Python.

build_corpus(data=None, *, corpus)

Tokenize data (or corpus.data_file when no data is piped in) into corpus.

train(corpus, *, model)

Train model on corpus and save the model file and its distributions.

sweep(corpus, *, model, k, alpha=None, eta=None)

Train, save, summarize, and plot one model per (k, alpha, eta) combination.

Models are named <name>(0), <name>(1), ... in sweep order, and every model's summary is appended to <output_dir>/model-summary.jsonl. Returns the summaries.

infer(corpus, *, model, output_file=None, iterations=100, tolerance=-1, num_workers=0, together=False)

Load model (as saved under its output_dir) and infer topics for corpus.