sammlapp's picture
Upload folder using huggingface_hub (part 4)
524a318 verified
Raw History Blame Contribute Delete
1.23 kB
import re
import ast
import pandas as pd
import numpy as np
def parse_list_of_lists(cell):
# Replace newline characters and convert the cell string to a list of lists
cell = (
re.sub(r"\s+", ",", cell.replace("\n", ""))
.replace("[,", "[")
.replace(",,", ",")
)
# cell = cell.strip(', ')
return ast.literal_eval(cell)
def load_event_df(path):
"""load CSV containing localized events, parsing columns that contain lists"""
df = pd.read_csv(
path,
index_col=0,
converters={
"receiver_files": parse_list_of_lists,
"receiver_locations": parse_list_of_lists,
"tdoas": parse_list_of_lists,
"cc_maxs": parse_list_of_lists,
"receiver_start_time_offsets": parse_list_of_lists,
"distance_residuals": parse_list_of_lists,
"location_estimate": parse_list_of_lists,
},
parse_dates=["start_timestamp"],
)
df["residual_rms"] = np.sqrt(
df["distance_residuals"].apply(lambda x: np.mean(np.square(x)))
)
df["x"] = np.array(list(df.location_estimate.values))[:, 0]
df["y"] = np.array(list(df.location_estimate.values))[:, 1]
return df