def run_and_visualize(**kawrgs): """Visualize a simulation. Parameters ---------- kawrgs : run_multiple_sources kwargs. """ wave, speed = run_multiple_sources(**kawrgs) clim = np.percentile(wave, 99) wave_cmap = Colormap( [[0.55, 0, .32, 1], [0, 0, 0, 0], [0.15, 0.4, 0.1, 1]], name='PBlG') wave_dict = { 'colormap': wave_cmap, 'contrast_limits': [-clim, clim], 'name': 'wave', 'metadata': kawrgs, 'interpolation': 'bilinear' } speed_cmap = Colormap([[0, 0, 0, 0], [0.7, 0.5, 0, 1]], name='Orange') speed_dict = { 'colormap': speed_cmap, 'opacity': 0.5, 'name': 'speed', 'metadata': kawrgs, 'interpolation': 'bilinear' } viewer = napari.Viewer() viewer.add_image(np.atleast_2d(np.squeeze(wave)), **wave_dict) viewer.add_image(np.atleast_2d(speed[0, 0]), **speed_dict) napari.run()
def test_colormap_dump(tmp_path): cmap_list = [Colormap([(0, 0, 0), (1, 1, 1)]), Colormap([(0, 0, 0), (1, 1, 1)], controls=[0, 1])] with open(tmp_path / "test.json", "w") as f_p: json.dump(cmap_list, f_p, cls=ProfileEncoder) with open(tmp_path / "test.json") as f_p: cmap_list2 = json.load(f_p, object_hook=profile_hook) assert np.array_equal(cmap_list[0].colors, cmap_list2[0].colors) assert np.array_equal(cmap_list[0].controls, cmap_list2[0].controls) assert np.array_equal(cmap_list[1].colors, cmap_list2[1].colors) assert np.array_equal(cmap_list[1].controls, cmap_list2[1].controls) cmap_list = [ Colormap([(0, 0, 0), (1, 1, 1)]), Colormap([(0, 0, 0), (0, 0, 0), (1, 1, 1), (1, 1, 1)], controls=[0, 0.1, 0.8, 1]), ] with open(tmp_path / "test2.json", "w") as f_p: json.dump(cmap_list, f_p, cls=ProfileEncoder) with open(tmp_path / "test2.json") as f_p: cmap_list2 = json.load(f_p, object_hook=profile_hook) assert np.array_equal(cmap_list[0].colors, cmap_list2[0].colors) assert np.array_equal(cmap_list[0].controls, cmap_list2[0].controls) assert np.array_equal(cmap_list[1].colors, cmap_list2[1].colors) assert np.array_equal(cmap_list[1].controls, cmap_list2[1].controls) assert cmap_list2[1].controls[0] == 0 assert cmap_list2[1].controls[-1] == 1 assert np.array_equal(cmap_list[1].colors[0], cmap_list2[1].colors[0]) assert np.array_equal(cmap_list[1].colors[-1], cmap_list2[1].colors[-1])
def test_colormaps(): """Test setting test_colormaps.""" np.random.seed(0) data = np.random.random((10, 15)) layer = Image(data) assert layer.colormap.name == 'gray' assert isinstance(layer.colormap, Colormap) layer.colormap = 'magma' assert layer.colormap.name == 'magma' assert isinstance(layer.colormap, Colormap) cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.3, 0.7, 0.2, 1.0]]) layer.colormap = 'custom', cmap assert layer.colormap.name == 'custom' assert layer.colormap == cmap cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.7, 0.2, 0.6, 1.0]]) layer.colormap = {'new': cmap} assert layer.colormap.name == 'new' assert layer.colormap == cmap layer = Image(data, colormap='magma') assert layer.colormap.name == 'magma' assert isinstance(layer.colormap, Colormap) cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.3, 0.7, 0.2, 1.0]]) layer = Image(data, colormap=('custom', cmap)) assert layer.colormap.name == 'custom' assert layer.colormap == cmap cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.7, 0.2, 0.6, 1.0]]) layer = Image(data, colormap={'new': cmap}) assert layer.colormap.name == 'new' assert layer.colormap == cmap
def __init__(self): super().__init__() self.color_list: List[Color] = [] self.position_list: List[float] = [] self.move_ind = None self.image = convert_colormap_to_image(Colormap([(0, 0, 0)])) self.setMinimumHeight(60)
def colormap(self) -> Colormap: """colormap getter""" if len(self.color_list) == 0: return Colormap("black") color_list = self.color_list[:] position_list = self.position_list[:] if position_list[0] != 0: position_list.insert(0, 0) color_list.insert(0, color_list[0]) if position_list[-1] != 1: position_list.append(1) color_list.append(color_list[-1]) return Colormap(colors=color_list, controls=position_list)
def update_colormap(self, name, channel_id): fixed_range = self._settings.get_from_profile( f"{name}.lock_{channel_id}", False) gamma = self._settings.get_from_profile( f"{name}.gamma_value_{channel_id}", 1) if fixed_range: self.range = self._settings.get_from_profile( f"{name}.range_{channel_id}") else: self.range = self._settings.border_val[channel_id] cmap = self._settings.colormap_dict[ self._settings.get_channel_colormap_name(name, channel_id)][0] round_factor = self.round_base(self.range[1]) self.round_range = ( int(round(self.range[0] / round_factor) * round_factor), int(round(self.range[1] / round_factor) * round_factor), ) if self.round_range[0] < self.range[0]: self.round_range = self.round_range[ 0] + round_factor, self.round_range[1] if self.round_range[1] > self.range[1]: self.round_range = self.round_range[ 0], self.round_range[1] - round_factor # print(self.range, self.round_range) data = np.linspace(0, 1, 512) interpolated = cmap.map(data) data = data**gamma colormap = Colormap(interpolated, controls=data) self.image = NumpyQImage( np.array( make_colorbar(colormap, size=(512, 1), horizontal=False)[::-1])) self.repaint()
def clear(self): """ Remove color markers. Reset to initial state. """ self.color_list = [] self.position_list = [] self.image = convert_colormap_to_image(Colormap([(0, 0, 0), (1, 1, 1)])) self.repaint()
def load_simulation_dataset(path): """Load a simulation dataset. Parameters ---------- path : str Path to simulation data to load. Returns ------- dataset : zarr.hierarchy.Group Loaded simulation dataset. """ # Load dataset path = Path(path) dataset = zarr.open(path.as_posix(), mode='r') metadata = dataset.attrs.asdict() # If dataset is a full dataset return it if dataset.attrs['waver'] and dataset.attrs['dataset']: # Return simulation wave data clim = max(dataset['wave'][0].max(), abs( dataset['wave'][0].min())) / 3**dataset['wave'][0].ndim wave_cmap = Colormap( [[0.55, 0, .32, 1], [0, 0, 0, 0], [0.15, 0.4, 0.1, 1]], name='PBlG') wave_dict = { 'colormap': wave_cmap, 'contrast_limits': [-clim, clim], 'name': 'wave', 'metadata': metadata } speed_cmap = Colormap([[0, 0, 0, 0], [0.7, 0.5, 0, 1]], name='Orange') speed_dict = { 'colormap': speed_cmap, 'visible': False, 'contrast_limits': (metadata['min_speed'], metadata['max_speed']), 'name': 'speed', 'metadata': metadata } return [(dataset['wave'], wave_dict, 'image'), (dataset['speed'], speed_dict, 'image')] else: raise ValueError(f'Dataset at {path} not valid waver simulation')
def test_colormaps(): """Test setting test_colormaps.""" shapes = [(40, 20), (20, 10), (10, 5)] np.random.seed(0) data = [np.random.random(s) for s in shapes] layer = Image(data, multiscale=True) assert layer.colormap.name == 'gray' assert isinstance(layer.colormap, Colormap) layer.colormap = 'magma' assert layer.colormap.name == 'magma' assert isinstance(layer.colormap, Colormap) cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.3, 0.7, 0.2, 1.0]]) layer.colormap = 'custom', cmap assert layer.colormap.name == 'custom' assert layer.colormap == cmap cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.7, 0.2, 0.6, 1.0]]) layer.colormap = {'new': cmap} assert layer.colormap.name == 'new' assert layer.colormap == cmap layer = Image(data, multiscale=True, colormap='magma') assert layer.colormap.name == 'magma' assert isinstance(layer.colormap, Colormap) cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.3, 0.7, 0.2, 1.0]]) layer = Image(data, multiscale=True, colormap=('custom', cmap)) assert layer.colormap.name == 'custom' assert layer.colormap == cmap cmap = Colormap([[0.0, 0.0, 0.0, 0.0], [0.7, 0.2, 0.6, 1.0]]) layer = Image(data, multiscale=True, colormap={'new': cmap}) assert layer.colormap.name == 'new' assert layer.colormap == cmap
def save(self): if self.show_colormap.colormap: rand_name = custom_name_generate(self.prohibited_names, self.settings.colormap_dict) self.prohibited_names.add(rand_name) colors = list(self.show_colormap.colormap.colors) positions = list(self.show_colormap.colormap.controls) if positions[0] != 0: positions.insert(0, 0) colors.insert(0, colors[0]) if positions[-1] != 1: positions.append(1) colors.append(colors[-1]) self.settings.colormap_dict[rand_name] = Colormap(colors=np.array(colors), controls=np.array(positions)) self.settings.chosen_colormap_change(rand_name, True) self.colormap_selected.emit(self.settings.colormap_dict[rand_name][0])
def create_colormap(self) -> Colormap: return Colormap(name='IMC', colors=[[0., 0., 0., self.color[-1]], list(self._color)], interpolation='linear')
def simulation( ndim: int = 2, length: float = 12.8, spacing: float = 100, min_speed: float = 343, max_speed: float = 686, time_step: float = 50, duration: float = 60, frequency: float = 500, method: str = 'flat', custom: 'napari.layers.Layer' = None, spatial_downsample: int = 1, temporal_downsample: int = 1, boundary: int = 0, ) -> 'napari.types.LayerDataTuple': """Run a single simulation. """ size = ndim * (length / 1e3, ) # length always same in all dimensions spacing = spacing / 1e6 time_ste = time_step / 1e9 period = 1 / frequency / 1e3 location = (s / 2 for s in size) duration = duration / 1e6 # Add randomly sampled speed array if method == 'custom': raw_speed = custom.data if hasattr(custom, 'num_colors'): raw_speed = raw_speed / raw_speed.max() else: import numpy as np raw_speed = np.clip(raw_speed, custom.contrast_limits[0], custom.contrast_limits[1]) raw_speed = (raw_speed - custom.contrast_limits[0]) / ( custom.contrast_limits[1] - custom.contrast_limits[0]) speed = raw_speed * (max_speed - min_speed) + min_speed else: speed = method detected_wave, detected_speed = run_single_source( size=size, spacing=spacing, location=location, duration=duration, max_speed=max_speed, min_speed=min_speed, speed=speed, period=period, spatial_downsample=spatial_downsample, temporal_downsample=temporal_downsample, boundary=boundary) # Return simulation wave data clim = max(detected_wave.max(), abs(detected_wave.min())) / 3**ndim wave_cmap = Colormap( [[0.55, 0, .32, 1], [0, 0, 0, 0], [0.15, 0.4, 0.1, 1]], name='PBlG') wave_dict = { 'colormap': wave_cmap, 'contrast_limits': [-clim, clim], 'name': 'wave' } speed_cmap = Colormap([[0, 0, 0, 0], [0.7, 0.5, 0, 1]], name='Orange') speed_dict = { 'colormap': speed_cmap, 'opacity': 0.5, 'contrast_limits': (min_speed, max_speed), 'name': 'speed' } return [(detected_wave, wave_dict, 'Image'), (detected_speed, speed_dict, 'Image')]