-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.py
139 lines (101 loc) · 3.67 KB
/
lib.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
import numpy as np
import os
import argparse
from pathlib import Path
import json
from fanc import rootID_lookup as IDlook
# function - path
def validate_file(f):
if not os.path.exists(f):
# Argparse uses the ArgumentTypeError to give a rejection message like:
# error: argument input: x does not exist
raise argparse.ArgumentTypeError("{0} does not exist".format(f))
return f
def get_cv_path(version=None):
fname = Path.home() / '.cloudvolume' / 'segmentations.json'
with open(fname) as f:
paths = json.load(f)
if version is None:
return(paths)
else:
return(paths[version])
# function - cloud-volume
def mip0_to_mip2(x,y,z):
xyz_mip2 = np.array([(x/(2**2)),(y/(2**2)), z])
xyz_mip2 = xyz_mip2.astype('int64')
return xyz_mip2[0], xyz_mip2[1], xyz_mip2[2]
def mip0_to_mip2_array(array):
X, Y, Z = mip0_to_mip2(array[0], array[1], array[2])
result = np.array([X, Y, Z])
return result
def mip0_to_mip4(x,y,z):
xyz_mip4 = np.array([(x/(2**4)),(y/(2**4)), z])
xyz_mip4 = xyz_mip4.astype('int64')
return xyz_mip4[0], xyz_mip4[1], xyz_mip4[2]
def mip0_to_mip4_array(array):
X, Y, Z = mip0_to_mip4(array[0], array[1], array[2])
result = np.array([X, Y, Z])
return result
def mip2_to_mip0(x,y,z, img):
origin = img.bounds.minpt
xyz_mip2 = np.add(np.array([x,y,z]), origin)
xyz_mip0 = np.array([(xyz_mip2[0] * 2**2),(xyz_mip2[1] * 2**2), xyz_mip2[2]])
xyz_mip0 = xyz_mip0.astype('int64')
return xyz_mip0[0], xyz_mip0[1], xyz_mip0[2]
def mip2_to_mip0_array(array, img):
X, Y, Z = mip2_to_mip0(array[0], array[1], array[2], img)
result = np.array([X, Y, Z])
return result
def mip4_to_mip0(x,y,z, img):
origin = img.bounds.minpt
xyz_mip4 = np.add(np.array([x,y,z]), origin)
xyz_mip0 = np.array([(xyz_mip4[0] * 2**4),(xyz_mip4[1] * 2**4), xyz_mip4[2]])
xyz_mip0 = xyz_mip0.astype('int64')
return xyz_mip0[0], xyz_mip0[1], xyz_mip0[2]
def mip4_to_mip0_array(array, img):
X, Y, Z = mip4_to_mip0(array[0], array[1], array[2], img)
result = np.array([X, Y, Z])
return result
def Bbox2cloud(bbox):
cloud = [bbox.minpt.x, bbox.minpt.y, bbox.minpt.z, bbox.maxpt.x, bbox.maxpt.y, bbox.maxpt.z]
return cloud
# function - seg/svID
def find_most_frequent_ID(array):
uniqueID, count = np.unique(array, return_counts=True)
unsorted_max_indices = np.argsort(-count)
topIDs1 = uniqueID[unsorted_max_indices]
topIDs2 = topIDs1[~(topIDs1 == 0)] # no zero
if topIDs2.size == 0:
topID = np.zeros(1, dtype = 'int64') # empty then zero
else:
topID = topIDs2.astype('int64')[0]
return topID
def segID_to_svID(segID, ID_array, location_array_mip0, cv, reverse=False):
indices = np.where(ID_array == segID)[0]
pts = location_array_mip0[indices]
if reverse == True:
pts = pts[::-1]
for j in range(len(pts)):
ptsj = pts[j]
svID = IDlook.segIDs_from_pts_service(ptsj, return_roots=False)
if svID is None:
svID = [0]
if (svID[0] > 0) & (segID != 0):
break
# else: # reverse == True
# for j in reversed(range(len(pts))):
# ptsj = pts[j]
# svID = IDlook.segIDs_from_pts_service(ptsj, return_roots=False)
# if svID is None:
# svID = [0]
# if (svID[0] > 0) & (segID != 0):
# break
# if reverse == False:
# svID = IDlook.segIDs_from_pts_cv(pts=pts, cv=cv, return_roots=False, progress=False)
# ptsj = pts[0]
# else:
# pts = pts[::-1]
# svID = IDlook.segIDs_from_pts_cv(pts=pts, cv=cv, return_roots=False, progress=False)
# for j in range(len(pts)):
# ptsj = pts[0]
return svID[0],ptsj