cassiebuhler commited on
Commit
d9fb7d9
·
1 Parent(s): 38804a1

fixed bugs; using llama3-sdsc

Browse files

fixed 2 bugs: rounding errors for "color by" + stacked bar chart filters

Files changed (2) hide show
  1. app/app.py +6 -5
  2. app/utils.py +10 -13
app/app.py CHANGED
@@ -117,7 +117,8 @@ st.markdown(
117
 
118
  st.markdown("<h2>CA 30x30 Planning & Assessment Prototype</h2>", unsafe_allow_html=True)
119
 
120
- st.markdown('<p class = "medium-font"> In October 2020, Governor Newsom issued Executive Order N-82-20 which establishes a state goal of conserving 30% of California’s lands and coastal waters by 2030 – known as CA 30x30. </p>', unsafe_allow_html = True)
 
121
 
122
  st.markdown('<p class = "medium-font"> This is an interactive cloud-native geospatial tool for exploring and visualizing California\'s protected lands. </p>', unsafe_allow_html = True)
123
 
@@ -145,8 +146,8 @@ from langchain_openai import ChatOpenAI
145
  #llm = ChatOpenAI(model = "kosbu/Llama-3.3-70B-Instruct-AWQ", api_key = st.secrets['CIRRUS_LLM_API_KEY'], base_url = "https://llm.cirrus.carlboettiger.info/v1/", temperature=0)
146
  # llm = ChatOpenAI(model="gpt-4", temperature=0)
147
  # llm = ChatOpenAI(model = "llama3", api_key=st.secrets['NRP_API_KEY'], base_url = "https://llm.nrp-nautilus.io/", temperature=0)
148
- llm = ChatOpenAI(model = "groq-tools", api_key=st.secrets['NRP_API_KEY'], base_url = "https://llm.nrp-nautilus.io/", temperature=0)
149
-
150
 
151
  managers = ca.sql("SELECT DISTINCT manager FROM mydata;").execute()
152
  names = ca.sql("SELECT name FROM mydata GROUP BY name HAVING SUM(acres) >10000;").execute()
@@ -398,11 +399,11 @@ colors = (
398
  # df - charts; df_tab - printed table (omits colors)
399
  if 'out' not in locals():
400
  df, df_tab, df_percent, df_bar_30x30 = get_summary_table(ca, column, select_colors, color_choice, filter_cols, filter_vals,colorby_vals)
401
- total_percent = 100*df_percent.percent_CA.sum()
402
 
403
  else:
404
  df = get_summary_table_sql(ca, column, colors, ids)
405
- total_percent = 100*df.percent_CA.sum()
406
 
407
 
408
  # charts displayed based on color_by variable
 
117
 
118
  st.markdown("<h2>CA 30x30 Planning & Assessment Prototype</h2>", unsafe_allow_html=True)
119
 
120
+ st.markdown('<p class="medium-font"> In October 2020, Governor Newsom issued <a href="https://www.gov.ca.gov/wp-content/uploads/2020/10/10.07.2020-EO-N-82-20-.pdf" target="_blank">Executive Order N-82-20</a>, which establishes a state goal of conserving 30% of California’s lands and coastal waters by 2030 – known as <a href="https://www.californianature.ca.gov/" target="_blank">CA 30x30</a>. </p>',
121
+ unsafe_allow_html=True)
122
 
123
  st.markdown('<p class = "medium-font"> This is an interactive cloud-native geospatial tool for exploring and visualizing California\'s protected lands. </p>', unsafe_allow_html = True)
124
 
 
146
  #llm = ChatOpenAI(model = "kosbu/Llama-3.3-70B-Instruct-AWQ", api_key = st.secrets['CIRRUS_LLM_API_KEY'], base_url = "https://llm.cirrus.carlboettiger.info/v1/", temperature=0)
147
  # llm = ChatOpenAI(model="gpt-4", temperature=0)
148
  # llm = ChatOpenAI(model = "llama3", api_key=st.secrets['NRP_API_KEY'], base_url = "https://llm.nrp-nautilus.io/", temperature=0)
149
+ # llm = ChatOpenAI(model = "groq-tools", api_key=st.secrets['NRP_API_KEY'], base_url = "https://llm.nrp-nautilus.io/", temperature=0)
150
+ llm = ChatOpenAI(model = "llama3-sdsc", api_key=st.secrets['NRP_API_KEY'], base_url = "https://llm.nrp-nautilus.io/", temperature=0)
151
 
152
  managers = ca.sql("SELECT DISTINCT manager FROM mydata;").execute()
153
  names = ca.sql("SELECT name FROM mydata GROUP BY name HAVING SUM(acres) >10000;").execute()
 
399
  # df - charts; df_tab - printed table (omits colors)
400
  if 'out' not in locals():
401
  df, df_tab, df_percent, df_bar_30x30 = get_summary_table(ca, column, select_colors, color_choice, filter_cols, filter_vals,colorby_vals)
402
+ total_percent = (100*df_percent.percent_CA.sum()).round(2)
403
 
404
  else:
405
  df = get_summary_table_sql(ca, column, colors, ids)
406
+ total_percent = (100*df.percent_CA.sum()).round(2)
407
 
408
 
409
  # charts displayed based on color_by variable
app/utils.py CHANGED
@@ -30,10 +30,6 @@ def get_buttons(style_options, style_choice, default_boxes=None):
30
  buttons[name] = st.checkbox(f"{name}", value=st.session_state[key], key=key, on_change = sync_checkboxes, args = (key,))
31
  filter_choice = [key for key, value in buttons.items() if value]
32
  return {column: filter_choice}
33
-
34
- # buttons = {name: st.checkbox(name, value=st.session_state.get(column + str(name), False), key=column + str(name), on_change=sync_checkboxes, args=(column + str(name),)) for name in opts}
35
-
36
- # return {column: [key for key, value in buttons.items() if value]}
37
 
38
 
39
  def sync_checkboxes(source):
@@ -108,7 +104,7 @@ def get_summary(ca, combined_filter, column, main_group, colors = None):
108
  mean_rxburn=(_.rxburn * _.acres).sum()/_.acres.sum(),
109
  mean_disadvantaged=(_.disadvantaged_communities * _.acres).sum() / _.acres.sum(),
110
  mean_svi=(_.svi * _.acres).sum() / _.acres.sum())
111
- .mutate(percent_CA=_.percent_CA.round(3), acres=_.acres.round(0)))
112
  df = df.inner_join(group_totals, main_group).mutate(percent_group=( _.acres / _.total_acres).round(3))
113
  if colors is not None and not colors.empty:
114
  df = df.inner_join(colors, column[-1])
@@ -314,14 +310,15 @@ def get_label_transform(x, label=None):
314
 
315
  return transformations.get(x, (f"datum.{x}", None))[0]
316
 
317
- def get_hex(df, color, sort_order):
318
  """
319
- Returns a list of hex color codes sorted based on `sort_order`.
320
  """
321
- return list(df.drop_duplicates(subset=color, keep="first")
322
  .set_index(color)
323
- .reindex(sort_order)
324
- .dropna()["color"])
 
325
 
326
 
327
  def create_bar_chart(df, x, y, title, color=None, stacked=False, colors=None):
@@ -349,9 +346,9 @@ def create_bar_chart(df, x, y, title, color=None, stacked=False, colors=None):
349
 
350
  if stacked:
351
  # order stacks
352
- sort_order = ["30x30-conserved", "other-conserved", "unknown", "non-conserved"]
353
- color_hex = get_hex(df[[color, "color"]], color, sort_order)
354
- sort_order = sort_order[:len(color_hex)]
355
  df["stack_order"] = df[color].apply(lambda val: sort_order.index(val) if val in sort_order else len(sort_order))
356
 
357
  # build chart
 
30
  buttons[name] = st.checkbox(f"{name}", value=st.session_state[key], key=key, on_change = sync_checkboxes, args = (key,))
31
  filter_choice = [key for key, value in buttons.items() if value]
32
  return {column: filter_choice}
 
 
 
 
33
 
34
 
35
  def sync_checkboxes(source):
 
104
  mean_rxburn=(_.rxburn * _.acres).sum()/_.acres.sum(),
105
  mean_disadvantaged=(_.disadvantaged_communities * _.acres).sum() / _.acres.sum(),
106
  mean_svi=(_.svi * _.acres).sum() / _.acres.sum())
107
+ .mutate(percent_CA=_.percent_CA.round(5), acres=_.acres.round(0)))
108
  df = df.inner_join(group_totals, main_group).mutate(percent_group=( _.acres / _.total_acres).round(3))
109
  if colors is not None and not colors.empty:
110
  df = df.inner_join(colors, column[-1])
 
310
 
311
  return transformations.get(x, (f"datum.{x}", None))[0]
312
 
313
+ def get_hex(df, color, order):
314
  """
315
+ Returns a list of hex color codes and categories sorted based on `sort_order`.
316
  """
317
+ return (df.drop_duplicates(subset=color, keep="first")
318
  .set_index(color)
319
+ .reindex(order)
320
+ .dropna()
321
+ .reset_index()).T.values.tolist()
322
 
323
 
324
  def create_bar_chart(df, x, y, title, color=None, stacked=False, colors=None):
 
346
 
347
  if stacked:
348
  # order stacks
349
+ order = ["30x30-conserved", "other-conserved", "unknown", "non-conserved"]
350
+ sort_order ,color_hex = get_hex(df[[color, "color"]], color, order)
351
+
352
  df["stack_order"] = df[color].apply(lambda val: sort_order.index(val) if val in sort_order else len(sort_order))
353
 
354
  # build chart