def plot_contours(roi_array: np.ndarray, template: np.ndarray, fig: plt.figure = None, ax: plt.axis = None): """Plot contours of roi. Args: roi_array (numpy.ndarray): Array of roi masks. template (numpy.ndarray): Template image. fig (matplotlib.pyplot.figure, optional): Defaults to None. Figure to plot to. ax (matplotlib.pyplot.axis, optional): Defaults to None. Axis to plot to. """ if fig is None and ax is None: fig = plt.figure() ax = fig.add_subplot(111) elif ax is None and fig is not None: ax = fig.add_subplot(111) z, _, _ = roi_array.shape np.random.seed(128) colors = np.random.rand(3, z) for idx, img_slice in enumerate(roi_array): clean_border = segmentation.clear_border(img_slice).astype(np.int) template = segmentation.mark_boundaries(template, clean_border, color=colors[:, idx], mode='thick') ax.imshow(template)
def plot_image_grid(images: List[np.array], titles: List[str] = None, figure: plt.figure = None, grayscale: bool = False, transpose: bool = False) -> plt.figure: """ Plot a grid of n x m images Input ----- images: List[np.array] Images in a n x m array titles: List[str] (opt.) List of m titles for each image column figure: plt.figure (opt.) Pyplot figure (if None, will be created) grayscale: bool (opt.) If True return grayscaled images transpose: bool (opt.) If True, transpose the grid Output ------ Pyplot figure filled with the images """ num_cols, num_rows = len(images), len(images[0]) img_ratio = images[0][0].shape[1] / images[0][0].shape[0] if transpose: vert_grid_shape, hori_grid_shape = (1, num_rows), (num_cols, 1) figsize = (int(num_rows * 5 * img_ratio), num_cols * 5) wspace, hspace = 0.2, 0. else: vert_grid_shape, hori_grid_shape = (num_rows, 1), (1, num_cols) figsize = (int(num_cols * 5 * img_ratio), num_rows * 5) hspace, wspace = 0.2, 0. if figure is None: figure = plt.figure(figsize=figsize) imshow_params = {'cmap': plt.get_cmap('gray')} if grayscale else {} grid_spec = gridspec.GridSpec(*hori_grid_shape, wspace=0, hspace=0) for j in range(num_cols): grid_spec_j = gridspec.GridSpecFromSubplotSpec( *vert_grid_shape, subplot_spec=grid_spec[j], wspace=wspace, hspace=hspace) for i in range(num_rows): ax_img = figure.add_subplot(grid_spec_j[i]) # ax_img.axis('off') ax_img.set_yticks([]) ax_img.set_xticks([]) if titles is not None: if transpose: ax_img.set_ylabel(titles[j], fontsize=25) else: ax_img.set_title(titles[j], fontsize=15) ax_img.imshow(images[j][i], **imshow_params) figure.tight_layout() return figure
def plot_state_space_projection(values, conditions, figure_size = (5, 5), alpha = 0.5): """ Plots things in 2D for a single attentional condition :param values: [state dimension][time] :param conditions: conditions points by actual condition? vect of ints, one per 4 conds :param colormap: conditions to use per actual condition :param figure_size: matplotlib figure size, useful when embedding into ipython notebook :param alpha: alpha of the individual dots :return: reference to figure object """ figure = Figure(figsize = figure_size) axes = figure.add_subplot(111, aspect = 'equal') values_by_condition = sort_by_condition(values, conditions, 4) centroids = numpy.zeros([4, values.shape[0]]) for i in range(4): centroids[i, :] = numpy.mean(values_by_condition[i], 1) # QR decomposition can flip signs on axes # check and flip back if needed x_sign = 1 y_sign = 1 if centroids[3, 0] < centroids[0, 0]: x_sign = -1 if centroids[3, 1] < centroids[0, 1]: y_sign = -1 axes.scatter(x_sign * values_by_condition[0][0, :], y_sign * values_by_condition[0][1, :], alpha = alpha, marker = 'o', c = '#ff5100', edgecolor = 'none', lw = 0) axes.scatter(x_sign * values_by_condition[1][0, :], y_sign * values_by_condition[1][1, :], alpha = alpha, marker = 'o', c = '#91d90d', edgecolor = 'none', lw = 0) axes.scatter(x_sign * values_by_condition[2][0, :], y_sign * values_by_condition[2][1, :], alpha = alpha, marker = 'o', c = '#0084ff', edgecolor = 'none', lw = 0) axes.scatter(x_sign * values_by_condition[3][0, :], y_sign * values_by_condition[3][1, :], alpha = alpha, marker = 'o', c = '#a200ff', edgecolor = 'none', lw = 0) for i in range(4): colors = {0: '#ff5100', 1: '#91d90d', 2: '#0084ff', 3: '#a200ff'} axes.scatter(x_sign * centroids[i, 0], y_sign * centroids[i, 1], alpha = 1, c = "#000000", marker = '+', s = 700, linewidth = 8) axes.scatter(x_sign * centroids[i, 0], y_sign * centroids[i, 1], alpha = 1, c = colors[i], marker = '+', s = 500, linewidth = 4) axes.set_xlabel('Human presence') axes.set_ylabel('Vehicle presence') axes.title.set_fontsize(16) axes.xaxis.label.set_fontsize(20) axes.yaxis.label.set_fontsize(20) axes.set_xlim([-4, 4]) axes.set_ylim([-4, 4]) for item in (axes.get_xticklabels() + axes.get_yticklabels()): item.set_fontsize(12) return figure
def showPlot3D(self,x,y,z): fig = Figure(dpi=100, frameon=False) a = fig.add_subplot(111, projection='3d') a.plot_surface(x,y,z, rstride=4, cstride=4, linewidth=0, color='b') canvas = FigureCanvas(fig) #Makes possible to rotate the plot a.mouse_init() self.update_canvas(canvas)
def showPlot3D(self, x, y, z): fig = Figure(dpi=100, frameon=False) a = fig.add_subplot(111, projection='3d') a.plot_surface(x, y, z, rstride=4, cstride=4, linewidth=0, color='b') canvas = FigureCanvas(fig) #Makes possible to rotate the plot a.mouse_init() self.update_canvas(canvas)
def showPlot2D(self, x, y, squared): if squared: asp = 'equal' else: asp = 'auto' fig = Figure(dpi=100, frameon=False) a = fig.add_subplot(111, aspect=asp) a.plot(x, y) canvas = FigureCanvas(fig) self.update_canvas(canvas)
def showPlot2D(self, x, y, squared): if squared: asp = 'equal' else: asp = 'auto' fig = Figure(dpi=100, frameon=False) a = fig.add_subplot(111, aspect=asp) a.plot(x,y) canvas = FigureCanvas(fig) self.update_canvas(canvas)
def make_plots_mpl(data, timeDeltaDict=None): times = [d[0] for d in data] nTplot = 1 + ((len(supply.temperatures) - 1) // 2) nax = len(aNames) + len(rNames) plotWidth = 3.6 plotHeight = 0.8 * (len(aNames) + len(rNames) + nTplot) minPlotHeight = len(supply.outPins) * 0.42 # inch plotHeight = max(plotHeight, minPlotHeight) dpi = 100 fig = Figure(figsize=(plotWidth * 1.6, plotHeight * 1.6), dpi=dpi) fig.set_facecolor(facecolor) if not isTest: canvas = FigureCanvasAgg(fig) # noqa kw = {} if versiontuple(mpl.__version__) >= versiontuple("2.0.0"): kw['facecolor'] = facecolor else: kw['axisbg'] = facecolor gs = gridspec.GridSpec(nax + 2, 1, height_ratios=[2 * nTplot] + [2 for i in range(nax)] + [1]) ax0 = fig.add_subplot(gs[0], **kw) axi = [fig.add_subplot(gs[i], sharex=ax0, **kw) for i in range(1, nax + 2)] for ax in [ax0] + axi: for spine in ['bottom', 'top', 'left', 'right']: ax.spines[spine].set_color(spinecolor) ax.xaxis.label.set_color(spinecolor) ax.yaxis.label.set_color(spinecolor) ax.tick_params(axis='x', colors=spinecolor) ax.tick_params(axis='y', colors=spinecolor) tp = dict(bottom=True, top=True, labelbottom=False) for ax in axi: ax.tick_params(axis="x", labeltop=False, **tp) ax0.tick_params(axis="x", labeltop=True, **tp) axi[-1].set_xlabel(u'date time', fontsize=13) axt = ax0 # temperature axes axs = axi[-1] # state axes axd = axi[:-1] # other sensor axes axNames = ['temperature'] + aNames + rNames + ['ios'] axUnits = [supply.temperatureUnit] + aUnits + rUnits + [''] for it, color in enumerate(tColors): datat = [d[it + 2] for d in data] lo = supply.temperatureOutlierLimits times0 = [t for t, d in zip(times, datat) if lo[0] < d < lo[1]] datat0 = [d for t, d in zip(times, datat) if lo[0] < d < lo[1]] # td = [(t, d) for t, d in zip(times, datat) if lo[0] < d < lo[1]] # times0, datat0 = zip(*td) axt.plot(times0, datat0, 'o-', lw=lw, color=color, alpha=alpha, ms=ms, markeredgecolor=color) for it, (ax, color) in enumerate(zip(axd, aColors + rColors)): datao = [d[it + 2 + len(tNames)] for d in data] ax.plot(times, datao, 'o-', lw=lw, color=color, alpha=alpha, ms=ms, markeredgecolor=color) ioNames = supply.plotPins onVals = [1.15 - i * 0.1 for i in range(len(ioNames))] ioColors = [] ioDisplayed = [] ioState = [] for name, onVal in zip(ioNames, onVals): if name in ioKeys: ioDisplayed.append(name) iio = ioKeys.index(name) dataIO = [onVal if d[1] & 2**iio else 1 - onVal for d in data] ioState.append(1 if dataIO[-1] > 0.5 else 0) color = pColors[iio] ioColors.append(color) axs.plot(times, dataIO, 'o-', lw=lw, color=color, alpha=alpha, ms=ms, markeredgecolor=color) if timeDeltaDict is not None: now = datetime.datetime.now() ax0.set_xlim([now - datetime.timedelta(**timeDeltaDict), now]) if yrange == "user": axt.set_ylim(supply.temperatureDisplayLimits) elif yrange == "auto": axt.set_ylim([None, None]) axs.set_ylim(-0.2, 1.2) axs.set_yticks([0, 1]) axs.set_yticklabels(['off', 'on']) for ax, lims in zip(axd, aLimits + rLimits): if yrange == "user": ax.set_ylim(*lims) elif yrange == "auto": ax.set_ylim([None, None]) # fig.autofmt_xdate() fig.canvas.draw() if versiontuple(mpl.__version__) < versiontuple("2.0.0"): tlabels = [item.get_text() for item in axi[-1].get_xticklabels()] posdot = tlabels[0].find('.0') if posdot > 0: tlabels = [item[:posdot] for item in tlabels] axi[-1].set_xticklabels(tlabels) fig.subplots_adjust(left=0.09, bottom=0.04, right=0.98, top=0.94, hspace=0.04) for ax, name, unit in zip([ax0] + axi, axNames, axUnits): label = u'{0} ({1})'.format(name, unit) if unit else name if name == 'ios': color_text(0.01, 0.65, ioDisplayed, ioState, ioColors, ax, va='top', fontsize=13, alpha=0.7) else: ax.text(0.01, 0.95, label, transform=ax.transAxes, va='top', fontsize=13, color=spinecolor, alpha=0.7) if timeDeltaDict is not None: ax0.annotate('', xy=(1, 1), xycoords='axes fraction', xytext=(1, 1.25), textcoords='axes fraction', arrowprops=dict(color=spinecolor, width=1, headwidth=7, headlength=10)) fig.text(0.01, 0.01, '{0} time points'.format(len(times)), color=spinecolor, alpha=0.25) xticklabels = ax0.get_xticklabels() ax0.set_xticklabels(xticklabels[:-1], rotation=20, ha="left") if isTest: fig.show() else: buf = BytesIO() fig.savefig(buf, format="png", facecolor=facecolor) return (base64.b64encode(buf.getvalue()).decode("ascii"), plotHeight * dpi)
def plot_subimage(fig: plt.figure, hdu: Union[str, fits.HDUList], ra: float, dec: float, frame: Union[int, float], world_frame: bool = False, title: str = None, n: int = 1, n_x: int = 1, n_y: int = 1, cmap: str = 'viridis', show_cbar: bool = False, stretch: str = 'sqrt', vmin: float = None, vmax: float = None, show_grid: bool = False, ticks: int = None, interval: str = 'minmax', show_coords: bool = True, ylabel: str = None, font_size: int = 12, reverse_y=False): """ :param fig: :param hdu: :param ra: :param dec: :param frame: in pixels, or in arcsecs (?) if world_frame is True. :param world_frame: :param title: :param n: :param n_x: :param n_y: :param cmap: :param show_cbar: :param stretch: :param vmin: :param vmax: :param show_grid: :param ticks: :param interval: :param show_coords: :param ylabel: :param font_size: :param reverse_y: :return: """ print(hdu) hdu, path = ff.path_or_hdu(hdu=hdu) print(hdu[0].data.shape) hdu_cut = ff.trim_frame_point(hdu=hdu, ra=ra, dec=dec, frame=frame, world_frame=world_frame) wcs_cut = wcs.WCS(header=hdu_cut[0].header) print(n_y, n_x, n) if show_coords: plot = fig.add_subplot(n_y, n_x, n, projection=wcs_cut) if ticks is not None: lat = plot.coords[0] lat.set_ticks(number=ticks) else: plot = fig.add_subplot(n_y, n_x, n) frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.set_yticks([]) # frame1.axes.get_yaxis().set_visible(False) if show_grid: plt.grid(color='black', ls='dashed') if type(vmin) is str: if vmin == 'median_full': vmin = np.nanmedian(hdu[0].data) elif vmin == 'median_cut': vmin = np.nanmedian(hdu_cut[0].data) else: raise ValueError('Unrecognised vmin string argument.') if interval == 'minmax': interval = MinMaxInterval() elif interval == 'zscale': interval = ZScaleInterval() else: raise ValueError('Interval not recognised.') print(hdu_cut[0].data.shape) if stretch == 'log': norm = ImageNormalize(hdu_cut[0].data, interval=interval, stretch=LogStretch(), vmin=vmin, vmax=vmax) elif stretch == 'sqrt': norm = ImageNormalize(hdu_cut[0].data, interval=interval, stretch=SqrtStretch(), vmin=vmin, vmax=vmax) else: raise ValueError('Stretch not recognised.') plot.title.set_text(title) plot.title.set_size(font_size) print(ylabel) if ylabel is not None: plot.set_ylabel(ylabel, size=12) im = plt.imshow(hdu_cut[0].data, norm=norm, cmap=cmap) if reverse_y: plot.invert_yaxis() c_ticks = np.linspace(norm.vmin, norm.vmax, 5, endpoint=True) if show_cbar: cbar = plt.colorbar(im) # ticks=c_ticks) return plot, hdu_cut
class Plotter(object): """This class is responsible for managing the layout of a figure, and also implementing the plotting commands for simple graphs, hopefully making it even easier than using matplotlib directly.""" ACTIVE_AXES = "active axes" # A constant used as default target of the plotting methods default_hspace = 1.0 / 8.0 default_vspace = 1.0 / 8.0 default_hpadding = 0.05 default_vpadding = 0.05 def __init__(self, title=None, rows=0, cols=0, hspace=None, vspace=None, hpadding=None, vpadding=None): if hspace is None: hspace = self.default_hspace if vspace is None: vspace = self.default_vspace if hpadding is None: hpadding = self.default_hpadding if vpadding is None: vpadding = self.default_vpadding self.figure = Figure() self.figure.plotter = self self.title = self.figure.suptitle("" if title is None else title) self.rows = rows self.cols = cols self.hspace = hspace self.vspace = vspace self.hpadding = hpadding self.vpadding = vpadding self.active_axes = None def update(self): """Show changes in the figure.""" self.figure.show() def save(self, *args, **kwargs): """Save the figure. Please refer to matplotlib's documentation.""" self.figure.savefig(*args, **kwargs) def close(self): close_figure(self.figure) def layout(self, rows=None, cols=None, update=True): """Change the layout of the figure, i.e. the number of rows and columns.""" n = len(self.figure.axes) if rows is None and cols is None: rows = int(round(sqrt(n))) cols = rows + (1 if n > rows**2 else 0) elif rows is None: rows = int(ceil(float(n) / cols)) elif cols is None: cols = int(ceil(float(n) / rows)) elif rows * cols < n: raise ValueError("insufficient cells") self.rows = rows self.cols = cols self.redraw(update) def spacing(self, hspace=None, vspace=None, update=True): """Change the spacing between axes in the figure.""" if hspace is None and vspace is None: return if hspace is not None: if not 0.0 <= hspace <= 1.0: raise ValueError("illegal horizontal spacing (must be in [0, 1])") self.hspace = hspace if vspace is not None: if not 0.0 <= vspace <= 1.0: raise ValueError("illegal vertical spacing (must be in [0, 1])") self.vspace = vspace self.redraw(update) def padding(self, hpadding=None, vpadding=None, update=True): if hpadding is None and vpadding is None: return if hpadding is not None: if not 0.0 <= hpadding < 0.5: raise ValueError("illegal horizontal spacing (must be in [0, 0.5))") self.hpadding = hpadding if vpadding is not None: if not 0.0 <= vpadding < 0.5: raise ValueError("illegal vertical spacing (must be in [0, 0.5))") self.vpadding = vpadding self.redraw(update) def redraw(self, update=True): """Redraw the axes in the figure. This is usually used only after changes to layout or spacing in the figure.""" total_width = self.cols * (1.0 + 2.0*self.hspace) total_height = self.rows * (1.0 + 2.0*self.vspace) plot_width = (1.0 - 2.0*self.hpadding) / total_width plot_height = (1.0 - 2.0*self.vpadding) / total_height space_width = self.hspace * plot_width space_height = self.vspace * plot_height # Reposition the axes according to the new dimensions n = len(self.figure.axes) index = 0 y_pos = 1.0 - plot_height - space_height - self.vpadding for _ in xrange(self.rows): x_pos = space_width + self.hpadding for x in xrange(self.cols): axes = self.figure.axes[index] axes.set_position([x_pos, y_pos, plot_width, plot_height]) index += 1 x_pos += plot_width + 2 * space_width if index == n: break y_pos -= plot_height + 2 * space_height if index == n: break if update: self.update() def set_title(self, title, update=True): """Set the title of the figure (not axes!).""" self.title.set_text("" if title is None else title) if update: self.update() def set_size(self, width, height, inches=False): """Sets the size of the image in pixels or inches (if 'inches' is true).""" dpi = self.figure.get_dpi() if not inches: width /= dpi height /= dpi self.figure.set_size_inches(width, height, forward=True) def add_axes(self, make_active=True): """Add a new axes to the figure and place it in the right position. If the current grid of graphs cannot accommodate the new axes, the figure layout is recalculated.""" axes = self.figure.add_subplot(1, 1, 1, label=str(len(self.figure.axes))) if make_active: self.active_axes = axes rows, cols = None, None if self.rows * self.cols >= len(self.figure.axes): rows, cols = self.rows, self.cols self.layout(rows, cols, update=False) return axes def config_axes(self, axes=ACTIVE_AXES, title=None, xlabel=None, ylabel=None, xlimit=None, ylimit=None, legend=None, grid=None, update=True): """A many-in-one configuration method. Saves a few boring lines of matplotlib code.""" axes = self.get_axes(axes) if title is not None: axes.set_title(title) if xlabel is not None: axes.set_xlabel(xlabel) if ylabel is not None: axes.set_ylabel(ylabel) if xlimit is not None: axes.set_xlim(xlimit) if ylimit is not None: axes.set_ylim(ylimit) if legend is not None: axes.legend(loc=legend) if grid is not None: axes.grid(bool(grid)) if update: self.update() def get_axes(self, axes=ACTIVE_AXES): """This method can be used to check if a given axes belongs to the figure, retrieve the currently active axes, or add new axes to the figure.""" if axes is Plotter.ACTIVE_AXES: if self.active_axes is None: self.add_axes(make_active=True) return self.active_axes if axes is None: return self.add_axes(make_active=False) if axes in self.figure.axes: return axes raise Exception("axes does not belong to this Plotter") def set_active(self, axes): """Set the plotter's active axes, i.e. the default target of plotting commands.""" if axes not in self.figure.axes: raise Exception("axes does not belong to this Plotter") self.active_axes = axes # ------------------------------------------------- # Plotting methods @contextmanager def plotting_on(self, axes, update=True): yield self.get_axes(axes) if update: self.update() def legend(self, axes=ACTIVE_AXES, update=True, **kwargs): """Add a legend to the given axes.""" with self.plotting_on(axes, update) as axes: axes.legend(**kwargs) return axes def pie_chart(self, values, freqs, axes=ACTIVE_AXES, update=True, **kwargs): with self.plotting_on(axes, update) as axes: axes.pie(freqs, labels=values, **kwargs) return axes def bar_chart(self, values, freqs, axes=ACTIVE_AXES, update=True, **kwargs): with self.plotting_on(axes, update) as axes: data = self.__prepare_bar_chart(values, freqs) axes.xaxis.set_ticks(data.xtick_locs) axes.xaxis.set_ticklabels(data.xtick_labels) axes.bar(data.left, data.height, width=data.bar_width, **kwargs) return axes def pareto_chart(self, values, freqs, axes=ACTIVE_AXES, update=True, **kwargs): with self.plotting_on(axes, update) as axes: data = self.__prepare_pareto_chart(values, freqs) axes.xaxis.set_ticks(data.xtick_locs) axes.xaxis.set_ticklabels(data.xtick_labels) axes.bar(data.left, data.height, width=data.bar_width, **kwargs) axes.plot(data.xs, data.ys, "r-", label="Cumulative frequency") return axes def histogram(self, values, freqs=None, bins=10, axes=ACTIVE_AXES, update=True, **kwargs): with self.plotting_on(axes, update) as axes: data = self.__prepare_histogram(values, freqs, bins) axes.bar(data.left, data.height, width=data.bar_width, **kwargs) return axes def box_plot(self, values, axes=ACTIVE_AXES, update=True, **kwargs): with self.plotting_on(axes, update) as axes: axes.boxplot(values, **kwargs) return axes def run_chart(self, times, values, numeric=True, axes=ACTIVE_AXES, update=True, **kwargs): """A run chart of a time series.""" with self.plotting_on(axes, update) as axes: data = self.__prepare_run_chart(list(times), list(values), numeric) if not numeric: axes.yaxis.set_ticks(data.ytick_locs) axes.yaxis.set_ticklabels(data.ytick_labels) axes.plot(data.xs, data.ys, **kwargs) return axes def line_plot(self, xs, ys, axes=ACTIVE_AXES, update=True, **kwargs): """A simple 2D line plot.""" with self.plotting_on(axes, update) as axes: axes.plot(list(xs), list(ys), **kwargs) return axes def function_plot(self, function, start=0, stop=1.0, observations=100, axes=ACTIVE_AXES, update=True, **kwargs): """Make a quick plot of a function on a given interval.""" xs, ys = [], [] dx = float(stop - start) / (observations - 1) x = start for i in xrange(observations): xs.append(x) ys.append(function(x)) x += dx return self.line_plot(xs, ys, axes=axes, update=True, **kwargs) # ------------------------------------------------- # Preparation of data for plotting def __prepare_bar_chart(self, values, freqs, bar_width=1.0, bar_space=0.5): left = [(bar_width + bar_space) * x for x in xrange(len(freqs))] xtick_locs = [l + bar_width / 2.0 for l in left] return Namespace(left=left, height=freqs, bar_width=bar_width, xtick_locs=xtick_locs, xtick_labels=values) def __prepare_pareto_chart(self, values, freqs, bar_width=1.0): total = float(sum(freqs)) items = sorted([(f / total, v) for f, v in zip(freqs, values)], reverse=True) height = [] left = [bar_width * x for x in xrange(len(items))] xtick_locs = [l + bar_width / 2.0 for l in left] xtick_labels = [] xs = [bar_width * x for x in xrange(len(items) + 1)] ys = [0.0] for f, v in items: height.append(f) xtick_labels.append(v) ys.append(ys[-1] + f) return Namespace(left=left, height=height, xs=xs, ys=ys, bar_width=bar_width, xtick_locs=xtick_locs, xtick_labels=xtick_labels) def __prepare_histogram(self, values, freqs, bins): if freqs is None: freqs = [1.0] * len(values) items = sorted(zip(values, freqs)) minimum = items[ 0][0] maximum = items[-1][0] total_freq = sum(freqs) bin_span = float(maximum - minimum) / bins bin_end = [minimum + bin_span * (x + 1) for x in xrange(bins)] bin_end[-1] = maximum bin_freq = [0.0] * bins cur_bin = 0 for v, f in items: while v > bin_end[cur_bin]: cur_bin += 1 bin_freq[cur_bin] += f / (bin_span * total_freq) return Namespace(left=[end - bin_span for end in bin_end], height=bin_freq, bar_width=bin_span) def __prepare_run_chart(self, times, values, numeric): xs = [] ys = [] prev_y = values[0] for y, t in zip(values, times): xs.extend((t, t)) ys.extend((prev_y, y)) prev_y = y ytick_locs = None ytick_labels = None if not numeric: # map objects to integer y values if the tseries is not numeric y_set = sorted(set(ys)) y_mapping = dict(zip(y_set, xrange(len(y_set)))) ys = [y_mapping[y] for y in ys] # prepare y ticks explaining the translation from objects to integers yticks = sorted((i, v) for v, i in y_mapping.iteritems()) ytick_locs = [i for i, _ in yticks] ytick_labels = [v for _, v in yticks] return Namespace(xs=xs, ys=ys, ytick_locs=ytick_locs, ytick_labels=ytick_labels)
def run(filename, outfilename, plot_dir=None, tell_file='data/TelluricModel.dat', blaze_corrected=False): # prepare figures and axes if plot_dir is None: #interactive mode from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12, 6)) ax1 = fig1.add_subplot(111) orders, order_numbers = read_data(filename, debug=False) tell_model = get_tell_model(tell_file) kwargs = {} if blaze_corrected: kwargs["N"] = None else: print 'Roughly removing blaze function for {}'.format(filename) filtered_orders, original_pixels = remove_blaze(orders, tell_model, **kwargs) corrected_orders = [] print 'Optimizing wavelength for order ', for o_n, order in zip(order_numbers, filtered_orders): l_orig = plot_spec(ax1, order[0], order[1], 'k-', alpha=0.4) l_model = plot_spec(ax1, order[0], tell_model(order[0]), 'r-', alpha=0.6) # Use the wavelength fit function to fit the wavelength. print ' {}'.format(o_n), sys.stdout.flush() new_order = optimize_wavelength(order, tell_model, fitorder=2) l_modified = plot_spec(ax1, new_order[0], new_order[1], 'g-', alpha=0.4) corrected_orders.append(new_order) # do not trt to plot if number valid points is less than 2 if len(order[0]) < 2: continue ax1.legend([l_model, l_orig, l_modified], ["Telluric Model", "Original Spec.", "Modified Spec."]) if plot_dir is not None: ax1.set_title('Individual order fit : {}'.format(o_n)) ax1.set_xlim(order[0][0], order[0][-1]) ax1.set_ylim(-0.05, 1.15) figout = os.path.join(plot_dir, 'individual_order') postfix = "_%03d" % (o_n, ) from igrins.libs.qa_helper import fig_to_png fig_to_png(figout, fig1, postfix=postfix) # fig1.savefig('{}{}-individual_order{}.png'.format(plot_dir, filename.split('/')[-1], i+1)) ax1.cla() print original_orders = [o.copy() for o in orders] # Now, fit the entire chip to a surface fig3d = Figure() import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") ax3d = fig3d.add_subplot(111, projection='3d') final_orders = fit_chip(original_orders, corrected_orders, original_pixels, order_numbers, tell_model, ax3d=ax3d) if plot_dir is not None: figout = os.path.join(plot_dir, 'fullchip_fit') from igrins.libs.qa_helper import fig_to_png fig_to_png(figout, fig3d) fig3 = Figure(figsize=(12, 6)) ax3 = fig3.add_subplot(111) # Filter again just for plotting final_filtered, _ = remove_blaze(final_orders, tell_model, **kwargs) for o_n, order in zip(order_numbers, final_filtered): l_final = plot_spec(ax3, order[0], order[1], 'k-', alpha=0.4) l_model = plot_spec(ax3, order[0], tell_model(order[0]), 'r-', alpha=0.6) if len(order[0]) < 2: continue ax3.legend([l_model, l_final], ["Telluric Model", "Final Spec."]) if plot_dir is not None: ax3.set_title('Final wavelength solution : {}'.format(o_n)) ax3.set_xlim(order[0][0], order[0][-1]) ax3.set_ylim(-0.05, 1.15) figout = os.path.join(plot_dir, 'final_order') postfix = "_%03d" % (o_n, ) from igrins.libs.qa_helper import fig_to_png fig_to_png(figout, fig3, postfix=postfix) # fig1.savefig('{}{}-individual_order{}.png'.format(plot_dir, filename.split('/')[-1], i+1)) ax3.cla() if plot_dir is None: ax3.set_title('Final wavelength solution') import matplotlib.pyplot as plt with warnings.catch_warnings(): warnings.simplefilter("ignore") plt.show() # Output wave_arr = np.array([o[0] for o in final_orders]) hdulist = fits.PrimaryHDU(wave_arr) hdulist.writeto(outfilename, clobber=True)
def process_abba_band(recipe, utdate, refdate, band, obsids, frametypes, config, do_interactive_figure=False, threshold_a0v=0.1, objname="", multiply_model_a0v=False, html_output=False): from libs.products import ProductDB, PipelineStorage if recipe == "A0V_AB": FIX_TELLURIC = False elif recipe == "STELLAR_AB": FIX_TELLURIC = True elif recipe == "EXTENDED_AB": FIX_TELLURIC = True elif recipe == "EXTENDED_ONOFF": FIX_TELLURIC = True else: raise ValueError("Unsupported Recipe : %s" % recipe) if 1: igr_path = IGRINSPath(config, utdate) igr_storage = PipelineStorage(igr_path) obj_filenames = igr_path.get_filenames(band, obsids) master_obsid = obsids[0] tgt_basename = os.path.splitext(os.path.basename(obj_filenames[0]))[0] db = {} basenames = {} db_types = ["flat_off", "flat_on", "thar", "sky"] for db_type in db_types: db_name = igr_path.get_section_filename_base( "PRIMARY_CALIB_PATH", "%s.db" % db_type, ) db[db_type] = ProductDB(db_name) # db on output path db_types = ["a0v"] for db_type in db_types: db_name = igr_path.get_section_filename_base( "OUTDATA_PATH", "%s.db" % db_type, ) db[db_type] = ProductDB(db_name) # to get basenames db_types = ["flat_off", "flat_on", "thar", "sky"] if FIX_TELLURIC: db_types.append("a0v") for db_type in db_types: basenames[db_type] = db[db_type].query(band, master_obsid) if 1: # make aperture from libs.storage_descriptions import SKY_WVLSOL_JSON_DESC sky_basename = db["sky"].query(band, master_obsid) wvlsol_products = igr_storage.load([SKY_WVLSOL_JSON_DESC], sky_basename)[SKY_WVLSOL_JSON_DESC] orders_w_solutions = wvlsol_products["orders"] wvl_solutions = map(np.array, wvlsol_products["wvl_sol"]) # prepare i1i2_list from libs.storage_descriptions import ORDER_FLAT_JSON_DESC prod = igr_storage.load([ORDER_FLAT_JSON_DESC], basenames["flat_on"])[ORDER_FLAT_JSON_DESC] new_orders = prod["orders"] i1i2_list_ = prod["i1i2_list"] order_indices = [] for o in orders_w_solutions: o_new_ind = np.searchsorted(new_orders, o) order_indices.append(o_new_ind) i1i2_list = get_fixed_i1i2_list(order_indices, i1i2_list_) from libs.storage_descriptions import (SPEC_FITS_DESC, SN_FITS_DESC) if 1: # load target spectrum tgt_spec_ = igr_storage.load([SPEC_FITS_DESC], tgt_basename)[SPEC_FITS_DESC] tgt_spec = list(tgt_spec_.data) tgt_sn_ = igr_storage.load([SN_FITS_DESC], tgt_basename)[SN_FITS_DESC] tgt_sn = list(tgt_sn_.data) fig_list = [] # telluric if 1: #FIX_TELLURIC: A0V_basename = db["a0v"].query(band, master_obsid) from libs.storage_descriptions import SPEC_FITS_FLATTENED_DESC telluric_cor_ = igr_storage.load( [SPEC_FITS_FLATTENED_DESC], A0V_basename)[SPEC_FITS_FLATTENED_DESC] #A0V_path = ProductPath(igr_path, A0V_basename) #fn = A0V_path.get_secondary_path("spec_flattened.fits") telluric_cor = list(telluric_cor_.data) a0v_spec_ = igr_storage.load([SPEC_FITS_DESC], A0V_basename)[SPEC_FITS_DESC] a0v_spec = list(a0v_spec_.data) if 1: if do_interactive_figure: from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12, 6)) fig_list.append(fig1) ax1a = fig1.add_subplot(211) ax1b = fig1.add_subplot(212, sharex=ax1a) for wvl, s, sn in zip(wvl_solutions, tgt_spec, tgt_sn): #s[s<0] = np.nan #sn[sn<0] = np.nan ax1a.plot(wvl, s) ax1b.plot(wvl, sn) ax1a.set_ylabel("Counts [DN]") ax1b.set_ylabel("S/N per Res. Element") ax1b.set_xlabel("Wavelength [um]") ax1a.set_title(objname) if FIX_TELLURIC: fig2 = Figure(figsize=(12, 6)) fig_list.append(fig2) ax2a = fig2.add_subplot(211) ax2b = fig2.add_subplot(212, sharex=ax2a) #from libs.stddev_filter import window_stdev tgt_spec_cor = [] #for s, t in zip(s_list, telluric_cor): for s, t, t2 in zip(tgt_spec, a0v_spec, telluric_cor): st = s / t #print np.percentile(t[np.isfinite(t)], 95), threshold_a0v t0 = np.percentile(t[np.isfinite(t)], 95) * threshold_a0v st[t < t0] = np.nan st[t2 < threshold_a0v] = np.nan tgt_spec_cor.append(st) if multiply_model_a0v: # multiply by A0V model from libs.a0v_spec import A0VSpec a0v_model = A0VSpec() a0v_interp1d = a0v_model.get_flux_interp1d(1.3, 2.5, flatten=True, smooth_pixel=32) for wvl, s in zip(wvl_solutions, tgt_spec_cor): aa = a0v_interp1d(wvl) s *= aa for wvl, s, t in zip(wvl_solutions, tgt_spec_cor, telluric_cor): ax2a.plot(wvl, t, "0.8", zorder=0.5) ax2b.plot(wvl, s, zorder=0.5) s_max_list = [] s_min_list = [] for s in tgt_spec_cor[3:-3]: s_max_list.append(np.nanmax(s)) s_min_list.append(np.nanmin(s)) s_max = np.max(s_max_list) s_min = np.min(s_min_list) ds_pad = 0.05 * (s_max - s_min) ax2a.set_ylabel("A0V flattened") ax2a.set_ylim(-0.05, 1.1) ax2b.set_ylabel("Target / A0V") ax2b.set_xlabel("Wavelength [um]") ax2b.set_ylim(s_min - ds_pad, s_max + ds_pad) ax2a.set_title(objname) # save figures if fig_list: for fig in fig_list: fig.tight_layout() figout = igr_path.get_section_filename_base("QA_PATH", "spec_" + tgt_basename, "spec_" + tgt_basename) #figout = obj_path.get_secondary_path("spec", "spec_dir") from libs.qa_helper import figlist_to_pngs figlist_to_pngs(figout, fig_list) # save html if html_output: dirname = config.get_value('HTML_PATH', utdate) objroot = "%04d" % (master_obsid, ) html_save(utdate, dirname, objroot, band, orders_w_solutions, wvl_solutions, tgt_spec, tgt_sn, i1i2_list) if FIX_TELLURIC: objroot = "%04dA0V" % (master_obsid, ) html_save(utdate, dirname, objroot, band, orders_w_solutions, wvl_solutions, telluric_cor, tgt_spec_cor, i1i2_list, spec_js_name="jj_a0v.js") if do_interactive_figure: import matplotlib.pyplot as plt plt.show()
def run(filename, outfilename, plot_dir=None, tell_file='data/TelluricModel.dat', blaze_corrected=False): # prepare figures and axes if plot_dir is None: #interactive mode from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12,6)) ax1 = fig1.add_subplot(111) orders, order_numbers = read_data(filename, debug=False) tell_model = get_tell_model(tell_file) kwargs = {} if blaze_corrected: kwargs["N"] = None else: print 'Roughly removing blaze function for {}'.format(filename) filtered_orders, original_pixels = remove_blaze(orders, tell_model, **kwargs) corrected_orders = [] print 'Optimizing wavelength for order ', for o_n, order in zip(order_numbers, filtered_orders): l_orig = plot_spec(ax1, order[0], order[1], 'k-', alpha=0.4) l_model = plot_spec(ax1, order[0], tell_model(order[0]), 'r-', alpha=0.6) # Use the wavelength fit function to fit the wavelength. print ' {}'.format(o_n), sys.stdout.flush() new_order = optimize_wavelength(order, tell_model, fitorder=2) l_modified = plot_spec(ax1, new_order[0], new_order[1], 'g-', alpha=0.4) corrected_orders.append(new_order) # do not trt to plot if number valid points is less than 2 if len(order[0]) < 2: continue ax1.legend([l_model, l_orig, l_modified], ["Telluric Model", "Original Spec.", "Modified Spec."]) if plot_dir is not None: ax1.set_title('Individual order fit : {}'.format(o_n)) ax1.set_xlim(order[0][0], order[0][-1]) ax1.set_ylim(-0.05, 1.15) figout = os.path.join(plot_dir, 'individual_order') postfix="_%03d" % (o_n,) from libs.qa_helper import fig_to_png fig_to_png(figout, fig1, postfix=postfix) # fig1.savefig('{}{}-individual_order{}.png'.format(plot_dir, filename.split('/')[-1], i+1)) ax1.cla() print original_orders = [o.copy() for o in orders] # Now, fit the entire chip to a surface fig3d = Figure() import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") ax3d = fig3d.add_subplot(111, projection='3d') final_orders = fit_chip(original_orders, corrected_orders, original_pixels, order_numbers, tell_model, ax3d=ax3d) if plot_dir is not None: figout = os.path.join(plot_dir, 'fullchip_fit') from libs.qa_helper import fig_to_png fig_to_png(figout, fig3d) fig3 = Figure(figsize=(12,6)) ax3 = fig3.add_subplot(111) # Filter again just for plotting final_filtered, _ = remove_blaze(final_orders, tell_model, **kwargs) for o_n, order in zip(order_numbers, final_filtered): l_final = plot_spec(ax3, order[0], order[1], 'k-', alpha=0.4) l_model = plot_spec(ax3, order[0], tell_model(order[0]), 'r-', alpha=0.6) if len(order[0]) < 2: continue ax3.legend([l_model, l_final], ["Telluric Model", "Final Spec."]) if plot_dir is not None: ax3.set_title('Final wavelength solution : {}'.format(o_n)) ax3.set_xlim(order[0][0], order[0][-1]) ax3.set_ylim(-0.05, 1.15) figout = os.path.join(plot_dir, 'final_order') postfix="_%03d" % (o_n,) from libs.qa_helper import fig_to_png fig_to_png(figout, fig3, postfix=postfix) # fig1.savefig('{}{}-individual_order{}.png'.format(plot_dir, filename.split('/')[-1], i+1)) ax3.cla() if plot_dir is None: ax3.set_title('Final wavelength solution') import matplotlib.pyplot as plt with warnings.catch_warnings(): warnings.simplefilter("ignore") plt.show() # Output wave_arr = np.array([o[0] for o in final_orders]) hdulist = fits.PrimaryHDU(wave_arr) hdulist.writeto(outfilename, clobber=True)
def _create_ax(self, fig: plt.figure): """ Create the Axes onto which the plotted data will be drawn. """ return fig.add_subplot(1, 1, 1)
def process_abba_band(recipe, utdate, refdate, band, groupname, obsids, frametypes, config, do_interactive_figure=False, threshold_a0v=0.1, objname="", multiply_model_a0v=False, html_output=False, a0v_obsid=None, basename_postfix=None): target_type, nodding_type = recipe.split("_") if target_type in ["A0V"]: FIX_TELLURIC = False elif target_type in ["STELLAR", "EXTENDED"]: FIX_TELLURIC = True else: raise ValueError("Unknown recipe : %s" % recipe) from recipe_extract_base import RecipeExtractPR extractor = RecipeExtractPR(utdate, band, obsids, config) master_obsid = extractor.pr.master_obsid igr_path = extractor.pr.igr_path mastername = igr_path.get_basename(band, groupname) tgt = extractor.get_oned_spec_helper(mastername, basename_postfix=basename_postfix) orders_w_solutions = extractor.orders_w_solutions # if wavelengths list are sorted in increasing order of # wavelength, recerse the order list if tgt.um[0][0] < tgt.um[-1][0]: orders_w_solutions = orders_w_solutions[::-1] if FIX_TELLURIC: if (a0v_obsid is None) or (a0v_obsid == "1"): A0V_basename = extractor.basenames["a0v"] else: A0V_basename = "SDC%s_%s_%04d" % (band, utdate, int(a0v_obsid)) print A0V_basename a0v = extractor.get_oned_spec_helper(A0V_basename, basename_postfix=basename_postfix) tgt_spec_cor = get_tgt_spec_cor(tgt, a0v, threshold_a0v, multiply_model_a0v, config) # prepare i1i2_list i1i2_list = get_i1i2_list(extractor, orders_w_solutions) fig_list = [] if 1: if do_interactive_figure: from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12, 6)) fig_list.append(fig1) ax1a = fig1.add_subplot(211) ax1b = fig1.add_subplot(212, sharex=ax1a) for wvl, s, sn in zip(tgt.um, tgt.spec, tgt.sn): #s[s<0] = np.nan #sn[sn<0] = np.nan ax1a.plot(wvl, s) ax1b.plot(wvl, sn) ax1a.set_ylabel("Counts [DN]") ax1b.set_ylabel("S/N per Res. Element") ax1b.set_xlabel("Wavelength [um]") ax1a.set_title(objname) if FIX_TELLURIC: fig2 = Figure(figsize=(12, 6)) fig_list.append(fig2) ax2a = fig2.add_subplot(211) ax2b = fig2.add_subplot(212, sharex=ax2a) #from igrins.libs.stddev_filter import window_stdev for wvl, s, t in zip(tgt.um, tgt_spec_cor, a0v.flattened): ax2a.plot(wvl, t, "0.8", zorder=0.5) ax2b.plot(wvl, s, zorder=0.5) s_max_list = [] s_min_list = [] for s in tgt_spec_cor[3:-3]: s_max_list.append(np.nanmax(s)) s_min_list.append(np.nanmin(s)) s_max = np.max(s_max_list) s_min = np.min(s_min_list) ds_pad = 0.05 * (s_max - s_min) ax2a.set_ylabel("A0V flattened") ax2a.set_ylim(-0.05, 1.1) ax2b.set_ylabel("Target / A0V") ax2b.set_xlabel("Wavelength [um]") ax2b.set_ylim(s_min - ds_pad, s_max + ds_pad) ax2a.set_title(objname) # save figures if fig_list: for fig in fig_list: fig.tight_layout() # tgt_basename = extractor.pr.tgt_basename tgt_basename = mastername dirname = "spec_" + tgt_basename basename_postfix_s = basename_postfix if basename_postfix is not None else "" filename_prefix = "spec_" + tgt_basename + basename_postfix_s figout = igr_path.get_section_filename_base("QA_PATH", filename_prefix, dirname) #figout = obj_path.get_secondary_path("spec", "spec_dir") from igrins.libs.qa_helper import figlist_to_pngs figlist_to_pngs(figout, fig_list) # save html if html_output: if basename_postfix is not None: igr_log.warn( "For now, no html output is generated if basename-postfix option is used" ) else: dirname = config.get_value('HTML_PATH', utdate) from igrins.libs.path_info import get_zeropadded_groupname objroot = get_zeropadded_groupname(groupname) html_save(utdate, dirname, objroot, band, orders_w_solutions, tgt.um, tgt.spec, tgt.sn, i1i2_list) if FIX_TELLURIC: objroot = get_zeropadded_groupname(groupname) + "A0V" html_save(utdate, dirname, objroot, band, orders_w_solutions, tgt.um, a0v.flattened, tgt_spec_cor, i1i2_list, spec_js_name="jj_a0v.js") if do_interactive_figure: import matplotlib.pyplot as plt plt.show()
def process_abba_band(recipe, utdate, refdate, band, groupname, obsids, frametypes, config, do_interactive_figure=False, threshold_a0v=0.1, objname="", multiply_model_a0v=False, html_output=False, a0v_obsid=None, basename_postfix=None): target_type, nodding_type = recipe.split("_") if target_type in ["A0V"]: FIX_TELLURIC=False elif target_type in ["STELLAR", "EXTENDED"]: FIX_TELLURIC=True else: raise ValueError("Unknown recipe : %s" % recipe) from recipe_extract_base import RecipeExtractPR extractor = RecipeExtractPR(utdate, band, obsids, config) master_obsid = extractor.pr.master_obsid igr_path = extractor.pr.igr_path mastername = igr_path.get_basename(band, groupname) tgt = extractor.get_oned_spec_helper(mastername, basename_postfix=basename_postfix) orders_w_solutions = extractor.orders_w_solutions # if wavelengths list are sorted in increasing order of # wavelength, recerse the order list if tgt.um[0][0] < tgt.um[-1][0]: orders_w_solutions = orders_w_solutions[::-1] if FIX_TELLURIC: if (a0v_obsid is None) or (a0v_obsid == "1"): A0V_basename = extractor.basenames["a0v"] else: A0V_basename = "SDC%s_%s_%04d" % (band, utdate, int(a0v_obsid)) print A0V_basename a0v = extractor.get_oned_spec_helper(A0V_basename, basename_postfix=basename_postfix) tgt_spec_cor = get_tgt_spec_cor(tgt, a0v, threshold_a0v, multiply_model_a0v, config) # prepare i1i2_list i1i2_list = get_i1i2_list(extractor, orders_w_solutions) fig_list = [] if 1: if do_interactive_figure: from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12,6)) fig_list.append(fig1) ax1a = fig1.add_subplot(211) ax1b = fig1.add_subplot(212, sharex=ax1a) for wvl, s, sn in zip(tgt.um, tgt.spec, tgt.sn): #s[s<0] = np.nan #sn[sn<0] = np.nan ax1a.plot(wvl, s) ax1b.plot(wvl, sn) ax1a.set_ylabel("Counts [DN]") ax1b.set_ylabel("S/N per Res. Element") ax1b.set_xlabel("Wavelength [um]") ax1a.set_title(objname) if FIX_TELLURIC: fig2 = Figure(figsize=(12,6)) fig_list.append(fig2) ax2a = fig2.add_subplot(211) ax2b = fig2.add_subplot(212, sharex=ax2a) #from igrins.libs.stddev_filter import window_stdev for wvl, s, t in zip(tgt.um, tgt_spec_cor, a0v.flattened): ax2a.plot(wvl, t, "0.8", zorder=0.5) ax2b.plot(wvl, s, zorder=0.5) s_max_list = [] s_min_list = [] for s in tgt_spec_cor[3:-3]: s_max_list.append(np.nanmax(s)) s_min_list.append(np.nanmin(s)) s_max = np.max(s_max_list) s_min = np.min(s_min_list) ds_pad = 0.05 * (s_max - s_min) ax2a.set_ylabel("A0V flattened") ax2a.set_ylim(-0.05, 1.1) ax2b.set_ylabel("Target / A0V") ax2b.set_xlabel("Wavelength [um]") ax2b.set_ylim(s_min-ds_pad, s_max+ds_pad) ax2a.set_title(objname) # save figures if fig_list: for fig in fig_list: fig.tight_layout() # tgt_basename = extractor.pr.tgt_basename tgt_basename = mastername dirname = "spec_"+tgt_basename basename_postfix_s = basename_postfix if basename_postfix is not None else "" filename_prefix = "spec_" + tgt_basename + basename_postfix_s figout = igr_path.get_section_filename_base("QA_PATH", filename_prefix, dirname) #figout = obj_path.get_secondary_path("spec", "spec_dir") from igrins.libs.qa_helper import figlist_to_pngs figlist_to_pngs(figout, fig_list) # save html if html_output: if basename_postfix is not None: igr_log.warn("For now, no html output is generated if basename-postfix option is used") else: dirname = config.get_value('HTML_PATH', utdate) from igrins.libs.path_info import get_zeropadded_groupname objroot = get_zeropadded_groupname(groupname) html_save(utdate, dirname, objroot, band, orders_w_solutions, tgt.um, tgt.spec, tgt.sn, i1i2_list) if FIX_TELLURIC: objroot = get_zeropadded_groupname(groupname)+"A0V" html_save(utdate, dirname, objroot, band, orders_w_solutions, tgt.um, a0v.flattened, tgt_spec_cor, i1i2_list, spec_js_name="jj_a0v.js") if do_interactive_figure: import matplotlib.pyplot as plt plt.show()
def process_abba_band(recipe, utdate, refdate, band, obsids, frametypes, config, do_interactive_figure=False, threshold_a0v=0.1, objname="", multiply_model_a0v=False, html_output=False): from libs.products import ProductDB, PipelineStorage if recipe == "A0V_AB": FIX_TELLURIC=False elif recipe == "STELLAR_AB": FIX_TELLURIC=True elif recipe == "EXTENDED_AB": FIX_TELLURIC=True elif recipe == "EXTENDED_ONOFF": FIX_TELLURIC=True else: raise ValueError("Unsupported Recipe : %s" % recipe) if 1: igr_path = IGRINSPath(config, utdate) igr_storage = PipelineStorage(igr_path) obj_filenames = igr_path.get_filenames(band, obsids) master_obsid = obsids[0] tgt_basename = os.path.splitext(os.path.basename(obj_filenames[0]))[0] db = {} basenames = {} db_types = ["flat_off", "flat_on", "thar", "sky"] for db_type in db_types: db_name = igr_path.get_section_filename_base("PRIMARY_CALIB_PATH", "%s.db" % db_type, ) db[db_type] = ProductDB(db_name) # db on output path db_types = ["a0v"] for db_type in db_types: db_name = igr_path.get_section_filename_base("OUTDATA_PATH", "%s.db" % db_type, ) db[db_type] = ProductDB(db_name) # to get basenames db_types = ["flat_off", "flat_on", "thar", "sky"] if FIX_TELLURIC: db_types.append("a0v") for db_type in db_types: basenames[db_type] = db[db_type].query(band, master_obsid) if 1: # make aperture from libs.storage_descriptions import SKY_WVLSOL_JSON_DESC sky_basename = db["sky"].query(band, master_obsid) wvlsol_products = igr_storage.load([SKY_WVLSOL_JSON_DESC], sky_basename)[SKY_WVLSOL_JSON_DESC] orders_w_solutions = wvlsol_products["orders"] wvl_solutions = map(np.array, wvlsol_products["wvl_sol"]) # prepare i1i2_list from libs.storage_descriptions import ORDER_FLAT_JSON_DESC prod = igr_storage.load([ORDER_FLAT_JSON_DESC], basenames["flat_on"])[ORDER_FLAT_JSON_DESC] new_orders = prod["orders"] i1i2_list_ = prod["i1i2_list"] order_indices = [] for o in orders_w_solutions: o_new_ind = np.searchsorted(new_orders, o) order_indices.append(o_new_ind) i1i2_list = get_fixed_i1i2_list(order_indices, i1i2_list_) from libs.storage_descriptions import (SPEC_FITS_DESC, SN_FITS_DESC) if 1: # load target spectrum tgt_spec_ = igr_storage.load([SPEC_FITS_DESC], tgt_basename)[SPEC_FITS_DESC] tgt_spec = list(tgt_spec_.data) tgt_sn_ = igr_storage.load([SN_FITS_DESC], tgt_basename)[SN_FITS_DESC] tgt_sn = list(tgt_sn_.data) fig_list = [] # telluric if 1: #FIX_TELLURIC: A0V_basename = db["a0v"].query(band, master_obsid) from libs.storage_descriptions import SPEC_FITS_FLATTENED_DESC telluric_cor_ = igr_storage.load([SPEC_FITS_FLATTENED_DESC], A0V_basename)[SPEC_FITS_FLATTENED_DESC] #A0V_path = ProductPath(igr_path, A0V_basename) #fn = A0V_path.get_secondary_path("spec_flattened.fits") telluric_cor = list(telluric_cor_.data) a0v_spec_ = igr_storage.load([SPEC_FITS_DESC], A0V_basename)[SPEC_FITS_DESC] a0v_spec = list(a0v_spec_.data) if 1: if do_interactive_figure: from matplotlib.pyplot import figure as Figure else: from matplotlib.figure import Figure fig1 = Figure(figsize=(12,6)) fig_list.append(fig1) ax1a = fig1.add_subplot(211) ax1b = fig1.add_subplot(212, sharex=ax1a) for wvl, s, sn in zip(wvl_solutions, tgt_spec, tgt_sn): #s[s<0] = np.nan #sn[sn<0] = np.nan ax1a.plot(wvl, s) ax1b.plot(wvl, sn) ax1a.set_ylabel("Counts [DN]") ax1b.set_ylabel("S/N per Res. Element") ax1b.set_xlabel("Wavelength [um]") ax1a.set_title(objname) if FIX_TELLURIC: fig2 = Figure(figsize=(12,6)) fig_list.append(fig2) ax2a = fig2.add_subplot(211) ax2b = fig2.add_subplot(212, sharex=ax2a) #from libs.stddev_filter import window_stdev tgt_spec_cor = [] #for s, t in zip(s_list, telluric_cor): for s, t, t2 in zip(tgt_spec, a0v_spec, telluric_cor): st = s/t #print np.percentile(t[np.isfinite(t)], 95), threshold_a0v t0 = np.percentile(t[np.isfinite(t)], 95)*threshold_a0v st[t<t0] = np.nan st[t2 < threshold_a0v] = np.nan tgt_spec_cor.append(st) if multiply_model_a0v: # multiply by A0V model from libs.a0v_spec import A0VSpec a0v_model = A0VSpec() a0v_interp1d = a0v_model.get_flux_interp1d(1.3, 2.5, flatten=True, smooth_pixel=32) for wvl, s in zip(wvl_solutions, tgt_spec_cor): aa = a0v_interp1d(wvl) s *= aa for wvl, s, t in zip(wvl_solutions, tgt_spec_cor, telluric_cor): ax2a.plot(wvl, t, "0.8", zorder=0.5) ax2b.plot(wvl, s, zorder=0.5) s_max_list = [] s_min_list = [] for s in tgt_spec_cor[3:-3]: s_max_list.append(np.nanmax(s)) s_min_list.append(np.nanmin(s)) s_max = np.max(s_max_list) s_min = np.min(s_min_list) ds_pad = 0.05 * (s_max - s_min) ax2a.set_ylabel("A0V flattened") ax2a.set_ylim(-0.05, 1.1) ax2b.set_ylabel("Target / A0V") ax2b.set_xlabel("Wavelength [um]") ax2b.set_ylim(s_min-ds_pad, s_max+ds_pad) ax2a.set_title(objname) # save figures if fig_list: for fig in fig_list: fig.tight_layout() figout = igr_path.get_section_filename_base("QA_PATH", "spec_"+tgt_basename, "spec_"+tgt_basename) #figout = obj_path.get_secondary_path("spec", "spec_dir") from libs.qa_helper import figlist_to_pngs figlist_to_pngs(figout, fig_list) # save html if html_output: dirname = config.get_value('HTML_PATH', utdate) objroot = "%04d" % (master_obsid,) html_save(utdate, dirname, objroot, band, orders_w_solutions, wvl_solutions, tgt_spec, tgt_sn, i1i2_list) if FIX_TELLURIC: objroot = "%04dA0V" % (master_obsid,) html_save(utdate, dirname, objroot, band, orders_w_solutions, wvl_solutions, telluric_cor, tgt_spec_cor, i1i2_list, spec_js_name="jj_a0v.js") if do_interactive_figure: import matplotlib.pyplot as plt plt.show()