Spaces:
Sleeping
Sleeping
Rundstedtzz
commited on
Commit
·
0876c97
1
Parent(s):
89d59ab
upload app.py
Browse files- app.py +73 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# !pip install streamlit
|
2 |
+
# !pip install pandas
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import streamlit as st
|
6 |
+
import base64
|
7 |
+
import io
|
8 |
+
import base64
|
9 |
+
|
10 |
+
# Functions
|
11 |
+
def map_data_to_template(mapping_df, template_df, data_df):
|
12 |
+
# Initialize the final output dataframe with the template columns, filled with NaN
|
13 |
+
final_output_df = pd.DataFrame(columns=template_df.columns)
|
14 |
+
|
15 |
+
# Prepare a dictionary to hold the mapping from MEDLab to NDA variables
|
16 |
+
variable_mapping = mapping_df.set_index('MEDLab Variable')['NDA Variable'].to_dict()
|
17 |
+
|
18 |
+
# Iterate over each NDA variable to map the data
|
19 |
+
for nda_var in final_output_df.columns:
|
20 |
+
medlab_vars = [medlab_var for medlab_var, nda_mapped_var in variable_mapping.items() if nda_mapped_var == nda_var]
|
21 |
+
|
22 |
+
# Initialize the column with None
|
23 |
+
final_output_df[nda_var] = [None] * len(data_df)
|
24 |
+
|
25 |
+
# Go through each potential MEDLab variable until we find one that's present and has data
|
26 |
+
for medlab_var in medlab_vars:
|
27 |
+
if medlab_var in data_df.columns and not data_df[medlab_var].isnull().all():
|
28 |
+
# If a date column, convert to the specified format
|
29 |
+
if 'date' in medlab_var:
|
30 |
+
final_output_df[nda_var] = pd.to_datetime(data_df[medlab_var], errors='coerce').dt.strftime('%m/%d/%Y')
|
31 |
+
else:
|
32 |
+
final_output_df[nda_var] = data_df[medlab_var]
|
33 |
+
break # Stop checking once we've mapped one
|
34 |
+
|
35 |
+
return final_output_df
|
36 |
+
|
37 |
+
|
38 |
+
# Streamlit app
|
39 |
+
def main():
|
40 |
+
st.markdown("<h1 style='text-align: center; color: #E694FF;'>Data Transformer</h1>", unsafe_allow_html=True)
|
41 |
+
|
42 |
+
# File Uploader for each CSV
|
43 |
+
st.subheader("Upload Files")
|
44 |
+
nimh_template_file = st.file_uploader("Choose NIMH Template CSV", type=['csv'])
|
45 |
+
redcap_data_file = st.file_uploader("Choose REDCap Data CSV", type=['csv'])
|
46 |
+
conversion_key_file = st.file_uploader("Choose Conversion Key CSV", type=['csv'])
|
47 |
+
|
48 |
+
if nimh_template_file and redcap_data_file and conversion_key_file:
|
49 |
+
# Convert the file objects to DataFrames
|
50 |
+
nimh_template_df = pd.read_csv(io.StringIO(nimh_template_file.getvalue().decode('utf-8')), skiprows=1)
|
51 |
+
redcap_data_df = pd.read_csv(io.StringIO(redcap_data_file.getvalue().decode('utf-8')))
|
52 |
+
conversion_key_df = pd.read_csv(io.StringIO(conversion_key_file.getvalue().decode('utf-8')))
|
53 |
+
|
54 |
+
transformed_data_df = map_data_to_template(
|
55 |
+
conversion_key_df,
|
56 |
+
nimh_template_df,
|
57 |
+
redcap_data_df
|
58 |
+
)
|
59 |
+
|
60 |
+
# Display transformed data
|
61 |
+
st.subheader("Transformed Data")
|
62 |
+
st.write(transformed_data_df)
|
63 |
+
|
64 |
+
# Download button for transformed data
|
65 |
+
st.subheader("Download Transformed Data")
|
66 |
+
csv = transformed_data_df.to_csv(index=False)
|
67 |
+
b64 = base64.b64encode(csv.encode()).decode() # some strings <-> bytes conversions necessary here
|
68 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="transformed_data.csv">Download CSV File</a>'
|
69 |
+
st.markdown(href, unsafe_allow_html=True)
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == '__main__':
|
73 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|