How to use the treecorr._lib function in TreeCorr

To help you get started, we’ve selected a few TreeCorr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rmjarvis / TreeCorr / treecorr / field.py View on Github external
def __del__(self):
        # Using memory allocated from the C layer means we have to explicitly deallocate it
        # rather than being able to rely on the Python memory manager.

        # In case __init__ failed to get that far
        if hasattr(self,'data'):  # pragma: no branch
            # I don't get this, but sometimes it gets here when the fft.lock is already locked.
            # When that happens, this will freeze in a `with ffi._lock` line in the ffi api.py.
            # So, don't do that, and just accept the memory leak instead.
            if not treecorr._ffi._lock.locked(): # pragma: no branch
                treecorr._lib.DestroyNField(self.data, self._coords)
github rmjarvis / TreeCorr / treecorr / nkcorrelation.py View on Github external
self._set_metric(metric, cat1.coords, cat2.coords)

        self._set_num_threads(num_threads)

        min_size, max_size = self._get_minmax_size()

        f1 = cat1.getNField(min_size, max_size, self.split_method,
                            self.brute is True or self.brute == 1,
                            self.min_top, self.max_top, self.coords)
        f2 = cat2.getKField(min_size, max_size, self.split_method,
                            self.brute is True or self.brute == 2,
                            self.min_top, self.max_top, self.coords)

        self.logger.info('Starting %d jobs.',f1.nTopLevelNodes)
        treecorr._lib.ProcessCross2(self.corr, f1.data, f2.data, self.output_dots,
                                    f1._d, f2._d, self._coords, self._bintype, self._metric)
github rmjarvis / TreeCorr / treecorr / field.py View on Github external
def __init__(self, cat, logger=None):
        from treecorr.util import double_ptr as dp
        if logger:
            if cat.name != '':
                logger.info('Building KSimpleField from cat %s',cat.name)
            else:
                logger.info('Building KSimpleField')
        self._d = 2  # KData
        self.coords = cat.coords
        self._coords = treecorr.util.coord_enum(self.coords)  # These are the C++-layer enums

        self.data = treecorr._lib.BuildKSimpleField(dp(cat.x), dp(cat.y), dp(cat.z),
                                                    dp(cat.k),
                                                    dp(cat.w), dp(cat.wpos), cat.ntot,
                                                    self._coords)
        if logger:
            logger.debug('Finished building KSimpleField (%s)',self.coords)
github rmjarvis / TreeCorr / treecorr / field.py View on Github external
Finally, in cases where ra, dec are allowed, you may instead provide a
        ``coord.CelestialCoord`` instance as the first argument to specify both RA and Dec.
        """
        x,y,z,sep = treecorr.util.parse_xyzsep(args, kwargs, self._coords)
        if self.min_size == 0:
            # If min_size == 0, then regular method is already exact.
            ind = self._get_near(x, y, z, sep)
        else:
            # Expand the radius by the minimum size of the cells.
            sep1 = sep + self.min_size
            # Get those indices
            ind = self._get_near(x, y, z, sep1)
            # Now check the actual radii of these points using the catalog x,y,z values.
            rsq = (self.cat.x[ind]-x)**2 + (self.cat.y[ind]-y)**2
            if self._coords != treecorr._lib.Flat:
                rsq += (self.cat.z[ind]-z)**2
            # Select the ones with r < sep
            near = rsq < sep**2
            ind = ind[near]
        # It comes back unsorted, so sort it.  (Not really required, but nicer output.)
        return np.sort(ind)
github rmjarvis / TreeCorr / treecorr / field.py View on Github external
def __init__(self, cat, logger=None):
        from treecorr.util import double_ptr as dp
        if logger:
            if cat.name != '':
                logger.info('Building NSimpleField from cat %s',cat.name)
            else:
                logger.info('Building NSimpleField')
        self._d = 1  # NData
        self.coords = cat.coords
        self._coords = treecorr.util.coord_enum(self.coords)  # These are the C++-layer enums

        self.data = treecorr._lib.BuildNSimpleField(dp(cat.x), dp(cat.y), dp(cat.z),
                                                    dp(cat.w), dp(cat.wpos), cat.ntot,
                                                    self._coords)
        if logger:
            logger.debug('Finished building NSimpleField (%s)',self.coords)
github rmjarvis / TreeCorr / treecorr / util.py View on Github external
def metric_enum(metric):
    """Return the C++-layer enum for the given string value of metric.
    """
    if metric == 'Euclidean':
        return treecorr._lib.Euclidean
    elif metric == 'Rperp':
        return metric_enum(treecorr.Rperp_alias)
    elif metric == 'FisherRperp':
        return treecorr._lib.Rperp
    elif metric in ['OldRperp']:
        return treecorr._lib.OldRperp
    elif metric == 'Rlens':
        return treecorr._lib.Rlens
    elif metric == 'Arc':
        return treecorr._lib.Arc
    elif metric == 'Periodic':
        return treecorr._lib.Periodic
    else:
        raise ValueError("Invalid metric %s"%metric)
github rmjarvis / TreeCorr / treecorr / kkcorrelation.py View on Github external
def __del__(self):
        # Using memory allocated from the C layer means we have to explicitly deallocate it
        # rather than being able to rely on the Python memory manager.
        if hasattr(self, 'corr'):
            if not treecorr._ffi._lock.locked(): # pragma: no branch
                treecorr._lib.DestroyCorr2(self.corr, self._d1, self._d2, self._bintype)
github rmjarvis / TreeCorr / treecorr / field.py View on Github external
def __del__(self):
        # Using memory allocated from the C layer means we have to explicitly deallocate it
        # rather than being able to rely on the Python memory manager.

        # In case __init__ failed to get that far
        if hasattr(self,'data'):  # pragma: no branch
            if not treecorr._ffi._lock.locked(): # pragma: no branch
                treecorr._lib.DestroyKSimpleField(self.data, self._coords)
github rmjarvis / TreeCorr / treecorr / binnedcorr2.py View on Github external
self.max_sep = self.nbins * self.bin_size / 2.

            sep = np.linspace(-self.max_sep, self.max_sep, self.nbins, endpoint=False,
                                 dtype=float)
            sep += 0.5 * self.bin_size
            self.dx, self.dy = np.meshgrid(sep, sep)
            self.left_edges = self.dx - 0.5*self.bin_size
            self.right_edges = self.dx + 0.5*self.bin_size
            self.bottom_edges = self.dy - 0.5*self.bin_size
            self.top_edges = self.dy + 0.5*self.bin_size
            self.rnom = np.sqrt(self.dx**2 + self.dy**2)
            self.logr = np.zeros_like(self.rnom)
            np.log(self.rnom, out=self.logr, where=self.rnom > 0)
            self.logr[self.rnom==0.] = -np.inf
            self._nbins = self.nbins**2
            self._bintype = treecorr._lib.TwoD
            min_log_bin_size = self.bin_size / self.max_sep
            max_log_bin_size = self.bin_size / (self.min_sep + self.bin_size/2)
            max_good_slop = 0.1 / max_log_bin_size
        else:  # pragma: no cover  (Already checked by config layer)
            raise ValueError("Invalid bin_type %s"%self.bin_type)

        if self.sep_units == '':
            self.logger.info("nbins = %d, min,max sep = %g..%g, bin_size = %g",
                             self.nbins, self.min_sep, self.max_sep, self.bin_size)
        else:
            self.logger.info("nbins = %d, min,max sep = %g..%g %s, bin_size = %g",
                             self.nbins, self.min_sep, self.max_sep, self.sep_units,
                             self.bin_size)
        # The underscore-prefixed names are in natural units (radians for angles)
        self._min_sep = self.min_sep * self._sep_units
        self._max_sep = self.max_sep * self._sep_units
github rmjarvis / TreeCorr / treecorr / binnedcorr2.py View on Github external
elif self.bin_size is None:
                self.bin_size = (self.max_sep-self.min_sep)/self.nbins
            elif self.max_sep is None:
                self.max_sep = self.min_sep + self.nbins*self.bin_size
            else:
                self.min_sep = self.max_sep - self.nbins*self.bin_size

            self.rnom = np.linspace(self.min_sep, self.max_sep, self.nbins, endpoint=False,
                                       dtype=float)
            # Offset by the position of the center of the first bin.
            self.rnom += 0.5*self.bin_size
            self.left_edges = self.rnom - 0.5*self.bin_size
            self.right_edges = self.rnom + 0.5*self.bin_size
            self.logr = np.log(self.rnom)
            self._nbins = self.nbins
            self._bintype = treecorr._lib.Linear
            min_log_bin_size = self.bin_size / self.max_sep
            max_log_bin_size = self.bin_size / (self.min_sep + self.bin_size/2)
            max_good_slop = 0.1 / max_log_bin_size
        elif self.bin_type == 'TwoD':
            if self.nbins is None:
                self.nbins = int(math.ceil(2.*self.max_sep / self.bin_size))
                self.bin_size = 2.*self.max_sep/self.nbins
            elif self.bin_size is None:
                self.bin_size = 2.*self.max_sep/self.nbins
            else:
                self.max_sep = self.nbins * self.bin_size / 2.

            sep = np.linspace(-self.max_sep, self.max_sep, self.nbins, endpoint=False,
                                 dtype=float)
            sep += 0.5 * self.bin_size
            self.dx, self.dy = np.meshgrid(sep, sep)