def _load_dictionary(self, dic): """ Load data from dictionary. Parameters ---------- dict : dict A dictionary containing at least the following fields: * _id_name: _id_name of the original parameter, used to create the dictionary. Has to match with the self._id_name * parameters: a list of dictionaries, one per parameter of the component (see :py:meth:`~hyperspy.component.Parameter.as_dictionary` documentation for more details) * _whitelist: a dictionary, which keys are used as keywords to match with the parameter attributes. For more information see :py:func:`~hyperspy.misc.export_dictionary.load_from_dictionary` * any field from _whitelist.keys() Returns ------- twin_dict : dict Dictionary of 'id' values from input dictionary as keys with all of the parameters of the component, to be later used for setting up correct twins. """ if dic['_id_name'] == self._id_name: if (self._id_name == "Polynomial" and Version(hyperspy.__version__) >= Version("2.0")): # in HyperSpy 2.0 the polynomial definition changed from hyperspy._components.polynomial import convert_to_polynomial dic = convert_to_polynomial(dic) load_from_dictionary(self, dic) id_dict = {} for p in dic['parameters']: idname = p['_id_name'] if hasattr(self, idname): par = getattr(self, idname) t_id = par._load_dictionary(p) id_dict[t_id] = par else: raise ValueError( "_id_name of parameters in component and dictionary do not match" ) return id_dict else: raise ValueError( "_id_name of component and dictionary do not match, \ncomponent._id_name = %s\ \ndictionary['_id_name'] = %s" % (self._id_name, dic['_id_name']))
def test_conversion_dictionary_to_polynomial2(self): from hyperspy._components.polynomial import convert_to_polynomial s = hs.signals.Signal1D(np.zeros(1024)) s.axes_manager[0].offset = -5 s.axes_manager[0].scale = 0.01 poly = hs.model.components1D.Polynomial(order=2, legacy=True) poly.coefficients.value = [1, 2, 3] poly.coefficients.value = [1, 2, 3] poly.coefficients._bounds = ((None, None), (10, 0.0), (None, None)) poly_dict = poly.as_dictionary(True) poly2_dict = convert_to_polynomial(poly_dict) poly2 = hs.model.components1D.Polynomial(order=2, legacy=False) _ = poly2._load_dictionary(poly2_dict) assert poly2.a2.value == 1 assert poly2.a2._bounds == (None, None) assert poly2.a1.value == 2 assert poly2.a1._bounds == (10, 0.0) assert poly2.a0.value == 3