def _resample_nearest_linear(orig, dimensions, method, offset, m1): """Resample Map using either linear or nearest interpolation.""" dimlist = [] # calculate new dims for i in range(orig.ndim): base = np.arange(dimensions[i]) dimlist.append((orig.shape[i] - m1) / (dimensions[i] - m1) * (base + offset) - offset) # specify old coordinates old_coords = [np.arange(i, dtype=np.float) for i in orig.shape] # first interpolation - for ndims = any mint = scipy.interpolate.interp1d(old_coords[-1], orig, bounds_error=False, fill_value=min(old_coords[-1]), kind=method) new_data = mint(dimlist[-1]) trorder = [orig.ndim - 1] + list(range(orig.ndim - 1)) for i in range(orig.ndim - 2, -1, -1): new_data = new_data.transpose(trorder) mint = scipy.interpolate.interp1d(old_coords[i], new_data, bounds_error=False, fill_value=min(old_coords[i]), kind=method) new_data = mint(dimlist[i]) if orig.ndim > 1: # need one more transpose to return to original dimensions new_data = new_data.transpose(trorder) return new_data
def randomized_auto_const_bg(self, amount): """Automatically determine background. Only consider a randomly chosen subset of the image. Parameters ---------- amount : int Size of random sample that is considered for calculation of the background. """ cols = [randint(0, self.shape[1] - 1) for _ in range(amount)] # pylint: disable=E1101,E1103 data = self.data.astype(to_signed(self.dtype)) # Subtract average value from every frequency channel. tmp = (data - np.average(self.data, 1).reshape(self.shape[0], 1)) # Get standard deviation at every point of time. # Need to convert because otherwise this class's __getitem__ # is used which assumes two-dimensionality. tmp = tmp[:, cols] sdevs = np.asarray(np.std(tmp, 0)) # Get indices of values with lowest standard deviation. cand = sorted(range(amount), key=lambda y: sdevs[y]) # Only consider the best 5 %. realcand = cand[:max(1, int(0.05 * len(cand)))] # Average the best 5 % bg = np.average(self[:, [cols[r] for r in realcand]], 1) return bg.reshape(self.shape[0], 1)
def __init__(self, data, image_axes=[-2, -1], axis_ranges=None, **kwargs): all_axes = list(range(self.naxis)) # Handle negative indexes self.image_axes = [all_axes[i] for i in image_axes] slider_axes = list(range(self.naxis)) for x in self.image_axes: slider_axes.remove(x) if len(slider_axes) != self.num_sliders: raise ValueError("Specific the same number of axes as sliders!") self.slider_axes = slider_axes # Verify that combined slider_axes and image_axes make all axes ax = self.slider_axes + self.image_axes ax.sort() if ax != list(range(self.naxis)): raise ValueError("spatial_axes and slider_axes mismatch") self.axis_ranges = self._sanitize_axis_ranges(axis_ranges, data) # create data slice self.frame_slice = [slice(None)] * self.naxis for i in self.slider_axes: self.frame_slice[i] = 0 base_kwargs = {'slider_functions': [self.update_plot] * self.num_sliders, 'slider_ranges': [self.axis_ranges[i] for i in self.slider_axes]} base_kwargs.update(kwargs) BaseFuncAnimator.__init__(self, data, **base_kwargs)
def __init__(self, data, image_axes=[-2, -1], axis_ranges=None, **kwargs): all_axes = list(range(self.naxis)) # Handle negative indexes self.image_axes = [all_axes[i] for i in image_axes] slider_axes = list(range(self.naxis)) for x in self.image_axes: slider_axes.remove(x) if len(slider_axes) != self.num_sliders: raise ValueError("Specific the same number of axes as sliders!") self.slider_axes = slider_axes # Verify that combined slider_axes and image_axes make all axes ax = self.slider_axes + self.image_axes ax.sort() if ax != list(range(self.naxis)): raise ValueError("spatial_axes and slider_axes mismatch") self.axis_ranges = self._sanitize_axis_ranges(axis_ranges, data) # create data slice self.frame_slice = [slice(None)] * self.naxis for i in self.slider_axes: self.frame_slice[i] = 0 base_kwargs = { 'slider_functions': [self.update_plot] * self.num_sliders, 'slider_ranges': [self.axis_ranges[i] for i in self.slider_axes] } base_kwargs.update(kwargs) BaseFuncAnimator.__init__(self, data, **base_kwargs)
def __init__(self, data, image_axes=[-2, -1], axis_ranges=None, **kwargs): all_axes = list(range(self.naxis)) # Handle negative indexes self.image_axes = [all_axes[i] for i in image_axes] slider_axes = list(range(self.naxis)) for x in self.image_axes: slider_axes.remove(x) if len(slider_axes) != self.num_sliders: raise ValueError("Specific the same number of axes as sliders!") self.slider_axes = slider_axes # Verify that combined slider_axes and image_axes make all axes ax = self.slider_axes + self.image_axes ax.sort() if ax != list(range(self.naxis)): raise ValueError("spatial_axes and slider_axes mismatch") self.axis_ranges = self._sanitize_axis_ranges(axis_ranges, data) # Due to some reason, if a slider axis range is of length two and the # difference between the entries is 1, the slider start and end both get # set to the 0th value stopping the animation updating the plot. # In this case iterate the latter element by one to get the desired behaviour. hacked_axis_ranges = [False] * len(self.axis_ranges) index_changed = [None] * len(self.axis_ranges) for i in range(len(self.axis_ranges)): if len(self.axis_ranges[i]) == 2 and data.shape[i] == 2: if 0 <= (self.axis_ranges[i][-1] - self.axis_ranges[i][0]) <= 1: axis_ranges[i][-1] += 1 index_changed[i] = -1 elif -1 <= (self.axis_ranges[i][-1] - self.axis_ranges[i][0]) <= 0: axis_ranges[i][0] -= 1 index_changed[i] = 0 hacked_axis_ranges[i] = True # create data slice self.frame_slice = [slice(None)] * self.naxis for i in self.slider_axes: self.frame_slice[i] = 0 base_kwargs = { 'slider_functions': [self.update_plot] * self.num_sliders, 'slider_ranges': [self.axis_ranges[i] for i in self.slider_axes] } base_kwargs.update(kwargs) BaseFuncAnimator.__init__(self, data, **base_kwargs) # Undo hack of length 2 axis ranges for i in np.arange(len(self.axis_ranges)): if len(self.axis_ranges[i]) == 2 and data.shape[i] == 2: if hacked_axis_ranges[i]: if index_changed[i] == 0: axis_ranges[i][0] += 1 elif index_changed[i] == -1: axis_ranges[i][-1] -= 1
def test_apply_shifts(aia171_test_map): # take two copies of the AIA image and create a test mapcube. mc = map.Map([aia171_test_map, aia171_test_map], cube=True) # Pixel displacements have the y-displacement as the first entry numerical_displacements = {"x": np.asarray([0.0, -2.7]), "y": np.asarray([0.0, -10.4])} astropy_displacements = {"x": numerical_displacements["x"] * u.pix, "y": numerical_displacements["y"] * u.pix} # Test to see if the code can detect the fact that the input shifts are not # astropy quantities with pytest.raises(TypeError): tested = apply_shifts(mc, numerical_displacements["y"], astropy_displacements["x"]) with pytest.raises(TypeError): tested = apply_shifts(mc, astropy_displacements["y"], numerical_displacements["x"]) with pytest.raises(TypeError): tested = apply_shifts(mc, numerical_displacements["y"], numerical_displacements["x"]) # Test returning with no extra options - the code returns a mapcube only test_output = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"]) assert(isinstance(test_output, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"], clip=False) assert(test_mc[0].data.shape == aia171_test_map.data.shape) assert(test_mc[1].data.shape == aia171_test_map.data.shape) # Test returning with clipping. Output layers should be smaller than the # original layer by a known amount. test_mc = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"], clip=True) for i in range(0, len(test_mc.maps)): clipped = calculate_clipping(astropy_displacements["y"], astropy_displacements["x"]) assert(test_mc[i].data.shape[0] == mc[i].data.shape[0] - np.max(clipped[0].value)) assert(test_mc[i].data.shape[1] == mc[i].data.shape[1] - np.max(clipped[1].value)) # Test returning with default clipping. The default clipping is set to # true, that is the mapcube is clipped. Output layers should be smaller # than the original layer by a known amount. test_mc = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"]) for i in range(0, len(test_mc.maps)): clipped = calculate_clipping(astropy_displacements["y"], astropy_displacements["x"]) assert(test_mc[i].data.shape[0] == mc[i].data.shape[0] - np.max(clipped[0].value)) assert(test_mc[i].data.shape[1] == mc[i].data.shape[1] - np.max(clipped[1].value)) # Test that keywords are correctly passed # Test for an individual keyword test_mc = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"], clip=False, cval=np.nan) assert(np.all(np.logical_not(np.isfinite(test_mc[1].data[:, -1])))) # Test for a combination of keywords, and that changing the interpolation # order and how the edges are treated changes the results. test_mc1 = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"], clip=False, order=2, mode='reflect') test_mc2 = apply_shifts(mc, astropy_displacements["y"], astropy_displacements["x"], clip=False) assert(np.all(test_mc1[1].data[:, -1] != test_mc2[1].data[:, -1]))
def _add_widgets(self): self.buttons = [] for i in range(0, self.num_buttons): x = i * 2 # The i+1/10. is a bug that if you make two axes directly on top of # one another then the divider doesn't work. self.buttons.append(self.fig.add_axes((0., 0., 0. + i / 10., 1.))) locator = self.divider.new_locator(nx=x, ny=self.button_ny) self.buttons[-1].set_axes_locator(locator) self.buttons[-1]._button = widgets.Button(self.buttons[-1], self.button_labels[i]) self.buttons[-1]._button.on_clicked(self.button_func[i]) self.sliders = [] self.slider_buttons = [] for i in range(self.num_sliders): x = i * 2 self.sliders.append(self.fig.add_axes( (0., 0., 0.01 + i / 10., 1.))) if self.num_buttons == 0: nx1 = 1 else: nx1 = -3 locator = self.divider.new_locator(nx=0, ny=x, nx1=nx1) self.sliders[-1].set_axes_locator(locator) sframe = SliderPB(self.sliders[-1], "{slide:d}".format(slide=i), self.slider_ranges[i][0], self.slider_ranges[i][-1] - 1, valinit=self.slider_ranges[i][0], valfmt='%4.1f') sframe.on_changed(self._slider_changed, sframe) sframe.slider_ind = i sframe.cval = sframe.val self.sliders[-1]._slider = sframe self.slider_buttons.append( self.fig.add_axes((0., 0., 0.05 + x / 10., 1.))) if self.num_buttons == 0: nx = 2 else: nx = 2 + 2 * (self.num_buttons - 1) locator = self.divider.new_locator(nx=nx, ny=x) self.slider_buttons[-1].set_axes_locator(locator) butt = ButtonPB(self.slider_buttons[-1], ">") butt.on_clicked(self._click_slider_button, butt, sframe) butt.clicked = False self.slider_buttons[-1]._button = butt
def get_dates(self): """ Return all partial days contained within the timerange """ dates = [] dates = [self.start.date() + timedelta(days=i) for i in range(int(self.days.value) + 1)] return dates
def split(self, n=2): """ Splits the TimeRange into multiple equally sized parts. Parameters ---------- n : int The number of times to split the time range (must >= 1) Returns ------- time ranges: list An list of equally sized TimeRange objects between the start and end times. Raises ------ ValueError If requested amount is less than 1 """ if n <= 0: raise ValueError('n must be greater than or equal to 1') subsections = [] previous_time = self.start next_time = None for _ in range(n): next_time = previous_time + self.dt // n next_range = TimeRange(previous_time, next_time) subsections.append(next_range) previous_time = next_time return subsections
def test_combine_freqs(): image = np.random.rand(5, 3600) spec = LinearTimeSpectrogram(image, np.linspace(0, image.shape[1] - 1, image.shape[1]), np.array([8, 6, 4, 2, 0]), datetime(2010, 1, 1, 0, 15), datetime(2010, 1, 1, 0, 30), 900, 0.25 ) image = np.random.rand(5, 3600) spec2 = LinearTimeSpectrogram(image, np.linspace(0, image.shape[1] - 1, image.shape[1]), np.array([9, 7, 5, 3, 1]), datetime(2010, 1, 1, 0, 15), datetime(2010, 1, 1, 0, 30), 900, 0.25 ) comb = LinearTimeSpectrogram.combine_frequencies([spec, spec2]) stuff = [spec, spec2] # print comb for freq in range(10): assert np.array_equal( comb[9 - freq, :], stuff[freq % 2][4 - freq // 2, :] )
def resample(self, dimensions, method='linear'): """Returns a new Map that has been resampled up or down""" new_maps = [] for i in range(0, len(self)): new_maps.append( self._get_map(i).resample(dimensions, method=method)) return MapCubed(new_maps)
def __getitem__(self, key): if isinstance(key, slice): entries = [] start = 0 if key.start is None else key.start stop = len(self) if key.stop is None else key.stop step = 1 if key.step is None else key.step for i in range(start, stop, step): try: entry = self[i] except IndexError: break else: self._cache[entry.id] entries.append(entry) return entries # support negative indices if key < 0 < abs(key) <= len(self): key %= len(self) for i, entry in enumerate(self): if i == key: # "touch" the entry in the cache to intentionally cause # possible side-effects self._cache[entry.id] return entry raise IndexError
def test_setting_cache_size(database_using_lrucache): assert database_using_lrucache.cache_maxsize == 3 assert database_using_lrucache.cache_size == 0 for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 3 assert database_using_lrucache.cache_size == 3 assert database_using_lrucache.cache_maxsize == 3 database_using_lrucache.set_cache_size(5) assert database_using_lrucache.cache_size == 3 assert database_using_lrucache.cache_maxsize == 5 for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 5 assert database_using_lrucache.cache_size == 5 assert database_using_lrucache.cache_maxsize == 5
def make_mask(self, max_dist): mask = np.zeros(self.shape, dtype=np.bool) for n, item in enumerate(range(len(self))): freq = self.arr.freq_axis[0] - item * self.delt if abs(self.get_freq(item) - freq) > max_dist: mask[n, :] = True return mask
def submap(self, range_a, range_b, range_c=None): """Returns a submap of the map with the specified range. """ new_maps = [] for i in range(0, len(self)): new_maps.append(self._get_map(i).submap(range_a, range_b)) return MapCubed(new_maps)
def range(self, timerange): """ Gets the directories for a certain range of time (i.e. using `~sunpy.time.TimeRange`). Parameters ---------- timerange : `~sunpy.time.timerange.TimeRange` Time interval where to find the directories for a given pattern. Returns ------- directories : list of strings List of all the possible directories valid for the time range given. Notice that these directories may not exist in the archive. """ # find directory structure - without file names directorypattern = os.path.dirname(self.pattern) + '/' # TODO what if there's not slashes? rangedelta = timerange.dt timestep = self._smallerPattern(directorypattern) if timestep is None: return [directorypattern] else: # Number of elements in the time range (including end) n_steps = rangedelta.total_seconds()/timestep.total_seconds() TotalTimeElements = int(round(n_steps)) + 1 directories = [(timerange.start + n * timestep).strftime(directorypattern) for n in range(TotalTimeElements)] # TODO if date <= endate return directories
def test_split_series_using_lytaf(): '''test the downloading of the LYTAF file and subsequent queries''' tmp_dir = tempfile.mkdtemp() lyra.download_lytaf_database(lytaf_dir=tmp_dir) assert os.path.exists(os.path.join(tmp_dir, 'annotation_ppt.db')) # test split_series_using_lytaf # construct a dummy signal for testing purposes basetime = parse_time('2010-06-13 02:00') seconds = 3600 dummy_time = [basetime + datetime.timedelta(0, s) for s in range(seconds)] dummy_data = np.random.random(seconds) lytaf_tmp = lyra.get_lytaf_events('2010-06-13 02:00', '2010-06-13 06:00', lytaf_path=tmp_dir, combine_files=["ppt"]) split = lyra.split_series_using_lytaf(dummy_time, dummy_data, lytaf_tmp) assert type(split) == list assert len(split) == 4 assert split[0]['subtimes'][0] == datetime.datetime(2010, 6, 13, 2, 0) assert split[0]['subtimes'][-1] == datetime.datetime(2010, 6, 13, 2, 7, 2) assert split[3]['subtimes'][0] == datetime.datetime(2010, 6, 13, 2, 59, 41) assert split[3]['subtimes'][-1] == datetime.datetime( 2010, 6, 13, 2, 59, 58) # Test case when no LYTAF events found in time series. split_no_lytaf = lyra.split_series_using_lytaf(dummy_time, dummy_data, LYTAF_TEST) assert type(split_no_lytaf) == list assert type(split_no_lytaf[0]) == dict assert not set(split_no_lytaf[0].keys()).symmetric_difference( {'subtimes', 'subdata'}) assert split_no_lytaf[0]["subtimes"] == dummy_time assert split_no_lytaf[0]["subdata"].all() == dummy_data.all()
def test_split_series_using_lytaf(): '''test the downloading of the LYTAF file and subsequent queries''' tmp_dir = tempfile.mkdtemp() lyra.download_lytaf_database(lytaf_dir=tmp_dir) assert os.path.exists(os.path.join(tmp_dir, 'annotation_ppt.db')) # test split_series_using_lytaf # construct a dummy signal for testing purposes basetime = parse_time('2010-06-13 02:00') seconds = 3600 dummy_time = [basetime + datetime.timedelta(0, s) for s in range(seconds)] dummy_data = np.random.random(seconds) lytaf_tmp = lyra.get_lytaf_events('2010-06-13 02:00', '2010-06-13 06:00', lytaf_path=tmp_dir, combine_files=["ppt"]) split = lyra.split_series_using_lytaf(dummy_time, dummy_data, lytaf_tmp) assert type(split) == list assert len(split) == 4 assert split[0]['subtimes'][0] == datetime.datetime(2010, 6, 13, 2, 0) assert split[0]['subtimes'][-1] == datetime.datetime(2010, 6, 13, 2, 7, 2) assert split[3]['subtimes'][0] == datetime.datetime(2010, 6, 13, 2, 59, 41) assert split[3]['subtimes'][-1] == datetime.datetime(2010, 6, 13, 2, 59, 58) # Test case when no LYTAF events found in time series. split_no_lytaf = lyra.split_series_using_lytaf(dummy_time, dummy_data, LYTAF_TEST) assert type(split_no_lytaf) == list assert type(split_no_lytaf[0]) == dict assert not set(split_no_lytaf[0].keys()).symmetric_difference({'subtimes', 'subdata'}) assert split_no_lytaf[0]["subtimes"] == dummy_time assert split_no_lytaf[0]["subdata"].all() == dummy_data.all()
def _add_widgets(self): self.buttons = [] for i in range(0, self.num_buttons): x = i*2 # The i+1/10. is a bug that if you make two axes directly on top of # one another then the divider doesn't work. self.buttons.append(self.fig.add_axes((0., 0., 0.+i/10., 1.))) locator = self.divider.new_locator(nx=x, ny=self.button_ny) self.buttons[-1].set_axes_locator(locator) self.buttons[-1]._button = widgets.Button(self.buttons[-1], self.button_labels[i]) self.buttons[-1]._button.on_clicked(self.button_func[i]) self.sliders = [] self.slider_buttons = [] for i in range(self.num_sliders): x = i * 2 self.sliders.append(self.fig.add_axes((0., 0., 0.01+i/10., 1.))) if self.num_buttons == 0: nx1 = 1 else: nx1 = -3 locator = self.divider.new_locator(nx=0, ny=x, nx1=nx1) self.sliders[-1].set_axes_locator(locator) sframe = SliderPB(self.sliders[-1], "{slide:d}".format(slide=i), self.slider_ranges[i][0], self.slider_ranges[i][-1]-1, valinit=self.slider_ranges[i][0], valfmt='%4.1f') sframe.on_changed(self._slider_changed, sframe) sframe.slider_ind = i sframe.cval = sframe.val self.sliders[-1]._slider = sframe self.slider_buttons.append( self.fig.add_axes((0., 0., 0.05+x/10., 1.))) if self.num_buttons == 0: nx = 2 else: nx = 2 + 2*(self.num_buttons-1) locator = self.divider.new_locator(nx=nx, ny=x) self.slider_buttons[-1].set_axes_locator(locator) butt = ButtonPB(self.slider_buttons[-1], ">") butt.on_clicked(self._click_slider_button, butt, sframe) butt.clicked = False self.slider_buttons[-1]._button = butt
def draw(self): """ Draw current state of progress bar onto and empty bar. """ cur = self.current self.current = 0 for _ in range(cur): self.poke()
def test_lytaf_event2string(): """Test _lytaf_event2string() associates correct numbers and events.""" out_test = lyra._lytaf_event2string(list(range(12))) assert out_test == ['LAR', 'N/A', 'UV occult.', 'Vis. occult.', 'Offpoint', 'SAA', 'Auroral zone', 'Moon in LYRA', 'Moon in SWAP', 'Venus in LYRA', 'Venus in SWAP'] out_test_single = lyra._lytaf_event2string(1) assert out_test_single == ['LAR']
def test_repair_image_nonfinite(): for i in range(0, 9): for non_number in [np.nan, np.inf]: a = np.ones((9)) a[i] = non_number b = a.reshape(3, 3) c = repair_image_nonfinite(b) assert (np.isfinite(c).all())
def test_repair_image_nonfinite(): for i in range(0, 9): for non_number in [np.nan, np.inf]: a = np.ones((9)) a[i] = non_number b = a.reshape(3, 3) c = repair_image_nonfinite(b) assert(np.isfinite(c).all())
def test_parse_time_pandas_index(): inputs = [datetime(2012, 1, i) for i in range(1, 13)] ind = pandas.DatetimeIndex(inputs) dts = parse_time(ind) assert isinstance(dts, np.ndarray) assert all([isinstance(dt, datetime) for dt in dts]) assert list(dts) == inputs
def test_parse_time_pandas_index(): inputs = [datetime(2012, 1, i) for i in range(1, 13)] ind = pandas.tseries.index.DatetimeIndex(inputs) dts = parse_time(ind) assert isinstance(dts, np.ndarray) assert all([isinstance(dt, datetime) for dt in dts]) assert list(dts) == inputs
def test_add_many(database): assert len(database) == 0 database.add_many((DatabaseEntry() for _ in range(5))) assert len(database) == 5 database.undo() with pytest.raises(EmptyCommandStackError): database.undo() assert len(database) == 0 database.redo() assert len(database) == 5
def test_setting_cache_size_shrinking(database_using_lrucache): assert database_using_lrucache.cache_maxsize == 3 assert database_using_lrucache.cache_size == 0 for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 3 assert database_using_lrucache.cache_maxsize == 3 assert database_using_lrucache.cache_size == 3 database_using_lrucache.set_cache_size(2) assert database_using_lrucache.cache_maxsize == 2 assert database_using_lrucache.cache_size == 2 assert len(database_using_lrucache) == 2 assert list(database_using_lrucache) == [ DatabaseEntry(id=4), DatabaseEntry(id=5) ] for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 2 assert database_using_lrucache.cache_maxsize == 2 assert database_using_lrucache.cache_size == 2
def undo(self, n=1): """Undo the last n commands. The default is to undo only the last command. If there is no command that can be undone because n is too big or because no command has been executed yet, :exc:`sunpy.database.commands.EmptyCommandStackError` is raised. """ for _ in range(n): command = self.pop_undo_command() command.undo() self.push_redo_command(command)
def __init__(self, data, plot_axis_index=-1, axis_ranges=None, ylabel=None, xlabel=None, xlim=None, ylim=None, **kwargs): # Check inputs. self.plot_axis_index = int(plot_axis_index) if self.plot_axis_index not in range(-data.ndim, data.ndim): raise ValueError( "plot_axis_index must be either 0 or 1 (or equivalent " "negative indices) referring to the axis of data to be " "used for a single plot.") if data.ndim < 2: raise ValueError( "data must have at least two dimensions. One for data " "for each single plot and at least one for time/iteration.") # Ensure axis_ranges are input correctly. if axis_ranges is not None: if axis_ranges[self.plot_axis_index] is not None: if (len(axis_ranges[self.plot_axis_index]) != data.shape[self.plot_axis_index] and axis_ranges[self.plot_axis_index].shape != data.shape): raise ValueError( "The plot_axis_index axis range must be specified as None " "a 1D array of same length as plot_axis_index axis, " "or an array of same shape as data.") # Define number of slider axes. self.naxis = data.ndim self.num_sliders = self.naxis - 1 # Attach data to class. if axis_ranges is not None and all(axis_range is None for axis_range in axis_ranges): axis_ranges = None if axis_ranges is None or axis_ranges[self.plot_axis_index] is None: self.xdata = np.arange(data.shape[self.plot_axis_index]) else: self.xdata = np.asarray(axis_ranges[self.plot_axis_index]) if ylim is None: ylim = (data.min(), data.max()) if xlim is None: xlim = (self.xdata.min(), self.xdata.max()) self.ylim = ylim self.xlim = xlim self.xlabel = xlabel self.ylabel = ylabel # Run init for base class super(LineAnimator, self).__init__(data, image_axes=[self.plot_axis_index], axis_ranges=axis_ranges, **kwargs)
def test_setting_cache_size_shrinking(database_using_lrucache): assert database_using_lrucache.cache_maxsize == 3 assert database_using_lrucache.cache_size == 0 for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 3 assert database_using_lrucache.cache_maxsize == 3 assert database_using_lrucache.cache_size == 3 database_using_lrucache.set_cache_size(2) assert database_using_lrucache.cache_maxsize == 2 assert database_using_lrucache.cache_size == 2 assert len(database_using_lrucache) == 2 assert list(database_using_lrucache) == [ DatabaseEntry(id=4), DatabaseEntry(id=5)] for _ in range(5): database_using_lrucache.add(DatabaseEntry()) assert len(database_using_lrucache) == 2 assert database_using_lrucache.cache_maxsize == 2 assert database_using_lrucache.cache_size == 2
def superpixel(self, dimensions, offset=(0, 0) * u.pixel, func=np.sum): """Returns a new map consisting of superpixels formed from the original data. Useful for increasing signal to noise ratio in images. """ new_maps = [] print(offset) for i in range(0, len(self)): new_maps.append( self._get_map(i).superpixel(dimensions, offset=offset, func=func)) return MapCubed(new_maps)
def poke(self, n=1): """ Increase finished items by n. May advance the progress bar by one or more fields. """ if self.current > self.n: raise ValueError("ProgressBar overflowed.") diff = int((self.current + n) / self.step) - int(self.current / self.step) for _ in range(diff): self._draw_one() self.current += n
def _resample_neighbor(orig, dimensions, offset, m1): """Resample Map using closest-value interpolation.""" dimlist = [] for i in range(orig.ndim): base = np.indices(dimensions)[i] dimlist.append((orig.shape[i] - m1) / (dimensions[i] - m1) * (base + offset) - offset) cd = np.array(dimlist).round().astype(int) return orig[list(cd)]
def redo(self, n=1): """Redo the last n commands which have been undone using the undo method. The default is to redo only the last command which has been undone using the undo method. If there is no command that can be redone because n is too big or because no command has been undone yet, :exc:`sunpy.database.commands.EmptyCommandStackError` is raised. """ for _ in range(n): command = self.pop_redo_command() command() self.push_undo_command(command)
def poke(self, n=1): """ Increase finished items by n. May advance the progress bar by one or more fields. """ if self.current > self.n: raise ValueError("ProgressBar overflowed.") diff = int( (self.current + n) / self.step) - int(self.current / self.step) for _ in range(diff): self._draw_one() self.current += n
def filled_database(): database = Database('sqlite:///:memory:') for i in range(1, 11): entry = DatabaseEntry() database.add(entry) # every fourth entry gets the tag 'foo' if i % 4 == 0: database.tag(entry, 'foo') # every fifth entry gets the tag 'bar' if i % 5 == 0: database.tag(entry, 'bar') database.commit() return database
def __init__(self, data, wcs=None, image_axes=[-1, -2], unit_x_axis=None, unit_y_axis=None, axis_ranges=None, **kwargs): if not isinstance(wcs, astropy.wcs.WCS): raise ValueError("wcs data should be provided.") if wcs.wcs.naxis is not data.ndim: raise ValueError("Dimensions of data and wcs not matching") self.wcs = wcs list_slices_wcsaxes = [0 for i in range(self.wcs.naxis)] list_slices_wcsaxes[image_axes[0]] = 'x' list_slices_wcsaxes[image_axes[1]] = 'y' self.slices_wcsaxes = list_slices_wcsaxes[::-1] self.unit_x_axis = unit_x_axis self.unit_y_axis = unit_y_axis super(ImageAnimatorWCS, self).__init__(data, image_axes=image_axes, axis_ranges=axis_ranges, **kwargs)
def base_difference(self, base=0, fraction=False): """ Calculate the base difference of a mapcube. Parameters ---------- base : int, sunpy.map.Map If base is an integer, this is understood as an index to the input mapcube. Differences are calculated relative to the map at index 'base'. If base is a sunpy map, then differences are calculated relative to that map fraction : boolean If False, then absolute changes relative to the base map are returned. If True, then fractional changes relative to the base map are returned Returns ------- sunpy.map.MapCube A mapcube containing base difference of the input mapcube. """ if not (isinstance(base, GenericMap)): base_data = self.data[:, :, base] else: base_data = base.data if base_data.shape != self.data.shape: raise ValueError( 'Base map does not have the same shape as the maps in the input mapcube.' ) # Fractional changes or absolute changes if fraction: relative = base_data else: relative = 1.0 # Create a list containing the data for the new map object new_mc = [] for i in range(0, len(self)): new_data = (self.data[:, :, i] - base_data) / relative new_mc.append(Map(new_data, self._meta[i])) # Create the new mapcube and return return MapCubed(new_mc)
def times(self): """Returns a list of time ranges where values are True. Returns ------- outtr : `~sunpy.time.TimeRange` array An array of time ranges """ labeling = label(self.data) timeranges = [] for i in range(1, labeling[1] + 1): eventindices = (labeling[0] == i).nonzero() timeranges.append(TimeRange(self.data.index[eventindices[0][0]], self.data.index[eventindices[0][-1]])) return timeranges
def make_table_list(self): """ Creates a list of table names and prompts the user for a choice This takes the table of table names from get_table_names(), creates a list of the names, sorts them, then presents the tables in a convenient menu for the user to choose from. It returns a string containing the name of the table that the user picked. Returns ------- temp: `str` contains the name of the table that the user picked. Examples -------- >>> from sunpy.net.helio import hec >>> hc = hec.HECClient() >>> hc.make_table_list() # doctest: +SKIP """ table_list = [] tables = self.get_table_names() for i in tables: table = i[0] if len(table) > 0: table_list.append(table) table_list.sort() for index, table in enumerate(table_list): print(('{number:3d}) {table}'.format(number=index + 1, table=table))) while True: stdinput = input("\nPlease enter a table number between 1 and " "{elem:d} " "('e' to exit): ".format(elem=len(table_list))) if stdinput.lower() == "e" or stdinput.lower() == "exit": temp = None break temp = [int(s) for s in stdinput.split() if s.isdigit()] temp = temp[0] - 1 if temp in range(0, len(table_list)): temp = table_list[temp] break else: print("Choice outside of bounds") return temp
def __init__(self, *args, **kwargs): self._maps = expand_list(args) for m in self._maps: if not isinstance(m, GenericMap): raise ValueError( 'CompositeMap expects pre-constructed map objects.') # Default alpha and zorder values alphas = [1] * len(self._maps) zorders = list(range(0, 10 * len(self._maps), 10)) levels = [False] * len(self._maps) # Set z-order and alpha values for the map for i, m in enumerate(self._maps): m.zorder = zorders[i] m.alpha = alphas[i] m.levels = levels[i]
def combine_frequencies(cls, specs): """Return new spectrogram that contains frequencies from all the spectrograms in spec. Only returns time intersection of all of them. Parameters ---------- spec : list List of spectrograms of which to combine the frequencies into one. """ if not specs: raise ValueError("Need at least one spectrogram.") specs = cls.intersect_time(specs) one = specs[0] dtype_ = max(sp.dtype for sp in specs) fsize = sum(sp.shape[0] for sp in specs) new = np.zeros((fsize, one.shape[1]), dtype=dtype_) freq_axis = np.zeros((fsize,)) for n, (data, row) in enumerate(merge( [ [(sp, n) for n in range(sp.shape[0])] for sp in specs ], key=lambda x: x[0].freq_axis[x[1]] )): new[n, :] = data[row, :] freq_axis[n] = data.freq_axis[row] params = { 'time_axis': one.time_axis, # Should be equal 'freq_axis': freq_axis, 'start': one.start, 'end': one.end, 't_delt': one.t_delt, 't_init': one.t_init, 't_label': one.t_label, 'f_label': one.f_label, 'content': one.content, 'instruments': _union(spec.instruments for spec in specs) } return common_base(specs)(new, **params)
def session(): database = Database('sqlite:///:memory:') for i in range(1, 11): entry = tables.DatabaseEntry() database.add(entry) # every entry has a fake download time of 2005-06-15 i:00:00 database.edit(entry, download_time=datetime(2005, 6, 15, i)) # every second entry gets starred if i % 2 == 0: database.star(entry) # every third entry is stored in the path /tmp if i % 3 == 0: database.edit(entry, path='/tmp') # every fifth entry gets the tag 'foo' if i % 5 == 0: database.tag(entry, 'foo') # the last entry gets the FITS header entry INSTRUME=EIT entry.fits_header_entries.append(tables.FitsHeaderEntry('INSTRUME', 'EIT')) database.commit() return database.session