def autocorrelation(data, name, maxlag=100, format='png', suffix='-acf', path='./', fontmap={ 1: 10, 2: 8, 3: 6, 4: 5, 5: 4 }, verbose=1): """ Generate bar plot of a series, usually autocorrelation or autocovariance. :Arguments: data: array or list A trace from an MCMC sample. 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). """ # If there is just one data series, wrap it in a list if rank(data) == 1: data = [data] # Number of plots per page rows = min(len(data), 4) for i, values in enumerate(data): if verbose > 0: print 'Plotting', name + suffix if not i % rows: # Generate new figure figure(figsize=(10, 6)) # New subplot subplot(rows, 1, i - (rows * (i / rows)) + 1) x = arange(maxlag) y = [_autocorr(values, lag=i) for i in x] bar(x, y) # Set axis bounds ylim(-1.0, 1.0) xlim(0, len(y)) # Plot options ylabel(name, fontsize='x-small') tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[rows]) tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[rows]) # Save to file if not (i + 1) % rows or i == len(values) - 1: # Label X-axis on last subplot xlabel('Lag', fontsize='x-small') if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' if rows > 4: # Append plot number to suffix, if there will be more than one suffix += '_%i' % i savefig("%s%s%s.%s" % (path, name, suffix, format))
def autocorrelation(data, name, maxlag=100, format='png', suffix='-acf', path='./', fontmap = {1:10, 2:8, 3:6, 4:5, 5:4}, new=True, last=True, rows=1, num=1, verbose=1): """ Generate bar plot of the autocorrelation function for a series (usually an MCMC trace). :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. maxlag (optional): int The largest discrete value for the autocorrelation to be calculated (defaults to 100). 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). fontmap (optional): dict Font mapping for plot labels; most users should not specify this. verbose (optional): int Level of output verbosity. """ # If there is only one data array, go ahead and plot it ... if rank(data)==1: if verbose>0: print 'Plotting', name+suffix # If new plot, generate new frame if new: figure(figsize=(10, 6)) subplot(rows, 1, num) x = arange(maxlag) y = [_autocorr(data, lag=i) for i in x] bar(x, y) # Set axis bounds ylim(-1.0, 1.0) xlim(0, len(y)) # Plot options ylabel(name, fontsize='x-small') tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[rows]) tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[rows]) if last: # Label X-axis on last subplot xlabel('Lag', fontsize='x-small') if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' if rows>4: # Append plot number to suffix, if there will be more than one suffix += '_%i' % i savefig("%s%s%s.%s" % (path, name, suffix, format)) else: # ... otherwise plot recursively tdata = swapaxes(data, 0, 1) # 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 = not (_num + 1) % (_rows * 2) or (i==len(tdata)-1) autocorrelation(tdata[i], name+'_'+str(i), maxlag=maxlag, format=format, suffix=suffix, path=path, fontmap=fontmap, new=_new, last=_last, rows=_rows, num=_num, verbose=verbose)
def autocorrelation(data, name, maxlag=100, format='png', suffix='-acf', path='./', fontmap = {1:10, 2:8, 3:6, 4:5, 5:4}, verbose=1): """ Generate bar plot of a series, usually autocorrelation or autocovariance. :Arguments: data: array or list A trace from an MCMC sample. 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). """ # If there is just one data series, wrap it in a list if rank(data)==1: data = [data] # Number of plots per page rows = min(len(data), 4) for i,values in enumerate(data): if verbose>0: print 'Plotting', name+suffix if not i % rows: # Generate new figure figure(figsize=(10, 6)) # New subplot subplot(rows, 1, i - (rows*(i/rows)) + 1) x = arange(maxlag) y = [_autocorr(values, lag=i) for i in x] bar(x, y) # Set axis bounds ylim(-1.0, 1.0) xlim(0, len(y)) # Plot options ylabel(name, fontsize='x-small') tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[rows]) tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[rows]) # Save to file if not (i+1) % rows or i == len(values)-1: # Label X-axis on last subplot xlabel('Lag', fontsize='x-small') if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' if rows>4: # Append plot number to suffix, if there will be more than one suffix += '_%i' % i savefig("%s%s%s.%s" % (path, name, suffix, format))