Skip to main content
Version: 0.93

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__

__init__(lfs=None)

Initialize a package.

Parameters

NameTypeDefaultInfo
lfsUnion[LF, List[LF], None]NoneList of LFs.

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

add(lf)

Add an LF to the package.

Parameters

NameTypeDefaultInfo
lfLFLF to add.

Return type

None

difference

difference(other)

Return a new package with LFs in the package that are not in the other package.

Parameters

NameTypeDefaultInfo
otherLFPackageThe package to compare.

Returns

New package with LFs in the package that are not in the other package

Return type

LFPackage

get

get(name)

Get the LF specified by name.

Parameters

NameTypeDefaultInfo
namestrName of the LF to get.

Returns

LF to get

Return type

LF

intersection

intersection(other)

Return a new package with LFs common to the package and the other.

Parameters

NameTypeDefaultInfo
otherLFPackageThe package to compare.

Returns

New package with LFs common to the package and the other

Return type

LFPackage

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 has LF(name='lf_a', label=1, ...).

pop

pop(name)

Remove the LF specified by name from the package and return it.

Parameters

NameTypeDefaultInfo
namestrName of the LF to remove.

Returns

LF that is removed

Return type

LF

rename_lfs

rename_lfs(prefix='', suffix='')

Rename all LFs in the package.

Parameters

NameTypeDefaultInfo
prefixstr''The string to append at the beginning of each LF name.
suffixstr''The string to append at the end of each LF name.

Return type

None

union

union(other)

Return a new package with LFs from the package and the other.

Parameters

NameTypeDefaultInfo
otherLFPackageThe package to compare.

Returns

New package with LFs from the package and the other

Return type

LFPackage

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 has LF(name='lf_a', label=1, ...).

update_label

update_label(label_mapping)

Update label strings.

Parameters

NameTypeDefaultInfo
label_mappingDict[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})