Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/kima-org/kima
Browse files Browse the repository at this point in the history
pull and merge
  • Loading branch information
ThomasBaycroft committed Jan 15, 2024
2 parents 8ca9e5f + f780f61 commit 756b8de
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 28 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest
environment:
name: deploy-to-pypi
url: https://pypi.org/p/kima
permissions:
id-token: write

steps:
- uses: actions/checkout@v3
Expand All @@ -31,7 +32,3 @@ jobs:

- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
user: __token__
password: ${{ secrets.PYPIT_API_TOKEN }}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dev = [
"nanobind",
"scikit-build-core[pyproject]",
"mkdocs",
"mkdocstrings[crystal,python]",
"mkdocstrings[python]",
"mkdocs-autorefs",
"mkdocs-material",
"mkdocs-git-revision-date-localized-plugin"
Expand Down
119 changes: 111 additions & 8 deletions src/kima/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ RVData::RVData(const vector<double> _t, const vector<double> _y, const vector<do
number_indicators = 0;
number_instruments = 1;
_instrument = instrument;
_instruments = {};

if (units == "kms")
{
Expand All @@ -71,6 +72,106 @@ RVData::RVData(const vector<double> _t, const vector<double> _y, const vector<do

}

/// @brief Load data from vectors directly, for multiple instruments
RVData::RVData(const vector<vector<double>> _t,
const vector<vector<double>> _y,
const vector<vector<double>> _sig,
const string& units, const vector<string>& instruments)
{
t.clear();
y.clear();
sig.clear();

y2.clear();
sig2.clear();

medians.clear();

if (_t.size() != _y.size())
{
string msg = "RVData: data arrays must have the same size size(t) != size(y)";
throw invalid_argument(msg);
}
if (_t.size() != _sig.size())
{
string msg = "RVData: data arrays must have the same size size(t) != size(sig)";
throw invalid_argument(msg);
}

for (size_t i = 0; i < _t.size(); i++)
{
t.insert(t.end(), _t[i].begin(), _t[i].end());
y.insert(y.end(), _y[i].begin(), _y[i].end());
sig.insert(sig.end(), _sig[i].begin(), _sig[i].end());

// store medians
medians.push_back(median(_y[i]));

for (size_t n = 0; n < _t[i].size(); n++)
obsi.push_back(i + 1);
}
actind.clear();

_datafile = "";
_datafiles = {};
_units = units;
_skip = 0;
_multi = true;
_indicator_names = {};
number_indicators = 0;
number_instruments = _t.size();
_instrument = "";
_instruments = instruments;

if (units == "kms")
{
for (size_t i = 0; i < N(); i++)
{
y[i] *= 1e3;
sig[i] *= 1e3;
}
}

// epoch for the mean anomaly, by default the time of the first observation
M0_epoch = t[0];

// How many points did we read?
if (VERBOSE)
printf("# Loaded %zu data points from arrays\n", t.size());

// What are the units?
if (units == "kms" && VERBOSE)
printf("# Multiplied all RVs by 1000; units are now m/s.\n");

// We need to sort t because it comes from different instruments
if (number_instruments > 1) {
size_t N = t.size();
vector<double> tt(N), yy(N);
vector<double> sigsig(N), obsiobsi(N);
vector<int> order(N);

// order = argsort(t)
int x = 0;
std::iota(order.begin(), order.end(), x++);
sort(order.begin(), order.end(),
[&](int i, int j) { return t[i] < t[j]; });

for (size_t i = 0; i < N; i++) {
tt[i] = t[order[i]];
yy[i] = y[order[i]];
sigsig[i] = sig[order[i]];
obsiobsi[i] = obsi[order[i]];
}

for (size_t i = 0; i < N; i++) {
t[i] = tt[i];
y[i] = yy[i];
sig[i] = sigsig[i];
obsi[i] = obsiobsi[i];
}
}

}



Expand Down Expand Up @@ -468,9 +569,6 @@ void RVData::load_multi(vector<string> filenames, const string units, int skip,
}
}

// store RV medians per instrument


// epoch for the mean anomaly, by default the time of the first observation
M0_epoch = t[0];
}
Expand Down Expand Up @@ -818,19 +916,23 @@ NB_MODULE(Data, m) {
.def("__call__", &loadtxt::operator());

//
nb::class_<RVData>(m, "RVData", "docs")
nb::class_<RVData>(m, "RVData", "Load and store RV data")
// constructors
.def(nb::init<const vector<string>&, const string&, int, int, const string&, const vector<string>&>(),
"filenames"_a, "units"_a="ms", "skip"_a=0, "max_rows"_a=0, "delimiter"_a=" ", "indicators"_a=vector<string>(),
.def(nb::init<const vector<string>&, const string&, int, int, const string&, const vector<string>&>(),
"filenames"_a, "units"_a="ms", "skip"_a=0, "max_rows"_a=0, "delimiter"_a=" ", "indicators"_a=vector<string>(),
"Load RV data from a list of files")
//
.def(nb::init<const string&, const string&, int, int, const string&, const vector<string>&>(),
.def(nb::init<const string&, const string&, int, int, const string&, const vector<string>&>(),
"filename"_a, "units"_a="ms", "skip"_a=0, "max_rows"_a=0, "delimiter"_a=" ", "indicators"_a=vector<string>(),
"Load RV data from a file")
//
.def(nb::init<const vector<double>, const vector<double>, const vector<double>, const string&, const string&>(),
"t"_a, "y"_a, "sig"_a, "units"_a="ms", "instrument"_a="",
"t"_a, "y"_a, "sig"_a, "units"_a="ms", "instrument"_a="",
"Load RV data from arrays")
//
.def(nb::init<const vector<vector<double>>, const vector<vector<double>>, const vector<vector<double>>, const string&, const vector<string>&>(),
"t"_a, "y"_a, "sig"_a, "units"_a="ms", "instruments"_a=vector<string>(),
"Load RV data from arrays, for multiple instruments")


// properties
Expand Down Expand Up @@ -867,6 +969,7 @@ NB_MODULE(Data, m) {
})
//
.def("get_timespan", &RVData::get_timespan)
.def("get_RV_span", &RVData::get_RV_span)
.def("topslope", &RVData::topslope)
// ...
.def("load", &RVData::load, "filename"_a, "units"_a, "skip"_a, "max_rows"_a, "delimiter"_a, "indicators"_a,
Expand Down
6 changes: 6 additions & 0 deletions src/kima/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class KIMA_API RVData {
public:
string _datafile;
string _instrument;
vector<string> _instruments;
vector<string> _datafiles;
string _units;
int _skip;
Expand All @@ -81,6 +82,11 @@ class KIMA_API RVData {

RVData(const vector<double> t, const vector<double> y, const vector<double> sig,
const string& units="ms", const string& instrument="");

RVData(const vector<vector<double>> t,
const vector<vector<double>> y,
const vector<vector<double>> sig,
const string& units="ms", const vector<string>& instruments={});

friend ostream& operator<<(ostream& os, const RVData& d);

Expand Down
16 changes: 13 additions & 3 deletions src/kima/Data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PHOTdata:

class RVData:
"""
docs
Load and store RV data
"""

@property
Expand All @@ -106,9 +106,9 @@ class RVData:
"""
...

def __init__(self, t: list[float], y: list[float], sig: list[float], units: str = 'ms', instrument: str = '') -> None:
def __init__(self, t: list[list[float]], y: list[list[float]], sig: list[list[float]], units: str = 'ms', instruments: list[str] = []) -> None:
"""
Load RV data from arrays
Load RV data from arrays, for multiple instruments
"""
...

Expand All @@ -126,13 +126,23 @@ class RVData:
"""
...

@overload
def __init__(self, t: list[float], y: list[float], sig: list[float], units: str = 'ms', instrument: str = '') -> None:
"""
Load RV data from arrays
"""
...

@property
def actind(self) -> list[list[float]]:
"""
Activity indicators
"""
...

def get_RV_span(self) -> float:
...

def get_timespan(self) -> float:
...

Expand Down
2 changes: 1 addition & 1 deletion src/kima/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# kima.run
from .Sampler import run
# kima.load_results
from .pykima.results import load_results
from .pykima.results import load_results, KimaResults

# sub-packages
from .kepler import keplerian
Expand Down
17 changes: 17 additions & 0 deletions src/kima/distributions.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <sstream>

#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
namespace nb = nanobind;
using namespace nb::literals;

Expand All @@ -19,92 +22,106 @@ NB_MODULE(distributions, m)
// Cauchy.cpp
nb::class_<DNest4::Cauchy, DNest4::ContinuousDistribution>(m, "Cauchy", "Cauchy distribution")
.def(nb::init<double, double>(), "loc"_a, "scale"_a)
.def("__repr__", [](const DNest4::Cauchy &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Cauchy::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Cauchy::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Cauchy::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");
nb::class_<DNest4::TruncatedCauchy, DNest4::ContinuousDistribution>(m, "TruncatedCauchy", "docs")
.def(nb::init<double, double, double, double>(), "loc"_a, "scale"_a, "lower"_a, "upper"_a)
.def("__repr__", [](const DNest4::TruncatedCauchy &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::TruncatedCauchy::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::TruncatedCauchy::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::TruncatedCauchy::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Exponential.cpp
nb::class_<DNest4::Exponential, DNest4::ContinuousDistribution>(m, "Exponential", "Exponential distribution")
.def(nb::init<double>(), "scale"_a)
.def("__repr__", [](const DNest4::Exponential &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Exponential::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Exponential::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Exponential::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

nb::class_<DNest4::TruncatedExponential, DNest4::ContinuousDistribution>(m, "TruncatedExponential", "Exponential distribution truncated to [lower, upper]")
.def(nb::init<double, double, double>(), "scale"_a, "lower"_a, "upper"_a)
.def("__repr__", [](const DNest4::TruncatedExponential &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::TruncatedExponential::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::TruncatedExponential::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::TruncatedExponential::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Fixed.cpp
nb::class_<DNest4::Fixed, DNest4::ContinuousDistribution>(m, "Fixed", "'Fixed' distribution")
.def(nb::init<double>(), "value"_a)
.def("__repr__", [](const DNest4::Fixed &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Fixed::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Fixed::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Fixed::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Gaussian.cpp
nb::class_<DNest4::Gaussian, DNest4::ContinuousDistribution>(m, "Gaussian", "Gaussian distribution")
.def(nb::init<double, double>(), "loc"_a, "scale"_a)
.def("__repr__", [](const DNest4::Gaussian &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Gaussian::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Gaussian::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Gaussian::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Kumaraswamy.cpp
nb::class_<DNest4::Kumaraswamy, DNest4::ContinuousDistribution>(m, "Kumaraswamy", "Kumaraswamy distribution (similar to a Beta distribution)")
.def(nb::init<double, double>(), "a"_a, "b"_a)
.def("__repr__", [](const DNest4::Kumaraswamy &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Kumaraswamy::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Kumaraswamy::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Kumaraswamy::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Laplace.cpp
nb::class_<DNest4::Laplace, DNest4::ContinuousDistribution>(m, "Laplace", "Laplace distribution")
.def(nb::init<double, double>(), "loc"_a, "scale"_a)
.def("__repr__", [](const DNest4::Laplace &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Laplace::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Laplace::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Laplace::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// LogUniform.cpp
nb::class_<DNest4::LogUniform, DNest4::ContinuousDistribution>(m, "LogUniform", "LogUniform distribution (sometimes called reciprocal or Jeffrey's distribution)")
.def(nb::init<double, double>(), "lower"_a, "upper"_a)
.def("__repr__", [](const DNest4::LogUniform &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::LogUniform::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::LogUniform::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::LogUniform::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

nb::class_<DNest4::ModifiedLogUniform, DNest4::ContinuousDistribution>(m, "ModifiedLogUniform", "ModifiedLogUniform distribution")
.def(nb::init<double, double>(), "knee"_a, "upper"_a)
.def("__repr__", [](const DNest4::ModifiedLogUniform &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::ModifiedLogUniform::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::ModifiedLogUniform::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::ModifiedLogUniform::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Rayleigh.cpp
nb::class_<DNest4::Rayleigh, DNest4::ContinuousDistribution>(m, "Rayleigh", "Rayleigh distribution")
.def(nb::init<double>(), "scale"_a)
.def("__repr__", [](const DNest4::Rayleigh &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Rayleigh::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Rayleigh::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Rayleigh::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

nb::class_<DNest4::TruncatedRayleigh, DNest4::ContinuousDistribution>(m, "TruncatedRayleigh", "Rayleigh distribution truncated to [lower, upper]")
.def(nb::init<double, double, double>(), "scale"_a, "lower"_a, "upper"_a)
.def("__repr__", [](const DNest4::TruncatedRayleigh &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::TruncatedRayleigh::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::TruncatedRayleigh::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::TruncatedRayleigh::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Triangular.cpp
nb::class_<DNest4::Triangular, DNest4::ContinuousDistribution>(m, "Triangular", "Triangular distribution")
.def(nb::init<double, double, double>(), "lower"_a, "center"_a, "upper"_a)
.def("__repr__", [](const DNest4::Triangular &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Triangular::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Triangular::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Triangular::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");

// Uniform.cpp
nb::class_<DNest4::Uniform, DNest4::ContinuousDistribution>(m, "Uniform", "Uniform distribuion in [lower, upper]")
.def(nb::init<double, double>(), "lower"_a, "upper"_a)
.def("__repr__", [](const DNest4::Uniform &d){ std::ostringstream out; d.print(out); return out.str(); })
.def("cdf", &DNest4::Uniform::cdf, "x"_a, "Cumulative distribution function evaluated at `x`")
.def("ppf", &DNest4::Uniform::cdf_inverse, "q"_a, "Percent point function (inverse of cdf) evaluated at `q`")
.def("logpdf", &DNest4::Uniform::log_pdf, "x"_a, "Log of the probability density function evaluated at `x`");
Expand Down
Loading

0 comments on commit 756b8de

Please sign in to comment.