def plot_results(): # Add in deleted obs deleted = np.in1d(X[:, 1], yatsm.X[:, 1], invert=True) plot_dataset() plt.plot(dates[deleted], Y[plot_index, deleted], 'ro') # Get qualitative color map for model segment lines # Color map ncolors goes from 3 - 9 ncolors = min(9, max(3, len(yatsm.record))) # Repeat if number of segments > 9 repeat = int(math.ceil(len(yatsm.record) / 9.0)) fit_colors = brewer2mpl.get_map('set1', 'qualitative', ncolors).hex_colors * repeat # Direction for prediction x step = -1 if reverse else 1 for i, r in enumerate(yatsm.record): # Predict mx = np.arange(r['start'], r['end'], step) my = np.dot(r['coef'][:, plot_index], make_X(mx, freq)) mx_date = np.array([dt.fromordinal(int(_x)) for _x in mx]) plt.plot(mx_date, my, fit_colors[i]) idx = np.where((yatsm.X[:, 1] >= r['start']) & (yatsm.X[:, 1] <= r['end']))[0] sklearn_lasso = sklearn.linear_model.Lasso(alpha=20).fit(yatsm.X[idx, :], yatsm.Y[plot_index, idx]) plt.plot(mx_date, sklearn_lasso.predict(make_X(mx, freq).T), fit_colors[i], ls='dashed', lw=3) # from IPython.core.debugger import Pdb # Pdb().set_trace() if r['break'] != 0: break_date = dt.fromordinal(int(r['break'])) break_i = np.where(X[:, 1] == r['break'])[0] if not plot_ylim: _plot_ylim = (Y[plot_index, :].min(), Y[plot_index, :].max()) else: _plot_ylim = plot_ylim plt.vlines(break_date, _plot_ylim[0], _plot_ylim[1], 'r') plt.plot(break_date, Y[plot_index, break_i], 'ro', mec='r', mfc='none', ms=10, mew=5)
def preprocess(location, px, py, freq, image_pattern='L*stack'): """ Read and preprocess Landsat data before analysis """ dates, files = find_stack_images(location, image_pattern=image_pattern) x = np.array(dates) Y = read_pixel_timeseries(files, px, py) # Filter out time series and remove Fmask clear = np.logical_and(Y[fmask, :] <= 1, np.all(Y <= 10000, axis=0)) Y = Y[:, clear][:fmask, :] x = x[clear] # Ordinal date ord_x = np.array(map(dt.toordinal, x)) # Make X matrix X = make_X(ord_x, freq).T return X, Y, clear