def make_1d_moments(outdir = '', sim_type = 'tristan-mp', n='1', prtl_type='', xval='', yval = '', weights = '', boolstr = '', xbins ='200', xvalmin = '', xvalmax = '', xtra_stride = '1', xscale = 'linear', selPolyXval = '', selPolyYval = '', selPolyXarr = '', selPolyYarr= ''): '''We calculate a 1D average of yval as a function of xval at outdir, and then return a list of dictionaries where each dictinary is of the form {'num': the number count, 'x0': left edge of the bin, 'x1': right edge of the bin}.''' ### first we open up a tristan sim if sim_type =='tristan-mp': cur_sim = TristanSim(outdir, n = int(n), xtra_stride = int(xtra_stride)) # first we evaluate the boolean string to see what values we should discard: bool_arr = parse_boolstr(boolstr, cur_sim, prtl_type) # Now we go through an fill out some of the unfilled data needed to make # take an average xarr = getattr(getattr(cur_sim, prtl_type), xval) xarr = xarr if bool_arr is None else xarr[bool_arr] yarr = getattr(getattr(cur_sim, prtl_type), yval) yarr = yarr if bool_arr is None else yarr[bool_arr] warr = np.array([]) if len(weights) != 0: warr = getattr(getattr(cur_sim, prtl_type), weights) warr = warr if bool_arr is None else warr[bool_arr] # NOW WE APPLY THE POLYGON: if (len(selPolyXval) != 0): inside = np.zeros(len(xarr), dtype='bool') datX = getattr(getattr(cur_sim, prtl_type), selPolyXval) datX = datX if bool_arr is None else datX[bool_arr] datY = getattr(getattr(cur_sim, prtl_type), selPolyYval) datY = datY if bool_arr is None else datY[bool_arr] polyX = np.fromstring(selPolyXarr, sep=',') polyY = np.fromstring(selPolyYarr, sep=',') bbox = np.array([polyX.min(), polyX.max(), polyY.min(), polyY.max()]) point_in_polygon(datX, datY, polyX, polyY, bbox, inside) xarr = xarr[inside] yarr = yarr[inside] if (len(warr) != 0): warr = warr[inside] xvalmin = xarr.min() if len(xvalmin)==0 else float(xvalmin) xvalmax = xarr.max() if len(xvalmax)==0 else float(xvalmax) if len(warr)==0: if xscale =='log' and xvalmin > 0: hist = CalcMoments(np.log10(xarr), yarr, np.log10(xvalmin), np.log10(xvalmax), int(float(xbins))) else: hist = CalcMoments(xarr, yarr, xvalmin, xvalmax, int(float(xbins))) else: #calculate unweighed histogram if xscale =='log' and xvalmin > 0: hist = CalcWeightedMoment(np.log10(xarr), yarr, warr, np.log10(xvalmin), np.log10(xvalmax), int(float(xbins))) else: hist = CalcWeightedMoment(xarr, yarr, warr, xvalmin, xvalmax, int(float(xbins))) #### # # Now we have the histogram, we need to turn it into a JSON compatible with # D3 hist function. D3 hist return a sorted array that keeps all of the particle # data. That is not possible in our case due to memory constraints. Instead we # simply return a list # ### if xscale =='log' and xvalmin >0: bin_width = (np.log10(xvalmax)-np.log10(xvalmin))/int(xbins) bins = np.logspace(np.log10(xvalmin), np.log10(xvalmax), num = int(xbins)+1) else: bin_width = (xvalmax-xvalmin)/int(xbins) bins = np.linspace(xvalmin, xvalmax, num = int(xbins)+1) x_arr, y_arr = stepify(bins, hist) x_arr = x_arr[1:-1]; y_arr = y_arr[1:-1] mom1D = [{'y': y_arr[i], 'x': x_arr[i]} for i in range(len(x_arr))] return { 'lineData': mom1D, 'xscale': 'log' if xscale =='log' and xvalmin >0 else 'linear', 'xmin': mom1D[0]['x'], 'xmax': mom1D[-1]['x'], 'vmin': np.min(hist), 'vmax': np.max(hist) }
def make_2d_mom_img(outdir = '', sim_type = 'tristan-mp', n='1', prtl_type='', yval='', xval='', mval='', weights = '', boolstr = '', ybins = '200', xbins ='200', yvalmin='', yvalmax='', xvalmin = '', xvalmax = '', normhist = 'true',cmap='viridis', cnorm = 'log', pow_zero = '0', pow_gamma='1.0', vmin = '', clip = 'true', vmax = '', xmin='', xmax ='', ymin='', ymax='', interpolation = 'bicubic', px ='400', py='400', aspect='auto', mask_zeros='true', xtra_stride = '1', selPolyXval= '', selPolyYval='', selPolyXarr='', selPolyYarr=''): # First we calculate the histogram, then we turn it into an image and return # the image as a bytesIO ### first we open up a tristan sim if sim_type =='tristan-mp': cur_sim = TristanSim(outdir, n = int(n), xtra_stride = int(xtra_stride)) # first we evaluate the boolean string to see what values we should discard: bool_arr = parse_boolstr(boolstr, cur_sim, prtl_type) # Now we go through an fill out some of the unfilled data needed to make # a histogram xarr = getattr(getattr(cur_sim, prtl_type), xval) xarr = xarr if bool_arr is None else xarr[bool_arr] yarr = getattr(getattr(cur_sim, prtl_type), yval) yarr = yarr if bool_arr is None else yarr[bool_arr] marr = getattr(getattr(cur_sim, prtl_type), mval) marr = marr if bool_arr is None else marr[bool_arr] warr = np.array([]) if len(weights) != 0: warr = getattr(getattr(cur_sim, prtl_type), weights) warr = warr if bool_arr is None else warr[bool_arr] # NOW WE APPLY THE POLYGON: if (len(selPolyXval) != 0): inside = np.zeros(len(xarr), dtype='bool') datX = getattr(getattr(cur_sim, prtl_type), selPolyXval) datX = datX if bool_arr is None else datX[bool_arr] datY = getattr(getattr(cur_sim, prtl_type), selPolyYval) datY = datY if bool_arr is None else datY[bool_arr] polyX = np.fromstring(selPolyXarr, sep=',') polyY = np.fromstring(selPolyYarr, sep=',') bbox = np.array([polyX.min(), polyX.max(), polyY.min(), polyY.max()]) point_in_polygon(datX, datY, polyX, polyY, bbox, inside) xarr = xarr[inside] yarr = yarr[inside] marr = marr[inside] if (len(warr) != 0): warr = warr[inside] yvalmin = yarr.min() if len(yvalmin)==0 else float(yvalmin) yvalmax = yarr.max() if len(yvalmax)==0 else float(yvalmax) xvalmin = xarr.min() if len(xvalmin)==0 else float(xvalmin) xvalmax = xarr.max() if len(xvalmax)==0 else float(xvalmax) if len(warr)==0: hist = Calc2DMoments(yarr, xarr, marr, yvalmin, yvalmax, int(float(ybins)), xvalmin, xvalmax, int(float(xbins))) else: #calculate unweighed histogram hist = Calc2DWeightedMoments(yarr, xarr, marr, warr, yvalmin, yvalmax, int(float(ybins)), xvalmin, xvalmax, int(float(xbins))) #### # # Now we have the histogram, we need to turn it into an image # ### hist_img = myNumbaImage(int(py), int(px)) hist_img.setInterpolation(interpolation) hist_img.setData(hist) hist_img.setExtent([xvalmin,xvalmax,yvalmin, yvalmax]) hist_img.set_xlim(xmin = None if len(xmin)==0 else float(xmin), xmax = None if len(xmax)==0 else float(xmax)) hist_img.set_ylim(ymin = None if len(ymin)==0 else float(ymin), ymax = None if len(ymax)==0 else float(ymax)) if cnorm =='log': hist_img.setNorm('log', clipped = True if clip =='true' else False) if cnorm =='linear': hist_img.setNorm('linear', clipped = True if clip =='true' else False) if cnorm =='pow': hist_img.setNorm('pow',zero = float(pow_zero), gamma = float(pow_gamma), clipped = True if clip =='true' else False) hist_img.setCmap(cmap) hist_img.set_clim(cmin = None if len(vmin) ==0 else float(vmin), cmax = None if len(vmax)==0 else float(vmax)) hist_img.set_aspect(0 if aspect=='auto' else 1) #hist_img.set_aspect(1)# if aspect=='auto' else 1) return hist_img.renderImageDict()
quotechar='"', quoting=csv.QUOTE_MINIMAL) line_num = 0 start_on_campus = 0 end_on_campus = 0 start_on_end_on = 0 start_on_end_off = 0 start_off_end_on = 0 start_off_end_off = 0 for row in csv_reader: if line_num == 0: row.append("START INSIDE") row.append("END INSIDE") csv_writer.writerow(row) else: start_inside = point_in_polygon( boundaries, [float(row[12]), float(row[13])]) end_inside = point_in_polygon( boundaries, [float(row[14]), float(row[15])]) if start_inside: start_on_campus += 1 if end_inside: end_on_campus += 1 start_on_end_on += 1 else: start_on_end_off += 1 else: if end_inside: end_on_campus += 1 start_off_end_on += 1 else: start_off_end_off += 1