-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyConv.py
438 lines (352 loc) · 16.4 KB
/
pyConv.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from logging import StrFormatStyle
import pandas as pd
import re
import json
import uuid
import warnings
import numpy as np
from pandas.io.formats.format import DataFrameFormatter
import streamlit as st
import os
import base64
from io import BytesIO
from streamlit.type_util import data_frame_to_bytes
def remove_rcna(df):
"""
Removes empty columns and rows from df
"""
df.dropna(how = 'all', axis = 'columns', inplace = True)
df.dropna(how = 'all', axis = 'rows', inplace = True)
return df
#===========================================================================================================================================
#TODO: add something to catch typos
def verLocal(df):
"""
Creates verbatimLocality column from user specified columns
"""
locality_cols= []
df = df.assign(verbatimLocality = "")
print(df.columns)
while True:
entry = input('Please select columns from dataframe to add to verbatimLocality (type d when done): ')
if entry.lower() == 'd':
break
# elif entry.lower() not in df.columns:
# print ("Column not found in dataframe")
# continue
else:
locality_cols.append(entry)
df["verbatimLocality"] = df[locality_cols].astype(str).apply(", ".join, axis=1)
return df
#===========================================================================================================================================
#TODO: This needs to be modified to handle universal data
#HOW: Let the user decide which words convert to which materialSampleType
#print out unique list of what's in there
#ask them to write a dictionary or fix it; column of theirs and fill in column with options
def matSampType(df):
## More description to status column -- in connection with GENOME
dct = pd.read_csv("https://raw.githubusercontent.com/futres/fovt-data-mapping/master/Mapping%20Files/MST_dict.csv")
if df["Status"].eq(dct["userTerm"]):
inpt = input(f'Would you like to replace {df["Status"]} with {dct["userTerm"]}? ')
if inpt.lower() == "yes":
df["Status"] = str(dct["userTerm"])
else:
ask = input(f'Whould you like to replace {df["Status"]}?')
if ask.lower() == "yes":
replace = input(f'What would you like to replace {df["Status"]} with? ')
df["Status"] = str(replace)
dct["userTerm"].append(df["Status"])
dct["replacedWith"].append(str(replace))
dct.to_csv('MST_dict.csv')
return(df, dct)
#===========================================================================================================================================
#TODO: make for non-english labels
def sex(df):
"""
Standardizes sex values with GEOME vocabulary
"""
female = df['Sex'].eq("F", "f")
male = df['Sex'].eq("M", "m")
df['Sex'][(female == False)&(male==False)] = "not collected"
df['Sex'][female == True] = "female"
df['Sex'][male == True] = "male"
return df
#===========================================================================================================================================
def inConv(df):
"""
Converts length from inches to millimeters
"""
df['Length'] = df['Length'] * 25.4
return df
#===========================================================================================================================================
def lbsConv(df):
"""
Converts weight from pounds to grams
"""
df['Weight'] = df['Weight'] * 453.59237
return df
#===========================================================================================================================================
def cmConv(df):
"""
Converts length from cenitmeters to millimeters
"""
df['Length'] = df['Length'] * 10
return df
#===========================================================================================================================================
def kgConv(df):
"""
Converts weight from kilograms to grams
"""
df['Weight'] = df['Weight'] * 1000
return df
#===========================================================================================================================================
def mConv(df):
"""
Converts length from meters to millimeters
"""
df['Length'] = df['Length'] * 1000
return df
#===========================================================================================================================================
def mgConv(df):
"""
Converts weight from milligrams to grams
"""
df['Weight'] = df['Weight'] / 1000
return df
#===========================================================================================================================================
#ask which column is EventDate or use column eventDate (should have it based off READ.md)
def yc(df):
"""
Create and populate yearCollected through the date column
"""
df = df.assign(yearCollected = df['Date'].str[:4])
df = df.rename(columns = {"Date" : "verbatimEventDate"})
return df
#===========================================================================================================================================
def colcheck(df):
"""
Checks dataframe columns and flags column names that do not
match with template.
Template found here: https://github.com/futres/template/blob/master/template.csv
"""
print("Checking Dataframe Columns")
geome_col_names = pd.read_csv("https://raw.githubusercontent.com/futres/fovt-data-mapping/master/Mapping%20Files/template_col_names.csv")
df_col_names = df.columns
error = str(list(set(df_col_names) - set(geome_col_names["Template Column Names"])))
required_columns = ['eventID', 'country','locality','yearCollected','samplingProtocol',
'materialSampleID', 'basisOfRecord','scientificName','diagnosticID',
'measurementMethod','measurementUnit','measurementType','measurementValue']
missing_req = str(list(set(required_columns) - set(df_col_names)))
#have it break if the set difference isn't zero
output = str(f"These column names do not match the template: {error} \n These required columns are missing: {missing_req}")
return output
# # renames columns through user input
# col_names = []
# for i in range(len(df.columns)):
# inpt = input("What would you like column " + str(i + 1) + " to be named?: ")
# col_names.append(inpt)
# df.columns = col_names
# return df
#===========================================================================================================================================
def countryValidity(df):
'''
Checks to make sure all country names are valid according the GENOME.
Valid countries can be found here: https://github.com/futres/fovt-data-mapping/blob/ade4d192a16dd329364362966eaa01d116950e1d/Mapping%20Files/geome_country_list.csv
'''
#print("Checking Validity of Countries")
if "country" in df.columns:
GENOMEcountries = pd.read_csv("https://raw.githubusercontent.com/futres/fovt-data-mapping/master/Mapping%20Files/geome_country_list.csv")
invalid = str(list(set(df["country"]) - set(GENOMEcountries["GEOME_Countries"])))
output = str(f"These countrys found in your data are not recognized by GEOME: {invalid}")
return output
else:
output = str("The ""country"" column was not found in your dataframe, to apply the ""Country Validity"" function this column is required.")
return output
#===========================================================================================================================================
def add_ms_and_evID(df):
"""
Adds unique hex value materialSampleID and eventID to dataframe
"""
df = df.assign(materialSampleID = [uuid.uuid4().hex for _ in range(len(df.index))])
return df
#===========================================================================================================================================
#TODO: dynamically update the id_vars with everything accept the term columns
#How: Let the user give the column names or range id_vars
def dataMelt(df):
"""
Converts dataframe into long format
"""
dataCols = []
dfCols = df.columns.values.tolist()
df = df.assign(verbatimLocality = "")
print(df.columns)
while True:
entry = input('Please select columns from dataframe to melt (type d when done): ')
if entry.lower() == 'd':
break
# elif entry.lower() not in df.columns:
# print ("Column not found in dataframe")
# continue
else:
dataCols.append(entry)
VARS = list(set(dfCols) - set(dataCols))
ID_VARS = np.array(VARS)
df = pd.melt(df, id_vars = ID_VARS, value_vars = dataCols, var_name = 'measurementType', value_name = 'measurementValue')
return df
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # some strings <-> bytes conversions necessary here
href = f'<a href="data:file/csv;base64,{b64}">Download csv file</a>'
#===========================================================================================================================================
st.write("""
# Data Cleaning - [READme](https://github.com/futres/fovt-data-mapping/tree/master/Scripts/pythonConversion#readme)
""")
page_names = ['matSampType', 'verLocal', 'sex', 'uc', 'yc', 'colcheck', 'countryValidity', 'eventID', 'dataMelt' ]
uploadedFile = st.file_uploader("Upload a CSV file", type=['csv'],accept_multiple_files=False)
if uploadedFile:
df = pd.read_csv(uploadedFile)
st.write("""
# Data Pre-Cleaning
""")
st.dataframe(df,width=700, height=208)
st.write("""
# Select which functions you would like applied:""")
matSamp = st.checkbox('Material Sample Type')
verLoc = st.checkbox('Verbatim Locality')
sex_var = st.checkbox('Sex')
uc = st.checkbox('Unit Conversions')
yc_var = st.checkbox('Year Collected')
colCheck = st.checkbox('Column Check')
counVald = st.checkbox('Country Validity')
eveID = st.checkbox('Material Sample ID')
datamelt = st.checkbox('Data Melt')
if True:
if matSamp:
df = remove_rcna(df)
if "materialSampleType" in df.columns:
dct = pd.read_csv("https://raw.githubusercontent.com/futres/fovt-data-mapping/master/Mapping%20Files/MST_dict.csv")
if (df["materialSampleType"].eq(dct["userTerm"])).any():
inpt = st.text_input(f'Would you like to replace {df["materialSampleType"]} with {dct["userTerm"]}? ')
if inpt.lower() == "yes":
df["materialSampleType"] = str(dct["userTerm"])
if inpt.lower() == "no":
count = 0
else:
ask = st.text_input(f'Whould you like to replace {df["materialSampleType"].unique()}?')
if ask.lower() == "yes":
replace = st.text_input(f'What would you like to replace {df["materialSampleType"].unique()} with? ')
if replace:
uniq_vals = []
replace_split = []
uniq_vals = df["materialSampleType"].unique()
d = {}
replace_split = replace.split(",")
for i in range(len(uniq_vals)):
replace = df['materialSampleType'].eq(uniq_vals[i])
replacement = replace_split[i]
df['materialSampleType'][(replace) == True] = replacement
df['materialSampleType'][(replace) == False] = df['materialSampleType']
uniq_vals_new = pd.Series(uniq_vals)
#uniq_vals_new = uniq_vals_new.to_frame()
replace_split = pd.Series(replace_split)
#replace_split = replace_split.to_frame()
dct["userTerm"].append(uniq_vals_new)
dct["replacedWith"].append(replace_split)
#df_download= dct
#csv = df_download.to_csv(index=False)
#b64 = base64.b64encode(csv.encode()).decode() # some strings
if verLoc:
df = remove_rcna(df)
locality_cols= []
if 'verbatimLocality' in df.columns:
number = 0
else:
df = df.assign(verbatimLocality = "")
cols = st.text_input("Input the columns you would like to add to verbaitmLocality seperated by commas")
if cols:
locality_cols = cols.split (",")
df["verbatimLocality"] = df[locality_cols].astype(str).apply(", ".join, axis=1)
if sex_var:
df = remove_rcna(df)
df = sex(df)
if uc:
df = remove_rcna(df)
st.write("What units are your measurements in?")
weight_pages = ["Grams", "Pounds", "Kilograms", "Milligrams"]
length_pages = ["Millimeters","Inches", "Meters", "Centimeters"]
weight = st.radio("Weight Measurements", weight_pages)
length = st.radio("Length Measurements", length_pages)
if weight == "Kilograms":
df = kgConv(df)
if weight == "Milligrams":
df = mgConv(df)
if weight == "Pounds":
df = lbsConv(df)
if length == "Meters":
df = mConv(df)
if length == "Centimeters":
df = cmConv(df)
if length == "Inches":
df = inConv(df)
if yc_var:
df = remove_rcna(df)
df = yc(df)
if colCheck:
df = remove_rcna(df)
if counVald:
df = remove_rcna(df)
if eveID:
df = remove_rcna(df)
df = add_ms_and_evID(df)
if datamelt:
df = remove_rcna(df)
dfCols = df.columns.values.tolist()
if 'verbatimLocality' in df.columns:
number = 0
else:
df = df.assign(verbatimLocality = "")
cols = st.text_input("Input the columns in which you have your weight and length measurements stored in:")
if cols:
dataCols = cols.split (",")
VARS = list(set(dfCols) - set(dataCols))
ID_VARS = np.array(VARS)
df = pd.melt(df, id_vars = ID_VARS, value_vars = dataCols, var_name = 'measurementType', value_name = 'measurementValue')
df = df
st.write("""
# Data Post-Cleaning
""")
st.dataframe(df,width=700, height=208)
if True:
st.write("""
# Feedback
""")
if colCheck:
prnt = colcheck(df)
st.write(prnt)
if counVald:
if "country" in df.columns:
prnt = countryValidity(df)
st.write(prnt)
else:
prnt = countryValidity(df)
st.write(prnt)
if matSamp:
if "materialSampleType" in df.columns:
count = 1
#download = st.button('Generate materialSampleType CSV')
#if download:
#linko= f'<a href="data:file/csv;base64,{b64}" download="MST_dict.csv">Download MST_dict.csv</a>'
#st.markdown(linko, unsafe_allow_html=True)
else:
st.write("The ""materialSampleType"" column was not found in your dataframe, to apply the ""Material Sample Type"" function this column is required.")
clean_df = st.button("Click here to generate the cleaned version of your dataframe in CSV format")
if clean_df:
df_download = df
csv = df_download.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # some strings
linko= f'<a href="data:file/csv;base64,{b64}" download="clean-df.csv">Download clean-df.csv</a>'
st.markdown(linko, unsafe_allow_html=True)