Azaya89 commited on
Commit
93dd158
·
1 Parent(s): 439a6ec

refactor main app code

Browse files
Files changed (1) hide show
  1. app.py +43 -29
app.py CHANGED
@@ -2,28 +2,36 @@
2
  import pandas as pd, panel as pn
3
  import hvplot.pandas # noqa
4
 
5
- # Data loading and cleaning
6
- df = pd.read_csv('omoku_data.csv', index_col='Date', parse_dates=True)
 
 
 
 
 
 
7
 
8
- def clean_df(df):
9
- df = df[df['Remark'].isna()]
10
- return df.assign(Day=df.index.day_name())
11
 
12
- cleaned_df = clean_df(df)
 
13
 
14
- daily_average = cleaned_df['Power_time'].mean()
15
- max_power = cleaned_df['Power_time'].max()
16
- min_power = cleaned_df['Power_time'].min()
17
 
18
  days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
19
- weekly_group = cleaned_df.groupby('Day', sort=False)[['Power_time', 'Outages']].mean().reindex(days)
 
 
 
20
 
21
  # Plots
22
- box_plot = cleaned_df.hvplot.box(xlabel="Variables", ylabel="Number of hours", grid=True, title="Box Plot of Power Time and Outages")
23
- line_plot = cleaned_df.hvplot.line(y='Power_time', ylabel='Number of hours', title='Daily power supply')
24
- density_plot = cleaned_df.hvplot.kde('Power_time', xlabel='Number of hours', xlim=(0,24), yaxis=None, hover=False,
25
- title='Density distribution of power supply').opts(padding=(0,0))
26
- weekly_plot = weekly_group.hvplot.bar(stacked=True, rot=45, ylabel='Number of hours', title='Average power supply by week day')
27
 
28
  # Dashboard
29
  pn.extension('tabulator', sizing_mode="stretch_width")
@@ -44,6 +52,12 @@ styles = {
44
  }
45
 
46
  indicators = pn.FlexBox(
 
 
 
 
 
 
47
  pn.indicators.Number(
48
  value=daily_average,
49
  name="Average daily supply (Hrs)",
@@ -52,41 +66,41 @@ indicators = pn.FlexBox(
52
  styles=styles
53
  ),
54
  pn.indicators.Number(
55
- value=max_power,
56
- name="Highest daily supply (Hrs)",
57
  default_color="green",
58
- format=number_format,
59
  styles=styles,
60
  ),
61
  pn.indicators.Number(
62
- value=min_power,
63
- name="Lowest daily supply (Hrs)",
64
- default_color="red",
65
- format=number_format,
66
- styles=styles,
67
  ),
68
  )
69
 
70
  table = pn.widgets.Tabulator(df.head(10), sizing_mode="stretch_width", name="Table")
71
- tabs = pn.Tabs(('Daily total', line_plot), ('Weekly average', weekly_plot), ('Box plot', box_plot), ('Density distribution', density_plot),
 
72
  styles=styles, sizing_mode="scale_both", margin=10)
73
- logo = '<img src="https://panel.pyviz.org/_static/logo_stacked.png" width=180 height=150>'
74
 
75
  text = f"""This is a [Panel](https://panel.holoviz.org) dashboard that shows the number of hours of power supply in Omoku, Rivers State, Nigeria.
76
 
77
  Omoku is divided into three (3) areas in terms of power supply and this data was collected at one of the three areas.
78
 
79
- The data was collected by calculating the total number of hours during which there was no power and subtracting it from 24 hours.
80
 
81
  This data was collected consecutively over a period of `{len(df)}` days."""
82
-
83
  template = pn.template.FastListTemplate(
84
  title="Power supply dashboard",
85
  sidebar=[logo, text],
86
  sidebar_width=250,
87
- main=[pn.Column('# Data Summary', indicators, '# Sample Data', table, '# Plots', tabs, sizing_mode="stretch_both")],
88
  main_layout=None,
89
  accent=ACCENT,
90
  )
91
 
92
- template.servable()
 
2
  import pandas as pd, panel as pn
3
  import hvplot.pandas # noqa
4
 
5
+ # Data loading and manipulation
6
+ df = pd.read_csv(
7
+ "omoku_data.csv",
8
+ parse_dates=['Date'],
9
+ index_col="Date",
10
+ dtype={"Remark":pd.api.types.CategoricalDtype()}
11
+ )
12
+ df['Day'] = df.index.day_name().astype('category')
13
 
14
+ recorded = df[df['Power_time'].notna()]
15
+ record_days = len(recorded)
 
16
 
17
+ days_with_power = len(recorded[recorded['Power_time'] != 0])
18
+ power_issues = recorded[recorded['Remark'].str.contains('Repairs|Maintenance', case=False, na=False)]
19
 
20
+ percent_avai = round(days_with_power/record_days *100, 1)
21
+ daily_average = recorded['Power_time'].mean()
 
22
 
23
  days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
24
+ weekly_group = recorded.groupby('Day', sort=False, observed=True)[['Power_time']].mean().reindex(days)
25
+
26
+ monthly_avg = recorded['Power_time'].resample("ME").mean().reset_index().set_index("Date")
27
+ monthly_avg['Month'] = monthly_avg.index.month_name()
28
 
29
  # Plots
30
+ line_plot = recorded.hvplot.line(y='Power_time', ylabel='Number of hours', title='Total daily power supply')
31
+ density_plot = recorded['Power_time'].hvplot.kde('Power_time', xlabel='Number of hours', xlim=(0,24), yaxis=None, hover=False,
32
+ title='Density distribution of power supply')
33
+ weekly_plot = weekly_group.hvplot.bar(rot=45, ylabel='Number of hours', title='Average power supply by week day')
34
+ monthly_plot = monthly_avg.hvplot.bar(rot=45, hover_tooltips=['Month', 'Power_time'], hover_cols=['Month'], title="Monthly average power supply")
35
 
36
  # Dashboard
37
  pn.extension('tabulator', sizing_mode="stretch_width")
 
52
  }
53
 
54
  indicators = pn.FlexBox(
55
+ pn.indicators.Number(
56
+ value=record_days,
57
+ name="Number of days recorded",
58
+ default_color="blue",
59
+ styles=styles,
60
+ ),
61
  pn.indicators.Number(
62
  value=daily_average,
63
  name="Average daily supply (Hrs)",
 
66
  styles=styles
67
  ),
68
  pn.indicators.Number(
69
+ value=percent_avai,
70
+ name="Power availability rate",
71
  default_color="green",
72
+ format=f"{number_format}%",
73
  styles=styles,
74
  ),
75
  pn.indicators.Number(
76
+ value=len(power_issues),
77
+ name="Days in repairs or maintenance",
78
+ default_color="red",
79
+ styles=styles,
 
80
  ),
81
  )
82
 
83
  table = pn.widgets.Tabulator(df.head(10), sizing_mode="stretch_width", name="Table")
84
+ tabs = pn.Tabs(('Daily total', line_plot), ('Monthly average', monthly_plot),
85
+ ('Weekly average',weekly_plot), ('Density distribution', density_plot),
86
  styles=styles, sizing_mode="scale_both", margin=10)
87
+ logo = '<img src="https://panel.holoviz.org/_static/logo_stacked.png" width=180 height=150>'
88
 
89
  text = f"""This is a [Panel](https://panel.holoviz.org) dashboard that shows the number of hours of power supply in Omoku, Rivers State, Nigeria.
90
 
91
  Omoku is divided into three (3) areas in terms of power supply and this data was collected at one of the three areas.
92
 
93
+ The data was collected by calculating the total number of hours during which there was no power and subtracting it from 24 hours.
94
 
95
  This data was collected consecutively over a period of `{len(df)}` days."""
96
+
97
  template = pn.template.FastListTemplate(
98
  title="Power supply dashboard",
99
  sidebar=[logo, text],
100
  sidebar_width=250,
101
+ main=[pn.Column('# Data Summary', indicators, '# Sample Data', table, '# Plots', tabs)],
102
  main_layout=None,
103
  accent=ACCENT,
104
  )
105
 
106
+ template.servable()