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
@ -120,11 +121,10 @@ for name in filenames:
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]):