Quickstart

Install

pip install bitvf

installs the Python package, with otswap, which bitvf uses for the reconstruction. bitvf and otswap must be installed in the same environment, as pip does.

The C++ library and the bitvf and bitvf-field executables are built and installed with CMake. The build needs GCC or Clang (C, and C++17) and cfitsio, uses OpenMP when found, and fetches otswap and meshsearch when they are not installed:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
cmake --install build --prefix /path/to/prefix

A CMake project of your own then uses the library with

find_package(bitvf 0.1 CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE bitvf::bitvf)

A first lightcone run

The lightcone geometry is that of a galaxy survey: tracers observed on a part of the sky, each with a right ascension, a declination and a redshift. This first run finds the voids of a small survey-like catalogue shipped with the repository, in examples/data: about 30,000 tracers between redshifts 0.885 and 1.10, a random catalogue, a set of points with the same footprint and redshift distribution as the tracers but no clustering, and the footprint itself as a HEALPix mask, a map of the pixels of the sky that the survey observed. The data come with a clone of the repository, not with the Python package:

git clone https://github.com/SimoSartori/bitvf.git
cd bitvf

The run has two steps. otswap first reconstructs the displacements, the vectors that move each tracer back in time to a nearly uniform distribution; bitvf then computes their divergence on a grid and finds the voids. The code below is the core of the lightcone example, where data is the data folder and output the folder the files are written to:

# Right ascension and declination in degrees, and redshift, shape (N, 3).
# They are passed as they are, with angle_unit="deg": the Python
# interface converts at its boundary
tracers_sky = otswap.io.read(data / "lightcone_tracers.dat", [0, 1, 2]).values
randoms_sky = otswap.io.read(data / "lightcone_randoms.dat", [0, 1, 2]).values

print(f"Tracers read: {len(tracers_sky)}")
print(f"Randoms read: {len(randoms_sky)}, {len(randoms_sky) / len(tracers_sky):g} per tracer")

# ---------------------------------------
# ------------ Read the mask ------------
# ---------------------------------------

# A HEALPix map, RING or NESTED, from a FITS file, here gzip-compressed:
# a pixel is observed when its value is greater than 0. Tracers on
# unobserved pixels keep their row in every per-tracer output, flagged,
# and take no part
mask = otswap.Mask(str(data / "lightcone_mask.fits.gz"))

print(f"Mask: NSIDE {mask.nside}, {mask.sky_area_deg2:g} deg^2")

# ----------------------------------------------------
# ------------ Set the cosmological model ------------
# ----------------------------------------------------

# The comoving distances, from Omega_m, h, w0 and wa of a flat
# cosmology, tabulated by default from z = 0 to 10. The table must cover
# the redshifts of tracers and randoms
distances = otswap.DistanceTable.flat(0.286, 0.7, w0=-1.0, wa=0.0)

# ------------------------------------------------
# ------------ Run the reconstruction ------------
# ------------------------------------------------

# 8 realizations, a convergence threshold of 1e-2 and a fixed seed, so
# that the run can be repeated; displacements that cross an unobserved
# pixel of the mask are rejected. Each realization uses as many randoms
# as there are tracers, so at least 8 per tracer are needed: 30 per
# tracer, the minimum with otswap's default of 30 realizations, is a
# good start
ot_config = otswap.Config(n_realizations=8, convergence=1e-2, seed=12345, reject_crossings=True,
                          max_unobserved_pixels_crossed=0, verbosity="normal")

# The mean particle separation (mps) is measured in 30 redshift bins;
# each needs enough tracers, so use fewer bins for a small sample. The
# redshift cut spans the whole catalogue: narrow it to search a slice
reconstruction = otswap.reconstruct_lightcone(tracers_sky, randoms_sky, mask=mask, n_bins=30,
                                              distances=distances, angle_unit="deg",
                                              redshift_cut=otswap.RedshiftCut(0.885, 1.10),
                                              config=ot_config)

# ---------------------------------------------
# ------------ Find the voids -----------------
# ---------------------------------------------

# The search reads everything it needs from the reconstruction: the
# tracers, their displacements in each realization, the mps(z) profile,
# the distance table and the redshift cut. The randoms are given again
# because the grid and the mean density of each cell are built on them.
#
# The two parameters that change the result:
# - the smoothing: a Gaussian of width sigma, here 1 mps, the default and
#   a reasonable starting value; a larger sigma merges small basins into
#   larger ones;
# - the cell size, left to its default: 1 mps up to 20 realizations,
#   (20 / n_realizations)^(1/3) mps above, measured at the centre of each
#   shell.
#
# keep_assignment keeps, for each tracer, the basin it ended up in, which
# the galaxies file records. "detailed" prints one line per step
config = bitvf.Config(smoothing=bitvf.Smoothing(sigma=1.0, unit=bitvf.LengthUnit.MPS),
                      keep_assignment=True, verbosity="detailed")
result = bitvf.find_voids_lightcone(reconstruction, randoms_sky, mask=mask, angle_unit="deg", config=config)

# ----------------------------------------------
# ------------ Summarize the result ------------
# ----------------------------------------------

# Each catalogue has one row per basin, in the order of its seed cell.
# radius is the radius of the sphere with the volume of the void's core,
# the part where the divergence is below 0, in Mpc/h; edge_flag is 0 for
# a void that does not touch the edges of the grid, where the flow is not
# known
voids = result.voids
inside = int((voids.edge_flag == 0).sum())

print(f"Tracers on unobserved pixels: {result.counts.tracers_outside_mask}")
print(f"Voids: {voids.n_basins}, {inside} not touching the edges of the grid")
print(f"Largest void radius: {voids.radius.max():g} Mpc/h")
print(f"Overdense basins: {result.overdense.n_basins}")

# ------------------------------------------
# ------------ Write the output ------------
# ------------------------------------------

# One file per product, ASCII from the extension: the voids and the
# overdense basins, one row per basin, and where each tracer ended up,
# one row per tracer. Angles are written in degrees. io.file_name gives
# the conventional names, voids_lightcone.dat,
# overdense_basins_lightcone.dat and galaxies_lightcone.dat. The writers
# are the C++ library's, so the files are the C++ example's, byte for
# byte
product = bitvf.io.Product
bitvf.io.write_voids(bitvf.io.file_name(product.VOIDS, output, "lightcone.dat", zspace=False), result)
bitvf.io.write_overdense(bitvf.io.file_name(product.OVERDENSE, output, "lightcone.dat", zspace=False), result)
bitvf.io.write_galaxies(bitvf.io.file_name(product.GALAXIES, output, "lightcone.dat", zspace=False), result)
// Right ascension, declination and redshift for each object, one row
// after the other. The angles are read in degrees and converted to
// radians by skyToRadians: the C++ library works in radians
const std::vector<double> tracersSky =
  otswap::skyToRadians(otswap::io::read(data + "/lightcone_tracers.dat", {"0", "1", "2"}).values);
const std::vector<double> randomsSky =
  otswap::skyToRadians(otswap::io::read(data + "/lightcone_randoms.dat", {"0", "1", "2"}).values);

const std::size_t nTracers = tracersSky.size() / 3;
const std::size_t nRandoms = randomsSky.size() / 3;

std::cout << "Tracers read: " << nTracers << std::endl;
std::cout << "Randoms read: " << nRandoms << ", " << (double)nRandoms / (double)nTracers << " per tracer"
          << std::endl;


// ---------------------------------------
// ------------ Read the mask ------------
// ---------------------------------------

// A HEALPix map, RING or NESTED, from a FITS file, here gzip-compressed:
// a pixel is observed when its value is greater than 0. Tracers on
// unobserved pixels keep their row in every per-tracer output, flagged,
// and take no part
const otswap::Mask mask(data + "/lightcone_mask.fits.gz");

std::cout << "Mask: NSIDE " << mask.nside() << ", " << mask.skyAreaDeg2() << " deg^2" << std::endl;


// ----------------------------------------------------
// ------------ Set the cosmological model ------------
// ----------------------------------------------------

// The comoving distances, from Omega_m, h, w0 and wa of a flat
// cosmology, tabulated by default from z = 0 to 10. The table must
// cover the redshifts of tracers and randoms
const otswap::DistanceTable distances(0.286, 0.7, -1., 0.);


// ------------------------------------------------
// ------------ Run the reconstruction ------------
// ------------------------------------------------

// 8 realizations, a convergence threshold of 1e-2 and a fixed seed, so
// that the run can be repeated; displacements that cross an unobserved
// pixel of the mask are rejected. Each realization uses as many randoms
// as there are tracers, so at least 8 per tracer are needed: 30 per
// tracer, the minimum with otswap's default of 30 realizations, is a
// good start. The report goes to std::cout, in order with
// this program's lines
otswap::Config otConfig;
otConfig.nRealizations = 8;
otConfig.convergence = 1.e-2;
otConfig.seed = 12345;
otConfig.rejectCrossings = true;
otConfig.maxUnobservedPixelsCrossed = 0;
otConfig.verbosity = otswap::Verbosity::Normal;
otConfig.log = &std::cout;

// The mean particle separation (mps) is measured in 30 redshift bins;
// each needs enough tracers, so use fewer bins for a small sample. The
// redshift cut spans the whole catalogue: narrow it to search a slice
const unsigned nBins = 30;
const otswap::RedshiftCut cut{0.885, 1.10};

const otswap::Result reconstruction =
  otswap::reconstructLightcone(tracersSky, randomsSky, mask, nBins, distances, otConfig, cut);


// ---------------------------------------------
// ------------ Find the voids -----------------
// ---------------------------------------------

// The search reads everything it needs from the reconstruction: the
// tracers, their displacements in each realization, the mps(z) profile,
// the distance table and the redshift cut. The randoms are given again
// because the grid and the mean density of each cell are built on them.
//
// The two parameters that change the result:
// - the smoothing: a Gaussian of width sigma, here 1 mps, the default
//   and a reasonable starting value; a larger sigma merges small basins
//   into larger ones;
// - the cell size, left to its default: 1 mps up to 20 realizations,
//   (20 / nRealizations)^(1/3) mps above, measured at the centre of
//   each shell.
//
// keepAssignment keeps, for each tracer, the basin it ended up in, which
// the galaxies file records. Detailed prints one line per step
bitvf::Config config;
config.smoothing.sigma = 1.;
config.smoothing.unit = bitvf::LengthUnit::Mps;
config.keepAssignment = true;
config.verbosity = otswap::Verbosity::Detailed;
config.log = &std::cout;

const bitvf::Result result = bitvf::findVoidsLightcone(reconstruction, randomsSky, mask, config);


// ----------------------------------------------
// ------------ Summarize the result ------------
// ----------------------------------------------

// Each catalogue has one row per basin, in the order of its seed cell.
// radius is the radius of the sphere with the volume of the void's
// core, the part where the divergence is below 0, in Mpc/h; edgeFlag is
// 0 for a void that does not touch the edges of the grid, where the
// flow is not known
const bitvf::BasinCatalog& voids = result.voids;
std::size_t inside = 0;
double largest = 0.;
for (std::size_t b = 0; b < voids.nBasins; ++b) {
  if (voids.edgeFlag[b] == 0) ++inside;
  largest = std::max(largest, voids.radius[b]);
}

std::cout << "Tracers on unobserved pixels: " << result.counts.tracersOutsideMask << std::endl;
std::cout << "Voids: " << voids.nBasins << ", " << inside << " not touching the edges of the grid" << std::endl;
std::cout << "Largest void radius: " << largest << " Mpc/h" << std::endl;
std::cout << "Overdense basins: " << result.overdense.nBasins << std::endl;


// ------------------------------------------
// ------------ Write the output ------------
// ------------------------------------------

// One file per product, ASCII from the extension: the voids and the
// overdense basins, one row per basin, and where each tracer ended up,
// one row per tracer. Angles are written in degrees. io::fileName gives
// the conventional names, voids_lightcone.dat,
// overdense_basins_lightcone.dat and galaxies_lightcone.dat
const std::string voidsFile = bitvf::io::fileName(bitvf::io::Product::Voids, output, "lightcone.dat", false);
const std::string overdenseFile = bitvf::io::fileName(bitvf::io::Product::Overdense, output, "lightcone.dat", false);
const std::string galaxiesFile = bitvf::io::fileName(bitvf::io::Product::Galaxies, output, "lightcone.dat", false);

bitvf::io::writeVoids(voidsFile, result);
bitvf::io::writeOverdense(overdenseFile, result);
bitvf::io::writeGalaxies(galaxiesFile, result);

A few points of the code deserve a word.

  • Angles. The catalogues give the angles in degrees. Python takes them as they are, with angle_unit="deg", and returns angles in the same unit; the C++ library works in radians, so the program converts them with otswap::skyToRadians.

  • Randoms. They describe the survey, so they must follow its footprint, its redshift distribution and its systematics. Each realization of the reconstruction uses as many randoms as there are tracers, so at least the realizations times the tracers are needed; this catalogue has about eight per tracer, enough for the 8 realizations of the run. For an analysis, 30 randoms per tracer, the minimum with otswap’s default of 30 realizations, is a reasonable start.

  • Cosmology. The distance table converts redshifts into comoving distances, here in Mpc/h, the unit of every length in the result.

  • Mean particle separation. The mps, the cube root of the volume per tracer, changes with redshift in a survey; the reconstruction measures it in 30 redshift bins, and it sets the size of the grid cells and the width of the smoothing.

  • Search. The search takes everything else from the reconstruction; the randoms and the mask are given again, because the grid is built on them.

To run the whole example in Python:

python examples/lightcone/lightcone.py

or in C++, with bitvf installed under <prefix>:

cmake -S examples/lightcone -B build/examples/lightcone -DCMAKE_PREFIX_PATH=<prefix>
cmake --build build/examples/lightcone
build/examples/lightcone/lightcone

Both print the steps as they run, then the number of voids and of overdense basins, and write three files to examples/lightcone/output/: voids_lightcone.dat, overdense_basins_lightcone.dat and galaxies_lightcone.dat, the last with the basin of every tracer. The two programs write the same files, byte for byte, with any number of threads.

Where to go next

  • The method explains what each step computes.

  • The lightcone guide explains the inputs and the parameters, the correction of redshift-space distortions, and how to read and check the results; the full reconstruction example runs the whole pipeline, correction included, in one call.

  • The lightcone executable runs the same search from a parameter file, with no code to write.

The box geometry

For a simulation snapshot, with tracers in Cartesian coordinates filling a rectangular volume, bitvf searches in the box geometry. There the randoms are optional, since otswap can draw them uniformly in the box, and no mask is needed: bitvf::runBox() (bitvf.run_box()) goes from the tracers to the voids in one call. The box guide and the box examples show how.