Coder Social home page Coder Social logo

cds-healpix-rust's Introduction

cdshealpix-rust

CDS implementation of the HEALPix tesselation in Rust and modules to generate libraries in WebAssembly, Python, ...

API Documentation on docs.rs Rust

About

This library is an implementation in Rust of the HEALPix tesselation. This implementation has been made by the Strasbourg astronomical Data Centre (Centre de Données astronomique de Strasbourg, CDS).

It is used in:

Initially, it is a port of a part of the CDS Java library available here, but improvement have been added while porting the code.

For information on HEALPix in general, see:

Official implementations, are available here. It contains GPL v2 codes in Fortran, C++, Java, IDL, Python, ...

Other independent HEALPix implementations:

  • Astropy-healpix python wrapper using a C code (C code by Dustin Lang, python wrapper by Thomas Robitaille and others)
  • Javascript/Typescript implementation by Koike Michitaro
  • Julia implementation by Maurizio Tomasi
  • C "official" core functionalities implementation in BSD by Martin Reinecke
  • ... (Help me to add links to other HEALPix resources and codes).

Warning

For best performances on your specific hardware, you can compile using:

RUSTFLAGS='-C target-cpu=native' cargo build --release

This uses BMI2 instructions PDEP and PEXT, if supported by your processor, for bit interleaving.

However, the implementaion of those instructions on AMD Ryzen processors are extremely slow (20x slower than a lookup table, doubling the hash computation time)! You can test it usingi:

RUSTFLAGS='-C target-cpu=native' cargo bench

If the result of ZOrderCurve/BMI is slower thatn ZOrderCurve/LUPT, compile without the native support:

cargo build --release

Target 32 bit on a 64 bit linux

rustup target install i686-unknown-linux-gnu
sudo apt-get install gcc-multilib
RUSTFLAGS='-C target-cpu=native' cargo build --target=i686-unknown-linux-gnu --release

Features

  • Supports the HEALix Nested scheme
    • Supports approximated cone and elliptical cone coverage plus exact polygon coverage queries
    • Supports BMOC (MOC with a flag telling if a cell is fully or partially covered by a surface) as a result of cone, polygon ot elliptical cone coverage queries
    • Supports logical operations on BMOCs and BMOC creation from a list of cell number at a given depth
  • Supports the HEALPix Ring scheme with any NSIDE (i.e. not necessarilly powers of 2)

Missing Features

  • Not supported
    • Polygon and ellipse in the RING scheme
    • Spherical Harmonics computations
    • (Help me fill this)
  • Not yet implemented
    • Exact cone and ellipse solution (but using the custom approx methods, one can handle the rate of false positives)
    • Cone query in the RING scheme

Examples

Compute the cell number of a given position on the unit-sphere at a given HEALPix depth.

use cdshealpix::{nside};
use cdshealpix::nested::{get_or_create, Layer};


let depth = 12_u8;
let lon = 12.5_f64.to_radians();
let lat = 89.99999_f64.to_radians();

let nested_d12 = get_or_create(depth);
let nside = nside(depth) as u_64;
let expected_cell_number = nside * nside - 1

assert_eq!(expected_cell_number, nested_d12.hash(lon, lat));

Get the spherical coorinates of the 4 vertices of a given cell at a given depth:

use cdshealpix::nested::{get_or_create, Layer};

let depth = 12_u8;
let cell_number= 10_u64;

let nested_d12 = get_or_create(depth);

let [
  (lon_south, lat_south), 
  (lon_east,  lat_east), 
  (lon_north, lat_north), 
  (lon_west,  lat_west)
] = nested_d12.vertices(cell_number);

Get a hierarchical view (a MOC) on the cells overlapped by a given cone:

use cdshealpix::nested::{get_or_create, Layer};

let depth = 6_u8;
let nested_d6 = get_or_create(depth);

let lon = 13.158329_f64.to_radians();
let lat = -72.80028_f64.to_radians();
let radius = 5.64323_f64.to_radians();
 
let moc = nested_d6.cone_overlap_approx(lon, lat, radius);
for cell in moc.into_iter() {
    println!("cell: {:?}", cell);
}

Standalone

(Not on crates.io, but on github) The code source of the very beginning of a standalone exec can be found in cli/src/bin.rs.

WebAssembly

(Not on crates.io, but on github) To build and use the WebAssembly (and Javascript) files, the libwasmbingen directory. We rely on wasm-bingen.

Python

(Not on crates.io, but on github) See the libpython directory containing a very first integration in python using CFFI.

For a clean Python wrapper and associated Wheels, see Matthieu Baumann's project cds-healpix-python. To use the library in python, install it through pip (examples are provided on github cds-healpix-python):

pip install cdshealpix

ToDo list

  • Modify elliptical cone: compute distance to both foci
  • Implement the exact cone solution

License

Like most projects in Rust, this project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Disclaimer

It a first code in Rust, feel free to give some advice/feedback.

cds-healpix-rust's People

Contributors

berke avatar bmatthieu3 avatar fxpineau avatar gmantele avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cds-healpix-rust's Issues

Errors in tests

@fxpineau - If I try to run the tests I get these errors:

(gammapy-dev) hfm-1804a:cds-healpix-rust deil$ cargo test
warning: unused manifest key: package.edition
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling katex-doc v0.1.0                                                   
   Compiling cdshealpix v0.2.0 (file:///Users/deil/work/code/cds-healpix-rust)
error[E0433]: failed to resolve. Use of undeclared type or module `std`
   --> src/nested/bmoc.rs:908:35
    |
908 |   pub fn to_ranges(&self) -> Box<[std::ops::Range<u64>]> {
    |                                   ^^^ Use of undeclared type or module `std`

error[E0433]: failed to resolve. Use of undeclared type or module `std`
   --> src/nested/bmoc.rs:909:25
    |
909 |     let mut ranges: Vec<std::ops::Range<u64>> = Vec::with_capacity(self.entries.len());
    |                         ^^^ Use of undeclared type or module `std`

error[E0658]: `crate` in paths is experimental (see issue #45477)
 --> src/external_edge.rs:5:5
  |
5 | use crate::compass_point::{Cardinal, Ordinal};
  |     ^^^^^

error[E0658]: `crate` in paths is experimental (see issue #45477)
    --> src/lib.rs:1340:5
     |
1340 | use crate::compass_point::{MainWind};
     |     ^^^^^

error[E0658]: `crate` in paths is experimental (see issue #45477)
    --> src/lib.rs:1341:5
     |
1341 | use crate::compass_point::MainWind::*;
     |     ^^^^^

error: aborting due to 5 previous errors

Some errors occurred: E0433, E0658.
For more information about an error, try `rustc --explain E0433`.
error: Could not compile `cdshealpix`.
warning: build failed, waiting for other jobs to finish...
error[E0433]: failed to resolve. Use of undeclared type or module `std`
   --> src/nested/bmoc.rs:908:35
    |
908 |   pub fn to_ranges(&self) -> Box<[std::ops::Range<u64>]> {
    |                                   ^^^ Use of undeclared type or module `std`

error[E0433]: failed to resolve. Use of undeclared type or module `std`
   --> src/nested/bmoc.rs:909:25
    |
909 |     let mut ranges: Vec<std::ops::Range<u64>> = Vec::with_capacity(self.entries.len());
    |                         ^^^ Use of undeclared type or module `std`

error[E0554]: #![feature] may not be used on the stable release channel
 --> src/lib.rs:9:19
  |
9 | #![cfg_attr(test, feature(test))]
  |                   ^^^^^^^^^^^^^^

error[E0658]: `crate` in paths is experimental (see issue #45477)
 --> src/external_edge.rs:5:5
  |
5 | use crate::compass_point::{Cardinal, Ordinal};
  |     ^^^^^

error[E0658]: `crate` in paths is experimental (see issue #45477)
    --> src/lib.rs:1340:5
     |
1340 | use crate::compass_point::{MainWind};
     |     ^^^^^

error[E0658]: `crate` in paths is experimental (see issue #45477)
    --> src/lib.rs:1341:5
     |
1341 | use crate::compass_point::MainWind::*;
     |     ^^^^^

error: aborting due to 6 previous errors

Some errors occurred: E0433, E0554, E0658.
For more information about an error, try `rustc --explain E0433`.
error: Could not compile `cdshealpix`.

To learn more, run the command again with --verbose.

This is on macOS with rust installed via Homebrew. This is what I have:

(gammapy-dev) hfm-1804a:cds-healpix-rust deil$ rustc --version
rustc 1.27.0
(gammapy-dev) hfm-1804a:cds-healpix-rust deil$ cargo --version
cargo 1.26.0

Can you reproduce?
Do I need a newer version of rust?

Assertion failure on polygon_coverage with triangle crossing the antimeridian

Hello,

I get assertion failed: intersect2.lon() < p2.lon() at special_points_finder.rs:428:7 with the following code:

    let bad = [(f64::PI, 1.000),
	       (-f64::PI + 1e-3, 1.001),
	       (f64::PI - 1e-3, 1.002)];
    let lay = hp::nested::get(4);
    let _ = lay.polygon_coverage(&bad,true);

This doesn't happen if exact_solution is false.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.