Skip to content

Package architecture

This page describes how the code is organized and why.

Layout

src/fpgrowth/
├── __init__.py    # public API re-exports and __version__
├── __main__.py    # enables `python -m fpgrowth`
├── core.py        # FPNode, FPTree, mine_frequent_itemsets
├── io.py          # database parsing and results serialization
└── cli.py         # argparse command-line interface

The package uses a src/ layout: the importable code lives under src/ and the tests live outside it. This keeps imports honest — the tests always import the installed (or path-injected) package rather than a stray local module.

Module boundaries

  • core.py contains the algorithm and nothing else. It knows about transactions as iterables of hashable items and returns a dictionary of itemsets to counts. It performs no file I/O and reads no command-line arguments.
  • io.py owns the text formats. It converts between files and the plain Python objects that core.py consumes and produces.
  • cli.py is a thin orchestration layer: parse arguments, call io then core then io, print a summary. All real logic lives in the other modules.
  • __init__.py is the stable public surface. It re-exports the supported names and defines __version__.

This separation means you can use the algorithm without touching files, and you can reuse the parsing code without mining.

Data flow

file ──read_database──▶ list[frozenset] ──mine_frequent_itemsets──▶ dict[frozenset, int]
                                                                        │
                                            file ◀──write_frequent_itemsets

The command-line interface wires exactly these three steps together and adds progress messages.

Design trade-offs

  • Pure Python, standard library only. The package is easy to install and audit, at the cost of raw speed. There is no C extension or optional fast path.
  • Recursive mining. The implementation mirrors the algorithm's definition directly, which keeps it readable, but recursion depth is bounded by the longest frequent itemset.
  • Weighted conditional trees. Prefix paths are inserted with a weight equal to the count of the item node, so the conditional tree is built in one pass instead of replaying every transaction.

Testing strategy

The suite in tests/ combines targeted unit tests with a brute-force oracle: tests/helpers.py enumerates every subset of the item universe and counts its support, and the tests assert that the FP-Growth implementation agrees. The randomized databases are seeded, so the tests are reproducible.

A dedicated regression test covers the historical inconsistent-ordering bug described in How FP-Growth works.