generated from TheOpenScienceNerd/tosn_python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_utility.py
More file actions
123 lines (102 loc) · 3.87 KB
/
app_utility.py
File metadata and controls
123 lines (102 loc) · 3.87 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Basic utility functions for loading and formatting data in the app.
"""
import pandas as pd
def remove_daytype_formatting(df):
"""
Standardizes day type DataFrame for internal use.
Removes formatting (e.g., emojis) from column names and values
used for display in the Streamlit app, converting them to a
standard format required by the simulation engine.
Parameters
----------
df : pd.DataFrame
DataFrame containing day type probabilities, potentially with
display formatting. Expected columns include 'Type of sales day'
and potentially others depending on the display format.
Returns
-------
pd.DataFrame
DataFrame with standardized column names ('day_type') and values
('good', 'fair', 'poor').
"""
column_names = {"Type of sales day": "day_type"}
day_recodings = {
"day_type": {"☀️ Good": "good", "⛅ Fair": "fair", "⛈️ Poor": "poor"}
}
return df.rename(columns=column_names).replace(day_recodings)
def remove_demand_formatting(df):
"""
Standardizes demand DataFrame for internal use.
Removes formatting (e.g., emojis) from column names used for display
in the Streamlit app, converting them to a standard format required
by the simulation engine.
Parameters
----------
df : pd.DataFrame
DataFrame containing demand probabilities for different day types,
potentially with display formatting in column names. Expected columns
include '📰 Demand', '☀️ Good', '⛅ Fair', '⛈️ Poor'.
Returns
-------
pd.DataFrame
DataFrame with standardized column names ('demand', 'good', 'fair',
'poor').
"""
column_names = {
"📰 Demand": "demand",
"☀️ Good": "good",
"⛅ Fair": "fair",
"⛈️ Poor": "poor",
}
return df.rename(columns=column_names)
def fetch_demand(path):
"""
Fetches and formats demand probability data from a CSV file.
Reads the demand distribution data from the specified path,
renames columns for user-friendly display in Streamlit (e.g., adding
emojis), and caches the result to improve performance.
Parameters
----------
path : str or Path-like
The file path to the CSV containing demand probability data.
The CSV should have columns like 'demand', 'good', 'fair', 'poor'.
Returns
-------
pd.DataFrame
DataFrame containing the demand data with columns formatted for display
('📰 Demand', '☀️ Good', '⛅ Fair', '⛈️ Poor').
"""
# Fetch basic data frame containing input data
column_names = {
"demand": "📰 Demand",
"good": "☀️ Good",
"fair": "⛅ Fair",
"poor": "⛈️ Poor",
}
return pd.read_csv(path).rename(columns=column_names)
def fetch_day_type(path):
"""
Fetches and formats day type probability data from a CSV file.
Reads the day type probability data from the specified path,
recodes values and renames columns for user-friendly display in
Streamlit (e.g., adding emojis), and caches the result to improve
performance.
Parameters
----------
path : str or Path-like
The file path to the CSV containing day type probability data.
The CSV should have columns like 'day_type' and 'prob', with
'day_type' values such as 'good', 'fair', 'poor'.
Returns
-------
pd.DataFrame
DataFrame containing the day type data with values and columns
formatted for display ('Type of sales day', emojis for day types).
"""
# Fetch basic data frame containing input data
column_names = {"day_type": "Type of sales day"}
day_recodings = {
"day_type": {"good": "☀️ Good", "fair": "⛅ Fair", "poor": "⛈️ Poor"}
}
df = pd.read_csv(path).replace(day_recodings).rename(columns=column_names)
return df