Plot membership probabilities and MC source ids together.
762228
def validateMembership(likelihood, p, mc_source_id=1):
    

    cut_mc_source_id = (likelihood.catalog.mc_source_id == mc_source_id)

    # Spatial

    projector = ugali.utils.projector.Projector(likelihood.kernel.lon, likelihood.kernel.lat)
    x, y = projector.sphereToImage(likelihood.catalog.lon, likelihood.catalog.lat)
    
    pylab.figure()
    pylab.scatter(x[cut_mc_source_id], y[cut_mc_source_id], c='gray', s=100, edgecolors='none')
    pylab.scatter(x, y, c=p, edgecolors='none')
    pylab.colorbar()
    pylab.title('Membership Probability')
    pylab.xlabel(r'$\delta$ Lon (deg)')
    pylab.ylabel(r'$\delta$ Lat (deg)')
    
    # Spectral
    
    pylab.figure()
    pylab.scatter(likelihood.catalog.color[cut_mc_source_id], likelihood.catalog.mag[cut_mc_source_id], c='gray', s=100, edgecolors='none')
    pylab.scatter(likelihood.catalog.color, likelihood.catalog.mag, c=p, edgecolors='none')
    pylab.colorbar()
    pylab.title('Membership Probability')
    pylab.xlim(likelihood.roi.bins_color[0], likelihood.roi.bins_color[-1])
    pylab.ylim(likelihood.roi.bins_mag[-1], likelihood.roi.bins_mag[0])
    pylab.xlabel('Color (mag)')
    pylab.ylabel('Magnitude (mag)')
    
    # Membership accuracy

    p_array = []
    frac_members_array = []
    frac_members_array_low = []
    frac_members_array_high = []

    probability_bins = numpy.arange(0., 1. + 1.e-10, 0.1)
    for ii in range(0, len(probability_bins) - 1):

        if not numpy.any(numpy.logical_and(p >= probability_bins[ii], p < probability_bins[ii + 1])):
            continue
        
        #p_array.append(0.5 * (probability_bins[ii] + probability_bins[ii + 1]))
        cut = numpy.logical_and(p >= probability_bins[ii], p < probability_bins[ii + 1])
        p_array.append(numpy.median(p[cut]))

        n_members = numpy.sum(numpy.all([likelihood.catalog.mc_source_id == mc_source_id,
                                         p >= probability_bins[ii],
                                         p < probability_bins[ii + 1]], axis=0))
        n_not_members = numpy.sum(numpy.all([likelihood.catalog.mc_source_id != mc_source_id,
                                             p >= probability_bins[ii],
                                             p < probability_bins[ii + 1]], axis=0))

        frac_members, (frac_members_low, frac_members_high) = ugali.utils.bayesian_efficiency.confidenceInterval(n_members + n_not_members,
                                                                                                                 n_members,
                                                                                                                 0.68)
        frac_members_array.append(frac_members)
        frac_members_array_low.append(frac_members - frac_members_low)
        frac_members_array_high.append(frac_members_high - frac_members)
        
    pylab.figure()
    pylab.plot([0,1], [0,1], c='red')
    pylab.errorbar(p_array, frac_members_array, yerr=[frac_members_array_low, frac_members_array_high],
                   marker='o', color='blue', linestyle='none')
    pylab.xlim(0, 1)
    pylab.ylim(0, 1)
    pylab.xlabel('Membership Probability')
    pylab.ylabel('Fraction of True Satellite Members')

    # Where does richness come from?
    
    p_array = []
    p_sum_array = []
    n_members_array = []

    for ii in range(0, len(probability_bins) - 1):
        p_array.append(0.5 * (probability_bins[ii] + probability_bins[ii + 1]))
        p_sum_array.append(numpy.sum(p[numpy.logical_and(p >= probability_bins[ii],
                                                         p < probability_bins[ii + 1])]))
        n_members_array.append(numpy.sum(numpy.all([likelihood.catalog.mc_source_id == mc_source_id,
                                                    p >= probability_bins[ii],
                                                    p < probability_bins[ii + 1]], axis=0)))
        
    pylab.figure()
    pylab.scatter(p_array, p_sum_array, marker='o', c='blue')
    pylab.scatter(p_array, n_members_array, marker='o', c='red')
    pylab.xlim(0, 1)
    #pylab.ylim(0, 1)
    pylab.xlabel('Membership Probability')
    pylab.ylabel('Membership Probability Sum')

    # Purity and completeness

    x = numpy.linspace(0., 1., 1001) 
    purity = []
    completeness = []

    purity_reconstructed = []
    completeness_reconstructed = []

    for ii in range(0, len(x)):
        cut = p > (1 - x[ii])

        if numpy.sum(cut) < 1:
            purity.append(1.)
            completeness.append(0.)
            purity_reconstructed.append(1.)
            completeness_reconstructed.append(0.)
            continue
        
        purity_cut = numpy.logical_and(cut, likelihood.catalog.mc_source_id == mc_source_id)
        completeness_cut = likelihood.catalog.mc_source_id == mc_source_id
        
        purity.append(float(numpy.sum(purity_cut)) / numpy.sum(cut))
        completeness.append(float(numpy.sum(purity_cut)) / numpy.sum(completeness_cut))

        purity_reconstructed.append(numpy.mean(p[cut]))
        completeness_reconstructed.append(numpy.sum(p[cut]) / numpy.sum(p))
        
    pylab.figure()
    pylab.plot(x, purity, c='red', label='Purity')
    pylab.plot(x, completeness, c='blue', label='Completeness')
    pylab.plot(x, purity_reconstructed, c='red', linestyle='--', label='Purity Reconstructed')
    pylab.plot(x, completeness_reconstructed, c='blue', linestyle='--', label='Completeness Reconstructed')
    pylab.xlim(0, 1)
    pylab.ylim(0, 1)
    pylab.xlabel('1 - Membership Probability')
    #pylab.ylabel('Purity or Completeness')
    pylab.legend(loc='lower center')
1029213
def plotted_data(self):
        """"""
        return InteractiveList(
            [arr for arr, val in zip(self.iter_data,
                                     cycle(slist(self.value)))
             if val is not None])
369329
def plot_losses(calc_id, bins=7):
    
    # read the hazard data
    dstore = util.read(calc_id)
    losses_by_rlzi = dict(extract(dstore, 'losses_by_event'))
    oq = dstore['oqparam']
    plt = make_figure(losses_by_rlzi, oq.loss_dt().names, bins)
    plt.show()
118979
def expanded_counts_map(self):
        
        if self.hpx._ipix is None:
            return self.counts

        output = np.zeros(
            (self.counts.shape[0], self.hpx._maxpix), self.counts.dtype)
        for i in range(self.counts.shape[0]):
            output[i][self.hpx._ipix] = self.counts[i]
        return output
125218
def join_conn_groups( conns, descs, mat_ids, concat = False ):
    """"""

    el = dict_from_keys_init( descs, list )
    for ig, desc in enumerate( descs ):
        el[desc].append( ig )
    groups = [ii for ii in el.values() if ii]
##     print el, groups

    descs_out, conns_out, mat_ids_out = [], [], []
    for group in groups:
        n_ep = conns[group[0]].shape[1]

        conn = nm.zeros( (0, n_ep), nm.int32 )
        mat_id = nm.zeros( (0,), nm.int32 )
        for ig in group:
            conn = nm.concatenate( (conn, conns[ig]) )
            mat_id = nm.concatenate( (mat_id, mat_ids[ig]) )

        if concat:
            conn = nm.concatenate( (conn, mat_id[:,nm.newaxis]), 1 )
        else:
            mat_ids_out.append( mat_id )
        conns_out.append( conn )
        descs_out.append( descs[group[0]] )

    if concat:
        return conns_out, descs_out
    else:
        return conns_out, descs_out, mat_ids_out
270140
def main():
    
    dataset = 'RD'

    observables = ['log(<\sigma v>)', '\Omega_{\chi}h^2', 'log(\sigma_p^{SI})']
    var  = ['log(m_{\chi})']
    var += ['log(C_1)', 'log(C_2)', 'log(C_3)', 'log(C_4)', 'log(C_5)', 'log(C_6)']
    var += observables

    plot_vs_mass(dataset, observables, 'mass_vs_observables.png')
    plot_oneD(dataset, var, 'oneD.png')
    pairplot(dataset, var, 'pairplot.png')
1281221
def _plot_go_group(self, hdrgo, usrgos, pltargs, go2parentids):
        """"""
        gosubdagplotnts = self._get_gosubdagplotnts(hdrgo, usrgos, pltargs, go2parentids)
        # Create pngs and return png names
        pngs = [obj.wrplt(pltargs.fout_dir, pltargs.plt_ext) for obj in gosubdagplotnts]
        return pngs
1364382
def get_zeta_i_j_given_separate_counts(self, y_i, y_j):
		'''
		
		'''
		n_i, n_j = y_i.sum(), y_j.sum()
		delta_i_j = (np.log((y_i + 1) / (1. + n_i - y_i))
		             - np.log((y_j + 1) / (1. + n_j - y_j)))
		return delta_i_j
57761
def plot_results(fout_png, goea_results, *args, **kws):
    """"""
    if "{NS}" not in fout_png:
        plt_goea_results(fout_png, goea_results, *args, **kws)
    else:
        # Plot separately by NS: BP, MF, CC
        ns2goea_results = cx.defaultdict(list)
        for rec in goea_results:
            ns2goea_results[rec.NS].append(rec)
        for ns_name, ns_res in ns2goea_results.items():
            png = fout_png.format(NS=ns_name)
            plt_goea_results(png, ns_res, *args, **kws)
779772
def plot_macadam(
    ellipse_scaling=10,
    plot_filter_positions=False,
    plot_standard_deviations=False,
    plot_rgb_triangle=True,
    plot_mesh=True,
    n=1,
    xy_to_2d=lambda xy: xy,
    axes_labels=("x", "y"),
):
    
    dir_path = os.path.dirname(os.path.realpath(__file__))
    with open(os.path.join(dir_path, "data/macadam1942/table3.yaml")) as f:
        data = yaml.safe_load(f)

    # if plot_filter_positions:
    #     with open(os.path.join(dir_path, 'data/macadam1942/table1.yaml')) as f:
    #         filters_xyz = yaml.safe_load(f)
    #     filters_xyz = {
    #         key: 100 * numpy.array(value) for key, value in filters_xyz.items()
    #         }
    #     for key, xyz in filters_xyz.items():
    #         x, y = xyz100_to_2d(xyz)
    #         plt.plot(x, y, 'xk')
    #         ax.annotate(key, (x, y))

    # collect the ellipse centers and offsets
    centers = []
    offsets = []
    for datak in data:
        # collect ellipse points
        _, _, _, _, delta_y_delta_x, delta_s = numpy.array(datak["data"]).T

        offset = (
            numpy.array([numpy.ones(delta_y_delta_x.shape[0]), delta_y_delta_x])
            / numpy.sqrt(1 + delta_y_delta_x ** 2)
            * delta_s
        )

        if offset.shape[1] < 2:
            continue

        centers.append([datak["x"], datak["y"]])
        offsets.append(numpy.column_stack([+offset, -offset]))

    centers = numpy.array(centers)

    _plot_ellipse_data(
        centers,
        offsets,
        ellipse_scaling=ellipse_scaling,
        xy_to_2d=xy_to_2d,
        plot_mesh=plot_mesh,
        n=n,
        plot_rgb_triangle=plot_rgb_triangle,
    )
    return
827931
def main(net):
  '''
  
  '''
  # calculate the distance between the data points within the same category and
  # compare to null distribution
  for inst_rc in ['row', 'col']:

    inst_nodes = deepcopy(net.dat['nodes'][inst_rc])

    inst_index = deepcopy(net.dat['node_info'][inst_rc]['clust'])

    # reorder based on clustered order
    inst_nodes = [ inst_nodes[i] for i in inst_index]

    # make distance matrix dataframe
    dm = dist_matrix_lattice(inst_nodes)

    node_infos = list(net.dat['node_info'][inst_rc].keys())

    all_cats = []
    for inst_info in node_infos:
      if 'dict_cat_' in inst_info:
        all_cats.append(inst_info)

    for cat_dict in all_cats:

      tmp_dict = net.dat['node_info'][inst_rc][cat_dict]

      pval_name = cat_dict.replace('dict_','pval_')
      net.dat['node_info'][inst_rc][pval_name] = {}

      for cat_name in tmp_dict:

        subset = tmp_dict[cat_name]

        inst_median = calc_median_dist_subset(dm, subset)

        hist = calc_hist_distances(dm, subset, inst_nodes)

        pval = 0

        for i in range(len(hist['prob'])):
          if i == 0:
            pval = hist['prob'][i]
          if i >= 1:
            if inst_median >= hist['bins'][i]:
              pval = pval + hist['prob'][i]

        net.dat['node_info'][inst_rc][pval_name][cat_name] = pval
118904
def plotReceptiveFields(sp, nDim1=8, nDim2=8):
  
  columnNumber = np.product(sp.getColumnDimensions())
  fig, ax = plt.subplots(nrows=4, ncols=4)
  for rowI in range(4):
    for colI in range(4):
      col = np.random.randint(columnNumber)
      connectedSynapses = np.zeros((nDim1*nDim2,), dtype=uintType)
      sp.getConnectedSynapses(col, connectedSynapses)
      receptiveField = connectedSynapses.reshape((nDim1, nDim2))
      ax[rowI, colI].imshow(receptiveField, cmap='gray')
      ax[rowI, colI].set_title("col: {}".format(col))
434908
def _plot_rprof_list(sdat, lovs, rprofs, metas, stepstr, rads=None):
    """"""
    if rads is None:
        rads = {}
    for vfig in lovs:
        fig, axes = plt.subplots(ncols=len(vfig), sharey=True)
        axes = [axes] if len(vfig) == 1 else axes
        fname = 'rprof_'
        for iplt, vplt in enumerate(vfig):
            xlabel = None
            for ivar, rvar in enumerate(vplt):
                fname += rvar + '_'
                rad = rads[rvar] if rvar in rads else rprofs['r']
                if conf.rprof.depth:
                    rad = rprofs['bounds'][1] - rad
                axes[iplt].plot(rprofs[rvar], rad,
                                conf.rprof.style,
                                label=metas[rvar].description)
                if conf.rprof.depth:
                    axes[iplt].invert_yaxis()
                if xlabel is None:
                    xlabel = metas[rvar].kind
                elif xlabel != metas[rvar].kind:
                    xlabel = ''
            if ivar == 0:
                xlabel = metas[rvar].description
            if xlabel:
                _, unit = sdat.scale(1, metas[rvar].dim)
                if unit:
                    xlabel += ' ({})'.format(unit)
                axes[iplt].set_xlabel(xlabel)
            if vplt[0][:3] == 'eta':  # list of log variables
                axes[iplt].set_xscale('log')
            axes[iplt].set_xlim(left=conf.plot.vmin, right=conf.plot.vmax)
            if ivar:
                axes[iplt].legend()
        ylabel = 'Depth' if conf.rprof.depth else 'Radius'
        _, unit = sdat.scale(1, 'm')
        if unit:
            ylabel += ' ({})'.format(unit)
        axes[0].set_ylabel(ylabel)
        misc.saveplot(fig, fname + stepstr)
868219
def plotter(molecules, ensemble_lookup, options):
    

    try:
        import matplotlib

        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
    except ImportError:
        print("\n Plotting requires matplotlib to be installed\n")
        sys.exit(1)

    for ensemble in ensemble_lookup.keys():

        # create figure
        fig = plt.figure()

        # create the queries subplot, the left subplot
        # create the left hand subplot
        ax1 = fig.add_subplot(121)

        for query in sorted(ensemble_lookup[ensemble]):
            query_list = []
            query_list.append(query)
            score_structure = classification.make_score_structure(molecules, query_list)
            auc_structure = classification.make_auc_structure(score_structure)
            tpf = []
            fpf = []
            for mol in auc_structure:
                fpf.append(mol[4])
                tpf.append(mol[5])

            # add axis-labels and a title
            ax1.set_xlabel('FPF')
            ax1.set_ylabel('TPF')
            title = 'query performance'
            ax1.set_title(title)

            # add plot data and labels for the legend
            lbl = query
            ax1.plot(fpf, tpf, lw=3, label=lbl)

        # get legend handles and labels, then reverse their order
        handles, labels = ax1.get_legend_handles_labels()
        ax1.legend(handles[::-1], labels[::-1])

        # add the legend
        ax1.legend(handles, labels, loc='best')

        # create the ensemble subplot, the right subplot
        score_structure = classification.make_score_structure(molecules, ensemble_lookup[ensemble])
        auc_structure = classification.make_auc_structure(score_structure)
        tpf = []
        fpf = []
        for mol in auc_structure:
            fpf.append(mol[4])
            tpf.append(mol[5])

        # create right hand subplot
        ax2 = fig.add_subplot(122)

        # add axis-labels and a title
        ax2.set_xlabel('FPF')
        ax2.set_ylabel('TPF')
        title = 'ensemble performance'
        ax2.set_title(title)

        # add plot data and a label for the legend
        lbl = 'ensemble'
        ax2.plot(fpf, tpf, lw=3, label=lbl)

        # get legend handles and labels, then reverse their order
        handles, labels = ax2.get_legend_handles_labels()
        ax2.legend(handles[::-1], labels[::-1])

        # add the legend
        ax2.legend(handles, labels, loc='best')

        # save figure
        figurename = options.outname + '_' + ensemble.replace('.csv', '') + '.pdf'
        filename = os.path.join(os.getcwd(), figurename)
        plt.savefig(filename, bbobx='tight', format='pdf')
672778
def _compute_mixing_probabilities(self):
        

        self.cbar = dot(self.mu, self.M)
        for i in range(self.N):
            for j in range(self.N):
                self.omega[i, j] = (self.M[i, j]*self.mu[i]) / self.cbar[j]
523794
def map_predict(interface, state, label, inp):
    
    import numpy as np
    out = interface.output(0)

    continuous = [j for i, j in enumerate(state["X_indices"]) if
                  state["X_meta"][i] == "c"]  # indices of continuous features
    discrete = [j for i, j in enumerate(state["X_indices"]) if
                state["X_meta"][i] == "d"]  # indices of discrete features

    cont = True if len(continuous) > 0 else False  # enables calculation of Gaussian probabilities
    disc = True if len(discrete) > 0 else False  # enables calculation of multinomial probabilities.

    for row in inp:
        row = row.strip().split(state["delimiter"])
        if len(row) > 1:  # if row is empty
            # set id of a sample
            x_id = "" if state["id_index"] == -1 else row[state["id_index"]]
            # initialize prior probability for all labels
            probs = state["fit_model"]["prior_log"]

            if cont:  # continuous features
                x = np.array([(0 if row[j] in state["missing_vals"] else float(row[j])) for j in
                              continuous])  # sets selected features of the sample
                # Gaussian distribution
                probs = probs - 0.5 * np.sum(
                    np.true_divide((x - state["fit_model"]["mean"]) ** 2, state["fit_model"]["var"]) +
                    state["fit_model"]["var_log"], axis=1)

            if disc:  # discrete features
                # multinomial distribution
                probs = probs + np.sum(
                    [(0 if row[i] in state["missing_vals"] else state["fit_model"].get((str(i), row[i]), np.zeros(1)))
                     for i in discrete], axis=0)

            # normalize by P(x) = P(f_1, ..., f_n)
            log_prob_x = np.log(np.sum(np.exp(probs)))
            probs = np.exp(np.array(probs) - log_prob_x)
            # Predicted label is the one with highest probability
            y_predicted = max(zip(probs, state["fit_model"]["y_labels"]))[1]
            out.add(x_id, (y_predicted, probs.tolist()))
96121
def combine_pmaps(self, pmap_by_grp):
        
        grp = list(pmap_by_grp)[0]  # pmap_by_grp must be non-empty
        num_levels = pmap_by_grp[grp].shape_y
        pmaps = [probability_map.ProbabilityMap(num_levels, 1)
                 for _ in self.realizations]
        array = self.by_grp()
        for grp in pmap_by_grp:
            for gsim_idx, rlzis in enumerate(array[grp]):
                pmap = pmap_by_grp[grp].extract(gsim_idx)
                for rlzi in rlzis:
                    pmaps[rlzi] |= pmap
        return pmaps