| 1 | + | import json |
| 2 | + | import streamlit as st |
| 3 | + | from streamlit_agraph import agraph, Config |
| 4 | + | from utils import ( |
| 5 | + | build_agraph_components, |
| 6 | + | get_subgraph_nodes_df, |
| 7 | + | get_subgraph_df, |
| 8 | + | get_subgraph_edges_df, |
| 9 | + | get_subgraph_with_risk_score, |
| 10 | + | build_markdown_strings_for_node, |
| 11 | + | ) |
| 12 | + | |
| 13 | + | |
| 14 | + | st.set_page_config(layout="wide") |
| 15 | + | |
| 16 | + | |
| 17 | + | SLIDER_MIN = 0 |
| 18 | + | SLIDER_MAX = 100 |
| 19 | + | SLIDER_DEFAULT = 50 |
| 20 | + | DEFAULT_NUM_SUBGRAPHS_TO_SHOW = 3 |
| 21 | + | GRAPH_PLOT_HEIGHT_PX = 400 |
| 22 | + | GRAPH_SIZE_RENDER_LIMIT = 30 |
| 23 | + | subgraphs = get_subgraph_df() |
| 24 | + | |
| 25 | + | with st.sidebar: |
| 26 | + | st.title("Corporate risks") |
| 27 | + | |
| 28 | + | weight_chains = ( |
| 29 | + | st.slider( |
| 30 | + | "Long ownership chains", |
| 31 | + | min_value=SLIDER_MIN, |
| 32 | + | max_value=SLIDER_MAX, |
| 33 | + | value=SLIDER_DEFAULT, |
| 34 | + | ) |
| 35 | + | / SLIDER_MAX |
| 36 | + | ) |
| 37 | + | weight_cyclic = ( |
| 38 | + | st.slider( |
| 39 | + | "Cyclic ownership", |
| 40 | + | min_value=SLIDER_MIN, |
| 41 | + | max_value=SLIDER_MAX, |
| 42 | + | value=SLIDER_DEFAULT, |
| 43 | + | ) |
| 44 | + | / SLIDER_MAX |
| 45 | + | ) |
| 46 | + | weight_psc_haven = ( |
| 47 | + | st.slider( |
| 48 | + | "Persons of significant control associated with tax havens", |
| 49 | + | min_value=SLIDER_MIN, |
| 50 | + | max_value=SLIDER_MAX, |
| 51 | + | value=SLIDER_DEFAULT, |
| 52 | + | ) |
| 53 | + | / SLIDER_MAX |
| 54 | + | ) |
| 55 | + | weight_pep = ( |
| 56 | + | st.slider( |
| 57 | + | "Officers/PSCs are politically exposed", |
| 58 | + | min_value=SLIDER_MIN, |
| 59 | + | max_value=SLIDER_MAX, |
| 60 | + | value=SLIDER_DEFAULT, |
| 61 | + | ) |
| 62 | + | / SLIDER_MAX |
| 63 | + | ) |
| 64 | + | weight_sanctions = ( |
| 65 | + | st.slider( |
| 66 | + | "Officers/PSCs/Companies are sanctioned", |
| 67 | + | min_value=SLIDER_MIN, |
| 68 | + | max_value=SLIDER_MAX, |
| 69 | + | value=SLIDER_DEFAULT, |
| 70 | + | ) |
| 71 | + | / SLIDER_MAX |
| 72 | + | ) |
| 73 | + | weight_disqualified = ( |
| 74 | + | st.slider( |
| 75 | + | "Officers are disqualified directors", |
| 76 | + | min_value=SLIDER_MIN, |
| 77 | + | max_value=SLIDER_MAX, |
| 78 | + | value=SLIDER_DEFAULT, |
| 79 | + | ) |
| 80 | + | / SLIDER_MAX |
| 81 | + | ) |
| 82 | + | # custom_names_a = st.multiselect( |
| 83 | + | # label="Custom persons of interest", |
| 84 | + | # options=nodes["node_id"], |
| 85 | + | # default=None, |
| 86 | + | # ) |
| 87 | + | custom_names_b = st.file_uploader(label="Custom persons of interest", type="csv") |
| 88 | + | |
| 89 | + | go = st.button("Go") |
| 90 | + | |
| 91 | + | |
| 92 | + | with st.container(): |
| 93 | + | |
| 94 | + | subgraph_with_risk_scores = get_subgraph_with_risk_score( |
| 95 | + | subgraphs, |
| 96 | + | weight_chains=weight_chains, |
| 97 | + | weight_cyclic=weight_cyclic, |
| 98 | + | weight_psc_haven=weight_psc_haven, |
| 99 | + | weight_pep=weight_pep, |
| 100 | + | weight_sanctions=weight_sanctions, |
| 101 | + | weight_disqualified=weight_disqualified, |
| 102 | + | ) |
| 103 | + | |
| 104 | + | st.dataframe(data=subgraph_with_risk_scores, use_container_width=True) |
| 105 | + | |
| 106 | + | selected_subgraph_hashes = st.multiselect( |
| 107 | + | label="Select corporate network(s) to explore", |
| 108 | + | options=list(subgraph_with_risk_scores.index), |
| 109 | + | default=list( |
| 110 | + | subgraph_with_risk_scores.head(DEFAULT_NUM_SUBGRAPHS_TO_SHOW).index |
| 111 | + | ), |
| 112 | + | ) |
| 113 | + | |
| 114 | + | |
| 115 | + | with st.container(): |
| 116 | + | num_subgraphs_to_display = len(selected_subgraph_hashes) |
| 117 | + | |
| 118 | + | if num_subgraphs_to_display > 0: |
| 119 | + | cols = st.columns(num_subgraphs_to_display) |
| 120 | + | |
| 121 | + | for c, subgraph_hash in enumerate(selected_subgraph_hashes): |
| 122 | + | nodes_selected = get_subgraph_nodes_df(subgraph_hash) |
| 123 | + | edges_selected = get_subgraph_edges_df(subgraph_hash) |
| 124 | + | |
| 125 | + | with cols[c]: |
| 126 | + | if len(nodes_selected) < GRAPH_SIZE_RENDER_LIMIT: |
| 127 | + | (node_objects, edge_objects) = build_agraph_components( |
| 128 | + | nodes_selected, edges_selected |
| 129 | + | ) |
| 130 | + | agraph( |
| 131 | + | nodes=node_objects, |
| 132 | + | edges=edge_objects, |
| 133 | + | config=Config( |
| 134 | + | width=round(1080 / num_subgraphs_to_display), |
| 135 | + | height=GRAPH_PLOT_HEIGHT_PX, |
| 136 | + | ), |
| 137 | + | ) |
| 138 | + | else: |
| 139 | + | st.error("Subgraph is too large to render") |
| 140 | + | |
| 141 | + | st.write(nodes_selected) |
| 142 | + | # # Build markdown strings for representing metadata |
| 143 | + | # markdown_strings = build_markdown_strings_for_node(nodes_selected) |
| 144 | + | |
| 145 | + | # st.markdown(":busts_in_silhouette: **People**") |
| 146 | + | # for p in markdown_strings["people"]: |
| 147 | + | # if ("SANCTIONED" in p) or ("PEP" in p): |
| 148 | + | # st.markdown(p) |
| 149 | + | # else: |
| 150 | + | # st.markdown(p) |
| 151 | + | |
| 152 | + | # st.markdown(":office: **Companies**") |
| 153 | + | # for c in markdown_strings["companies"]: |
| 154 | + | # if ("SANCTIONED" in c) or ("PEP" in c): |
| 155 | + | # st.markdown(c) |
| 156 | + | # else: |
| 157 | + | # st.markdown(c) |
| 158 | + | |