Skip to content

Commit a70f392

Browse files
committed
fixed bugs with runner
1 parent 1b00529 commit a70f392

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

modifinder/runner.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def run_single(match_index, network = None, networkUnknowns = None, unknown_comp
4646
if network is not None:
4747
mf = ModiFinder(network = network, networkUnknowns = networkUnknowns, **kwargs)
4848
else:
49-
mf = ModiFinder()
49+
mf = ModiFinder(**kwargs)
5050
unknown_compound = load_Compound_from_cache(unknown_compound, cached_compounds, **kwargs)
5151
if unknown_compound is None:
5252
raise ValueError("Unknown compound is not found.")
@@ -96,35 +96,40 @@ def run_single(match_index, network = None, networkUnknowns = None, unknown_comp
9696

9797
for known_index, node in enumerate(knowns):
9898
try:
99+
result = {
100+
"match_index": match_index,
101+
"unknown_id": unknown_id,
102+
"known_id": node,
103+
}
99104
known_compound = mf.network.nodes[node]["compound"]
100-
probs = mf.generate_probabilities(unknown_id=unknown_id, known_id=node, **kwargs)
105+
if abs(known_compound.spectrum.precursor_mz - unknown_compound.spectrum.precursor_mz) < 0.1:
106+
# exact match
107+
probs = [0] * len(known_compounds)
108+
result["entropy"] = None
109+
else:
110+
probs = mf.generate_probabilities(unknown_id=unknown_id, known_id=node, **kwargs)
111+
result["entropy"] = entropy(probs)
101112
data = mf.get_meta_data(unknown_id=unknown_id, known_id=node)
102113
try:
103114
if images_name is not None:
104115
if isinstance(images_name, str):
105116
data['image_path'] = os.path.join(output_dir, images_name + str(match_index) + ".png")
106117
else:
107118
data['image_path'] = os.path.join(output_dir, images_name[known_index])
108-
png = mf_vis.draw_molecule(known_compound.structure)
119+
png = mf_vis.draw_molecule_heatmap(known_compound.structure, probs, show_labels=True, shrink_labels=True, show_legend=False)
109120
plt.imsave(data['image_path'], png)
110121
except Exception as err:
111122
data['image_path'] = str(err)
112123
pass
113-
result = {
114-
"match_index": match_index,
115-
"unknown_id": unknown_id,
116-
"known_id": node,
117-
}
118124
result.update(data)
119-
120125
if unknown_compound.structure:
121126
for method in ["is_max", "average_distance"]:
122127
evaluation_result = evaluation_engine.evaluate_single(known_compound.structure, unknown_compound.structure,
123128
probs, evaluation_method=method, **kwargs)
124129
result[method] = evaluation_result
125130

126131
# add entropy of the probabilities
127-
result["entropy"] = entropy(probs)
132+
128133
result["error"] = "No Issues"
129134
final_result[known_index] = result
130135

0 commit comments

Comments
 (0)