Implemented loading and saving of fits.

This commit is contained in:
jupfi 2024-05-23 16:28:39 +02:00
parent a273bed28a
commit f5b6f3a689

View file

@ -140,15 +140,20 @@ class Measurement:
Measurement : The measurement. Measurement : The measurement.
""" """
tdy = np.array([complex(y[0], y[1]) for y in json["tdy"]]) tdy = np.array([complex(y[0], y[1]) for y in json["tdy"]])
return cls( obj = cls(
json["name"], json["name"],
np.array(json["tdx"]), np.array(json["tdx"]),
tdy, tdy,
target_frequency=json["target_frequency"], target_frequency=json["target_frequency"],
IF_frequency=json["IF_frequency"], IF_frequency=json["IF_frequency"],
fits=[Fit.from_json(fit) for fit in json["fits"]],
) )
# Add fits
for fit in json["fits"]:
obj.add_fit(Fit.from_json(fit, obj))
return obj
# Measurement data # Measurement data
@property @property
def name(self): def name(self):
@ -223,6 +228,12 @@ class Fit():
A fit has a name, a nqrduck function and a strategy for the algorithm to use. A fit has a name, a nqrduck function and a strategy for the algorithm to use.
""" """
subclasses = []
def __init_subclass__(cls, **kwargs):
"""Adds the subclass to the list of subclasses."""
super().__init_subclass__(**kwargs)
cls.subclasses.append(cls)
def __init__(self, name: str, domain: str, measurement : Measurement) -> None: def __init__(self, name: str, domain: str, measurement : Measurement) -> None:
"""Initializes the fit.""" """Initializes the fit."""
@ -230,8 +241,10 @@ class Fit():
self.domain = domain self.domain = domain
self.measurement = measurement self.measurement = measurement
self.fit()
def fit(self): def fit(self):
"""Fits the measurement data, sets the x and y data and returns the fit parameters and covariance. """ """Fits the measurement data, sets the x and y data and sets the fit parameters and covariance. """
if self.domain == "time": if self.domain == "time":
x = self.measurement.tdx x = self.measurement.tdx
y = self.measurement.tdy y = self.measurement.tdy
@ -246,9 +259,9 @@ class Fit():
self.x = x self.x = x
self.y = self.fit_function(x, *parameters) self.y = self.fit_function(x, *parameters)
self.parameters = parameters
return parameters, covariance self.covariance = covariance
def get_fit_parameters_string(self): def get_fit_parameters_string(self):
"""Get the fit parameters as a string. """Get the fit parameters as a string.
@ -287,22 +300,27 @@ class Fit():
""" """
return { return {
"name": self.name, "name": self.name,
"function": self.function.to_json(), "class": self.__class__.__name__,
"strategy": self.strategy,
} }
@classmethod @classmethod
def from_json(cls, json: dict): def from_json(cls, data: dict, measurement : Measurement):
"""Converts the json format to a fit. """Converts the json format to a fit.
Args: Args:
json (dict) : The fit in json-compatible format. data (dict) : The fit in json-compatible format.
measurement (Measurement) : The measurement.
Returns: Returns:
Fit : The fit. Fit : The fit.
""" """
function = Function.from_json(json["function"]) for subclass in cls.subclasses:
return cls(json["name"], function, json["strategy"]) logger.debug(f"Keys data: {data.keys()}")
if subclass.__name__ == data["class"]:
cls = subclass
break
return cls(measurement, data["name"])
@property @property
def x(self): def x(self):
@ -324,18 +342,18 @@ class Fit():
class T2StarFit(Fit): class T2StarFit(Fit):
def __init__(self, measurement: Measurement) -> None: def __init__(self, measurement: Measurement, name = "T2*") -> None:
self.name = "T2*" domain = "time"
self.domain = "time" measurement = measurement
self.measurement = measurement super().__init__(name, domain, measurement)
def fit(self): def fit(self):
parameters, covariance = super().fit() super().fit()
# Create dict with fit parameters and covariance # Create dict with fit parameters and covariance
self.parameters = { self.parameters = {
"S0": parameters[0], "S0": self.parameters[0],
"T2Star": parameters[1], "T2Star": self.parameters[1],
"covariance": covariance "covariance": self.covariance
} }
def fit_function (self, t, S0, T2Star): def fit_function (self, t, S0, T2Star):