forked from msgordon/IRMOS-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.py
executable file
·197 lines (165 loc) · 6.45 KB
/
rotate.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
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure,filter
#import ds9
import pyfits
from scipy.ndimage.interpolation import rotate
from scipy.stats import linregress
class Rotator:
def __cal__(self,*args):
return self
# Construct Rotator from data and gaussian sigma
def __init__(self,data,sigma=15,threshold=0.8,header=None):
self.data = data.astype(np.float)
self.sigma = sigma
self.threshold = threshold
self.edges = None
self.contours = None
self.slopes = None
self.slope = None
self.angle = None
self.rotated = data
self.header = header # Just in case
self.regions = None
def find_edges(self,sigma=None):
if sigma is not None:
self.sigma = sigma
print 'Identifying edges...'
self.edges = filter.canny(self.data,sigma=self.sigma)
return self.edges
def find_contours(self,threshold=None):
if threshold is not None:
self.threshold = threshold
if self.edges is None:
self.edges = self.find_edges()
print 'Finding contours...'
self.contours = measure.find_contours(self.edges,self.threshold,fully_connected='high')
return self.contours
def regression(self):
if self.contours is None:
self.contours = self.find_contours()
print 'Calculating regression line...'
self.slopes = []
self.intercepts = []
for contour in self.contours:
slope, intercept, r_value, p_value, std_err = linregress(contour[:,1],contour[:,0])
# Cut on contours
if (np.isfinite(slope) and slope != 0.0 and intercept < self.data.shape[0] and intercept > 0):
self.slopes.append(slope)
self.intercepts.append(intercept)
#self.slopes = np.array(self.slopes)
#self.slopes = self.slopes[np.isfinite(self.slopes)]
#self.slopes = self.slopes[np.where(self.slopes != 0.0)[0]]
self.slope = np.median(self.slopes)
return self.slope
def find_angle(self):
if self.slope is None:
self.slope = self.regression()
print 'Computing angle of correction...'
self.angle = np.arctan(self.slope)*180.0/np.pi
return self.angle
def rotate(self,angle=0,transpose=False):
print 'Rotating data by %f degrees...' % angle
self.rotated = rotate(self.data,angle)
if transpose:
self.rotated = self.rotated.transpose()
return self.rotated
def run(self,sigma=None,threshold=None,transpose=False,angle=False):
if sigma is not None:
self.sigma = sigma
if threshold is not None:
self.threshold = threshold
self.find_edges(sigma=self.sigma)
self.find_contours(threshold=self.threshold)
self.regression()
if angle is False:
angle = self.find_angle()
self.angle=angle
self.rotate(angle=angle,transpose=transpose)
return self.rotated
def display(self):#,plotDS9=False,plotPLT=False,plotAll=True,show=True):
'''
if (plotAll or plotDS9) and not plotPLT:
d = ds9.ds9()
d.set("frame 1")
print self.data.dtype
d.set_np2arr(self.data,dtype=float)
if self.edges is not None:
d.set("frame 2")
d.set_np2arr(self.edges,dtype=int)
if self.rotated is not None:
d.set("frame 3")
d.set_np2arr(self.rotated,dtype=float)
if (plotPLT or plotAll) and (not plotDS9) and (self.contours is not None):
for n,contour in enumerate(self.contours):
plt.plot(contour[:,1], contour[:,0])
if show:
plt.show()
#'''
plt.figure()
plt.imshow(self.data,origin='lower',cmap='gray_r')
plt.title('Original')
for n,contour in enumerate(self.contours):
plt.plot(contour[:,1], contour[:,0])
plt.figure()
plt.imshow(self.edges,origin='lower',cmap='gray_r')
plt.title('Edges')
plt.figure()
plt.imshow(self.rotated,origin='lower',cmap='gray_r')
plt.title('Rotated')
plt.show()
return
def targets(self):
targets = []
for i in xrange(0,len(self.contours)-1):
targets.append((self.contours[i][0,1],self.contours[i+1][0,1]))
return targets
def make_regions(self,filename,transpose=False):
header = '''# Region file format: DS9 version 4.1\n# Filename:\nglobal color=green dashlist=8 3 width=1 font="helvetica 10 normal" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\nphysical'''
'''
f = open(filename,'w')
print 'Writing regions to %s...' % filename
## Get two points on line (center x, y) (0,b)
for m,b in zip(self.slopes,self.intercepts):
#xc = data.shape[0]/2
#calculate y at center x
#yc = m*xc + b
xp = -b*np.sin(self.angle)
yp = b*np.cos(self.angle)
if transpose:
f.write('line(%f,%f,%f,%f)\n' % (
'''
'''
self.regions = filename
f = open(filename,'w')
print 'Writing regions to %s...' % filename
if transpose:
for x in self.intercepts:
f.write('vector(%f,%f,%f,%f)\n' % (x,0,self.data.shape[0],90))
else:
for y in self.intercepts:
f.write('vector(%f,%f,%f,%f)\n' % (0,y,self.data.shape[0],0))
f.close()
'''
self.regions = filename
f = open(filename,'w')
print 'Writing regions to %s...' % filename
length = self.data.shape[0]
angle = 90
for idx,contour in enumerate(self.contours):
f.write('vector(%f,%f,%f,%f) # text = "%i"\n' % (contour[0,1],0,length,angle,idx))
return
@staticmethod
def targets_from_region(filename):
f = open(filename,'r')
targets = []
for line in f:
line = line.split(',')
line[0] = line[0].split('(')
targets.append(line[0][1])
f.close()
targets = np.array(targets).astype(float).astype(int)
targets_t = []
for i in np.arange(1,len(targets),2):
targets_t.append((targets[i-1],targets[i]))
return targets_t