dclab 0.67.0__cp314-cp314-macosx_11_0_arm64.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.
Potentially problematic release.
This version of dclab might be problematic. Click here for more details.
- dclab/__init__.py +41 -0
- dclab/_version.py +34 -0
- dclab/cached.py +97 -0
- dclab/cli/__init__.py +10 -0
- dclab/cli/common.py +237 -0
- dclab/cli/task_compress.py +126 -0
- dclab/cli/task_condense.py +223 -0
- dclab/cli/task_join.py +229 -0
- dclab/cli/task_repack.py +98 -0
- dclab/cli/task_split.py +154 -0
- dclab/cli/task_tdms2rtdc.py +186 -0
- dclab/cli/task_verify_dataset.py +75 -0
- dclab/definitions/__init__.py +79 -0
- dclab/definitions/feat_const.py +202 -0
- dclab/definitions/feat_logic.py +182 -0
- dclab/definitions/meta_const.py +252 -0
- dclab/definitions/meta_logic.py +111 -0
- dclab/definitions/meta_parse.py +94 -0
- dclab/downsampling.cpython-314-darwin.so +0 -0
- dclab/downsampling.pyx +230 -0
- dclab/external/__init__.py +4 -0
- dclab/external/packaging/LICENSE +3 -0
- dclab/external/packaging/LICENSE.APACHE +177 -0
- dclab/external/packaging/LICENSE.BSD +23 -0
- dclab/external/packaging/__init__.py +6 -0
- dclab/external/packaging/_structures.py +61 -0
- dclab/external/packaging/version.py +505 -0
- dclab/external/skimage/LICENSE +28 -0
- dclab/external/skimage/__init__.py +2 -0
- dclab/external/skimage/_find_contours.py +216 -0
- dclab/external/skimage/_find_contours_cy.cpython-314-darwin.so +0 -0
- dclab/external/skimage/_find_contours_cy.pyx +188 -0
- dclab/external/skimage/_pnpoly.cpython-314-darwin.so +0 -0
- dclab/external/skimage/_pnpoly.pyx +99 -0
- dclab/external/skimage/_shared/__init__.py +1 -0
- dclab/external/skimage/_shared/geometry.cpython-314-darwin.so +0 -0
- dclab/external/skimage/_shared/geometry.pxd +6 -0
- dclab/external/skimage/_shared/geometry.pyx +55 -0
- dclab/external/skimage/measure.py +7 -0
- dclab/external/skimage/pnpoly.py +53 -0
- dclab/external/statsmodels/LICENSE +35 -0
- dclab/external/statsmodels/__init__.py +6 -0
- dclab/external/statsmodels/nonparametric/__init__.py +1 -0
- dclab/external/statsmodels/nonparametric/_kernel_base.py +203 -0
- dclab/external/statsmodels/nonparametric/kernel_density.py +165 -0
- dclab/external/statsmodels/nonparametric/kernels.py +36 -0
- dclab/features/__init__.py +9 -0
- dclab/features/bright.py +81 -0
- dclab/features/bright_bc.py +93 -0
- dclab/features/bright_perc.py +63 -0
- dclab/features/contour.py +161 -0
- dclab/features/emodulus/__init__.py +339 -0
- dclab/features/emodulus/load.py +252 -0
- dclab/features/emodulus/lut_HE-2D-FEM-22.txt +16432 -0
- dclab/features/emodulus/lut_HE-3D-FEM-22.txt +1276 -0
- dclab/features/emodulus/lut_LE-2D-FEM-19.txt +13082 -0
- dclab/features/emodulus/pxcorr.py +135 -0
- dclab/features/emodulus/scale_linear.py +247 -0
- dclab/features/emodulus/viscosity.py +260 -0
- dclab/features/fl_crosstalk.py +95 -0
- dclab/features/inert_ratio.py +377 -0
- dclab/features/volume.py +242 -0
- dclab/http_utils.py +322 -0
- dclab/isoelastics/__init__.py +468 -0
- dclab/isoelastics/iso_HE-2D-FEM-22-area_um-deform.txt +2440 -0
- dclab/isoelastics/iso_HE-2D-FEM-22-volume-deform.txt +2635 -0
- dclab/isoelastics/iso_HE-3D-FEM-22-area_um-deform.txt +1930 -0
- dclab/isoelastics/iso_HE-3D-FEM-22-volume-deform.txt +2221 -0
- dclab/isoelastics/iso_LE-2D-FEM-19-area_um-deform.txt +2151 -0
- dclab/isoelastics/iso_LE-2D-FEM-19-volume-deform.txt +2250 -0
- dclab/isoelastics/iso_LE-2D-ana-18-area_um-deform.txt +1266 -0
- dclab/kde/__init__.py +1 -0
- dclab/kde/base.py +459 -0
- dclab/kde/contours.py +222 -0
- dclab/kde/methods.py +313 -0
- dclab/kde_contours.py +10 -0
- dclab/kde_methods.py +11 -0
- dclab/lme4/__init__.py +5 -0
- dclab/lme4/lme4_template.R +94 -0
- dclab/lme4/rsetup.py +204 -0
- dclab/lme4/wrapr.py +386 -0
- dclab/polygon_filter.py +398 -0
- dclab/rtdc_dataset/__init__.py +15 -0
- dclab/rtdc_dataset/check.py +902 -0
- dclab/rtdc_dataset/config.py +533 -0
- dclab/rtdc_dataset/copier.py +353 -0
- dclab/rtdc_dataset/core.py +896 -0
- dclab/rtdc_dataset/export.py +867 -0
- dclab/rtdc_dataset/feat_anc_core/__init__.py +24 -0
- dclab/rtdc_dataset/feat_anc_core/af_basic.py +75 -0
- dclab/rtdc_dataset/feat_anc_core/af_emodulus.py +160 -0
- dclab/rtdc_dataset/feat_anc_core/af_fl_max_ctc.py +133 -0
- dclab/rtdc_dataset/feat_anc_core/af_image_contour.py +113 -0
- dclab/rtdc_dataset/feat_anc_core/af_ml_class.py +102 -0
- dclab/rtdc_dataset/feat_anc_core/ancillary_feature.py +320 -0
- dclab/rtdc_dataset/feat_anc_ml/__init__.py +32 -0
- dclab/rtdc_dataset/feat_anc_plugin/__init__.py +3 -0
- dclab/rtdc_dataset/feat_anc_plugin/plugin_feature.py +329 -0
- dclab/rtdc_dataset/feat_basin.py +762 -0
- dclab/rtdc_dataset/feat_temp.py +102 -0
- dclab/rtdc_dataset/filter.py +263 -0
- dclab/rtdc_dataset/fmt_dcor/__init__.py +7 -0
- dclab/rtdc_dataset/fmt_dcor/access_token.py +52 -0
- dclab/rtdc_dataset/fmt_dcor/api.py +173 -0
- dclab/rtdc_dataset/fmt_dcor/base.py +299 -0
- dclab/rtdc_dataset/fmt_dcor/basin.py +73 -0
- dclab/rtdc_dataset/fmt_dcor/logs.py +26 -0
- dclab/rtdc_dataset/fmt_dcor/tables.py +66 -0
- dclab/rtdc_dataset/fmt_dict.py +103 -0
- dclab/rtdc_dataset/fmt_hdf5/__init__.py +6 -0
- dclab/rtdc_dataset/fmt_hdf5/base.py +192 -0
- dclab/rtdc_dataset/fmt_hdf5/basin.py +30 -0
- dclab/rtdc_dataset/fmt_hdf5/events.py +276 -0
- dclab/rtdc_dataset/fmt_hdf5/feat_defect.py +164 -0
- dclab/rtdc_dataset/fmt_hdf5/logs.py +33 -0
- dclab/rtdc_dataset/fmt_hdf5/tables.py +60 -0
- dclab/rtdc_dataset/fmt_hierarchy/__init__.py +11 -0
- dclab/rtdc_dataset/fmt_hierarchy/base.py +278 -0
- dclab/rtdc_dataset/fmt_hierarchy/events.py +146 -0
- dclab/rtdc_dataset/fmt_hierarchy/hfilter.py +140 -0
- dclab/rtdc_dataset/fmt_hierarchy/mapper.py +134 -0
- dclab/rtdc_dataset/fmt_http.py +102 -0
- dclab/rtdc_dataset/fmt_s3.py +354 -0
- dclab/rtdc_dataset/fmt_tdms/__init__.py +476 -0
- dclab/rtdc_dataset/fmt_tdms/event_contour.py +264 -0
- dclab/rtdc_dataset/fmt_tdms/event_image.py +220 -0
- dclab/rtdc_dataset/fmt_tdms/event_mask.py +62 -0
- dclab/rtdc_dataset/fmt_tdms/event_trace.py +146 -0
- dclab/rtdc_dataset/fmt_tdms/exc.py +37 -0
- dclab/rtdc_dataset/fmt_tdms/naming.py +151 -0
- dclab/rtdc_dataset/load.py +77 -0
- dclab/rtdc_dataset/meta_table.py +25 -0
- dclab/rtdc_dataset/writer.py +1019 -0
- dclab/statistics.py +226 -0
- dclab/util.py +176 -0
- dclab/warn.py +15 -0
- dclab-0.67.0.dist-info/METADATA +153 -0
- dclab-0.67.0.dist-info/RECORD +142 -0
- dclab-0.67.0.dist-info/WHEEL +6 -0
- dclab-0.67.0.dist-info/entry_points.txt +8 -0
- dclab-0.67.0.dist-info/licenses/LICENSE +283 -0
- dclab-0.67.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from . import _find_contours_cy
|
|
3
|
+
|
|
4
|
+
from collections import deque
|
|
5
|
+
|
|
6
|
+
_param_options = ('high', 'low')
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def find_contours(array, level,
|
|
10
|
+
fully_connected='low', positive_orientation='low'):
|
|
11
|
+
"""Find iso-valued contours in a 2D array for a given level value.
|
|
12
|
+
|
|
13
|
+
Uses the "marching squares" method to compute a the iso-valued contours of
|
|
14
|
+
the input 2D array for a particular level value. Array values are linearly
|
|
15
|
+
interpolated to provide better precision for the output contours.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
array : 2D ndarray of double
|
|
20
|
+
Input data in which to find contours.
|
|
21
|
+
level : float
|
|
22
|
+
Value along which to find contours in the array.
|
|
23
|
+
fully_connected : str, {'low', 'high'}
|
|
24
|
+
Indicates whether array elements below the given level value are to be
|
|
25
|
+
considered fully-connected (and hence elements above the value will
|
|
26
|
+
only be face connected), or vice-versa. (See notes below for details.)
|
|
27
|
+
positive_orientation : either 'low' or 'high'
|
|
28
|
+
Indicates whether the output contours will produce positively-oriented
|
|
29
|
+
polygons around islands of low- or high-valued elements. If 'low' then
|
|
30
|
+
contours will wind counter- clockwise around elements below the
|
|
31
|
+
iso-value. Alternately, this means that low-valued elements are always
|
|
32
|
+
on the left of the contour. (See below for details.)
|
|
33
|
+
|
|
34
|
+
Returns
|
|
35
|
+
-------
|
|
36
|
+
contours : list of (n,2)-ndarrays
|
|
37
|
+
Each contour is an ndarray of shape ``(n, 2)``,
|
|
38
|
+
consisting of n ``(row, column)`` coordinates along the contour.
|
|
39
|
+
|
|
40
|
+
Notes
|
|
41
|
+
-----
|
|
42
|
+
The marching squares algorithm is a special case of the marching cubes
|
|
43
|
+
algorithm [1]_. A simple explanation is available here::
|
|
44
|
+
|
|
45
|
+
http://www.essi.fr/~lingrand/MarchingCubes/algo.html
|
|
46
|
+
|
|
47
|
+
There is a single ambiguous case in the marching squares algorithm: when
|
|
48
|
+
a given ``2 x 2``-element square has two high-valued and two low-valued
|
|
49
|
+
elements, each pair diagonally adjacent. (Where high- and low-valued is
|
|
50
|
+
with respect to the contour value sought.) In this case, either the
|
|
51
|
+
high-valued elements can be 'connected together' via a thin isthmus that
|
|
52
|
+
separates the low-valued elements, or vice-versa. When elements are
|
|
53
|
+
connected together across a diagonal, they are considered 'fully
|
|
54
|
+
connected' (also known as 'face+vertex-connected' or '8-connected'). Only
|
|
55
|
+
high-valued or low-valued elements can be fully-connected, the other set
|
|
56
|
+
will be considered as 'face-connected' or '4-connected'. By default,
|
|
57
|
+
low-valued elements are considered fully-connected; this can be altered
|
|
58
|
+
with the 'fully_connected' parameter.
|
|
59
|
+
|
|
60
|
+
Output contours are not guaranteed to be closed: contours which intersect
|
|
61
|
+
the array edge will be left open. All other contours will be closed. (The
|
|
62
|
+
closed-ness of a contours can be tested by checking whether the beginning
|
|
63
|
+
point is the same as the end point.)
|
|
64
|
+
|
|
65
|
+
Contours are oriented. By default, array values lower than the contour
|
|
66
|
+
value are to the left of the contour and values greater than the contour
|
|
67
|
+
value are to the right. This means that contours will wind
|
|
68
|
+
counter-clockwise (i.e. in 'positive orientation') around islands of
|
|
69
|
+
low-valued pixels. This behavior can be altered with the
|
|
70
|
+
'positive_orientation' parameter.
|
|
71
|
+
|
|
72
|
+
The order of the contours in the output list is determined by the position
|
|
73
|
+
of the smallest ``x,y`` (in lexicographical order) coordinate in the
|
|
74
|
+
contour. This is a side-effect of how the input array is traversed, but
|
|
75
|
+
can be relied upon.
|
|
76
|
+
|
|
77
|
+
.. warning::
|
|
78
|
+
|
|
79
|
+
Array coordinates/values are assumed to refer to the *center* of the
|
|
80
|
+
array element. Take a simple example input: ``[0, 1]``. The interpolated
|
|
81
|
+
position of 0.5 in this array is midway between the 0-element (at
|
|
82
|
+
``x=0``) and the 1-element (at ``x=1``), and thus would fall at
|
|
83
|
+
``x=0.5``.
|
|
84
|
+
|
|
85
|
+
This means that to find reasonable contours, it is best to find contours
|
|
86
|
+
midway between the expected "light" and "dark" values. In particular,
|
|
87
|
+
given a binarized array, *do not* choose to find contours at the low or
|
|
88
|
+
high value of the array. This will often yield degenerate contours,
|
|
89
|
+
especially around structures that are a single array element wide. Instead
|
|
90
|
+
choose a middle value, as above.
|
|
91
|
+
|
|
92
|
+
References
|
|
93
|
+
----------
|
|
94
|
+
.. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High
|
|
95
|
+
Resolution 3D Surface Construction Algorithm. Computer Graphics
|
|
96
|
+
(SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).
|
|
97
|
+
|
|
98
|
+
Examples
|
|
99
|
+
--------
|
|
100
|
+
>>> a = np.zeros((3, 3))
|
|
101
|
+
>>> a[0, 0] = 1
|
|
102
|
+
>>> a
|
|
103
|
+
array([[ 1., 0., 0.],
|
|
104
|
+
[ 0., 0., 0.],
|
|
105
|
+
[ 0., 0., 0.]])
|
|
106
|
+
>>> find_contours(a, 0.5)
|
|
107
|
+
[array([[ 0. , 0.5],
|
|
108
|
+
[ 0.5, 0. ]])]
|
|
109
|
+
"""
|
|
110
|
+
array = np.asarray(array, dtype=np.double)
|
|
111
|
+
if array.ndim != 2:
|
|
112
|
+
raise ValueError('Only 2D arrays are supported.')
|
|
113
|
+
level = float(level)
|
|
114
|
+
if (fully_connected not in _param_options or
|
|
115
|
+
positive_orientation not in _param_options):
|
|
116
|
+
raise ValueError('Parameters "fully_connected" and'
|
|
117
|
+
' "positive_orientation" must be either "high" or'
|
|
118
|
+
' "low".')
|
|
119
|
+
point_list = _find_contours_cy.iterate_and_store(array, level,
|
|
120
|
+
fully_connected == 'high')
|
|
121
|
+
contours = _assemble_contours(_take_2(point_list))
|
|
122
|
+
if positive_orientation == 'high':
|
|
123
|
+
contours = [c[::-1] for c in contours]
|
|
124
|
+
return contours
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _take_2(seq):
|
|
128
|
+
iterator = iter(seq)
|
|
129
|
+
while True:
|
|
130
|
+
try:
|
|
131
|
+
n1 = next(iterator)
|
|
132
|
+
n2 = next(iterator)
|
|
133
|
+
yield (n1, n2)
|
|
134
|
+
except StopIteration:
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _assemble_contours(points_iterator):
|
|
139
|
+
current_index = 0
|
|
140
|
+
contours = {}
|
|
141
|
+
starts = {}
|
|
142
|
+
ends = {}
|
|
143
|
+
for from_point, to_point in points_iterator:
|
|
144
|
+
# Ignore degenerate segments.
|
|
145
|
+
# This happens when (and only when) one vertex of the square is
|
|
146
|
+
# exactly the contour level, and the rest are above or below.
|
|
147
|
+
# This degnerate vertex will be picked up later by neighboring squares.
|
|
148
|
+
if from_point == to_point:
|
|
149
|
+
continue
|
|
150
|
+
|
|
151
|
+
tail_data = starts.get(to_point)
|
|
152
|
+
head_data = ends.get(from_point)
|
|
153
|
+
|
|
154
|
+
if tail_data is not None and head_data is not None:
|
|
155
|
+
tail, tail_num = tail_data
|
|
156
|
+
head, head_num = head_data
|
|
157
|
+
# We need to connect these two contours.
|
|
158
|
+
if tail is head:
|
|
159
|
+
# We need to closed a contour.
|
|
160
|
+
# Add the end point, and remove the contour from the
|
|
161
|
+
# 'starts' and 'ends' dicts.
|
|
162
|
+
head.append(to_point)
|
|
163
|
+
del starts[to_point]
|
|
164
|
+
del ends[from_point]
|
|
165
|
+
else: # tail is not head
|
|
166
|
+
# We need to join two distinct contours.
|
|
167
|
+
# We want to keep the first contour segment created, so that
|
|
168
|
+
# the final contours are ordered left->right, top->bottom.
|
|
169
|
+
if tail_num > head_num:
|
|
170
|
+
# tail was created second. Append tail to head.
|
|
171
|
+
head.extend(tail)
|
|
172
|
+
# remove all traces of tail:
|
|
173
|
+
del starts[to_point]
|
|
174
|
+
try:
|
|
175
|
+
del ends[tail[-1]]
|
|
176
|
+
except KeyError:
|
|
177
|
+
pass
|
|
178
|
+
del contours[tail_num]
|
|
179
|
+
# remove the old end of head and add the new end.
|
|
180
|
+
del ends[from_point]
|
|
181
|
+
ends[head[-1]] = (head, head_num)
|
|
182
|
+
else: # tail_num <= head_num
|
|
183
|
+
# head was created second. Prepend head to tail.
|
|
184
|
+
tail.extendleft(reversed(head))
|
|
185
|
+
# remove all traces of head:
|
|
186
|
+
del starts[head[0]]
|
|
187
|
+
del ends[from_point]
|
|
188
|
+
del contours[head_num]
|
|
189
|
+
# remove the old start of tail and add the new start.
|
|
190
|
+
del starts[to_point]
|
|
191
|
+
starts[tail[0]] = (tail, tail_num)
|
|
192
|
+
elif tail_data is None and head_data is None:
|
|
193
|
+
# we need to add a new contour
|
|
194
|
+
current_index += 1
|
|
195
|
+
new_num = current_index
|
|
196
|
+
new_contour = deque((from_point, to_point))
|
|
197
|
+
contours[new_num] = new_contour
|
|
198
|
+
starts[from_point] = (new_contour, new_num)
|
|
199
|
+
ends[to_point] = (new_contour, new_num)
|
|
200
|
+
elif tail_data is not None and head_data is None:
|
|
201
|
+
tail, tail_num = tail_data
|
|
202
|
+
# We've found a single contour to which the new segment should be
|
|
203
|
+
# prepended.
|
|
204
|
+
tail.appendleft(from_point)
|
|
205
|
+
del starts[to_point]
|
|
206
|
+
starts[from_point] = (tail, tail_num)
|
|
207
|
+
elif tail_data is None and head_data is not None:
|
|
208
|
+
head, head_num = head_data
|
|
209
|
+
# We've found a single contour to which the new segment should be
|
|
210
|
+
# appended
|
|
211
|
+
head.append(to_point)
|
|
212
|
+
del ends[from_point]
|
|
213
|
+
ends[to_point] = (head, head_num)
|
|
214
|
+
# end iteration over from_ and to_ points
|
|
215
|
+
|
|
216
|
+
return [np.array(contour) for (num, contour) in sorted(contours.items())]
|
|
Binary file
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#cython: cdivision=True
|
|
2
|
+
#cython: boundscheck=False
|
|
3
|
+
#cython: nonecheck=False
|
|
4
|
+
#cython: wraparound=False
|
|
5
|
+
#cython: language_level=3
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
cdef inline double _get_fraction(double from_value, double to_value,
|
|
10
|
+
double level):
|
|
11
|
+
if (to_value == from_value):
|
|
12
|
+
return 0
|
|
13
|
+
return ((level - from_value) / (to_value - from_value))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def iterate_and_store(double[:, :] array,
|
|
17
|
+
double level, Py_ssize_t vertex_connect_high):
|
|
18
|
+
"""Iterate across the given array in a marching-squares fashion,
|
|
19
|
+
looking for segments that cross 'level'. If such a segment is
|
|
20
|
+
found, its coordinates are added to a growing list of segments,
|
|
21
|
+
which is returned by the function. if vertex_connect_high is
|
|
22
|
+
nonzero, high-values pixels are considered to be face+vertex
|
|
23
|
+
connected into objects; otherwise low-valued pixels are.
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
if array.shape[0] < 2 or array.shape[1] < 2:
|
|
27
|
+
raise ValueError("Input array must be at least 2x2.")
|
|
28
|
+
|
|
29
|
+
cdef list arc_list = []
|
|
30
|
+
cdef Py_ssize_t n
|
|
31
|
+
|
|
32
|
+
# The plan is to iterate a 2x2 square across the input array. This means
|
|
33
|
+
# that the upper-left corner of the square needs to iterate across a
|
|
34
|
+
# sub-array that's one-less-large in each direction (so that the square
|
|
35
|
+
# never steps out of bounds). The square is represented by four pointers:
|
|
36
|
+
# ul, ur, ll, and lr (for 'upper left', etc.). We also maintain the current
|
|
37
|
+
# 2D coordinates for the position of the upper-left pointer. Note that we
|
|
38
|
+
# ensured that the array is of type 'double' and is C-contiguous (last
|
|
39
|
+
# index varies the fastest).
|
|
40
|
+
|
|
41
|
+
# Current coords start at 0,0.
|
|
42
|
+
cdef Py_ssize_t[2] coords
|
|
43
|
+
coords[0] = 0
|
|
44
|
+
coords[1] = 0
|
|
45
|
+
|
|
46
|
+
# Calculate the number of iterations we'll need
|
|
47
|
+
cdef Py_ssize_t num_square_steps = (array.shape[0] - 1) \
|
|
48
|
+
* (array.shape[1] - 1)
|
|
49
|
+
|
|
50
|
+
cdef unsigned char square_case = 0
|
|
51
|
+
cdef tuple top, bottom, left, right
|
|
52
|
+
cdef double ul, ur, ll, lr
|
|
53
|
+
cdef Py_ssize_t r0, r1, c0, c1
|
|
54
|
+
|
|
55
|
+
for n in range(num_square_steps):
|
|
56
|
+
# There are sixteen different possible square types, diagramed below.
|
|
57
|
+
# A + indicates that the vertex is above the contour value, and a -
|
|
58
|
+
# indicates that the vertex is below or equal to the contour value.
|
|
59
|
+
# The vertices of each square are:
|
|
60
|
+
# ul ur
|
|
61
|
+
# ll lr
|
|
62
|
+
# and can be treated as a binary value with the bits in that order. Thus
|
|
63
|
+
# each square case can be numbered:
|
|
64
|
+
# 0-- 1+- 2-+ 3++ 4-- 5+- 6-+ 7++
|
|
65
|
+
# -- -- -- -- +- +- +- +-
|
|
66
|
+
#
|
|
67
|
+
# 8-- 9+- 10-+ 11++ 12-- 13+- 14-+ 15++
|
|
68
|
+
# -+ -+ -+ -+ ++ ++ ++ ++
|
|
69
|
+
#
|
|
70
|
+
# The position of the line segment that cuts through (or
|
|
71
|
+
# doesn't, in case 0 and 15) each square is clear, except in
|
|
72
|
+
# cases 6 and 9. In this case, where the segments are placed
|
|
73
|
+
# is determined by vertex_connect_high. If
|
|
74
|
+
# vertex_connect_high is false, then lines like \\ are drawn
|
|
75
|
+
# through square 6, and lines like // are drawn through square
|
|
76
|
+
# 9. Otherwise, the situation is reversed.
|
|
77
|
+
# Finally, recall that we draw the lines so that (moving from tail to
|
|
78
|
+
# head) the lower-valued pixels are on the left of the line. So, for
|
|
79
|
+
# example, case 1 entails a line slanting from the middle of the top of
|
|
80
|
+
# the square to the middle of the left side of the square.
|
|
81
|
+
|
|
82
|
+
r0, c0 = coords[0], coords[1]
|
|
83
|
+
r1, c1 = r0 + 1, c0 + 1
|
|
84
|
+
|
|
85
|
+
ul = array[r0, c0]
|
|
86
|
+
ur = array[r0, c1]
|
|
87
|
+
ll = array[r1, c0]
|
|
88
|
+
lr = array[r1, c1]
|
|
89
|
+
|
|
90
|
+
# now in advance the coords indices
|
|
91
|
+
if coords[1] < array.shape[1] - 2:
|
|
92
|
+
coords[1] += 1
|
|
93
|
+
else:
|
|
94
|
+
coords[0] += 1
|
|
95
|
+
coords[1] = 0
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
square_case = 0
|
|
99
|
+
if (ul > level): square_case += 1
|
|
100
|
+
if (ur > level): square_case += 2
|
|
101
|
+
if (ll > level): square_case += 4
|
|
102
|
+
if (lr > level): square_case += 8
|
|
103
|
+
|
|
104
|
+
if (square_case != 0 and square_case != 15):
|
|
105
|
+
# only do anything if there's a line passing through the
|
|
106
|
+
# square. Cases 0 and 15 are entirely below/above the contour.
|
|
107
|
+
|
|
108
|
+
top = r0, c0 + _get_fraction(ul, ur, level)
|
|
109
|
+
bottom = r1, c0 + _get_fraction(ll, lr, level)
|
|
110
|
+
left = r0 + _get_fraction(ul, ll, level), c0
|
|
111
|
+
right = r0 + _get_fraction(ur, lr, level), c1
|
|
112
|
+
|
|
113
|
+
if (square_case == 1):
|
|
114
|
+
# top to left
|
|
115
|
+
arc_list.append(top)
|
|
116
|
+
arc_list.append(left)
|
|
117
|
+
elif (square_case == 2):
|
|
118
|
+
# right to top
|
|
119
|
+
arc_list.append(right)
|
|
120
|
+
arc_list.append(top)
|
|
121
|
+
elif (square_case == 3):
|
|
122
|
+
# right to left
|
|
123
|
+
arc_list.append(right)
|
|
124
|
+
arc_list.append(left)
|
|
125
|
+
elif (square_case == 4):
|
|
126
|
+
# left to bottom
|
|
127
|
+
arc_list.append(left)
|
|
128
|
+
arc_list.append(bottom)
|
|
129
|
+
elif (square_case == 5):
|
|
130
|
+
# top to bottom
|
|
131
|
+
arc_list.append(top)
|
|
132
|
+
arc_list.append(bottom)
|
|
133
|
+
elif (square_case == 6):
|
|
134
|
+
if vertex_connect_high:
|
|
135
|
+
arc_list.append(left)
|
|
136
|
+
arc_list.append(top)
|
|
137
|
+
|
|
138
|
+
arc_list.append(right)
|
|
139
|
+
arc_list.append(bottom)
|
|
140
|
+
else:
|
|
141
|
+
arc_list.append(right)
|
|
142
|
+
arc_list.append(top)
|
|
143
|
+
arc_list.append(left)
|
|
144
|
+
arc_list.append(bottom)
|
|
145
|
+
elif (square_case == 7):
|
|
146
|
+
# right to bottom
|
|
147
|
+
arc_list.append(right)
|
|
148
|
+
arc_list.append(bottom)
|
|
149
|
+
elif (square_case == 8):
|
|
150
|
+
# bottom to right
|
|
151
|
+
arc_list.append(bottom)
|
|
152
|
+
arc_list.append(right)
|
|
153
|
+
elif (square_case == 9):
|
|
154
|
+
if vertex_connect_high:
|
|
155
|
+
arc_list.append(top)
|
|
156
|
+
arc_list.append(right)
|
|
157
|
+
|
|
158
|
+
arc_list.append(bottom)
|
|
159
|
+
arc_list.append(left)
|
|
160
|
+
else:
|
|
161
|
+
arc_list.append(top)
|
|
162
|
+
arc_list.append(left)
|
|
163
|
+
|
|
164
|
+
arc_list.append(bottom)
|
|
165
|
+
arc_list.append(right)
|
|
166
|
+
elif (square_case == 10):
|
|
167
|
+
# bottom to top
|
|
168
|
+
arc_list.append(bottom)
|
|
169
|
+
arc_list.append(top)
|
|
170
|
+
elif (square_case == 11):
|
|
171
|
+
# bottom to left
|
|
172
|
+
arc_list.append(bottom)
|
|
173
|
+
arc_list.append(left)
|
|
174
|
+
elif (square_case == 12):
|
|
175
|
+
# lef to right
|
|
176
|
+
arc_list.append(left)
|
|
177
|
+
arc_list.append(right)
|
|
178
|
+
elif (square_case == 13):
|
|
179
|
+
# top to right
|
|
180
|
+
arc_list.append(top)
|
|
181
|
+
arc_list.append(right)
|
|
182
|
+
elif (square_case == 14):
|
|
183
|
+
# left to top
|
|
184
|
+
arc_list.append(left)
|
|
185
|
+
arc_list.append(top)
|
|
186
|
+
|
|
187
|
+
return arc_list
|
|
188
|
+
|
|
Binary file
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#cython: cdivision=True
|
|
2
|
+
#cython: boundscheck=False
|
|
3
|
+
#cython: nonecheck=False
|
|
4
|
+
#cython: wraparound=False
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
cimport numpy as cnp
|
|
8
|
+
from ._shared.geometry cimport point_in_polygon, points_in_polygon
|
|
9
|
+
|
|
10
|
+
cnp.import_array()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _grid_points_in_poly(shape, verts):
|
|
14
|
+
"""Test whether points on a specified grid are inside a polygon.
|
|
15
|
+
|
|
16
|
+
For each ``(r, c)`` coordinate on a grid, i.e. ``(0, 0)``, ``(0, 1)`` etc.,
|
|
17
|
+
test whether that point lies inside a polygon.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
shape : tuple (M, N)
|
|
22
|
+
Shape of the grid.
|
|
23
|
+
verts : (V, 2) array
|
|
24
|
+
Specify the V vertices of the polygon, sorted either clockwise
|
|
25
|
+
or anti-clockwise. The first point may (but does not need to be)
|
|
26
|
+
duplicated.
|
|
27
|
+
|
|
28
|
+
See Also
|
|
29
|
+
--------
|
|
30
|
+
points_in_poly
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
mask : (M, N) ndarray of bool
|
|
35
|
+
True where the grid falls inside the polygon.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
cdef double[:] vx, vy
|
|
39
|
+
verts = np.asarray(verts)
|
|
40
|
+
|
|
41
|
+
vx = verts[:, 0].astype(np.double)
|
|
42
|
+
vy = verts[:, 1].astype(np.double)
|
|
43
|
+
cdef Py_ssize_t V = vx.shape[0]
|
|
44
|
+
|
|
45
|
+
cdef Py_ssize_t M = shape[0]
|
|
46
|
+
cdef Py_ssize_t N = shape[1]
|
|
47
|
+
cdef Py_ssize_t m, n
|
|
48
|
+
|
|
49
|
+
cdef cnp.ndarray[dtype=cnp.uint8_t, ndim=2, mode="c"] out = \
|
|
50
|
+
np.zeros((M, N), dtype=np.uint8)
|
|
51
|
+
|
|
52
|
+
with nogil:
|
|
53
|
+
for m in range(M):
|
|
54
|
+
for n in range(N):
|
|
55
|
+
out[m, n] = point_in_polygon(V, &vx[0], &vy[0], m, n)
|
|
56
|
+
|
|
57
|
+
return out.view(bool)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _points_in_poly(points, verts):
|
|
61
|
+
"""Test whether points lie inside a polygon.
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
points : (N, 2) array
|
|
66
|
+
Input points, ``(x, y)``.
|
|
67
|
+
verts : (M, 2) array
|
|
68
|
+
Vertices of the polygon, sorted either clockwise or anti-clockwise.
|
|
69
|
+
The first point may (but does not need to be) duplicated.
|
|
70
|
+
|
|
71
|
+
See Also
|
|
72
|
+
--------
|
|
73
|
+
grid_points_in_poly
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
mask : (N,) array of bool
|
|
78
|
+
True if corresponding point is inside the polygon.
|
|
79
|
+
|
|
80
|
+
"""
|
|
81
|
+
cdef double[:] x, y, vx, vy
|
|
82
|
+
|
|
83
|
+
points = np.asarray(points)
|
|
84
|
+
verts = np.asarray(verts)
|
|
85
|
+
|
|
86
|
+
x = points[:, 0].astype(np.double)
|
|
87
|
+
y = points[:, 1].astype(np.double)
|
|
88
|
+
|
|
89
|
+
vx = verts[:, 0].astype(np.double)
|
|
90
|
+
vy = verts[:, 1].astype(np.double)
|
|
91
|
+
|
|
92
|
+
cdef cnp.ndarray[cnp.uint8_t, ndim=1] out = \
|
|
93
|
+
np.zeros(x.shape[0], dtype=np.uint8)
|
|
94
|
+
|
|
95
|
+
points_in_polygon(vx.shape[0], &vx[0], &vy[0],
|
|
96
|
+
x.shape[0], &x[0], &y[0],
|
|
97
|
+
<unsigned char*>out.data)
|
|
98
|
+
|
|
99
|
+
return out.astype(bool)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from . import geometry # noqa: F401
|
|
Binary file
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#cython: cdivision=True
|
|
2
|
+
#cython: boundscheck=False
|
|
3
|
+
#cython: nonecheck=False
|
|
4
|
+
#cython: wraparound=False
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
cdef inline unsigned char point_in_polygon(Py_ssize_t nr_verts, double *xp,
|
|
8
|
+
double *yp, double x,
|
|
9
|
+
double y) nogil:
|
|
10
|
+
"""Test whether point lies inside a polygon.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
nr_verts : int
|
|
15
|
+
Number of vertices of polygon.
|
|
16
|
+
xp, yp : double array
|
|
17
|
+
Coordinates of polygon with length nr_verts.
|
|
18
|
+
x, y : double
|
|
19
|
+
Coordinates of point.
|
|
20
|
+
"""
|
|
21
|
+
cdef Py_ssize_t i
|
|
22
|
+
cdef unsigned char c = 0
|
|
23
|
+
cdef Py_ssize_t j = nr_verts - 1
|
|
24
|
+
for i in range(nr_verts):
|
|
25
|
+
if (
|
|
26
|
+
(((yp[i] <= y) and (y < yp[j])) or
|
|
27
|
+
((yp[j] <= y) and (y < yp[i])))
|
|
28
|
+
and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])
|
|
29
|
+
):
|
|
30
|
+
c = not c
|
|
31
|
+
j = i
|
|
32
|
+
return c
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
cdef void points_in_polygon(Py_ssize_t nr_verts, double *xp, double *yp,
|
|
36
|
+
Py_ssize_t nr_points, double *x, double *y,
|
|
37
|
+
unsigned char *result) nogil:
|
|
38
|
+
"""Test whether points lie inside a polygon.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
nr_verts : int
|
|
43
|
+
Number of vertices of polygon.
|
|
44
|
+
xp, yp : double array
|
|
45
|
+
Coordinates of polygon with length nr_verts.
|
|
46
|
+
nr_points : int
|
|
47
|
+
Number of points to test.
|
|
48
|
+
x, y : double array
|
|
49
|
+
Coordinates of points.
|
|
50
|
+
result : unsigned char array
|
|
51
|
+
Test results for each point.
|
|
52
|
+
"""
|
|
53
|
+
cdef Py_ssize_t n
|
|
54
|
+
for n in range(nr_points):
|
|
55
|
+
result[n] = point_in_polygon(nr_verts, xp, yp, x[n], y[n])
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from ._pnpoly import _grid_points_in_poly, _points_in_poly
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def grid_points_in_poly(shape, verts):
|
|
5
|
+
"""Test whether points on a specified grid are inside a polygon.
|
|
6
|
+
|
|
7
|
+
For each ``(r, c)`` coordinate on a grid, i.e. ``(0, 0)``, ``(0, 1)`` etc.,
|
|
8
|
+
test whether that point lies inside a polygon.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
shape : tuple (M, N)
|
|
13
|
+
Shape of the grid.
|
|
14
|
+
verts : (V, 2) array
|
|
15
|
+
Specify the V vertices of the polygon, sorted either clockwise
|
|
16
|
+
or anti-clockwise. The first point may (but does not need to be)
|
|
17
|
+
duplicated.
|
|
18
|
+
|
|
19
|
+
See Also
|
|
20
|
+
--------
|
|
21
|
+
points_in_poly
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
mask : (M, N) ndarray of bool
|
|
26
|
+
True where the grid falls inside the polygon.
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
return _grid_points_in_poly(shape, verts)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def points_in_poly(points, verts):
|
|
33
|
+
"""Test whether points lie inside a polygon.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
points : (N, 2) array
|
|
38
|
+
Input points, ``(x, y)``.
|
|
39
|
+
verts : (M, 2) array
|
|
40
|
+
Vertices of the polygon, sorted either clockwise or anti-clockwise.
|
|
41
|
+
The first point may (but does not need to be) duplicated.
|
|
42
|
+
|
|
43
|
+
See Also
|
|
44
|
+
--------
|
|
45
|
+
grid_points_in_poly
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
mask : (N,) array of bool
|
|
50
|
+
True if corresponding point is inside the polygon.
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
return _points_in_poly(points, verts)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Copyright (C) 2006, Jonathan E. Taylor
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2006-2008 Scipy Developers.
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2009-2018 Statsmodels Developers.
|
|
8
|
+
All rights reserved.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
Redistribution and use in source and binary forms, with or without
|
|
12
|
+
modification, are permitted provided that the following conditions are met:
|
|
13
|
+
|
|
14
|
+
a. Redistributions of source code must retain the above copyright notice,
|
|
15
|
+
this list of conditions and the following disclaimer.
|
|
16
|
+
b. Redistributions in binary form must reproduce the above copyright
|
|
17
|
+
notice, this list of conditions and the following disclaimer in the
|
|
18
|
+
documentation and/or other materials provided with the distribution.
|
|
19
|
+
c. Neither the name of Statsmodels nor the names of its contributors
|
|
20
|
+
may be used to endorse or promote products derived from this software
|
|
21
|
+
without specific prior written permission.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
25
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
26
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
27
|
+
ARE DISCLAIMED. IN NO EVENT SHALL STATSMODELS OR CONTRIBUTORS BE LIABLE FOR
|
|
28
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
29
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
30
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
31
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
32
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
33
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
34
|
+
DAMAGE.
|
|
35
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from . import kernel_density # noqa: F401
|