def make_subset_data(data, random_subset=None, pixels=None, return_selection=False): if random_subset is None and pixels is None: return data if random_subset is not None and pixels is not None: raise ValueError("You can only specify one of pixels or random_subset") tot_pix = len(data.x)*len(data.y) if pixels is not None: n_sel = pixels else: n_sel = int(np.ceil(tot_pix*random_subset)) selection = np.random.choice(tot_pix, n_sel, replace=False) subset = flat(data).isel(flat=selection) subset = copy_metadata(data, subset, do_coords=False) shape = (len(data.x), len(data.y)) spacing = (get_spacing(data)) start = (np.asscalar(data.x[0]), np.asscalar(data.y[0])) coords = {key:val.values for key, val in dict_without(dict(data.coords), ['x','y','z']).items()} subset.attrs['original_dims'] = yaml.dump((shape, spacing, start, coords)) if return_selection: return subset, selection else: return subset
def _find_new_ties(self): reference_parameters = self.raw_parameters for fullkey, par in reference_parameters.items(): if fullkey not in self._all_ties: # not already in the list of ties, so check if it should be for ref_key, ref_par in dict_without( reference_parameters, fullkey).items(): # can't simply check par in parameters because then two # priors defined separately, but identically will match # whereas this way they are counted as separate objects. if par is ref_par and not isinstance(par, Number): self.add_tie(ref_key, fullkey) break
def forward(self, pars): if hasattr(self.data, 'original_dims'): # dealing with subset data original_dims = self.data.original_dims # can't currently handle non-0 values of z, as in detector_grid x = original_dims['x'] y = original_dims['y'] shape = (len(x), len(y)) spacing = (np.diff(x)[0], np.diff(y)[0]) extra_dims = dict_without(original_dims, ['x', 'y', 'z']) schema = detector_grid(shape, spacing, extra_dims=extra_dims) schema = copy_metadata(self.data, schema, do_coords=False) schema['x'] = x schema['y'] = y else: schema = self.data return self.model.forward(pars, schema)
def unpack_attrs(a): if len(a) == 0: return a new_attrs = {} attr_ref = yaml.load(a[attr_coords], Loader=FullLoader) attrs_to_ignore = ['spacing', 'name', '_dummy_channel', '_image_scaling'] for attr in dict_without(attr_ref, attrs_to_ignore): if attr_ref[attr]: new_attrs[attr] = xr.DataArray(a[attr], coords=attr_ref[attr], dims=list(attr_ref[attr].keys())) elif attr in a: try: new_attrs[attr] = yaml.safe_load(a[attr]) except AttributeError: from holopy.inference.result import warn_text warnings.warn(warn_text) new_attrs[attr] = a[attr] else: new_attrs[attr] = None return new_attrs
def test_dict_without(self): input_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4} output_dict = dict_without(input_dict, ['a', 'd', 'e']) self.assertEqual(input_dict, {'a': 1, 'b': 2, 'c': 3, 'd': 4}) self.assertEqual(output_dict, {'b': 2, 'c': 3})