Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .github/workflows/ci.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/gui-build.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/mypy.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/pypi.yaml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
204 changes: 91 additions & 113 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,33 @@

# ECOFFitter

**ECOFFitter** is a Python tool for fitting interval regression models
to censored Minimum Inhibitory Concentration (MIC) data.\
It provides a command-line interface (CLI) to process input MIC
distributions, estimate epidemiological cutoffs (ECOFFs), and generate
model outputs and diagnostic plots.
**ECOFFitter** fits interval-censored normal models to Minimum Inhibitory
Concentration (MIC) observations and estimates epidemiological cutoff values
(ECOFFs). It supports a single distribution, a finite mixture, and automatic
selection of the number of mixture components. Text and PDF reports can include
aggregate results and optional per-assay results.

The package supports:
Demo input files are in `demo_files/`.

- interval-censored, left-censored,
right-censored, and uncensored MIC data
- mixed datasets with variable
censoring
- automated output files (PDF reports, text outputs, plots)
- configuration via parameter files
## Installation

Demo input files are provided in `demo_files/` to illustrate basic use.

---

## 🛠 Installation

### Install from source

Assuming in project directory (after git cloning):
Install from a local checkout:

```bash
pip install -e .
```

---

## 📥 Input

### 1. MIC Data Input File

The MIC dataset describes the distribution of Minimum Inhibitory Concentration (MIC) values and the number of isolates observed at each dilution.
## Input data

Alternatively, an array or single-column file of observed MICs can be supplied (ie 1 row per sample).
Input can be a CSV, TSV, TXT, XLSX, or XLS file, or a DataFrame, array-like
sequence, or mapping when using the Python API. MIC values may be exact (for
example `0.25`), left-censored (such as `<=0.125`), or right-censored (such as
`>64`). Ratio values such as `1/2` are accepted; the numerator is used as the
MIC value.

It must be a **tabular file** in one of these formats:

- `.csv`
- `.tsv`
- `.txt`
- `.xlsx` / `.xls`

#### Required columns

| Column | Type | Description |
| -------------- | --------------- | ---------------------------------------------------------------------------------------------------------- |
| `MIC` | float or string | MIC dilution (e.g., `0.125`, `0.25`, `1`, `2`, …). Can include censored values such as `<=0.125` or `>64`. |
| `observations` | integer | Number of isolates observed at that MIC dilution. |

or

A single column/array of observed MIC values.

#### Example (from `demo_files/censored.txt`)
A single-column input contains one MIC per row. Duplicate MIC values are
aggregated into counts. A canonical single-fit table has `MIC` and
`observations` columns:

```text
MIC observations
Expand All @@ -75,22 +44,21 @@ MIC observations
>8 2196
```

- censored values (e.g >16) are automatically detected and handled

### 2. Parameter File (`--params`)
For aggregate and per-assay analyses, use a table with a `MIC` column and one
or more assay columns. Counts across assay columns are summed for the aggregate
fit. Each assay is fitted separately when individual fits are enabled.

This is an **optional** configuration file (YAML, TXT, or `key=value` list) that overrides CLI arguments.
Supported input extensions are `.csv`, `.tsv`, `.txt`, `.xlsx`, and `.xls`.
For Excel files, the first worksheet is used unless a sheet is selected through
the Python API.

Typical fields include:
## Configuration

| Key | Description |
| ------------------ | ----------------------------------------------------------------------------- |
| `dilution_factor` | Fold-change between MIC dilutions (e.g., `2`). |
| `distributions` | Number of Gaussian mixture components. |
| `boundary_support` | How many dilution steps to extend upper and/or lower tails for censored data. |
| `percentile` | Percentile used to compute the ECOFF (e.g., `99`). |
The optional `--params` file can be YAML or TXT (`key=value`). Values in this
file override the corresponding manually supplied parameters. Supported keys
are `dilution_factor`, `distributions`, `boundary_support`, and `percentile`.

#### Example (`demo_files/params.txt`)
TXT example (`demo_files/params.txt`):

```text
dilution_factor=2
Expand All @@ -99,80 +67,90 @@ boundary_support=1
percentile=99
```

or YAML:
YAML example:

```text
```yaml
dilution_factor: 2
distributions: 2
boundary_support: 1
percentile: 99
```

## 📤 Output

The tool produces one or more of the following:

---

### 1. Text Output
Set `boundary_support` to `None` in a parameter file to use unbounded tails for
censored observations.

If `--outfile` ends in `.txt`, the tool writes:
## Python API

- ECOFF estimate
- fitted mean & variance (per component)
- mixture weights (if multiple distributions)
Fit one dataset directly:

### 2. PDF Report
```python
from ecoff_fitter import ECOFFitter

If `--outfile` ends in `.pdf`, the tool writes:

- histogram of observed MICs
- fitted distribution curve(s)
- ECOFF location marker
- fitted mean & variance (per component)
- mixture weights (if multiple distributions)
fitter = ECOFFitter(input="demo_files/censored.txt", distributions=1)
result = fitter.generate(percentile=99)
print(result)
```

## 🚀 Command-Line Usage
Run the aggregate and optional per-assay workflow:

Once installed, you can call the CLI.
```python
from ecoff_fitter import run_ecoff_analysis

Example using demo files:
run = run_ecoff_analysis(
"demo_files/multi_assay.txt",
distributions=2,
fit_individual=True,
percentile=99,
)

```bash
ecoff-fitter --input demo_files/censored.txt --params demo_files/params.txt --outfile demo_files/output.txt
print(run.global_report.to_text())
for assay, report in run.individual_reports.items():
print(report.to_text(label=assay))
```

Instead of using a parameter file, you can also specify parameters directly.
Set `distributions="auto"` in `run_ecoff_analysis` to select a component
count by the AIC or BIC criterion. `max_distributions` sets the largest count
to consider; `criterion`, `min_delta`, `min_pi`, and `min_separation` control
the selection checks. The selected aggregate count is available as
`run.selected_k`.

Usage:
## Command-line usage

The CLI accepts a dataset with a `MIC` column and either `observations` or one
or more assay columns:

```bash
ecoff-fitter [-h] --input INPUT [--params PARAMS]
[--dilution_factor DILUTION_FACTOR]
[--distributions DISTRIBUTIONS] [--boundary_support BOUNDARY_SUPPORT]
[--percentile PERCENTILE] [--outfile OUTFILE] [--verbose]
ecoff-fitter \
--input demo_files/multi_assay.txt \
--distributions 2 \
--percentile 99 \
--fit_individual \
--outfile demo_files/output.pdf
```

---

## ⚙️ CLI Parameters

### CLI Options

| Option | Argument | Description | Default |
| -------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------- |
| `-h`, `--help` | — | Show help message and exit. | — |
| `--input` | `INPUT` | Path to the input MIC dataset (CSV, TSV, XLSX, or XLS). Must contain columns `MIC` and `observations`. | `None` |
| `--params` | `PARAMS` | Optional parameter file (`YAML`, `TXT`, or `key=value` list). Defines `dilution_factor`, `distributions`, and `boundary_support`. Overrides CLI flags if provided. | `None` |
| `--dilution_factor` | `DILUTION_FACTOR` | Dilution factor | `2` |
| `--distributions` | `DISTRIBUTIONS` | Number of Gaussian mixture components to fit. | `1` |
| `--boundary_support` | `boundary_support` | Extra upper- and lower-dilution steps for censored MICs (set to `None` to disable). | `1` |
| `--percentile` | `PERCENTILE` | Percentile used to compute the ECOFF (0–100). | `99.0` |
| `--outfile` | `OUTFILE` | Path to save ECOFF results (`.txt` or `.pdf`). | `None` |
| `--verbose` | — | Print detailed model information and parameters. | `False` |

---

## 📄 License
Run `ecoff-fitter --help` for the complete option list. The same CLI is
available as `python -m ecoff_fitter`.

| Option | Default | Description |
| --- | --- | --- |
| `--input INPUT` | Required | Input MIC dataset. |
| `--params PARAMS` | None | YAML or TXT parameter file; its values override corresponding command-line values. |
| `--dilution_factor N` | `2` | Ratio between adjacent MIC dilution steps. |
| `--distributions K` | `1` | Number of normal components to fit. The CLI accepts an integer; automatic selection is available in the Python workflow API. |
| `--boundary_support N` | `1` | Number of dilution steps used to extend censoring bounds. Use `None` in a parameter file for unbounded tails. |
| `--percentile P` | `99.0` | Percentile used to calculate the ECOFF; must be between 0 and 100. |
| `--fit_individual` | Off | Fit each assay column as well as the aggregate observations. |
| `--density` | Off | Plot probability density instead of counts. |
| `--outfile PATH` | None | Write a text report (`.txt`) or PDF report (`.pdf`). |
| `--verbose` | Off | Include model details in printed text output. |

## Output

The results are printed to the terminal. With `--outfile`, the CLI also writes a
text or PDF report. PDF reports show observed MIC intervals, fitted component
curves, and the ECOFF marker. When per-assay fits are enabled, the report
contains the aggregate fit and individual assay fits.

## License

MIT License.
20 changes: 10 additions & 10 deletions demo_files/censored.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

MIC x1

<=0.03 89
0.06 223
0.12 346
0.25 150
0.5 60
1 2
2 3
4 12
8 55
>8 67
<=0.03 593
0.06 23
0.12 4
0.25 8
0.5 5
1 6
2 2
4 2
8 1
>8 0

Empty file modified demo_files/demo_input.xlsx
100644 → 100755
Empty file.
Empty file modified demo_files/multi_assay.txt
100644 → 100755
Empty file.
Binary file modified demo_files/output.pdf
100644 → 100755
Binary file not shown.
Empty file modified demo_files/output.txt
100644 → 100755
Empty file.
Empty file modified demo_files/params.txt
100644 → 100755
Empty file.
Empty file modified demo_files/uncensored.txt
100644 → 100755
Empty file.
Empty file modified demo_files/unimodal.txt
100644 → 100755
Empty file.
Empty file modified env.yml
100644 → 100755
Empty file.
Loading
Loading