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
80 changes: 80 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: CI

on:
push:
branches: [master]
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
c:
name: C (${{ matrix.os }}, ${{ matrix.cc }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
cc: gcc
- os: ubuntu-latest
cc: clang
- os: macos-latest
cc: clang
steps:
- uses: actions/checkout@v5
- name: Install GSL (Ubuntu)
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y libgsl-dev
- name: Install GSL (macOS)
if: runner.os == 'macOS'
run: |
brew install gsl
# Homebrew's prefix is not on Apple clang's default search paths
echo "CPATH=$(brew --prefix gsl)/include" >> "$GITHUB_ENV"
echo "LIBRARY_PATH=$(brew --prefix gsl)/lib" >> "$GITHUB_ENV"
- name: Compiler version
run: ${{ matrix.cc }} --version
- name: Build
working-directory: C
run: make CC=${{ matrix.cc }}
- name: Test (values against hctsa v1.03)
working-directory: C
run: make test CC=${{ matrix.cc }}

python:
name: Python ${{ matrix.python-version }} (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
python-version: "3.9"
- os: ubuntu-latest
python-version: "3.13"
- os: macos-latest
python-version: "3.13"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install GSL (Ubuntu)
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y libgsl-dev
- name: Install GSL (macOS)
if: runner.os == 'macOS'
run: brew install gsl
- name: Install build and test dependencies
run: python -m pip install setuptools numpy pytest
- name: Build and install the extension
working-directory: Python
run: |
python setup_P3.py build
python setup_P3.py install
- name: Test (values against hctsa v1.03, input handling)
working-directory: Python
run: python -m pytest -v test_features.py
30 changes: 20 additions & 10 deletions Matlab/M_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void M_wrapper_double( int nlhs, mxArray *plhs[],
double (*f) (const double*, const int), int normalize )

{
double *inMatrix; /* 1xN input matrix */
double *inMatrix; /* input vector (1xN or Nx1) */
int nrows;
double *outMatrix; /* output matrix */

Expand All @@ -26,19 +26,27 @@ void M_wrapper_double( int nlhs, mxArray *plhs[],
mexErrMsgIdAndTxt("catchaMouse16:notDouble",
"Input vector must be type double.");
}
if(mxGetN(prhs[0]) != 1) {
mexErrMsgIdAndTxt("catchaMouse16:notColumnVector",
"Input must be a column vector.");
// accept a row or a column vector (the data are contiguous either way)
if(!mxIsEmpty(prhs[0]) && (mxGetNumberOfDimensions(prhs[0]) != 2 ||
(mxGetM(prhs[0]) != 1 && mxGetN(prhs[0]) != 1))) {
mexErrMsgIdAndTxt("catchaMouse16:notVector",
"Input must be a vector.");
}

// get input
inMatrix = mxGetPr(prhs[0]);
nrows = mxGetM(prhs[0]);
nrows = (int) mxGetNumberOfElements(prhs[0]);

// set output
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
outMatrix = mxGetPr(plhs[0]);

// an empty series has no features
if (nrows == 0) {
outMatrix[0] = mxGetNaN();
return;
}

// calculate result
if (normalize){

Expand All @@ -62,7 +70,7 @@ void M_wrapper_int( int nlhs, mxArray *plhs[],
int (*f) (const double*, const int), int normalize )

{
double *inMatrix; /* 1xN input matrix */
double *inMatrix; /* input vector (1xN or Nx1) */
int nrows;
int *outMatrix; /* output matrix */

Expand All @@ -80,14 +88,16 @@ void M_wrapper_int( int nlhs, mxArray *plhs[],
mexErrMsgIdAndTxt("catchaMouse16:notDouble",
"Input vector must be type double.");
}
if(mxGetN(prhs[0]) != 1) {
mexErrMsgIdAndTxt("catchaMouse16:notColumnVector",
"Input must be a column vector.");
// accept a row or a column vector (the data are contiguous either way)
if(!mxIsEmpty(prhs[0]) && (mxGetNumberOfDimensions(prhs[0]) != 2 ||
(mxGetM(prhs[0]) != 1 && mxGetN(prhs[0]) != 1))) {
mexErrMsgIdAndTxt("catchaMouse16:notVector",
"Input must be a vector.");
}

// get input
inMatrix = mxGetPr(prhs[0]);
nrows = mxGetM(prhs[0]);
nrows = (int) mxGetNumberOfElements(prhs[0]);

// set output
plhs[0] = mxCreateNumericMatrix(1,1, mxINT32_CLASS, mxREAL);
Expand Down
16 changes: 15 additions & 1 deletion Matlab/catchaMouse16_all.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function [featureValues, featureNames] = catchaMouse16_all(data)
% catchaMouse16_all Compute all 16 catchaMouse16 features of a time series.
%
% [featureValues, featureNames] = catchaMouse16_all(data)
%
% data is a row or column vector. Each feature z-scores the series first.
% Run mexAll first to compile the feature functions.

if ~(isnumeric(data) || islogical(data)) || ~isreal(data)
error('catchaMouse16:notReal', 'Input must be a real numeric vector.');
end
if ~isvector(data) && ~isempty(data)
error('catchaMouse16:notVector', 'Input must be a vector (row or column).');
end
data = double(data(:)); % the feature functions take a double column vector

featureNames = ...
{ ...
Expand Down Expand Up @@ -27,7 +41,7 @@

fh = str2func(['catchaMouse16_', featureName]);

featureValues(featureInd) = fh(data');
featureValues(featureInd) = fh(data);

end

Expand Down
125 changes: 104 additions & 21 deletions Matlab/mexAll.m
Original file line number Diff line number Diff line change
@@ -1,33 +1,116 @@
% path where C implementation lives
basePath = '../C/src';
ipath = {['-I', basePath], ['-I','/usr/local/include'], '-I.'};
function mexAll(gslPrefix)
% mexAll Compile the catchaMouse16 MEX functions (needs GSL).
%
% mexAll finds GSL with gsl-config, or else in /opt/homebrew,
% /usr/local or /usr
% mexAll(prefix) uses the GSL installed under prefix (prefix/include and
% prefix/lib), e.g., mexAll('/opt/homebrew') or
% mexAll(getenv('CONDA_PREFIX'))
%
% The MEX files are written to the folder that contains this file.

% list all C files to include in the mex-call
CFiles = dir(basePath);
CFiles = CFiles(cellfun(@(x) contains(x, '.c'), {CFiles.name}));
CFileNames = {CFiles.name};
% folder of this file, and where the C implementation lives
matlabDir = fileparts(mfilename('fullpath'));
basePath = fullfile(matlabDir, '..', 'C', 'src');

if nargin < 1
gslPrefix = '';
end
[gslIncludeDirs, gslLibDirs] = findGSL(gslPrefix);
fprintf('Using GSL headers in %s\n', strjoin(gslIncludeDirs, ', '));

ipath = [{['-I', basePath], ['-I', matlabDir]}, strcat('-I', gslIncludeDirs)];
lpath = strcat('-L', gslLibDirs);
% find GSL's shared libraries at run time too (e.g., conda's @rpath libraries)
if isunix && ~isempty(gslLibDirs)
lpath{end+1} = ['LDFLAGS=$LDFLAGS', sprintf(' -Wl,-rpath,%s', gslLibDirs{:})];
end

% list all C files to include in the mex-call (not the command-line program)
CFiles = dir(fullfile(basePath, '*.c'));
CFileNames = setdiff({CFiles.name}, {'main.c'});

% add path to the c file names
includeFiles = cellfun(@(x) fullfile(basePath, x), CFileNames, 'UniformOutput', false);

% get function names
fid = fopen('../featureList.txt','r');
i = 1;
tline = fgetl(fid);
featureNames{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
featureNames{i} = tline;
end
fclose(fid);
featureNames = strsplit(strtrim(fileread(fullfile(matlabDir, '..', 'featureList.txt'))));

% mex all feature functions separately
for i = 1:length(featureNames)-1
for i = 1:length(featureNames)

featureName = featureNames{i};

fprintf('Compiling %s...\n', featureName);
mex(ipath{:}, ['catchaMouse16_', featureName,'.c'], 'M_wrapper.c', includeFiles{:}, ['-L','/usr/local/lib'], '-lgsl', '-lgslcblas', '-lm')%, '-v', 'OPTIMFLAGS=-O2')
mex(ipath{:}, fullfile(matlabDir, ['catchaMouse16_', featureName, '.c']), ...
fullfile(matlabDir, 'M_wrapper.c'), includeFiles{:}, lpath{:}, ...
'-lgsl', '-lgslcblas', '-lm', '-outdir', matlabDir)
fprintf('\n');
end

end

%-------------------------------------------------------------------------------
function [includeDirs, libDirs] = findGSL(gslPrefix)
% Find GSL's include and library directories.

hasHeaders = @(includeDir) exist(fullfile(includeDir, 'gsl', 'gsl_vector.h'), 'file') == 2;

% 1. a prefix given by the user
if ~isempty(gslPrefix)
includeDirs = {fullfile(gslPrefix, 'include')};
libDirs = {fullfile(gslPrefix, 'lib')};
if ~hasHeaders(includeDirs{1})
error('catchaMouse16:noGSL', 'GSL headers not found in %s', includeDirs{1});
end
return
end

% 2. gsl-config (MATLAB's PATH may not include Homebrew or conda, so also
% look in their usual bin folders)
gslConfigs = {'gsl-config', '/opt/homebrew/bin/gsl-config', '/usr/local/bin/gsl-config'};
condaPrefix = getenv('CONDA_PREFIX');
if ~isempty(condaPrefix)
gslConfigs = [{fullfile(condaPrefix, 'bin', 'gsl-config')}, gslConfigs];
end
for k = 1:length(gslConfigs)
[status, cflags] = system([gslConfigs{k}, ' --cflags']);
if status ~= 0
continue
end
[status, libs] = system([gslConfigs{k}, ' --libs']);
if status ~= 0
continue
end
includeDirs = flagValues(cflags, '-I');
libDirs = flagValues(libs, '-L');
if any(cellfun(hasHeaders, includeDirs))
return
end
end

% 3. the usual install locations
prefixes = {'/opt/homebrew', '/usr/local', '/usr'};
for k = 1:length(prefixes)
if hasHeaders(fullfile(prefixes{k}, 'include'))
includeDirs = {fullfile(prefixes{k}, 'include')};
if strcmp(prefixes{k}, '/usr')
libDirs = {}; % the linker searches the system folders already
else
libDirs = {fullfile(prefixes{k}, 'lib')};
end
return
end
end

error('catchaMouse16:noGSL', ['GSL not found. Install GSL (e.g., brew install gsl, ', ...
'or apt install libgsl-dev) or call mexAll(prefix) with its install prefix.']);
end

%-------------------------------------------------------------------------------
function values = flagValues(flags, flag)
% Values of the compiler flags that start with flag, e.g., '-I' or '-L'.
tokens = strsplit(strtrim(flags));
tokens = tokens(startsWith(tokens, flag));
values = cellfun(@(t) t(length(flag)+1:end), tokens, 'UniformOutput', false);
end
Loading
Loading