def check_model(monkeypatch=None, use_granular=False, state_file=h5_state_file, continue_from_state=False, mode="simulate", data_fp=data_fp, state_checker=check_state, prior_idx=[1, 2, 3, 4], **kwargs): if not continue_from_state: try: if state_file is not None: os.unlink(state_file) except OSError: pass if monkeypatch is not None: monkeypatch.setattr('builtins.input', lambda _: "0") # start the review process. reviewer = get_reviewer(data_fp, mode=mode, embedding_fp=embedding_fp, prior_idx=prior_idx, state_file=state_file, **kwargs) if use_granular: with open_state(state_file) as state: # Two loops of training and classification. reviewer.train() reviewer.log_probabilities(state) query_idx = reviewer.query(1) inclusions = reviewer._get_labels(query_idx) reviewer.classify(query_idx, inclusions, state) reviewer.train() reviewer.log_probabilities(state) query_idx = reviewer.query(1) inclusions = reviewer._get_labels(query_idx) reviewer.classify(query_idx, inclusions, state) else: with open_state(state_file) as state: if state_file is None: state.set_labels(reviewer.y) init_idx, init_labels = reviewer._prior_knowledge() reviewer.query_i = 0 reviewer.train_idx = np.array([], dtype=np.int) reviewer.classify(init_idx, init_labels, state, method="initial") reviewer._do_review(state) if state_file is None: print(state._state_dict) check_state(state) if state_file is not None: with open_state(state_file, read_only=True) as state: state_checker(state)
def test_read_hdf5_state(tmpdir): state_fp = Path("tests", "state_files", "test_1_inst.h5") state_tmp_fp = Path(tmpdir, "test_1_inst.h5") # copy state to open to tmpdir shutil.copyfile(state_fp, state_tmp_fp) with open_state(state_tmp_fp) as state: assert isinstance(state, HDF5State)
def check_write_state(tmpdir, state_file): if state_file is not None: state_fp = os.path.join(tmpdir, state_file) else: state_fp = None settings = ASReviewSettings(mode="simulate", model="nb", query_strategy="rand_max", balance_strategy="simple", feature_extraction="tfidf") n_records = 6 n_half = int(n_records / 2) start_labels = np.full(n_records, np.nan, dtype=np.int) labels = np.zeros(n_records, dtype=np.int) labels[::2] = np.ones(n_half, dtype=np.int) methods = np.full((n_records), "initial") methods[2::] = np.full((int(n_records - 2)), "random") methods[2::2] = np.full((int((n_records - 2) / 2)), "max") with open_state(state_fp) as state: state.settings = settings state.set_labels(start_labels) current_labels = np.copy(start_labels) for i in range(n_records): query_i = int(i / 2) proba = None if i >= 2 and (i % 2) == 0: proba = np.random.rand(n_records) state.add_classification([i], [labels[i]], [methods[i]], query_i) if proba is not None: state.add_proba(np.arange(i + 1, n_records), np.arange(i + 1), proba, query_i) current_labels[i] = labels[i] state.set_labels(current_labels) check_state(state, i, query_i, labels, methods, proba)
def test_read_dict_state(): with open_state(None) as state: assert isinstance(state, DictState)
def test_read_hdf5_state(): state_fp = Path("tests", "state_files", "test_1_inst.h5") with open_state(str(state_fp)) as state: assert isinstance(state, HDF5State)
def test_read_json_state(): state_fp = Path("tests", "state_files", "test_1_inst.json") with open_state(str(state_fp)) as state: assert isinstance(state, JSONState)