From 499274dea7e6c008f4fe8b8dcd9d6b1f632d0a08 Mon Sep 17 00:00:00 2001 From: mancini <mancini@astron.nl> Date: Fri, 15 Nov 2019 10:00:22 +0100 Subject: [PATCH] SSB-47: Move collection comparison outside class --- .../lib/datacontainers/holography_dataset.py | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py b/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py index 7c6fe94e69d..76d76b368ec 100644 --- a/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py +++ b/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py @@ -11,6 +11,32 @@ from numpy import isnan logger = logging.getLogger(__file__) +def compare_nested_collections(dict1, dict2): + """ + This function compares the contents of nested collections that can be accessed through the __item__ method. + It recursively iterates over all keys and embedded dicts. + @return: False if a key in dict1 is not a key in dict2 or if values for + a key differ in the dicts, True if the contents of both dicts match. + @param dict1: A dict + @param dict2: Another dict + """ + result = True + for key in dict1.keys(): + if key in dict2.keys(): + if isinstance(dict1[key], dict) and isinstance(dict2[key], dict): + result = result and compare_nested_collections(dict1[key], dict2[key]) + else: + if isinstance(dict1[key], numpy.ndarray) and isinstance(dict2[key], + numpy.ndarray): + # Compares element by element the two arrays + return numpy.array_equal(dict1[key], dict2[key]) + else: + return dict1[key] == dict2[key] + else: + + return False + return result + def bytestring_list_to_string(list): return [item.decode('utf-8') for item in list] @@ -105,33 +131,6 @@ class HolographyDataset(): # numpy.array() self.derived_data = None - @staticmethod - def compare_dicts(dict1, dict2): - ''' - This function compares the contents of two dicts. It recursively - iterates over all keys and embedded dicts. - @return: False if a key in dict1 is not a key in dict2 or if values for - a key differ in the dicts, True if the contents of both dicts match. - @param dict1: A dict - @param dict2: Another dict - ''' - result = True - for key in dict1.keys(): - if key in dict2.keys(): - if isinstance(dict1[key], dict) and isinstance(dict2[key], dict): - result = result and HolographyDataset.compare_dicts(dict1[key], dict2[key]) - else: - if isinstance(dict1[key], numpy.ndarray) and isinstance(dict2[key], - numpy.ndarray): - # Compares element by element the two arrays - return numpy.array_equal(dict1[key], dict2[key]) - else: - return dict1[key] == dict2[key] - else: - - return False - return result - def __eq__(self, hds=None): ''' This comparison operator compares the values of the relevant members of @@ -155,8 +154,7 @@ class HolographyDataset(): this_equality = numpy.array_equal(attribute_value, other_value) elif isinstance(attribute_value, dict) is True and isinstance(other_value, dict) is True: - this_equality = HolographyDataset.compare_dicts(attribute_value, - other_value) + this_equality = compare_nested_collections(attribute_value, other_value) elif attribute_value != other_value: this_equality = False logger.error("values for field \"%s\" differs", attribute_name) @@ -175,7 +173,7 @@ class HolographyDataset(): return False return equality - def find_central_beamlets(self, source, ra_dec, frequencies, beamlets): + def find_central_beamlets(_, source, ra_dec, frequencies, beamlets): ''' This function finds the central beamlet of a target station for every frequency. -- GitLab