File size: 2,151 Bytes
8158335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from datetime import date, timedelta


def list_files(startpath):
    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, "").count(os.sep)
        indent = " " * 4 * (level)
        print("{}{}/".format(indent, os.path.basename(root)))
        subindent = " " * 4 * (level + 1)
        for f in files:
            print("{}{}".format(subindent, f))


def load_configs(configs_file_path):
    """Load a configuration file containing keys and secrets and return them as a dictionary.

    Args:
        config_file_path (str): The path to the configuration file to load.

    Returns:
        dict: A dictionary containing the keys and secrets loaded from
    """
    with open(configs_file_path) as f:
        configs = json.load(f)
    return configs


def wilson_score_interval(obs, conf_level=0.95):
    import math
    from scipy import stats

    """
    Calculates the Wilson score interval for a given list of observations.
    
    Args:
        obs (list): A list of observations (0s and 1s).
        conf_level (float): The desired confidence level (default: 0.95).
    
    Returns:
        tuple: A tuple with the lower and upper bounds of the confidence interval.
    """
    n = len(obs)
    if n == 0:
        return None
    z = stats.norm.ppf(1 - (1 - conf_level) / 2)
    phat = sum(obs) / n
    term = z * math.sqrt(phat * (1 - phat) / n + z * z / (4 * n * n))
    lower_bound = (phat + z * z / (2 * n) - term) / (1 + z * z / n)
    upper_bound = (phat + z * z / (2 * n) + term) / (1 + z * z / n)
    return round(lower_bound, 2), round(upper_bound, 2)


def get_yesterday_date():
    today = date.today()
    yesterday = today - timedelta(days=1)
    return yesterday


def load_data_from_file(filename):
    """
    Loads data from a file in JSON format if the file exists.

    Args:
        filename (str): The name of the file to load.

    Returns:
        The loaded data, or None if the file doesn't exist.
    """
    if os.path.isfile(filename):
        with open(filename, "r") as f:
            data = json.load(f)
        return data
    else:
        return None