SegmentSignalPy 0.1.3__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- C DLL/DLLExport.h +11 -0
- CPP Static Library/NoiseVarianceEstimateMethod.h +17 -0
- CPP Static Library/SegmentSignal.cpp +167 -0
- CPP Static Library/SegmentSignal.h +195 -0
- CPP Static Library/SegmentSignalFunctions.cpp +797 -0
- CPP Static Library/SegmentSignalFunctions.h +52 -0
- CPP Static Library/SegmentationResults.cpp +82 -0
- CPP Static Library/SegmentationResults.h +101 -0
- CPP Unit Tests/InputDataEnum.h +14 -0
- CPP Unit Tests/stdafx.h +13 -0
- CPP Unit Tests/targetver.h +8 -0
- Python Extension/SegmentationResultsPy.cpp +126 -0
- Python Extension/SegmentationResultsPy.h +119 -0
- Python Extension/module.cpp +140 -0
- Python Extension/setup.py +26 -0
- SegmentSignalPy.cp315-win_amd64.pyd +0 -0
- segmentsignalpy-0.1.3.dist-info/METADATA +72 -0
- segmentsignalpy-0.1.3.dist-info/RECORD +21 -0
- segmentsignalpy-0.1.3.dist-info/WHEEL +5 -0
- segmentsignalpy-0.1.3.dist-info/licenses/License.txt +20 -0
- segmentsignalpy-0.1.3.dist-info/top_level.txt +9 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#ifndef SEGMENTSIGNALFUNCTIONS_H
|
|
2
|
+
#define SEGMENTSIGNALFUNCTIONS_H
|
|
3
|
+
/*
|
|
4
|
+
C function calls for the signal segmentation libary.
|
|
5
|
+
These functions are where the work is performed.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// MAIN FUNCTION
|
|
9
|
+
// In Out
|
|
10
|
+
void SegmentSignal(double LOG[], int NSAMPS, double F, int ORDER, int ORDER1, int RMODE, int NITER, int Q[], int& NQ, double FLTLOG[], double SEGLOG[], double R[], double& C, double& D, int& NACT, int& IER);
|
|
11
|
+
|
|
12
|
+
// HELPER FUNCTIONS
|
|
13
|
+
// In Out
|
|
14
|
+
void SingleMostLikelihoodReplacement(double G[], double S[], double C, int NSAMPS, double D, int Q[], bool& CONV, int& IER);
|
|
15
|
+
|
|
16
|
+
// In Out
|
|
17
|
+
void Threshold(double JMPSEQ[], double C, double F, int NSAMPS, int Q[], double& D);
|
|
18
|
+
|
|
19
|
+
// In Out
|
|
20
|
+
void MovingAverage(double ARR1[], int NSAMPS, int ORDER, double ARR2[]);
|
|
21
|
+
|
|
22
|
+
// In Out
|
|
23
|
+
void KalmanFilter(double LOG[], int Q[], double C, double R[], int NSAMPS, double GAIN[], double ZTLT[], double ETA[], double STATE[]);
|
|
24
|
+
|
|
25
|
+
// In In/Out
|
|
26
|
+
void FixedIntervalOptimalSmoother(int Q[], int NSAMPS, double C, double ARR1[], double ARR2[], double ARR3[]);
|
|
27
|
+
|
|
28
|
+
// In Out
|
|
29
|
+
void AverageArraySegments(double ARR[], int Q[], int NSAMPS, double AVGOUT[]);
|
|
30
|
+
|
|
31
|
+
// In Intermediate Out
|
|
32
|
+
void DeglitchAndEstimateNoiseVariance(double LOG[], int Q[], int NSAMPS, int RMODE, double WORK1[], double R[]);
|
|
33
|
+
|
|
34
|
+
// In Out
|
|
35
|
+
void GetSegment(double LOG[], int Q[], int NSAMPS, int SEGSTR, int& SEGEND, int& SEGLEN, double& SEGAVG, bool& endofarray);
|
|
36
|
+
|
|
37
|
+
// In In/Out
|
|
38
|
+
void Deglitch(int NSAMPS, int Q[]);
|
|
39
|
+
|
|
40
|
+
// In Out
|
|
41
|
+
void GetSpikeyZone(int Q[], int NSAMPS, int SEGSTR, int& SEGEND, bool& endofarray);
|
|
42
|
+
|
|
43
|
+
// In Out
|
|
44
|
+
void EstimateEventDensity(int Q[], int NSAMPS, double& D, int& NQ);
|
|
45
|
+
|
|
46
|
+
// In Intermediate Out
|
|
47
|
+
void EstimateVarianceOfJumpSequence(double LOG[], int NSAMPS, double WORK1[], double& C);
|
|
48
|
+
|
|
49
|
+
// In Out
|
|
50
|
+
void CalculateMeanAndVariance(double ARRAY[], int NSAMPS, int MODE, double& MEAN, double& VAR);
|
|
51
|
+
|
|
52
|
+
#endif
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#include "SegmentationResults.h"
|
|
2
|
+
|
|
3
|
+
namespace Algorithms
|
|
4
|
+
{
|
|
5
|
+
SegmentationResults::SegmentationResults(int signalLength, int binaryEventSequence[], int numberOfBinaryEvents, double filteredSignal[], double segmentedLog[], double noiseVariance[], double jumpSequenceVariance, double segmentDensity, int iterations, int error)
|
|
6
|
+
{
|
|
7
|
+
_signalLength = signalLength;
|
|
8
|
+
|
|
9
|
+
_binaryEventSequence = binaryEventSequence;
|
|
10
|
+
_numberOfBinaryEvents = numberOfBinaryEvents;
|
|
11
|
+
_filteredSignal = filteredSignal;
|
|
12
|
+
_segmentedLog = segmentedLog;
|
|
13
|
+
_noiseVariance = noiseVariance;
|
|
14
|
+
|
|
15
|
+
_jumpSequenceVariance = jumpSequenceVariance;
|
|
16
|
+
_segmentDensity = segmentDensity;
|
|
17
|
+
_iterations = iterations;
|
|
18
|
+
_error = error;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
SegmentationResults::~SegmentationResults()
|
|
22
|
+
{
|
|
23
|
+
if (_binaryEventSequence)
|
|
24
|
+
{
|
|
25
|
+
delete _binaryEventSequence;
|
|
26
|
+
delete _filteredSignal;
|
|
27
|
+
delete _segmentedLog;
|
|
28
|
+
delete _noiseVariance;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int SegmentationResults::GetSignalLength()
|
|
33
|
+
{
|
|
34
|
+
return _signalLength;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
int* SegmentationResults::GetBinaryEventSequence()
|
|
38
|
+
{
|
|
39
|
+
return _binaryEventSequence;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
int SegmentationResults::GetNumberOfBinaryEvents()
|
|
43
|
+
{
|
|
44
|
+
return _numberOfBinaryEvents;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
double* SegmentationResults::GetFilteredSignal()
|
|
48
|
+
{
|
|
49
|
+
return _filteredSignal;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
double* SegmentationResults::GetSegmentedLog()
|
|
53
|
+
{
|
|
54
|
+
return _segmentedLog;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
double* SegmentationResults::GetNoiseVariance()
|
|
58
|
+
{
|
|
59
|
+
return _noiseVariance;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
double SegmentationResults::GetJumpSequenceVariance()
|
|
63
|
+
{
|
|
64
|
+
return _jumpSequenceVariance;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
double SegmentationResults::GetSegmentDensity()
|
|
68
|
+
{
|
|
69
|
+
return _segmentDensity;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
int SegmentationResults::GetIterations()
|
|
73
|
+
{
|
|
74
|
+
return _iterations;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
int SegmentationResults::GetError()
|
|
78
|
+
{
|
|
79
|
+
return _error;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
} // End namespace.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
#ifndef SEGMENTATIONRESULTS_H
|
|
2
|
+
#define SEGMENTATIONRESULTS_H
|
|
3
|
+
|
|
4
|
+
namespace Algorithms
|
|
5
|
+
{
|
|
6
|
+
class SegmentationResults
|
|
7
|
+
{
|
|
8
|
+
private:
|
|
9
|
+
int _signalLength;
|
|
10
|
+
int* _binaryEventSequence = 0;
|
|
11
|
+
int _numberOfBinaryEvents;
|
|
12
|
+
double* _filteredSignal = 0;
|
|
13
|
+
double* _segmentedLog = 0;
|
|
14
|
+
double* _noiseVariance = 0;
|
|
15
|
+
double _jumpSequenceVariance;
|
|
16
|
+
double _segmentDensity;
|
|
17
|
+
int _iterations;
|
|
18
|
+
int _error;
|
|
19
|
+
|
|
20
|
+
public:
|
|
21
|
+
/// <summary>
|
|
22
|
+
/// Constructor.
|
|
23
|
+
/// </summary>
|
|
24
|
+
/// <param name="signalLength">Length of the input signal. The output sequences are the name length.</param>
|
|
25
|
+
/// <param name="binaryEventSequence">Binary event sequence.</param>
|
|
26
|
+
/// <param name="numberOfBinaryEvents">Number of binary events found (number of "1"s found in binary event sequence).</param>
|
|
27
|
+
/// <param name="filteredSignal">Filtered signal.</param>
|
|
28
|
+
/// <param name="segmentedLog">Segmented log.</param>
|
|
29
|
+
/// <param name="noiseVariance">Noise variance.</param>
|
|
30
|
+
/// <param name="jumpSequenceVariance">Jump sequence variance.</param>
|
|
31
|
+
/// <param name="segmentDensity">Segment density (ratio of events to total entries in binary event sequence).</param>
|
|
32
|
+
/// <param name="iterations">Number of iterations performed.</param>
|
|
33
|
+
/// <param name="error">Error flag.</param>
|
|
34
|
+
SegmentationResults(int signalLength, int binaryEventSequence[], int numberOfBinaryEvents, double filteredSignal[], double segmentedLog[], double noiseVariance[], double jumpSequenceVariance, double segmentDensity, int iterations, int error);
|
|
35
|
+
|
|
36
|
+
/// <summary>
|
|
37
|
+
/// destructor.
|
|
38
|
+
/// </summary>
|
|
39
|
+
~SegmentationResults();
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
/// <summary>
|
|
43
|
+
/// Length of the input signal. The output sequences are the name length.
|
|
44
|
+
/// </summary>
|
|
45
|
+
int GetSignalLength();
|
|
46
|
+
|
|
47
|
+
/// <summary>
|
|
48
|
+
/// Array that contains 1s at segmented log boundaries and 0s elsewhere.
|
|
49
|
+
/// </summary>
|
|
50
|
+
int* GetBinaryEventSequence();
|
|
51
|
+
|
|
52
|
+
/// <summary>
|
|
53
|
+
/// Number of binary event sequences detected (number of "1"s in the BinaryEventSequence array).
|
|
54
|
+
/// </summary>
|
|
55
|
+
int GetNumberOfBinaryEvents();
|
|
56
|
+
|
|
57
|
+
/// <summary>
|
|
58
|
+
/// Filtered estimate of the signal.
|
|
59
|
+
/// </summary>
|
|
60
|
+
double* GetFilteredSignal();
|
|
61
|
+
|
|
62
|
+
/// <summary>
|
|
63
|
+
/// Average of filter log for each segment.
|
|
64
|
+
/// </summary>
|
|
65
|
+
double* GetSegmentedLog();
|
|
66
|
+
|
|
67
|
+
/// <summary>
|
|
68
|
+
/// Estimate noise variance.
|
|
69
|
+
/// </summary>
|
|
70
|
+
double* GetNoiseVariance();
|
|
71
|
+
|
|
72
|
+
/// <summary>
|
|
73
|
+
/// Estimated variance of the jump sequence.
|
|
74
|
+
/// </summary>
|
|
75
|
+
double GetJumpSequenceVariance();
|
|
76
|
+
|
|
77
|
+
/// <summary>
|
|
78
|
+
/// Estimate segment density.
|
|
79
|
+
/// </summary>
|
|
80
|
+
double GetSegmentDensity();
|
|
81
|
+
|
|
82
|
+
/// <summary>
|
|
83
|
+
/// SMLR iteration count.
|
|
84
|
+
/// </summary>
|
|
85
|
+
int GetIterations();
|
|
86
|
+
|
|
87
|
+
/// <summary>
|
|
88
|
+
/// Error flag.
|
|
89
|
+
/// Zero - no error.
|
|
90
|
+
/// Less than zero - invalid event density estimated after threshold. Reduce/increase
|
|
91
|
+
/// threshold and rerun.
|
|
92
|
+
/// Greater than zero - logarithm argument became zero at sample number returned in error during
|
|
93
|
+
/// the calculation of likelihood ratios in SMLR. There may be more samples of this type which
|
|
94
|
+
/// may give rise to this problem. Edit/rescale data values and rerun.
|
|
95
|
+
/// </summary>
|
|
96
|
+
int GetError();
|
|
97
|
+
|
|
98
|
+
}; // End class.
|
|
99
|
+
} // End namespace.
|
|
100
|
+
|
|
101
|
+
#endif
|
CPP Unit Tests/stdafx.h
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// stdafx.h : include file for standard system include files,
|
|
2
|
+
// or project specific include files that are used frequently, but
|
|
3
|
+
// are changed infrequently
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
#pragma once
|
|
7
|
+
|
|
8
|
+
#include "targetver.h"
|
|
9
|
+
|
|
10
|
+
// Headers for CppUnitTest
|
|
11
|
+
#include "CppUnitTest.h"
|
|
12
|
+
|
|
13
|
+
// TODO: reference additional headers your program requires here
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// Including SDKDDKVer.h defines the highest available Windows platform.
|
|
4
|
+
|
|
5
|
+
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
|
|
6
|
+
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
|
|
7
|
+
|
|
8
|
+
#include <SDKDDKVer.h>
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#include "SegmentationResultsPy.h"
|
|
2
|
+
|
|
3
|
+
namespace PythonAlgorithms
|
|
4
|
+
{
|
|
5
|
+
SegmentationResults::SegmentationResults()
|
|
6
|
+
{
|
|
7
|
+
_numberOfBinaryEvents = 0;
|
|
8
|
+
_jumpSequenceVariance = 0;
|
|
9
|
+
_segmentDensity = 0;
|
|
10
|
+
_iterations = 0;
|
|
11
|
+
_error = 0;
|
|
12
|
+
|
|
13
|
+
_binaryEventSequence = py::array_t<int>();
|
|
14
|
+
_filteredSignal = py::array_t<double>();
|
|
15
|
+
_segmentedLog = py::array_t<double>();
|
|
16
|
+
_noiseVariance = py::array_t<double>();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
SegmentationResults::SegmentationResults(Algorithms::SegmentationResults* segmentationResults)
|
|
20
|
+
{
|
|
21
|
+
// Initial scalar values from the input.
|
|
22
|
+
_signalLength = segmentationResults->GetSignalLength();
|
|
23
|
+
_numberOfBinaryEvents = segmentationResults->GetNumberOfBinaryEvents();
|
|
24
|
+
_jumpSequenceVariance = segmentationResults->GetJumpSequenceVariance();
|
|
25
|
+
_segmentDensity = segmentationResults->GetSegmentDensity();
|
|
26
|
+
_iterations = segmentationResults->GetIterations();
|
|
27
|
+
_error = segmentationResults->GetError();
|
|
28
|
+
|
|
29
|
+
// Create the arrays.
|
|
30
|
+
_binaryEventSequence = py::array_t<int>(_signalLength);
|
|
31
|
+
_filteredSignal = py::array_t<double>(_signalLength);
|
|
32
|
+
_segmentedLog = py::array_t<double>(_signalLength);
|
|
33
|
+
_noiseVariance = py::array_t<double>(_signalLength);
|
|
34
|
+
|
|
35
|
+
// Get pointers to the results. The data is private, we use the access functions.
|
|
36
|
+
int* binaryEventSequence = segmentationResults->GetBinaryEventSequence();
|
|
37
|
+
double* filteredSignal = segmentationResults->GetFilteredSignal();
|
|
38
|
+
double* segmentedLog = segmentationResults->GetSegmentedLog();
|
|
39
|
+
double* noiseVariance = segmentationResults->GetNoiseVariance();
|
|
40
|
+
|
|
41
|
+
// Get pointers to the actual data inside of the Python arrays.
|
|
42
|
+
int* binaryEventSequenceBuffer = static_cast<int*>(_binaryEventSequence.request().ptr);
|
|
43
|
+
double* filteredSignalBuffer = static_cast<double*>(_filteredSignal.request().ptr);
|
|
44
|
+
double* segmentedLogBuffer = static_cast<double*>(_segmentedLog.request().ptr);
|
|
45
|
+
double* noiseVarianceBuffer = static_cast<double*>(_noiseVariance.request().ptr);
|
|
46
|
+
|
|
47
|
+
// Copy the data to the Python data structures.
|
|
48
|
+
for (int i = 0; i < _signalLength; i++)
|
|
49
|
+
{
|
|
50
|
+
binaryEventSequenceBuffer[i] = binaryEventSequence[i];
|
|
51
|
+
filteredSignalBuffer[i] = filteredSignal[i];
|
|
52
|
+
segmentedLogBuffer[i] = segmentedLog[i];
|
|
53
|
+
noiseVarianceBuffer[i] = noiseVariance[i];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
SegmentationResults::SegmentationResults(int signalLength, py::array_t<int> binaryEventSequence, int numberOfBinaryEvents, py::array_t<double> filteredSignal,
|
|
58
|
+
py::array_t<double> segmentedLog, py::array_t<double> noiseVariance, double jumpSequenceVariance, double segmentDensity, int iterations, int error)
|
|
59
|
+
{
|
|
60
|
+
_signalLength = signalLength;
|
|
61
|
+
_binaryEventSequence = binaryEventSequence;
|
|
62
|
+
_numberOfBinaryEvents = numberOfBinaryEvents;
|
|
63
|
+
_filteredSignal = filteredSignal;
|
|
64
|
+
_segmentedLog = segmentedLog;
|
|
65
|
+
_noiseVariance = noiseVariance;
|
|
66
|
+
_jumpSequenceVariance = jumpSequenceVariance;
|
|
67
|
+
_segmentDensity = segmentDensity;
|
|
68
|
+
_iterations = iterations;
|
|
69
|
+
_error = error;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
SegmentationResults::~SegmentationResults()
|
|
73
|
+
{
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
int SegmentationResults::GetSignalLength() const
|
|
77
|
+
{
|
|
78
|
+
return _signalLength;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
py::array_t<int> SegmentationResults::GetBinaryEventSequence() const
|
|
82
|
+
{
|
|
83
|
+
return _binaryEventSequence;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
int SegmentationResults::GetNumberOfBinaryEvents() const
|
|
87
|
+
{
|
|
88
|
+
return _numberOfBinaryEvents;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
py::array_t<double> SegmentationResults::GetFilteredSignal() const
|
|
92
|
+
{
|
|
93
|
+
return _filteredSignal;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
py::array_t<double> SegmentationResults::GetSegmentedLog() const
|
|
97
|
+
{
|
|
98
|
+
return _segmentedLog;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
py::array_t<double> SegmentationResults::GetNoiseVariance() const
|
|
102
|
+
{
|
|
103
|
+
return _noiseVariance;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
double SegmentationResults::GetJumpSequenceVariance() const
|
|
107
|
+
{
|
|
108
|
+
return _jumpSequenceVariance;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
double SegmentationResults::GetSegmentDensity() const
|
|
112
|
+
{
|
|
113
|
+
return _segmentDensity;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
int SegmentationResults::GetIterations() const
|
|
117
|
+
{
|
|
118
|
+
return _iterations;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
int SegmentationResults::GetError() const
|
|
122
|
+
{
|
|
123
|
+
return _error;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
} // End namespace.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#ifndef SEGMENTATIONRESULTSPY_H
|
|
2
|
+
#define SEGMENTATIONRESULTSPY_H
|
|
3
|
+
|
|
4
|
+
#include <pybind11/pybind11.h>
|
|
5
|
+
#include <pybind11/numpy.h>
|
|
6
|
+
#include "SegmentationResults.h"
|
|
7
|
+
|
|
8
|
+
namespace py = pybind11;
|
|
9
|
+
|
|
10
|
+
namespace PythonAlgorithms
|
|
11
|
+
{
|
|
12
|
+
class SegmentationResults
|
|
13
|
+
{
|
|
14
|
+
private:
|
|
15
|
+
int _signalLength;
|
|
16
|
+
py::array_t<int> _binaryEventSequence;
|
|
17
|
+
int _numberOfBinaryEvents;
|
|
18
|
+
py::array_t<double> _filteredSignal;
|
|
19
|
+
py::array_t<double> _segmentedLog;
|
|
20
|
+
py::array_t<double> _noiseVariance;
|
|
21
|
+
double _jumpSequenceVariance;
|
|
22
|
+
double _segmentDensity;
|
|
23
|
+
int _iterations;
|
|
24
|
+
int _error;
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
/// <summary>
|
|
28
|
+
/// Default constructor.
|
|
29
|
+
/// </summary>
|
|
30
|
+
SegmentationResults();
|
|
31
|
+
|
|
32
|
+
/// <summary>
|
|
33
|
+
/// Constructor.
|
|
34
|
+
/// </summary>
|
|
35
|
+
/// <param name="segmentationResults">SegmentationResults which will be converted for use in Python.</param>
|
|
36
|
+
SegmentationResults(Algorithms::SegmentationResults* segmentationResults);
|
|
37
|
+
|
|
38
|
+
/// <summary>
|
|
39
|
+
/// Constructor for calling from Python.
|
|
40
|
+
/// </summary>
|
|
41
|
+
/// <param name="signalLength">Length of the signal/binary event sequence/filtered signal/et cetera.</param>
|
|
42
|
+
/// <param name="binaryEventSequence">Binary event sequence.</param>
|
|
43
|
+
/// <param name="numberOfBinaryEvents">Number of binary events found (number of "1"s found in binary event sequence).</param>
|
|
44
|
+
/// <param name="filteredSignal">Filtered signal.</param>
|
|
45
|
+
/// <param name="segmentedLog">Segmented log.</param>
|
|
46
|
+
/// <param name="noiseVariance">Noise variance.</param>
|
|
47
|
+
/// <param name="jumpSequenceVariance">Jump sequence variance.</param>
|
|
48
|
+
/// <param name="segmentDensity">Segment density (ratio of events to total entries in binary event sequence).</param>
|
|
49
|
+
/// <param name="iterations">Number of iterations performed.</param>
|
|
50
|
+
/// <param name="error">Error flag.</param>
|
|
51
|
+
SegmentationResults(int signalLength, py::array_t<int> binaryEventSequence, int numberOfBinaryEvents, py::array_t<double> filteredSignal, py::array_t<double> segmentedLog,
|
|
52
|
+
py::array_t<double> noiseVariance, double jumpSequenceVariance, double segmentDensity, int iterations, int error);
|
|
53
|
+
|
|
54
|
+
/// <summary>
|
|
55
|
+
/// destructor.
|
|
56
|
+
/// </summary>
|
|
57
|
+
~SegmentationResults();
|
|
58
|
+
|
|
59
|
+
public:
|
|
60
|
+
/// <summary>
|
|
61
|
+
/// Length of the input signal. The output sequences are the name length.
|
|
62
|
+
/// </summary>
|
|
63
|
+
int GetSignalLength() const;
|
|
64
|
+
|
|
65
|
+
/// <summary>
|
|
66
|
+
/// Array that contains 1s at segmented log boundaries and 0s elsewhere.
|
|
67
|
+
/// </summary>
|
|
68
|
+
py::array_t<int> GetBinaryEventSequence() const;
|
|
69
|
+
|
|
70
|
+
/// <summary>
|
|
71
|
+
/// Number of binary event sequences detected (number of "1"s in the BinaryEventSequence array).
|
|
72
|
+
/// </summary>
|
|
73
|
+
int GetNumberOfBinaryEvents() const;
|
|
74
|
+
|
|
75
|
+
/// <summary>
|
|
76
|
+
/// Filtered estimate of the signal.
|
|
77
|
+
/// </summary>
|
|
78
|
+
py::array_t<double> GetFilteredSignal() const;
|
|
79
|
+
|
|
80
|
+
/// <summary>
|
|
81
|
+
/// Average of filter log for each segment.
|
|
82
|
+
/// </summary>
|
|
83
|
+
py::array_t<double> GetSegmentedLog() const;
|
|
84
|
+
|
|
85
|
+
/// <summary>
|
|
86
|
+
/// Estimate noise variance.
|
|
87
|
+
/// </summary>
|
|
88
|
+
py::array_t<double> GetNoiseVariance() const;
|
|
89
|
+
|
|
90
|
+
/// <summary>
|
|
91
|
+
/// Estimated variance of the jump sequence.
|
|
92
|
+
/// </summary>
|
|
93
|
+
double GetJumpSequenceVariance() const;
|
|
94
|
+
|
|
95
|
+
/// <summary>
|
|
96
|
+
/// Estimate segment density.
|
|
97
|
+
/// </summary>
|
|
98
|
+
double GetSegmentDensity() const;
|
|
99
|
+
|
|
100
|
+
/// <summary>
|
|
101
|
+
/// SMLR iteration count.
|
|
102
|
+
/// </summary>
|
|
103
|
+
int GetIterations() const;
|
|
104
|
+
|
|
105
|
+
/// <summary>
|
|
106
|
+
/// Error flag.
|
|
107
|
+
/// Zero - no error.
|
|
108
|
+
/// Less than zero - invalid event density estimated after threshold. Reduce/increase
|
|
109
|
+
/// threshold and rerun.
|
|
110
|
+
/// Greater than zero - logarithm argument became zero at sample number returned in error during
|
|
111
|
+
/// the calculation of likelihood ratios in SMLR. There may be more samples of this type which
|
|
112
|
+
/// may give rise to this problem. Edit/rescale data values and rerun.
|
|
113
|
+
/// </summary>
|
|
114
|
+
int GetError() const;
|
|
115
|
+
|
|
116
|
+
}; // End class.
|
|
117
|
+
} // End namespace.
|
|
118
|
+
|
|
119
|
+
#endif
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#include <stdexcept>
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
#include <pybind11/numpy.h>
|
|
4
|
+
//#include <pybind11/stl.h>
|
|
5
|
+
|
|
6
|
+
#include "SegmentSignalFunctions.h"
|
|
7
|
+
#include "SegmentSignal.h"
|
|
8
|
+
#include "SegmentationResults.h"
|
|
9
|
+
#include "SegmentationResultsPy.h"
|
|
10
|
+
|
|
11
|
+
#include <iostream>
|
|
12
|
+
#include <fstream>
|
|
13
|
+
#include <iomanip> // std::setprecision
|
|
14
|
+
|
|
15
|
+
namespace py = pybind11;
|
|
16
|
+
|
|
17
|
+
PythonAlgorithms::SegmentationResults* Segment(py::array_t<double> signalAsPyList, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize, int noiseVarianceEstimateMethod, int maxSMLRIterations)
|
|
18
|
+
{
|
|
19
|
+
// Gets the information about the object and a pointer to the actual data (buffer).
|
|
20
|
+
py::buffer_info info = signalAsPyList.request();
|
|
21
|
+
double* signalDataPointer = static_cast<double*>(info.ptr);
|
|
22
|
+
|
|
23
|
+
// Make sure a 1-dimensional array/list was passed and not a scalar, two-dimensional, or other.
|
|
24
|
+
if (info.ndim != 1)
|
|
25
|
+
{
|
|
26
|
+
throw std::runtime_error("Error: The array passed to \"Segment\" function is not 1 dimensional.");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Get the length of the array/list. It is required for the lower level functions operation on C arrays.
|
|
30
|
+
int signalLength = static_cast<int>(info.shape[0]);
|
|
31
|
+
|
|
32
|
+
// Call the algorithm, then convert the results for returning to Python.
|
|
33
|
+
Algorithms::SegmentationResults* cppResults = Algorithms::SegmentSignal::Segment(signalDataPointer, signalLength, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, (NoiseVarianceEstimateMethod)noiseVarianceEstimateMethod, maxSMLRIterations);
|
|
34
|
+
PythonAlgorithms::SegmentationResults* pythonResults = new PythonAlgorithms::SegmentationResults(cppResults);
|
|
35
|
+
|
|
36
|
+
// The C results are not longer needed. Everything has been copied to the Python results.
|
|
37
|
+
delete cppResults;
|
|
38
|
+
|
|
39
|
+
return pythonResults;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
py::array_t<int> FindSignificantZones(py::array_t<int> binaryEvents, py::array_t<double> xData, double threshold, bool includeBoundaries)
|
|
44
|
+
{
|
|
45
|
+
// Gets the information about the object and a pointer to the actual data (buffer).
|
|
46
|
+
py::buffer_info eventsInfo = binaryEvents.request();
|
|
47
|
+
int* binaryEventsPointer = static_cast<int*>(eventsInfo.ptr);
|
|
48
|
+
py::buffer_info xDataInfo = xData.request();
|
|
49
|
+
double* xDataPointer = static_cast<double*>(xDataInfo.ptr);
|
|
50
|
+
|
|
51
|
+
// Make sure a 1-dimensional array/list was passed and not a scalar, two-dimensional, or other.
|
|
52
|
+
if (eventsInfo.ndim != 1 || xDataInfo.ndim != 1)
|
|
53
|
+
{
|
|
54
|
+
throw std::runtime_error("Error: An array passed to \"FindSignificantZones\" function is not 1 dimensional.");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Make sure a data is the same length.
|
|
58
|
+
if (eventsInfo.shape[0] != xDataInfo.shape[0])
|
|
59
|
+
{
|
|
60
|
+
throw std::runtime_error("Error: Arrays passed to \"FindSignificantZones\" function are not the same length.");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Get the length of the array/list. It is required for the lower level functions operation on C arrays.
|
|
64
|
+
int signalLength = static_cast<int>(eventsInfo.shape[0]);
|
|
65
|
+
std::vector<std::array<int, 2>>* cppResults = Algorithms::SegmentSignal::FindSignificantZones(binaryEventsPointer, xDataPointer, signalLength, threshold, includeBoundaries);
|
|
66
|
+
|
|
67
|
+
size_t size = cppResults->size();
|
|
68
|
+
py::array_t<int> pyResults = py::array_t<int>(2*size);
|
|
69
|
+
int* pyResultsBuffer = static_cast<int*>(pyResults.request().ptr);
|
|
70
|
+
|
|
71
|
+
for (int i = 0; i < size; i++)
|
|
72
|
+
{
|
|
73
|
+
std::array<int, 2> indexSet = (*cppResults)[i];
|
|
74
|
+
pyResultsBuffer[2*i] = indexSet[0];
|
|
75
|
+
pyResultsBuffer[2*i+1] = indexSet[1];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
pyResults.resize({(int)size, 2});
|
|
79
|
+
|
|
80
|
+
delete cppResults;
|
|
81
|
+
|
|
82
|
+
return pyResults;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
PYBIND11_MODULE(SegmentSignalPy, m)
|
|
87
|
+
{
|
|
88
|
+
m.def("Segment", &Segment, "Segments a signal based on maximum likelihood estimation");
|
|
89
|
+
|
|
90
|
+
m.def("FindSignificantZones", &FindSignificantZones, "Post processes a binary event sequence to find regions that are greater than the specified threashold.");
|
|
91
|
+
|
|
92
|
+
//py::class_<Algorithms::SegmentSignal>(m, "SegmentSignal")
|
|
93
|
+
// .def_static("Segment", static_cast<Algorithms::SegmentationResults* (*)(double[], int, double, int, int)>(&Algorithms::SegmentSignal::Segment));
|
|
94
|
+
|
|
95
|
+
py::class_<PythonAlgorithms::SegmentationResults>(m, "SegmentationResults")
|
|
96
|
+
.def(py::init<>())
|
|
97
|
+
.def(py::init<int, py::array_t<int>, int, py::array_t<double>, py::array_t<double>, py::array_t<double>, double, double, int, int>())
|
|
98
|
+
.def(py::pickle(
|
|
99
|
+
[](const PythonAlgorithms::SegmentationResults& p) { // __getstate__
|
|
100
|
+
// Return a tuple that fully encodes the state of the object.
|
|
101
|
+
return py::make_tuple(p.GetSignalLength(), p.GetBinaryEventSequence(), p.GetNumberOfBinaryEvents(), p.GetFilteredSignal(), p.GetSegmentedLog(), p.GetNoiseVariance(), p.GetJumpSequenceVariance(), p.GetSegmentDensity(), p.GetIterations(), p.GetError());
|
|
102
|
+
},
|
|
103
|
+
[](py::tuple t) { // __setstate__
|
|
104
|
+
if (t.size() != 10)
|
|
105
|
+
throw std::runtime_error("Invalid state!");
|
|
106
|
+
|
|
107
|
+
// Create a new C++ instance.
|
|
108
|
+
PythonAlgorithms::SegmentationResults p(
|
|
109
|
+
t[0].cast<int>(), // Signal length.
|
|
110
|
+
t[1].cast<py::array_t<int>>(), // Binary event sequence.
|
|
111
|
+
t[2].cast<int>(), // Number of binary events.
|
|
112
|
+
t[3].cast<py::array_t<double>>(), // Filtered signal.
|
|
113
|
+
t[4].cast<py::array_t<double>>(), // Segmented log.
|
|
114
|
+
t[5].cast<py::array_t<double>>(), // Noise variance.
|
|
115
|
+
t[6].cast<double>(), // Noise variance jump sequence.
|
|
116
|
+
t[7].cast<double>(), // Segment density.
|
|
117
|
+
t[8].cast<int>(), // Iterations.
|
|
118
|
+
t[9].cast<int>() // Error.
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return p;
|
|
122
|
+
}
|
|
123
|
+
))
|
|
124
|
+
.def_property_readonly("SignalLength", &PythonAlgorithms::SegmentationResults::GetSignalLength)
|
|
125
|
+
.def_property_readonly("BinaryEventSequence", &PythonAlgorithms::SegmentationResults::GetBinaryEventSequence)
|
|
126
|
+
.def_property_readonly("NumberOfBinaryEvents", &PythonAlgorithms::SegmentationResults::GetNumberOfBinaryEvents)
|
|
127
|
+
.def_property_readonly("FilteredSignal", &PythonAlgorithms::SegmentationResults::GetFilteredSignal)
|
|
128
|
+
.def_property_readonly("SegmentedLog", &PythonAlgorithms::SegmentationResults::GetSegmentedLog)
|
|
129
|
+
.def_property_readonly("NoiseVariance", &PythonAlgorithms::SegmentationResults::GetNoiseVariance)
|
|
130
|
+
.def_property_readonly("JumpSequenceVariance", &PythonAlgorithms::SegmentationResults::GetJumpSequenceVariance)
|
|
131
|
+
.def_property_readonly("SegmentDensity", &PythonAlgorithms::SegmentationResults::GetSegmentDensity)
|
|
132
|
+
.def_property_readonly("Iterations", &PythonAlgorithms::SegmentationResults::GetIterations)
|
|
133
|
+
.def_property_readonly("Error", &PythonAlgorithms::SegmentationResults::GetError);
|
|
134
|
+
|
|
135
|
+
#ifdef VERSION_INFO
|
|
136
|
+
m.attr("__version__") = VERSION_INFO;
|
|
137
|
+
#else
|
|
138
|
+
m.attr("__version__") = "dev";
|
|
139
|
+
#endif
|
|
140
|
+
}
|