Example #1
0
def set_axis_0():
    pylab.xlabel('time (days)')
    pylab.gcf().subplots_adjust(top=1.0-0.13, bottom=0.2, right=1-0.02,
                                left=0.2)
    a = list(pylab.axis())
    na = [a[0], a[1], 0, a[3]*1.05]
    pylab.axis(na)
Example #2
0
def make_report(event, dataframes, sequence, scores, part, n_iter, report_dir_base):

    # Run through the sequence of decisions.
    df = evaluate_sequence(sequence, dataframes)
    df = pd.concat([df, scores], axis=1)
    ns = ['a', 'b', 'c', 'd', 'e', 'f']
    l_ns = map(lambda x: "l_" + x, ns)
    o_ns = map(lambda x: "o_" + x, ns)

    cols = [u'acc', u'rec', u'avg. gain', u'action', u'gain', 
            u'max gain', #u'num nuggets', u'max nuggets',
            u'min select score', u'next score',] + l_ns + o_ns
    print df[cols]
                
    report_dir = os.path.join(
        report_dir_base, "iter-{}".format(n_iter + 1), part)
    if not os.path.exists(report_dir): os.makedirs(report_dir)

    results_path = os.path.join(report_dir, event.fs_name() + ".tsv")
    with open(results_path, "w") as f:
        df.to_csv(f, index=False, sep="\t")
    df["timestamp"] = df["timestamp"].apply(datetime.utcfromtimestamp)
    df.set_index("timestamp")[["acc", "rec", "avg. gain"]].plot()
    plt.gcf().suptitle(event.title+ " " + learner + " iter-{}".format(n_iter + 1))
    plt.gcf().savefig(os.path.join(report_dir, "{}.png".format(event.fs_name())))
def test_one_box(box,tree,graphics=False,callback=None):#,f):
	print 'box',box[0],box[1],':',
	s = tree.search(box)
	print ""
	print "box search:", s
	print "len(s):", len( s )
	boxes = tree.boxes()
	if graphics:
		plt.close()
		gfx.show_uboxes(boxes)
		gfx.show_uboxes(boxes, S=s, col='r')
	if len(s) < ((tree.dim**tree.depth)/2): # dim^depth/2
		t = tree.insert(box)
		if graphics:
			boxes = tree.boxes()
			gfx.show_uboxes(boxes, S=t, col='c')
		print 'ins:',t
 	else:
 		t = tree.remove(s)
		print 'rem:',t

	if graphics:
		gfx.show_box(box,col='g',alpha=0.5)
		if callback:
			plt.gcf().canvas.mpl_connect('button_press_event', callback)
		plt.show()
def PlotDebuggingData(allDraws,bestDraws,numIters,numRandomDraws,title=None,fileName=None):
  # put into array form
  allDraws = np.asarray(allDraws)
  bestDraws = np.asarray(bestDraws)

  # create a matrix of random draws versus iteration
  vals= np.ndarray.flatten(allDraws)
  iters = np.repeat([np.arange(numIters)],numRandomDraws)
  scatteredData= np.asarray(zip(iters,vals))

  veryBest = bestDraws[-1]
  ###CMTprint bestDraws
  ###CMTprint veryBest
  norm_by = 1/veryBest

  
  plt.scatter(scatteredData[:,0], norm_by*scatteredData[:,1],label="draws")
  plt.plot(np.arange(numIters), norm_by*bestDraws, label="best")
  plt.legend()
  if title!= None:
    plt.title(title)

  plt.xlabel("number of iterations")
  plt.xlim([0,numIters])
  plt.ylim([0,np.max(norm_by*scatteredData[:,1])])

  plt.ylabel("value (relative to best %e)"%veryBest)

  if fileName == None:
    plt.gcf().savefig("mytest.png")
  else:
    plt.gcf().savefig(fileName)
Example #5
0
def mean_per_hour_in_a_day(in_path, server, mac):
    ts = TimeSeries(in_path, "loss", dt_start=dt_start, dt_end=dt_end)
    hour_samples, hour_cnt = [], []
    for hour in xrange(24):
        hour_samples.append([])
        hour_cnt.append(0)
    for i in xrange(len(ts.x)):
        hour_samples[ts.x[i].hour].append(ts.y[i])
        hour_cnt[ts.x[i].hour] += 1

    hour_list, mean_list, var_list = [], [], []
    for hour in xrange(24):
        if len(hour_samples) > 0:
            hour_list.append(hour)
            mean_list.append(np.mean(hour_samples[hour]))
            var_list.append(np.var(hour_samples[hour]))
    hour_list = np.asarray(hour_list)
    mean_list = np.asarray(mean_list)
    var_list = np.asarray(var_list)

    print "hour_cnt={}".format(hour_cnt)
    plt.clf()
    matplotlib.rcParams.update({'font.size': 30})
    plt.gcf().set_size_inches(16, 15)
    plt.grid()
    plt.ylabel("Mean Loss Fraction")
    plt.xlabel("Hour")
    plt.xticks(range(0, 24, 2), rotation=45)
    plt.plot(hour_list, mean_list, marker="o")
    plt.fill_between(hour_list,
                     mean_list - var_list,
                     mean_list + var_list,
                     alpha=0.3)
    plt.savefig("./plots/mean_per_hour_in_a_day/mean_per_hour_in_a_day_{}_{}."
                "png".format(server, mac))
Example #6
0
def save_plot(P, filename='expected_f1.png'):
    E_F1 = pd.DataFrame(F1Optimizer.get_expectations(P).T,
                        columns=["/w None", "/wo None"])
    best_k, _, max_f1 = F1Optimizer.maximize_expectation(P)

    plt.style.use('ggplot')
    plt.figure()
    E_F1.plot()
    plt.title('Expected F1-Score for \n {}'.format("P = [{}]".format(",".join(
        map(str, P)))),
              fontsize=12)
    plt.xlabel('k')
    plt.xticks(np.arange(0, len(P) + 1, 1.0))
    plt.ylabel('E[F1(P,k)]')
    plt.plot([best_k], [max_f1], 'o', color='#000000', markersize=4)
    plt.annotate('max E[F1(P,k)] = E[F1(P,{})] = {:.5f}'.format(
        best_k, max_f1),
                 xy=(best_k, max_f1),
                 xytext=(best_k, max_f1 * 0.8),
                 arrowprops=dict(facecolor='black',
                                 shrink=0.05,
                                 width=1,
                                 headwidth=7),
                 horizontalalignment='center',
                 verticalalignment='top')
    plt.gcf().savefig(filename)
def density_scatter(x, y, bins, ax, sort=True, **kwargs):

    np.nan_to_num(y, copy=False)
    xy = np.vstack([x, y])
    z = gaussian_kde(xy)(xy)

    ax.scatter(x,
               y,
               c=z,
               cmap=cmap_vir,
               marker=".",
               s=4,
               picker=True,
               **kwargs)
    plt.subplots_adjust(wspace=0.3, hspace=0.3)
    plt.minorticks_on()
    #plt.grid(b=True, which='both', color='0.65', linestyle='-')
    plt.grid(b=True, which='minor', color='0.85', linestyle='--')
    plt.grid(b=True, which='major', color='0.85', linestyle='-')
    plt.xticks()
    plt.yticks()
    plt.rcParams["font.size"] = 12
    plt.gcf().set_tight_layout(False)

    return ax
Example #8
0
    def get_importance_rf(self, file_path=None, save=True, k=5):
        from sklearn.ensemble import ExtraTreesClassifier
        if self.algo not in ['*', 'random_forest']:
            raise ValueError(
                'class obj is not created with the {} algorithm support'.
                format(self.algo))

        if k < 1 or k > len(self.features):
            raise ValueError('Error in k value of {}'.format(k))

        X_train = self.train_data.drop(['poi'], axis=1)
        X_train = np.array(X_train)
        y_train = self.train_data.loc[:, 'poi'].values
        forest = ExtraTreesClassifier(n_estimators=250, random_state=42)
        forest.fit(X_train, y_train)
        importance = forest.feature_importances_
        ftr_imps = OrderedDict()
        for each in list(np.argsort(importance)[::-1]):
            ftr_imps[self.features[each]] = importance[each]
        if not save:
            return OrderedDict(list(ftr_imps.items())[:k])
        else:
            from matplotlib import pylab as plt
            plt.figure(figsize=(17, 10))
            plt.title('Random Forest Feature Importance')
            plt.barh(range(X_train.shape[1]), ftr_imps.values(), color="r")
            plt.yticks(range(X_train.shape[1]), ftr_imps.keys())
            plt.xlim([0, 0.3])
            plt.xlabel('relative importance')
            plt.gcf().savefig(file_path)
Example #9
0
def channel_transform(fitsfiles, h5file, iref= None):
    """
    Channel Transformation

    Take a list of k2 pixel files (must be from the same
    channel). Find the centroids of each image and solve for the
    linear transformation that takes one scene to another
    """
    nstars = len(fitsfiles)

    # Pull the first file to get length and data type
    fitsfile0 = fitsfiles[0]
    cent0 = fits_to_chip_centroid(fitsfile0)
    channel = get_channel(fitsfile0)
    print "Using channel = %i" % channel

    # Determine the refence frame
    if iref==None:
        dfcent0 = pd.DataFrame(LE(cent0))
        ncad = len(dfcent0)
        med = dfcent0.median()
        dfcent0['dist'] = (
            (dfcent0['centx'] - med['centx'])**2 +
            (dfcent0['centy'] - med['centy'])**2
            )
        dfcent0 = dfcent0.iloc[ncad/4:-ncad/4]
        dfcent0 = dfcent0.dropna(subset=['centx','centy'])
        iref = dfcent0['dist'].idxmin()
    
    print "using reference frame %i" % iref
    assert np.isnan(cent0['centx'][iref])==False,\
        "Must select a valid reference cadence. No nans"

    cent = np.zeros((nstars,cent0.shape[0]), cent0.dtype)
    for i,fitsfile in enumerate(fitsfiles):
        if (i%10)==0:
            print i
        cent[i] = fits_to_chip_centroid(fitsfile)
        channel_i = get_channel(fitsfile)
        assert channel==channel_i,"%i != %i" % (channel, channel_i)

    trans,pnts = imtran.linear_transform(cent['centx'],cent['centy'],iref)
    trans = pd.DataFrame(trans)
    trans = pd.concat([trans,pd.DataFrame(LE(cent0))[['t','cad']]],axis=1)
    trans = trans.to_records(index=False)

    keys = cent.dtype.names
    pnts = mlab.rec_append_fields(pnts,keys,[cent[k] for k in keys])

    if h5file!=None:
        with h5plus.File(h5file) as h5:
            h5['trans'] = trans
            h5['pnts'] = pnts
            
    trans,pnts = read_channel_transform(h5file)
    plot_trans(trans, pnts)
    figpath = h5file[:-3] + '.png'
    plt.gcf().savefig(figpath)
    print "saving %s " % figpath
    return cent
Example #10
0
def giveAvgStdofDicts(ShamDict, HFDict, MI_DDict, MI_MDict, MI_PDict):
    ### make a big dictionary to iterate through
    results = {
        'Sham': ShamDict,
        'HF': HFDict,
        'MI_D': MI_DDict,
        'MI_M': MI_MDict,
        'MI_P': MI_PDict
    }
    ### make dictionaries to store results
    avgDict = {}
    stdDict = {}
    for model, dictionary in results.iteritems():
        ### make holders to store results
        angleAvgs = []
        angleStds = []
        for name, angleCounts in dictionary.iteritems():
            print(name)
            if 'angle' not in name:
                continue
            angleAvgs.append(np.mean(angleCounts))
            angleStds.append(np.std(angleCounts))
            print("Average striation angle:", angleAvgs[-1])
            print("Standard deviation of striation angle:", angleStds[-1])
        avgDict[model] = angleAvgs
        stdDict[model] = angleStds

    ### Normalize Standard Deviations to Sham Standard Deviation
    ShamAvgStd = np.mean(stdDict['Sham'])
    stdStdDev = {}
    for name, standDev in stdDict.iteritems():
        standDev = np.asarray(standDev, dtype=float) / ShamAvgStd
        stdDict[name] = np.mean(standDev)
        stdStdDev[name] = np.std(standDev)

    ### Make bar chart for angles
    # need to have results in ordered arrays...
    names = ['Sham', 'HF', 'MI_D', 'MI_M', 'MI_P']
    avgs = []
    stds = []
    for name in names:
        avgs.append(avgDict[name])
        stds.append(stdDict[name])
    width = 0.25
    N = 1
    indices = np.arange(N) + width
    fig, ax = plt.subplots()
    for i, name in enumerate(names):
        ax.bar(indices + i * width,
               stdDict[name],
               width,
               yerr=stdStdDev[name],
               ecolor='k',
               alpha=0.5)
    ax.set_ylabel("Average Angle Standard Deviation Normalized to Sham")
    xtickLocations = np.arange(len(names)) * width + width * 3. / 2.
    ax.set_xticks(xtickLocations)
    ax.set_xticklabels(names, rotation='vertical')
    plt.gcf().savefig("Whole_Dataset_Angles.pdf", dpi=300)
Example #11
0
def plotStateSeq(jobname, showELBOInTitle=1, **kwargs):
  global dataName, StateColorMap
  if 'cmap' not in kwargs:
      kwargs['cmap'] = StateColorMap
  axes, zBySeq = bnpy.viz.SequenceViz.plotSingleJob(dataName, jobname,
      showELBOInTitle=showELBOInTitle, **kwargs)
  pylab.gcf().set_size_inches(ZW, ZH);
  return axes
Example #12
0
def plot_sun_image(img, filename, wavelength=193, title = ''):
    #cmap = plt.get_cmap('sdoaia{}'.format(wavelength))
    cmap = plt.get_cmap('sohoeit195')
    plt.title(title)
    cax = plt.imshow(img,cmap=cmap,origin='lower',vmin=0, vmax=3000)#,vmin=vmin, vmax=vmax)
    plt.gcf().colorbar(cax)
    plt.savefig(filename)
    plt.close("all")
Example #13
0
 def display(self):
     plt.figure(figsize=(8, 8))
     ax = plt.subplot(aspect='equal')
     plt.gcf().gca().invert_yaxis()  ## to plot in the usual way for images
     plt.axis('off')
     self.plotEdges()
     self.plotNodes()
     plt.show()
Example #14
0
def myplot(img, fileName=None, clim=None):
    plt.axis('equal')
    plt.pcolormesh(img, cmap='gray')
    plt.colorbar()
    if fileName != None:
        plt.gcf().savefig(fileName, dpi=300)
    if clim != None:
        plt.clim(clim)
Example #15
0
def channel_transform(fitsfiles, h5file, iref=None):
    """
    Channel Transformation

    Take a list of k2 pixel files (must be from the same
    channel). Find the centroids of each image and solve for the
    linear transformation that takes one scene to another
    """
    nstars = len(fitsfiles)

    # Pull the first file to get length and data type
    fitsfile0 = fitsfiles[0]
    cent0 = fits_to_chip_centroid(fitsfile0)
    channel = get_channel(fitsfile0)
    print "Using channel = %i" % channel

    # Determine the refence frame
    if iref == None:
        dfcent0 = pd.DataFrame(LE(cent0))
        ncad = len(dfcent0)
        med = dfcent0.median()
        dfcent0['dist'] = ((dfcent0['centx'] - med['centx'])**2 +
                           (dfcent0['centy'] - med['centy'])**2)
        dfcent0 = dfcent0.iloc[ncad / 4:-ncad / 4]
        dfcent0 = dfcent0.dropna(subset=['centx', 'centy'])
        iref = dfcent0['dist'].idxmin()

    print "using reference frame %i" % iref
    assert np.isnan(cent0['centx'][iref])==False,\
        "Must select a valid reference cadence. No nans"

    cent = np.zeros((nstars, cent0.shape[0]), cent0.dtype)
    for i, fitsfile in enumerate(fitsfiles):
        if (i % 10) == 0:
            print i
        cent[i] = fits_to_chip_centroid(fitsfile)
        channel_i = get_channel(fitsfile)
        assert channel == channel_i, "%i != %i" % (channel, channel_i)

    trans, pnts = imtran.linear_transform(cent['centx'], cent['centy'], iref)
    trans = pd.DataFrame(trans)
    trans = pd.concat([trans, pd.DataFrame(LE(cent0))[['t', 'cad']]], axis=1)
    trans = trans.to_records(index=False)

    keys = cent.dtype.names
    pnts = mlab.rec_append_fields(pnts, keys, [cent[k] for k in keys])

    if h5file != None:
        with h5plus.File(h5file) as h5:
            h5['trans'] = trans
            h5['pnts'] = pnts

    trans, pnts = read_channel_transform(h5file)
    plot_trans(trans, pnts)
    figpath = h5file[:-3] + '.png'
    plt.gcf().savefig(figpath)
    print "saving %s " % figpath
    return cent
Example #16
0
def DisplayFit(simulation,
               odeModel=None,
               jobDuration=30e3,
               tsteps=None,
               fixedParamDict=None,
               results=None,
               output_dir="."):
    print("Running demo with new parameters for comparison against truth")

    # run job with best parameters
    outputList = results['outputList']
    varDict = results[
        'bestFitDict']  # {variedParamKey: results['bestFitParam']}
    jobDict = {
        'simulation': simulation,
        'odeModel': odeModel,
        'varDict': varDict,
        'fixedParamDict': fixedParamDict,
        'jobNum': 0,
        'jobDuration': jobDuration,
        'tsteps': tsteps,
        'outputList': results['outputList']
    }
    dummy, workerResults = workerParams(jobDict,
                                        skipProcess=True,
                                        verbose=True)

    # cludgy way of plotting result
    for key in results['outputList'].keys():
        1
    #key = outputList.keys()[0]
    obj = outputList[key]
    testStateName = obj.odeKeyName
    data = workerResults.outputResults
    dataSub = analyze.GetData(data, testStateName)

    plt.figure()
    ts = dataSub.t
    plt.plot(ts, dataSub.valsIdx, label="pred")
    #print("SDF",obj.truthValue)
    #print(obj.timeInterpolations)
    #print( isinstance( None, np.ndarray ) ) # obj.timeInterpolations,np.ndarray))
    #if isinstance( obj.timeInterpolations,np.ndarray):
    #print(np.size(obj.timeInterpolations))

    if np.size(obj.timeInterpolations) > 1:
        plt.scatter(obj.timeInterpolations, obj.truthValue, label="truth")
    else:
        plt.plot([np.min(ts), np.max(ts)], [obj.truthValue, obj.truthValue],
                 'r--',
                 label="truth")

    plt.title(testStateName)
    plt.legend(loc=0)
    file_path = os.path.join(output_dir, testStateName + ".png")
    plt.gcf().savefig(file_path)

    return data
def XGB_native(train,test,features,features_non_numeric):
    depth = 13
    eta = 0.01
    ntrees = 8000
    mcw = 3
    params = {"objective": "reg:linear",
              "booster": "gbtree",
              "eta": eta,
              "max_depth": depth,
              "min_child_weight": mcw,
              "subsample": 0.9,
              "colsample_bytree": 0.7,
              "silent": 1
              }
    print "Running with params: " + str(params)
    print "Running with ntrees: " + str(ntrees)
    print "Running with features: " + str(features)

    # Train model with local split
    tsize = 0.05
    X_train, X_test = cross_validation.train_test_split(train, test_size=tsize)
    dtrain = xgb.DMatrix(X_train[features], np.log(X_train[goal] + 1))
    dvalid = xgb.DMatrix(X_test[features], np.log(X_test[goal] + 1))
    watchlist = [(dvalid, 'eval'), (dtrain, 'train')]
    gbm = xgb.train(params, dtrain, ntrees, evals=watchlist, early_stopping_rounds=100, feval=rmspe_xg, verbose_eval=True)
    train_probs = gbm.predict(xgb.DMatrix(X_test[features]))
    indices = train_probs < 0
    train_probs[indices] = 0
    error = rmspe(np.exp(train_probs) - 1, X_test[goal].values)
    print error

    # Predict and Export
    test_probs = gbm.predict(xgb.DMatrix(test[features]))
    indices = test_probs < 0
    test_probs[indices] = 0
    submission = pd.DataFrame({myid: test[myid], goal: np.exp(test_probs) - 1})
    if not os.path.exists('result/'):
        os.makedirs('result/')
    submission.to_csv("./result/dat-xgb_d%s_eta%s_ntree%s_mcw%s_tsize%s.csv" % (str(depth),str(eta),str(ntrees),str(mcw),str(tsize)) , index=False)
    # Feature importance
    if plot:
      outfile = open('xgb.fmap', 'w')
      i = 0
      for feat in features:
          outfile.write('{0}\t{1}\tq\n'.format(i, feat))
          i = i + 1
      outfile.close()
      importance = gbm.get_fscore(fmap='xgb.fmap')
      importance = sorted(importance.items(), key=operator.itemgetter(1))
      df = pd.DataFrame(importance, columns=['feature', 'fscore'])
      df['fscore'] = df['fscore'] / df['fscore'].sum()
      # Plotitup
      plt.figure()
      df.plot()
      df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(25, 15))
      plt.title('XGBoost Feature Importance')
      plt.xlabel('relative importance')
      plt.gcf().savefig('Feature_Importance_xgb_d%s_eta%s_ntree%s_mcw%s_tsize%s.png' % (str(depth),str(eta),str(ntrees),str(mcw),str(tsize)))
Example #18
0
def plot(*args):
    # Create a new figure
    new_figure = plt.figure()
    axes = new_figure.add_subplot(111)
    axes.plot(*args)
    # axes.set_xlabel('Trial')
    # axes.set_ylabel('Latency')
    plt.grid()
    plt.gcf().show()
Example #19
0
def showDecomposition(values, components=None, title=None):
    """
    Show the decomposition of values into principal basis vectors (obtained
    through sigular value decomposition). The component axes can be passed
    in, or new ones can be created from values themselves if nothing is
    passed in.
    """
    MAX_SVS_TO_SHOW = 10

    if components is None:
        # Decompose the weight sequence into its principal components
        components = np.linalg.svd(values, full_matrices=False)

        # Get the corresponding singular values
        singular_values = components[1]
        n_singular_values = len(singular_values)
        svs_to_show = min(MAX_SVS_TO_SHOW, n_singular_values)
        # print(singular_values)

        # Plot the singular values
        plt.figure()
        plt.scatter(range(1, 1 + svs_to_show),
                    singular_values[:svs_to_show],
                    marker='s',
                    c='red',
                    alpha=0.8)
        plt.yscale('log')
        plt.ylabel('Singular Value')
        plt.grid()
        plt.gcf().show()
    else:
        singular_values = components[1]

    # Components
    major_component = components[0][:, 0] / np.linalg.norm(components[0][:, 0])
    minor_component = components[0][:, 1] / np.linalg.norm(components[0][:, 1])
    vector_norms = np.linalg.norm(values, axis=0)

    # Get the decomposition of the weight vectors into the constituents
    n_samples = np.shape(values)[1]
    transformed_values = [
        np.dot(major_component, values) / vector_norms,
        np.dot(minor_component, values) / vector_norms
    ]
    plt.figure()
    plt.scatter(transformed_values[0], transformed_values[1], c=range(n_samples), \
        cmap='viridis', marker='d', alpha=0.9)
    plt.xlabel('Major, SV - %.2f' % singular_values[0])
    plt.ylabel('Minor, SV - %.2f' % singular_values[1])
    plt.colorbar()
    plt.grid()

    if title is not None:
        plt.title(title)

    plt.gcf().show()
    return components
def plot_spectrogram(spec, path):
    spec = spec.transpose(1, 0) # (seq_len, feature_dim) -> (feature_dim, seq_len)
    plt.gcf().clear()
    plt.figure(figsize=(12, 3))
    plt.imshow(spec, aspect="auto", origin="lower")
    plt.colorbar()
    plt.tight_layout()
    plt.savefig(path, dpi=300, format="png")
    plt.close() 
def plot_embedding(spec, path):
    spec = spec.transpose(1, 0) # (seq_len, feature_dim) -> (feature_dim, seq_len)
    plt.gcf().clear()
    plt.figure(figsize=(12, 3))
    plt.pcolormesh(spec, norm=SymLogNorm(linthresh=1e-3))
    plt.colorbar()
    plt.tight_layout()
    plt.savefig(path, dpi=300, format="png")
    plt.close()
def plot_series(x, y_array, labels):
    for y_arr, label in zip(y_array, labels):
        plt.plot(x, y_arr, label=label)
        plt.xlabel('Datetime')
        plt.ylabel('Demand')
        plt.title('Models of demand using trends and ARMA')
    plt.gcf().set_size_inches(26,20)
    plt.legend()
    plt.show()
Example #23
0
def histogram(data):
    new_figure = plt.figure()
    axes = new_figure.add_subplot(111)
    axes.set_yscale("log", nonposy='clip')
    axes.hist(data)
    axes.set_xlabel('Bin')
    axes.set_ylabel('Instances')
    axes.grid(True)
    plt.gcf().show()
 def plot_function(self):
     plt.close()
     plt.ion()
     plt.gcf().canvas.set_window_title('Trigonometric function')
     plt.title("y=A*sin(Bx), where A = {}, and B ={}".format(
         self.A, self.B))
     plt.plot(super().x_range(), self.y_values())
     plt.xlabel("x values")
     plt.ylabel("y values")
     plt.draw()
Example #25
0
def draw(x, y, title='K value for kNN'):
    plt.plot(x, y, label='k value')
    plt.title(title)
    plt.xlabel('k')
    plt.ylabel('Score')
    plt.grid(True)
    plt.legend(loc='best', framealpha=0.5, prop={'size':'small'})
    plt.tight_layout(pad=1)
    plt.gcf().set_size_inches(8,4)
    plt.show()
Example #26
0
def plotHammingDistVsELBO(JDict, n='', **kwargs):
  names, paths = filterJDictForRunsWithELBO(JDict)
  bnpy.viz.PlotTrace.plotJobs(MakePaths(paths, n), names, MakeStyles(names),
                                     yvar='hamming-distance',
                                     xvar='evidence', 
                                     tickfontsize=tickfontsize, 
                                     density=1, **kwargs)
  pylab.ylim(Hlims); 
  pylab.yticks(Hticks);
  pylab.gcf().set_size_inches(W, H);
Example #27
0
def plotK(JDict, xscale='linear', n='', **kwargs):
  paths = JDict.values()
  names = JDict.keys()
  bnpy.viz.PlotTrace.plotJobs(MakePaths(paths,n), names, MakeStyles(names),
                                     yvar='K', tickfontsize=tickfontsize,
                                     density=1, **kwargs)
  set_xscale(xscale)
  pylab.ylim(Klims); pylab.yticks(Kticks);
  pylab.gca().yaxis.grid() # horizontal lines
  pylab.gcf().set_size_inches(W, H);
def plot(c, name, color):
    fig, ax = plt.subplots(1)
    plt.plot(np.arange(904), c, color=color)
    plt.xlabel('sequence position')
    plt.ylabel('value')
    ax.set_ylim([0, 1000])
    plt.gcf().subplots_adjust(bottom=0.15)
    plt.title('movement ' + name)
    plt.show()
    fig.savefig('./cluster' + name + '.png')
    def graph_all_and_not_removed():
        """ Graph fractures from all the families. If some fractures have been removed, also graph the fractures that remain after removal. 
                """
        numBuckets = 50
        allList = families['all']
        unRemovedList = families['notRemoved']
        minSize = min(allList)
        maxSize = max(allList)
        # If constant fracture size, increase max so histogram is not a delta function
        if minSize == maxSize:
            maxSize += 1.0
        twentiethOfWidth = (maxSize - minSize) * 0.05
        fig, ax = plt.subplots()
        histCount, bins, patches = plt.hist(
            allList,
            bins=numBuckets,
            color='b',
            range=(minSize, maxSize),
            alpha=0.7,
            label='All Fractures\n(Connected and Unconnected')

        binWidth = bins[1] - bins[0]
        figHeight = max(histCount) * 1.2  ## need room to show vals above bars
        ax.set_xticks(bins)
        ax.locator_params(tight=True, axis='x',
                          nbins=numBuckets / 5.0)  ## num of axis value labels
        ax.xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))

        ## if no fractures removed, there's no point in using the same 2 histograms,
        if len(allList) != len(unRemovedList):
            plt.hist(unRemovedList,
                     bins=numBuckets,
                     color='r',
                     range=(minSize, maxSize),
                     alpha=0.7,
                     label='Non-isolated fractures (connected)')

        ## Add y values above all non-zero histogram bars
        for count, x in zip(histCount, bins):
            if count != 0:
                ax.annotate(str(count),
                            xy=(x, count + (figHeight * 0.03)),
                            rotation='vertical',
                            va='bottom')

        plt.ylim(ymin=0, ymax=figHeight)
        plt.xlim(xmin=minSize - twentiethOfWidth,
                 xmax=maxSize + twentiethOfWidth)
        plt.gcf().subplots_adjust(right=0.98)
        plt.title("Fractures Sizes From All Families")
        plt.xlabel("Fracture Radius")
        plt.ylabel("Number of Fractures")
        plt.legend()
        plt.savefig(outputPDF, format='pdf')
        if show: plt.show()
Example #30
0
def df_to_emotion_histogram(df,
                            palette=plt.cm.Pastel1,
                            emotion_column='emotion',
                            verbose=False):
    """ Take a dataset like ArtEmis and return a histogram over the emotion choices made by the annotators.
    :param df: dataframe carrying dataset
    :param palette: matplotlib color palette, e.g., plt.cm.jet
    :param emotion_column: (str) indicate which column of the dataframe carries the emotion
    :return: a list carrying the resulting histogram figure.
    """
    hist_vals = []
    for emotion in ARTEMIS_EMOTIONS:
        hist_vals.append(sum(df[emotion_column] == emotion) / len(df))

    norm = plt.Normalize(min(hist_vals), max(hist_vals))
    colors = palette(norm(hist_vals))

    s = pd.DataFrame({"emotions": ARTEMIS_EMOTIONS, "vals": hist_vals})
    s.set_index("emotions", drop=True, inplace=True)
    plt.figure()
    s.index.name = None
    ax = s.plot.bar(grid=True,
                    figsize=(12, 4),
                    color=colors,
                    fontsize=16,
                    rot=45,
                    legend=False,
                    ec="k")
    ax.set_ylabel('Percentage of data', fontsize=15)

    for rec, col in zip(ax.patches, colors):
        rec.set_color(col)

    plt.tight_layout()
    res = [plt.gcf()]

    plt.figure()
    s = df[emotion_column].apply(positive_negative_else).value_counts() / len(
        df)

    if verbose:
        print('Pos-Neg-Else, percents:', s.round(3))

    ax = s.plot.bar(grid=True,
                    figsize=(8, 4),
                    fontsize=16,
                    rot=45,
                    legend=False,
                    color='gray')
    ax.set_xticklabels(['positive', 'negative', 'else'])
    plt.tight_layout()
    res.append(plt.gcf())

    return res
Example #31
0
def dataAnalysis(cid,schoolYear,semester) :

    host = '127.0.0.1'
    user = '******'
    password = '******'
    database = 'cims'
    port = 3306

    mysql = pymysql.connect(host=host,user = user,password=password,database=database,port=port)

    sql1 = "SELECT score,school_year,semester FROM tb_class_score_info WHERE cid = '" + cid +"' ORDER BY school_year,semester"

    sql2 = "SELECT b.indicator_name,a.score FROM tb_first_index_score a,tb_evaluation_template b WHERE a.first_indicator_id = b.indicator_id and school_year = '" + schoolYear + "' and semester = " + str(semester)

    print(sql2)
    cursor1 = mysql.cursor()
    cursor1.execute(sql1)
    #得到一个元组,元组中有两条数据,第一条为学年,第二条为学期
    result1 = cursor1.fetchall()
    cursor1.close()

    cursor2 = mysql.cursor()
    cursor2.execute(sql2)
    # 得到一个元组,元组中有两条数据,第一条为学年,第二条为学期
    result2 = cursor2.fetchall()
    cursor2.close()

    x1 = []
    y1 = []
    for i in range(len(result1)) :
        y1.append(result1[i][0])
        x1.append(result1[i][1] + '-' + str(result1[i][2]))
    x2 = []
    y2 = []
    for j in range(len(result2)) :
        x2.append(result2[j][0])
        y2.append(result2[j][1])

    mysql.close()
    #解决乱码问题
    pyl.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    pyl.rcParams['axes.unicode_minus'] = False
    pyl.subplot(1,2,1)#行,列,当前区域
    pyl.title('历史对比')
    pyl.xlabel('时间')
    pyl.ylabel('得分')
    pyl.plot(x1,y1)
    pyl.gcf().autofmt_xdate()
    pyl.subplot(1,2,2)
    pyl.title("指标得分")
    pyl.xlabel("指标名称")
    pyl.ylabel("得分")
    pyl.bar(x2,y2,align='center')
    pyl.show()
Example #32
0
def plot(c, name, color):
    fig, ax = plt.subplots(1)
    #plt.plot(np.arange(808), np.mean(c, axis=0), color=color)
    plt.scatter(np.arange(808), c, s=4, color=color)
    plt.xlabel('sequence position')
    plt.ylabel('average count')
    #ax.set_ylim([50, 210])
    ax.set_ylim([0, 400])
    plt.gcf().subplots_adjust(bottom=0.15)
    plt.title(name)
    plt.savefig('./cluster' + name + '.eps')
Example #33
0
def giveBarChartfromDict(dictionary, tag):
    ### instantiate lists to contain contents
    wtC = []
    ltC = []
    lossC = []
    for name, content in dictionary.iteritems():
        if "angle" in name:
            continue
        wtC.append(content[0])
        ltC.append(content[1])
        lossC.append(content[2])

    wtC = np.asarray(wtC)
    ltC = np.asarray(ltC)
    lossC = np.asarray(lossC)

    wtAvg = np.mean(wtC)
    ltAvg = np.mean(ltC)
    lossAvg = np.mean(lossC)

    wtStd = np.std(wtC)
    ltStd = np.std(ltC)
    lossStd = np.std(lossC)

    ### now make a bar chart from this
    colors = ["blue", "green", "red"]
    marks = ["WT", "LT", "Loss"]
    width = 0.25
    N = 1
    indices = np.arange(N) + width
    fig, ax = plt.subplots()
    rects1 = ax.bar(indices,
                    wtAvg,
                    width,
                    color=colors[0],
                    yerr=wtStd,
                    ecolor='k')
    rects2 = ax.bar(indices + width,
                    ltAvg,
                    width,
                    color=colors[1],
                    yerr=ltStd,
                    ecolor='k')
    rects3 = ax.bar(indices + 2 * width,
                    lossAvg,
                    width,
                    color=colors[2],
                    yerr=lossStd,
                    ecolor='k')
    ax.set_ylabel('Normalized Content')
    ax.legend(marks)
    ax.set_xticks([])
    ax.set_ylim([0, 1])
    plt.gcf().savefig(tag + '_BarChart.pdf', dpi=300)
Example #34
0
 def psquare(self, x, y, col, full=False):
     if full:
         plt.gcf().gca().add_artist(
             plt.Rectangle((x, y), self.Size, self.Size, ec=col, color=col))
     else:
         plt.gcf().gca().add_artist(
             plt.Rectangle((x, y),
                           self.Size,
                           self.Size,
                           ec=col,
                           color='white'))
    def graph_rejections():
        """
                Graph the fractures that were rejected by the Feature Rejection Algorithm for Meshing (FRAM) in dfnGen, using a histogram of rejection reasons.
	        Rejection File line format:   "118424 Short Intersections" --> {"Short Intersections": 118424}
                """
        rejects = {}
        plt.subplots()

        for line in open(jobname + rejectFile):
            num = int(line[:line.index(" ", 0)]
                      )  ## number comes before first space in line
            name = line[line.index(" ", 0) +
                        1:].strip()  ## name comes after first space
            midSpaceIndex = 1
            while midSpaceIndex < len(name) / 2 and " " in name[
                    midSpaceIndex + 1:]:  ## cut long names in half
                midSpaceIndex = name.index(" ", midSpaceIndex + 1)
            name = name[:midSpaceIndex] + "\n" + name[midSpaceIndex + 1:]
            rejects[name] = num

        totalRejects = float(sum(rejects.values()))
        figWidth = max(
            rejects.values()) * 1.25  ## make width 25% bigger than biggest val
        labelOffset = figWidth * 0.02 if figWidth != 0 else 0.05
        offset = 2
        h = 0.35  # height of horiz bar (vertical thickness)

        horizBar = plt.barh(np.arange(len(rejects)) + offset,
                            rejects.values(),
                            height=h,
                            align='center')
        plt.yticks(np.arange(len(rejects)) + offset,
                   rejects.keys(),
                   fontsize=6)
        plt.title("Rejection Reasons", fontsize=18)
        plt.xlim(xmin=0, xmax=figWidth if figWidth != 0 else 1)

        for bar in horizBar:
            width = bar.get_width()
            if width != 0:
                label = '{0:d}\n{1:.2f}%'.format(int(width),
                                                 width / totalRejects * 100)
            else:
                label = 0
            plt.text(bar.get_width() + labelOffset,
                     bar.get_y() + h / 2.0,
                     label,
                     va='center',
                     fontsize=10)

        plt.gcf().subplots_adjust(right=0.98)
        plt.savefig(outputPDF, format='pdf')
        if show: plt.show()
def matplotlib_make_figure(figsize=(10,7), style='seaborn-dark'):
    try:
        plt.style.use(style)
    except ValueError:
        warning(" matplotlib style %s not found." % style)
        pass

    fig=plt.figure('scatter3d', figsize)
    plt.gcf().set_tight_layout(True)
    ax=fig.add_subplot(111,projection='3d')

    return fig, ax
Example #37
0
def visualization(true_label, guess_label):
    """
        可视化统计分析
    Args:
        true_label: 测试样本真实标签序列
        guess_label: 测试样本预测标签序列
    returns:
        None
    """
    
    plt.clf()
    plt.gcf().set_size_inches(16, 9)    
    
    # 整体预判概率分布
    plt.subplot(2, 2, 1)
    plt.hist(guess_label, bins=50, color='green', weights=np.ones_like(guess_label) / len(guess_label))
    plt.grid()
    plt.xlabel(u'预测概率')
    plt.ylabel(u'用户占比')    
    plt.title(u'整体预判概率分布')
           
    # ROC曲线
    fpr, tpr, _ = roc_curve(true_label, guess_label)
    plt.subplot(2, 2, 2)
    plt.plot(fpr, tpr, label='ROC Curve')
    plt.plot([0, 1], [0, 1], 'y--')
    plt.grid()
    plt.xlabel('False positive rate')
    plt.ylabel('True positive rate')
    plt.title(u'ROC曲线')
           
    # 正负类别概率分布
    plt.subplot(2, 2, 3)
    plt.hist(guess_label[true_label == 1], bins=50, color='blue',
               weights=np.ones_like(guess_label[true_label == 1]) / len(guess_label[true_label == 1]), label='Bad users')
    plt.hist(guess_label[true_label == 0], bins=50, color='green', alpha=0.8,
               weights=np.ones_like(guess_label[true_label == 0]) / len(guess_label[true_label == 0]), label='Good users')
    plt.grid()
    plt.xlabel(u'预测概率')
    plt.ylabel(u'用户占比')
    plt.title(u'正负类别概率分布')
    plt.legend(loc='best')
           
    # 概率累积分布
    plt.subplot(2, 2, 4)
    cumulative_1, _, cumu_delta = cumulation(true_label, guess_label)
    plt.plot(cumulative_1[1][1:], cumu_delta, color='red', label='KS Curve')
    plt.grid()
    plt.title(u'概率累积分布')
    plt.xlabel(u'预测概率')
    plt.ylabel(u'累积占比')
    plt.legend(loc='upper left')
    plt.savefig(workspace() + '/evaluate.png', dpi=100)
Example #38
0
def plotELBO(JDict, xscale='linear', n='', **kwargs):
  names, paths = filterJDictForRunsWithELBO(JDict)
  bnpy.viz.PlotTrace.plotJobs(MakePaths(paths,n), names, MakeStyles(names),
                                     yvar='evidence', tickfontsize=tickfontsize,
                                     density=1, **kwargs)
  set_xscale(xscale)
  if ELBOlims is not None:
      pylab.ylim(ELBOlims);
  if ELBOticks is not None:
      pylab.yticks(ELBOticks);
  pylab.gca().yaxis.grid() # horizontal lines
  pylab.gcf().set_size_inches(W, H);
Example #39
0
def matplotlib_make_figure(figsize=(10, 7), style='seaborn-dark'):
    try:
        plt.style.use(style)
    except ValueError:
        warning(" matplotlib style %s not found." % style)
        pass

    fig = plt.figure('scatter3d', figsize)
    plt.gcf().set_tight_layout(True)
    ax = fig.add_subplot(111, projection='3d')

    return fig, ax
Example #40
0
def plot_training(log_every, train_loss, val_loss, title, ylabel):
    clear_output(wait=False)
    plt.gcf().clear()
    iters = np.arange(0, len(train_loss)) * log_every
    iters_val = np.arange(0, len(val_loss)) * log_every
    #train_plot, = plt.plot(iters, train_loss, 'r', label="training")
    val_plot, = plt.plot(iters, val_loss, 'b', label="validation")

    plot_learning([val_plot], ylabel)
    plt.title(title)

    display(plt.gcf())
Example #41
0
def plot_data(tag):
    data_array = tag.references[0]
    voltage = np.zeros(data_array.data.shape)
    data_array.data.read_direct(voltage)

    x_axis = data_array.dimensions[0]
    time = x_axis.axis(data_array.data_extent[0])

    spike_times = tag.positions[:]

    feature_data_array = tag.features[0].data
    snippets = tag.features[0].data[:]

    single_snippet = tag.feature_data(3, 0)[:]

    snippet_time_dim = feature_data_array.dimensions[1]
    snippet_time = snippet_time_dim.axis(feature_data_array.data_extent[1])

    response_axis = plt.subplot2grid((2, 2), (0, 0), rowspan=1, colspan=2)
    single_snippet_axis = plt.subplot2grid((2, 2), (1, 0), rowspan=1, colspan=1)
    average_snippet_axis = plt.subplot2grid((2, 2), (1, 1), rowspan=1, colspan=1)

    response_axis.plot(time, voltage, color='dodgerblue', label=data_array.name)
    response_axis.scatter(spike_times, np.ones(spike_times.shape)*np.max(voltage), color='red', label=tag.name)
    response_axis.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else ""))
    response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
    response_axis.set_title(data_array.name)
    response_axis.set_xlim(0, np.max(time))
    response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage)))
    response_axis.legend(ncol=2, loc="lower center", fontsize=8)

    single_snippet_axis.plot(snippet_time, single_snippet.T, color="red", label=("snippet No 4"))
    single_snippet_axis.set_xlabel(snippet_time_dim.label + ((" [" + snippet_time_dim.unit + "]") if snippet_time_dim.unit else ""))
    single_snippet_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else ""))
    single_snippet_axis.set_title("single stimulus snippet")
    single_snippet_axis.set_xlim(np.min(snippet_time), np.max(snippet_time))
    single_snippet_axis.set_ylim((1.2 * np.min(snippets[3,:]), 1.2 * np.max(snippets[3,:])))
    single_snippet_axis.legend()

    mean_snippet = np.mean(snippets, axis=0)
    std_snippet = np.std(snippets, axis=0)
    average_snippet_axis.fill_between(snippet_time, mean_snippet + std_snippet, mean_snippet - std_snippet, color="tab:red", alpha=0.25)
    average_snippet_axis.plot(snippet_time, mean_snippet, color="red", label=(feature_data_array.name + str(4)))
    average_snippet_axis.set_xlabel(snippet_time_dim.label + ((" [" + snippet_time_dim.unit + "]") if snippet_time_dim.unit else ""))
    average_snippet_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else ""))
    average_snippet_axis.set_title("spike-triggered average")
    average_snippet_axis.set_xlim(np.min(snippet_time), np.max(snippet_time))
    average_snippet_axis.set_ylim((1.2 * np.min(mean_snippet - std_snippet), 1.2 * np.max(mean_snippet + std_snippet)))

    plt.subplots_adjust(left=0.15, top=0.875, bottom=0.1, right=0.98, hspace=0.35, wspace=0.25)
    plt.gcf().set_size_inches((5.5, 4.5))
    # plt.savefig("../images/spike_features.png")
    plt.show()
Example #42
0
def format_bar_chart(ax):
    plt.gcf().subplots_adjust(bottom=0.2)
    plt.gcf().subplots_adjust(left=0.2)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.set_xlabel('Head direction (deg)', fontsize=30)
    ax.set_ylabel('Frequency (Hz)', fontsize=30)
    ax.xaxis.set_tick_params(labelsize=20)
    ax.yaxis.set_tick_params(labelsize=20)
    return ax
def format_bar_chart(ax, x_label, y_label):
    plt.gcf().subplots_adjust(bottom=0.2)
    plt.gcf().subplots_adjust(left=0.2)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.set_xlabel(x_label, fontsize=25)
    ax.set_ylabel(y_label, fontsize=25)
    ax.xaxis.set_tick_params(labelsize=20)
    ax.yaxis.set_tick_params(labelsize=20)
    return ax
Example #44
0
def plotStateSeq(jobname, showELBOInTitle=1, xticks=None, **kwargs):
  global dataName, StateColorMap
  if 'cmap' not in kwargs:
      kwargs['cmap'] = StateColorMap
  axes, zBySeq = bnpy.viz.SequenceViz.plotSingleJob(dataName, jobname,
      showELBOInTitle=showELBOInTitle, **kwargs)
  pylab.subplots_adjust(top=0.85, bottom=0.1);
  axes[-1].tick_params(axis='both', which='major', labelsize=20)
  if xticks is not None:
      axes[-1].set_xticks(xticks);
  pylab.gcf().set_size_inches(ZW, ZH);
  pylab.draw();
  return axes
def plot_episode(args):
    """Plot an episode plucked from the large h5 database"""
    print "plot_episode"
    # load the data file
    tblfilename = "bf_optimize_mavlink.h5"
    h5file = tb.open_file(tblfilename, mode = "a")
    # get the table handle
    table = h5file.root.v2.evaluations

    # selected episode
    episode_row = table.read_coordinates([int(args.epinum)])
    # compare episodes
    episode_row_1 = table.read_coordinates([2, 3, 22, 46]) # bad episodes
    print "row_1", episode_row_1.shape
    # episode_row = table.read_coordinates([3, 87])
    episode_target = episode_row["alt_target"]
    episode_target_1 = [row["alt_target"] for row in episode_row_1]
    print "episode_target_1.shape", episode_target_1
    episode_timeseries = episode_row["timeseries"][0]
    episode_timeseries_1 = [row["timeseries"] for row in episode_row_1]
    print "row", episode_timeseries.shape
    print "row_1", episode_timeseries_1

    sl_start = 0
    sl_end = 2500
    sl_len = sl_end - sl_start
    sl = slice(sl_start, sl_end)
    pl.plot(episode_timeseries[sl,1], "k-", label="alt", lw=2.)
    print np.array(episode_timeseries_1)[:,:,1]
    pl.plot(np.array(episode_timeseries_1)[:,:,1].T, "k-", alpha=0.2)
    # alt_hold = episode_timeseries[:,0] > 4
    alt_hold_act = np.where(episode_timeseries[sl,0] == 11)
    print "alt_hold_act", alt_hold_act[0].shape, sl_len
    alt_hold_act_min = np.min(alt_hold_act)
    alt_hold_act_max = np.max(alt_hold_act)
    print "min, max", alt_hold_act_min, alt_hold_act_max, alt_hold_act_min/float(sl_len), alt_hold_act_max/float(sl_len),

    # pl.plot(episode_timeseries[sl,0] * 10, label="mode")
    pl.axhspan(-100., 1000,
               alt_hold_act_min/float(sl_len),
               alt_hold_act_max/float(sl_len),
               facecolor='0.5', alpha=0.25)
    pl.axhline(episode_target, label="target")
    pl.xlim((0, sl_len))
    pl.xlabel("Time steps [1/50 s]")
    pl.ylabel("Alt [cm]")
    pl.legend()
    if args.plotsave:
        pl.gcf().set_size_inches((10, 3))
        pl.gcf().savefig("%s.pdf" % (sys.argv[0][:-3]), dpi=300, bbox_inches="tight")
    pl.show()
Example #46
0
def _plot_eigenvalues(figure_id, model, figure_size, x_scale, y_scale):
    r"""
    Helper function that plots a model's eigenvalues.

    Parameters
    -----------
    figure_id : matplotlib.pyplot.Figure instance
        The handle of the figure to be saved.

    model : :map:`PCAModel` or subclass
       The model to be used.

    figure_size : (`int`, `int`)
        The size of the plotted figures.

    x_scale : `float`
        The scale of x axis.

    y_scale : `float`
        The scale of y axis.
    """
    # select figure
    figure_id = plt.figure(figure_id.number)

    # plot eigenvalues ratio
    plt.subplot(211)
    plt.bar(range(len(model.eigenvalues_ratio())),
            model.eigenvalues_ratio())
    plt.ylabel('Variance Ratio')
    plt.xlabel('Component Number')
    plt.title('Variance Ratio per Eigenvector')
    plt.grid("on")

    # plot eigenvalues cumulative ratio
    plt.subplot(212)
    plt.bar(range(len(model.eigenvalues_cumulative_ratio())),
            model.eigenvalues_cumulative_ratio())
    plt.ylim((0., 1.))
    plt.ylabel('Cumulative Variance Ratio')
    plt.xlabel('Component Number')
    plt.title('Cumulative Variance Ratio')
    plt.grid("on")

    # set figure size
    #plt.gcf().tight_layout()
    plt.gcf().set_size_inches([x_scale, y_scale] * np.asarray(figure_size))

    plt.show()

    return figure_id
Example #47
0
def plot_p(frame,file_prefix='claw_p',path='./_output/_p',plot_slices=True,plot_pcolor=True,slices_limits=None,xshift=0.0,name='',title=True):
    sol_ref=Solution(frame+450,file_format='petsc',read_aux=False,path='_output/reference/_p/',file_prefix=file_prefix)
    sol=Solution(frame,file_format='petsc',read_aux=False,path=path,file_prefix=file_prefix)
    x=sol.state.grid.x.centers; y=sol.state.grid.y.centers
    x=x+xshift
    mx=len(x); my=len(y)    
    yy,xx = np.meshgrid(y,x)

    if frame < 10:
        str_frame = "00"+str(frame)
    elif frame < 100:
        str_frame = "0"+str(frame)
    else:
        str_frame = str(frame)

    p=sol.state.q[0,:,:]
    p_ref=sol_ref.state.q[0,:,:]

    if plot_pcolor:
        pl.pcolormesh(xx,yy,p,cmap=cm.OrRd)
        pl.title("t= "+str(sol.state.t),fontsize=20)
        pl.xlabel('x',fontsize=20); pl.ylabel('y',fontsize=20)
        pl.xticks(size=20); pl.yticks(size=20)
        cb = pl.colorbar();
        #pl.clim(colorbar_min,colorbar_max);
        imaxes = pl.gca(); pl.axes(cb.ax)
        pl.yticks(fontsize=20); pl.axes(imaxes)
        pl.axis([np.min(x),np.max(x),np.min(y),np.max(y)])
        #pl.axis([0.25,60.25,0.25,60.25])
        pl.savefig('./_plots_to_paper/co-interaction_'+str_frame+name+'.png')
        #pl.show()                            
        pl.close()
    if plot_slices:
        pl.figure(figsize=(8,3))
        pl.gcf().subplots_adjust(left=0.10)
        # plot reference
        pl.plot(x,p_ref[:,my/4.],'--b',linewidth=1)
        pl.plot(x,p_ref[:,3*my/4.],'--r',linewidth=1)
        # plot solution of interaction
        pl.plot(x,p[:,3*my/4.],'-r',linewidth=2)
        pl.plot(x,p[:,my/4.],'-b',linewidth=2)
        pl.title("t= "+str(sol.state.t),fontsize=20)
        pl.xlabel('x',fontsize=20)
        if title:
            pl.ylabel('Stress',fontsize=20)
        pl.xticks(size=20); pl.yticks(size=20)
        if slices_limits is not None:
            pl.axis([slices_limits[0]+xshift,slices_limits[1]+xshift,slices_limits[2],slices_limits[3]])
        pl.savefig('./_plots_to_paper/co-interaction_'+str_frame+name+'.eps')
        pl.close()
def main():
    # Process input data
    #json_data=open('C:/Users/rthomas/Documents/DemandPrediction/demand_prediction.json')
    json_data = open(sys.argv[1])
    x, y, last, total_hours = process_input(json_data)
    FUTURE_DAYS = 15  # will make prediciton 15 days into future

    # I looked at a few different regression families in sm.GLM but found very similar rms errors so I chose to use a simple linear regression
    trend_model = sm.OLS(y, sm.add_constant(range(len(x)), prepend=True)).fit()
    trend = trend_model.fittedvalues

    # y1 is y with the trend line (growth over time) removed
    y1 = y - trend
    
    # y2 is y1 with hour-of-week trends removed
    hours = [w.hour + 24*w.weekday() for w in x]
    hours_mean = [np.mean([y1[i] for i in range(len(y1)) if hours[i] == k]) for k in range(7*24)]
    y2 = [y1[i] - hours_mean[hours[i]] for i in range(len(y1))]

    trend_y = [hours_mean[hours[i]] + trend[i] for i in range(len(trend))]
    
    future_hours = FUTURE_DAYS*24 + total_hours
    future_trend = trend_model.predict(sm.add_constant(range(total_hours, future_hours), prepend=True))
    future_x = [last + datetime.timedelta(hours=k) for k in range(1,FUTURE_DAYS*24+1)]
    future_hours = [w.hour + 24*w.weekday() for w in future_x]
    future_hours_trend = [hours_mean[future_hours[i]] for i in range(len(future_x))]
    future_y = [sum(pair) for pair in zip(future_trend, future_hours_trend)] 
    
    plt.plot(x, y, label='Original Time Series')
    plt.plot(x + future_x, trend_y + future_y, label='Model')
    plt.xlabel('Datetime')
    plt.ylabel('Demand')
    plt.title('Original data and model of demand')
    plt.gcf().set_size_inches(26,20)
    plt.legend()
    plt.show()
            
    app = flask.Flask(__name__)
    app.run()
    
    @app.route('/api/prediction')
    def predictcsv():
        filename = 'prediction.csv'
        data = [[x, y] for (x,y) in zip(future_x, future_y)]
        lines = csv2string(data)
        resp = flask.Response(lines, status=200, mimetype='text/csv')
        resp.headers['Content-Disposition'] = 'attachment; filename=' + filename
        return resp 
Example #49
0
def plotELBO(JDict, xscale='linear', n='', **kwargs):
  names, paths = filterJDictForRunsWithELBO(JDict)
  bnpy.viz.PlotTrace.plotJobs(MakePaths(paths,n), names, MakeStyles(names),
                                     yvar='evidence', tickfontsize=tickfontsize,
                                     density=1, **kwargs)
  set_xscale(xscale)
  if ELBOlims is not None:
      pylab.ylim(ELBOlims);
  if ELBOticks is not None:
      pylab.yticks(ELBOticks);
      if np.max(np.abs(ELBOticks)) < 0.1:
          print 'Amplifying tick labels by 100'
          pylab.gca().set_yticklabels(100 * np.asarray(ELBOticks))
          pylab.ylabel('objective (x100)')
  pylab.gca().yaxis.grid() # horizontal lines
  pylab.gcf().set_size_inches(W, H);
Example #50
0
def plotPhot(label=False):
    """ Plot the SN Camille Med Band Photometry on a color-color diagram
    """
    m7 = 25.813 ; dm7 = 0.142
    m8 = 25.430 ; dm8 = 0.161
    mI = 25.359 ; dmI = 0.090
    mP = 25.593 ; dmP = 0.117
    mN = 25.142 ; dmN = 0.052

    # F763M - F814W = 7 - I
    c7I = m7-mI
    c7Ierr = np.sqrt( dm7**2 + dmI**2)

    # F845M - F814W = 8 - I
    c8I = m8-mI
    c8Ierr = np.sqrt( dm8**2 + dmI**2)

    # F139M - F140W = P - N
    cPN = mP-mN
    cPNerr = np.sqrt( dmP**2 + dmN**2)

    print("F763M-F814W = %.2f +- %.2f"%(c7I,c7Ierr))
    print("F845M-F814W = %.2f +- %.2f"%(c8I,c8Ierr))
    print("F139M-F140W = %.2f +- %.2f"%(cPN,cPNerr))
    fig = pl.gcf()
    ax1 = fig.add_subplot(2,2,1)
    ax1.errorbar( c7I, cPN, cPNerr, c7Ierr, color='k', marker='o', ms=15, capsize=0 )
    ax2 = fig.add_subplot(2,2,2)
    ax2.errorbar( c8I, cPN, cPNerr, c8Ierr, color='k', marker='o', ms=15, capsize=0 )
    ax3 = fig.add_subplot(2,2,3)
    ax3.errorbar( c7I, c8I, c8Ierr, c7Ierr, color='k', marker='o', ms=15, capsize=0 )

    if label :
        ax2.text( 0.95,0.95,'GND13Cam', transform=ax2.transAxes, ha='right',va='top',fontsize='large')
Example #51
0
def get_histogram_scale(distances_dict, nbins):
    """Draws histogram to outfile_name.
    """
    scale_dict = defaultdict(list)
    #draw histograms
    for d_dict in distances_dict.values():
        for i, (field, data) in enumerate(d_dict.items()):
            if len(data) < 1:
                continue
            histogram = hist(data,bins=nbins)
            
            fig  = gcf()
            axis = fig.gca()

            #get height scale: y/x
            ymin,ymax = axis.get_ylim()
        
            xmin,xmax = axis.get_xlim()
            scale_dict['ymin'].append(ymin)
            scale_dict['ymax'].append(ymax)
            scale_dict['xmin'].append(xmin)
            scale_dict['xmax'].append(xmax)

            clf()
    
    yscale = (min(scale_dict['ymin']),max(scale_dict['ymax']))
    xscale = (min(scale_dict['xmin']),max(scale_dict['xmax']))
    
    return xscale,yscale
Example #52
0
def triple_plot(cccsum, cccsum_hist, trace, threshold, save=False,
                savefile=''):
    r"""Main function to make a triple plot with a day-long seismogram, \
    day-long correlation sum trace and histogram of the correlation sum to \
    show normality.

    :type cccsum: numpy.ndarray
    :param cccsum: Array of the cross-channel cross-correlation sum
    :type cccsum_hist: numpy.ndarray
    :param cccsum_hist: cccsum for histogram plotting, can be the same as \
        cccsum but included if cccsum is just an envelope.
    :type trace: obspy.Trace
    :param trace: A sample trace from the same time as cccsum
    :type threshold: float
    :param threshold: Detection threshold within cccsum
    :type save: bool, optional
    :param save: If True will svae and not plot to screen, vice-versa if False
    :type savefile: str, optional
    :param savefile: Path to save figure to, only required if save=True
    """
    if len(cccsum) != len(trace.data):
        print('cccsum is: ' +
              str(len(cccsum))+' trace is: '+str(len(trace.data)))
        msg = ' '.join(['cccsum and trace must have the',
                        'same number of data points'])
        raise ValueError(msg)
    df = trace.stats.sampling_rate
    npts = trace.stats.npts
    t = np.arange(npts, dtype=np.float32) / (df * 3600)
    # Generate the subplot for the seismic data
    ax1 = plt.subplot2grid((2, 5), (0, 0), colspan=4)
    ax1.plot(t, trace.data, 'k')
    ax1.axis('tight')
    ax1.set_ylim([-15 * np.mean(np.abs(trace.data)),
                  15 * np.mean(np.abs(trace.data))])
    # Generate the subplot for the correlation sum data
    ax2 = plt.subplot2grid((2, 5), (1, 0), colspan=4, sharex=ax1)
    # Plot the threshold values
    ax2.plot([min(t), max(t)], [threshold, threshold], color='r', lw=1,
             label="Threshold")
    ax2.plot([min(t), max(t)], [-threshold, -threshold], color='r', lw=1)
    ax2.plot(t, cccsum, 'k')
    ax2.axis('tight')
    ax2.set_ylim([-1.7 * threshold, 1.7 * threshold])
    ax2.set_xlabel("Time after %s [hr]" % trace.stats.starttime.isoformat())
    # ax2.legend()
    # Generate a small subplot for the histogram of the cccsum data
    ax3 = plt.subplot2grid((2, 5), (1, 4), sharey=ax2)
    ax3.hist(cccsum_hist, 200, normed=1, histtype='stepfilled',
             orientation='horizontal', color='black')
    ax3.set_ylim([-5, 5])
    fig = plt.gcf()
    fig.suptitle(trace.id)
    fig.canvas.draw()
    if not save:
        plt.show()
        plt.close()
    else:
        plt.savefig(savefile)
    return
Example #53
0
def plot_matches_group(g,how='chi',ntop=8):
    """
    Plot matches from h5 group

    Pulls out the relavent arrays from a group and runs plot_matches
    
    Parameters
    ----------
    g : h5 group containing the following datasets
        - arr : DataSet with spectra
        - smres : DataSet with specmatch results
    
    """
    smres = pd.DataFrame(g['smres'][:])
    smres = results.smres_add_chi(smres)

    lspec = g['lspec'][:]
    smres.index = np.arange(len(smres))

    if how=='chi':
        smresbest = smres.sort_values(by='chi')
    elif how=='close':
        targname = smio.kbc_query(smres.targobs[0])['name']
        tpar = dict(lib.ix[targname])
        smres['close'] = close(tpar,smres)
        smresbest = smres.sort_values(by='close')
    smresbest = smresbest.iloc[:ntop]

    plot_matches(smresbest,lspec)
    plt.sca(plt.gcf().get_axes()[0])
Example #54
0
def doit():
    # test it out

    L = 1

    R = 10

    theta = np.radians(np.arange(0,361))

    # draw a circle
    plt.plot(R*np.cos(theta), R*np.sin(theta), c="b")


    # draw some people
    angles = [30, 60, 90, 120, 180, 270, 300]

    for l in angles:
        center = ( (R + 0.5*L)*np.cos(np.radians(l)),
                   (R + 0.5*L)*np.sin(np.radians(l)) )
        draw_person(center, L, np.radians(l - 90), color="r")
        L = 1.1*L

    plt.axis("off")

    ax = plt.gca()
    ax.set_aspect("equal", "datalim")


    plt.subplots_adjust(left=0.05, right=0.98, bottom=0.05, top=0.98)
    plt.axis([-1.2*R, 1.2*R, -1.2*R, 1.2*R])

    f = plt.gcf()
    f.set_size_inches(6.0, 6.0)

    plt.savefig("test.png")
Example #55
0
    def plot(self, fig=None, ax=None, fontsize=14, lw=2, ms=6, legend=True, noshow=False):
        """
        Plotting routine.
        Depends on self._get_xy()
                   self._get_markers_and_colors()
                   self._get_datalabels()
                   self._get_xylabels()
        """

        # get current figure and axis
        if (fig is None and ax is None):
            fig, ax = plt.gcf(), plt.gca()

        self.fig, self.ax = fig, ax

        # plot data collections
        for i in range(self.nplots):
            self.ax.plot(self.x[i], self.y[i], marker=self.markers[i], color=self.colors[i], label=self.datalabels[i],
                         linewidth=lw, markersize=ms)

        # set plot labels
        self.ax.set_xlabel(self.xlabel, fontsize=fontsize)
        self.ax.set_ylabel(self.ylabel, fontsize=fontsize)

        if legend:
            self.ax.legend(loc='best', frameon=False, fontsize=fontsize)

        if ~noshow:
            plt.show()
Example #56
0
def normal_curve(ax=None, linewidth=4, color='k', mean=0, SD=1,
                 facecolor='gray',
                 xlabel='standardized units',
                 ylabel='% per standardized unit',
                 alpha=0.5,
                 **plot_opts):

   if ax is None:
      fig = plt.gcf()
      ax = fig.add_subplot(111)

   plot_opts['linewidth'] = linewidth
   plot_opts['color'] = color

   Z = np.linspace(-4,4,101)
   X = mean+SD*Z
   Y = ndist.pdf(Z) / SD
   ax.plot(X, Y, **plot_opts)
   ax.fill_between(X, 0*X, Y, alpha=alpha, facecolor=facecolor)
   if xlabel:
      ax.set_xlabel(xlabel, fontsize=20)
   if ylabel:
      ax.set_ylabel(ylabel, fontsize=20)
   ax.set_ylim([0,0.45/SD])
   ax.set_xlim([X.min(),X.max()])
   return ax
Example #57
0
def SD_rule_of_thumb_skewed(mult, ax=None, bins=30, regions=(), **opts):

   sample = np.random.exponential(size=15000) * 1.1 + np.random.uniform(size=15000) * 2.

   if ax is None:
      fig = plt.gcf()
      ax = fig.add_subplot(111)
      
   ax, density, CDF = sample_density(sample, bins=bins, **opts)
   SD = np.std(sample)
   ax.annotate('Average', xy=(np.mean(sample), 0),
              arrowprops=dict(facecolor='black'), xytext=(np.mean(sample),-0.1),
              fontsize=20,
              horizontalalignment='center')

   interval = np.linspace(np.mean(sample) - mult * SD,
                          np.mean(sample) + mult * SD,
                          500)
   ax.fill_between(interval, 0*interval, density(interval), 
                   hatch='/',
                   facecolor='yellow')

   standY = (sample - np.mean(sample)) / SD
   within = (np.fabs(standY) <= mult).sum() * 1. / sample.shape[0] * 100
   ax.set_title('Percentage within %0.1f SD: %d %%' % (mult, int(within)), fontsize=20, color='red')
   ax.set_yticks([])
   ax.set_xlim([-2,12])
   ax.set_ylim([0,ax.get_ylim()[1]])
   return ax
Example #58
0
def show(size=(12, 12)):
    # Make all plots have square aspect ratio
    plt.axis('equal')
    # Make high quality
    fig = plt.gcf()
    fig.set_size_inches(*size)
    plt.show()
Example #59
0
    def filtray(self, itx, irx, tau0, tau1, col='b'):
        """ filter rays

        Parameters
        ----------
        itx :
        irx :
        tau0 :
        tau1 :
        col :

        Display ray and nstr
        """
        gr = GrRay3D()
        gr.load(self.dtra[itx][irx], self.L)
        self.L.display['Thin'] = True
        self.L.display['Node'] = False
        self.L.display['NodeNum'] = False
        self.L.display['EdgeNum'] = False
        plt.axis('scaled')
        #self.L.show(fig,ax,nodelist,seglist)
        self.L.showGs()
        delays = gr.delay()
        rayset = np.nonzero((delays >= tau0) & (delays <= tau1))[0]
        fig = plt.gcf()
        ax = fig.get_axes()[0]
        gr.show(ax, rayset, col=col, node=False)
        plt.title('Tx' + str(itx) + '-Rx' + str(irx) + ' : ' + str(
            tau0) + ' < tau < ' + str(tau1))
Example #60
0
def survival_and_stats(feature, surv, upper_lim=5, axs=None, figsize=(7, 5), title=None,
                       order=None, colors=None, **args):
    if axs is None:
        fig = plt.figure(figsize=figsize)
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=2)
        ax2 = plt.subplot2grid((3, 3), (2, 0), colspan=2)
        ax3 = plt.subplot2grid((3, 3), (2, 2))
    else:
        ax1, ax2, ax3 = axs
        fig = plt.gcf()
    if feature.dtype != str:
        feature = feature.astype(str)
    if colors is None:
        colors = colors_global
    
    t = get_surv_fit(surv, feature)
    if order is None:
        t = t.sort([('5y Survival', 'Surv')], ascending=True)
    else:
        t = t.ix[order]
    survival_stat_plot(t, axs=[ax2, ax3], upper_lim=upper_lim, colors=colors)
    r = pd.Series({s:i for i, s in enumerate(t.index)})
    color_lookup = {c: colors[i % len(colors)] for i, c in enumerate(t.index)}
    
    draw_survival_curve(feature, surv, ax=ax1, colors=color_lookup, **args)
    ax1.legend().set_visible(False)
    if title:
        ax1.set_title(title)
    
    fig.tight_layout()