Exemple #1
0
def create_plot(imp, method, average, threshold=0.1):
	intensity = cross_section_intensity(imp, method)
	cal = imp.getCalibration()
	x_inc = cal.pixelWidth;
	units = cal.getUnits();
	x_label = "Distance (%s)" % units
	y_label = 'Intensity' # cal.getValueUnit()
	x_values = [i*x_inc for i in range(len(intensity))]

	lastindex = len(x_values)-1
	for i in range(1, len(x_values)+1):
		index = len(x_values)-i
		if intensity[index] == 0:
			lastindex = index-1
		else:
			break
	ax = [x_values[i] for i in range(lastindex)]
	ay = [intensity[i] for i in range(lastindex)]
	average_x, average_y = rolling_average(ax, ay, average)

	firstidx, lastidx, threshold_intensity = get_thresholded_idx(average_y, threshold=threshold)
	perform_trim = firstidx!=-1 and lastidx!=-1
	if perform_trim:
	    trim_x = [average_x[i] for i in range(firstidx, lastidx+1)]
	    trim_y = [average_y[i] for i in range(firstidx, lastidx+1)]

	# raw data
	flags = Plot.getDefaultFlags()
	flags = flags - Plot.Y_GRID - Plot.X_GRID
	plot = Plot("%s-Plot" % imp.getTitle(), x_label, y_label, flags)
	plot.setLineWidth(1)
	plot.setColor(Color.BLACK)
	plot.addPoints(x_values, intensity,Plot.LINE)

	# threshold line
	plot.setLineWidth(2)
	plot.setColor(Color.BLACK)
	plot.addPoints([0,x_inc * imp.getWidth()], [threshold_intensity,threshold_intensity],Plot.LINE)

	# rolling average
	plot.setLineWidth(2)
	plot.setColor(Color.MAGENTA)
	plot.addPoints(average_x,average_y,Plot.LINE)

	# standard legend labels
	labels = "\t".join(['Raw Data (%s)' % method, 'Intensity threshold (%d%s)' % (100*threshold, '%'), 'Rolling Average (n=%d)' % average])

	# trimmed rolling average
	if perform_trim:
	    plot.setLineWidth(2)
	    plot.setColor(Color.GREEN)
	    plot.addPoints(trim_x,trim_y,Plot.LINE)
	    labels+='\tTrimmed Rolling Average (n=%d)' % average

	plot.setColor(Color.BLACK)
	plot.setLimitsToFit(False)
	plot.addLegend(labels)

	rt = ResultsTable()
	for row,x in enumerate(x_values):
		rt.setValue(DIST_RAW_COL, row, x)
		rt.setValue(INT_RAW_COL, row, intensity[row])
	for row,x in enumerate(average_x):
		rt.setValue(DIST_AVG_COL, row, x)
		rt.setValue(INT_AVG_COL, row, average_y[row])
	if perform_trim:
	    for row,x in enumerate(trim_x):
		    rt.setValue(DIST_TRIM_COL, row, x)
		    rt.setValue(INT_TRIM_COL, row, trim_y[row])
    
	return plot, rt