2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2011-12-22 16:00:25 +05:30
|
|
|
# $Id$
|
|
|
|
|
2015-05-20 02:41:49 +05:30
|
|
|
import os,sys
|
2014-08-07 14:21:25 +05:30
|
|
|
import numpy as np
|
2015-09-11 18:25:43 +05:30
|
|
|
import util
|
2014-08-07 14:21:25 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
class ASCIItable():
|
|
|
|
'''
|
|
|
|
There should be a doc string here :)
|
|
|
|
'''
|
2014-08-07 14:21:25 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
__slots__ = ['__IO__',
|
|
|
|
'info',
|
2015-08-08 00:33:26 +05:30
|
|
|
'labeled',
|
2011-12-14 01:32:26 +05:30
|
|
|
'data',
|
|
|
|
]
|
|
|
|
|
2015-08-12 23:12:38 +05:30
|
|
|
tmpext = '_tmp' # filename extension for in-place access
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def __init__(self,
|
2015-08-12 23:12:38 +05:30
|
|
|
name = None,
|
2015-08-08 00:33:26 +05:30
|
|
|
outname = None,
|
|
|
|
buffered = False, # flush writes
|
|
|
|
labeled = True, # assume table has labels
|
|
|
|
readonly = False, # no reading from file
|
|
|
|
):
|
|
|
|
self.__IO__ = {'output': [],
|
|
|
|
'buffered': buffered,
|
|
|
|
'labeled': labeled, # header contains labels
|
|
|
|
'labels': [], # labels according to file info
|
|
|
|
'readBuffer': [], # buffer to hold non-advancing reads
|
2012-01-20 02:07:53 +05:30
|
|
|
'dataStart': 0,
|
2011-12-14 01:32:26 +05:30
|
|
|
}
|
|
|
|
|
2015-08-12 23:12:38 +05:30
|
|
|
self.__IO__['inPlace'] = not outname and name and not readonly
|
2015-08-13 00:11:44 +05:30
|
|
|
if self.__IO__['inPlace']: outname = name + self.tmpext # transparently create tmp file
|
2015-09-17 01:14:11 +05:30
|
|
|
try:
|
|
|
|
self.__IO__['in'] = (open( name,'r') if os.access( name, os.R_OK) else None) if name else sys.stdin
|
|
|
|
except TypeError:
|
|
|
|
self.__IO__['in'] = name
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.__IO__['out'] = (open(outname,'w') if (not os.path.isfile(outname) \
|
|
|
|
or os.access(outname, os.W_OK)) \
|
|
|
|
and (not self.__IO__['inPlace'] \
|
|
|
|
or os.access(name, os.W_OK)) else None) if outname else sys.stdout
|
|
|
|
except TypeError:
|
|
|
|
self.__IO__['out'] = outname
|
2015-08-12 23:12:38 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
self.info = []
|
|
|
|
self.labels = []
|
|
|
|
self.data = []
|
2015-08-21 01:08:06 +05:30
|
|
|
self.line = ''
|
2012-02-02 22:43:51 +05:30
|
|
|
|
2015-08-12 23:12:38 +05:30
|
|
|
if self.__IO__['in'] == None \
|
|
|
|
or self.__IO__['out'] == None: raise IOError # complain if any required file access not possible
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def _transliterateToFloat(self,
|
|
|
|
x):
|
2012-02-02 22:43:51 +05:30
|
|
|
try:
|
|
|
|
return float(x)
|
|
|
|
except:
|
|
|
|
return 0.0
|
|
|
|
|
2015-05-20 02:05:56 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def croak(self,
|
|
|
|
what, newline = True):
|
|
|
|
|
|
|
|
sys.stderr.write(('\n'.join(map(str,what)) if not hasattr(what, "strip")
|
|
|
|
and hasattr(what, "__getitem__")
|
|
|
|
or hasattr(what, "__iter__") else str(what))
|
|
|
|
+('\n' if newline else '')),
|
|
|
|
|
2015-09-11 18:25:43 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def report_name(self, scriptName, fileName):
|
|
|
|
self.croak(util.emph(scriptName)+(': '+fileName if fileName else ''))
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def close(self,
|
|
|
|
dismiss = False):
|
2015-05-20 02:41:49 +05:30
|
|
|
self.input_close()
|
2015-08-08 00:33:26 +05:30
|
|
|
self.output_flush()
|
2015-05-20 02:41:49 +05:30
|
|
|
self.output_close(dismiss)
|
2015-05-20 02:05:56 +05:30
|
|
|
|
2014-08-22 21:07:46 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def input_close(self):
|
2015-06-19 11:38:09 +05:30
|
|
|
try:
|
2015-08-24 06:10:52 +05:30
|
|
|
if self.__IO__['in'] != sys.stdin: self.__IO__['in'].close()
|
2015-06-19 11:38:09 +05:30
|
|
|
except:
|
|
|
|
pass
|
2014-08-22 21:07:46 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def output_write(self,
|
|
|
|
what):
|
2013-12-12 08:06:05 +05:30
|
|
|
'''
|
|
|
|
aggregate a single row (string) or list of (possibly containing further lists of) rows into output
|
|
|
|
'''
|
|
|
|
if not isinstance(what, (str, unicode)):
|
|
|
|
try:
|
|
|
|
for item in what: self.output_write(item)
|
|
|
|
except:
|
|
|
|
self.__IO__['output'] += [str(what)]
|
2011-12-14 01:32:26 +05:30
|
|
|
else:
|
2013-12-12 08:06:05 +05:30
|
|
|
self.__IO__['output'] += [what]
|
|
|
|
|
|
|
|
return self.__IO__['buffered'] or self.output_flush()
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def output_flush(self,
|
|
|
|
clear = True):
|
2012-02-17 00:12:04 +05:30
|
|
|
try:
|
|
|
|
self.__IO__['output'] == [] or self.__IO__['out'].write('\n'.join(self.__IO__['output']) + '\n')
|
2013-09-14 16:22:02 +05:30
|
|
|
except(IOError) as e:
|
2012-02-17 00:12:04 +05:30
|
|
|
return False
|
2011-12-14 01:32:26 +05:30
|
|
|
if clear: self.output_clear()
|
2012-02-17 00:12:04 +05:30
|
|
|
return True
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def output_clear(self):
|
|
|
|
self.__IO__['output'] = []
|
|
|
|
|
2014-08-22 21:07:46 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def output_close(self,
|
|
|
|
dismiss = False):
|
2015-06-19 11:38:09 +05:30
|
|
|
try:
|
2015-08-24 06:10:52 +05:30
|
|
|
if self.__IO__['out'] != sys.stdout: self.__IO__['out'].close()
|
2015-06-19 11:38:09 +05:30
|
|
|
except:
|
|
|
|
pass
|
2015-06-06 16:48:39 +05:30
|
|
|
if dismiss and os.path.isfile(self.__IO__['out'].name): os.remove(self.__IO__['out'].name)
|
2015-08-13 00:11:44 +05:30
|
|
|
if self.__IO__['inPlace']: os.rename(self.__IO__['out'].name, self.__IO__['out'].name[:-len(self.tmpext)])
|
2014-08-22 21:07:46 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def head_read(self):
|
|
|
|
'''
|
2015-08-08 00:33:26 +05:30
|
|
|
get column labels by either reading
|
|
|
|
the first row or, if keyword "head[*]" is present,
|
|
|
|
the last line of the header
|
2011-12-14 01:32:26 +05:30
|
|
|
'''
|
2011-12-18 21:19:44 +05:30
|
|
|
import re
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
try:
|
|
|
|
self.__IO__['in'].seek(0)
|
|
|
|
except:
|
|
|
|
pass
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
firstline = self.__IO__['in'].readline()
|
2015-08-08 00:33:26 +05:30
|
|
|
m = re.search('(\d+)\s+head', firstline.lower()) # search for "head" keyword
|
|
|
|
if self.__IO__['labeled']: # table features labels
|
2013-06-30 05:51:51 +05:30
|
|
|
if m: # found header info
|
2015-08-08 00:33:26 +05:30
|
|
|
self.info = [self.__IO__['in'].readline().strip() for i in xrange(1,int(m.group(1)))]
|
|
|
|
self.labels = self.__IO__['in'].readline().split() # store labels found in last line
|
2013-06-30 05:51:51 +05:30
|
|
|
else: # no header info (but labels)
|
2015-08-08 00:33:26 +05:30
|
|
|
self.labels = firstline.split() # store labels from first line
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
self.__IO__['labels'] = list(self.labels) # backup labels (make COPY, not link)
|
2013-06-30 05:51:51 +05:30
|
|
|
|
|
|
|
else: # no labels present in table
|
|
|
|
if m: # found header info
|
2015-08-08 00:33:26 +05:30
|
|
|
self.info = [self.__IO__['in'].readline().strip() for i in xrange(0,int(m.group(1)))] # all header is info ...
|
2013-06-30 05:51:51 +05:30
|
|
|
# ... without any labels
|
2015-08-08 00:33:26 +05:30
|
|
|
else: # otherwise file starts with data right away
|
|
|
|
try:
|
|
|
|
self.__IO__['in'].seek(0) # try to rewind
|
|
|
|
except:
|
|
|
|
self.__IO__['readBuffer'] = firstline # or at least save data in buffer
|
2012-02-16 23:33:14 +05:30
|
|
|
try:
|
2013-06-30 05:51:51 +05:30
|
|
|
self.__IO__['dataStart'] = self.__IO__['in'].tell() # current file position is at start of data
|
2013-09-14 16:22:02 +05:30
|
|
|
except(IOError):
|
2012-02-16 23:33:14 +05:30
|
|
|
pass
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def head_write(self,
|
|
|
|
header = True):
|
2012-12-07 03:16:19 +05:30
|
|
|
'''
|
|
|
|
write current header information (info + labels)
|
|
|
|
'''
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
head = ['{}\theader'.format(len(self.info)+self.__IO__['labeled'])] if header else []
|
|
|
|
head.append(self.info)
|
|
|
|
if self.__IO__['labeled']: head.append('\t'.join(self.labels))
|
|
|
|
|
|
|
|
return self.output_write(head)
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def head_getGeom(self):
|
|
|
|
'''
|
|
|
|
interpret geom header
|
|
|
|
'''
|
|
|
|
identifiers = {
|
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
|
|
|
}
|
|
|
|
mappings = {
|
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'size': lambda x: float(x),
|
|
|
|
'origin': lambda x: float(x),
|
|
|
|
'homogenization': lambda x: int(x),
|
|
|
|
'microstructures': lambda x: int(x),
|
|
|
|
}
|
|
|
|
info = {
|
|
|
|
'grid': np.zeros(3,'i'),
|
|
|
|
'size': np.zeros(3,'d'),
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'homogenization': 0,
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
|
|
|
extra_header = []
|
|
|
|
|
|
|
|
for header in self.info:
|
|
|
|
headitems = map(str.lower,header.split())
|
|
|
|
if len(headitems) == 0: continue # skip blank lines
|
|
|
|
if headitems[0] in mappings.keys():
|
|
|
|
if headitems[0] in identifiers.keys():
|
|
|
|
for i in xrange(len(identifiers[headitems[0]])):
|
|
|
|
info[headitems[0]][i] = \
|
|
|
|
mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1])
|
|
|
|
else:
|
|
|
|
info[headitems[0]] = mappings[headitems[0]](headitems[1])
|
|
|
|
else:
|
|
|
|
extra_header.append(header)
|
|
|
|
|
|
|
|
return info,extra_header
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def labels_append(self,
|
2015-08-21 01:08:06 +05:30
|
|
|
what,
|
|
|
|
reset = False):
|
2012-12-07 03:16:19 +05:30
|
|
|
'''
|
2014-02-04 05:14:29 +05:30
|
|
|
add item or list to existing set of labels (and switch on labeling)
|
2012-12-07 03:16:19 +05:30
|
|
|
'''
|
2013-12-12 08:06:05 +05:30
|
|
|
if not isinstance(what, (str, unicode)):
|
|
|
|
try:
|
|
|
|
for item in what: self.labels_append(item)
|
|
|
|
except:
|
|
|
|
self.labels += [str(what)]
|
|
|
|
else:
|
|
|
|
self.labels += [what]
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
self.__IO__['labeled'] = True # switch on processing (in particular writing) of labels
|
2015-08-21 01:08:06 +05:30
|
|
|
if reset: self.__IO__['labels'] = list(self.labels) # subsequent data_read uses current labels as data size
|
2013-10-08 19:24:13 +05:30
|
|
|
|
2013-06-30 05:51:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def labels_clear(self):
|
2014-02-04 05:14:29 +05:30
|
|
|
'''
|
|
|
|
delete existing labels and switch to no labeling
|
|
|
|
'''
|
2013-06-30 05:51:51 +05:30
|
|
|
self.labels = []
|
2015-08-08 00:33:26 +05:30
|
|
|
self.__IO__['labeled'] = False
|
2013-06-30 05:51:51 +05:30
|
|
|
|
2012-12-07 03:16:19 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-06-05 17:14:17 +05:30
|
|
|
def label_index(self,
|
|
|
|
labels):
|
2012-12-07 03:16:19 +05:30
|
|
|
'''
|
2015-05-21 05:32:32 +05:30
|
|
|
tell index of column label(s).
|
|
|
|
return numpy array if asked for list of labels.
|
|
|
|
transparently deals with label positions implicitly given as numbers or their headings given as strings.
|
2012-12-07 03:16:19 +05:30
|
|
|
'''
|
2015-06-08 10:32:11 +05:30
|
|
|
from collections import Iterable
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if isinstance(labels, Iterable) and not isinstance(labels, str): # check whether list of labels is requested
|
2012-12-07 03:16:19 +05:30
|
|
|
idx = []
|
|
|
|
for label in labels:
|
2015-05-22 03:23:54 +05:30
|
|
|
if label != None:
|
2013-12-09 21:15:18 +05:30
|
|
|
try:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx.append(int(label)) # column given as integer number?
|
2013-12-09 21:15:18 +05:30
|
|
|
except ValueError:
|
2015-05-22 03:23:54 +05:30
|
|
|
try:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx.append(self.labels.index(label)) # locate string in label list
|
2015-05-22 03:23:54 +05:30
|
|
|
except ValueError:
|
2015-06-08 10:32:11 +05:30
|
|
|
try:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx.append(self.labels.index('1_'+label)) # locate '1_'+string in label list
|
2015-06-08 10:32:11 +05:30
|
|
|
except ValueError:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx.append(-1) # not found...
|
2012-12-07 03:16:19 +05:30
|
|
|
else:
|
|
|
|
try:
|
2015-05-21 05:32:32 +05:30
|
|
|
idx = int(labels)
|
|
|
|
except ValueError:
|
2013-12-09 21:15:18 +05:30
|
|
|
try:
|
|
|
|
idx = self.labels.index(labels)
|
2015-06-08 10:32:11 +05:30
|
|
|
except ValueError:
|
|
|
|
try:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx = self.labels.index('1_'+labels) # locate '1_'+string in label list
|
2015-06-08 10:32:11 +05:30
|
|
|
except ValueError:
|
|
|
|
idx = None if labels == None else -1
|
2012-12-07 03:16:19 +05:30
|
|
|
|
2015-05-21 05:32:32 +05:30
|
|
|
return np.array(idx) if isinstance(idx,list) else idx
|
2012-12-07 03:16:19 +05:30
|
|
|
|
2015-06-05 17:14:17 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-06-08 10:32:11 +05:30
|
|
|
def label_dimension(self,
|
|
|
|
labels):
|
2015-06-05 17:14:17 +05:30
|
|
|
'''
|
|
|
|
tell dimension (length) of column label(s).
|
|
|
|
return numpy array if asked for list of labels.
|
|
|
|
transparently deals with label positions implicitly given as numbers or their headings given as strings.
|
|
|
|
'''
|
2015-06-08 10:32:11 +05:30
|
|
|
|
|
|
|
from collections import Iterable
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if isinstance(labels, Iterable) and not isinstance(labels, str): # check whether list of labels is requested
|
2015-06-05 17:14:17 +05:30
|
|
|
dim = []
|
|
|
|
for label in labels:
|
|
|
|
if label != None:
|
2015-06-08 10:32:11 +05:30
|
|
|
myDim = -1
|
2015-08-08 00:33:26 +05:30
|
|
|
try: # column given as number?
|
2015-06-08 10:32:11 +05:30
|
|
|
idx = int(label)
|
2015-08-08 00:33:26 +05:30
|
|
|
myDim = 1 # if found has at least dimension 1
|
2015-09-09 04:42:45 +05:30
|
|
|
if self.labels[idx].startswith('1_'): # column has multidim indicator?
|
|
|
|
while idx+myDim < len(self.labels) and self.labels[idx+myDim].startswith("%i_"%(myDim+1)):
|
2015-08-08 00:33:26 +05:30
|
|
|
myDim += 1 # add while found
|
|
|
|
except ValueError: # column has string label
|
|
|
|
if label in self.labels: # can be directly found?
|
|
|
|
myDim = 1 # scalar by definition
|
|
|
|
elif '1_'+label in self.labels: # look for first entry of possible multidim object
|
|
|
|
idx = self.labels.index('1_'+label) # get starting column
|
|
|
|
myDim = 1 # (at least) one-dimensional
|
2015-09-09 04:42:45 +05:30
|
|
|
while idx+myDim < len(self.labels) and self.labels[idx+myDim].startswith("%i_"%(myDim+1)):
|
2015-08-08 00:33:26 +05:30
|
|
|
myDim += 1 # keep adding while going through object
|
2015-06-08 10:32:11 +05:30
|
|
|
|
|
|
|
dim.append(myDim)
|
2015-06-05 17:14:17 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
dim = -1 # assume invalid label
|
2015-06-08 10:32:11 +05:30
|
|
|
idx = -1
|
2015-08-08 00:33:26 +05:30
|
|
|
try: # column given as number?
|
2015-06-05 17:14:17 +05:30
|
|
|
idx = int(labels)
|
2015-08-08 00:33:26 +05:30
|
|
|
dim = 1 # if found has at least dimension 1
|
2015-09-09 04:42:45 +05:30
|
|
|
if self.labels[idx].startswith('1_'): # column has multidim indicator?
|
|
|
|
while idx+dim < len(self.labels) and self.labels[idx+dim].startswith("%i_"%(dim+1)):
|
2015-08-08 00:33:26 +05:30
|
|
|
dim += 1 # add as long as found
|
|
|
|
except ValueError: # column has string label
|
|
|
|
if labels in self.labels: # can be directly found?
|
|
|
|
dim = 1 # scalar by definition
|
|
|
|
elif '1_'+labels in self.labels: # look for first entry of possible multidim object
|
|
|
|
idx = self.labels.index('1_'+labels) # get starting column
|
|
|
|
dim = 1 # is (at least) one-dimensional
|
2015-09-09 04:42:45 +05:30
|
|
|
while idx+dim < len(self.labels) and self.labels[idx+dim].startswith("%i_"%(dim+1)):
|
2015-08-08 00:33:26 +05:30
|
|
|
dim += 1 # keep adding while going through object
|
2015-06-05 17:14:17 +05:30
|
|
|
|
2015-06-08 10:32:11 +05:30
|
|
|
return np.array(dim) if isinstance(dim,list) else dim
|
2015-06-05 17:14:17 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def label_indexrange(self,
|
|
|
|
labels):
|
|
|
|
'''
|
|
|
|
tell index range for given label(s).
|
|
|
|
return numpy array if asked for list of labels.
|
|
|
|
transparently deals with label positions implicitly given as numbers or their headings given as strings.
|
|
|
|
'''
|
|
|
|
|
|
|
|
from collections import Iterable
|
|
|
|
|
|
|
|
start = self.label_index(labels)
|
|
|
|
dim = self.label_dimension(labels)
|
|
|
|
|
|
|
|
return map(lambda a,b: xrange(a,a+b), zip(start,dim)) if isinstance(labels, Iterable) and not isinstance(labels, str) \
|
|
|
|
else xrange(start,start+dim)
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def info_append(self,
|
|
|
|
what):
|
2013-06-30 05:51:51 +05:30
|
|
|
'''
|
|
|
|
add item or list to existing set of infos
|
|
|
|
'''
|
2013-12-12 08:06:05 +05:30
|
|
|
if not isinstance(what, (str, unicode)):
|
|
|
|
try:
|
|
|
|
for item in what: self.info_append(item)
|
|
|
|
except:
|
|
|
|
self.info += [str(what)]
|
|
|
|
else:
|
|
|
|
self.info += [what]
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2012-02-23 19:24:38 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def info_clear(self):
|
2014-02-04 05:14:29 +05:30
|
|
|
'''
|
|
|
|
delete any info block
|
|
|
|
'''
|
2012-02-23 19:24:38 +05:30
|
|
|
self.info = []
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2012-01-20 02:07:53 +05:30
|
|
|
def data_rewind(self):
|
2015-08-08 00:33:26 +05:30
|
|
|
self.__IO__['in'].seek(self.__IO__['dataStart']) # position file to start of data section
|
|
|
|
self.__IO__['readBuffer'] = [] # delete any non-advancing data reads
|
|
|
|
self.labels = list(self.__IO__['labels']) # restore label info found in header (as COPY, not link)
|
|
|
|
self.__IO__['labeled'] = len(self.labels) > 0
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2012-04-18 17:12:57 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def data_skipLines(self,
|
|
|
|
count):
|
2014-02-04 05:14:29 +05:30
|
|
|
'''
|
|
|
|
wind forward by count number of lines
|
|
|
|
'''
|
|
|
|
for i in xrange(count):
|
|
|
|
alive = self.data_read()
|
|
|
|
|
|
|
|
return alive
|
2012-04-18 17:12:57 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def data_read(self,
|
|
|
|
advance = True):
|
2014-02-04 05:14:29 +05:30
|
|
|
'''
|
|
|
|
read next line (possibly buffered) and parse it into data array
|
|
|
|
'''
|
|
|
|
if len(self.__IO__['readBuffer']) > 0:
|
2015-08-21 01:08:06 +05:30
|
|
|
self.line = self.__IO__['readBuffer'].pop(0) # take buffered content
|
2014-02-04 05:14:29 +05:30
|
|
|
else:
|
2015-08-21 01:08:06 +05:30
|
|
|
self.line = self.__IO__['in'].readline() # get next data row from file
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2014-02-04 05:14:29 +05:30
|
|
|
if not advance:
|
2015-08-21 01:08:06 +05:30
|
|
|
self.__IO__['readBuffer'].append(self.line) # keep line just read in buffer
|
|
|
|
|
|
|
|
self.line = self.line.rstrip('\n')
|
2014-02-04 05:14:29 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if self.__IO__['labeled']: # if table has labels
|
2015-08-21 01:08:06 +05:30
|
|
|
items = self.line.split()[:len(self.__IO__['labels'])] # use up to label count (from original file info)
|
2015-08-08 00:33:26 +05:30
|
|
|
self.data = items if len(items) == len(self.__IO__['labels']) else [] # take entries if correct number, i.e. not too few compared to label count
|
2013-06-30 05:51:51 +05:30
|
|
|
else:
|
2015-08-21 01:08:06 +05:30
|
|
|
self.data = self.line.split() # otherwise take all
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
return self.data != []
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2013-12-14 09:21:22 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def data_readArray(self,
|
|
|
|
labels = []):
|
|
|
|
'''
|
|
|
|
read whole data of all (given) labels as numpy array
|
|
|
|
'''
|
2015-08-24 04:48:34 +05:30
|
|
|
from collections import Iterable
|
2013-12-14 09:21:22 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
try:
|
|
|
|
self.data_rewind() # try to wind back to start of data
|
|
|
|
except:
|
|
|
|
pass # assume/hope we are at data start already...
|
|
|
|
|
|
|
|
if labels == None or labels == []:
|
2015-07-09 09:10:15 +05:30
|
|
|
use = None # use all columns (and keep labels intact)
|
2015-05-21 05:32:32 +05:30
|
|
|
labels_missing = []
|
2015-05-14 22:37:50 +05:30
|
|
|
else:
|
2015-08-24 04:48:34 +05:30
|
|
|
if isinstance(labels, str) or not isinstance(labels, Iterable): # check whether labels are a list or single item
|
|
|
|
labels = [labels]
|
2015-08-08 00:33:26 +05:30
|
|
|
indices = self.label_index(labels) # check requested labels ...
|
|
|
|
dimensions = self.label_dimension(labels) # ... and remember their dimension
|
2015-05-21 05:32:32 +05:30
|
|
|
present = np.where(indices >= 0)[0] # positions in request list of labels that are present ...
|
|
|
|
missing = np.where(indices < 0)[0] # ... and missing in table
|
2015-06-13 17:13:34 +05:30
|
|
|
labels_missing = np.array(labels)[missing] # labels of missing data
|
|
|
|
|
|
|
|
columns = []
|
2015-08-08 00:33:26 +05:30
|
|
|
for i,(c,d) in enumerate(zip(indices[present],dimensions[present])): # for all valid labels ...
|
2015-07-22 02:14:40 +05:30
|
|
|
columns += range(c,c + \
|
2015-08-08 00:33:26 +05:30
|
|
|
(d if str(c) != str(labels[present[i]]) else \
|
|
|
|
1)) # ... transparently add all components unless column referenced by number or with explicit dimension
|
2015-06-13 17:13:34 +05:30
|
|
|
use = np.array(columns)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
self.labels = list(np.array(self.labels)[use]) # update labels with valid subset
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2015-05-21 05:32:32 +05:30
|
|
|
self.data = np.loadtxt(self.__IO__['in'], usecols=use,ndmin=2)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-05-21 05:32:32 +05:30
|
|
|
return labels_missing
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
def data_write(self,
|
|
|
|
delimiter = '\t'):
|
2014-02-04 05:14:29 +05:30
|
|
|
'''
|
|
|
|
write current data array and report alive output back
|
|
|
|
'''
|
|
|
|
if len(self.data) == 0: return True
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2011-12-14 01:32:26 +05:30
|
|
|
if isinstance(self.data[0],list):
|
2013-12-17 13:46:29 +05:30
|
|
|
return self.output_write([delimiter.join(map(str,items)) for items in self.data])
|
2011-12-14 01:32:26 +05:30
|
|
|
else:
|
2013-12-17 13:46:29 +05:30
|
|
|
return self.output_write(delimiter.join(map(str,self.data)))
|
2011-12-14 01:32:26 +05:30
|
|
|
|
2013-06-30 05:51:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-08-21 01:08:06 +05:30
|
|
|
def data_writeArray(self, fmt = None, delimiter = '\t'):
|
2013-06-30 05:51:51 +05:30
|
|
|
'''
|
|
|
|
write whole numpy array data
|
|
|
|
'''
|
2015-08-21 01:08:06 +05:30
|
|
|
for row in self.data:
|
|
|
|
self.__IO__['out'].write(delimiter.join([fmt % value for value in row] if fmt else map(repr,row)) + '\n')
|
2013-06-30 05:51:51 +05:30
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
2011-12-14 01:32:26 +05:30
|
|
|
def data_append(self,
|
|
|
|
what):
|
2013-12-12 08:06:05 +05:30
|
|
|
if not isinstance(what, (str, unicode)):
|
|
|
|
try:
|
|
|
|
for item in what: self.data_append(item)
|
|
|
|
except:
|
|
|
|
self.data += [str(what)]
|
2013-12-09 21:15:18 +05:30
|
|
|
else:
|
2013-12-12 08:06:05 +05:30
|
|
|
self.data += [what]
|
2012-02-02 22:43:51 +05:30
|
|
|
|
2012-02-15 20:20:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def data_set(self,
|
2015-08-08 00:33:26 +05:30
|
|
|
what, where):
|
|
|
|
'''
|
|
|
|
update data entry in column "where". grows data array if needed.
|
|
|
|
'''
|
2012-02-15 20:20:51 +05:30
|
|
|
idx = -1
|
|
|
|
try:
|
2015-08-08 00:33:26 +05:30
|
|
|
idx = self.label_index(where)
|
2012-02-15 20:20:51 +05:30
|
|
|
if len(self.data) <= idx:
|
|
|
|
self.data_append(['n/a' for i in xrange(idx+1-len(self.data))]) # grow data if too short
|
|
|
|
self.data[idx] = str(what)
|
2013-09-14 16:22:02 +05:30
|
|
|
except(ValueError):
|
2012-02-15 20:20:51 +05:30
|
|
|
pass
|
|
|
|
|
|
|
|
return idx
|
2015-07-22 02:14:40 +05:30
|
|
|
|
2012-02-15 20:20:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def data_clear(self):
|
|
|
|
self.data = []
|
|
|
|
|
2012-02-02 22:43:51 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def data_asFloat(self):
|
|
|
|
return map(self._transliterateToFloat,self.data)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def microstructure_read(self,
|
|
|
|
grid):
|
|
|
|
'''
|
|
|
|
read microstructure data (from .geom format)
|
|
|
|
'''
|
|
|
|
|
|
|
|
N = grid.prod() # expected number of microstructure indices in data
|
|
|
|
microstructure = np.zeros(N,'i') # initialize as flat array
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < N and self.data_read():
|
|
|
|
items = self.data
|
|
|
|
if len(items) > 2:
|
|
|
|
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
|
|
|
|
elif items[1].lower() == 'to': items = range(int(items[0]),1+int(items[2]))
|
|
|
|
else: items = map(int,items)
|
|
|
|
else: items = map(int,items)
|
|
|
|
|
|
|
|
s = min(len(items), N-i) # prevent overflow of microstructure array
|
|
|
|
microstructure[i:i+s] = items[:s]
|
|
|
|
i += s
|
|
|
|
|
2015-09-11 18:25:43 +05:30
|
|
|
return microstructure
|