Calculates coverage, overlap, conflicts, and accuracy for a single LF

    Args:
        Y_p: a np.array or torch.Tensor of predicted labels
        Y: a np.array or torch.Tensor of true labels (if known)
14231
def single_lf_summary(Y_p, Y=None):
    
    L = sparse.csr_matrix(arraylike_to_numpy(Y_p).reshape(-1, 1))
    return lf_summary(L, Y)
1241300
def logloss(y, p):
    

    p[p < EPS] = EPS
    p[p > 1 - EPS] = 1 - EPS
    return log_loss(y, p)
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')
758850
def fit(self, x, y):
        
        verbose = self.verbose
        # Create temporary files
        data_file = tempfile.NamedTemporaryFile("w+b", delete=False)
        label_file = tempfile.NamedTemporaryFile("w+b", delete=False)

        start = time.time()
        raw_rules = categorical2pysbrl_data(x, y, data_file.name, label_file.name, supp=self.min_support,
                                            zmin=self.min_rule_len, zmax=self.max_rule_len, method=self.fim_method)
        if verbose > 1:
            print("Info: sbrl data files saved to %s and %s temporarily" % (data_file.name, label_file.name))
        data_file.close()
        label_file.close()
        cat_time = time.time() - start
        if verbose:
            print("Info: time for rule mining: %.4fs" % cat_time)
        n_labels = int(np.max(y)) + 1
        start = time.time()
        _model = train_sbrl(data_file.name, label_file.name, self.lambda_, eta=self.eta,
                            max_iters=self.iters, n_chains=self.n_chains, seed=self.seed,
                            alpha=self.alpha, verbose=verbose)
        train_time = time.time() - start
        if verbose:
            print("Info: training time: %.4fs" % train_time)

        # update model parameters
        self._n_classes = n_labels
        self._n_features = x.shape[1]

        # convert the raw parameters to rules
        self.from_raw(_model[0], _model[1], raw_rules)
        self._supports = self.compute_support(x, y)

        # Close the temp files
        os.unlink(data_file.name)
        os.unlink(label_file.name)
138149
def rmse(y, p):
    

    # check and get number of samples
    assert y.shape == p.shape

    return np.sqrt(mse(y, p))
1272489
def fit(self, X, y=None, **fit_params):
        

        # Handle the feature names if they're None.
        if self.features_ is None:

            # If X is a data frame, get the columns off it.
            if is_dataframe(X):
                self.features_ = np.array(X.columns)

            # Otherwise create numeric labels for each column.
            else:
                _, ncols = X.shape
                self.features_ = np.arange(0, ncols)

        return self
1281351
def _calc_overlap_coef(
    markers1: dict,
    markers2: dict,
):
    
    overlap_coef=np.zeros((len(markers1), len(markers2)))

    j=0
    for marker_group in markers1:
        tmp = [len(markers2[i].intersection(markers1[marker_group]))/
               max(min(len(markers2[i]), len(markers1[marker_group])),1) for i in markers2.keys()]
        overlap_coef[j,:] = tmp
        j += 1

    return overlap_coef
866581
def _prob_match(self, features):
        

        # compute the probabilities
        probs = self.kernel.predict_proba(features)

        # get the position of match probabilities
        classes = list(self.kernel.classes_)
        match_class_position = classes.index(1)

        return probs[:, match_class_position]
1055068
def _overlap(y,yr,psd):
    
    yyr  = _inner_product(y,yr,psd)
    yy   = _inner_product(y,y,psd)
    yryr = _inner_product(yr,yr,psd)
    olap = yyr/np.sqrt(yy*yryr)
    return olap
175502
def _intersect(self, label, xmin, ymin, xmax, ymax):
        """"""
        left = np.maximum(label[:, 0], xmin)
        right = np.minimum(label[:, 2], xmax)
        top = np.maximum(label[:, 1], ymin)
        bot = np.minimum(label[:, 3], ymax)
        invalid = np.where(np.logical_or(left >= right, top >= bot))[0]
        out = label.copy()
        out[:, 0] = left
        out[:, 1] = top
        out[:, 2] = right
        out[:, 3] = bot
        out[invalid, :] = 0
        return out
1261877
def _preprocess_Y(self, Y, k=None):
        """"""
        # If not a list, convert to a singleton list
        if not isinstance(Y, list):
            if self.t != 1:
                msg = "For t > 1, Y must be a list of n-dim or [n, K_t] tensors"
                raise ValueError(msg)
            Y = [Y]

        if not len(Y) == self.t:
            msg = f"Expected Y to be a t-length list (t={self.t}), not {len(Y)}"
            raise ValueError(msg)

        return [EndModel._preprocess_Y(self, Y_t, self.K[t]) for t, Y_t in enumerate(Y)]
1214268
def ylogydu(y, u):
    
    mask = (np.atleast_1d(y)!=0.)
    out = np.zeros_like(u)
    out[mask] = y[mask] * np.log(y[mask] / u[mask])
    return out
1164104
def fit(self, X, y, **kwargs):
        
        self.estimator.fit(X, y, **kwargs)
        self.draw()
        return self
1268718
def _get_y_scores(self, X):
        
        # TODO refactor shared method with ROCAUC

        # Resolution order of scoring functions
        attrs = (
            'decision_function',
            'predict_proba',
        )

        # Return the first resolved function
        for attr in attrs:
            try:
                method = getattr(self.estimator, attr, None)
                if method:
                    # Compute the scores from the decision function
                    y_scores = method(X)

                    # Return only the positive class for binary predict_proba
                    if self.target_type_ == BINARY and y_scores.ndim == 2:
                        return y_scores[:,1]
                    return y_scores

            except AttributeError:
                # Some Scikit-Learn estimators have both probability and
                # decision functions but override __getattr__ and raise an
                # AttributeError on access.
                continue

        # If we've gotten this far, we can't do anything
        raise ModelError((
            "{} requires estimators with predict_proba or decision_function methods."
        ).format(self.__class__.__name__))
983863
def fpr(y, z):
    
    tp, tn, fp, fn = contingency_table(y, z)
    return fp / (fp + tn)
210810
def accuracy_thresh(y_pred:Tensor, y_true:Tensor, thresh:float=0.5, sigmoid:bool=True)->Rank0Tensor:
    ""
    if sigmoid: y_pred = y_pred.sigmoid()
    return ((y_pred>thresh)==y_true.byte()).float().mean()
1330028
def local_accuracy(X, y, model_generator, method_name):
    

    def score_map(true, pred):
        """ Converts local accuracy from % of standard deviation to numerical scores for coloring.
        """

        v = min(1.0, np.std(pred - true) / (np.std(true) + 1e-8))
        if v < 1e-6:
            return 1.0
        elif v < 0.01:
            return 0.9
        elif v < 0.05:
            return 0.75
        elif v < 0.1:
            return 0.6
        elif v < 0.2:
            return 0.4
        elif v < 0.3:
            return 0.3
        elif v < 0.5:
            return 0.2
        elif v < 0.7:
            return 0.1
        else:
            return 0.0
    def score_function(X_train, X_test, y_train, y_test, attr_function, trained_model, random_state):
        return measures.local_accuracy(
            X_train, y_train, X_test, y_test, attr_function(X_test),
            model_generator, score_map, trained_model
        )
    return None, __score_method(X, y, None, model_generator, score_function, method_name)