-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.py
696 lines (636 loc) · 22.7 KB
/
Test.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
from PIL import Image
import numpy as np
import scipy.ndimage
import time
import sys
import psutil
import Queue
from scipy.spatial.distance import euclidean
from scipy.spatial import Delaunay
from mayavi import mlab
import math
def _2d_to_3d(fid): # turns 2d numpy arrays into 3d numpy arrays
if len(fid.shape) == 2:
fid = fid.tolist()
fid = [fid]
return fid
def tiff_to_3d(img,start_frame,end_frame):
if((start_frame == None) | (start_frame < 0)):
start_frame = 0
if ((end_frame == None) | (end_frame > img.n_frames)):
end_frame = img.n_frames
img.seek(start_frame)
slice_2d = np.asarray(img)
img_3d = _2d_to_3d(slice_2d)
if(end_frame == start_frame + 1):
return img_3d
for frame in range(start_frame + 1, end_frame):
img.seek(frame)
slice_2d = np.asarray(img)
slice_3d = _2d_to_3d(slice_2d)
img_3d = np.concatenate((img_3d, slice_3d), axis = 0)
return img_3d
#call blackify on a color 2-D image to turn it into a 3-D numpy array
#that can be passed to rock_AStar
def blackify(rock):
rock2 = np.zeros((rock.shape[0], rock.shape[1], 1), dtype = np.uint8)
rock2.fill(255)
for x in range(rock.shape[0]):
for y in range(rock.shape[1]):
for z in range(rock.shape[2]):
if(rock[x][y][z] != 255):
rock2[x][y][0] = 0
return rock2
def tif_to_3d(img, x0, x1, y0, y1, z0, z1):
img.seek(x0)
slice_2d = np.asarray(img.crop((y0, z0, y1, z1)))
img_3d = _2d_to_3d(slice_2d)
for frame in range(x0+1, x1):
img.seek(frame)
slice_2d = np.asarray(img.crop((y0, z0, y1, z1)))
slice_3d = _2d_to_3d(slice_2d)
img_3d = np.concatenate((img_3d, slice_3d))
return img_3d
def read_tiff(filename):
img = Image.open(filename)
return tiff_to_3d(img, 0, img.n_frames)
def read_tif(filename):
img = Image.open(filename)
data = tiff_to_3d(img, 0, img.n_frames)
core = np.asarray(data, dtype=bool)
label, objs = scipy.ndimage.label(core)
print(objs)
return label
def read_coloredslice(filename):
img = Image.open(filename)
rock = tiff_to_3d(img, None, None)
rock2 = blackify(rock)
return rock2
def sample(path):
total = 0
for i in range(len(path)):
total += euclidean(path[i], path[i+1])
return total
def crop(array, shape):
new = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
for k in range(shape[2]):
new[i][j][k] = array[i][j][k]
return new
def get_edges(rock):
edges = []
for x in range(rock.shape[0]):
for y in range(rock.shape[1]):
for z in range(rock.shape[2]):
if(not rock[x][y][z] == rock[x+1][y][z]):
edges = edges + [(x, y, z)]
break
if(not rock[x][y][z] == rock[x-1][y][z]):
edges = edges + [(x, y, z)]
break
if(not rock[x][y][z] == rock[x][y+1][z]):
edges = edges + [(x, y, z)]
break
if(not rock[x][y][z] == rock[x][y-1][z]):
edges = edges + [(x, y, z)]
break
if(not rock[x][y][z] == rock[x][y][z+1]):
edges = edges + [(x, y, z)]
break
if(not rock[x][y][z] == rock[x][y][z-1]):
edges = edges + [(x, y, z)]
break
return edges
#Sample metric function, takes in the rock as a numpy array
def metric(rock):
def distance(path):
total = 0
if(path == []):
return 0
for i in range(len(path)-1):
if(rock[path[i][0]][path[i][1]][path[i][2]] == 0):
total += euclidean(path[i], path[i+1])
else:
total += 100*euclidean(path[i], path[i+1])
return total
return distance
def rock_dijkstra(rock, start, end, distance):
def adj(point, visit):
x, y, z = point
list = []
if(x < rock.shape[0] - 1):
if((x+1, y, z) not in visit):
list = list + [(x+1, y, z)]
if(x > 0):
if((x-1, y, z) not in visit):
list = list + [(x-1, y, z)]
if(y < rock.shape[1] - 1):
if((x, y+1, z) not in visit):
list = list + [(x, y+1, z)]
if(y > 0):
if((x, y-1, z) not in visit):
list = list + [(x, y-1, z)]
if(z < rock.shape[2] - 1):
if((x, y, z+1) not in visit):
list = list + [(x, y, z+1)]
if(z > 0):
if((x, y, z-1) not in visit):
list = list + [(x, y, z-1)]
visit += list
return list
PQ = Queue.PriorityQueue()
PQ.put((0, start, []))
visited = [start]
while(not PQ.empty()):
(d, rover, path) = PQ.get()
if(rover == end):
break
for point in adj(rover, visited):
PQ.put((distance(path + [rover]), point, path + [rover]))
if(rover != end):
print("Couldn't find end")
return []
else:
return path
def dot(point1, point2):
x, y, z = point1
a, b, c = point2
return (x*a + y*b + c*z)
def sub(point1, point2):
x, y, z = point1
a, b, c = point2
return (x-a, y-b, z-c)
#Sample heuristic. Returns the function to be passed into A*
def sampleh(start, end):
return (lambda point : 300*(-dot(sub(start, point), sub(start, end)/(euclidean(start, point)*euclidean(start, end)))))
def metric2(rock, c):
def distance(point1, point2):
if(rock[point2[0]][point2[1]][point2[2]] == 0):
return euclidean(point1, point2)
else:
return c*euclidean(point1, point2)
return distance
def metric3(rock, c):
def distance(point1, point2):
x1 = point1[0]
x2 = point2[0]
if(rock[point2[0]][point2[1]][point2[2]] == 0):
return (1 + 4*abs(x1-x2))*euclidean(point1, point2)
else:
return (1 +4*abs(x1-x2))*c*euclidean(point1, point2)
return distance
def adj(rock, point, visit):
x, y, z = point
list = [(x+1, y, z), (x-1, y, z), (x, y+1, z), (x, y-1, z), (x, y, z+1), (x, y, z-1)]
list = filter(lambda (x, y, z): (x in range(rock.shape[0]) and y in range(rock.shape[1]) and z in range(rock.shape[2]) and not (x, y, z) in visit), list)
return list
def rock_AStar(rock, start, end, distance, h, adj):
PQ = Queue.PriorityQueue()
for x in start:
PQ.put((h(x), 0, x, []))
visited = start
while(not PQ.empty()):
(heu, d, rover, path) = PQ.get()
n = adj(rock, rover, visited)
visited = visited + n.keys()
if(rover in end):
break
for point in n:
newd = d + distance(rover, point)
PQ.put((newd + h(point), newd, point, path + [rover]))
if(not rover in end):
print("Couldn't find end")
return []
else:
return d, path, visited
def adj2(rock, point, visit):
x, y, z = point
list = [(x+1, y, z), (x-1, y, z), (x, y+1, z), (x, y-1, z), (x, y, z+1), (x, y, z-1), (x+1, y+1, z), (x-1, y+1, z), (x+1, y-1, z), (x+1, y, z+1), (x+1, y, z-1),
(x, y+1, z+1), (x, y+1, z-1)]
list = filter(lambda (x, y, z): (x in range(rock.shape[0]) and y in range(rock.shape[1]) and z in range(rock.shape[2]) and not (x, y, z) in visit), list)
out = dict()
for elem in list:
out[elem] = []
return out
def Astar(rock, start, end, distance, h, adj):
PQ = Queue.PriorityQueue()
visited = [start]
paths = dict()
paths[start] = []
distances = dict()
PQ.put((h(start), 0, start, []))
while(not PQ.empty()):
(he, d, rover, path) = PQ.get()
n = adj(rock, rover, visited)
visited = visited + n.keys()
for point in n.keys():
if(point in end):
paths[point] = path + n[point]
newd = d+distance(rover, point)
distances[point] = newd
PQ.put((newd+h(point), newd, point, path+[rover]+n[point]))
if(set(end).issubset(set(visited))):
break
if(not set(end).issubset(set(visited))):
print("search failed")
return paths, visited, distances
def edges(chunk):
e = []
for (x, y, z) in chunk:
if(not (x+1, y, z) in chunk):
e = e + [(x, y, z)]
continue
if(not (x-1, y, z) in chunk):
e = e + [(x, y, z)]
continue
if(not (x, y+1, z) in chunk):
e = e + [(x, y, z)]
continue
if(not (x, y-1, z) in chunk):
e = e + [(x, y, z)]
continue
if(not (x, y, z+1) in chunk):
e = e + [(x, y, z)]
continue
if(not (x, y, z-1) in chunk):
e = e + [(x, y, z)]
continue
return e
def chunk(rock, distance, chunks):
adj_dict = dict()
distance_dict = dict()
total_chunk = []
for chunk in chunks:
total_chunk += chunk
e = edges(chunk)
print(e)
for point1 in e:
print point1
temp = e[:]
temp.remove(point1)
paths, visited, distances = Astar(rock, point1, temp, distance, lambda x : 0, adj2)
distance_dict[point1] = distances
adj_dict[point1] = paths
print(distance_dict)
print(adj_dict)
def d(point1, point2):
if(point1 in distance_dict):
if(point2 in distance_dict[point1]):
return distance_dict[point1][point2]
return distance(point1, point2)
def adjac(rock, point, visit):
if(point in adj_dict):
temp_dict = adj_dict[point].copy()
for p in adj2(rock, point, visit):
temp_dict[p] = []
l = filter(lambda x : not x in visited or not x in chunk, temp_dict.keys())
ret = dict()
for x in l:
ret[x] = temp_dict[x]
return ret
else:
return adj2(rock, point, visit)
return d, adjac
def plot_path(points, rock):
path_rock = np.ones(rock.shape)
for (x, y, z) in points:
path_rock[x][y][z] = 0
@mlab.animate
def anim():
f = mlab.gcf()
while 1:
f.scene.camera.azimuth(1)
f.scene.render()
yield
mlab.figure(bgcolor=(1,1,1)) # Set bkg color
mlab.contour3d(rock,
color = (0,0,0),
contours = 2,
opacity = .2 + .8/rock.shape[0]) # Draw pores for 3d, changed froo .20 * 100 / self.shape[0]
mlab.contour3d(path_rock)
a = anim()
def boundingbox(x1, y1, z1, x2, y2, z2):
temp = []
for x in range(x1, x2):
for y in range(y1, y2):
for z in range(z1, z2):
temp += [(x, y, z)]
return temp
def get_component_array(array, list):
if(list == []):
return []
temp = []
for (x, y, z) in list:
list.remove((x, y, z))
if(array[x][y][z] == 0):
array[x][y][z] = 255
temp = temp + [(x, y, z)]
if(x < array.shape[0] - 1):
list = list + [(x+1, y, z)]
if(x > 0):
list = list + [(x-1, y, z)]
if(y < array.shape[1] - 1):
list = list + [(x, y+1, z)]
if(y > 0):
list = list + [(x, y-1, z)]
if(z < array.shape[2] - 1):
list = list + [(x, y, z+1)]
if(z > 0):
list = list + [(x, y, z-1)]
return temp + get_component_array(array, list)
def get_component(array, temp, x, y, z, i):
list = [(x, y, z)]
l = get_component_array(array, list)
if(l == []):
return (temp, i)
temp[i] = l
i = i+1
return (temp, i)
def scan(array):
temp = dict()
i = 0
a = array.copy()
labels = list(list(list(-1 for x in range(array.shape[2])) for y in range(array.shape[1])) for z in range(array.shape[0]))
for x in range(array.shape[0]):
for y in range(array.shape[1]):
for z in range(array.shape[2]):
if(array[x][y][z] == 0):
(temp, i) = get_component(a, temp, x, y, z, i)
for (x, y, z) in temp[i-1]:
labels[x][y][z] = i-1
return temp, labels
def adjCount(labels, point, eps):
x, y, z = point
found = [labels[x][y][z]]
for x1 in range(x-eps, x+eps):
for y1 in range(y-eps, y+eps):
for z1 in range(z-eps, z+eps):
if(x1 in range(len(labels)) and y1 in range(len(labels[0])) and z1 in range(len(labels[0][0])) and euclidean((x1, y1, z1), point) < eps):
if((not labels[x1][y1][z1] in found) and labels[x1][y1][z1] != -1):
found = list(set(found) | set([labels[x1][y1][z1]]))
return found
def regionQuery(graph, labels, i, eps):
found = []
for point in edges(graph[i]):
found = list(set(found) | set(adjCount(labels, point, eps)))
return found
def findClusters(graph, labels, eps, mincount):
visited = []
noise = []
numClusters = 0
clusters = dict()
for i in graph:
if(i in visited):
continue
else:
visited = visited + [i]
found = regionQuery(graph, labels, i, eps)
if(len(found) < mincount):
noise = noise + [i]
else:
clusters, visited = expandCluster(graph, labels, i, found, numClusters, eps, mincount, clusters, visited)
numClusters = numClusters+1
return clusters
def expandCluster(graph, labels, P, neighbors, C, eps, MinPts, clusters, visited):
clusters[C] = [P]
for i in neighbors:
if(not i in visited):
visited = visited + [i]
neighbors2 = regionQuery(graph, labels, i, eps)
if(len(neighbors2) >= MinPts):
neighbors = neighbors + neighbors2
if(not i in clusters.values()):
clusters[C] += [i]
return clusters, visited
def displayClusters(rock, graph, clusters):
thing = np.zeros(rock.shape)
points = []
voids = []
for i in clusters.values():
voids += i
for i in voids:
points += graph[i]
for point in points:
x, y, z = point
thing[x][y][z] = 255
@mlab.animate
def anim():
f = mlab.gcf()
while 1:
f.scene.camera.azimuth(1)
f.scene.render()
yield
mlab.figure(bgcolor=(1,1,1)) # Set bkg color
mlab.contour3d(thing,
color = (0,0,0),
contours = 2,
opacity = .2 + .8/rock.shape[0]) # Draw pores for 3d, changed froo .20 * 100 / self.shape[0]
a = anim()
def generateCenters(clusters, graph):
centers = dict()
means = dict()
for i in clusters:
tx = 0
ty = 0
tz = 0
count = 0
for j in clusters[i]:
for (x, y, z) in graph[j]:
tx += x
ty += y
tz += z
count += 1
centers[i] = (tx/count, ty/count, tz/count)
for i in clusters:
std2 = 0
for j in clusters[i]:
for (x, y, z) in graph[j]:
std2 += (euclidean((x, y, z), centers[i]).astype(int))^2
means[i] = math.sqrt(std2)
return centers, means
def gaussian(x, mu, sig):
return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
def clusterh(centers, means, c):
def heuristic(point):
total = 0
for i in centers:
total += gaussian(euclidean(centers[i], point), 0, means[i])
total = total * c
return -total
return heuristic
def find_values():
#Replace the file name with the location of the picture
#When you generate the picture, crop out the velocity bar and make it a seperate picture.
a = Image.open(r"C:\MyWork\Summer\Kaushik2016\velocity bar.png")
b = Image.open(r"C:\MyWork\Summer\Kaushik2016\velocity picture.png")
bar = np.array(a)
pic = np.array(b)
print pic.shape
ret = np.zeros((pic.shape[0], pic.shape[1], 1))
for x in range(pic.shape[0]):
for y in range(pic.shape[1]):
(r, g, bl) = (pic[x][y][0], pic[x][y][1], pic[x][y][2])
for x2 in range(bar.shape[0]):
for y2 in range(bar.shape[1]):
if(abs(bar[x2][y2][0] - r) + abs(bar[x2][y2][1] - g) + abs(bar[x2][y2][2] - bl) < 5):
ret[x][y][0] = x2
break
return ret
def comparison(array, visited):
total = 0
for (x, y, z) in visited:
total += array[x][y][z]
total = total/len(visited)
return total
start = []
for i in range(292):
start = start + [(i, 0, 0)]
end = []
for i in range(292):
end = end + [(i, 382, 0)]
#Pass in the array from find_values, or take out the commented line below
def test(array):
#array = find_values()
rock = read_coloredslice(r"C:\MyWork\Summer\Kaushik2016\Picture\pic3.tif")
rock2 = crop(rock, array.shape)
p = []
v = []
best = 0
for i in range(50):
print 123456, i
for j in range(3):
print (j+1)/4.0
d, path, visited = rock_AStar(rock2, start, end, metric3(rock2, 4*(i+1)), sampleh((145, 0, 0), (145, 382, 0)), adj2)
c = comparison(array, visited)
if(c > best):
best = c
v = visited
p = path
return v
###########################################
##Code to make D-RNG.
###########################################
# def intersect(l1, l2):
# return set(l1).intersection(l2)
# #array is the image array, temp is a dictionary, x and y are positions, and i is the number of components already scanned.
# def get_component_array(array, list):
# if(list == []):
# return []
# temp = []
# for (x, y, z) in list:
# list.remove((x, y, z))
# if(array[x][y][z] == 0):
# array[x][y][z] = 255
# temp = temp + [(x, y, z)]
# if(x < array.shape[0] - 1):
# list = list + [(x+1, y, z)]
# if(x > 0):
# list = list + [(x-1, y, z)]
# if(y < array.shape[1] - 1):
# list = list + [(x, y+1, z)]
# if(y > 0):
# list = list + [(x, y-1, z)]
# if(z < array.shape[2] - 1):
# list = list + [(x, y, z+1)]
# if(z > 0):
# list = list + [(x, y, z-1)]
# return temp + get_component_array(array, list)
# def get_component(array, temp, x, y, z, i):
# list = [(x, y, z)]
# l = get_component_array(array, list)
# temp[i] = l
# i = i+1
# return (temp, i)
# def display_coordinates(points, lx, ly):
# out = numpy.empty((lx, ly))
# for (x, y) in points:
# if(x < lx and y < ly):
# out[x][y] = 255
# img = Image.fromarray(out)
# img.show()
# def display_graph(dic, lx, ly, number):
# out = numpy.empty((lx, ly))
# for i in range(number):
# for (x, y) in dic[i]:
# out[x][y] = 255
# img = Image.fromarray(out)
# img.show()
# return img
# def scan(array, temp, i):
# for x in range(array.shape[0]):
# for y in range(array.shape[1]):
# for z in range(array.shape[2]):
# if(array[x][y][z] == 0):
# (temp, i) = get_component(array, temp, x, y, z, i)
# return temp
# #scan_pictures takes a file name and creates a graph from the image
# def scan_picture(filename):
# ar = read_image(filename)
# out = scan(ar, dict(), 0)
# return out
# def create_lines(base, lx, ly):
# out = []
# for i in range(ly):
# out = out + [(base, i)]
# return out
# def intersection(graph, line):
# out = []
# for key in list(graph.keys()):
# if(len(intersect(line, graph[key])) != 0):
# out = out + [key]
# del graph[key]
# return out
# def vertex_intersection(graph, line):
# out = []
# for key in list(graph.keys()):
# if(len(intersect(line, graph[key])) != 0):
# out = out + [key]
# return out
# def flip(dic):
# ret = dict()
# for key in dic.keys():
# for elem in dic[key]:
# ret[elem] = key
# return ret
# def generate_edges(graph, lx, ly):
# assoc = dict()
# temp = graph
# display_coordinates(create_lines(200, lx, ly), lx, ly)
# for base in range(lx):
# crosses = intersection(temp, create_lines(base, lx, ly))
# if(len(crosses) != 0):
# for i in crosses:
# if(not i in assoc.keys()):
# assoc[i] = base
# return assoc
# def generate_dRNG(graph, lx, ly):
# output = dict()
# for key in graph.keys():
# output[key] = []
# temp = graph.copy()
# for key in graph.keys():
# holder = graph[key]
# del temp[key]
# for i in range(ly):
# holder2 = [(x, y+i) for (x, y) in holder]
# if(vertex_intersection(temp, holder2) != []):
# intersects = vertex_intersection(temp, holder2)
# for elem in intersects:
# output[key] += [elem]
# break
# return output
# def join(x1, y1, x2, y2, image):
# array = numpy.array(image)
# for t in range(10000):
# xt = x1*t/10000 + x2*(10000-t)/10000
# yt = y1*t/10000 + x2*(10000-t)/10000
# array[int(xt)][int(yt)] = 100
# return Image.fromarray(array)
# def draw_edges(edgeset, graph, image):
# for key in edgeset.keys():
# if(edgeset[key] != []):
# for elem in edgeset[key]:
# print("Joining: ", graph[key][0][0], graph[key][0][1], graph[elem][0][0], graph[elem][0][1])
# image = join(graph[key][0][0], graph[key][0][1], graph[elem][0][0], graph[elem][0][1], image)
# image.show()