use np.histogram2d, fixed list.append bug when using weight column

This commit is contained in:
chen 2016-09-09 16:17:00 -04:00
parent 8c4767d58c
commit ee322be870
1 changed files with 14 additions and 14 deletions

View File

@ -20,7 +20,7 @@ Produces a binned grid of two columns from an ASCIItable, i.e. a two-dimensional
parser.add_option('-d','--data', parser.add_option('-d','--data',
dest = 'data', dest = 'data',
type='string', nargs = 2, metavar = 'string string', type = 'string', nargs = 2, metavar = 'string string',
help = 'column labels containing x and y ') help = 'column labels containing x and y ')
parser.add_option('-w','--weight', parser.add_option('-w','--weight',
dest = 'weight', dest = 'weight',
@ -49,15 +49,15 @@ parser.add_option('-z','--zrange',
parser.add_option('-i','--invert', parser.add_option('-i','--invert',
dest = 'invert', dest = 'invert',
action = 'store_true', action = 'store_true',
help = 'invert probability density [%default]') help = 'invert probability density')
parser.add_option('-r','--rownormalize', parser.add_option('-r','--rownormalize',
dest = 'normRow', dest = 'normRow',
action = 'store_true', action = 'store_true',
help = 'normalize probability density in each row [%default]') help = 'normalize probability density in each row')
parser.add_option('-c','--colnormalize', parser.add_option('-c','--colnormalize',
dest = 'normCol', dest = 'normCol',
action = 'store_true', action = 'store_true',
help = 'normalize probability density in each column [%default]') help = 'normalize probability density in each column')
parser.set_defaults(bins = (10,10), parser.set_defaults(bins = (10,10),
type = ('linear','linear','linear'), type = ('linear','linear','linear'),
@ -79,7 +79,8 @@ result = np.zeros((options.bins[0],options.bins[1],3),'f')
if options.data is None: parser.error('no data columns specified.') if options.data is None: parser.error('no data columns specified.')
labels = options.data labels = list(options.data)
if options.weight is not None: labels += [options.weight] # prevent character splitting of single string value if options.weight is not None: labels += [options.weight] # prevent character splitting of single string value
@ -106,7 +107,7 @@ for name in filenames:
# ------------------------------------------ sanity checks ---------------------------------------- # ------------------------------------------ sanity checks ----------------------------------------
missing_labels = table.data_readArray(labels) missing_labels = table.data_readArray(labels)
if len(missing_labels) > 0: if len(missing_labels) > 0:
damask.util.croak('column{} {} not found.'.format('s' if len(missing_labels) > 1 else '',', '.join(missing_labels))) damask.util.croak('column{} {} not found.'.format('s' if len(missing_labels) > 1 else '',', '.join(missing_labels)))
table.close(dismiss = True) table.close(dismiss = True)
@ -119,12 +120,11 @@ for name in filenames:
minmax[c] = np.log(minmax[c]) # change minmax to log, too minmax[c] = np.log(minmax[c]) # change minmax to log, too
delta = minmax[:,1]-minmax[:,0] delta = minmax[:,1]-minmax[:,0]
for i in xrange(len(table.data)): (grid,xedges,yedges) = np.histogram2d(table.data[:,0],table.data[:,1],
x = int(options.bins[0]*(table.data[i,0]-minmax[0,0])/delta[0]) bins=options.bins,
y = int(options.bins[1]*(table.data[i,1]-minmax[1,0])/delta[1]) range=minmax,
if x >= 0 and x < options.bins[0] and y >= 0 and y < options.bins[1]: weights=None if options.weight is None else table.data[:,2])
grid[x,y] += 1. if options.weight is None else table.data[i,2] # count (weighted) occurrences
if options.normCol: if options.normCol:
for x in xrange(options.bins[0]): for x in xrange(options.bins[0]):
@ -136,7 +136,7 @@ for name in filenames:
sum = np.sum(grid[:,y]) sum = np.sum(grid[:,y])
if sum > 0.0: if sum > 0.0:
grid[:,y] /= sum grid[:,y] /= sum
if (minmax[2] == 0.0).all(): minmax[2] = [grid.min(),grid.max()] # auto scale from data if (minmax[2] == 0.0).all(): minmax[2] = [grid.min(),grid.max()] # auto scale from data
if minmax[2,0] == minmax[2,1]: if minmax[2,0] == minmax[2,1]:
minmax[2,0] -= 1. minmax[2,0] -= 1.
@ -147,7 +147,7 @@ for name in filenames:
if options.type[2].lower() == 'log': if options.type[2].lower() == 'log':
grid = np.log(grid) grid = np.log(grid)
minmax[2] = np.log(minmax[2]) minmax[2] = np.log(minmax[2])
delta[2] = minmax[2,1]-minmax[2,0] delta[2] = minmax[2,1]-minmax[2,0]
for x in xrange(options.bins[0]): for x in xrange(options.bins[0]):