Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

# -*- coding: ascii -*- 

 

r""" 

A wrapper for numpy arrays providing named axes, interpolation, iteration, disk persistence and numerical calcs 

 

 

M_Pool wraps multidimensional numpy arrays to provide the following features:: 

 

#. MatrixPool objects contain related Axis and Matrix objects 

- MP = MatrixPool(name='N2O4_MMH') 

 

#. Axis objects are added by name and interpolation transform (used to linearize interpolation) 

- epsAxis = Axis({'name':'eps', 'valueL':[10., 20., 30., 40., 50.], 'units':'', 'transform':'log10'}) 

- pcAxis = Axis({'name':'pc', 'valueL':[100.,200.,300,400], 'units':'psia', 'transform':'log10'}) 

- mrAxis = Axis({'name':'mr', 'valueL':[1,2,3], 'units':'', 'transform':''}) 

 

#. Matrix objects added by name  

- M = MP.add_matrix( name='cea_isp', units='sec', axisNameL=['eps','pc','mr'] ) 

 

#. Find interpolated minimum or maximum 

- interpD, max_val = M.solve_interp_max( order=3, method='TNC', tol=1.0E-8) 

- where interpD and max_val look something like: 

- interpD = {'pc': 225.00641803120988, 'eps': 34.991495018803455, 'mr': 1.7499612975876655} 

- max_val = -0.000155216246295 

 

#. Disk-based persistence 

- Save to pickle or hdf5 file 

- MP.save_to_pickle() # saves MP to "N2O4_MMH_matrix.pool" 

 

#. Built-in statistics (standard deviation, median, mean/average, sum, minimum, maximum 

- M.get_range() 

- M.get_ave() 

- M.get_mean() 

- M.get_std() 

- M.get_median() 

 

#. Interpolation on axes with named values 

- interp_val = M.interp(order=2, pc=100, eps=20, mr=2.0) 

- Uses transformed axes to help linearize interpolation 

 

#. Iterate over matrix 

- for indeces,D,val in M.full_iter_items(): 

- gives something like: 

- (0, 0, 0) {'pc': 100.0, 'eps': 10.0, 'mr': 1.0} 111.0 

- (0, 0, 1) {'pc': 100.0, 'eps': 10.0, 'mr': 2.0} 112.0 

- (0, 0, 2) {'pc': 100.0, 'eps': 10.0, 'mr': 3.0} 113.0 

- ... 

 

 

 

M_Pool 

Copyright (C) 2015 Charlie Taylor 

 

This program is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

This program is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

----------------------- 

 

""" 

import os 

here = os.path.abspath(os.path.dirname(__file__)) 

 

 

# for multi-file projects see LICENSE file for authorship info 

# for single file projects, insert following information 

__author__ = 'Charlie Taylor' 

__copyright__ = 'Copyright (c) 2015 Charlie Taylor' 

__license__ = 'GPL-3' 

exec( open(os.path.join( here,'_version.py' )).read() ) # creates local __version__ variable 

__email__ = "cet@appliedpython.com" 

__status__ = "3 - Alpha" # "3 - Alpha", "4 - Beta", "5 - Production/Stable" 

 

import pickle as pickle 

import os 

import numpy as np 

try: 

import tables 

except: 

print('... WARNING... No HDF5 Support Available (failed import tables)') 

from m_pool.axis_obj import Axis 

from m_pool.axis_pool import AxisPool 

from m_pool.matrix_obj import Matrix 

 

class MatrixPool(object): 

""" 

A wrapper for numpy arrays providing named axes, interpolation, iteration, disk persistence and numerical calcs 

 

A MatrixPool object is a Collection of Axis and Matrix objects.  

Like AxisPool, it is used to define a collection of Matrix objects 

 

*** Structured to easily pickle via a dictionary of named values for properties. *** 

""" 

 

def __init__(self, name='Pool Data', descD=None): 

 

self.name = name 

self.descD = descD # descriptive data in dictionary format 

 

self.axisPoolObj = AxisPool() 

 

self.matrixL = [] # list of Matrix objects 

self.matrixD = {} # cross ref by name 

 

def add_axis(self, axOrD): 

self.axisPoolObj.add_axis( axOrD ) 

 

def rename_matrix(self, old_name, new_name ): 

M = self.matrixD[old_name] 

M.name = new_name 

del self.matrixD[old_name] 

self.matrixD[new_name] = M 

 

def add_matrix(self, name='matrixName', units='', axisNameL=None, 

matValArr=None): 

 

D={'name':name, 'units':units, 'matValArr':matValArr, 

'axisNameL':axisNameL, 'axisPoolObj':self.axisPoolObj} 

self.matrixL.append( Matrix(D) ) 

 

self.matrixD[name] = self.matrixL[-1] 

 

return self.matrixL[-1] 

 

def get_matrix_by_name(self, matName): 

return self.matrixD.get(matName,None) 

 

def get_axis_by_name(self, axName): 

return self.axisPoolObj[axName] 

 

def __str__(self): 

sL = ['MatrixPool: %s'%self.name ] 

for M in self.matrixL: 

sL.append( ' Matrix:%s, shape=%s, units=%s, %%full=%i'%(M.name, M.shape(), M.units, M.iPercentFull()) ) 

return '\n'.join( sL ) 

 

def summ(self): 

sL = ['MatrixPool: %s'%self.name ] 

if self.descD is not None: 

keyL = sorted( list(self.descD.keys()), key=str.lower ) 

m = max( [len(str(k)) for k in keyL] ) 

fmt = '%' + '%is'%m 

for key in keyL: 

sL.append( ' ' + fmt%str(key) + ' : ' + str(self.descD[key]) ) 

 

for M in self.matrixL: 

s = M.short_summ() 

ssL = s.split('\n') 

for s in ssL: 

sL.append( ' ' + s ) 

return '\n'.join( sL ) 

 

 

def __len__(self): 

return len( self.matrixL ) 

 

def __iter__(self): 

for value in self.matrixL: 

yield value 

 

def __getitem__(self, i): # retrieve as: A[i] 

return self.matrixL[i] 

 

def save_to_hdf5(self, fname=None): 

if fname==None: 

fname = '%s_mpool.h5'%(self.name) 

 

h5file = tables.open_file(fname, mode='w') 

# Get the HDF5 root group 

root = h5file.root 

 

h5file.create_array(root, 'axes_name_list', [A.name for A in self.axisPoolObj], "String array") 

h5file.create_array(root, 'matrix_name_list', [M.name for M in self.matrixL], "String array") 

axes_group = h5file.create_group(root, 'axes', 'All the Axes used in Matrix Pool') 

mat_group = h5file.create_group(root, 'matrices', 'All the Matrices used in Matrix Pool') 

 

''' {'name':self.name, 'valueL':self.valueL, 'units':self.units,  

'transform':self.transform, 'roundDigits':self.roundDigits}''' 

for A in self.axisPoolObj: 

d = h5file.create_array(axes_group, A.name, A.valueArr) 

h5file.create_array(axes_group, 'transform_%s'%A.name, A.transArr) 

h5file.create_array(axes_group, 'desc_%s'%A.name, 

[str(A.units), str(A.transform), str(A.roundDigits)], "String array") 

#d.transform_desc = A.transform 

#d.roundDigits_desc = A.roundDigits 

#d.units_desc = A.units 

 

# Just use last axis "A" for atom creation 

atom = tables.Atom.from_dtype(A.valueArr.dtype) 

filters = tables.Filters(complib='blosc', complevel=5) 

 

for M in self.matrixL: 

ds = h5file.create_carray(mat_group, M.name, atom, M.matValArr.shape, filters=filters) 

ds.attrs.units = M.units 

ds[:] = M.matValArr 

 

h5file.create_array(mat_group, M.name+'_axis_list', [aname for aname in M.axisNameL], "String array") 

 

h5file.flush() 

h5file.close() 

 

def read_from_hdf5(self, fname=None): 

if fname==None: 

fname = '%s_mpool.h5'%(self.name) 

print('Reading:', fname) 

 

if os.path.exists(fname): 

h5file = tables.open_file(fname, mode='r') 

 

# reinit self 

self.axisPoolObj = AxisPool() 

self.matrixL = [] # list of Matrix objects 

self.matrixD = {} # cross ref by name 

self.descD = None 

 

root = h5file.root 

 

# First get the axes  

axis_nameL = [_ for _ in root.axes_name_list] 

print('axis_nameL =',axis_nameL) 

 

for aname in axis_nameL: 

val_arr = getattr( root.axes, aname ).read() 

dname = 'desc_' + aname 

desc = getattr( root.axes, dname ).read() 

units, trans, alen = desc 

 

A = Axis({'name':aname, 'valueL':val_arr, 'units':units, 'transform':trans}) 

self.add_axis( A ) 

 

# Then get the matrices 

matrix_nameL = [_ for _ in root.matrix_name_list] 

print('matrix_nameL =',matrix_nameL) 

 

for mname in matrix_nameL: 

m = getattr( root.matrices, mname ).read() 

units = getattr( root.matrices, mname ).attrs.units 

a_list = getattr( root.matrices, mname+'_axis_list' ).read() 

self.add_matrix( name=mname, units=units, axisNameL=list(a_list), matValArr=m ) 

 

h5file.close() 

 

def save_to_pickle(self, fname=None): 

if fname==None: 

fname = '%s_matrix.pool'%(self.name) 

D = {} 

D['axisL'] = [A.get_pickleable_dict() for A in self.axisPoolObj] 

D['matrixL'] = [M.get_pickleable_dict() for M in self.matrixL] 

D['descD'] = self.descD 

 

fOut = open(fname, 'wb') 

pickle.dump( D, fOut ) 

fOut.close() 

 

def read_from_pickle(self, fname=None): 

if fname==None: 

fname = '%s_matrix.pool'%(self.name) 

print('Reading:', fname) 

 

if os.path.exists(fname): 

 

fInp = open(fname, 'rb') 

D = pickle.load( fInp ) 

fInp.close() 

 

# reinit self 

self.axisPoolObj = AxisPool() 

self.matrixL = [] # list of Matrix objects 

self.matrixD = {} # cross ref by name 

 

self.descD = D.get('descD',{}) # if descriptive dictionary was saved, restore it 

 

for AD in D['axisL']: 

#print 'Adding',AD 

self.add_axis( AD ) 

#print self.axisPoolObj 

#print 

#print self 

 

for MD in D['matrixL']: 

#print 'add Matrix',MD 

self.add_matrix( **MD ) 

else: 

print('...WARNING... could not find:',fname) 

 

if __name__=="__main__": 

 

MP = MatrixPool(name='N2O4_MMH') 

epsAxis = Axis({'name':'eps', 'valueL':[10., 20., 30., 40., 50.], 'units':'', 'transform':'log10'}) 

pcAxis = Axis({'name':'pc', 'valueL':[100.,200.,300,400], 'units':'psia', 'transform':'log10'}) 

mrAxis = Axis({'name':'mr', 'valueL':[1,2,3], 'units':'', 'transform':''}) 

for A in [epsAxis, pcAxis, mrAxis]: 

MP.add_axis( A ) 

 

M = MP.add_matrix( name='cea_isp', units='sec', axisNameL=['eps','pc','mr'] ) 

for eps in epsAxis: 

for pc in pcAxis: 

for mr in mrAxis: 

M.setByName( pc=pc, eps=eps, mr=mr, val=eps+pc+mr ) 

 

 

M = MP.add_matrix( name='SEA_isp', units='sec', axisNameL=['eps','pc','mr'] ) 

for eps in epsAxis: 

for pc in pcAxis: 

for mr in mrAxis: 

M.setByName( pc=pc, eps=eps, mr=mr, val=eps+pc+mr+0.321 ) 

 

 

print(MP) 

#MP.save_to_pickle() 

#MP.save_to_hdf5() 

print('='*55) 

print(MP.summ())