Metadata-Version: 2.4
Name: fake-process-estimation
Version: 0.1.0
Summary: Time-based progress estimation heuristics for long-running tasks.
License-Expression: MIT
License-File: LICENSE
Author: GGN_2015
Author-email: neko@jlulug.org
Requires-Python: >=3.10
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Description-Content-Type: text/markdown

# Fake Process Estimation

`fake-process-estimation` is a small, dependency-free Python package for
displaying plausible progress when a task exposes only an estimated duration
and its elapsed time.

It is useful for indeterminate operations such as external commands, network
requests, model execution, file conversion, or legacy jobs that cannot report
real work units.

> A time-based estimate is a presentation heuristic, not a measurement of
> completed work. When real work units are available, use them instead.

## Features

- A minimal abstract interface for custom estimation strategies.
- A legacy piecewise linear/exponential estimator.
- A smooth S-curve estimator with configurable timing and shape.
- Configurable integer output ranges for progress bars and UI widgets.
- No runtime dependencies.
- Python 3.10 or newer.

## Installation

Install the package from the project directory:

```bash
pip install .
```

For development, install it in editable mode:

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

## Recommended Usage

`C_SCurveProcessEstimation` is the recommended time-only estimator. It starts
slowly, moves faster through the middle of the expected duration, and then
approaches the configured maximum without a sudden change in speed.

```python
import time

from fake_process_estimation import C_SCurveProcessEstimation


estimator = C_SCurveProcessEstimation(
    estimated_time_cost=30.0,
    lowest_value=0,
    highest_value=1000,
    progress_at_estimated_time=0.8,
    shape=2.0,
)

started_at = time.monotonic()

while task_is_running():
    elapsed = time.monotonic() - started_at
    estimated_progress = estimator.estimate(elapsed)
    update_progress_bar(estimated_progress / 10)
    time.sleep(0.1)

# Only the real task completion event should display 100%.
update_progress_bar(estimator.getHighestValue() / 10)
```

With the default curve parameters, the approximate normalized progress is:

| Elapsed / estimated time | Estimated progress |
| ---: | ---: |
| `0.00` | `0%` |
| `0.25` | `20%` |
| `0.50` | `50%` |
| `1.00` | `80%` |
| `2.00` | `94%` |
| `3.00` | `97%` |

## How the S-Curve Works

Let:

- `t` be the elapsed time;
- `T` be the estimated total duration;
- `r = t / T` be the elapsed-time ratio;
- `q` be `progress_at_estimated_time`;
- `s` be `shape`.

The normalized progress is calculated with a log-logistic curve:

```text
                    r^s
progress(r) = -----------------
              r^s + (1 - q) / q
```

This gives the curve several useful properties:

- `progress(0) = 0`;
- `progress(1) = q`;
- progress is monotonic and bounded;
- the curve is smooth at the estimated duration;
- progress approaches the maximum as elapsed time increases.

The estimator reserves `highest_value` for actual task completion when
`estimated_time_cost` is greater than zero. For example, a range of `0..100`
produces estimated values no greater than `99`; the caller should set `100`
after the task really finishes.

### Tuning the Curve

`progress_at_estimated_time` controls how much progress is shown at the
estimated duration:

- `0.7` is conservative and leaves a larger tail.
- `0.8` is the default and works well for general UI feedback.
- `0.9` is optimistic and reaches high values earlier.

`shape` controls the curve profile:

- values below `1.0` move quickly at the beginning;
- `1.0` produces a simple saturating curve;
- values above `1.0` produce a stronger S-shape;
- `2.0` is the default and gives a balanced slow-fast-slow transition.

## Legacy Estimator

`C_ProcessEstimation` preserves the original algorithm:

- before the estimated duration, progress increases linearly from `0%` to
  `70%` of the configured range;
- after the estimated duration, progress follows an exponential tail toward
  `100%`.

```python
from fake_process_estimation import C_ProcessEstimation


estimator = C_ProcessEstimation(
    estimated_time_cost=30,
    lowest_value=0,
    highest_value=100,
)

print(estimator.estimate(15))  # About 35
print(estimator.estimate(30))  # About 70
print(estimator.estimate(60))  # About 85
```

This strategy is simple, but the rate of change drops abruptly when elapsed
time reaches the estimate. The S-curve estimator avoids that visible corner.

## API

### `I_ProcessEstimation`

Abstract base class implemented by all estimators.

```python
class I_ProcessEstimation(ABC):
    def getLowestValue(self) -> int: ...
    def getHighestValue(self) -> int: ...
    def estimate(self, time_cost_now: float) -> int: ...
```

### `C_SCurveProcessEstimation`

```python
C_SCurveProcessEstimation(
    estimated_time_cost: float,
    lowest_value: int = 1,
    highest_value: int = 999,
    progress_at_estimated_time: float = 0.8,
    shape: float = 2.0,
)
```

- `estimated_time_cost` must be finite and non-negative.
- `progress_at_estimated_time` must be strictly between `0` and `1`.
- `shape` must be finite and positive.
- Reversed minimum and maximum values are automatically swapped.
- An estimated duration of zero immediately returns `highest_value`.

### `C_ProcessEstimation`

```python
C_ProcessEstimation(
    estimated_time_cost: int,
    lowest_value: int = 1,
    highest_value: int = 999,
)
```

## Implementing a Custom Strategy

New strategies can derive from `I_ProcessEstimation`:

```python
from fake_process_estimation import I_ProcessEstimation


class WorkUnitEstimation(I_ProcessEstimation):
    def __init__(self, total_units: int) -> None:
        if total_units <= 0:
            raise ValueError("total_units must be positive")
        self._total_units = total_units

    def getLowestValue(self) -> int:
        return 0

    def getHighestValue(self) -> int:
        return self._total_units

    def estimate(self, completed_units: float) -> int:
        return max(0, min(int(completed_units), self._total_units))
```

The parameter name in the interface reflects the built-in time-based
estimators, but a custom implementation may interpret the numeric input as a
different progress signal.

## Better Alternatives to Time-Only Estimation

Use the strongest progress signal available, in this order:

1. **Completed work units:** report `completed / total` for records, bytes,
   frames, files, or iterations.
2. **Weighted phases:** assign weights to known stages such as download,
   parsing, processing, and writing.
3. **Measured throughput:** estimate remaining time from a rolling or
   exponentially weighted average of units processed per second.
4. **Historical calibration:** learn expected durations from previous runs with
   similar input sizes and environments.
5. **Time-only curves:** use `C_SCurveProcessEstimation` when no observable work
   signal exists.

A strong production design often combines weighted phases with measured work
inside each phase. This remains accurate when tasks run faster or slower than
their original time estimate, while the S-curve remains a useful fallback for
opaque stages.

## License

This project is licensed under the MIT License. See `LICENSE` for details.

