Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_read_json_state():
state_fp = Path("test", "state_files", "test_1_inst.json")
with open_state(str(state_fp)) as state:
assert isinstance(state, JSONState)
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)
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:
check_state(state)
def test_read_hdf5_state():
state_fp = Path("test", "state_files", "test_1_inst.h5")
with open_state(str(state_fp)) as state:
assert isinstance(state, HDF5State)
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:
check_state(state)
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=[1, 2, 3, 4],
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)