Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +50 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
from itertools import islice
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def guarantee_multiindex_rows(df):
|
| 9 |
+
if not isinstance(df.index, pd.MultiIndex):
|
| 10 |
+
path = df.index[0]
|
| 11 |
+
try:
|
| 12 |
+
sep = "/" if "/" in path else "\\"
|
| 13 |
+
splits = tuple(df.index.str.split(sep))
|
| 14 |
+
df.index = pd.MultiIndex.from_tuples(splits)
|
| 15 |
+
except TypeError:
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
df.index = df.index.set_levels(df.index.levels[1].astype(str), level=1)
|
| 20 |
+
except AttributeError:
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def convertcsv2h5(csv_name):
|
| 25 |
+
csv_name = csv_name.name
|
| 26 |
+
csv_path = os.path.splitext(csv_name)[0]
|
| 27 |
+
scorer = csv_path.split('_')[1]
|
| 28 |
+
with open(csv_name) as datafile:
|
| 29 |
+
head = list(islice(datafile, 0, 5))
|
| 30 |
+
if "individuals" in head[1]:
|
| 31 |
+
header = list(range(4))
|
| 32 |
+
else:
|
| 33 |
+
header = list(range(3))
|
| 34 |
+
if head[-1].split(",")[0] == "labeled-data":
|
| 35 |
+
index_col = [0, 1, 2]
|
| 36 |
+
else:
|
| 37 |
+
index_col = 0
|
| 38 |
+
data = pd.read_csv(csv_name, index_col=index_col, header=header)
|
| 39 |
+
data.columns = data.columns.set_levels([scorer], level="scorer")
|
| 40 |
+
guarantee_multiindex_rows(data)
|
| 41 |
+
|
| 42 |
+
with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as temp_file:
|
| 43 |
+
temp_file_path = csv_name.replace(".csv", ".h5")
|
| 44 |
+
data.to_hdf(temp_file_path, key="df_with_missing", mode="w")
|
| 45 |
+
|
| 46 |
+
return temp_file_path
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
iface = gr.Interface(fn=convertcsv2h5, inputs="file", outputs="file")
|
| 50 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pandas
|