-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathworkflow.py
181 lines (166 loc) · 11.3 KB
/
workflow.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import streamlit as st
import pandas as pd
import numpy as np
import altair as alt
from st_aggrid import (
AgGrid,
DataReturnMode,
GridOptionsBuilder,
GridUpdateMode,
ColumnsAutoSizeMode
)
from util.download_pdf import add_download_pdf
import workflows.attribute_patterns.functions as functions
import workflows.attribute_patterns.classes as classes
import workflows.attribute_patterns.variables as vars
import workflows.attribute_patterns.config as config
import util.AI_API
import util.ui_components
def create():
workflow = 'attribute_patterns'
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_title='Intelligence Toolkit | Attribute Patterns')
sv = vars.SessionVariables('attribute_patterns')
intro_tab, uploader_tab, detect_tab, explain_tab = st.tabs(['Attribute patterns workflow:', 'Create graph model', 'Detect patterns', 'Generate AI pattern reports'])
df = None
with intro_tab:
st.markdown(config.intro)
with uploader_tab:
uploader_col, model_col = st.columns([2, 1])
with uploader_col:
util.ui_components.single_csv_uploader(workflow, 'Upload CSV', sv.attribute_last_file_name, sv.attribute_input_df, sv.attribute_binned_df, sv.attribute_final_df, key='attributes_uploader', height=500)
with model_col:
util.ui_components.prepare_input_df(workflow, sv.attribute_input_df, sv.attribute_binned_df, sv.attribute_final_df, sv.attribute_subject_identifier)
options = [''] + [c for c in sv.attribute_final_df.value.columns.values if c != 'Subject ID']
sv.attribute_time_col.value = st.selectbox('Period column', options, index=options.index(sv.attribute_time_col.value) if sv.attribute_time_col.value in options else 0)
time_col = sv.attribute_time_col.value
att_cols = [col for col in sv.attribute_final_df.value.columns.values if col not in ['Subject ID', time_col] and st.session_state[f'{workflow}_{col}'] == True]
ready = len(att_cols) > 0 and sv.attribute_time_col.value != ''
if st.button("Generate graph model", disabled=not ready):
with st.spinner('Adding links to model...'):
time_col = sv.attribute_time_col.value
df = sv.attribute_final_df.value.copy(deep=True)
df = util.df_functions.fix_null_ints(df).astype(str).replace('nan', '').replace('<NA>', '')
df['Subject ID'] = [str(x) for x in range(1, len(df) + 1)]
df['Subject ID'] = df['Subject ID'].astype(str)
pdf = df.copy(deep=True)[[time_col, 'Subject ID'] + att_cols]
pdf = pdf[pdf[time_col].notna() & pdf['Subject ID'].notna()]
pdf.rename(columns={time_col : 'Period'}, inplace=True)
pdf['Period'] = pdf['Period'].astype(str)
pdf = pd.melt(pdf, id_vars=['Subject ID', 'Period'], value_vars=att_cols, var_name='Attribute Type', value_name='Attribute Value')
pdf = pdf[pdf['Attribute Value'] != '']
pdf['Full Attribute'] = pdf.apply(lambda x: str(x['Attribute Type']) + config.type_val_sep + str(x['Attribute Value']), axis=1)
sv.attribute_dynamic_df.value = pdf
if ready and len(sv.attribute_dynamic_df.value) > 0:
st.markdown(f'Graph model has **{len(sv.attribute_dynamic_df.value)}** links spanning **{len(sv.attribute_dynamic_df.value["Subject ID"].unique())}** cases, **{len(sv.attribute_dynamic_df.value["Full Attribute"].unique())}** attributes, and **{len(sv.attribute_dynamic_df.value["Period"].unique())}** periods.')
print(sv.attribute_dynamic_df.value)
with detect_tab:
if not ready or len(sv.attribute_final_df.value) == 0:
st.markdown('Generate a graph model to continue.')
else:
b1, b2, b3, b4, b5 = st.columns([1, 1, 1, 1, 2])
with b1:
st.number_input('Minimum pattern count', min_value=1, step=1, key=sv.attribute_min_pattern_count.key, value=sv.attribute_min_pattern_count.value)
with b2:
st.number_input('Maximum pattern length', min_value=1, step=1, key=sv.attribute_max_pattern_length.key, value=sv.attribute_max_pattern_length.value)
with b3:
if st.button('Detect patterns'):
with st.spinner('Detecting patterns...'):
sv.attribute_table_index.value += 1
sv.attribute_df.value, time_to_graph = functions.prepare_graph(sv)
sv.attribute_embedding_df.value, sv.attribute_node_to_centroid.value, sv.attribute_period_embeddings.value = functions.generate_embedding(sv, sv.attribute_df.value, time_to_graph)
rc = classes.RecordCounter(sv.attribute_dynamic_df.value)
sv.attribute_record_counter.value = rc
sv.attribute_pattern_df.value, sv.attribute_close_pairs.value, sv.attribute_all_pairs.value = functions.detect_patterns(sv)
with b4:
st.download_button('Download patterns', data=sv.attribute_pattern_df.value.to_csv(index=False), file_name='attribute_patterns.csv', mime='text/csv', disabled=len(sv.attribute_pattern_df.value) == 0)
if len(sv.attribute_pattern_df.value) > 0:
prop = sv.attribute_close_pairs.value / sv.attribute_all_pairs.value if sv.attribute_all_pairs.value > 0 else 0
prop = round(prop, -int(np.floor(np.log10(abs(prop))))) if prop > 0 else 0
period_count = len(sv.attribute_pattern_df.value["period"].unique())
pattern_count = len(sv.attribute_pattern_df.value)
unique_count = len(sv.attribute_pattern_df.value['pattern'].unique())
st.markdown(f'Over **{period_count}** periods, detected **{pattern_count}** attribute patterns (**{unique_count}** unique) from **{sv.attribute_converging_pairs.value}**/**{sv.attribute_all_pairs.value}** converging attribute pairs (**{round(sv.attribute_converging_pairs.value / sv.attribute_all_pairs.value * 100, 2) if sv.attribute_all_pairs.value > 0 else 0}%**).')
show_df = sv.attribute_pattern_df.value
tdf = functions.create_time_series_df(sv.attribute_record_counter.value, sv.attribute_pattern_df.value)
gb = GridOptionsBuilder.from_dataframe(show_df)
gb.configure_default_column(flex=1, wrapText=True, wrapHeaderText=True, enablePivot=False, enableValue=False, enableRowGroup=False)
gb.configure_selection(selection_mode="single", use_checkbox=False)
gb.configure_side_bar()
gridoptions = gb.build()
response = AgGrid(
show_df,
key=f'report_grid_{sv.attribute_table_index.value}',
height=400,
gridOptions=gridoptions,
enable_enterprise_modules=False,
update_mode=GridUpdateMode.SELECTION_CHANGED,
data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
fit_columns_on_grid_load=False,
header_checkbox_selection_filtered_only=False,
use_checkbox=False,
enable_quicksearch=True,
reload_data=False,
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW
) # type: ignore
selected_pattern = response['selected_rows'][0]['pattern'] if len(response['selected_rows']) > 0 else ''
selected_pattern_period = response['selected_rows'][0]['period'] if len(response['selected_rows']) > 0 else ''
if selected_pattern != '' and selected_pattern != sv.attribute_selected_pattern.value:
sv.attribute_selected_pattern.value = selected_pattern
sv.attribute_selected_pattern_period.value = selected_pattern_period
sv.attribute_report.value = ''
st.markdown('**Selected pattern: ' + selected_pattern + ' (' + selected_pattern_period + ')**')
tdf = tdf[tdf['pattern'] == selected_pattern]
sv.attribute_selected_pattern_df.value = tdf
sv.attribute_selected_pattern_att_counts.value = functions.compute_attribute_counts(sv.attribute_final_df.value, selected_pattern, time_col, selected_pattern_period)
count_ct = alt.Chart(tdf).mark_line().encode(
x='period:O',
y='count:Q',
color=alt.ColorValue('blue')
).properties(
height = 200,
width = 600
)
st.altair_chart(count_ct, use_container_width=True)
with explain_tab:
if not ready or len(sv.attribute_final_df.value) == 0 or sv.attribute_selected_pattern.value == '':
st.markdown('Select a pattern to continue.')
else:
c1, c2 = st.columns([2, 3])
with c1:
variables = {
'pattern': sv.attribute_selected_pattern.value,
'period': sv.attribute_selected_pattern_period.value,
'time_series': sv.attribute_selected_pattern_df.value.to_csv(index=False),
'attribute_counts': sv.attribute_selected_pattern_att_counts.value.to_csv(index=False)
}
generate, messages = util.ui_components.generative_ai_component(sv.attribute_system_prompt, sv.attribute_instructions, variables)
with c2:
st.markdown('##### Selected attribute pattern')
selected_pattern = sv.attribute_selected_pattern.value
selected_pattern_period = sv.attribute_selected_pattern_period.value
if selected_pattern != '':
st.markdown('**' + selected_pattern + ' (' + selected_pattern_period + ')**')
tdf = sv.attribute_selected_pattern_df.value
count_ct = alt.Chart(tdf).mark_line().encode(
x='period:O',
y='count:Q',
color=alt.ColorValue('blue')
).properties(
height = 200,
width = 600
)
st.altair_chart(count_ct, use_container_width=True)
report_placeholder = st.empty()
if generate:
result = util.AI_API.generate_text_from_message_list(
placeholder=report_placeholder,
messages=messages,
prefix=''
)
sv.attribute_report.value = result
report_data = sv.attribute_report.value
report_placeholder.markdown(report_data)
is_download_disabled = report_data == ''
name = 'attribute_pattern_report'
add_download_pdf(f'{name}.pdf', report_data, 'Download PDF report', disabled=is_download_disabled)
st.download_button('Download markdown report', data=report_data, file_name=f'{name}.md', mime='text/markdown', disabled=is_download_disabled)