Skip to content

Commit d9f3b67

Browse files
committed
Restructure NMR block to avoid saving all data in db
1 parent e753525 commit d9f3b67

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

pydatalab/src/pydatalab/apps/nmr/blocks.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class NMRBlock(DataBlock):
2323
description = "A data block for loading and visualizing 1D NMR data from Bruker projects or JCAMP-DX files."
2424

2525
accepted_file_extensions = BRUKER_FILE_EXTENSIONS + JCAMP_FILE_EXTENSIONS
26+
processed_data: dict | None = None
2627
defaults = {"process number": 1}
2728
_supports_collections = False
2829

@@ -34,7 +35,7 @@ def read_bruker_nmr_data(
3435
self,
3536
filename: str | Path | None = None,
3637
file_info: dict | None = None,
37-
) -> tuple[dict | None, dict] | None:
38+
) -> tuple[dict | None, dict]:
3839
"""Loads a Bruker project from the passed or attached zip file
3940
and parses it into a serialized dataframe and metadata dictionary.
4041
@@ -119,7 +120,6 @@ def read_bruker_nmr_data(
119120
metadata["title"] = topspin_title
120121

121122
self.data["metadata"] = metadata
122-
self.data["processed_data"] = serialized_df
123123

124124
return serialized_df, metadata
125125

@@ -202,24 +202,26 @@ def read_jcamp_nmr_data(
202202
pass
203203

204204
serialized_df = df.to_dict() if (df is not None) else None
205-
206-
self.data["processed_data"] = serialized_df
207205
self.data["metadata"] = metadata
208206

207+
return serialized_df, metadata
208+
209209
def load_nmr_data(self, file_info: dict):
210210
location, name, ext = self._extract_file_info(file_info=file_info)
211211

212212
if ext == ".zip":
213-
self.read_bruker_nmr_data(file_info=file_info)
213+
df, metadata = self.read_bruker_nmr_data(file_info=file_info)
214214

215215
elif ext in (".jdx", ".dx"):
216-
self.read_jcamp_nmr_data(file_info=file_info)
216+
df, metadata = self.read_jcamp_nmr_data(file_info=file_info)
217217

218218
else:
219219
raise RuntimeError(
220220
f"Unsupported file extension for NMR reader: {ext} (must be one of {self.accepted_file_extensions})"
221221
)
222222

223+
return df
224+
223225
def generate_nmr_plot(self, parse: bool = True):
224226
"""Generate an NMR plot and store processed data for the
225227
data files attached to this block.
@@ -232,7 +234,7 @@ def generate_nmr_plot(self, parse: bool = True):
232234
file_info = get_file_info_by_id(self.data["file_id"], update_if_live=True)
233235
name, ext = os.path.splitext(file_info["name"])
234236

235-
self.load_nmr_data(file_info)
237+
self.processed_data = self.load_nmr_data(file_info)
236238

237239
processed_data_shape = self.data.get("metadata", {}).get("processed_data_shape", [])
238240
if not processed_data_shape or len(processed_data_shape) > 1:
@@ -241,14 +243,14 @@ def generate_nmr_plot(self, parse: bool = True):
241243
)
242244
return
243245

244-
if "processed_data" not in self.data or not self.data["processed_data"]:
246+
if not self.processed_data:
245247
self.data["bokeh_plot_data"] = None
246248
warnings.warn(
247249
"No compatible processed data available for plotting, only metadata will be displayed."
248250
)
249251
return
250252

251-
df = pd.DataFrame(self.data["processed_data"])
253+
df = pd.DataFrame(self.processed_data)
252254
df["normalized intensity"] = df.intensity / df.intensity.max()
253255

254256
self.data["bokeh_plot_data"] = self.make_nmr_plot(df, self.data["metadata"])

0 commit comments

Comments
 (0)