forked from mtpenny/gbtds_optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyieldMap.py
189 lines (137 loc) · 6.77 KB
/
yieldMap.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import copy
class yieldMap:
def __init__(self,filename,read_csv_kwargs={},
spike=[],units='per_tile'):
'''
Constructor that takes a filename and pandas read_csv
keyword arguments to build a yieldMap object. The file
should have at least three columns: l and b coordinates
and a yield, and these columns should be named "l", "b",
and "yield". The default arguments assume a 3-column file
with columns, l, b, and yield with no header line. The
map should be a regular grid with all values defined. It
can be modified with "spike" - a list of 3-tuples with
(l,b,yield) - which is useful for testing.
'''
self.filename = filename
acceptable=False
#Try to autodetect the separator if no sep keyword arg passed
if not 'sep' in read_csv_kwargs:
read_csv_kwargs['sep']=None
read_csv_kwargs['engine']='python'
#Read the map and account for different formats
try:
self.lbmap_orig = pd.read_csv(self.filename,
on_bad_lines='error',
dtype=float,**read_csv_kwargs)
#usecols=['l','b','yield','alphaTexp','alphaCadence'],
if {'l','b','yield','alphaTexp','alphaCadence'}.issubset(self.lbmap_orig.columns):
acceptable=True
except:
pass
if not acceptable:
try:
self.lbmap_orig = pd.read_csv(self.filename,
usecols=['l','b','yield'],
on_bad_lines='error',
dtype=float,**read_csv_kwargs)
if {'l','b','yield'}.issubset(self.lbmap_orig.columns):
acceptable=True
except:
pass
if not acceptable:
try:
self.lbmap_orig = pd.read_csv(self.filename,header=None,
names=['l','b','yield','alphaTexp',
'alphaCadence'],
dtype=float,
on_bad_lines='error',
**read_csv_kwargs)
if {'l','b','yield','alphaTexp','alphaCadence'}.issubset(self.lbmap_orig.columns):
acceptable=True
except:
pass
if not acceptable:
try:
self.lbmap_orig = pd.read_csv(self.filename,header=None,
names=['l','b','yield'],
dtype=float,
on_bad_lines='error',
**read_csv_kwargs)
if {'l','b','yield'}.issubset(self.lbmap_orig.columns):
acceptable=True
except:
raise RuntimeError('Error reading yieldMap file (%s). Try checking its formatting. It must have l,b, and yield columns, and can optionally have alphaTexp and alphaCadence columns too.' % (self.filename))
#print(self.lbmap_orig)
#print(self.lbmap_orig.shape)
if units=='per_deg2' or units=='per-deg2' or units=='per_deg' or units=='per-deg':
self.lpix = np.unique(self.lbmap_orig['l']) #Unique returns sorted list
self.bpix = np.unique(self.lbmap_orig['b'])
self.lspacing = np.average(self.lpix[1:]-self.lpix[:-1])
self.bspacing = np.average(self.bpix[1:]-self.bpix[:-1])
print("Assuming yield units are per deg2. Tile area is %g deg^2" % (self.lspacing*self.bspacing))
self.lbmap_orig['yield'] *= self.bspacing * self.lspacing
else:
print("Assuming yield units are per tile")
#Make a copy that will actually be used and modified
self.lbmap_working = copy.deepcopy(self.lbmap_orig)
self.processMap(spike)
def processMap(self,spike=[]):
'''
Process a map from a pandas dataframe. The map is
converted into an integer grid and conversion methods
are supplied as part of the class. Spike is a list
of 3-tuples that can be used to modify the map using its
integer coordinates - the main purpose is for testing.
'''
#sort the map before converting it
self.lbmap = self.lbmap_working.sort_values(['b','l'],
ignore_index=True)
#The map coordinates and values in 1d form
self.llist = self.lbmap['l']
self.blist = self.lbmap['b']
self.yieldlist = self.lbmap['yield']
#Assume complete, evenly spaced map & compute properties
self.lpix = np.unique(self.llist)
self.lspacing = np.average(self.lpix[1:]-self.lpix[:-1])
self.lorigin = self.lpix[0]
self.bpix = np.unique(self.blist)
self.bspacing = np.average(self.bpix[1:]-self.bpix[:-1])
self.borigin = self.bpix[0]
self.map_naxis = (self.bpix.shape[0],self.lpix.shape[0])
#The map coordiantes and values in 2d form
self.lmap = self.llist.to_numpy().reshape(self.map_naxis)
self.bmap = self.blist.to_numpy().reshape(self.map_naxis)
self.yieldmap = self.yieldlist.to_numpy().reshape(self.map_naxis)
self.covfac = np.zeros(self.yieldmap.shape)
#Apply a "spike" to modify the map
for sp in spike:
print("Adding spike",sp)
self.yieldmap[sp[1],sp[0]] = sp[2]
def plotMap(self,ax=None,pcolormesh_kwargs={}):
if ax is None:
ret = plt.pcolormesh(self.lmap,self.bmap,self.yieldmap,
shading='nearest',**pcolormesh_kwargs)
else:
ret = ax.pcolormesh(self.lmap,self.bmap,self.yieldmap,
shading='nearest',**pcolormesh_kwargs)
return ret
def l2x(self,l_vertices):
'''
Convert 2-d numpy array of l values to 2-d list of
map x pixel values
'''
return ((l_vertices.astype(float)-self.lorigin)/self.lspacing+0.5).tolist()
def b2y(self,b_vertices):
'''
Convert 2-d numpy array of l values to 2-d list of
map x pixel values
'''
return ((b_vertices.astype(float)-self.borigin)/self.bspacing+0.5).tolist()
def x2l(self,x):
return (np.array(x)-0.5)*self.lspacing+self.lorigin
def y2b(self,y):
return (np.array(y)-0.5)*self.bspacing+self.borigin