ppsingh commited on
Commit
b5fab4b
1 Parent(s): f435b72

policy download

Browse files
Files changed (1) hide show
  1. appStore/policyaction.py +132 -5
appStore/policyaction.py CHANGED
@@ -1,5 +1,6 @@
1
  # set path
2
- import glob, os, sys;
 
3
  sys.path.append('../utils')
4
 
5
  #import needed libraries
@@ -79,7 +80,28 @@ def to_excel(df):
79
  'Policies_Plans Score','Conditional Score'],inplace=True)
80
  action_hits = action_hits.sort_values(by=['Action Score'], ascending=False)
81
  action_hits = action_hits.reset_index(drop=True)
82
- action_hits.to_excel(writer,index=False,sheet_name = 'Action')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # hits = hits.drop(columns = ['Target Score','Netzero Score','GHG Score'])
85
  workbook = writer.book
@@ -176,7 +198,7 @@ def action_display():
176
 
177
 
178
  st.caption("Filter table to select rows to keep for Action category")
179
- filter_dataframe(hits)
180
  # filtered_df = filtered_df[filtered_df.keep == True]
181
  # st.write('Explore the data')
182
  # AgGrid(hits)
@@ -189,7 +211,7 @@ def action_display():
189
  data=df_xlsx ,
190
  file_name= os.path.splitext(os.path.basename(st.session_state['filename']))[0]+'.xlsx')
191
 
192
- def filter_dataframe(df: pd.DataFrame) -> pd.DataFrame:
193
  """
194
  Adds a UI on top of a dataframe to let viewers filter columns
195
 
@@ -293,6 +315,111 @@ def filter_dataframe(df: pd.DataFrame) -> pd.DataFrame:
293
  st.session_state['action_hits'] = df
294
 
295
  return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  def policy_display():
297
  if 'key1' in st.session_state:
298
  df = st.session_state.key1
@@ -314,7 +441,7 @@ def policy_display():
314
 
315
 
316
  st.caption("Filter table to select rows to keep for Policies and Plans category")
317
- filter_dataframe(hits)
318
  # filtered_df = filtered_df[filtered_df.keep == True]
319
  # st.write('Explore the data')
320
  # AgGrid(hits)
 
1
  # set path
2
+ import glob, os, sys
3
+ from keras.engine.base_layer import policy;
4
  sys.path.append('../utils')
5
 
6
  #import needed libraries
 
80
  'Policies_Plans Score','Conditional Score'],inplace=True)
81
  action_hits = action_hits.sort_values(by=['Action Score'], ascending=False)
82
  action_hits = action_hits.reset_index(drop=True)
83
+ action_hits.to_excel(writer,index=False,sheet_name = 'Action')
84
+
85
+ if 'policy_hits' in st.session_state:
86
+ policy_hits = st.session_state['policy_hits']
87
+ if 'keep' in policy_hits.columns:
88
+ policy_hits = policy_hits[policy_hits.keep == True]
89
+ policy_hits = policy_hits.reset_index(drop=True)
90
+ policy_hits.drop(columns = ['keep'], inplace=True)
91
+ policy_hits.to_excel(writer,index=False,sheet_name = 'Policy')
92
+ else:
93
+ policy_hits = policy_hits.sort_values(by=['Policies_Plans Score'], ascending=False)
94
+ policy_hits = policy_hits.reset_index(drop=True)
95
+ policy_hits.to_excel(writer,index=False,sheet_name = 'Policy')
96
+ else:
97
+ policy_hits = df[df['Action Label'] == True]
98
+ policy_hits.drop(columns=['Target Label','Target Score','Netzero Score',
99
+ 'Netzero Label','GHG Label',
100
+ 'GHG Score','Action Label','Policies_Plans Label',
101
+ 'Action Score','Conditional Score'],inplace=True)
102
+ policy_hits = policy_hits.sort_values(by=['Policies_Plans Score'], ascending=False)
103
+ policy_hits = policy_hits.reset_index(drop=True)
104
+ policy_hits.to_excel(writer,index=False,sheet_name = 'Policy')
105
 
106
  # hits = hits.drop(columns = ['Target Score','Netzero Score','GHG Score'])
107
  workbook = writer.book
 
198
 
199
 
200
  st.caption("Filter table to select rows to keep for Action category")
201
+ filter_dataframe_action(hits)
202
  # filtered_df = filtered_df[filtered_df.keep == True]
203
  # st.write('Explore the data')
204
  # AgGrid(hits)
 
211
  data=df_xlsx ,
212
  file_name= os.path.splitext(os.path.basename(st.session_state['filename']))[0]+'.xlsx')
213
 
214
+ def filter_dataframe_action(df: pd.DataFrame) -> pd.DataFrame:
215
  """
216
  Adds a UI on top of a dataframe to let viewers filter columns
217
 
 
315
  st.session_state['action_hits'] = df
316
 
317
  return
318
+ def filter_dataframe_policy(df: pd.DataFrame) -> pd.DataFrame:
319
+ """
320
+ Adds a UI on top of a dataframe to let viewers filter columns
321
+
322
+ Args:
323
+ df (pd.DataFrame): Original dataframe
324
+
325
+ Returns:
326
+ pd.DataFrame: Filtered dataframe
327
+ """
328
+ modify = st.checkbox("Add filters")
329
+
330
+ if not modify:
331
+ st.session_state['policy_hits'] = df
332
+ return
333
+
334
+
335
+ df = df.copy()
336
+
337
+ # Try to convert datetimes into a standard format (datetime, no timezone)
338
+ # for col in df.columns:
339
+ # if is_object_dtype(df[col]):
340
+ # try:
341
+ # df[col] = pd.to_datetime(df[col])
342
+ # except Exception:
343
+ # pass
344
+
345
+ # if is_datetime64_any_dtype(df[col]):
346
+ # df[col] = df[col].dt.tz_localize(None)
347
+
348
+ modification_container = st.container()
349
+
350
+ with modification_container:
351
+ to_filter_columns = st.multiselect("Filter dataframe on", df.columns)
352
+ for column in to_filter_columns:
353
+ left, right = st.columns((1, 20))
354
+ left.write("↳")
355
+ # Treat columns with < 10 unique values as categorical
356
+ if is_categorical_dtype(df[column]):
357
+ user_cat_input = right.multiselect(
358
+ f"Values for {column}",
359
+ df[column].unique(),
360
+ default=list(df[column].unique()),
361
+ )
362
+ df = df[df[column].isin(user_cat_input)]
363
+ elif is_numeric_dtype(df[column]):
364
+ _min = float(df[column].min())
365
+ _max = float(df[column].max())
366
+ step = (_max - _min) / 100
367
+ user_num_input = right.slider(
368
+ f"Values for {column}",
369
+ _min,
370
+ _max,
371
+ (_min, _max),
372
+ step=step,
373
+ )
374
+ df = df[df[column].between(*user_num_input)]
375
+ elif is_list_like(df[column]):
376
+ list_vals = set(x for lst in df[column].tolist() for x in lst)
377
+ user_multi_input = right.multiselect(
378
+ f"Values for {column}",
379
+ list_vals,
380
+ default=list_vals,
381
+ )
382
+ df['check'] = df[column].apply(lambda x: any(i in x for i in user_multi_input))
383
+ df = df[df.check == True]
384
+ df.drop(columns = ['check'],inplace=True)
385
+
386
+ # df[df[column].between(*user_num_input)]
387
+ # elif is_datetime64_any_dtype(df[column]):
388
+ # user_date_input = right.date_input(
389
+ # f"Values for {column}",
390
+ # value=(
391
+ # df[column].min(),
392
+ # df[column].max(),
393
+ # ),
394
+ # )
395
+ # if len(user_date_input) == 2:
396
+ # user_date_input = tuple(map(pd.to_datetime, user_date_input))
397
+ # start_date, end_date = user_date_input
398
+ # df = df.loc[df[column].between(start_date, end_date)]
399
+ else:
400
+ user_text_input = right.text_input(
401
+ f"Substring or regex in {column}",
402
+ )
403
+ if user_text_input:
404
+ df = df[df[column].str.contains(user_text_input)]
405
+ df['keep'] = True
406
+ df = df[['keep','text','Policies_Plans Score','Conditional Label',
407
+ 'Sector Label','Adapt-Mitig Label','Indicator Label','page']]
408
+ df = st.data_editor(
409
+ df,
410
+ column_config={
411
+ "keep": st.column_config.CheckboxColumn(
412
+ help="Select which rows to keep",
413
+ default=False,
414
+ )
415
+ },
416
+ disabled=list(set(df.columns) - {'keep'}),
417
+ hide_index=True,
418
+ )
419
+ st.session_state['policy_hits'] = df
420
+
421
+ return
422
+
423
  def policy_display():
424
  if 'key1' in st.session_state:
425
  df = st.session_state.key1
 
441
 
442
 
443
  st.caption("Filter table to select rows to keep for Policies and Plans category")
444
+ filter_dataframe_policy(hits)
445
  # filtered_df = filtered_df[filtered_df.keep == True]
446
  # st.write('Explore the data')
447
  # AgGrid(hits)