~dricottone/filters

ref: 8479aea2db66090f2b0db01a1c42593975a3ba0e filters/rng/notrandom.py -rw-r--r-- 1.8 KiB
8479aea2Dominic Ricottone Added rng package, convolve + kalman filters 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

"""rng notrandom [OPTIONS]
Generate non-random data.

Options:
  -d, --delta    velocity of state per time unit [Default: 0]
  -i, --initial  initial state [Default: 0]
  -n, --number   number of data points to generate [Default: 10]
"""

import sys
import itertools
from typing import Callable, List, Dict, Iterator

def cli_wrapper(**data: Dict):
    """Handler for the uniform distribution. Checks and cleans given options,
    and performs optional reporting.
    """
    _number = data["number"] if data["number"] is not None else 10
    _init_state = data["initial"] if data["initial"] is not None else 0.0
    _init_velocity = data["delta"] if data["delta"] is not None else 0.0
    _acceleration = lambda x: x #constant acceleration

    _distribution = distribution(
        _init_state,
        _init_velocity,
        _acceleration,
    )

    if data["report"]:
        sys.stdout.write(
            report_header(_init_state, _init_velocity, _acceleration)
        )
    if _number > 0:
        for number in itertools.islice(_distribution, _number):
            sys.stdout.write("{0:.4f}\n".format(number))

def distribution(
    init_state: float,
    init_velocity: float,
    acceleration: Callable[[float], float],
) -> Iterator[float]:
    state = init_state
    velocity = init_velocity

    while True:
        yield state
        state += velocity
        velocity = acceleration(velocity)

def report_header(
    init_state: float,
    init_velocity: float,
    acceleration: Callable[[float], float],
):
    _msg = (
        "Not random data",
        "  Initial value of {0:.4f}, changing {1:.4f} per time unit".format(
            init_state, init_velocity,
        ),
        "Data:",
        "========",
    )
    return "\n".join(_msg) + "\n"