-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube_to_cloudplot.py
213 lines (174 loc) · 6.43 KB
/
cube_to_cloudplot.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
#!/usr/bin/env python
'''
Take a number of cubes and make them into a cloud plot.
Note to self. You are suppose to run this in the directory
with the files (TODO: fix this later)
TODO: write in a function to recognize when filenames are
too long for matplotlib to handle.
'''
import itertools
import os.path
import numpy as np
import pandas as pd
from pysis import isis
from glob import iglob
from pysis import CubeFile
from os.path import basename
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
x1= 320
x2= 415
y1= 950
y2= 750
x = x1/x2
y = y1/y2
# Colors will be applyed to filters by filter in alphabetical ordear
colors = [
'red',
'blue',
'green',
'purple',
'cyan',
'magenta',
'black',
'darkorange',
'burlywood',
'lightgreen'
]
colorloop=itertools.cycle(colors) # using intertools!
def make_cloud_plot(image_list, color, groupname):
'''
Just pass one of the image_list
'''
for img_name in image_list:
roi_name = os.path.basename(img_name)[:-8]
base_path = img_name[:-8]
image1 = CubeFile.open(img_name) # pysis.cubefile.CubeFile
wac_320 = image1.apply_numpy_specials()[0].T
wac_360 = image1.apply_numpy_specials()[1].T
wac_415 = image1.apply_numpy_specials()[2].T
wac_566 = image1.apply_numpy_specials()[3].T
image2 = CubeFile.open(base_path+'_clm.cub') # pysis.cubefile.CubeFile
clm_750 = image2.apply_numpy_specials()[1].T
clm_950 = image2.apply_numpy_specials()[3].T
clm_415 = image2.apply_numpy_specials()[0].T
xaxis = wac_320/wac_415
yaxis = clm_950/clm_750
plt.scatter(xaxis, yaxis, marker='.', label=(roi_name), c=color, edgecolor=color)
#coloriter = colorloop.next()
#plt.scatter(xaxis, yaxis, marker='.', label=(roi_name), c=coloriter, edgecolor=coloriter)
def forceAspect(ax,aspect=1):
'''
From Stackoverflow
'''
im = ax.get_images()
extent = im[0].get_extent()
ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)
def make_cross_plot(wac_df, clm_df):
'''
x = 320/415
y = 950/750
'''
for index_name in wac_df.index:
roi_name = index_name[:-4]
x = wac_df.loc[index_name].values
y = clm_df.loc[roi_name+'_clm'].values
x_data = np.ma.masked_array(x[0],np.isnan(x[0]))
y_data = np.ma.masked_array(y[0],np.isnan(y[0]))
print roi_name, np.mean(x_data), np.mean(y_data), np.std(x_data), np.std(y_data)
plt.errorbar(np.mean(x_data), np.mean(y_data), xerr=np.std(x_data),
yerr=np.std(y_data), marker='o', label=(roi_name),
c=colorloop.next())
rois_rough = pd.read_csv('/home/sbraden/imps_ratio_rough.csv', index_col=0)
rois_mare = pd.read_csv('/home/sbraden/imps_ratio_mare.csv', index_col=0)
for roi_name in rois_rough.index:
ratio = rois_rough.loc[roi_name].values
plt.scatter(ratio[1], ratio[0], marker='s', c='blue')
for roi_name in rois_mare.index:
ratio = rois_mare.loc[roi_name].values
plt.scatter(ratio[1], ratio[0], marker='s', c='red')
fontP = FontProperties()
fontP.set_size('small')
plt.legend(loc='lower right', prop=fontP, numpoints=1)
plt.xlabel('320/415 nm WAC ratio', fontsize=14)
plt.ylabel('950/750 nm CLEM ratio', fontsize=14)
plt.savefig('lunar_roi_cross_plot.png', dpi=300)
plt.close()
def clem_or_wac(img_name):
'''
Reads in image, reads extension,
Python arrays are y, x instead of isis x, y => take the transpose of the
image data array
totally rewrite this so that the function only does 1 thing,
not two. Ideally the function should only differentiate between
wac and clm files, not do math.
'''
image = CubeFile.open(source_dir+img_name) # pysis.cubefile.CubeFile
if img_name[-7:-4] == 'wac':
wac_320 = image.apply_numpy_specials()[0].T
print wac_320
wac_415 = image.apply_numpy_specials()[2].T
# tell pandas to handle the very negative numbers
wac_320_over_415 = wac_320/wac_415
#print wac_320_over_415
return img_name[-7:-4], wac_320_over_415
#return img_name[-7:-4], wac_415
elif img_name[-7:-4] == 'clm':
clm_950 = image.apply_numpy_specials()[3].T
clm_750 = image.apply_numpy_specials()[1].T
clm_950_over_750 = clm_950/clm_750
print clm_950
# tell pandas to handle the very negative numbers
return img_name[-7:-4], clm_950_over_750
#return img_name[-7:-4], clm_750
else:
print 'This is not a input cube that works with this script.'
#applies scaling and Transposes
def get_banddata(image_list):
'''
Takes in a list of images
Performs math on selected bands
Returns data in a pandas dataframe
'''
index = []
data = []
for img_name in image_list:
print img_name
camera, ratio_array = clem_or_wac(img_name)
index.append(basename(img_name[:-4]))
data.append(ratio_array)
print data
print 'applied scaling and transposing'
return pd.DataFrame(data, index=index)
def main():
# Write a part to put image directories into "groups"
source_dirs = [
'/home/sbraden/400mpp_15x15_clm_wac/mare/',
'/home/sbraden/400mpp_15x15_clm_wac/pyro/',
'/home/sbraden/400mpp_15x15_clm_wac/imps/',
'/home/sbraden/400mpp_15x15_clm_wac/mare_immature/'
]
for directory in source_dirs:
print directory
groupname = os.path.split(os.path.dirname(directory))[1]
print groupname
# read in LROC WAC images
wac_img_list = iglob(directory+'*_wac.cub')
# read in Clementine images
clm_img_list = iglob(directory+'*_clm.cub')
make_cloud_plot(wac_img_list, colorloop.next(), groupname)
fontP = FontProperties()
fontP.set_size('small')
#plt.legend(loc='upper left', fancybox=True, prop=fontP, scatterpoints=1)
#plt.axis([0.70, 0.86, 0.90, 1.15],fontsize=14)
plt.axis([0.60, 0.90, 0.90, 1.20],fontsize=14)
plt.axes().set_aspect('equal')
# THIS next line does not get called:
plt.margins(0.20) # 4% add "padding" to the data limits before they're autoscaled
plt.xlabel('WAC 320/415 nm', fontsize=14)
plt.ylabel('CLM 950/750 nm', fontsize=14)
plt.savefig('lunar_roi_cloud_plot.png', dpi=300)
plt.close()
#make_cross_plot(wac_df, clm_df)
if __name__ == '__main__':
main()