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.
Parameters
Parameters
Name Type Default Info lfs Union[LF, List[LF], None]
None
List of LFs.
\_\_init\_\_
__init__
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.
add
add
- difference(other)
Return a new package with LFs in the package that are not in the other package.
difference
difference
- get(name)
Get the LF specified by name.
get
get
- intersection(other)
Return a new package with LFs common to the package and the other.
Parameters
Parameters
Returns
Returns
New package with LFs common to the package and the other
Return type
Return type
Raises
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, ...)
.
Name Type Default Info other LFPackage
The package to compare.
intersection
intersection
- pop(name)
Remove the LF specified by name from the package and return it.
pop
pop
- rename_lfs(prefix='', suffix='')
Rename all LFs in the package.
rename\_lfs
rename_lfs
- union(other)
Return a new package with LFs from the package and the other.
Parameters
Parameters
Returns
Returns
New package with LFs from the package and the other
Return type
Return type
Raises
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, ...)
.
Name Type Default Info other LFPackage
The package to compare.
union
union
- update_label(label_mapping)
Update label strings.
Parameters
Parameters
Return type
Return type
None
Name Type Default Info label_mapping Dict[Any, Any]
Dictionary that maps from old (raw) label to new (raw) label. Examples
>>> # Find LFs whose label is 0 and update their label to 1
>>> pkg.update_label({0: 1})
update\_label
update_label