Skip to content

Commit ec31475

Browse files
committed
Make peak dict loading more robust
1 parent 2895eeb commit ec31475

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

candycrunch/prediction.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,9 +1065,37 @@ def load_spectra_filepath(spectra_filepath):
10651065
return loaded_file
10661066
if spectra_filepath.endswith(".xlsx"):
10671067
loaded_file = pd.read_excel(spectra_filepath)
1068-
mask = loaded_file['peak_d'].str.endswith('}', na = False)
1069-
loaded_file = loaded_file[mask]
1070-
loaded_file['peak_d'] = loaded_file['peak_d'].apply(ast.literal_eval)
1068+
1069+
def parse_peak_dict(value):
1070+
def convert_dict(d):
1071+
converted = {}
1072+
for k, v in d.items():
1073+
try:
1074+
key = float(k)
1075+
val = float(v)
1076+
except (TypeError, ValueError):
1077+
continue
1078+
converted[key] = val
1079+
return converted if converted else None
1080+
1081+
if isinstance(value, dict):
1082+
return convert_dict(value)
1083+
if isinstance(value, str):
1084+
text = value.strip()
1085+
if not text:
1086+
return None
1087+
if 'np.float64' in text:
1088+
text = re.sub(r'np\.float64\(([^)]+)\)', r'\1', text)
1089+
try:
1090+
parsed = ast.literal_eval(text)
1091+
except (SyntaxError, ValueError):
1092+
return None
1093+
if isinstance(parsed, dict):
1094+
return convert_dict(parsed)
1095+
return None
1096+
1097+
loaded_file['peak_d'] = loaded_file['peak_d'].apply(parse_peak_dict)
1098+
loaded_file = loaded_file[loaded_file['peak_d'].notnull()].reset_index(drop=True)
10711099
return loaded_file
10721100
if spectra_filepath.endswith('.csv'):
10731101
storage = DictStorage()

0 commit comments

Comments
 (0)