.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/advanced_features/algo_serialize.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_advanced_features_algo_serialize.py: .. _algo_serialize: Export Algorithms to JSON ========================= This example demonstrates how to save algorithms including their configuration to json and load them again to ensure reproducibility. The example will use :class:`~gaitmap.trajectory_reconstruction.StrideLevelParameter` as example, but the same will work with all other algorithms (and Pipelines). .. warning:: This json export only stores the Parameters of an algorithm and **not** any potential results! .. warning:: While calling an Algorithms with the same parameters should produce the same results, you also need to ensure that the same version of gaitmap (and all other dependencies) is used to ensure full reproducibility. This means you should save the exact library version together with the json version of the used algorithms. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: default from pprint import pprint .. GENERATED FROM PYTHON SOURCE LINES 24-28 To JSON ------- In the following we will create an algorithm instance that itself has other nested algorithm objects. In the from the json output .. GENERATED FROM PYTHON SOURCE LINES 28-41 .. code-block:: default from scipy.spatial.transform import Rotation from gaitmap.base import BaseAlgorithm from gaitmap.trajectory_reconstruction import ForwardBackwardIntegration, MadgwickAHRS, StrideLevelTrajectory # Setting an initial orientation here, is pointless as it will be overwritten by StrideLevelTrajectory # It is used here to demonstrate the ability to serialize Rotation objects. custom_ori_method = MadgwickAHRS(beta=0.5, initial_orientation=Rotation.from_quat([0, 3, 3, 0])) custom_pos_method = ForwardBackwardIntegration(turning_point=0.8) slt = StrideLevelTrajectory(ori_method=custom_ori_method, pos_method=custom_pos_method, align_window_width=10) pprint(slt.get_params()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'align_window_width': 10, 'ori_method': MadgwickAHRS(beta=0.5, initial_orientation=, memory=None), 'ori_method__beta': 0.5, 'ori_method__initial_orientation': , 'ori_method__memory': None, 'pos_method': ForwardBackwardIntegration(gravity=array([0. , 0. , 9.81]), level_assumption=True, steepness=0.08, turning_point=0.8), 'pos_method__gravity': array([0. , 0. , 9.81]), 'pos_method__level_assumption': True, 'pos_method__steepness': 0.08, 'pos_method__turning_point': 0.8, 'trajectory_method': None} .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: default json_str = slt.to_json() print(json_str) .. rst-class:: sphx-glr-script-out .. code-block:: none { "_gaitmap_obj": "StrideLevelTrajectory", "params": { "align_window_width": 10, "ori_method": { "_gaitmap_obj": "MadgwickAHRS", "params": { "beta": 0.5, "initial_orientation": { "_obj_type": "Rotation", "quat": [ 0.0, 0.7071067811865476, 0.7071067811865476, 0.0 ] }, "memory": null } }, "pos_method": { "_gaitmap_obj": "ForwardBackwardIntegration", "params": { "gravity": { "_obj_type": "Array", "array": [ 0.0, 0.0, 9.81 ] }, "level_assumption": true, "steepness": 0.08, "turning_point": 0.8 } }, "trajectory_method": null } } .. GENERATED FROM PYTHON SOURCE LINES 47-55 This output could now be stored in a file to document the exact parameters that were used for a specific analysis. From JSON --------- All algorithms can be loaded from json as well using the `from_json` method. This method can be called on any algorithm class and the Algorithm class specified in the json object is returned. To avoid confusion it is advisable to use either the exact algorithm class that was stored or `BaseAlgorithm`. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: default loaded_slt = BaseAlgorithm.from_json(json_str) pprint(loaded_slt.get_params()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'align_window_width': 10, 'ori_method': MadgwickAHRS(beta=0.5, initial_orientation=, memory=None), 'ori_method__beta': 0.5, 'ori_method__initial_orientation': , 'ori_method__memory': None, 'pos_method': ForwardBackwardIntegration(gravity=array([0. , 0. , 9.81]), level_assumption=True, steepness=0.08, turning_point=0.8), 'pos_method__gravity': array([0. , 0. , 9.81]), 'pos_method__level_assumption': True, 'pos_method__steepness': 0.08, 'pos_method__turning_point': 0.8, 'trajectory_method': None} .. GENERATED FROM PYTHON SOURCE LINES 59-61 To show that you can call `from_json` from any Algorithm class we will perform the same import using the `StrideLevelTrajectory`. .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: default loaded_slt = StrideLevelTrajectory.from_json(json_str) pprint(loaded_slt.get_params()) from joblib import Memory .. rst-class:: sphx-glr-script-out .. code-block:: none {'align_window_width': 10, 'ori_method': MadgwickAHRS(beta=0.5, initial_orientation=, memory=None), 'ori_method__beta': 0.5, 'ori_method__initial_orientation': , 'ori_method__memory': None, 'pos_method': ForwardBackwardIntegration(gravity=array([0. , 0. , 9.81]), level_assumption=True, steepness=0.08, turning_point=0.8), 'pos_method__gravity': array([0. , 0. , 9.81]), 'pos_method__level_assumption': True, 'pos_method__steepness': 0.08, 'pos_method__turning_point': 0.8, 'trajectory_method': None} .. GENERATED FROM PYTHON SOURCE LINES 67-71 Caching Support --------------- Note that the json export does not cover `joblib.memory` objects that some algorithms use to cache results. When you attempt to do this, you will get a warning with further information. .. GENERATED FROM PYTHON SOURCE LINES 71-79 .. code-block:: default from gaitmap.stride_segmentation import BarthDtw # Create a memory object. Usually you would pass the path to a cache dir. mem = Memory() instance = BarthDtw(memory=Memory()) pprint(instance.get_params()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'conflict_resolution': True, 'find_matches_method': 'find_peaks', 'max_cost': 4.0, 'max_match_length_s': 3.0, 'max_signal_stretch_ms': None, 'max_template_stretch_ms': None, 'memory': Memory(location=None), 'min_match_length_s': 0.6, 'resample_template': True, 'snap_to_min_axis': 'gyr_ml', 'snap_to_min_win_ms': 300, 'template': BarthOriginalTemplate(scaling=FixedScaler(offset=0, scale=500.0), use_cols=None), 'template__scaling': FixedScaler(offset=0, scale=500.0), 'template__scaling__offset': 0, 'template__scaling__scale': 500.0, 'template__use_cols': None} .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default json_str_cached = instance.to_json() print(json_str_cached) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/gaitmap/checkouts/v2.5.2/gaitmap/base.py:77: UserWarning: Exporting `joblib.Memory` objects to json is not supported. The value will be replaced by `None` and caching needs to be reactivated after loading the object again. This can be using `instance.set_params(memory=Memory(...))` warnings.warn( { "_gaitmap_obj": "BarthDtw", "params": { "conflict_resolution": true, "find_matches_method": "find_peaks", "max_cost": 4.0, "max_match_length_s": 3.0, "max_signal_stretch_ms": null, "max_template_stretch_ms": null, "memory": null, "min_match_length_s": 0.6, "resample_template": true, "snap_to_min_axis": "gyr_ml", "snap_to_min_win_ms": 300, "template": { "_gaitmap_obj": "BarthOriginalTemplate", "params": { "scaling": { "_gaitmap_obj": "FixedScaler", "params": { "offset": 0, "scale": 500.0 } }, "use_cols": null } } } } .. GENERATED FROM PYTHON SOURCE LINES 84-88 If we now load the object again, the memory argument is empty. We need to set it again. If we use the same memory object or recreate a new memory object that points to the same cache-location, the same cache will be used as before. .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: default loaded_instance = BarthDtw.from_json(json_str_cached) print(loaded_instance.memory) .. rst-class:: sphx-glr-script-out .. code-block:: none None .. GENERATED FROM PYTHON SOURCE LINES 92-93 We can simply reactivate the memory. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: default loaded_instance = loaded_instance.set_params(memory=mem) pprint(loaded_instance.get_params()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'conflict_resolution': True, 'find_matches_method': 'find_peaks', 'max_cost': 4.0, 'max_match_length_s': 3.0, 'max_signal_stretch_ms': None, 'max_template_stretch_ms': None, 'memory': Memory(location=None), 'min_match_length_s': 0.6, 'resample_template': True, 'snap_to_min_axis': 'gyr_ml', 'snap_to_min_win_ms': 300, 'template': BarthOriginalTemplate(scaling=FixedScaler(offset=0, scale=500.0), use_cols=None), 'template__scaling': FixedScaler(offset=0, scale=500.0), 'template__scaling__offset': 0, 'template__scaling__scale': 500.0, 'template__use_cols': None} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.766 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_advanced_features_algo_serialize.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: algo_serialize.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: algo_serialize.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_