otswap

otswap reconstructs the Lagrangian displacement field of a catalogue of large-scale structure tracers, such as galaxies, quasars or dark matter halos, by discrete optimal transport. Each tracer is paired one to one with a point drawn from a random catalogue that samples the same volume uniformly, standing for the homogeneous initial conditions, the pairing being chosen to make the sum of squared distances between paired points small. The pairing is found by local search, swapping the partners of neighbouring tracers: an approximate solution of the assignment problem, not the exact optimum (see the method page).

The reconstruction is repeated on independent sets of N points extracted from the random catalogue. A tracer’s displacement is the mean, over these realizations, of the vector from the tracer to its partner; the mean Lagrangian position approximates the centroid of the Lagrangian volume from which the observed object has evolved, as the method page explains.

It handles boxes, periodic or not, as in simulations, and lightcones given on the sky or in comoving Cartesian coordinates, as in surveys; in a lightcone the footprint enters through a HEALPix mask and through a random catalogue reproducing the selection function, with an optional cut in redshift or distance, and displacements crossing unobserved regions are rejected. From the reconstructed displacements it corrects the catalogue for redshift-space distortions, given a fiducial cosmology and a bias b(z), and it writes its results as ASCII or FITS tables. For a given seed, results are identical bit for bit on every supported platform and compiler, whatever the number of threads. C++17, with a Python package on the same code.

The two APIs are one library: the C++ library and the Python package run the same code, and write the same files from the same values; angles given in degrees in Python are converted at the boundary, within rounding. Names side by side maps one to the other, and the guide covers what applies to both.

Install

pip install otswap

Wheels are built for CPython 3.10 to 3.14, on Linux (x86_64, aarch64) and macOS (x86_64, arm64). From a clone, pip install . builds the package from source, with the requirements below.

Build from source

Needs CMake 3.16 or later, GCC or Clang (C, and C++17) and cfitsio; OpenMP when found. meshsearch 1.0 or a later 1.x is used when installed, and 1.0.0 is fetched from GitHub otherwise. A pip build takes meshsearch from its build requirements (its wheel, from PyPI or a local wheelhouse) and fetches nothing from GitHub. The meshsearch CMake package of meshsearch’s Python wheel cannot be installed with otswap: configuring with it and the install rules fails with a message; point meshsearch_DIR to a CMake installation of meshsearch, remove the wheel’s package from the search path so that otswap fetches v1.0.0, or set OTSWAP_INSTALL=OFF.

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

This installs the library, its headers and CMake package, and the otswap executable, bin/otswap (see “The executable” in the documentation).

Set CFITSIO_ROOT (and ZLIB_ROOT for a static cfitsio) where cfitsio is not in a system path. The options:

  • OTSWAP_BUILD_TESTS: the C++ test suite, on when otswap is the top-level project;

  • OTSWAP_BUILD_PYTHON: the Python extension, off (pip install . sets it);

  • OTSWAP_INSTALL: the install rules;

  • OTSWAP_BUILD_EXECUTABLE: the otswap executable, installed into bin/, on when otswap is the top-level project;

  • OTSWAP_MESHSEARCH_REPOSITORY, OTSWAP_MESHSEARCH_TAG: where meshsearch is fetched from, when it is not installed; the repository may be a local path.

Using it from CMake

find_package(otswap 0.5 CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE otswap::otswap)

Configure your project with -DCMAKE_PREFIX_PATH=/path/to/prefix. otswap can also be built as part of your project, with add_subdirectory or FetchContent, and the same target. The dependencies the package looks for, and the OpenMP runtime the program must share with it, are in Using otswap from another package.

In a few lines

A box reconstruction of tracers given as an array, its mean displacement, and a file with one row per tracer:

import otswap

config = otswap.Config(n_realizations=8, seed=12345)

result = otswap.reconstruct_box(tracers, config=config)
print(result.mean_displacement[0])   # tracer 0
otswap.io.write_displacement_mean("displacements.dat", result)
#include <iostream>
#include "otswap/OT.h"

otswap::Config config;
config.nRealizations = 8;
config.seed = 12345;

otswap::Result result = otswap::reconstructBox(tracers, {}, config);
std::cout << result.meanDisplacement[0] << ' ' << result.meanDisplacement[1]
          << ' ' << result.meanDisplacement[2] << '\n';   // tracer 0
otswap::io::writeDisplacementMean("displacements.dat", result);

tracers is an array of shape (N, 3) in Python, a std::vector<double> of 3 * N entries in C++, in coordinate units. In a box, without randoms, otswap draws them in the bounding box of the tracers. Eight realizations keep the snippet fast; the default is 30. The file formats page gives the columns of the file.

This box is not periodic. For the whole volume of a simulation with periodic boundary conditions, give the box limits and periodic, as in the periodic box example; the guide describes each geometry step by step, and the examples read real catalogues and show every step.

The pages

  • Guide: the concepts and their use, in the order a new user needs them, with a path of steps for each geometry and starting values for the parameters.

  • Method: the problem otswap solves, how it solves it, and the redshift-space correction.

  • Examples: six programs, each in C++ and in Python, on the data of the repository.

  • The executable: a reconstruction and its correction from a parameter file, with no code to write.

  • File formats: the tables otswap reads and writes.

  • C++ reference and Python reference: every member, its arguments and the errors it raises.

  • Names side by side: the C++ and Python spelling of each member.

  • Using otswap from another package: linking the library, sharing its Python types, and loading the same OpenMP runtime.