def merge_variable(fills, var): return merge_variable_interpolated(fills, var) xmin = sys.maxsize xmax = -xmin for fill in fills: xmin = min(fill.data[var].x.min(), xmin) xmax = max(fill.data[var].x.max(), xmax) # The x-series newx = np.arange( xmin, xmax + 1, 1, dtype=np.int) # using int's to maybe make comparisons more safe # Will store the average values of y avgy = np.zeros(newx.size, dtype=np.float) norm = np.zeros( newx.size, dtype=np.float ) # we use this to basically take the average value at each timestamp # Will store maximum and minimum values of y maxy = np.zeros(newx.size, dtype=np.float) miny = 1.0e10 * np.ones(newx.size, dtype=np.float) for fill in fills: x_i = fill.data[var].x.astype(np.int) index_map = np.searchsorted(newx, x_i) np.add.at(avgy, index_map, fill.data[var].y) np.add.at(norm, index_map, 1.0) np.maximum.at(maxy, index_map, fill.data[var].y) np.minimum.at(miny, index_map, fill.data[var].y) # delete empty values (where no data was written) to_delete = np.where(norm < 0.5) newx = np.delete(newx, to_delete) maxy = np.delete(maxy, to_delete) miny = np.delete(miny, to_delete) norm = np.delete(norm, to_delete) avgy = np.delete(avgy, to_delete) avgy /= norm newx = newx.astype(dtype=float) return { "average": Fill.Variable((newx, avgy)), "max": Fill.Variable((newx, maxy)), "min": Fill.Variable((newx, miny)) }
def merge_variable_interpolated(fills, var, resolution=0.1): xmin = -sys.maxsize xmax = -xmin for fill in fills: xmin = max(fill.data[var].x.min(), xmin) xmax = min(fill.data[var].x.max(), xmax) newx = np.arange(math.ceil(xmin), math.floor(xmax), resolution) avg = np.zeros(newx.size) maxy = np.zeros(newx.size) miny = np.full(maxy.shape, sys.maxsize, dtype=float) for fill in fills: ip = interpolate.interp1d(*fill.data[var], assume_sorted=True)(newx) avg += ip maxy = np.maximum(maxy, ip) miny = np.minimum(miny, ip) avg /= len(fills) return { "average": Fill.Variable((newx, avg)), "max": Fill.Variable((newx, maxy)), "min": Fill.Variable((newx, miny)) }