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_labeling_function_serialize(self) -> None:
db = [3, 6, 43]
lf = LabelingFunction(name="my_lf", f=g, resources=dict(db=db))
lf_load = pickle.loads(pickle.dumps(lf))
self._run_lf(lf_load)
def test_wrong_number_of_lfs(self) -> None:
with self.assertRaisesRegex(ValueError, "Number of LFs"):
LFAnalysis(np.array(L), [LabelingFunction(s, f) for s in "ab"])
pd.testing.assert_frame_equal(df.round(6), df_expected.round(6))
df = self.lfa.lf_summary(Y=None, est_weights=None)
df_expected = pd.DataFrame(
{
"Polarity": [[1, 2], [], [0, 2], [2], [0, 1], [0]],
"Coverage": [3 / 6, 0, 3 / 6, 2 / 6, 2 / 6, 4 / 6],
"Overlaps": [3 / 6, 0, 3 / 6, 1 / 6, 2 / 6, 4 / 6],
"Conflicts": [3 / 6, 0, 2 / 6, 1 / 6, 2 / 6, 3 / 6],
}
)
pd.testing.assert_frame_equal(df.round(6), df_expected.round(6))
est_weights = [1, 0, 1, 1, 1, 0.5]
names = list("abcdef")
lfs = [LabelingFunction(s, f) for s in names]
lfa = LFAnalysis(np.array(L), lfs)
df = lfa.lf_summary(self.Y, est_weights=est_weights)
df_expected = pd.DataFrame(
{
"j": [0, 1, 2, 3, 4, 5],
"Polarity": [[1, 2], [], [0, 2], [2], [0, 1], [0]],
"Coverage": [3 / 6, 0, 3 / 6, 2 / 6, 2 / 6, 4 / 6],
"Overlaps": [3 / 6, 0, 3 / 6, 1 / 6, 2 / 6, 4 / 6],
"Conflicts": [3 / 6, 0, 2 / 6, 1 / 6, 2 / 6, 3 / 6],
"Correct": [1, 0, 1, 1, 1, 2],
"Incorrect": [2, 0, 2, 1, 1, 2],
"Emp. Acc.": [1 / 3, 0, 1 / 3, 1 / 2, 1 / 2, 2 / 4],
"Learned Weight": [1, 0, 1, 1, 1, 0.5],
}
).set_index(pd.Index(names))
pd.testing.assert_frame_equal(df.round(6), df_expected.round(6))
def get_negative_labeling_function(divisor: int) -> LabelingFunction:
"""Get LabelingFunction that abstains unless x0 is divisible by divisor."""
def f(x):
return 0 if x.x0 % divisor == 0 and x.x1 <= x.x2 + 0.25 else -1
return LabelingFunction(f"lf_neg_{divisor}", f)
def test_labeling_function_preprocessor(self) -> None:
lf = LabelingFunction(name="my_lf", f=f, pre=[square, square])
x_43 = SimpleNamespace(num=43)
x_6 = SimpleNamespace(num=6)
x_2 = SimpleNamespace(num=2)
self.assertEqual(lf(x_43), 0)
self.assertEqual(lf(x_6), 0)
self.assertEqual(lf(x_2), -1)
def test_labeling_function_decorator_args(self) -> None:
db = [3, 6, 43 ** 2]
@labeling_function(name="my_lf", resources=dict(db=db), pre=[square])
def lf(x: DataPoint, db: List[int]) -> int:
return 0 if x.num in db else -1
self.assertIsInstance(lf, LabelingFunction)
self.assertEqual(lf.name, "my_lf")
self._run_lf(lf)
def make_keyword_lf(keywords, label=SPAM):
return LabelingFunction(
name=f"keyword_{keywords[0]}",
f=keyword_lookup,
resources=dict(keywords=keywords, label=label),
)