|
|
|
@ -66,122 +66,128 @@ st.markdown( |
|
|
|
with Gephi and [Gephi Light](https://gephi.org/gephi-lite/).*""" |
|
|
|
with Gephi and [Gephi Light](https://gephi.org/gephi-lite/).*""" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
#try: |
|
|
|
# Print explainer. |
|
|
|
# Print explainer. |
|
|
|
expl = st.expander(label="More info") |
|
|
|
expl = st.expander(label="More info") |
|
|
|
with expl: |
|
|
|
with expl: |
|
|
|
st.write(info.explainer) |
|
|
|
st.write(info.explainer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Ask for nodes file. |
|
|
|
# Ask for nodes file. |
|
|
|
csv_nodes = st.file_uploader( |
|
|
|
csv_nodes = st.file_uploader( |
|
|
|
label="Upload file with **nodes** (if you have one).", key="nodes", help=f'[Example]({info.node_example})' |
|
|
|
label="Upload file with **nodes** (if you have one).", key="nodes", help=f'[Example]({info.node_example})' |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Ask for relations file. |
|
|
|
|
|
|
|
csv_edges = st.file_uploader(label="Upload file with **relations**.", key="relations", help=f'[Example]({info.relations_example})') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
col1, col2 = st.columns([1,2]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Chose separator |
|
|
|
|
|
|
|
with col1: |
|
|
|
|
|
|
|
# Set standard separator. |
|
|
|
|
|
|
|
st.session_state["sep"] = ',' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Ask for separator. |
|
|
|
|
|
|
|
sep = st.radio( |
|
|
|
|
|
|
|
'Separator in your files:', |
|
|
|
|
|
|
|
options=['comma ( , )', 'semicolon ( ; )', 'tab ( \u21E5 )', 'pipe (|)', 'space ( )'], |
|
|
|
|
|
|
|
help='What are the values in your files separated with?' |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
separators = {'comma ( , )': ',', 'semicolon ( ; )': ';', 'tab ( \u21E5 )': '\t', 'pipe (|)': '|', 'space ( )': ' '} |
|
|
|
|
|
|
|
st.session_state["sep"] = separators[sep] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Preview file |
|
|
|
# Ask for relations file. |
|
|
|
with col2: |
|
|
|
csv_edges = st.file_uploader(label="Upload file with **relations**.", key="relations", help=f'[Example]({info.relations_example})') |
|
|
|
preview = st.button('Preview file.') |
|
|
|
|
|
|
|
if preview: |
|
|
|
col1, col2 = st.columns([1,2]) |
|
|
|
st.dataframe(pd.read_csv(csv_edges, sep=st.session_state["sep"])) |
|
|
|
|
|
|
|
|
|
|
|
# Chose separator |
|
|
|
|
|
|
|
with col1: |
|
|
|
|
|
|
|
# Set standard separator. |
|
|
|
|
|
|
|
st.session_state["sep"] = ',' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Ask for separator. |
|
|
|
|
|
|
|
separators = {'comma ( , )': ',', 'semicolon ( ; )': ';', 'tab ( \u21E5 )': '\t', 'pipe (|)': '|', 'space ( )': ' ', '':''} |
|
|
|
|
|
|
|
sep = st.radio( |
|
|
|
|
|
|
|
'Separator in your files:', |
|
|
|
|
|
|
|
options=['comma ( , )', 'semicolon ( ; )', 'tab ( \u21E5 )', 'pipe (|)', 'space ( )', 'custom'], |
|
|
|
|
|
|
|
help='What are the values in your files separated with?' |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
if sep == 'custom': |
|
|
|
|
|
|
|
sep = st.text_input('Custom delimiter:') |
|
|
|
|
|
|
|
separators[sep] = sep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.session_state["sep"] = separators[sep] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Preview file |
|
|
|
|
|
|
|
with col2: |
|
|
|
|
|
|
|
preview = st.button('Preview file.') |
|
|
|
|
|
|
|
if preview: |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
st.dataframe(pd.read_csv(csv_edges, sep=st.session_state["sep"]), use_container_width=True) |
|
|
|
|
|
|
|
except pd.errors.ParserError: |
|
|
|
|
|
|
|
st.markdown(':red[Have you selected a correct separator?]') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
files_uploaded = st.button('Done', 'files_uploaded') |
|
|
|
files_uploaded = st.button('Done', 'files_uploaded') |
|
|
|
|
|
|
|
|
|
|
|
if files_uploaded or 'files_already_uploaded' in st.session_state: |
|
|
|
if files_uploaded or 'files_already_uploaded' in st.session_state: |
|
|
|
st.session_state['files_already_uploaded'] = True |
|
|
|
st.session_state['files_already_uploaded'] = True |
|
|
|
|
|
|
|
|
|
|
|
if csv_edges == None: |
|
|
|
if csv_edges == None: |
|
|
|
st.markdown(':red[You need to upload a file with relations.]') |
|
|
|
st.markdown(':red[You need to upload a file with relations.]') |
|
|
|
st.stop() |
|
|
|
st.stop() |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
df = pd.read_csv(csv_edges, sep=st.session_state["sep"]) |
|
|
|
df = pd.read_csv(csv_edges, sep=st.session_state["sep"]) |
|
|
|
except pd.errors.EmptyDataError: |
|
|
|
except pd.errors.EmptyDataError: |
|
|
|
st.markdown(':red[Have you chosen the right kind of separator?]') |
|
|
|
st.markdown(':red[Have you chosen the right kind of separator?]') |
|
|
|
st.stop() |
|
|
|
st.stop() |
|
|
|
|
|
|
|
|
|
|
|
df.rename({'type': 'relation_type'}, inplace=True, axis=1) # 'type' can't be used as attribute. |
|
|
|
df.rename({'type': 'relation_type'}, inplace=True, axis=1) # 'type' can't be used as attribute. |
|
|
|
df.columns = [i.lower() for i in df.columns] # Remove capital letters from column names. |
|
|
|
df.columns = [i.lower() for i in df.columns] # Remove capital letters from column names. |
|
|
|
|
|
|
|
|
|
|
|
with st.form('select_columns'): |
|
|
|
with st.form('select_columns'): |
|
|
|
|
|
|
|
|
|
|
|
# Find and store target column. |
|
|
|
# Find and store target column. |
|
|
|
target = find_columns('target', df.columns.tolist()) |
|
|
|
target = find_columns('target', df.columns.tolist()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Find and store source column. |
|
|
|
|
|
|
|
source = find_columns('source', df.columns.tolist()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Confirm choise. |
|
|
|
|
|
|
|
columns_selected = st.form_submit_button("Done") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if columns_selected: |
|
|
|
|
|
|
|
# Remove source and target columns from list of options. |
|
|
|
|
|
|
|
columns = df.columns.tolist() |
|
|
|
|
|
|
|
columns.remove(st.session_state["target"]) |
|
|
|
|
|
|
|
columns.remove(st.session_state["source"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if all([st.session_state["source"] != "", st.session_state["target"] != ""]): |
|
|
|
|
|
|
|
source = st.session_state["source"] |
|
|
|
|
|
|
|
target = st.session_state["target"] |
|
|
|
|
|
|
|
|
|
|
|
# Find and store source column. |
|
|
|
# Let the user chose what columns that should be included. |
|
|
|
source = find_columns('source', df.columns.tolist()) |
|
|
|
chosen_columns = st.multiselect( |
|
|
|
|
|
|
|
label="Chose other columns to include.", options=columns, default=columns |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Confirm choise. |
|
|
|
if csv_nodes != None: # When a nodes file is uploaded. |
|
|
|
columns_selected = st.form_submit_button("Done") |
|
|
|
df_nodes = pd.read_csv(csv_nodes, sep=st.session_state["sep"]) |
|
|
|
|
|
|
|
df_nodes.columns = [i.lower() for i in df_nodes.columns] # Remove capital letters from column names. |
|
|
|
if columns_selected: |
|
|
|
|
|
|
|
# Remove source and target columns from list of options. |
|
|
|
|
|
|
|
columns = df.columns.tolist() |
|
|
|
|
|
|
|
columns.remove(st.session_state["target"]) |
|
|
|
|
|
|
|
columns.remove(st.session_state["source"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if all([st.session_state["source"] != "", st.session_state["target"] != ""]): |
|
|
|
|
|
|
|
source = st.session_state["source"] |
|
|
|
|
|
|
|
target = st.session_state["target"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Let the user chose what columns that should be included. |
|
|
|
label_column = find_columns('label', df_nodes.columns.tolist()) |
|
|
|
chosen_columns = st.multiselect( |
|
|
|
df_nodes.set_index(label_column, inplace=True) |
|
|
|
label="Chose other columns to include.", options=columns, default=columns |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if csv_nodes != None: # When a nodes file is uploaded. |
|
|
|
else: # If no node file provided. |
|
|
|
df_nodes = pd.read_csv(csv_nodes, sep=st.session_state["sep"]) |
|
|
|
nodes = list(set(df[source].tolist() + df[target].tolist())) |
|
|
|
df_nodes.columns = [i.lower() for i in df_nodes.columns] # Remove capital letters from column names. |
|
|
|
df_nodes = pd.DataFrame( |
|
|
|
|
|
|
|
nodes, index=range(0, len(nodes)), columns=["labels"] |
|
|
|
label_column = find_columns('label', df_nodes.columns.tolist()) |
|
|
|
|
|
|
|
df_nodes.set_index(label_column, inplace=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: # If no node file provided. |
|
|
|
|
|
|
|
nodes = list(set(df[source].tolist() + df[target].tolist())) |
|
|
|
|
|
|
|
df_nodes = pd.DataFrame( |
|
|
|
|
|
|
|
nodes, index=range(0, len(nodes)), columns=["labels"] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
df_nodes.set_index("labels", inplace=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Make empty graph. |
|
|
|
|
|
|
|
G = nx.MultiDiGraph() |
|
|
|
|
|
|
|
# Add nodes. |
|
|
|
|
|
|
|
G = add_nodes(G, df_nodes) |
|
|
|
|
|
|
|
# Add edges. |
|
|
|
|
|
|
|
G = add_edges( |
|
|
|
|
|
|
|
G, df, source=source, target=target, chosen_columns=chosen_columns |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
df_nodes.set_index("labels", inplace=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Make empty graph. |
|
|
|
|
|
|
|
G = nx.MultiDiGraph() |
|
|
|
|
|
|
|
# Add nodes. |
|
|
|
|
|
|
|
G = add_nodes(G, df_nodes) |
|
|
|
|
|
|
|
# Add edges. |
|
|
|
|
|
|
|
G = add_edges( |
|
|
|
|
|
|
|
G, df, source=source, target=target, chosen_columns=chosen_columns |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Turn the graph into a string. |
|
|
|
# Turn the graph into a string. |
|
|
|
graph_text = "\n".join([line for line in nx.generate_gexf(G)]) |
|
|
|
graph_text = "\n".join([line for line in nx.generate_gexf(G)]) |
|
|
|
|
|
|
|
|
|
|
|
# Download gexf-file. |
|
|
|
# Download gexf-file. |
|
|
|
gexf_file = "output.gexf" |
|
|
|
gexf_file = "output.gexf" |
|
|
|
st.download_button( |
|
|
|
st.download_button( |
|
|
|
"Download gexf-file", graph_text, file_name=gexf_file |
|
|
|
"Download gexf-file", graph_text, file_name=gexf_file |
|
|
|
) |
|
|
|
) |
|
|
|
st.write('Import the file to Gephi/Gephi Light, or try [Gephisto](https://jacomyma.github.io/gephisto/) to get an idea of the network.') |
|
|
|
st.write('Import the file to Gephi/Gephi Light, or try [Gephisto](https://jacomyma.github.io/gephisto/) to get an idea of the network.') |
|
|
|
|
|
|
|
|
|
|
|
except: |
|
|
|
# except: |
|
|
|
st.markdown(':red[Something went wrong, please try again or [write to me](https://twitter.com/lasseedfast).]') |
|
|
|
# st.markdown(':red[Something went wrong, please try again or [write to me](https://twitter.com/lasseedfast).]') |