Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Language: Cpp
BasedOnStyle: GNU
SortIncludes: false
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Linux
TabWidth: 4
IndentWidth: 4
ColumnLimit: 89
SpaceBeforeParens:
ControlStatements
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: true
IndentCaseLabels: true
AlignAfterOpenBracket: DontAlign
BinPackArguments: true
BinPackParameters: true
AlwaysBreakAfterReturnType: AllDefinitions

StatementMacros: ["PyObject_HEAD", "Py_BEGIN_ALLOW_THREADS", "Py_END_ALLOW_THREADS"]
AlignConsecutiveMacros: true
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
Docs:
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/docs.yml@v22
uses: tskit-dev/.github/.github/workflows/docs.yml@v23
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
Lint:
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/lint.yml@v22
uses: tskit-dev/.github/.github/workflows/lint.yml@v23
22 changes: 20 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,33 @@ jobs:
name: Python packaging
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/python-packaging.yml@v22
uses: tskit-dev/.github/.github/workflows/python-packaging.yml@v23
with:
cli-test-cmd: tsdate --help

test-c:
name: C tests
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/c-tests.yml@v23
with:
library-directory: lib
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

test-python-c:
name: Python-C tests
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/python-c-tests.yml@v23
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

test:
name: Python
permissions:
contents: read
uses: tskit-dev/.github/.github/workflows/python-tests.yml@v22
uses: tskit-dev/.github/.github/workflows/python-tests.yml@v23
with:
os: ${{ matrix.os }}
python-version: ${{ matrix.python }}
Expand Down
34 changes: 10 additions & 24 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,25 @@ permissions:
contents: read

jobs:
build:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
fetch-depth: 0
persist-credentials: false

- name: Install uv
uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0
with:
version: "0.13.6"
enable-cache: false

- name: Build
run: uv build

- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: dist
path: dist/
build-wheels:
uses: tskit-dev/.github/.github/workflows/build-wheels.yml@v23

publish:
runs-on: ubuntu-24.04
environment: release
needs: [build]
needs: [build-wheels]
permissions:
id-token: write
steps:
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
- name: Download artifacts
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: dist
pattern: build-*
path: dist
merge-multiple: true

- name: Show artifacts
run: ls -lah dist

- name: Publish to TestPyPI
if: github.event_name == 'push' && github.ref_name == 'test-publish'
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ evaluation/results/*
!evaluation/results/performance*.csv
!evaluation/results/performance*.png
evaluation/results/*_quick*

# C extension
*.so
lib/build*
build-*
*.gcda
*.gcno
6 changes: 6 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include lib/*.c
include lib/*.h
include _tsdatemodule.c
include README.md
include LICENSE
recursive-include tests *.py
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all: ext

ext:
uv run python setup.py build_ext --inplace

clean:
rm -f *.so *.o
rm -fR build lib/build
104 changes: 104 additions & 0 deletions _tsdatemodule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* MIT License
*
* Copyright (c) 2026 Tskit Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <Python.h>
#include <numpy/arrayobject.h>

#include "lib/tsdate.h"

#define MODULE_DOC "Low-level tsdate interface."

static PyObject *TsdLibraryError;

static void
handle_library_error(int err)
{
if (err == TSD_ERR_NO_MEMORY) {
PyErr_NoMemory();
} else {
PyErr_Format(TsdLibraryError, "%s", tsd_strerror(err));
}
}

/* Return the library's message for the given error code. */
static PyObject *
tsdate_strerror(PyObject *Py_UNUSED(self), PyObject *args)
{
PyObject *ret = NULL;
int err;

if (!PyArg_ParseTuple(args, "i", &err)) {
goto out;
}
ret = Py_BuildValue("s", tsd_strerror(err));
out:
return ret;
}

/* Raise the exception that the given library error code maps to. */
static PyObject *
tsdate_raise_library_error(PyObject *Py_UNUSED(self), PyObject *args)
{
PyObject *ret = NULL;
int err;

if (!PyArg_ParseTuple(args, "i", &err)) {
goto out;
}
handle_library_error(err);
out:
return ret;
}

static PyMethodDef tsdate_methods[] = {
{ "strerror", (PyCFunction) tsdate_strerror, METH_VARARGS,
"Return the message for a library error code." },
{ "raise_library_error", (PyCFunction) tsdate_raise_library_error, METH_VARARGS,
"Raise the Python exception corresponding to a library error code." },
{ NULL } /* Sentinel */
};

static struct PyModuleDef tsdatemodule = { PyModuleDef_HEAD_INIT, "_tsdate", MODULE_DOC,
-1, tsdate_methods, NULL, NULL, NULL, NULL };

PyObject *
PyInit__tsdate(void)
{
PyObject *module = PyModule_Create(&tsdatemodule);

if (module == NULL) {
return NULL;
}
/* Initialise numpy */
import_array();

TsdLibraryError = PyErr_NewException("_tsdate.LibraryError", NULL, NULL);
Py_INCREF(TsdLibraryError);
PyModule_AddObject(module, "LibraryError", TsdLibraryError);

return module;
}
25 changes: 25 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ignore:
- "lib/tests/"

codecov:
require_ci_to_pass: false

comment:
layout: "header, diff, flags, components" # show component info in the PR comment

component_management:
individual_components:
- component_id: python_code
name: Python API
paths:
- tsdate/*.py

- component_id: python_c_code
name: Python C interface
paths:
- _tsdatemodule.c

- component_id: c_code
name: C library
paths:
- lib
7 changes: 7 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ or

$ tsdate

Binary wheels are provided for the common platforms. Building from source (e.g.
from the sdist, or a development checkout) requires a C compiler, as `tsdate`
includes a C extension module (`_tsdate`).

Alternatively, the {ref}`Python API <sec_python_api>` allows more fine-grained control
of the inference process.

Expand All @@ -43,6 +47,9 @@ Install development dependencies with `uv sync` and run the tests with:

$ uv run pytest

`uv sync` also (re)builds the C extension. After editing C code, run `uv sync`
(or `make`) again to rebuild it.

_Tsdate_ makes extensive use of [numba](https://numba.pydata.org)'s
"just in time" (jit) compilation to speed up time-consuming numerical functions.
Because of the need to compile these functions, loading the tsdate package can take
Expand Down
62 changes: 62 additions & 0 deletions lib/err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2026 Tskit Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdlib.h>

#include "err.h"

const char *
tsd_strerror(int err)
{
const char *ret = "Unknown error";

switch (err) {
case 0:
ret = "Normal exit condition. This is not an error!";
break;
case TSD_ERR_GENERIC:
ret = "Generic tsdate error - please file a bug report.";
break;
case TSD_ERR_NO_MEMORY:
ret = "Out of memory";
break;
case TSD_ERR_BAD_PARAM:
ret = "Bad parameter value provided";
break;
default:
break;
}
return ret;
}

void
_tsd_safe_free(void **ptr)
{
if (ptr != NULL) {
if (*ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
}
Loading
Loading