def run_test(cls, obj, model_type, verbose=False): if verbose: print("Calling fit with `model_type = '%s'`..." % (model_type, ), end="") sys.stdout.flush() with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=PendingDeprecationWarning) warnings.filterwarnings("ignore", category=LineSearchWarning) try: fit( X=obj.X, Y=obj.Y, model_type=model_type, treated_units=obj.treated_units if model_type in ("retrospective", "prospective", "prospective-restricted") else None, # KWARGS: print_path=False, progress=verbose, grid_length=5, min_iter=-1, tol=1, verbose=0, ) if verbose: print("DONE") except LineSearchWarning: pass except PendingDeprecationWarning: pass except Exception as exc: print("Failed with %s: %s" % (exc.__class__.__name__, exc.message))
def run_test(cls, obj, model_type, verbose=False, w_pen_inner=False, match_space_maker=None): """ main test runner """ if verbose: print("Calling fit with `model_type = '%s'`..." % (model_type, ), end="") sys.stdout.flush() # Catch the LineSearchWarning silently, but allow others with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=PendingDeprecationWarning) warnings.filterwarnings("ignore", category=LineSearchWarning) try: fit( features=obj.X, targets=obj.Y, model_type=model_type, treated_units=obj.treated_units if model_type in ("retrospective", "prospective", "prospective-restricted") else None, # KWARGS: print_path=False, stopping_rule=1, progress=verbose, grid_length=5, min_iter=-1, tol=1, verbose=0, w_pen_inner=w_pen_inner, match_space_maker=match_space_maker, ) if verbose: print("DONE") except LineSearchWarning: pass except PendingDeprecationWarning: pass except Exception as exc: # pytlint: disable=broad-exception print( "Failed with {}({})/n{}\n==================================" .format(exc.__class__.__name__, getattr(exc, "message", ""), traceback.format_exc())) raise exc
def test0s(self): N1, N0_sim, N0_not = 1, 50, 50 N0 = N0_sim + N0_not #N = N1 + N0 treated_units = range(N1) #control_units = range(N1, N) T0, T1 = 2, 1 #T = T0 + T1 # unused te = 2 #configs = [[[1, 0, 0], [0, 1, 1]], # [[0, 1, 0], [1, 0, 1]], # [[1, 0, 2], [0, 1, 1]], # [[0, 1, 2], [1, 0, 1]]] #configs = [[[2, 1, 0], [1, 2, 1]], # [[1, 2, 0], [2, 1, 1]], # [[2, 1, 2], [1, 2, 1]], # [[1, 2, 2], [2, 1, 1]]] configs = [[[1, 2, 2], [2, 1, 1]], [[1, 2, 3], [2, 1, 1]]] for config in configs: proto_sim = np.array(config[0], ndmin=2) proto_not = np.array(config[1], ndmin=2) proto_tr = proto_sim + np.hstack((np.zeros((1, T0)), np.full((1, T1), te))) Y1 = np.matmul(np.ones((N1, 1)), proto_tr) Y0_sim = np.matmul(np.ones((N0_sim, 1)), proto_sim) Y0_not = np.matmul(np.ones((N0_not, 1)), proto_not) Y = np.vstack((Y1, Y0_sim, Y0_not)) with warnings.catch_warnings(): warnings.simplefilter("ignore") fit_res = fit(Y[:, :T0], Y[:, T0:], treated_units, model_type="retrospective", stopping_rule=4, progress=False, verbose=0, print_path=False) Y_sc = fit_res.predict(Y) treated_diff = (Y - Y_sc)[0, :] print("V: %s. Treated diff: %s" % (np.diag(fit_res.V), treated_diff))
def fitSparseSC_wrapper(Y_pre, Y_post, treated_units): return (fit(X=Y_pre, Y=Y_post, treated_units=treated_units, model_type="retrospective", print_path=False, progress=False, verbose=0))