Fixed loading and saving of measurement data.

This commit is contained in:
jupfi 2024-12-29 17:18:43 +01:00
parent 214c7d5743
commit 93d9b89c36

View file

@ -64,7 +64,7 @@ class Measurement:
""" """
# Add the y data in the second dimension # Add the y data in the second dimension
self.tdy = np.concatenate((self.tdy, tdy), axis=1) self.tdy = np.concatenate((self.tdy, tdy), axis=1)
_, fdy = sp.fft(self.tdx, tdy, self.frequency_shift) _, fdy = sp.fft(self.tdx, tdy, self.frequency_shift)
self.fdy = np.concatenate((self.fdy, fdy), axis=1) self.fdy = np.concatenate((self.fdy, fdy), axis=1)
def apodization(self, function: Function) -> "Measurement": def apodization(self, function: Function) -> "Measurement":
@ -140,7 +140,7 @@ class Measurement:
return { return {
"name": self.name, "name": self.name,
"tdx": self.tdx.tolist(), "tdx": self.tdx.tolist(),
"tdy": [[x.real, x.imag] for x in self.tdy], "tdy": self.serialize_complex_array(self.tdy),
"target_frequency": self.target_frequency, "target_frequency": self.target_frequency,
"IF_frequency": self.IF_frequency, "IF_frequency": self.IF_frequency,
"fits": [fit.to_json() for fit in self.fits], "fits": [fit.to_json() for fit in self.fits],
@ -156,7 +156,8 @@ class Measurement:
Returns: Returns:
Measurement: The measurement. Measurement: The measurement.
""" """
tdy = np.array([complex(y[0], y[1]) for y in json["tdy"]]) tdy = np.array(json["tdy"][0]) + 1j * np.array(json["tdy"][1])
measurement = cls( measurement = cls(
json["name"], json["name"],
np.array(json["tdx"]), np.array(json["tdx"]),
@ -169,6 +170,17 @@ class Measurement:
measurement.add_fit(Fit.from_json(fit_json, measurement)) measurement.add_fit(Fit.from_json(fit_json, measurement))
return measurement return measurement
def serialize_complex_array(self, array: np.array) -> list:
"""Serializes a complex array to a list.
Args:
array (np.array): The complex array.
Returns:
list: The serialized complex array.
"""
return [array.real.tolist(), array.imag.tolist()]
# Properties for encapsulation # Properties for encapsulation
@property @property