~dricottone/filters

ref: eb78014ec6905e49d62555285d53f42741d6a7c4 filters/filter/internals.py -rw-r--r-- 2.4 KiB
eb78014eDominic Ricottone Clean up 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3

import sys
import random

from typing import *

def _try_get_float(
    mapping: Dict,
    key: str,
    *,
    default: Optional[float] = None,
) -> Optional[float]:
    if key in mapping:
        return float(mapping[key])
    else:
        return default

def _read_stdin() -> Iterator[float]:
    try:
        for line in sys.stdin.readlines():
            line = line.strip()
            if len(line) == 0:
                continue
            try:
                yield float(line.strip())
            except:
                _print_invalid_data(line)
    except KeyboardInterrupt:
        sys.stdout.write("\n")

def _read_file(filename) -> Iterator[float]:
    try:
        with open(filename, 'r') as f:
            for line in f.readlines():
                line = line.strip()
                if len(line) == 0:
                    continue
                try:
                    yield float(line)
                except:
                    _print_invalid_data(line)
    except OSError:
        _print_invalid_file(filename)

def _get_raw_data(filenames: List[str]) -> List[float]:
    raw_data: List[float] = list()

    if len(filenames) == 0:
        raw_data.extend(list(_read_stdin()))
    else:
        for filename in filenames:
            if filename == '-':
                raw_data.extend(list(_read_stdin()))
            else:
                raw_data.extend(list(_read_file(filename)))

    return raw_data

def noise(data: List[float]) -> List[float]:
    """
    Introduce random noise (r in [-1,1]) to a set of data points.
    """
    def _noise_iter(data: List[float]) -> Iterator[float]:
        for d in data:
            yield d + random.uniform(-1,1)
    return list(_noise_iter(data))

def _print_help() -> None:
    _msg = "Usage: filter -m=METHOD DATA\n"
    sys.stdout.write(_msg)

def _print_version() -> None:
    _msg = "gap 1.0.0\n"
    sys.stdout.write(_msg)

def _print_usage() -> None:
    _msg = "Usage: filter -m=METHOD DATA\n"
    sys.stderr.write(_msg)

def _print_invalid_methodology(method: str) -> None:
    _msg = "{0}: Invalid methodology '{1}'\n".format(sys.argv[0], method)
    sys.stderr.write(_msg)

def _print_invalid_file(filename: str) -> None:
    _msg = "{0}: Invalid file '{1}'\n".format(sys.argv[0], filename)
    sys.stderr.write(_msg)

def _print_invalid_data(line: str) -> None:
    _msg = "{0}: Cannot convert '{1}' into numeric value\n".format(
        sys.argv[0], line,
    )
    sys.stderr.write(_msg)