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_query(query_strategy, n_features=50, n_sample=100,
n_instances_list=[0, 1, 5, 50], n_train_idx=[0, 1, 5, 50]):
classifier = get_model("rf")
query_model = get_query_model(query_strategy)
X = np.random.rand(n_sample, n_features)
y = np.concatenate((np.zeros(n_sample//2), np.ones(n_sample//2)), axis=0)
print(X.shape, y.shape)
order = np.random.permutation(n_sample)
print(order.shape)
X = X[order]
y = y[order]
sources = query_strategy.split('_')
classifier.fit(X, y)
assert isinstance(query_model.param, dict)
assert query_model.name == query_strategy
for n_instances in n_instances_list:
elif key.starts_with(strategy_2):
new_key = key[len(strategy_2)+1:]
kwargs_2[new_key] = value
else:
logging.warn(f"Key {key} is being ignored for the mixed "
"({strategy_1}, {strategy_2}) query strategy.")
self.strategy_1 = strategy_1
self.strategy_2 = strategy_2
self.query_model1 = get_query_model(strategy_1, **kwargs_1)
self.query_model2 = get_query_model(strategy_2, **kwargs_2)
self._random_state = get_random_state(random_state)
if "random_state" in self.query_model1.default_param:
self.query_model1 = get_query_model(strategy_1, **kwargs_1,
random_state=self._random_state
)
if "random_state" in self.query_model2.default_param:
self.query_model2 = get_query_model(strategy_2, **kwargs_2,
random_state=self._random_state
)
self.mix_ratio = mix_ratio
for key, value in kwargs.items():
if key.startswith(strategy_1):
new_key = key[len(strategy_1)+1:]
kwargs_1[new_key] = value
elif key.starts_with(strategy_2):
new_key = key[len(strategy_2)+1:]
kwargs_2[new_key] = value
else:
logging.warn(f"Key {key} is being ignored for the mixed "
"({strategy_1}, {strategy_2}) query strategy.")
self.strategy_1 = strategy_1
self.strategy_2 = strategy_2
self.query_model1 = get_query_model(strategy_1, **kwargs_1)
self.query_model2 = get_query_model(strategy_2, **kwargs_2)
self._random_state = get_random_state(random_state)
if "random_state" in self.query_model1.default_param:
self.query_model1 = get_query_model(strategy_1, **kwargs_1,
random_state=self._random_state
)
if "random_state" in self.query_model2.default_param:
self.query_model2 = get_query_model(strategy_2, **kwargs_2,
random_state=self._random_state
)
self.mix_ratio = mix_ratio
settings.balance_param = balance_param
if feature_param is not None:
settings.feature_param = feature_param
# Check if mode is valid
if mode in AVAILABLE_REVIEW_CLASSES:
logging.info(f"Start review in '{mode}' mode.")
else:
raise ValueError(f"Unknown mode '{mode}'.")
logging.debug(settings)
# Initialize models.
random_state = get_random_state(seed)
train_model = get_model(settings.model, **settings.model_param,
random_state=random_state)
query_model = get_query_model(settings.query_strategy,
**settings.query_param,
random_state=random_state)
balance_model = get_balance_model(settings.balance_strategy,
**settings.balance_param,
random_state=random_state)
feature_model = get_feature_model(settings.feature_extraction,
**settings.feature_param,
random_state=random_state)
# LSTM models need embedding matrices.
if train_model.name.startswith("lstm-"):
texts = as_data.texts
train_model.embedding_matrix = feature_model.get_embedding_matrix(
texts, embedding_fp)
# Initialize the review class.
logging.warn(f"Key {key} is being ignored for the mixed "
"({strategy_1}, {strategy_2}) query strategy.")
self.strategy_1 = strategy_1
self.strategy_2 = strategy_2
self.query_model1 = get_query_model(strategy_1, **kwargs_1)
self.query_model2 = get_query_model(strategy_2, **kwargs_2)
self._random_state = get_random_state(random_state)
if "random_state" in self.query_model1.default_param:
self.query_model1 = get_query_model(strategy_1, **kwargs_1,
random_state=self._random_state
)
if "random_state" in self.query_model2.default_param:
self.query_model2 = get_query_model(strategy_2, **kwargs_2,
random_state=self._random_state
)
self.mix_ratio = mix_ratio
kwargs_2 = {}
for key, value in kwargs.items():
if key.startswith(strategy_1):
new_key = key[len(strategy_1)+1:]
kwargs_1[new_key] = value
elif key.starts_with(strategy_2):
new_key = key[len(strategy_2)+1:]
kwargs_2[new_key] = value
else:
logging.warn(f"Key {key} is being ignored for the mixed "
"({strategy_1}, {strategy_2}) query strategy.")
self.strategy_1 = strategy_1
self.strategy_2 = strategy_2
self.query_model1 = get_query_model(strategy_1, **kwargs_1)
self.query_model2 = get_query_model(strategy_2, **kwargs_2)
self._random_state = get_random_state(random_state)
if "random_state" in self.query_model1.default_param:
self.query_model1 = get_query_model(strategy_1, **kwargs_1,
random_state=self._random_state
)
if "random_state" in self.query_model2.default_param:
self.query_model2 = get_query_model(strategy_2, **kwargs_2,
random_state=self._random_state
)
self.mix_ratio = mix_ratio
except (KeyError, TypeError):
print(f"Warning: value with key '{key}' is ignored "
"(spelling mistake, wrong type?).")
elif sect in ["model_param", "query_param", "balance_param",
"feature_param"]:
setattr(self, sect, dict(config.items(sect)))
elif sect != "DEFAULT":
print (f"Warning: section [{sect}] is ignored in "
f"config file {config_file}")
model = get_model(self.model)
_convert_types(model.default_param, self.model_param)
balance_model = get_balance_model(self.balance_strategy)
_convert_types(balance_model.default_param, self.balance_param)
query_model = get_query_model(self.query_strategy)
_convert_types(query_model.default_param, self.query_param)
feature_model = get_feature_model(self.feature_extraction)
_convert_types(feature_model.default_param, self.feature_param)