Repository: RemDelaporteMathurin/h-transport-materials

Issue:
PropertiesGroup need to break down export_to_json
The method export_to_json could be broken down.

This could simplify the export to json in HTM-dashboard.

Comments:

Patch:
diff --git a/h_transport_materials/properties_group.py b/h_transport_materials/properties_group.py
index 1099a1b..6efdd55 100644
--- a/h_transport_materials/properties_group.py
+++ b/h_transport_materials/properties_group.py
@@ -143,33 +143,9 @@ class PropertiesGroup(list):
         self.bibdata.to_file(filename)
 
     def export_to_json(self, filename: str):
-        keys = [
-            "material",
-            "pre_exp",
-            "act_energy",
-            "isotope",
-            "author",
-            "source",
-            "range",
-            "doi",
-            "units",
-        ]
-
         data = []
         for prop in self:
-
-            prop_dict = {key: getattr(prop, key) for key in keys if hasattr(prop, key)}
-            if "units" in prop_dict:
-                prop_dict["units"] = f"{prop_dict['units']:~}"
-            prop_dict["pre_exp"] = prop_dict["pre_exp"].magnitude
-            prop_dict["act_energy"] = prop_dict["act_energy"].magnitude
-            prop_dict["material"] = prop_dict["material"].name
-            if prop_dict["range"]:
-                prop_dict["range"] = (
-                    prop_dict["range"][0].magnitude,
-                    prop_dict["range"][1].magnitude,
-                )
-            data.append(prop_dict)
+            data.append(prop.to_json())
 
         with open(filename, "w") as outfile:
             json.dump(data, outfile, indent=4)
diff --git a/h_transport_materials/property.py b/h_transport_materials/property.py
index b9994e2..6bf41e2 100644
--- a/h_transport_materials/property.py
+++ b/h_transport_materials/property.py
@@ -36,7 +36,6 @@ class Property:
         author: str = "",
         note: str = None,
     ) -> None:
-
         self.material = material
         self.source = source
         self.range = range
@@ -137,6 +136,25 @@ class Property:
 
         self.bibdata.to_file(filename)
 
+    def to_json(self):
+        """Returns a dictionary with the relevant attributes of the property
+
+        Returns:
+            dict: a dict reprensation of the property
+        """
+        as_json = {}
+        if self.range is not None:
+            as_json["range"] = (self.range[0].magnitude, self.range[1].magnitude)
+        as_json["material"] = self.material.name
+        as_json["source"] = self.source
+        as_json["year"] = self.year
+        as_json["isotope"] = self.isotope
+        as_json["author"] = self.author
+        as_json["note"] = self.note
+        as_json["doi"] = self.doi
+
+        return as_json
+
     def value(self, T):
         pass
 
@@ -164,7 +182,6 @@ class ArrheniusProperty(Property):
         data_y: list = None,
         **kwargs,
     ) -> None:
-
         self.pre_exp = pre_exp
         self.act_energy = act_energy
         self.data_T = data_T
@@ -340,6 +357,29 @@ class ArrheniusProperty(Property):
             T *= ureg.K
         return self.pre_exp * exp(-self.act_energy / k_B / T)
 
+    def to_json(self):
+        """Returns a dictionary with the relevant attributes of the property
+
+        Returns:
+            dict: a dict reprensation of the property
+        """
+        as_json = super().to_json()
+        as_json["pre_exp"] = {}
+        as_json["act_energy"] = {}
+        as_json["pre_exp"]["value"] = self.pre_exp.magnitude
+        as_json["pre_exp"]["units"] = f"{self.pre_exp.units}"
+        as_json["act_energy"]["value"] = self.act_energy.magnitude
+        as_json["act_energy"]["units"] = f"{self.act_energy.units}"
+        if self.data_T is not None:
+            as_json["data_T"] = {}
+            as_json["data_T"]["value"] = self.data_T.magnitude.tolist()
+            as_json["data_T"]["units"] = f"{self.data_T.units}"
+        if self.data_y is not None:
+            as_json["data_y"] = {}
+            as_json["data_y"]["value"] = self.data_y.magnitude.tolist()
+            as_json["data_y"]["units"] = f"{self.data_y.units}"
+        return as_json
+
 
 class Solubility(ArrheniusProperty):
     """Solubility class
@@ -460,7 +500,6 @@ class Permeability(ArrheniusProperty):
         law: str = None,
         **kwargs,
     ) -> None:
-
         self.law = law
         super().__init__(
             pre_exp=pre_exp,
diff --git a/h_transport_materials/property_database/cucrzr/cucrzr.py b/h_transport_materials/property_database/cucrzr/cucrzr.py
index 5d58058..40d116b 100644
--- a/h_transport_materials/property_database/cucrzr/cucrzr.py
+++ b/h_transport_materials/property_database/cucrzr/cucrzr.py
@@ -35,7 +35,6 @@ note_serra_diffusivity_h = (
 serra_diffusivity_h = Diffusivity(
     data_T=1000 / data_diffusivity_serra_h[:, 0] * htm.ureg.K,
     data_y=data_diffusivity_serra_h[:, 1] * htm.ureg.m**2 * htm.ureg.s**-1,
-    range=(553 * htm.ureg.K, 773 * htm.ureg.K),
     source="serra_hydrogen_1998",
     isotope="H",
     note=note_serra_diffusivity_h,
@@ -49,7 +48,6 @@ note_serra_diffusivity_d = (
 serra_diffusivity_d = Diffusivity(
     data_T=1000 / data_diffusivity_serra_d[:, 0] * htm.ureg.K,
     data_y=data_diffusivity_serra_d[:, 1] * htm.ureg.m**2 * htm.ureg.s**-1,
-    range=(553 * htm.ureg.K, 773 * htm.ureg.K),
     source="serra_hydrogen_1998",
     isotope="D",
     note=note_serra_diffusivity_d,
@@ -69,7 +67,6 @@ serra_solubility_h = Solubility(
     * htm.ureg.mol
     * htm.ureg.m**-3
     * htm.ureg.Pa**-0.5,
-    range=(553 * htm.ureg.K, 773 * htm.ureg.K),
     source="serra_hydrogen_1998",
     isotope="H",
 )
@@ -80,7 +77,6 @@ serra_solubility_d = Solubility(
     * htm.ureg.mol
     * htm.ureg.m**-3
     * htm.ureg.Pa**-0.5,
-    range=(553 * htm.ureg.K, 773 * htm.ureg.K),
     source="serra_hydrogen_1998",
     isotope="D",
 )
diff --git a/h_transport_materials/property_database/lipb/lipb.py b/h_transport_materials/property_database/lipb/lipb.py
index 2691dde..5622be9 100644
--- a/h_transport_materials/property_database/lipb/lipb.py
+++ b/h_transport_materials/property_database/lipb/lipb.py
@@ -123,7 +123,6 @@ reiter_difusivity_data_H = reiter_diffusivity_data[2:, 2:]
 reiter_diffusivity_h = Diffusivity(
     data_T=1000 / reiter_difusivity_data_H[:, 0] * u.K,
     data_y=reiter_difusivity_data_H[:, 1] * u.m**2 * u.s**-1,
-    range=(508 * u.K, 700 * u.K),
     source="reiter_solubility_1991",
     isotope="H",
 )
@@ -134,7 +133,6 @@ reiter_difusivity_data_D = reiter_diffusivity_data[2:, :2]
 reiter_diffusivity_d = Diffusivity(
     data_T=1000 / reiter_difusivity_data_D[:, 0] * u.K,
     data_y=reiter_difusivity_data_D[:, 1] * u.m**2 * u.s**-1,
-    range=(508 * u.K, 700 * u.K),
     source="reiter_solubility_1991",
     isotope="D",
 )
@@ -154,7 +152,6 @@ reiter_solubility_data_H_y *= atom_density_lipb(nb_li=17, nb_pb=1)  # m-3 Pa-1/2
 reiter_solubility_h = Solubility(
     data_T=1000 / reiter_solubility_data_H[:, 0] * u.K,
     data_y=reiter_solubility_data_H_y,
-    range=(508 * u.K, 700 * u.K),
     source="reiter_solubility_1991",
     isotope="H",
 )
@@ -171,7 +168,6 @@ reiter_solubility_data_D_y *= atom_density_lipb(nb_li=17, nb_pb=1)  # m-3 Pa-1/2
 reiter_solubility_d = Solubility(
     data_T=reiter_solubility_data_D_T[np.isfinite(reiter_solubility_data_D_T)] * u.K,
     data_y=reiter_solubility_data_D_y[np.isfinite(reiter_solubility_data_D_y)],
-    range=(508 * u.K, 700 * u.K),
     source="reiter_solubility_1991",
     isotope="D",
 )
@@ -193,7 +189,6 @@ data_aiello = np.genfromtxt(
 aiello_solubility = Solubility(
     data_T=1000 / data_aiello[:, 0] * u.K,
     data_y=data_aiello[:, 1] * u.mol * u.m**-3 * u.Pa**-0.5,
-    range=(600 * u.K, 900 * u.K),
     source="aiello_determination_2006",
     isotope="H",
 )


Test patch:
diff --git a/tests/test_properties_group.py b/tests/test_properties_group.py
index cd40a60..4839a9f 100644
--- a/tests/test_properties_group.py
+++ b/tests/test_properties_group.py
@@ -165,7 +165,11 @@ def test_export_to_json():
                     if key == "units":
                         assert f"{getattr(prop_ref, key):~}" == val
                     elif key in ["pre_exp", "act_energy"]:
-                        assert getattr(prop_ref, key).magnitude == val
+                        assert getattr(prop_ref, key).magnitude == val["value"]
+                    elif key in ["data_T", "data_y"]:
+                        assert np.array_equal(
+                            getattr(prop_ref, key).magnitude, val["value"]
+                        )
                     else:
                         assert getattr(prop_ref, key) == val