def deduce_fix(self, ds_id, atypical_content, typical_content): dicts = [] atypical = atypical_content["data.dim_names"] typical = typical_content["data.dim_names"] # length of atypical should be longer if extra coord # Will this always be the case? if len(atypical) <= len(typical): return None extra_coords = get_extra_items_in_larger_sequence(typical, atypical) if extra_coords: if len(extra_coords) > 0: fix_cls = get_fix(self.associated_fix) for coord in extra_coords: index = atypical.index(coord) if atypical_content["data.shape"][index] != 1: extra_coords.remove(coord) operands = {"dims": extra_coords} fix = fix_cls(ds_id, **operands) d = fix.to_dict() dicts.append(d) return dicts # fix isn't suitable - no extra coords else: return None
def test_get_items_larger_sequence_6(): large = ["i", "j", "latitude", "longitude", "time", "type"] small = ["latitude", "longitude", "time", "x", "y"] res = get_extra_items_in_larger_sequence(small, large) assert res is None
def test_get_items_larger_sequence_5(): large = [("time", 3600), ("level", 1)] small = [("realisation", 1)] res = get_extra_items_in_larger_sequence(small, large) assert res is None
def test_get_items_larger_sequence_3(): large = [3540, 1] small = [3540] res = get_extra_items_in_larger_sequence(small, large) assert res == [large[1]]
def test_get_items_larger_sequence_2(): large = ["lev", "time"] small = ["time"] res = get_extra_items_in_larger_sequence(small, large) assert res == [large[0]]
def test_get_extra_items_in_larger_sequence(): large = [("time", 12), ("level", 1)] small = [("time", 12)] res = get_extra_items_in_larger_sequence(small, large) assert res == [large[1]]
def deduce_fix(self, ds_id, atypical_content, typical_content): dicts = [] atypical = atypical_content["coordinates.*.id"] typical = typical_content["coordinates.*.id"] # length of atypical should be shorter if missing coord # Will this always be the case? if len(atypical) >= len(typical): return None # # use mappings to get equivalent coords # for coord in atypical: # if coord not in typical: # equivalent_coord = coord_mappings[coord] # atypical = [equivalent_coord if i == coord else i for i in atypical] missing_coords = get_extra_items_in_larger_sequence(atypical, typical) if missing_coords: if len(missing_coords) > 0: fix_cls = get_fix(self.associated_fix) for coord in missing_coords: typical_coord_attrs = [] typical_ds_ids = self.sample.copy() typical_ds_ids.remove(ds_id[0]) for ds in typical_ds_ids: coord_attrs = nested_lookup(f"coordinates.{coord}", self._cache[ds], must_exist=True) coord_attrs = namedtuple( "coord_attrs", coord_attrs.keys())(**coord_attrs) typical_coord_attrs.append(coord_attrs) frequency = Counter(d for d in typical_coord_attrs) typical_coord = frequency.most_common(1)[0][0] typical_length = typical_coord.length # check missing coord is scalar if typical_length == 1: operands = {} operand_dict = dict(typical_coord._asdict()) for k in [ "dtype", "value", "id", "length", "coord_type" ]: v = operand_dict.pop(k) operands[k] = v operands["attrs"] = operand_dict fix = fix_cls(ds_id, **operands) d = fix.to_dict() # coordinate isn't scalar - fix isn't suitable else: d = None dicts.append(d) return dicts # fix isn't suitable - no missing coords else: return None