From f34f259221b5e9b9fd52b00843320f10e2c017f1 Mon Sep 17 00:00:00 2001 From: Brett Eiffert Date: Wed, 15 May 2024 08:32:54 -0400 Subject: [PATCH 1/4] add dataloader and r curve generator --- src/tgreft/utils/data/data.json | 58 ++++++ src/tgreft/utils/data/dataloader.py | 71 +++++++ src/tgreft/utils/data/r_curve_generator.py | 210 +++++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 src/tgreft/utils/data/data.json create mode 100644 src/tgreft/utils/data/dataloader.py create mode 100644 src/tgreft/utils/data/r_curve_generator.py diff --git a/src/tgreft/utils/data/data.json b/src/tgreft/utils/data/data.json new file mode 100644 index 0000000..6fa1507 --- /dev/null +++ b/src/tgreft/utils/data/data.json @@ -0,0 +1,58 @@ +{ + "parameters": { + "q_min": 0.008, + "q_max": 0.2, + "step_size": 0.015, + "FINAL_SEI_SLD": 6.13, + "SIMULATE_ERRORS": true, + "ERR": 0.15, + "total_time": 50, + "transition_time": 3, + "transition_midpoint": 15 + }, + "materials": [ + { + "name": "Si", + "rho": 2.07, + "irho": 0, + "thickness": 0, + "roughness": 0 + }, + { + "name": "THF", + "rho": 6.13, + "irho": 0, + "thickness": 0, + "roughness": 43.77 + }, + { + "name": "Ti", + "rho": -1.238, + "irho": 0, + "thickness": 52.91, + "roughness": 12.7 + }, + { + "name": "Cu", + "rho": 6.446, + "irho": 0, + "thickness": 566.1, + "roughness": 9.736 + }, + { + "name": "material", + "rho": -1.648, + "irho": 0, + "thickness": 21.73, + "roughness": 18.22 + }, + { + "name": "SEI", + "rho": 4.581, + "irho": 0, + "thickness": 177.7, + "roughness": 23.04 + } + ] + +} diff --git a/src/tgreft/utils/data/dataloader.py b/src/tgreft/utils/data/dataloader.py new file mode 100644 index 0000000..a58c62d --- /dev/null +++ b/src/tgreft/utils/data/dataloader.py @@ -0,0 +1,71 @@ +from refl1d.names import * + +class DataLoader: + + def __init__(self, json_dict: dict): + """Initialize the class. + + Parameters + ---------- + json_dict : json dictionary + json dictionary of all parameters and materials to stack in a sample. + """ + self.json_dict = json_dict + self.q_min = json_dict["parameters"]["q_min"] + self.q_max = json_dict["parameters"]["q_max"] + self.step_size = json_dict["parameters"]["step"] + self.final_sei_sld = json_dict["parameters"]["FINAL_SEI_SLD"] + self.simulate_errors = json_dict["parameters"]["SIMULATE_ERRORS"] + self.err = json_dict["parameters"]["ERR"] + self.total_time = json_dict["parameters"]["total_time"] + self.transition_time = json_dict["parameters"]["transition_time"] + self.transition_midpoint = json_dict["parameters"]["transition_midpoint"] + self.sample = self.create_sample_from_json(json_dict["materials"]) + + def create_slab_chunk(self, material : dict) -> "Slab": + + """Return a slab chunk built from given configuration. + + Parameters + ---------- + material : dictionary + Single material dictionary to turn into slab chunk. + + Returns + ------- + Slab + The slab chunk instance. + """ + + slab = Slab( + material=SLD( + name=material["name"], + rho=material["rho"], + irho=material["irho"], + ), + thickness=material["thickness"], + interface=material["roughness"], + ) + + return slab + + def create_sample_from_json(self, json_object : list) -> "Slab | Stack": + + """Return a slab instance built from given configuration. + + Parameters + ---------- + json_object : list + The sample configuration dictionary list. + + Returns + ------- + Slab + The slab instance. + """ + + sample = self.create_slab_chunk(json_object[0]) + for i in range(1, len(json_object)): + sample = sample | self.create_slab_chunk(json_object[i]) + + return sample \ No newline at end of file diff --git a/src/tgreft/utils/data/r_curve_generator.py b/src/tgreft/utils/data/r_curve_generator.py new file mode 100644 index 0000000..47b229b --- /dev/null +++ b/src/tgreft/utils/data/r_curve_generator.py @@ -0,0 +1,210 @@ +import json + +import numpy as np +import math + +from refl1d.names import * + +from dataloader import DataLoader + +class RCurveGenerator: + + def __init__(self, data : DataLoader): + + """Initialize the class. + + Parameters + ---------- + data : DataLoader object + Object of parsed data to use with RCurveGenerator and related functions + """ + + self.data = data + self.probe, self.tr_probe = self.create_probes(data.q_min, data.q_max, res=data.step) + self.expt = Experiment(probe=self.probe, sample=self.data.sample) + self.r_final, self.r_init = self.get_states(self.expt, self.data.final_sei_sld, self.data.simulate_errors, self.data.err) + self.tr_expt = Experiment(probe=self.tr_probe, sample=self.data.sample) + self.tr_data, self.rho_data = self.generate_tNR(self.tr_expt, self.data.total_time, self.data.transition_time, self.data.transition_midpoint, self.data.final_sei_sld, self.data.simulate_errors, self.data.err) + + def create_probes(self, q_min : float, q_max : float, step_size : float, resolution : float = 0.028) -> tuple: + + """Returns two probes, one using the full data and one with a slice, in the form of a tuple. + + Parameters + ---------- + q_min : float + minimum of the q range + + q_max : float + maximum of the q range + + res : float + step size between min and max + + Returns + ------- + tuple + tuple containing both relevant probes + """ + + q = np.arange(np.log(q_min), np.log(q_max), step_size) + q = np.exp(q) + + dq = resolution*q + + # Time-resolved Q range + q_tr = q[50:130] + dq_tr = dq[50:130] + + probe = QProbe(q, dq, data=None) + + # If using data for fits, replace data by the data to fit: + # probe = QProbe(q, dq, data=(data, errors)) + + tr_probe = QProbe(q_tr, dq_tr, data=None) + + return probe, tr_probe + + def get_states(self, expt, final_sei_sld, simulate_errors, err) -> tuple: + + """Returns a tuple containing both the initial and final r states. + + Parameters + ---------- + expt : Experiment + Experiment object - full range + final_sei_sld : float + final sei sld contant + simulate_errors : boolean + boolean indicating whether you want to include error or not in simulation + err : float + error constant + + Returns + ------- + tuple + Tuple of both r_final and r_init lists + """ + + # Final state ################################################################## + _, r_final = expt.reflectivity() + if simulate_errors: + r_final = np.random.normal(r_final, err*r_final) + + + # Initial state ################################################################ + expt.sample['SEI'].material.rho.value = final_sei_sld + expt.update() + _, r_init = expt.reflectivity() + if simulate_errors: + r_init = np.random.normal(r_init, err*r_init) + + return r_final, r_init + + def generate_tNR(self, tr_expt, total_time, transition_time, transition_midpoint, final_sei_sld, simulate_errors, err) -> tuple: + """Return a tuple built containing tr_data and rho_data. + + Parameters + ---------- + tr_expt : Experiment + Subset of experiment + total_time : int + + transition_time : int + + transition_midpoint : int + + final_sei_sld : float + + simulate_errors : bool + + err : float + + Returns + ------- + tuple + tuple of both tr_data and rho_data lists + """ + + tr_data = [] + rho_data = [] + for i in range(total_time): + # This is a simple ERF transition, but it could be any function! + rho_sei = final_sei_sld - (final_sei_sld-4.581) * (1 + math.erf((i-transition_midpoint)/transition_time)) / 2 + + tr_expt.sample['SEI'].material.rho.value = rho_sei + tr_expt.update() + _, _r = tr_expt.reflectivity() + if simulate_errors: + _r = np.random.normal(_r, err*_r) + + rho_data.append(rho_sei) + tr_data.append(_r) + + tr_data = np.asarray(tr_data) + return tr_data, rho_data + + def get_r_final(self) -> list: + + """Returns a list from the RCurveGenerator object instance. + + Returns + ------- + list + r_final list + """ + + return self.r_final + + def get_r_init(self) -> list: + + """Returns a list from the object instance. + + Returns + ------- + list + r_init list + """ + + return self.r_init + + def get_tr_data(self) -> list: + + """Returns a list from the object instance. + + Returns + ------- + list + tr_data list + """ + + return self.tr_data + + def get_rho_data(self) -> list: + + """Returns a list from the object instance. + + Returns + ------- + list + rho_data list + """ + + return self.rho_data + +""" USE EXAMPLE +if __name__ == "__main__": + f = open("data.json","r") + data = json.load(f) + dataloader = DataLoader(data) + rc = RCurveGenerator(dataloader) + print(rc.get_r_init()) + + #gen = RCurveGenerator(0.008, 0.2) + #print(gen.q) +""" + + + + + -- GitLab From 905c6bac1f7b65fc8b4a04b5f08f674c33c56736 Mon Sep 17 00:00:00 2001 From: Brett Eiffert Date: Thu, 16 May 2024 15:44:50 -0400 Subject: [PATCH 2/4] style and code improvements --- src/tgreft/utils/data/dataloader.py | 70 +++-- src/tgreft/utils/data/r_curve_generator.py | 294 +++++++++++---------- 2 files changed, 198 insertions(+), 166 deletions(-) diff --git a/src/tgreft/utils/data/dataloader.py b/src/tgreft/utils/data/dataloader.py index a58c62d..14ef48c 100644 --- a/src/tgreft/utils/data/dataloader.py +++ b/src/tgreft/utils/data/dataloader.py @@ -1,29 +1,52 @@ -from refl1d.names import * +from refl1d.names import ( + Slab, + SLD, +) + class DataLoader: + """Add description of the class here""" + + default_dict = { + "parameters": { + "q_min": 0.008, + "q_max": 0.2, + "step_size": 0.015, + "FINAL_SEI_SLD": 6.13, + "SIMULATE_ERRORS": True, + "ERR": 0.15, + "total_time": 50, + "transition_time": 3, + "transition_midpoint": 15, + } + } - def __init__(self, json_dict: dict): + def __init__(self, input_dict: dict): """Initialize the class. Parameters ---------- - json_dict : json dictionary - json dictionary of all parameters and materials to stack in a sample. + input_dict : dictionary + Dictionary of all parameters and materials to stack in a sample. """ - self.json_dict = json_dict - self.q_min = json_dict["parameters"]["q_min"] - self.q_max = json_dict["parameters"]["q_max"] - self.step_size = json_dict["parameters"]["step"] - self.final_sei_sld = json_dict["parameters"]["FINAL_SEI_SLD"] - self.simulate_errors = json_dict["parameters"]["SIMULATE_ERRORS"] - self.err = json_dict["parameters"]["ERR"] - self.total_time = json_dict["parameters"]["total_time"] - self.transition_time = json_dict["parameters"]["transition_time"] - self.transition_midpoint = json_dict["parameters"]["transition_midpoint"] - self.sample = self.create_sample_from_json(json_dict["materials"]) - - def create_slab_chunk(self, material : dict) -> "Slab": - + + self.param = input_dict.get("parameters", self.default_dict["parameters"]) + if self.param: + self.q_min = self.param.get("q_min", self.default_dict["parameters"]["q_min"]) + self.q_max = self.param.get("q_max", self.default_dict["parameters"]["q_max"]) + self.step_size = self.param.get("step_size", self.default_dict["parameters"]["step_size"]) + self.final_sei_sld = self.param.get("FINAL_SEI_SLD", self.default_dict["parameters"]["FINAL_SEI_SLD"]) + self.simulate_errors = self.param.get("SIMULATE_ERRORS", self.default_dict["parameters"]["SIMULATE_ERRORS"]) + self.err = self.param.get("ERR", self.default_dict["parameters"]["ERR"]) + self.total_time = self.param.get("total_time", self.default_dict["parameters"]["total_time"]) + self.transition_time = self.param.get("transition_time", self.default_dict["parameters"]["transition_time"]) + self.transition_midpoint = self.param.get( + "transition_midpoint", self.default_dict["parameters"]["transition_midpoint"] + ) + + self.sample = self.create_sample_from_dict(input_dict.get("materials")) + + def create_slab_chunk(self, material: dict) -> "Slab": """Return a slab chunk built from given configuration. Parameters @@ -49,8 +72,7 @@ class DataLoader: return slab - def create_sample_from_json(self, json_object : list) -> "Slab | Stack": - + def create_sample_from_dict(self, input_dict: list) -> Slab: """Return a slab instance built from given configuration. Parameters @@ -64,8 +86,8 @@ class DataLoader: The slab instance. """ - sample = self.create_slab_chunk(json_object[0]) - for i in range(1, len(json_object)): - sample = sample | self.create_slab_chunk(json_object[i]) + sample = self.create_slab_chunk(input_dict[0]) + for i in range(1, len(input_dict)): + sample = sample | self.create_slab_chunk(input_dict[i]) - return sample \ No newline at end of file + return sample diff --git a/src/tgreft/utils/data/r_curve_generator.py b/src/tgreft/utils/data/r_curve_generator.py index 47b229b..29a32a7 100644 --- a/src/tgreft/utils/data/r_curve_generator.py +++ b/src/tgreft/utils/data/r_curve_generator.py @@ -3,14 +3,13 @@ import json import numpy as np import math -from refl1d.names import * +from refl1d.names import QProbe, Experiment from dataloader import DataLoader -class RCurveGenerator: - - def __init__(self, data : DataLoader): +class RCurveGenerator: + def __init__(self, data: DataLoader): """Initialize the class. Parameters @@ -20,132 +19,24 @@ class RCurveGenerator: """ self.data = data - self.probe, self.tr_probe = self.create_probes(data.q_min, data.q_max, res=data.step) + self.probe, self.tr_probe = create_probes(data.q_min, data.q_max, data.step_size) self.expt = Experiment(probe=self.probe, sample=self.data.sample) - self.r_final, self.r_init = self.get_states(self.expt, self.data.final_sei_sld, self.data.simulate_errors, self.data.err) + self.r_final, self.r_init = get_states( + self.expt, self.data.final_sei_sld, self.data.simulate_errors, self.data.err + ) self.tr_expt = Experiment(probe=self.tr_probe, sample=self.data.sample) - self.tr_data, self.rho_data = self.generate_tNR(self.tr_expt, self.data.total_time, self.data.transition_time, self.data.transition_midpoint, self.data.final_sei_sld, self.data.simulate_errors, self.data.err) - - def create_probes(self, q_min : float, q_max : float, step_size : float, resolution : float = 0.028) -> tuple: - - """Returns two probes, one using the full data and one with a slice, in the form of a tuple. - - Parameters - ---------- - q_min : float - minimum of the q range - - q_max : float - maximum of the q range - - res : float - step size between min and max - - Returns - ------- - tuple - tuple containing both relevant probes - """ - - q = np.arange(np.log(q_min), np.log(q_max), step_size) - q = np.exp(q) - - dq = resolution*q - - # Time-resolved Q range - q_tr = q[50:130] - dq_tr = dq[50:130] - - probe = QProbe(q, dq, data=None) - - # If using data for fits, replace data by the data to fit: - # probe = QProbe(q, dq, data=(data, errors)) - - tr_probe = QProbe(q_tr, dq_tr, data=None) - - return probe, tr_probe - - def get_states(self, expt, final_sei_sld, simulate_errors, err) -> tuple: - - """Returns a tuple containing both the initial and final r states. - - Parameters - ---------- - expt : Experiment - Experiment object - full range - final_sei_sld : float - final sei sld contant - simulate_errors : boolean - boolean indicating whether you want to include error or not in simulation - err : float - error constant - - Returns - ------- - tuple - Tuple of both r_final and r_init lists - """ - - # Final state ################################################################## - _, r_final = expt.reflectivity() - if simulate_errors: - r_final = np.random.normal(r_final, err*r_final) - - - # Initial state ################################################################ - expt.sample['SEI'].material.rho.value = final_sei_sld - expt.update() - _, r_init = expt.reflectivity() - if simulate_errors: - r_init = np.random.normal(r_init, err*r_init) - - return r_final, r_init - - def generate_tNR(self, tr_expt, total_time, transition_time, transition_midpoint, final_sei_sld, simulate_errors, err) -> tuple: - """Return a tuple built containing tr_data and rho_data. - - Parameters - ---------- - tr_expt : Experiment - Subset of experiment - total_time : int - - transition_time : int - - transition_midpoint : int - - final_sei_sld : float - - simulate_errors : bool - - err : float - - Returns - ------- - tuple - tuple of both tr_data and rho_data lists - """ - - tr_data = [] - rho_data = [] - for i in range(total_time): - # This is a simple ERF transition, but it could be any function! - rho_sei = final_sei_sld - (final_sei_sld-4.581) * (1 + math.erf((i-transition_midpoint)/transition_time)) / 2 - - tr_expt.sample['SEI'].material.rho.value = rho_sei - tr_expt.update() - _, _r = tr_expt.reflectivity() - if simulate_errors: - _r = np.random.normal(_r, err*_r) - - rho_data.append(rho_sei) - tr_data.append(_r) - - tr_data = np.asarray(tr_data) - return tr_data, rho_data - + self.tr_data, self.rho_data = generate_tNR( + self.tr_expt, + self.data.total_time, + self.data.transition_time, + self.data.transition_midpoint, + self.data.final_sei_sld, + self.data.simulate_errors, + self.data.err, + ) + + @property def get_r_final(self) -> list: - """Returns a list from the RCurveGenerator object instance. Returns @@ -156,8 +47,8 @@ class RCurveGenerator: return self.r_final + @property def get_r_init(self) -> list: - """Returns a list from the object instance. Returns @@ -168,8 +59,8 @@ class RCurveGenerator: return self.r_init + @property def get_tr_data(self) -> list: - """Returns a list from the object instance. Returns @@ -180,8 +71,8 @@ class RCurveGenerator: return self.tr_data + @property def get_rho_data(self) -> list: - """Returns a list from the object instance. Returns @@ -191,20 +82,139 @@ class RCurveGenerator: """ return self.rho_data - -""" USE EXAMPLE -if __name__ == "__main__": - f = open("data.json","r") - data = json.load(f) - dataloader = DataLoader(data) - rc = RCurveGenerator(dataloader) - print(rc.get_r_init()) - - #gen = RCurveGenerator(0.008, 0.2) - #print(gen.q) -""" +def create_probes(q_min: float, q_max: float, step_size: float, resolution: float = 0.028) -> tuple: + """Returns two probes, one using the full data and one with a slice, in the form of a tuple. + + Parameters + ---------- + q_min : float + minimum of the q range + + q_max : float + maximum of the q range + + res : float + step size between min and max + + Returns + ------- + tuple + tuple containing both relevant probes + """ + + q = np.arange(np.log(q_min), np.log(q_max), step_size) + q = np.exp(q) + + dq = resolution * q + + # Time-resolved Q range + q_tr = q[50:130] + dq_tr = dq[50:130] + + probe = QProbe(q, dq, data=None) + + # If using data for fits, replace data by the data to fit: + # probe = QProbe(q, dq, data=(data, errors)) + + tr_probe = QProbe(q_tr, dq_tr, data=None) + + return probe, tr_probe + + +def get_states(expt: Experiment, final_sei_sld: float, simulate_errors: bool, err: float) -> tuple: + """Returns a tuple containing both the initial and final r states. + Parameters + ---------- + expt : Experiment + Experiment object - full range + final_sei_sld : float + final sei sld contant + simulate_errors : boolean + boolean indicating whether you want to include error or not in simulation + err : float + error constant + Returns + ------- + tuple + Tuple of both r_final and r_init lists + """ + # Final state ################################################################## + _, r_final = expt.reflectivity() + if simulate_errors: + r_final = np.random.normal(r_final, err * r_final) + + # Initial state ################################################################ + expt.sample["SEI"].material.rho.value = final_sei_sld + expt.update() + _, r_init = expt.reflectivity() + if simulate_errors: + r_init = np.random.normal(r_init, err * r_init) + + return r_final, r_init + + +def generate_tNR( + tr_expt: Experiment, + total_time: int, + transition_time: int, + transition_midpoint: int, + final_sei_sld: float, + simulate_errors: bool, + err: float, +) -> tuple: + """Return a tuple built containing tr_data and rho_data. + + Parameters + ---------- + tr_expt : Experiment + Subset of experiment + total_time : int + + transition_time : int + + transition_midpoint : int + + final_sei_sld : float + + simulate_errors : bool + + err : float + + Returns + ------- + tuple + tuple of both tr_data and rho_data lists + """ + + tr_data = [] + rho_data = [] + for i in range(total_time): + # This is a simple ERF transition, but it could be any function! + rho_sei = ( + final_sei_sld - (final_sei_sld - 4.581) * (1 + math.erf((i - transition_midpoint) / transition_time)) / 2 + ) + + tr_expt.sample["SEI"].material.rho.value = rho_sei + tr_expt.update() + _, _r = tr_expt.reflectivity() + if simulate_errors: + _r = np.random.normal(_r, err * _r) + + rho_data.append(rho_sei) + tr_data.append(_r) + + tr_data = np.asarray(tr_data) + return tr_data, rho_data + + +if __name__ == "__main__": + f = open("data.json", "r") + data = json.load(f) + dataloader = DataLoader(data) + rc = RCurveGenerator(dataloader) + print(rc.get_r_init) -- GitLab From ec1cff2aa76f0bc7a0e328620436880a0be661bf Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Fri, 17 May 2024 14:20:14 -0400 Subject: [PATCH 3/4] move to better location --- resource/example_config/dataloader.json | 58 +++++++++++++++++++++++++ src/tgreft/utils/data/data.json | 58 ------------------------- 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 resource/example_config/dataloader.json delete mode 100644 src/tgreft/utils/data/data.json diff --git a/resource/example_config/dataloader.json b/resource/example_config/dataloader.json new file mode 100644 index 0000000..c5a310a --- /dev/null +++ b/resource/example_config/dataloader.json @@ -0,0 +1,58 @@ +{ + "parameters": { + "q_min": 0.008, + "q_max": 0.2, + "step_size": 0.015, + "FINAL_SEI_SLD": 6.13, + "SIMULATE_ERRORS": true, + "ERR": 0.15, + "total_time": 50, + "transition_time": 3, + "transition_midpoint": 15 + }, + "materials": [ + { + "name": "Si", + "rho": 2.07, + "irho": 0, + "thickness": 0, + "roughness": 0 + }, + { + "name": "THF", + "rho": 6.13, + "irho": 0, + "thickness": 0, + "roughness": 43.77 + }, + { + "name": "Ti", + "rho": -1.238, + "irho": 0, + "thickness": 52.91, + "roughness": 12.7 + }, + { + "name": "Cu", + "rho": 6.446, + "irho": 0, + "thickness": 566.1, + "roughness": 9.736 + }, + { + "name": "material", + "rho": -1.648, + "irho": 0, + "thickness": 21.73, + "roughness": 18.22 + }, + { + "name": "SEI", + "rho": 4.581, + "irho": 0, + "thickness": 177.7, + "roughness": 23.04 + } + ] + +} diff --git a/src/tgreft/utils/data/data.json b/src/tgreft/utils/data/data.json deleted file mode 100644 index 6fa1507..0000000 --- a/src/tgreft/utils/data/data.json +++ /dev/null @@ -1,58 +0,0 @@ -{ - "parameters": { - "q_min": 0.008, - "q_max": 0.2, - "step_size": 0.015, - "FINAL_SEI_SLD": 6.13, - "SIMULATE_ERRORS": true, - "ERR": 0.15, - "total_time": 50, - "transition_time": 3, - "transition_midpoint": 15 - }, - "materials": [ - { - "name": "Si", - "rho": 2.07, - "irho": 0, - "thickness": 0, - "roughness": 0 - }, - { - "name": "THF", - "rho": 6.13, - "irho": 0, - "thickness": 0, - "roughness": 43.77 - }, - { - "name": "Ti", - "rho": -1.238, - "irho": 0, - "thickness": 52.91, - "roughness": 12.7 - }, - { - "name": "Cu", - "rho": 6.446, - "irho": 0, - "thickness": 566.1, - "roughness": 9.736 - }, - { - "name": "material", - "rho": -1.648, - "irho": 0, - "thickness": 21.73, - "roughness": 18.22 - }, - { - "name": "SEI", - "rho": 4.581, - "irho": 0, - "thickness": 177.7, - "roughness": 23.04 - } - ] - -} -- GitLab From d3f5a1578f309aa49788e1afef7d7d468c0543f0 Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Fri, 17 May 2024 14:20:53 -0400 Subject: [PATCH 4/4] light refactor --- src/tgreft/utils/data/dataloader.py | 80 ++++++++++++---------- src/tgreft/utils/data/r_curve_generator.py | 46 +++++++------ 2 files changed, 66 insertions(+), 60 deletions(-) diff --git a/src/tgreft/utils/data/dataloader.py b/src/tgreft/utils/data/dataloader.py index 14ef48c..367aa0c 100644 --- a/src/tgreft/utils/data/dataloader.py +++ b/src/tgreft/utils/data/dataloader.py @@ -1,3 +1,4 @@ +"""Config parser module for synthetic data generation.""" from refl1d.names import ( Slab, SLD, @@ -5,7 +6,7 @@ from refl1d.names import ( class DataLoader: - """Add description of the class here""" + """Load config and make sample from config dict.""" default_dict = { "parameters": { @@ -31,6 +32,7 @@ class DataLoader: """ self.param = input_dict.get("parameters", self.default_dict["parameters"]) + if self.param: self.q_min = self.param.get("q_min", self.default_dict["parameters"]["q_min"]) self.q_max = self.param.get("q_max", self.default_dict["parameters"]["q_max"]) @@ -44,50 +46,52 @@ class DataLoader: "transition_midpoint", self.default_dict["parameters"]["transition_midpoint"] ) - self.sample = self.create_sample_from_dict(input_dict.get("materials")) + self.sample = create_sample_from_dict(input_dict.get("materials")) - def create_slab_chunk(self, material: dict) -> "Slab": - """Return a slab chunk built from given configuration. - Parameters - ---------- - material : dictionary - Single material dictionary to turn into slab chunk. +def create_slab_chunk(material: dict) -> "Slab": + """Return a slab chunk built from given configuration. - Returns - ------- - Slab - The slab chunk instance. - """ + Parameters + ---------- + material : dictionary + Single material dictionary to turn into slab chunk. - slab = Slab( - material=SLD( - name=material["name"], - rho=material["rho"], - irho=material["irho"], - ), - thickness=material["thickness"], - interface=material["roughness"], - ) + Returns + ------- + Slab + The slab chunk instance. + """ - return slab + slab = Slab( + material=SLD( + name=material["name"], + rho=material["rho"], + irho=material["irho"], + ), + thickness=material["thickness"], + interface=material["roughness"], + ) - def create_sample_from_dict(self, input_dict: list) -> Slab: - """Return a slab instance built from given configuration. + return slab - Parameters - ---------- - json_object : list - The sample configuration dictionary list. - Returns - ------- - Slab - The slab instance. - """ +def create_sample_from_dict(input_dict: list) -> Slab: + """Return a slab instance built from given configuration. + + Parameters + ---------- + json_object : list + The sample configuration dictionary list. + + Returns + ------- + Slab + The slab instance. + """ - sample = self.create_slab_chunk(input_dict[0]) - for i in range(1, len(input_dict)): - sample = sample | self.create_slab_chunk(input_dict[i]) + sample = create_slab_chunk(input_dict[0]) + for i in range(1, len(input_dict)): + sample = sample | create_slab_chunk(input_dict[i]) - return sample + return sample diff --git a/src/tgreft/utils/data/r_curve_generator.py b/src/tgreft/utils/data/r_curve_generator.py index 29a32a7..4f95b28 100644 --- a/src/tgreft/utils/data/r_curve_generator.py +++ b/src/tgreft/utils/data/r_curve_generator.py @@ -1,11 +1,8 @@ import json - -import numpy as np import math - +import numpy as np from refl1d.names import QProbe, Experiment - -from dataloader import DataLoader +from tgreft.utils.data.dataloader import DataLoader class RCurveGenerator: @@ -21,11 +18,11 @@ class RCurveGenerator: self.data = data self.probe, self.tr_probe = create_probes(data.q_min, data.q_max, data.step_size) self.expt = Experiment(probe=self.probe, sample=self.data.sample) - self.r_final, self.r_init = get_states( + self._r_final, self._r_init = get_states( self.expt, self.data.final_sei_sld, self.data.simulate_errors, self.data.err ) self.tr_expt = Experiment(probe=self.tr_probe, sample=self.data.sample) - self.tr_data, self.rho_data = generate_tNR( + self._tr_data, self._rho_data = generate_tNR( self.tr_expt, self.data.total_time, self.data.transition_time, @@ -36,7 +33,7 @@ class RCurveGenerator: ) @property - def get_r_final(self) -> list: + def r_final(self) -> list: """Returns a list from the RCurveGenerator object instance. Returns @@ -45,10 +42,10 @@ class RCurveGenerator: r_final list """ - return self.r_final + return self._r_final @property - def get_r_init(self) -> list: + def r_init(self) -> list: """Returns a list from the object instance. Returns @@ -57,10 +54,10 @@ class RCurveGenerator: r_init list """ - return self.r_init + return self._r_init @property - def get_tr_data(self) -> list: + def tr_data(self) -> list: """Returns a list from the object instance. Returns @@ -69,10 +66,10 @@ class RCurveGenerator: tr_data list """ - return self.tr_data + return self._tr_data @property - def get_rho_data(self) -> list: + def rho_data(self) -> list: """Returns a list from the object instance. Returns @@ -81,7 +78,7 @@ class RCurveGenerator: rho_data list """ - return self.rho_data + return self._rho_data def create_probes(q_min: float, q_max: float, step_size: float, resolution: float = 0.028) -> tuple: @@ -174,16 +171,17 @@ def generate_tNR( tr_expt : Experiment Subset of experiment total_time : int - + total time to run transition_time : int - + time to transition transition_midpoint : int - + midpoint of transition final_sei_sld : float - + final sei sld constant simulate_errors : bool - + boolean indicating whether you want to include error or not in simulation err : float + error constant Returns ------- @@ -213,8 +211,12 @@ def generate_tNR( if __name__ == "__main__": - f = open("data.json", "r") + import sys + + json_file_name = sys.argv[1] + + f = open(json_file_name, "r") data = json.load(f) dataloader = DataLoader(data) rc = RCurveGenerator(dataloader) - print(rc.get_r_init) + print(rc.r_init, rc.r_final, rc.tr_data, rc.rho_data) -- GitLab