def plot(self, include, in_terminal=False): """ Plot relevant variables in a flexible manner. Parameters ---------- include : list The list of plots to include. in_terminal : bool, default=False If True, shows plots in terminal, giving an oldschool NASA vibe. If False, uses boring matplotlib. """ if in_terminal: import plotext as plt else: import matplotlib.pyplot as plt for plot in include: x_log = self.logs[plot['x']['logname']] x_data = x_log.get_data(plot['x']['ops']) y_log = self.logs[plot['y']['logname']] y_data = y_log.get_data(plot['y']['ops']) if in_terminal: plt.clear_plot() plt.nocolor() plt.figsize(75, 25) else: plt.figure() plt.plot(x_data, y_data) plt.show()
def make_figure_csq_curves(extractors, what): """ $ oq plot "csq_curves?agg_id=0&loss_type=structural&consequence=losses" -1 """ plt = import_plt() fig = plt.figure() got = {} # (calc_id, limit_state) -> curve for i, ex in enumerate(extractors): aw = ex.get(what) P, C = aw.shape if P < 2: raise RuntimeError('Not enough return periods: %d' % P) for c, csq in enumerate(aw.consequences): if csq in what: got[ex.calc_id, csq] = aw[:, c] oq = ex.oqparam periods = aw.return_period ax = fig.add_subplot(1, 1, 1) ax.set_xlabel('risk_inv_time=%dy' % oq.risk_investigation_time) ax.set_ylabel(csq) for ck, arr in got.items(): ax.loglog(periods, arr, '-', label=ck[0]) ax.loglog(periods, arr, '.') ax.grid(True) ax.legend() return plt
def make_figure_agg_curves(extractors, what): """ $ oq plot "agg_curves?kind=mean&loss_type=structural" -1 """ plt = import_plt() fig = plt.figure() got = {} # (calc_id, kind) -> curves for i, ex in enumerate(extractors): aw = ex.get(what + '&absolute=1') if isinstance(aw.json, numpy.ndarray): # from webui js = bytes(aw.json).decode('utf8') else: js = aw.json vars(aw).update(json.loads(js)) agg_curve = aw.array.squeeze() got[ex.calc_id, aw.kind[0]] = agg_curve oq = ex.oqparam periods = aw.return_period ax = fig.add_subplot(1, 1, 1) ax.set_xlabel('risk_inv_time=%dy' % oq.risk_investigation_time) ax.set_ylabel('loss') for ck, arr in got.items(): ax.loglog(periods, agg_curve, '-', label='%s_%s' % ck) ax.loglog(periods, agg_curve, '.') ax.grid(True) ax.legend() return plt
def make_figure_rups_by_mag_dist(extractors, what): """ $ oq plot "rups_by_mag_dist?" """ # NB: matplotlib is imported inside since it is a costly import plt = import_plt() from matplotlib import cm [ex] = extractors counts = ex.get(what) counts.array = numpy.log10(counts.array + 1) trts = ex.get('full_lt').trts mag_ticks = counts.mags[::-5] fig = plt.figure() cmap = cm.get_cmap('jet', 100) axes = [] vmax = counts.array.max() for trti, trt in enumerate(trts): ax = fig.add_subplot(len(trts), 1, trti + 1) axes.append(ax) ax.set_xticks(mag_ticks) ax.set_xlabel('Mag') dist_ticks = counts.dist_bins[trt][::10] ax.set_yticks(dist_ticks) ax.set_ylabel(trt) extent = mag_ticks[0], mag_ticks[-1], dist_ticks[0], dist_ticks[-1] im = ax.imshow(counts[:, :, trti], cmap=cmap, extent=extent, aspect='auto', vmin=0, vmax=vmax) fig.colorbar(im, ax=axes) return plt
def make_figure_hcurves(extractors, what): """ $ oq plot "hcurves?kind=mean&imt=PGA&site_id=0" """ plt = import_plt() fig = plt.figure() got = {} # (calc_id, kind) -> curves for i, ex in enumerate(extractors): hcurves = ex.get(what) for kind in hcurves.kind: arr = getattr(hcurves, kind) got[ex.calc_id, kind] = arr oq = ex.oqparam n_imts = len(hcurves.imt) [site] = hcurves.site_id for j, imt in enumerate(hcurves.imt): imls = oq.imtls[imt] ax = fig.add_subplot(n_imts, 1, j + 1) ax.set_xlabel('%s, site %s, inv_time=%dy' % (imt, site, oq.investigation_time)) ax.set_ylabel('PoE') for ck, arr in got.items(): if (arr == 0).all(): logging.warning('There is a zero curve %s_%s', *ck) ax.loglog(imls, arr.flat, '-', label='%s_%s' % ck) ax.grid(True) ax.legend() return plt
def make_figure_task_info(extractors, what): """ $ oq plot "task_info?kind=classical" """ plt = import_plt() if plt.__name__ == 'plotext': [ex] = extractors [(task_name, task_info)] = ex.get(what).to_dict().items() x = task_info['duration'] mean, std, med = x.mean(), x.std(ddof=1), numpy.median(x) plt.hist(x, bins=50) plt.title("mean=%d+-%d seconds, median=%d" % (mean, std, med)) return plt fig = plt.figure() [ex] = extractors [(task_name, task_info)] = ex.get(what).to_dict().items() x = task_info['duration'] ax = fig.add_subplot(2, 1, 1) mean, std = x.mean(), x.std(ddof=1) ax.hist(x, bins=50, rwidth=0.9) ax.set_title("mean=%d+-%d seconds" % (mean, std)) ax.set_ylabel("tasks=%d" % len(x)) from scipy.stats import linregress ax = fig.add_subplot(2, 1, 2) arr = numpy.sort(task_info, order='duration') x, y = arr['duration'], arr['weight'] reg = linregress(x, y) ax.plot(x, reg.intercept + reg.slope * x) ax.plot(x, y) ax.set_ylabel("weight") ax.set_xlabel("duration") return plt
def make_figure_uhs(extractors, what): """ $ oq plot "uhs?kind=mean&site_id=0" """ plt = import_plt() fig = plt.figure() got = {} # (calc_id, kind) -> curves for i, ex in enumerate(extractors): uhs = ex.get(what) for kind in uhs.kind: got[ex.calc_id, kind] = uhs[kind][0] # 1 site oq = ex.oqparam n_poes = len(oq.poes) periods = [imt.period for imt in oq.imt_periods()] imts = [imt.string for imt in oq.imt_periods()] [site] = uhs.site_id for j, poe in enumerate(oq.poes): ax = fig.add_subplot(n_poes, 1, j + 1) ax.set_xlabel('UHS on site %s, poe=%s, inv_time=%dy' % (site, poe, oq.investigation_time)) ax.set_ylabel('g') for ck, arr in got.items(): curve = list(arr[str(poe)][imts]) ax.plot(periods, curve, '-', label='%s_%s' % ck) ax.plot(periods, curve, '.') ax.grid(True) ax.legend() return plt
def make_figure_vs30(extractors, what): """ $ oq plot "vs30?" """ plt = import_plt() fig = plt.figure() [ex] = extractors sitecol = ex.get('sitecol') ax = fig.add_subplot(111) ax.grid(True) ax.set_xlabel('vs30 for calculation %d' % ex.calc_id) vs30 = sitecol['vs30'] vs30[numpy.isnan(vs30)] = 0 ax.scatter(sitecol['lon'], sitecol['lat'], c=vs30, cmap='jet') return plt
def make_figure_avg_gmf(extractors, what): """ $ oq plot "avg_gmf?imt=PGA" """ plt = import_plt() fig = plt.figure() imt = what.split('=')[1] ax = fig.add_subplot(1, 1, 1) ax.grid(True) ax.set_title('Avg GMF for %s' % imt) ax.set_xlabel('Lon') ax.set_ylabel('Lat') if len(extractors) == 2: # compare two avg_gmf ex1, ex2 = extractors avg_gmf = ex1.get(what) avg_gmf2 = ex2.get(what) gmf = avg_gmf[imt] - avg_gmf2[imt] else: # plot a single avg_gmf [ex] = extractors avg_gmf = ex.get(what) gmf = avg_gmf[imt] coll = ax.scatter(avg_gmf['lons'], avg_gmf['lats'], c=gmf, cmap='jet') plt.colorbar(coll) return plt
def make_figure_disagg(extractors, what): """ $ oq plot "disagg?kind=Mag&imt=PGA&poe_id=0" """ plt = import_plt() from matplotlib import cm fig = plt.figure() oq = extractors[0].oqparam disagg = extractors[0].get(what) kind = disagg.kind # ex. ('Mag', 'Dist') ndims = len(kind) [sid] = disagg.site_id [imt] = disagg.imt [poe_id] = disagg.poe_id poes = disagg.array[:, ndims:] # from the right columns y = numpy.average(poes, weights=disagg.weights, axis=-1) print(y) bins = getattr(disagg, kind[0]) ncalcs = len(extractors) width = (bins[1] - bins[0]) * 0.5 x = middle(bins) if ncalcs == 1 else middle(bins) - width if ndims == 1: # simple bar chart ax = fig.add_subplot(1, 1, 1) ax.set_xlabel('Disagg%s on site %s, imt=%s, poe_id=%d, inv_time=%dy' % (disagg.kind, sid, imt, poe_id, oq.investigation_time)) ax.set_xlabel(kind[0]) ax.set_xticks(bins) ax.bar(x, y, width) for ex in extractors[1:]: ax.bar(x + width, ex.get(what).array, width) return plt if ncalcs > 1: raise NotImplementedError('Comparison for %s' % disagg.kind) shape = [len(getattr(disagg, ax)) - 1 for ax in kind] # 2D images if ndims == 2: y = y.reshape(shape + [1]) zbins = [''] else: # ndims == 3 y = y.reshape(shape) zbins = getattr(disagg, kind[2]) Z = y.shape[-1] axes = [] for z in range(Z): arr = y[:, :, z] ax = fig.add_subplot(Z, 1, z + 1) axes.append(ax) ax.set_xlabel('%s' % kind[0]) ax.set_ylabel(kind[1]) vbins = getattr(disagg, kind[1]) # vertical bins cmap = cm.get_cmap('jet', 100) extent = bins[0], bins[-1], vbins[0], vbins[-1] im = ax.imshow(arr, cmap=cmap, extent=extent, aspect='auto', vmin=y.min(), vmax=y.max()) # stacked bar chart # stacked_bar(ax, x, y.T, width) # ys = ['%.1f' % y for y in getattr(disagg, kind[1])] # ax.legend(ys) fig.colorbar(im, ax=axes) return plt
def make_figure_hmaps(extractors, what): """ $ oq plot "hmaps?kind=mean&imt=PGA" """ plt = import_plt() fig = plt.figure() ncalcs = len(extractors) if ncalcs > 2: raise RuntimeError('Could not plot more than two calculations at once') elif ncalcs == 2: # plot the differences ex1, ex2 = extractors oq1 = ex1.oqparam oq2 = ex2.oqparam n_poes = len(oq1.poes) assert n_poes == len(oq2.poes) itime = oq1.investigation_time assert oq2.investigation_time == itime sitecol = ex1.get('sitecol') array2 = ex2.get('sitecol').array for name in ('lon', 'lat'): numpy.testing.assert_equal(array2[name], sitecol.array[name]) hmaps1 = ex1.get(what) hmaps2 = ex2.get(what) [imt] = hmaps1.imt assert [imt] == hmaps2.imt [kind] = hmaps1.kind assert hmaps1.kind == [kind] for j, poe in enumerate(oq1.poes): diff = hmaps1[kind][:, 0, j] - hmaps2[kind][:, 0, j] maxdiff = numpy.abs(diff).max() ax = fig.add_subplot(1, n_poes, j + 1) ax.grid(True) ax.set_xlabel( 'IMT=%s, kind=%s, poe=%s\ncalcs %d-%d, ' 'inv_time=%dy\nmaxdiff=%s' % (imt, kind, poe, ex1.calc_id, ex2.calc_id, itime, maxdiff)) coll = ax.scatter(sitecol['lon'], sitecol['lat'], c=diff, cmap='jet') plt.colorbar(coll) elif ncalcs == 1: # plot the hmap [ex] = extractors oq = ex.oqparam n_poes = len(oq.poes) sitecol = ex.get('sitecol') hmaps = ex.get(what) [imt] = hmaps.imt [kind] = hmaps.kind for j, poe in enumerate(oq.poes): ax = fig.add_subplot(1, n_poes, j + 1) ax.grid(True) ax.set_xlabel('hmap for IMT=%s, kind=%s, poe=%s\ncalculation %d, ' 'inv_time=%dy' % (imt, kind, poe, ex.calc_id, oq.investigation_time)) coll = ax.scatter(sitecol['lon'], sitecol['lat'], c=hmaps[kind][:, 0, j], cmap='jet') plt.colorbar(coll) return plt
import plotext as plt # format: <<ID:default_value>> scatter_size = <<SCATTER_SIZE:1>> labels = <<LABELS:None>> h = <<HEIGHT:None>> w = <<WIDTH:None>> vv = <<DATA:[]>> if w is not None: plt.figure(figsize=(w*cm, h*cm)) # scatter for ii in range(len(vv)): v = vv[ii] x = [v[2*i] for i in range(len(v)//2)] y = [v[2*i+1] for i in range(len(v)//2) if 2*i + 1 < len(v)] if labels and ii < len(labels): label = labels[ii] plt.scatter(x, y, label=label) else: plt.scatter(x, y) plt.canvas_color('black') plt.axes_color('black') plt.ticks_color('white') if w is not None: plt.plot_size(w, h) plt.show()