Skip to content

Commit

Permalink
Fix ACD chart bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Edge committed Nov 11, 2024
1 parent 4f3dfc3 commit 55eb3c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
78 changes: 41 additions & 37 deletions app/util/ui_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,12 @@ def df_updated(df_name, reset):
# add to all subsequent dataframes if not present
for df in df_sequence[df_sequence.index(df_name) + 1:]:
if df in st.session_state[f"{workflow}_intermediate_dfs"]:
st.session_state[f"{workflow}_intermediate_dfs"][df][col] = last_df[
col
]
if col in st.session_state[f"{workflow}_selected_compound_cols"]: # and df_name in ["expanded", "suppress_count", "suppress_null"]:
pass
else:
st.session_state[f"{workflow}_intermediate_dfs"][df][col] = last_df[
col
]
if reset:
for df in df_sequence[index:]:
st.session_state[f"{workflow}_intermediate_dfs"][df] = prior_df.copy(deep=True)
Expand Down Expand Up @@ -551,19 +554,20 @@ def prepare_stage(df_name):
options = [
x for x in last_df.columns.to_numpy()
]
columns = [
x
for x in options
if x not in st.session_state[f"{workflow}_selected_compound_cols"]
]
# columns = [
# x
# for x in options
# if x not in st.session_state[f"{workflow}_selected_compound_cols"]
# ]
selected_compound_cols = st.multiselect(
"Select compound columns to expand",
columns,
options,
key=f"{workflow}_selected_compound_cols",
help="Select the columns you want to expand into separate columns. If you do not select any columns, no expansion will be performed.",
)
col_delimiter = st.text_input(
"Column delimiter",
value="",
value=",",
help="The character used to separate values in compound columns. If the delimiter is not present in a cell, the cell will be left unchanged. Any quotes around the entire list or individual values will be removed before processing, as will any enclosing square brackets.",
)
prefix_type = st.radio(
Expand All @@ -589,35 +593,35 @@ def prepare_stage(df_name):
col_delimiter != ""
and to_add not in st.session_state[f"{workflow}_selected_compound_cols"]
):
st.session_state[f"{workflow}_selected_compound_cols"].append(to_add)
for cols, delim in st.session_state[
# st.session_state[f"{workflow}_selected_compound_cols"].append(to_add)
for col in st.session_state[
f"{workflow}_selected_compound_cols"
]:
for col in cols:
if prefix_type == "Source column":
prefix = col
def convert_to_list(x):
if type(x) != str:
return []
if x[0] == "[" and x[-1] == "]":
x = x[1:-1]
vals = [y.strip() for y in x.split(delim)]
vals = [y[1:-1] if y[0] == '"' and y[-1] == '"' else y for y in vals if len(y) > 1]
vals = [y[1:-1] if y[0] == '\'' and y[-1] == '\'' else y for y in vals if len(y) > 1]
if prefix != "":
vals = [f"{prefix}{y}" for y in vals]
return vals
# add each value as a separate column with a 1 if the value is present in the compound column and None otherwise
values = last_df[col].apply(convert_to_list)
unique_values = {v for vals in values for v in vals}
unique_values = [x for x in unique_values if x != ""]
for val in unique_values:
st.session_state[f"{workflow}_{val}"] = False
this_df[val] = values.apply(
lambda x: "1" if val in x and val != "nan" else ""
)
if col in this_df.columns:
this_df.drop(columns=[col], inplace=True)

if prefix_type == "Source column":
prefix = col
def convert_to_list(x):
if type(x) != str:
return []
if x[0] == "[" and x[-1] == "]":
x = x[1:-1]
vals = [y.strip() for y in x.split(col_delimiter)]
vals = [y[1:-1] if y[0] == '"' and y[-1] == '"' else y for y in vals if len(y) > 1]
vals = [y[1:-1] if y[0] == '\'' and y[-1] == '\'' else y for y in vals if len(y) > 1]
if prefix != "":
vals = [f"{prefix}{y}" for y in vals]
return vals
# add each value as a separate column with a 1 if the value is present in the compound column and None otherwise
values = last_df[col].apply(convert_to_list)
unique_values = {v for vals in values for v in vals}
unique_values = [x for x in unique_values if x != ""]
for val in unique_values:
st.session_state[f"{workflow}_{val}"] = False
this_df[val] = values.apply(
lambda x: "1" if val in x and val != "nan" else ""
)
if col in this_df.columns:
this_df.drop(columns=[col], inplace=True)
df_updated("expanded", False)
with c2:
if st.button("Reset", key="reset_expand"):
Expand Down
7 changes: 3 additions & 4 deletions app/workflows/anonymize_case_data/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def create(sv: ds_variables.SessionVariables, workflow: None):
selection.sort(
key=lambda x: x["attribute"] + val_separator + x["value"]
)

selection_key = att_separator.join(
[x["attribute"] + val_separator + x["value"] for x in selection]
)
Expand Down Expand Up @@ -441,12 +442,10 @@ def create(sv: ds_variables.SessionVariables, workflow: None):
time_attribute = st.selectbox(
"Time attribute",
options=time_options,
index=time_options.index(
chart_individual_configuration["time_attribute"]
index=time_options.index(chart_individual_configuration["time_attribute"])
if chart_individual_configuration["time_attribute"]
in sdf.columns.to_numpy()
else None,
),
else 0
)
series_attributes = st.multiselect(
"Series attributes",
Expand Down
5 changes: 3 additions & 2 deletions intelligence_toolkit/anonymize_case_data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,13 @@ def get_flow_chart_fig(
att_separator: str = ";",
val_separator: str = ":",
):
selection_keys = [x["attribute"] + val_separator + x["value"] for x in selection]
att_count = 2 if highlight_attribute == "" else 3
att_count += len(selection)
if att_count <= 4:
chart_df = queries.compute_aggregate_graph(
self.aggregate_df,
selection,
selection_keys,
source_attribute,
target_attribute,
highlight_attribute,
Expand All @@ -332,7 +333,7 @@ def get_flow_chart_fig(
else:
chart_df = queries.compute_synthetic_graph(
self.synthetic_df,
selection,
selection_keys,
source_attribute,
target_attribute,
highlight_attribute,
Expand Down

0 comments on commit 55eb3c8

Please sign in to comment.