def geweke_plot(data, name, format='png', suffix='-diagnostic', path='./', fontmap=None, verbose=1): # Generate Geweke (1992) diagnostic plots if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # Generate new scatter plot figure() x, y = transpose(data) scatter(x.tolist(), y.tolist()) # Plot options xlabel('First iteration', fontsize='x-small') ylabel('Z-score for %s' % name, fontsize='x-small') # Plot lines at +/- 2 sd from zero pyplot((nmin(x), nmax(x)), (2, 2), '--') pyplot((nmin(x), nmax(x)), (-2, -2), '--') # Set plot bound ylim(min(-2.5, nmin(y)), max(2.5, nmax(y))) xlim(0, nmax(x)) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def geweke_plot(data, name, format='png', suffix='-diagnostic', path='./', fontmap = None, verbose=1): # Generate Geweke (1992) diagnostic plots if fontmap is None: fontmap = {1:10, 2:8, 3:6, 4:5, 5:4} # Generate new scatter plot figure() x, y = transpose(data) scatter(x.tolist(), y.tolist()) # Plot options xlabel('First iteration', fontsize='x-small') ylabel('Z-score for %s' % name, fontsize='x-small') # Plot lines at +/- 2 sd from zero pyplot((nmin(x), nmax(x)), (2, 2), '--') pyplot((nmin(x), nmax(x)), (-2, -2), '--') # Set plot bound ylim(min(-2.5, nmin(y)), max(2.5, nmax(y))) xlim(0, nmax(x)) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def geweke_plot(data, name, format='png', suffix='-diagnostic', path='./', fontmap=None): ''' Generate Geweke (1992) diagnostic plots. :Arguments: data: list List (or list of lists for vector-valued variables) of Geweke diagnostics, output from the `pymc.diagnostics.geweke` function . name: string The name of the plot. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix (defaults to "-diagnostic"). path (optional): string Specifies location for saving plots (defaults to local directory). fontmap (optional): dict Font map for plot. ''' if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # Generate new scatter plot figure() x, y = transpose(data) scatter(x.tolist(), y.tolist()) # Plot options xlabel('First iteration', fontsize='x-small') ylabel('Z-score for %s' % name, fontsize='x-small') # Plot lines at +/- 2 sd from zero pyplot((nmin(x), nmax(x)), (2, 2), '--') pyplot((nmin(x), nmax(x)), (-2, -2), '--') # Set plot bound ylim(min(-2.5, nmin(y)), max(2.5, nmax(y))) xlim(0, nmax(x)) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def csv_output(self): """This method is used to report the results of a subscription test to a csv file""" # determine the file name csv_filename = "subscription-%s-%siter-%s-%s.csv" % (self.subscriptiontype, self.iterations, self.chart_type.lower(), self.testdatetime) # initialize the csv file csvfile_stream = open(csv_filename, "w") csvfile_writer = csv.writer(csvfile_stream, delimiter=',', quoting=csv.QUOTE_MINIMAL) # iterate over the SIBs for sib in self.results.keys(): row = [sib] # add all the times for value in self.results[sib]: row.append(value) # add the mean, min, max and variance value of the times to the row row.append(round(nmean(self.results[sib]),3)) row.append(round(nmin(self.results[sib]),3)) row.append(round(nmax(self.results[sib]),3)) row.append(round(nvar(self.results[sib]),3)) # write the row csvfile_writer.writerow(row) # close the csv file csvfile_stream.close()
def pce_param(V, T, P, aerosols): Smaxes = [] for aerosol in aerosols: N = aerosol.distribution.N mu = aerosol.distribution.mu sigma = aerosol.distribution.sigma kappa = aerosol.kappa Smax = _pce_fit(N, mu, sigma, kappa, V, T, P) Smaxes.append(Smax) print "PCE with", N, mu, sigma, kappa, V, T, P, Smax min_smax = nmin(Smaxes) if 0. <= min_smax <= 0.5: Smax = min_smax else: return 0., [0.]*len(aerosols) ## Compute scrit of each mode scrits = [] for aerosol in aerosols: _, scrit = kohler_crit(T, aerosol.distribution.mu*1e-6, aerosol.kappa) scrits.append(scrit) act_fracs = [] for aerosol, scrit in zip(aerosols, scrits): ui = 2.*np.log(scrit/Smax)/(3.*np.sqrt(2.)*np.log(aerosol.distribution.sigma)) N_act = 0.5*aerosol.distribution.N*erfc(ui) act_fracs.append(N_act/aerosol.distribution.N) return Smax, act_fracs
def imexamine(self, src): try: data = self.fit.data(src, table=False) the_min = nmin(data) the_max = nmax(data) the_mea = nmea(data) the_std = nstd(data) the_med = nmed(data) return ([the_mea, the_med, the_std, the_min, the_max]) except Exception as e: self.etc.log(e)
def discrepancy_plot(data, name, report_p=True, format='png', suffix='-gof', path='./', fontmap={ 1: 10, 2: 8, 3: 6, 4: 5, 5: 4 }, verbose=1): # Generate goodness-of-fit deviate scatter plot if verbose > 0: print 'Plotting', name + suffix # Generate new scatter plot figure() try: x, y = transpose(data) except ValueError: x, y = data scatter(x, y) # Plot x=y line lo = nmin(ravel(data)) hi = nmax(ravel(data)) datarange = hi - lo lo -= 0.1 * datarange hi += 0.1 * datarange pyplot((lo, hi), (lo, hi)) # Plot options xlabel('Observed deviates', fontsize='x-small') ylabel('Simulated deviates', fontsize='x-small') if report_p: # Put p-value in legend count = sum(s > o for o, s in zip(x, y)) text(lo + 0.1 * datarange, hi - 0.1 * datarange, 'p=%.3f' % (count / len(x)), horizontalalignment='center', fontsize=10) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def fits_stat(self, src): self.etc.log("Getting Stats from {}".format(src)) try: hdu = fts.open(src) image_data = hdu[0].data return ({ 'Min': nmin(image_data), 'Max': nmax(image_data), 'Mean': nmea(image_data), 'Stdev': nstd(image_data) }) except Exception as e: self.etc.log(e)
def closest_image(x, y, x1, y1): L = 2.0 r = [(x - x1) ** 2 + (y - y1) ** 2, # ( 0 0 ) (x - x1) ** 2 + (y - (y1 + L)) ** 2, # ( 0 L ) (x - (x1 + L)) ** 2 + (y - (y1 + L)) ** 2, # ( L L ) (x - (x1 + L)) ** 2 + (y - y1) ** 2, # ( L 0 ) (x - (x1 + L)) ** 2 + (y - (y1 - L)) ** 2, # ( L -L ) (x - x1) ** 2 + (y - (y1 - L)) ** 2, # ( 0 -L ) (x - (x1 - L)) ** 2 + (y - (y1 - L)) ** 2, # (-L -L ) (x - (x1 - L)) ** 2 + (y - y1) ** 2, # (-L 0 ) (x - (x1 - L)) ** 2 + (y - (y1 + L)) ** 2] # (-L L ) return sqrt(nmin(r, axis=0))
def get_network_extents(net): ''' For a given Emme Network, find the envelope (extents) of all of its elements. Includes link vertices as well as nodes. Args: -net: An Emme Network Object Returns: minx, miny, maxx, maxy tuple ''' xs, ys = [], [] for node in net.nodes(): xs.append(node.x) ys.append(node.y) for link in net.links(): for x, y in link.vertices: xs.append(x) ys.append(y) xa = array(xs) ya = array(ys) return nmin(xa) - 1.0, nmin(ya) - 1.0, nmax(xa) + 1.0, nmax(ya) + 1.0
def stats(self, file): """Returns statistics of a given fit file.""" self.logger.info("Getting Stats from {}".format(file)) try: hdu = fts.open(file) image_data = hdu[0].data return { 'Min': nmin(image_data), 'Max': nmax(image_data), 'Median': nmed(image_data), 'Mean': nmea(image_data), 'Stdev': nstd(image_data) } except Exception as e: self.logger.error(e)
def discrepancy_plot( data, name="discrepancy", report_p=True, format="png", suffix="-gof", path="./", fontmap=None, verbose=1 ): # Generate goodness-of-fit deviate scatter plot if verbose > 0: print_("Plotting", name + suffix) if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # Generate new scatter plot figure() try: x, y = transpose(data) except ValueError: x, y = data scatter(x, y) # Plot x=y line lo = nmin(ravel(data)) hi = nmax(ravel(data)) datarange = hi - lo lo -= 0.1 * datarange hi += 0.1 * datarange pyplot((lo, hi), (lo, hi)) # Plot options xlabel("Observed deviates", fontsize="x-small") ylabel("Simulated deviates", fontsize="x-small") if report_p: # Put p-value in legend count = sum(s > o for o, s in zip(x, y)) text( lo + 0.1 * datarange, hi - 0.1 * datarange, "p=%.3f" % (count / len(x)), horizontalalignment="center", fontsize=10, ) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith("/"): path += "/" savefig("%s%s%s.%s" % (path, name, suffix, format))
def csv_output(self): """This method is used to report the results of an update test to a csv file""" # determine the file name csv_filename = "update-%s-%sstep-%smax-%siter-%s-%s.csv" % (self.updatetype, self.step, self.limit, self.iterations, self.chart_type.lower(), self.testdatetime) # initialize the csv file csvfile_stream = open(csv_filename, "w") csvfile_writer = csv.writer(csvfile_stream, delimiter=',', quoting=csv.QUOTE_MINIMAL) # iterate over the SIBs for sib in self.results.keys(): # iterate over the possible block lengths for triple_length in sorted(self.results[sib].keys(), key=int): row = [sib] # add the length of the block to the row row.append(triple_length) # add all the times for value in self.results[sib][triple_length]: row.append(value) # add the mean value of the times to the row row.append(round(nmean(self.results[sib][triple_length]),3)) row.append(round(nmin(self.results[sib][triple_length]),3)) row.append(round(nmax(self.results[sib][triple_length]),3)) row.append(round(nvar(self.results[sib][triple_length]),3)) # write the row csvfile_writer.writerow(row) # close the csv file csvfile_stream.close()
def _creer_cmap(self, seuils): zmax = nmax(self._Z) zmin = nmin(self._Z) delta = zmax - zmin # On les ramène entre 0 et 1 par transformation affine if delta: a = 1/delta b = -zmin/delta seuils = [0] + [a*z + b for z in seuils if zmin < z < zmax] + [1] # NB: < et pas <= print(seuils) cdict = {'red': [], 'green': [], 'blue': []} def add_col(val, color1, color2): cdict['red'].append((val, color1[0], color2[0])) cdict['green'].append((val, color1[1], color2[1])) cdict['blue'].append((val, color1[2], color2[2])) n = len(self.couleurs) for i, seuil in enumerate(seuils): add_col(seuil, self.couleurs[(i - 1)%n], self.couleurs[i%n]) return LinearSegmentedColormap('seuils', cdict, 256)
def _creer_cmap(self, seuils): zmax = nmax(self._Z) zmin = nmin(self._Z) delta = zmax - zmin # On les ramène entre 0 et 1 par transformation affine if delta: a = 1 / delta b = -zmin / delta seuils = [0] + [a * z + b for z in seuils if zmin < z < zmax ] + [1] # NB: < et pas <= print seuils cdict = {'red': [], 'green': [], 'blue': []} def add_col(val, color1, color2): cdict['red'].append((val, color1[0], color2[0])) cdict['green'].append((val, color1[1], color2[1])) cdict['blue'].append((val, color1[2], color2[2])) n = len(self.couleurs) for i, seuil in enumerate(seuils): add_col(seuil, self.couleurs[(i - 1) % n], self.couleurs[i % n]) return LinearSegmentedColormap('seuils', cdict, 256)
def discrepancy_plot(data, name, report_p=True, format='png', suffix='-gof', path='./', fontmap = {1:10, 2:8, 3:6, 4:5, 5:4}, verbose=1): # Generate goodness-of-fit deviate scatter plot if verbose>0: print 'Plotting', name+suffix # Generate new scatter plot figure() try: x, y = transpose(data) except ValueError: x, y = data scatter(x, y) # Plot x=y line lo = nmin(ravel(data)) hi = nmax(ravel(data)) datarange = hi-lo lo -= 0.1*datarange hi += 0.1*datarange pyplot((lo, hi), (lo, hi)) # Plot options xlabel('Observed deviates', fontsize='x-small') ylabel('Simulated deviates', fontsize='x-small') if report_p: # Put p-value in legend count = sum(s>o for o,s in zip(x,y)) text(lo+0.1*datarange, hi-0.1*datarange, 'p=%.3f' % (count/len(x)), horizontalalignment='center', fontsize=10) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def discrepancy_plot( data, name='discrepancy', report_p=True, format='png', suffix='-gof', path='./', fontmap=None): ''' Generate goodness-of-fit deviate scatter plot. :Arguments: data: list List (or list of lists for vector-valued variables) of discrepancy values, output from the `pymc.diagnostics.discrepancy` function . name: string The name of the plot. report_p: bool Flag for annotating the p-value to the plot. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix (defaults to "-gof"). path (optional): string Specifies location for saving plots (defaults to local directory). fontmap (optional): dict Font map for plot. ''' if verbose > 0: print_('Plotting', name + suffix) if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # Generate new scatter plot figure() try: x, y = transpose(data) except ValueError: x, y = data scatter(x, y) # Plot x=y line lo = nmin(ravel(data)) hi = nmax(ravel(data)) datarange = hi - lo lo -= 0.1 * datarange hi += 0.1 * datarange pyplot((lo, hi), (lo, hi)) # Plot options xlabel('Observed deviates', fontsize='x-small') ylabel('Simulated deviates', fontsize='x-small') if report_p: # Put p-value in legend count = sum(s > o for o, s in zip(x, y)) text(lo + 0.1 * datarange, hi - 0.1 * datarange, 'p=%.3f' % (count / len(x)), horizontalalignment='center', fontsize=10) # Save to file if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format))
def plot( data, name, format='png', suffix='', path='./', common_scale=True, datarange=(None, None), new=True, last=True, rows=1, num=1, fontmap=None, verbose=1): """ Generates summary plots for nodes of a given PyMC object. :Arguments: data: PyMC object, trace or array A trace from an MCMC sample or a PyMC object with one or more traces. name: string The name of the object. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix. path (optional): string Specifies location for saving plots (defaults to local directory). common_scale (optional): bool Specifies whether plots of multivariate nodes should be on the same scale (defaults to True). """ if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # If there is only one data array, go ahead and plot it ... if ndim(data) == 1: if verbose > 0: print_('Plotting', name) # If new plot, generate new frame if new: figure(figsize=(10, 6)) # Call trace trace( data, name, datarange=datarange, rows=rows * 2, columns=2, num=num + 3 * (num - 1), last=last, fontmap=fontmap) # Call autocorrelation autocorrelation( data, name, rows=rows * 2, columns=2, num=num + 3 * ( num - 1) + 2, last=last, fontmap=fontmap) # Call histogram histogram( data, name, datarange=datarange, rows=rows, columns=2, num=num * 2, last=last, fontmap=fontmap) if last: if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' savefig("%s%s%s.%s" % (path, name, suffix, format)) else: # ... otherwise plot recursively tdata = swapaxes(data, 0, 1) datarange = (None, None) # Determine common range for plots if common_scale: datarange = (nmin(tdata), nmax(tdata)) # How many rows? _rows = min(4, len(tdata)) for i in range(len(tdata)): # New plot or adding to existing? _new = not i % _rows # Current subplot number _num = i % _rows + 1 # Final subplot of current figure? _last = (_num == _rows) or (i == len(tdata) - 1) plot( tdata[i], name + '_' + str(i), format=format, path=path, common_scale=common_scale, datarange=datarange, suffix=suffix, new=_new, last=_last, rows=_rows, num=_num)
def summary_plot( pymc_obj, name='model', format='png', suffix='-summary', path='./', alpha=0.05, chain=None, quartiles=True, hpd=True, rhat=True, main=None, xlab=None, x_range=None, custom_labels=None, chain_spacing=0.05, vline_pos=0): """ Model summary plot Generates a "forest plot" of 100*(1-alpha)% credible intervals for either the set of nodes in a given model, or a specified set of nodes. :Arguments: pymc_obj: PyMC object, trace or array A trace from an MCMC sample or a PyMC object with one or more traces. name (optional): string The name of the object. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix. path (optional): string Specifies location for saving plots (defaults to local directory). alpha (optional): float Alpha value for (1-alpha)*100% credible intervals (defaults to 0.05). chain (optional): int Where there are multiple chains, specify a particular chain to plot. If not specified (chain=None), all chains are plotted. quartiles (optional): bool Flag for plotting the interquartile range, in addition to the (1-alpha)*100% intervals (defaults to True). hpd (optional): bool Flag for plotting the highest probability density (HPD) interval instead of the central (1-alpha)*100% interval (defaults to True). rhat (optional): bool Flag for plotting Gelman-Rubin statistics. Requires 2 or more chains (defaults to True). main (optional): string Title for main plot. Passing False results in titles being suppressed; passing False (default) results in default titles. xlab (optional): string Label for x-axis. Defaults to no label x_range (optional): list or tuple Range for x-axis. Defaults to matplotlib's best guess. custom_labels (optional): list User-defined labels for each node. If not provided, the node __name__ attributes are used. chain_spacing (optional): float Plot spacing between chains (defaults to 0.05). vline_pos (optional): numeric Location of vertical reference line (defaults to 0). """ if not gridspec: print_( '\nYour installation of matplotlib is not recent enough to support summary_plot; this function is disabled until matplotlib is updated.') return # Quantiles to be calculated quantiles = [100 * alpha / 2, 50, 100 * (1 - alpha / 2)] if quartiles: quantiles = [100 * alpha / 2, 25, 50, 75, 100 * (1 - alpha / 2)] # Range for x-axis plotrange = None # Gridspec gs = None # Subplots interval_plot = None rhat_plot = None try: # First try Model type vars = pymc_obj._variables_to_tally except AttributeError: try: # Try a database object vars = pymc_obj._traces except AttributeError: if isinstance(pymc_obj, Variable): vars = [pymc_obj] else: # Assume an iterable vars = pymc_obj from .diagnostics import gelman_rubin # Calculate G-R diagnostics if rhat: try: R = {} for variable in vars: R[variable.__name__] = gelman_rubin(variable) except (ValueError, TypeError): print( 'Could not calculate Gelman-Rubin statistics. Requires multiple chains of equal length.') rhat = False # Empty list for y-axis labels labels = [] # Counter for current variable var = 1 # Make sure there is something to print if all([v._plot == False for v in vars]): print_('No variables to plot') return for variable in vars: # If plot flag is off, do not print if variable._plot == False: continue # Extract name varname = variable.__name__ # Retrieve trace(s) if chain is not None: chains = 1 traces = [variable.trace(chain=chain)] else: chains = variable.trace.db.chains traces = [variable.trace(chain=i) for i in range(chains)] if gs is None: # Initialize plot if rhat and chains > 1: gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) else: gs = gridspec.GridSpec(1, 1) # Subplot for confidence intervals interval_plot = subplot(gs[0]) # Get quantiles data = [calc_quantiles(d, quantiles) for d in traces] if hpd: # Substitute HPD interval for i, d in enumerate(traces): hpd_interval = calc_hpd(d, alpha) data[i][quantiles[0]] = hpd_interval[0] data[i][quantiles[-1]] = hpd_interval[1] data = [[d[q] for q in quantiles] for d in data] # Ensure x-axis contains range of current interval if plotrange: plotrange = [min( plotrange[0], nmin(data)), max(plotrange[1], nmax(data))] else: plotrange = [nmin(data), nmax(data)] try: # First try missing-value stochastic value = variable.get_stoch_value() except AttributeError: # All other variable types value = variable.value # Number of elements in current variable k = size(value) # Append variable name(s) to list if k > 1: names = var_str(varname, shape(value)[int(shape(value)[0]==1):]) labels += names else: labels.append(varname) # labels.append('\n'.join(varname.split('_'))) # Add spacing for each chain, if more than one e = [0] + [(chain_spacing * ((i + 2) / 2)) * ( -1) ** i for i in range(chains - 1)] # Loop over chains for j, quants in enumerate(data): # Deal with multivariate nodes if k > 1: ravelled_quants = list(map(ravel, quants)) for i, quant in enumerate(transpose(ravelled_quants)): q = ravel(quant) # Y coordinate with jitter y = -(var + i) + e[j] if quartiles: # Plot median pyplot(q[2], y, 'bo', markersize=4) # Plot quartile interval errorbar( x=(q[1], q[3]), y=(y, y), linewidth=2, color="blue") else: # Plot median pyplot(q[1], y, 'bo', markersize=4) # Plot outer interval errorbar( x=(q[0], q[-1]), y=(y, y), linewidth=1, color="blue") else: # Y coordinate with jitter y = -var + e[j] if quartiles: # Plot median pyplot(quants[2], y, 'bo', markersize=4) # Plot quartile interval errorbar( x=(quants[1], quants[3]), y=(y, y), linewidth=2, color="blue") else: # Plot median pyplot(quants[1], y, 'bo', markersize=4) # Plot outer interval errorbar( x=(quants[0], quants[-1]), y=(y, y), linewidth=1, color="blue") # Increment index var += k if custom_labels is not None: labels = custom_labels # Update margins left_margin = max([len(x) for x in labels]) * 0.015 gs.update(left=left_margin, right=0.95, top=0.9, bottom=0.05) # Define range of y-axis ylim(-var + 0.5, -0.5) datarange = plotrange[1] - plotrange[0] xlim(plotrange[0] - 0.05 * datarange, plotrange[1] + 0.05 * datarange) # Add variable labels yticks([-(l + 1) for l in range(len(labels))], labels) # Add title if main is not False: plot_title = main or str(int(( 1 - alpha) * 100)) + "% Credible Intervals" title(plot_title) # Add x-axis label if xlab is not None: xlabel(xlab) # Constrain to specified range if x_range is not None: xlim(*x_range) # Remove ticklines on y-axes for ticks in interval_plot.yaxis.get_major_ticks(): ticks.tick1On = False ticks.tick2On = False for loc, spine in six.iteritems(interval_plot.spines): if loc in ['bottom', 'top']: pass # spine.set_position(('outward',10)) # outward by 10 points elif loc in ['left', 'right']: spine.set_color('none') # don't draw spine # Reference line axvline(vline_pos, color='k', linestyle='--') # Genenerate Gelman-Rubin plot if rhat and chains > 1: # If there are multiple chains, calculate R-hat rhat_plot = subplot(gs[1]) if main is not False: title("R-hat") # Set x range xlim(0.9, 2.1) # X axis labels xticks((1.0, 1.5, 2.0), ("1", "1.5", "2+")) yticks([-(l + 1) for l in range(len(labels))], "") i = 1 for variable in vars: if variable._plot == False: continue # Extract name varname = variable.__name__ try: value = variable.get_stoch_value() except AttributeError: value = variable.value k = size(value) if k > 1: pyplot([min(r, 2) for r in R[varname]], [-(j + i) for j in range(k)], 'bo', markersize=4) else: pyplot(min(R[varname], 2), -i, 'bo', markersize=4) i += k # Define range of y-axis ylim(-i + 0.5, -0.5) # Remove ticklines on y-axes for ticks in rhat_plot.yaxis.get_major_ticks(): ticks.tick1On = False ticks.tick2On = False for loc, spine in six.iteritems(rhat_plot.spines): if loc in ['bottom', 'top']: pass # spine.set_position(('outward',10)) # outward by 10 points elif loc in ['left', 'right']: spine.set_color('none') # don't draw spine savefig("%s%s%s.%s" % (path, name, suffix, format))
def powerspectrum(*args,**kw): """Calling Sequence: alpha, beta, power, [freq] = powerspectrum(time, data, [freq,] weights=None, timeit=True, ofac=4) Input: time : Time array data : Data array freq : Frequency array with frequencies to be evaluated. If not present one will be generated Output: alpha : alpha coefficient (see spectrum_core) beta : beta coefficient (see spectrum_core) power : Power Spectrum freq : Generated frequency array (if not present in input) Keywords: weights : Array with weights to be used. If this array is not present no weighting will be used timeit : If true prints timing information ofac : Oversampling parameter Description: Main Routine for evaluation of power-spectra """ # ------ Starting time t0 = systemtime() # ------- Handle keywords weights = kw.get('weights',None) timeit = kw.get('timeit',True) ofac = kw.get('ofac',4.) # ------ Handle arguments if len(args) == 2: time = args[0] data = args[1] elif len(args) == 3: time = args[0] data = args[1] freq = args[2] else: raise InputError('Wrong number of inputs') time = array(time,dtype=float64).squeeze() data = array(data,dtype=float64).squeeze() # ------ Subtract zero frequency ddata = data - mean(data) # ------ Handle frequency array if len(args) == 2: dt = median(diff(time)) nyq = 1./(2*dt) tdiff = nmax(time) - nmin(time) freq = arange(0,nyq,1./(ofac*tdiff),dtype=float64) else: freq = array(freq,dtype=float64).squeeze() # ------ Handle weights if weights is not None: weights = array(weights,dtype=float64).squeeze() # ------ Calculate Power Spectrum alpha, beta, power = chunkeval(time,ddata,freq,weights=weights) # ------ Ending time if timeit: print 'spectrum.py: powerspectrum finished in %3.1f seconds' % (systemtime() - t0) # ------ Return if len(args) == 2: return alpha, beta, power, freq else: return alpha, beta, power
def windowfunction(*args,**kw): """Calling Sequence: power, [freq] = windowfunction(time, [freq,] weights=None, timeit=True, ofac=4, wcf=None) Input: time : Time array freq : Frequency array with frequencies to be evaluated. If not present one will be generated Output: power : Power Spectrum freq : Generated frequency array (if not present in input) Keywords: weights : Array with weights to be used. If this array is not present no weighting will be used timeit : If true prints timing information ofac : Oversampling parameter wcf : If given this will the be frequency for the pseudo window. If not given a central frequency is used Description: The window function for a Lomb periodogram is not strictly defined. This functions calculates a pseudo-window. """ # ------ Starting time t0 = systemtime() # ------- Handle keywords weights = kw.get('weights',None) timeit = kw.get('timeit',True) wcf = kw.get('wcf',None) #Window center frequency ofac = kw.get('ofac',4.) # ------ Handle arguments if len(args) == 1: time = args[0] elif len(args) == 2: time = args[0] freq = args[1] else: raise InputError('Wrong number of inputs') time = array(time,dtype=float64).squeeze() # ------ Handle frequency array if len(args) == 1: dt = median(diff(time)) nyq = 1./(2*dt) tdiff = nmax(time) - nmin(time) freq = arange(0.01*nyq,0.99*nyq,1./(ofac*tdiff),dtype=float64) else: freq = array(freq,dtype=float64).squeeze() # ------ Handle weights if weights is not None: weights = array(weights,dtype=float64).squeeze() # ------ Estimate Window Center Frequency if wcf is None: wcf = median(freq) # ------ Make data arg = 2*pi*wcf*time #data = 0.5 * (np.sin(arg) + np.cos(arg)) # ------ Calculate Power Spectrum (Only power output) power = 0.5*(chunkeval(time,sin(arg),freq,weights=weights)[-1] + chunkeval(time,cos(arg),freq,weights=weights)[-1]) # ------ Ending time if timeit: print 'spectrum.py: windowfunction finished in %3.1f seconds' % (systemtime() - t0) # ------ Return if len(args) == 1: return power, freq else: return power #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# """Calling Sequence:
def shiftEnergies(energies): emin = nmin(energies) if emin < 0: for i in range(len(energies)): energies[i] = energies[i] - emin return energies, emin
def summary_plot(pymc_obj, name='model', format='png', suffix='-summary', path='./', alpha=0.05, quartiles=True, rhat=True, main=None, chain_spacing=0.05, vline_pos=0): """ Model summary plot :Arguments: pymc_obj: PyMC object, trace or array A trace from an MCMC sample or a PyMC object with one or more traces. name (optional): string The name of the object. format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix. path (optional): string Specifies location for saving plots (defaults to local directory). alpha (optional): float Alpha value for (1-alpha)*100% credible intervals (defaults to 0.05). rhat (optional): bool Flag for plotting Gelman-Rubin statistics. Requires 2 or more chains (defaults to True). main (optional): string Title for main plot. Passing False results in titles being suppressed; passing False (default) results in default titles. chain_spacing (optional): float Plot spacing between chains (defaults to 0.05). vline_pos (optional): numeric Location of vertical reference line (defaults to 0). """ if not gridspec: print '\nYour installation of matplotlib is not recent enough to support summary_plot; this function is disabled until matplotlib is updated.' return # Quantiles to be calculated quantiles = [100*alpha/2, 50, 100*(1-alpha/2)] if quartiles: quantiles = [100*alpha/2, 25, 50, 75, 100*(1-alpha/2)] # Range for x-axis plotrange = None # Number of chains chains = None # Gridspec gs = None # Subplots interval_plot = None rhat_plot = None try: # First try Model type vars = pymc_obj._variables_to_tally except AttributeError: # Assume an iterable vars = pymc_obj # Empty list for y-axis labels labels = [] # Counter for current variable var = 1 # Make sure there is something to print if all([v._plot==False for v in vars]): print 'No variables to plot' return for variable in vars: # If plot flag is off, do not print if variable._plot==False: continue # Extract name varname = variable.__name__ # Retrieve trace(s) i = 0 traces = [] while True: try: #traces.append(pymc_obj.trace(varname, chain=i)[:]) traces.append(variable.trace(chain=i)) i+=1 except KeyError: break chains = len(traces) if gs is None: # Initialize plot if rhat and chains>1: gs = gridspec.GridSpec(1, 2, width_ratios=[3,1]) else: gs = gridspec.GridSpec(1, 1) # Subplot for confidence intervals interval_plot = subplot(gs[0]) # Get quantiles data = [calc_quantiles(d, quantiles) for d in traces] data = [[d[q] for q in quantiles] for d in data] # Ensure x-axis contains range of current interval if plotrange: plotrange = [min(plotrange[0], nmin(data)), max(plotrange[1], nmax(data))] else: plotrange = [nmin(data), nmax(data)] try: # First try missing-value stochastic value = variable.get_stoch_value() except AttributeError: # All other variable types value = variable.value # Number of elements in current variable k = size(value) # Append variable name(s) to list if k>1: names = var_str(varname, shape(value)) labels += names else: labels.append('\n'.join(varname.split('_'))) # Add spacing for each chain, if more than one e = [0] + [(chain_spacing * ((i+2)/2))*(-1)**i for i in range(chains-1)] # Loop over chains for j,quants in enumerate(data): # Deal with multivariate nodes if k>1: for i,q in enumerate(transpose(quants)): # Y coordinate with jitter y = -(var+i) + e[j] if quartiles: # Plot median pyplot(q[2], y, 'bo', markersize=4) # Plot quartile interval errorbar(x=(q[1],q[3]), y=(y,y), linewidth=2, color="blue") else: # Plot median pyplot(q[1], y, 'bo', markersize=4) # Plot outer interval errorbar(x=(q[0],q[-1]), y=(y,y), linewidth=1, color="blue") else: # Y coordinate with jitter y = -var + e[j] if quartiles: # Plot median pyplot(quants[2], y, 'bo', markersize=4) # Plot quartile interval errorbar(x=(quants[1],quants[3]), y=(y,y), linewidth=2, color="blue") else: # Plot median pyplot(quants[1], y, 'bo', markersize=4) # Plot outer interval errorbar(x=(quants[0],quants[-1]), y=(y,y), linewidth=1, color="blue") # Increment index var += k # Define range of y-axis ylim(-var+0.5, -0.5) datarange = plotrange[1] - plotrange[0] xlim(plotrange[0] - 0.05*datarange, plotrange[1] + 0.05*datarange) # Add variable labels ylabels = yticks([-(l+1) for l in range(len(labels))], labels) # Add title if main is not False: plot_title = main or str(int((1-alpha)*100)) + "% Credible Intervals" title(plot_title) # Remove ticklines on y-axes for ticks in interval_plot.yaxis.get_major_ticks(): ticks.tick1On = False ticks.tick2On = False for loc, spine in interval_plot.spines.iteritems(): if loc in ['bottom','top']: pass #spine.set_position(('outward',10)) # outward by 10 points elif loc in ['left','right']: spine.set_color('none') # don't draw spine # Reference line axvline(vline_pos, color='k', linestyle='--') # Genenerate Gelman-Rubin plot if rhat and chains>1: from diagnostics import gelman_rubin # If there are multiple chains, calculate R-hat rhat_plot = subplot(gs[1]) if main is not False: title("R-hat") # Set x range xlim(0.9,2.1) # X axis labels xticks((1.0,1.5,2.0), ("1", "1.5", "2+")) yticks([-(l+1) for l in range(len(labels))], "") # Calculate diagnostic try: R = gelman_rubin(pymc_obj) except ValueError: R = {} for variable in vars: R[variable.__name__] = gelman_rubin(variable) i = 1 for variable in vars: if variable._plot==False: continue # Extract name varname = variable.__name__ try: value = variable.get_stoch_value() except AttributeError: value = variable.value k = size(value) if k>1: pyplot([min(r, 2) for r in R[varname]], [-(j+i) for j in range(k)], 'bo', markersize=4) else: pyplot(min(R[varname], 2), -i, 'bo', markersize=4) i += k # Define range of y-axis ylim(-i+0.5, -0.5) # Remove ticklines on y-axes for ticks in rhat_plot.yaxis.get_major_ticks(): ticks.tick1On = False ticks.tick2On = False for loc, spine in rhat_plot.spines.iteritems(): if loc in ['bottom','top']: pass #spine.set_position(('outward',10)) # outward by 10 points elif loc in ['left','right']: spine.set_color('none') # don't draw spine savefig("%s%s%s.%s" % (path, name, suffix, format))