Skip to content
Snippets Groups Projects
Commit 499274de authored by Mattia Mancini's avatar Mattia Mancini
Browse files

SSB-47: Move collection comparison outside class

parent e07d33e5
No related branches found
No related tags found
1 merge request!44Merge back holography to master
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment