snorkelflow.lfs.LFPackage
- class snorkelflow.lfs.LFPackage(lfs=None)
Bases:
object
Set of LFs to represent an LF package.
This class allows manipulating a package at LF-level (e.g., adding an LF) as well as at package-level (e.g., taking a union of packages), but remember that LF names should be unique within a package.
Examples
Initialize a package with a list of LFs.
>>> from snorkelflow.lfs import LF, LFPackage
>>> lf_a = LF(name="lf_a", label=0, templates=...)
>>> lf_b = LF(name="lf_b", label=1, templates=...)
>>> pkg = LFPackage([lf_a, lf_b])
>>> [lf.name for lf in pkg]
['lf_a', 'lf_b']Rename all the LFs in a package.
>>> pkg.rename_lfs(prefix="my_")
>>> [lf.name for lf in pkg]
['my_lf_a', 'my_lf_b']Rename a single LF. Due to its immutability, an LF should be removed, recreated, and added again.
>>> lf_config = pkg.pop("lf_a").to_dict()
>>> lf_config["name"] = "my_lf_a_v1"
>>> pkg.add(LF(**lf_config))
>>> [lf.name for lf in pkg]
['lf_b', 'my_lf_a_v1']- __init__(lfs=None)
Initialize a package.
Methods
__init__
([lfs])Initialize a package.
add
(lf)Add an LF to the package.
difference
(other)Return a new package with LFs in the package that are not in the other package.
get
(name)Get the LF specified by name.
intersection
(other)Return a new package with LFs common to the package and the other.
pop
(name)Remove the LF specified by name from the package and return it.
rename_lfs
([prefix, suffix])Rename all LFs in the package.
union
(other)Return a new package with LFs from the package and the other.
update_label
(label_mapping)Update label strings.
- add(lf)
Add an LF to the package.
- Parameters:
lf (
LF
) – LF to add- Return type:
None
- difference(other)
Return a new package with LFs in the package that are not in the other package.
- get(name)
Get the LF specified by name.
- Parameters:
name (
str
) – Name of the LF to get- Returns:
LF to get
- Return type:
- intersection(other)
Return a new package with LFs common to the package and the other.
- Parameters:
other (
LFPackage
) – The package to compare- Returns:
New package with LFs common to the package and the other
- Return type:
- Raises:
ValueError – If both packages have an LF with the same name but a different value for the other attributes. For example, this package has
LF(name='lf_a', label=0, ...)
and the other hasLF(name='lf_a', label=1, ...)
.
- pop(name)
Remove the LF specified by name from the package and return it.
- Parameters:
name (
str
) – Name of the LF to remove- Returns:
LF that is removed
- Return type:
- rename_lfs(prefix='', suffix='')
Rename all LFs in the package.
- Parameters:
prefix (
str
, default:''
) – The string to append at the beginning of each LF namesuffix (
str
, default:''
) – The string to append at the end of each LF name
- Return type:
None
- union(other)
Return a new package with LFs from the package and the other.
- Parameters:
other (
LFPackage
) – The package to compare- Returns:
New package with LFs from the package and the other
- Return type:
- Raises:
ValueError – If both packages have an LF with the same name but a different value for the other attributes. For example, this package has
LF(name='lf_a', label=0, ...)
and the other hasLF(name='lf_a', label=1, ...)
.
- update_label(label_mapping)
Update label strings.
- Parameters:
label_mapping (
Dict
[Any
,Any
]) – Dictionary that maps from old (raw) label to new (raw) label.- Return type:
None
Examples
>>> # Find LFs whose label is 0 and update their label to 1
>>> pkg.update_label({0: 1})