-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_geometry.f90
327 lines (245 loc) · 11.7 KB
/
test_geometry.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
program test
use libflit
block
real, allocatable, dimension(:, :) :: ps, p1, p2, p3, p4
real, allocatable, dimension(:) :: pr1, pr2, pr3
integer, allocatable, dimension(:) :: sindex
integer, allocatable, dimension(:) :: unindex
integer :: np, np1, np2, np3, np4, ic
type(hdbscan_param) :: p
np1 = 100
p1 = zeros(np1, 3)
p1(:, 1) = rescale(random(np1, dist='normal'), [1.0, 14.0])
p1(:, 2) = rescale(random(np1, dist='normal'), [-1.0, 3.0])
p1(:, 3) = rescale(random(np1, dist='normal'), [0.2, 4.0])
p1 = rotate_point(p1, [0.0, real(30.0*const_deg2rad), real(50.0*const_deg2rad)], [0.0, 0.0, 0.0], 'zyx')
np2 = 200
p2 = zeros(np2, 3)
p2(:, 1) = rescale(random(np2, dist='normal'), [1.0, 11.0]) + 4.0
p2(:, 2) = rescale(random(np2, dist='normal'), [-1.0, 5.0]) - 2.0
p2(:, 3) = rescale(random(np2, dist='normal'), [0.5, 6.0]) + 2.0
p2 = rotate_point(p2, [real(20.0*const_deg2rad), real(-30.0*const_deg2rad), real(-10.0*const_deg2rad)], [0.0, 0.0, 0.0], 'zyx')
np3 = 300
p3 = zeros(np3, 3)
p3(:, 1) = rescale(random(np3, dist='normal'), [2.0, 10.0])
p3(:, 2) = rescale(random(np3, dist='normal'), [-5.0, -2.0]) - 2.0
p3(:, 3) = rescale(random(np3, dist='normal'), [-3.0, -2.0]) + 2.0
p3 = rotate_point(p3, [real(45.0*const_deg2rad), real(10.0*const_deg2rad), real(120.0*const_deg2rad)], [0.0, 0.0, 0.0], 'xyz')
np4 = 400
p4 = zeros(np4, 3)
p4(:, 1) = rescale(random(np4, dist='normal'), [2.0, 10.0])
p4(:, 2) = rescale(random(np4, dist='normal'), [-5.0, 5.0]) + 2.0
p4(:, 3) = rescale(random(np4, dist='normal'), [-3.0, 1.0]) - 2.0
p4 = rotate_point(p4, [real(45.0*const_deg2rad), real(90.0*const_deg2rad), real(20.0*const_deg2rad)], [0.0, 0.0, 0.0], 'yzx')
!===========================================================================
np = 200
p%data = zeros(np1 + np2 + np3 + np4 + np, 3)
p%data(1:np1, :) = p1
p%data(np1 + 1:np1 + np2, :) = p2
p%data(np1 + np2 + 1:np1 + np2 + np3, :) = p3
p%data(np1 + np2 + np3 + 1:np1 + np2 + np3 + np4, :) = p4
p%data(:np1 + np2 + np3 + np4, 1) = rescale(p%data(:np1 + np2 + np3 + np4, 1), [20.0, 280.0])
p%data(:np1 + np2 + np3 + np4, 2) = rescale(p%data(:np1 + np2 + np3 + np4, 2), [50.0, 350.0])
p%data(:np1 + np2 + np3 + np4, 3) = rescale(p%data(:np1 + np2 + np3 + np4, 3), [60.0, 440.0])
p%data(np1 + np2 + np3 + np4 + 1:, 1) = random(np, range=[0.0, 300.0])
p%data(np1 + np2 + np3 + np4 + 1:, 2) = random(np, range=[0.0, 400.0])
p%data(np1 + np2 + np3 + np4 + 1:, 3) = random(np, range=[0.0, 500.0])
open(3, file='./test_data_3d.txt')
do i = 1, size(p%data, 1)
write(3, *) p%data(i, :), rand()
end do
close(3)
p%n = size(p%data, 1)
p%nd = 3
p%min_sample = 25
p%min_cluster_size = 25
call hdbscan(p)
print *, p%ncluster, p%nnoisy
open(3, file='./cluster3.txt')
do i = 1, p%n
write(3, *) p%data(i, :), p%labels(i)*1.0
end do
close(3)
unindex = sort(unique(p%labels))
open(3, file='./cluster3_multiple_fit.txt')
do ic = 2, size(unindex)
sindex = pack(regspace(1, 1, p%n), mask=(p%labels==unindex(ic)))
np = size(sindex)
print *, ic, np
ps = zeros(np, 3)
ps(:, 1) = p%data(sindex, 1)
ps(:, 2) = p%data(sindex, 2)
ps(:, 3) = p%data(sindex, 3)
call fit_surface(ps(:, 1), ps(:, 2), ps(:, 3), pr1, pr2, pr3, method='polynomial', order=2)
do i = 1, np
write(3, *) pr1(i), pr2(i), pr3(i), ic*1.0
end do
print *, ic
end do
close(3)
end block
block
real, allocatable, dimension(:, :) :: ps
real, allocatable, dimension(:) :: pr1, pr2
integer, allocatable, dimension(:) :: sindex
integer, allocatable, dimension(:) :: unindex
integer :: i, ic, np
type(hdbscan_param) :: p
p%data = load('./multiple_cluster_data.txt', 2309, 2, ascii=.true.)
!===========================================================================
p%n = size(p%data, 1)
p%nd = 2
p%min_sample = 15
p%min_cluster_size = 15
call hdbscan(p)
open(3, file='./cluster_multiple.txt')
do i = 1, p%n
write(3, *) p%data(i, :), p%labels(i)*1.0, p%probs(i)
end do
close(3)
unindex = sort(unique(p%labels))
open(3, file='./cluster_multiple_fit.txt')
do ic = 2, size(unindex)
sindex = pack(regspace(1, 1, p%n), mask=(p%labels==unindex(ic)))
np = size(sindex)
ps = zeros(np, 2)
ps(:, 1) = p%data(sindex, 1)
ps(:, 2) = p%data(sindex, 2)
call fit_curve(ps(:, 1), ps(:, 2), pr1, pr2, method='polynomial', order=3)
do i = 1, np
write(3, *) pr1(i), pr2(i), 100.0, 1.0
end do
print *, ic
end do
close(3)
np = count(p%labels /= 0)
! This needs github.com/lanl/pymplot
call execute_command_line('x_showgraph -in=cluster_multiple.txt,cluster_multiple_fit.txt -ftype=ascii -n1='//num2str(p%n)//','//num2str(np)//' -ptype=3 -tick1beg=-0.6 -tick2beg=-0.6 -x1beg=-0.6 -x1end=0.6 -tick1d=0.3 -label1=X -x2beg=-0.6 -x2end=0.6 -tick2d=0.3 -mtick1=2 -mtick2=2 -select=1,2,3 -color=hsv -ctruncbeg=0.1 -cmin=0 -cmax='//num2str(p%ncluster + 10.0)//' -marker=o,v -markersizemin=5 -markersizemax=5 -markeredgecolor=gray,gray -out=cluster_multiple.png &')
end block
block
real, allocatable, dimension(:) :: p1, p2
integer :: n
type(hdbscan_param) :: p
character(len=1024) :: opts
! Raw data
n = 400
p%data = zeros(n, 2)
p%data(:, 1) = random(n, range=[0.0, 1.0], dist='normal')*2 + 3
p%data(:, 2) = random(n, range=[0.0, 1.0], dist='normal')*0.5 + 1
open(3, file='./test_data.txt')
do i = 1, size(p%data, 1)
write(3, *) p%data(i, 1:2)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='polynomial', order=2)
open(3, file='./test_data_fit_p2.txt')
do i = 1, size(p%data, 1)
write(3, *) p1(i), p2(i)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='lowess', smooth=0.25, order=2)
open(3, file='./test_data_fit_l1.txt')
do i = 1, size(p%data, 1)
write(3, *) p1(i), p2(i)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='lowess', smooth=0.5, order=2)
open(3, file='./test_data_fit_l2.txt')
do i = 1, size(p%data, 1)
write(3, *) p1(i), p2(i)
end do
close(3)
opts = '-x1beg=-5 -x1end=10 -x2beg=-5 -x2end=10 -ftype=ascii -n1=' &
//num2str(n)//','//num2str(n)//' -ptype=2 -marker=o,* -markerfacecolor=b,r -markeredgecolor=none,none -markersize=5,5 ' &
//' -label1=X -label2=Y '
call execute_command_line('x_showgraph -in=test_data.txt,test_data_fit_p2.txt '//tidy(opts)//' -out=raw_p2.png &')
call execute_command_line('x_showgraph -in=test_data.txt,test_data_fit_l1.txt '//tidy(opts)//' -out=raw_l1.png &')
call execute_command_line('x_showgraph -in=test_data.txt,test_data_fit_l2.txt '//tidy(opts)//' -out=raw_l2.png &')
! Rotate
do i = 1, n
p%data(i, :) = rotate_point(p%data(i, :), real(70.0*const_deg2rad), [0.0, 0.0])
end do
open(3, file='./test_data_rotation.txt')
do i = 1, n
write(3, *) p%data(i, 1:2)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='polynomial', order=2)
open(3, file='./test_data_rotation_fit_p2.txt')
do i = 1, n
write(3, *) p1(i), p2(i)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='lowess', smooth=0.25, order=2)
open(3, file='./test_data_rotation_fit_l1.txt')
do i = 1, n
write(3, *) p1(i), p2(i)
end do
close(3)
call fit_curve(p%data(:, 1), p%data(:, 2), p1, p2, method='lowess', smooth=0.5, order=2)
open(3, file='./test_data_rotation_fit_l2.txt')
do i = 1, n
write(3, *) p1(i), p2(i)
end do
close(3)
call execute_command_line('x_showgraph -in=test_data_rotation.txt '//tidy(opts)//' -n1='//num2str(n)//' -out=rotation.png &')
call execute_command_line('x_showgraph -in=test_data_rotation.txt,test_data_rotation_fit_p2.txt '//tidy(opts)//' -out=rotation_p2.png &')
call execute_command_line('x_showgraph -in=test_data_rotation.txt,test_data_rotation_fit_l1.txt '//tidy(opts)//' -out=rotation_l1.png &')
call execute_command_line('x_showgraph -in=test_data_rotation.txt,test_data_rotation_fit_l2.txt '//tidy(opts)//' -out=rotation_l2.png &')
end block
block
real, allocatable, dimension(:, :) :: w
real, allocatable, dimension(:, :) :: ps
real, allocatable, dimension(:) :: pr1, pr2
integer, allocatable, dimension(:) :: sindex
integer, allocatable, dimension(:) :: unindex
type(hdbscan_param) :: p
p%data = zeros(1000, 2)
p%data(1:400, 1) = random(400, range=[0.0, 1.0], dist='normal')*2 +0.7
p%data(1:400, 2) = random(400, range=[0.0, 1.0], dist='normal')*0.5 + 3
p%data(401:, 1) = random(600, range=[0.0, 1.0], dist='normal', sigma=2.0)/2 + 7.0
p%data(401:, 2) = random(600, range=[0.0, 1.0], dist='normal', sigma=2.0)*2
do i = 1, size(p%data, 1)
p%data(i, :) = rotate_point(p%data(i, :), real(30.0*const_deg2rad), [0.0, 0.0])
end do
!===========================================================================
p%n = size(p%data, 1)
p%nd = 2
open(3, file='./test_data.txt', status='replace')
do i = 1, p%n
write(3, *) p%data(i, :)
end do
close(3)
p%min_sample = 35
p%min_cluster_size = 35
call hdbscan(p)
open(3, file='./cluster.txt')
do i = 1, p%n
write(3, *) p%data(i, :), p%labels(i)*1.0
end do
close(3)
unindex = sort(unique(p%labels))
sindex = pack(regspace(1, 1, p%n), mask=(p%labels==unindex(2)))
ps = zeros(size(sindex), 2)
ps(:, 1) = p%data(sindex, 1)
ps(:, 2) = p%data(sindex, 2)
open(3, file='./select.txt')
do i = 1, size(ps, 1)
write(3, *) ps(i, 1:2)
end do
close(3)
w = zeros(400, 600)
! w = fit_curve(ps(:, 1), ps(:, 2), shape(w), [0.025, 0.025], [-5.0, -5.0], 'lowess', 0.5, 2)
! w = fit_curve(ps(:, 1), ps(:, 2), shape(w), [0.025, 0.025], [-5.0, -5.0], 'polynomial', 0.75, 4)
call fit_curve(ps(:, 1), ps(:, 2), pr1, pr2, w, shape(w), [0.025, 0.025], [-5.0, -5.0], 'lowess', 0.5, 2)
call output_array(w, './b.bin')
open(3, file='./select_r.txt')
do i = 1, size(pr1)
write(3, *) pr1(i), pr2(i)
end do
close(3)
! This needs github.com/lanl/pymplot
call execute_command_line('x_showmatrix -in=b.bin -color=bwr -n1=400 -d1=0.025 -o1=-5 -d2=0.025 -o2=-5 -curve=select.txt,select_r.txt -curveselect=1,2 -curvesize=5,10 -curvefacecolor=w,yellow -curvestyle=scattero,scatter* -out=fit.png; vv fit.png ')
end block
stop
end program test