Spaces:
Build error
Build error
Upload utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import string
|
3 |
+
|
4 |
+
|
5 |
+
def get_function_mapping(df_metadata, split_ref):
|
6 |
+
df_cp = df_metadata[['function', 'desc', 'split']].copy()
|
7 |
+
df_cp.drop_duplicates(inplace=True)
|
8 |
+
return {function:desc for function, desc, split in df_cp.values if split==split_ref}
|
9 |
+
|
10 |
+
def get_channel_mapping(df_metadata):
|
11 |
+
df_cp = df_metadata[['channel', 'channel_desc']].copy()
|
12 |
+
df_cp.drop_duplicates(inplace=True)
|
13 |
+
return {channel:desc for channel, desc in df_cp.values}
|
14 |
+
|
15 |
+
def get_valid_field(df_metadata):
|
16 |
+
df_cp = df_metadata[["function", "field"]].copy()
|
17 |
+
df_cp.drop_duplicates(inplace=True)
|
18 |
+
field_list = df_cp.field.tolist()
|
19 |
+
function_list = df_cp.function.tolist()
|
20 |
+
field_list_out=[]
|
21 |
+
field_mapping={}
|
22 |
+
for temp_field, temp_function in zip(field_list, function_list):
|
23 |
+
temp_field = temp_field.split(" ### ")
|
24 |
+
temp_field = [x.strip().lower() for x in temp_field if x.strip() != '']
|
25 |
+
temp_field = [x.translate(str.maketrans(' ', '_', string.punctuation)) for x in temp_field]
|
26 |
+
for item in temp_field:
|
27 |
+
if item not in field_list_out:
|
28 |
+
field_list_out.append(item)
|
29 |
+
temp_field = "(" + ", ".join(temp_field) + ")"
|
30 |
+
field_mapping[temp_function] = temp_field
|
31 |
+
return field_list_out, field_mapping
|
32 |
+
|
33 |
+
def get_channel_to_function_mapping(df_metadata):
|
34 |
+
df_cp = df_metadata[["channel", "function"]].copy()
|
35 |
+
df_cp.drop_duplicates(inplace=True)
|
36 |
+
out_dict={}
|
37 |
+
for temp_channel, temp_function in df_cp.values:
|
38 |
+
if temp_channel not in out_dict.keys():
|
39 |
+
out_dict[temp_channel] = []
|
40 |
+
temp_function = temp_function.split(".")[-1]
|
41 |
+
out_dict[temp_channel].append(temp_function)
|
42 |
+
out_dict = {k:", ".join(v) for k,v in out_dict.items()}
|
43 |
+
return out_dict
|
44 |
+
|
45 |
+
def get_metadata(path):
|
46 |
+
df_metadata = pd.read_csv(path)
|
47 |
+
df_metadata = df_metadata.fillna('')
|
48 |
+
function_dict_trigger = get_function_mapping(df_metadata=df_metadata, split_ref='trigger')
|
49 |
+
function_dict_action = get_function_mapping(df_metadata=df_metadata, split_ref='action')
|
50 |
+
channel_dict = get_channel_mapping(df_metadata=df_metadata)
|
51 |
+
valid_field,field_mapping = get_valid_field(df_metadata=df_metadata)
|
52 |
+
channel_to_function_dict = get_channel_to_function_mapping(df_metadata=df_metadata)
|
53 |
+
return channel_dict, function_dict_trigger, function_dict_action, field_mapping, valid_field, channel_to_function_dict
|
54 |
+
|
55 |
+
def append_prefix(desc, prefix):
|
56 |
+
return prefix + desc
|
57 |
+
|
58 |
+
def append_suffix(desc, suffix):
|
59 |
+
return desc + suffix
|
60 |
+
|
61 |
+
def process_field(raw_field):
|
62 |
+
field = raw_field.split(" ### ")
|
63 |
+
field = [x.strip().lower() for x in field if x.strip() != '']
|
64 |
+
field = [x.translate(str.maketrans(' ', '_', string.punctuation)) for x in field]
|
65 |
+
return field
|