def test_system_additional_dimension(self): """Test with a randomly named dimension with no metadata definition.""" arr = np.random.rand(2, 3, 4, 5, 6) src_names = ['time', 'flood', 'x', 'y', 'heat_wave'] dst_and_field_names = ['realization', 'time', 'level', 'y', 'x'] extra = [dn for dn in src_names if dn not in dst_and_field_names] extra = {e: {'index': src_names.index(e)} for e in extra} for v in list(extra.values()): v['size'] = arr.shape[v['index']] src_names_extra_removed = [dn for dn in src_names if dn not in extra] itr = itertools.product(*[ zip_longest( [v['index']], list(range(v['size'])), fillvalue=v['index']) for v in list(extra.values()) ]) for indices in itr: slc = [slice(None)] * arr.ndim for ii in indices: slc[ii[0]] = ii[1] extras_removed = arr.__getitem__(slc) carr = broadcast_array_by_dimension_names(extras_removed, src_names_extra_removed, dst_and_field_names) self.assertEqual(carr.shape, (1, 2, 1, 5, 4)) self.assertNumpyMayShareMemory(arr, carr)
def test_system_additional_dimension(self): """Test with a randomly named dimension with no metadata definition.""" arr = np.random.rand(2, 3, 4, 5, 6) src_names = ['time', 'flood', 'x', 'y', 'heat_wave'] dst_and_field_names = ['realization', 'time', 'level', 'y', 'x'] extra = [dn for dn in src_names if dn not in dst_and_field_names] extra = {e: {'index': src_names.index(e)} for e in extra} for v in list(extra.values()): v['size'] = arr.shape[v['index']] src_names_extra_removed = [dn for dn in src_names if dn not in extra] itr = itertools.product( *[zip_longest([v['index']], list(range(v['size'])), fillvalue=v['index']) for v in list(extra.values())]) for indices in itr: slc = [slice(None)] * arr.ndim for ii in indices: slc[ii[0]] = ii[1] extras_removed = arr.__getitem__(slc) carr = broadcast_array_by_dimension_names(extras_removed, src_names_extra_removed, dst_and_field_names) self.assertEqual(carr.shape, (1, 2, 1, 5, 4)) self.assertNumpyMayShareMemory(arr, carr)
def _iter_conformed_arrays_(self, crosswalk, variable_shape, arr, arr_fill, arr_fill_sample_size): # Allow sample size array to be set to None. if arr_fill_sample_size is None: calc_sample_size = False else: calc_sample_size = self.calc_sample_size itr_extra_indices, src_names_extra_removed = self._get_extra_indices_itr_and_src_names_( crosswalk, variable_shape) # Loop for the extra dimensions. for indices in itr_extra_indices: # Slice for the extra dimensions. slc = [slice(None)] * arr.ndim for ii in indices: slc[ii[0]] = ii[1] extras_removed = arr.__getitem__(slc) extras_removed_fill = arr_fill.__getitem__(slc) if calc_sample_size: extras_removed_fill_sample_size = arr_fill_sample_size.__getitem__( slc) # Swap axes for the calculation values, the fill array for the calculation result, and (potentially) the # sample size. carr, carr_fill = [ broadcast_array_by_dimension_names(t, src_names_extra_removed, STANDARD_DIMENSIONS) for t in [extras_removed, extras_removed_fill] ] if calc_sample_size: carr_fill_sample_size = broadcast_array_by_dimension_names( extras_removed_fill_sample_size, src_names_extra_removed, STANDARD_DIMENSIONS) if not calc_sample_size: yld = (carr, carr_fill) else: yld = (carr, carr_fill, carr_fill_sample_size) yield yld
def test(self): arr = np.random.rand(2, 3, 4) arr = np.ma.array(arr, mask=False) src_names = ['time', 'x', 'y'] dst_names = ['realization', 'time', 'level', 'y', 'x'] carr = broadcast_array_by_dimension_names(arr, src_names, dst_names) self.assertEqual(carr.shape, (1, 2, 1, 4, 3)) self.assertTrue(np.may_share_memory(arr, carr)) self.assertFalse(np.any(arr.mask)) carr.mask[:] = True self.assertTrue(np.all(carr.mask)) self.assertTrue(np.all(arr.mask)) self.assertTrue(np.may_share_memory(arr.mask, carr.mask)) carr.mask[:] = False carr2 = broadcast_array_by_dimension_names(carr, dst_names, src_names) self.assertNumpyAll(arr, carr2) self.assertTrue(np.may_share_memory(arr, carr2)) # self.assertTrue(np.may_share_memory(arr.mask, carr2.mask)) carr2[:] = 10 self.assertEqual(arr.mean(), 10)
def _iter_conformed_arrays_(self, crosswalk, variable_shape, arr, arr_fill, arr_fill_sample_size): # Allow sample size array to be set to None. if arr_fill_sample_size is None: calc_sample_size = False else: calc_sample_size = self.calc_sample_size itr_extra_indices, src_names_extra_removed = self._get_extra_indices_itr_and_src_names_(crosswalk, variable_shape) # Loop for the extra dimensions. for indices in itr_extra_indices: # Slice for the extra dimensions. slc = [slice(None)] * arr.ndim for ii in indices: slc[ii[0]] = ii[1] extras_removed = arr.__getitem__(slc) extras_removed_fill = arr_fill.__getitem__(slc) if calc_sample_size: extras_removed_fill_sample_size = arr_fill_sample_size.__getitem__(slc) # Swap axes for the calculation values, the fill array for the calculation result, and (potentially) the # sample size. carr, carr_fill = [broadcast_array_by_dimension_names(t, src_names_extra_removed, STANDARD_DIMENSIONS) for t in [extras_removed, extras_removed_fill]] if calc_sample_size: carr_fill_sample_size = broadcast_array_by_dimension_names(extras_removed_fill_sample_size, src_names_extra_removed, STANDARD_DIMENSIONS) if not calc_sample_size: yld = (carr, carr_fill) else: yld = (carr, carr_fill, carr_fill_sample_size) yield yld