SegmentSignalPy 0.1.2__tar.gz

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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2021-2024 Lance A. Endres
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ Metadata-Version: 2.1
2
+ Name: SegmentSignalPy
3
+ Version: 0.1.2
4
+ Summary: Signal segmentation algorithm by S. Radhakrishnan, et al. This is a Python interface for the C++ algorithm.
5
+ License: Copyright (c) 2021-2024 Lance A. Endres
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ Project-URL: homepage, https://github.com/lendres/Segment-Signals
27
+ Requires-Python: >=3.11.0
28
+ Description-Content-Type: text/markdown
29
+
30
+ # Segment Signals
31
+ A signal segmentation algorithm.
32
+
33
+ ## Compiling
34
+ The Python Extension library requires the knowledge of your local Python. See the *Read Me.txt* file in that directory for more information.
@@ -0,0 +1,5 @@
1
+ # Segment Signals
2
+ A signal segmentation algorithm.
3
+
4
+ ## Compiling
5
+ The Python Extension library requires the knowledge of your local Python. See the *Read Me.txt* file in that directory for more information.
@@ -0,0 +1,17 @@
1
+ [project]
2
+ name = "SegmentSignalPy"
3
+ version = "0.1.2"
4
+ requires-python = ">=3.11.0"
5
+ description = "Signal segmentation algorithm by S. Radhakrishnan, et al. This is a Python interface for the C++ algorithm."
6
+ readme = "README.md"
7
+ license = {file = "License.txt"}
8
+
9
+ [project.urls]
10
+ homepage = "https://github.com/lendres/Segment-Signals"
11
+
12
+ [build-system]
13
+ requires = [
14
+ "setuptools",
15
+ "pybind11"
16
+ ]
17
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,26 @@
1
+ import os
2
+ from setuptools import setup
3
+ from setuptools import Extension
4
+ from pybind11 import get_include
5
+
6
+
7
+ sourceDirectory = os.path.join("src", "CPP Static Library")
8
+ pythonDirectory = os.path.join("src", "Python Extension")
9
+
10
+ setup(
11
+ ext_modules=[
12
+ Extension(
13
+ name="SegmentSignalPy",
14
+ sources=[
15
+ os.path.join(pythonDirectory, file)
16
+ for file in ["module.cpp", "SegmentationResultsPy.cpp"]
17
+ ] + [
18
+ os.path.join(sourceDirectory, file)
19
+ for file in ["SegmentSignalFunctions.cpp", "SegmentationResults.cpp", "SegmentSignal.cpp"]
20
+ ],
21
+ include_dirs=[get_include(), sourceDirectory],
22
+ language="c++",
23
+ extra_compile_args=[]
24
+ )
25
+ ]
26
+ )
@@ -0,0 +1,167 @@
1
+ #include "SegmentSignal.h"
2
+ #include "NoiseVarianceEstimateMethod.h"
3
+ #include "SegmentSignalFunctions.h"
4
+ #include <vector>
5
+ #include <cmath>
6
+
7
+ using namespace std;
8
+
9
+ namespace Algorithms
10
+ {
11
+ SegmentationResults* SegmentSignal::Segment(double signal[], int signalLength, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize)
12
+ {
13
+ return Segment(signal, signalLength, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, NoiseVarianceEstimateMethod::Point, 300);
14
+ }
15
+
16
+ SegmentationResults* SegmentSignal::Segment(double signal[], int signalLength, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize, NoiseVarianceEstimateMethod noiseVarianceEstimateMethod)
17
+ {
18
+ return Segment(signal, signalLength, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, noiseVarianceEstimateMethod, 300);
19
+ }
20
+
21
+ SegmentationResults* SegmentSignal::Segment(double signal[], int signalLength, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize, NoiseVarianceEstimateMethod noiseVarianceEstimateMethod, int maxSMLRIterations)
22
+ {
23
+ // Scalars which are need for output from the call to the algorithm.
24
+ int numberOfBinaryEvents = 0;
25
+ double jumpSequenceVariance = 0;
26
+ double segmentDensity = 0;
27
+ int iterations = 0;
28
+ int error = 0;
29
+
30
+ // We seem to need to create the array in the function immediately prior to calling the unmanaged code, otherwise the garbage collector
31
+ // gets cute and moves things around, which the unmanaged code takes offense to.
32
+ //double* signalToPass = new double[signalLength];
33
+ //for (int i = 0; i < signalLength; i++)
34
+ //{
35
+ // signalToPass[i] = signal[i];
36
+ //}
37
+
38
+
39
+ // Create the output arrays. After being populated by the call to the algorithm they are grouped, stored, and returned in the SegmenationResults data structure.
40
+ int* Q = new int[signalLength]; // Binary event sequence.
41
+ double* FLTLOG = new double[signalLength]; // Filtered signal.
42
+ double* SEGLOG = new double[signalLength]; // Segmented log.
43
+ double* R = new double[signalLength]; // Noise variance.
44
+
45
+ // Function call to the C DLL.
46
+ ::SegmentSignal(signal, signalLength, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, (int)noiseVarianceEstimateMethod, maxSMLRIterations, Q, numberOfBinaryEvents, FLTLOG, SEGLOG, R, jumpSequenceVariance, segmentDensity, iterations, error);
47
+
48
+ return new SegmentationResults(signalLength, Q, numberOfBinaryEvents, FLTLOG, SEGLOG, R, jumpSequenceVariance, segmentDensity, iterations, error);
49
+ }
50
+
51
+ SegmentationResults* SegmentSignal::Segment(vector<double> signal, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize)
52
+ {
53
+ return Segment(signal, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, NoiseVarianceEstimateMethod::Point, 300);
54
+ }
55
+
56
+ SegmentationResults* SegmentSignal::Segment(vector<double> signal, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize, NoiseVarianceEstimateMethod noiseVarianceEstimateMethod)
57
+ {
58
+ return Segment(signal, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, noiseVarianceEstimateMethod, 300);
59
+ }
60
+
61
+ SegmentationResults* SegmentSignal::Segment(vector<double> signal, double threshold, int jumpSequenceWindowSize, int noiseVarianceWindowSize, NoiseVarianceEstimateMethod noiseVarianceEstimateMethod, int maxSMLRIterations)
62
+ {
63
+ // Scalars which are need for output from the call to the algorithm.
64
+ int numberOfBinaryEvents = 0;
65
+ double jumpSequenceVariance = 0;
66
+ double segmentDensity = 0;
67
+ int iterations = 0;
68
+ int error = 0;
69
+
70
+ int signalLength = (int)signal.size();
71
+ double* signalToPass = new double[signalLength];
72
+ for (int i = 0; i < signalLength; i++)
73
+ {
74
+ signalToPass[i] = signal[i];
75
+ }
76
+
77
+ // Create the output arrays. After being populated by the call to the algorithm they are grouped, stored, and returned in the SegmenationResults data structure.
78
+ int* Q = new int[signalLength]; // Binary event sequence.
79
+ double* FLTLOG = new double[signalLength]; // Filtered signal.
80
+ double* SEGLOG = new double[signalLength]; // Segmented log.
81
+ double* R = new double[signalLength]; // Noise variance.
82
+
83
+
84
+ int estimateMethod = (int)noiseVarianceEstimateMethod;
85
+
86
+ // Function call to the C DLL.
87
+ ::SegmentSignal(signalToPass, signalLength, threshold, jumpSequenceWindowSize, noiseVarianceWindowSize, estimateMethod, maxSMLRIterations, Q, numberOfBinaryEvents, FLTLOG, SEGLOG, R, jumpSequenceVariance, segmentDensity, iterations, error);
88
+
89
+ return new SegmentationResults(signalLength, Q, numberOfBinaryEvents, FLTLOG, SEGLOG, R, jumpSequenceVariance, segmentDensity, iterations, error);
90
+ }
91
+
92
+ vector<array<int, 2>>* SegmentSignal::FindSignificantZones(int binaryEvents[], double xData[], int signalLength, double threshold)
93
+ {
94
+ return FindSignificantZones(binaryEvents, xData, signalLength, threshold, false);
95
+ }
96
+
97
+ vector<array<int, 2>>* SegmentSignal::FindSignificantZones(int binaryEvents[], double xData[], int signalLength, double threshold, bool includeBoundaries)
98
+ {
99
+ vector<array<int, 2>>* significantZones = new vector<array<int, 2>>();
100
+
101
+ // Find the first "0" entry which will mark the beginning of a zone.
102
+ int zoneStart = FindNextZero(binaryEvents, signalLength, 0);
103
+
104
+ for (int currentIndex = zoneStart+1; currentIndex < signalLength; currentIndex++)
105
+ {
106
+ if (binaryEvents[currentIndex])
107
+ {
108
+ CheckIfValidZone(xData, threshold, includeBoundaries, significantZones, zoneStart, currentIndex-1);
109
+
110
+ // Scan for the start of the next potential section.
111
+ zoneStart = FindNextZero(binaryEvents, signalLength, currentIndex);
112
+ currentIndex = zoneStart;
113
+ }
114
+ }
115
+
116
+ // If the last entry of the binary events is zero, we need to handle the final section.
117
+ if (!binaryEvents[signalLength-1])
118
+ {
119
+ CheckIfValidZone(xData, threshold, includeBoundaries, significantZones, zoneStart, signalLength-1);
120
+ }
121
+
122
+ // When including the boundaries (includeBoundaries==true), it is possible to have added data points before the beginning of the array
123
+ // and after the end. We need to check for and, if they exist, correct them.
124
+ if (significantZones->size() > 0)
125
+ {
126
+ if ((*significantZones)[0][0] < 0)
127
+ {
128
+ (*significantZones)[0][0] = 0;
129
+ }
130
+
131
+ if ((*significantZones)[significantZones->size()-1][1] >= signalLength)
132
+ {
133
+ (*significantZones)[significantZones->size()-1][1] = signalLength-1;
134
+ }
135
+ }
136
+
137
+ return significantZones;
138
+ }
139
+
140
+ int SegmentSignal::FindNextZero(int binaryEvents[], int signalLength, int currentIndex)
141
+ {
142
+ // Find the first zero.
143
+ while (binaryEvents[currentIndex] && currentIndex < signalLength-1)
144
+ {
145
+ currentIndex++;
146
+ }
147
+ return currentIndex;
148
+ }
149
+
150
+ void SegmentSignal::CheckIfValidZone(double xData[], double threshold, bool includeBoundaries, vector<array<int, 2>>* significantZones, int startIndex, int endIndex)
151
+ {
152
+ // Check to see if the threshold is met. If it met, we will store the section
153
+ // as a significant zone. If the threshold is not met, then the zone is not long
154
+ // enough so it is ignored and we scan ahead.
155
+ if (abs(xData[endIndex] - xData[startIndex]) > threshold)
156
+ {
157
+ if (includeBoundaries)
158
+ {
159
+ significantZones->push_back({ {startIndex-1, endIndex+1} });
160
+ }
161
+ else
162
+ {
163
+ significantZones->push_back({{startIndex, endIndex}});
164
+ }
165
+ }
166
+ }
167
+ } // End namespace.