def __init__(self, data, pixel_scale, cadence, interop, savedir, **kwargs): all_axes = list(range(data.ndim)) image_axes = [all_axes[i] for i in kwargs.get('image_axes', [-2, -1])] self.slider_axes = list(range(data.ndim)) for x in image_axes: self.slider_axes.remove(x) if 'cmap' not in kwargs: kwargs['cmap'] = plt.get_cmap('gray') axis_range = [None, None, [0, pixel_scale * data[0, 0, :, :].shape[0]], [0, pixel_scale * data[0, 0, :, :].shape[1]]] axis_range = kwargs.pop('axis_range', axis_range) axis_range = self._sanitize_axis_range(axis_range, data) self.image_extent = list(itertools.chain.from_iterable([axis_range[i] for i in image_axes])) self.pixel_scale = pixel_scale self.cadence = cadence self.slits = [] self.savedir = savedir self.nt = data.shape[0] self.nlambda = data.shape[1] self.interop = interop self.range = range(0, data.shape[1]) button_labels, button_func = self.create_buttons() slider_functions = [self._updateimage]*len(self.slider_axes) + [self.update_range]*2 + [self.update_im_clim]*2 slider_ranges = [axis_range[i] for i in self.slider_axes] + [range(0, self.nlambda)]*2 + [np.arange(0, 99.9)]*2 ImageAnimator.__init__(self, data, axis_range=axis_range, button_labels=button_labels, button_func=button_func, slider_functions=slider_functions, slider_ranges=slider_ranges, **kwargs) # Sets up the slit sliders self.sliders[2]._slider.set_val(self.nlambda) self.sliders[3]._slider.slidermax = self.sliders[2]._slider self.sliders[2]._slider.slidermin = self.sliders[3]._slider self.slider_buttons[3].set_visible(False) self.slider_buttons[2].set_visible(False) self.label_slider(3, "Start") self.label_slider(2, "End") # Sets up the intensity scaling sliders self.sliders[-2]._slider.set_val(100) self.sliders[-1]._slider.slidermax = self.sliders[-2]._slider self.sliders[-2]._slider.slidermin = self.sliders[-1]._slider self.slider_buttons[-1].set_visible(False) self.slider_buttons[-2].set_visible(False) self.axes.autoscale(False) self.label_slider(-1, "Min") self.label_slider(-2, "Max")
def __init__(self, data, pixel_scale, savedir, **kwargs): all_axes = list(range(data.ndim)) image_axes = [all_axes[i] for i in kwargs.get('image_axes', [-2,-1])] self.slider_axes = list(range(data.ndim)) for x in image_axes: self.slider_axes.remove(x) axis_range = [None,None, [0, pixel_scale * data[0,0,:,:].shape[0]], [0, pixel_scale * data[0,0,:,:].shape[1]]] axis_range = kwargs.pop('axis_range', axis_range) axis_range = self._sanitize_axis_range(axis_range, data) self.image_extent = list(itertools.chain.from_iterable([axis_range[i] for i in image_axes])) self.pixel_scale = pixel_scale self.r_diff = [] self.slits = [] self.savedir = savedir self.nlambda = data.shape[1] self.nt = data.shape[0] button_labels, button_func = self.create_buttons() slider_functions = [self._updateimage]*len(self.slider_axes) + [self.update_im_clim]*2 slider_ranges = [axis_range[i] for i in self.slider_axes] + [np.arange(0,99.9)]*2 ImageAnimator.__init__(self, data, axis_range=axis_range, button_labels=button_labels, button_func=button_func, slider_functions=slider_functions, slider_ranges=slider_ranges, **kwargs) self.sliders[-2]._slider.set_val(100) self.sliders[-1]._slider.slidermax = self.sliders[-2]._slider self.sliders[-2]._slider.slidermin = self.sliders[-1]._slider self.slider_buttons[-1].set_visible(False) self.slider_buttons[-2].set_visible(False) self.axes.autoscale(False) self.label_slider(-1, "Min") self.label_slider(-2, "Max")
def _plot_3D_cube(self, plot_axis_indices=None, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): """ Plots an interactive visualization of this cube using sliders to move through axes plot using in the image. Parameters other than data and wcs are passed to ImageAnimatorWCS, which in turn passes them to imshow. Parameters ---------- plot_axis_indices: `list` The two axes that make the image. Like [-1,-2] this implies cube instance -1 dimension will be x-axis and -2 dimension will be y-axis. axes_unit: `list` of `astropy.units.Unit` axes_coordinates: `list` of physical coordinates for array or None If None array indices will be used for all axes. If a list it should contain one element for each axis of the numpy array. For the image axes a [min, max] pair should be specified which will be passed to :func:`matplotlib.pyplot.imshow` as extent. For the slider axes a [min, max] pair can be specified or an array the same length as the axis which will provide all values for that slider. If None is specified for an axis then the array indices will be used for that axis. """ # For convenience in inserting dummy variables later, ensure # plot_axis_indices are all positive. plot_axis_indices = [i if i >= 0 else self.data.ndim + i for i in plot_axis_indices] # If axes kwargs not set by user, set them as list of Nones for # each axis for consistent behaviour. if axes_coordinates is None: axes_coordinates = [None] * self.data.ndim if axes_units is None: axes_units = [None] * self.data.ndim # If data_unit set, convert data to that unit if data_unit is None: data = self.data else: data = (self.data * self.unit).to(data_unit).value # Combine data values with mask. data = np.ma.masked_array(data, self.mask) # If axes_coordinates not provided generate an ImageAnimatorWCS plot # using NDCube's wcs object. if (axes_coordinates[plot_axis_indices[0]] is None and axes_coordinates[plot_axis_indices[1]] is None): # If there are missing axes in WCS object, add corresponding dummy axes to data. if data.ndim < self.wcs.naxis: new_shape = list(data.shape) for i in np.arange(self.wcs.naxis)[self.missing_axis[::-1]]: new_shape.insert(i, 1) # Also insert dummy coordinates and units. axes_coordinates.insert(i, None) axes_units.insert(i, None) # Iterate plot_axis_indices if neccessary for j, pai in enumerate(plot_axis_indices): if pai >= i: plot_axis_indices[j] = plot_axis_indices[j] + 1 # Reshape data data = data.reshape(new_shape) # Generate plot ax = ImageAnimatorWCS(data, wcs=self.wcs, image_axes=plot_axis_indices, unit_x_axis=axes_units[plot_axis_indices[0]], unit_y_axis=axes_units[plot_axis_indices[1]], axis_ranges=axes_coordinates, **kwargs) # If one of the plot axes is set manually, produce a basic ImageAnimator object. else: new_axes_coordinates, new_axes_units, default_labels = \ self._derive_axes_coordinates(axes_coordinates, axes_units) # If axis labels not set by user add to kwargs. ax = ImageAnimator(data, image_axes=plot_axis_indices, axis_ranges=new_axes_coordinates, **kwargs) return ax