snorkelai.sdk.develop.Slice
- final class snorkelai.sdk.develop.Slice(dataset, slice_uid, name, description=None, templates=None, graph=None)
Bases:
Base
Represents a slice within a Snorkel dataset for identifying and managing subsets of datapoints.
A slice is a logical subset of datapoints within a dataset that can be created either manually by adding specific datapoints, or programmatically using slicing functions defined through templates and configurations. Slices are essential for data analysis, model evaluation, and targeted data operations within Snorkel workflows.
Key capabilities:
Manual datapoint management through add/remove operations
Programmatic datapoint identification using configurable slicing functions
This class provides methods for creating, retrieving, updating, and managing slice membership. Slice objects should not be instantiated directly - use the
create()
orget()
class methods instead.Read more in Using data slices.
Using the
Slice
class requires the following import:from snorkelai.sdk.develop import Slice
- __init__(dataset, slice_uid, name, description=None, templates=None, graph=None)
Create a Slice object in-memory with necessary properties. This constructor should not be called directly, and should instead be accessed through the
create()
andget()
methodsParameters
Parameters
Name Type Default Info dataset Union[str, int]
The UID or name for the dataset within Snorkel. slice_uid int
The UID for the slice within Snorkel. name str
The name of the slice. description Optional[str]
None
The description of the slice. templates Optional[List[Dict[str, Any]]]
None
Configuration defining slicing functions for programmatic datapoint identification. graph Optional[List[Any]]
None
A representation of the slicing function’s structure.
\_\_init\_\_
__init__
Methods
__init__
(dataset, slice_uid, name[, ...])Create a Slice object in-memory with necessary properties. add_x_uids
(x_uids)Adds datapoints to a slice. create
(dataset, name[, description, ...])Creates a slice for a dataset. delete
(dataset, slice_uid)Deletes a slice from a dataset. get
(dataset, slice)Retrieves a slice by UID or name. get_x_uids
()Retrieves the UIDs of the datapoints in the slice. list
(dataset)Retrieves all slices for a dataset. remove_x_uids
(x_uids)Removes datapoints from a slice. update
([name, description, templates, graph])Updates the slice properties. Attributes
dataset_uid
Return the UID of the dataset that the slice belongs to description
Return the description of the slice name
Return the name of the slice slice_uid
Return the UID of the slice uid
Return the UID of the slice - add_x_uids(x_uids)
Adds datapoints to a slice.
Parameters
Parameters
Raises
Raises
Exception – If other server errors occur during the operation.
Return type
Return type
None
Name Type Default Info x_uids List[str]
List of UIDs of the datapoints you want to add to the slice. Example
from snorkelai.sdk.develop import Slice
slice = Slice.get(dataset=1, slice=20)
slice.add_x_uids(["uid1", "uid2", "uid3"])
add\_x\_uids
add_x_uids
- classmethod create(dataset, name, description='', templates=None, graph=None)
Creates a slice for a dataset.
Slices are used to identify a subset of datapoints in a dataset. You can add datapoints to a slice manually, or if you define a config, you can add datapoints programmatically. Slice membership can contain both manual and programmatic identified datapoints.
Parameters
Parameters
Returns
Returns
The slice object.
Return type
Return type
Raises
Raises
ValueError – If the dataset doesn’t exist or cannot be found by name/UID.
ValueError – If the slice name is a reserved name or already exists for the dataset.
ValueError – If there are other validation or server errors during slice creation.
Name Type Default Info dataset Union[str, int]
The UID or name for the dataset within Snorkel. name str
The name of the slice. description str
''
A description of the slice, by default the empty string. templates Optional[List[Dict[str, Any]]]
None
A list of template dictionaries, by default None, you can reference the schema in the template
module for constructing these templates. These templates are used to define the Slicing Function for the slice, allowing it to programmatically add datapoints to the slice membership.graph Optional[List[Any]]
None
A representation of the slicing function’s structure, by default None. Examples
Example 1
Example 1
Create a simple slice without templates:
from snorkelai.sdk.develop import Slice
slice = Slice.create(
dataset=1,
name="my_slice",
description="A slice for testing purposes"
)Example 2
Example 2
Create a slice from a keyword template. This example creates a slice containing all datapoints with the string
capital
in theInstruction
field:from snorkelai.sdk.develop import Slice
from snorkelai.sdk.utils.graph import DEFAULT_GRAPH
from snorkelai.templates.keyword_template import KeywordTemplateSchema
keyword_template = KeywordTemplateSchema(
operator="CONTAINS",
keywords=["capital"],
field="Instruction",
case_sensitive=False,
tokenize=True,
)
template_config_dict = {
**keyword_template.to_dict(),
"template_type": "keyword"
}
slice = Slice.create(
dataset=1,
name="slice_name",
description="description",
templates=[
{
"transform_type": "dataset_template_filter",
"config": {
"transform_config_type": "template_filter_schema",
"filter_type": "text_template",
"filter_config_type": "dataset_text_template",
"dataset_uid": 1,
"template_config": template_config_dict,
},
},
],
graph=DEFAULT_GRAPH,
)
create
create
- classmethod delete(dataset, slice_uid)
Deletes a slice from a dataset.
Parameters
Parameters
Raises
Raises
ValueError – If the dataset or slice doesn’t exist or cannot be found by UID.
ValueError – If there are other validation or server errors during slice deletion.
Return type
Return type
None
Name Type Default Info dataset Union[str, int]
The UID or name for the dataset within Snorkel. slice_uid int
The UID of the slice to delete. Example
from snorkelai.sdk.develop import Slice
Slice.delete(dataset=1, slice_uid=20)
delete
delete
- classmethod get(dataset, slice)
Retrieves a slice by UID or name.
Parameters
Parameters
Returns
Returns
The slice object.
Return type
Return type
Raises
Raises
ValueError – If no slice is found with the given UID or name.
Name Type Default Info dataset Union[str, int]
The UID or name for the dataset within Snorkel. slice Union[str, int]
The UID or name of the slice. Example
from snorkelai.sdk.develop import Slice
slice = Slice.get(dataset=1, slice=20)
get
get
- get_x_uids()
Retrieves the UIDs of the datapoints in the slice.
Returns
Returns
List of UIDs of the datapoints in the slice.
Return type
Return type
List[str]
Raises
Raises
Exception – If other server errors occur during the operation.
Example
from snorkelai.sdk.develop import Slice
slice = Slice.get(dataset=1, slice=20)
x_uids = slice.get_x_uids()
get\_x\_uids
get_x_uids
- classmethod list(dataset)
Retrieves all slices for a dataset.
Parameters
Parameters
Returns
Returns
A list of all the slices available for that dataset.
Return type
Return type
List[Slice]
Raises
Raises
ValueError – If no dataset is found with the given id.
Name Type Default Info dataset Union[str, int]
The UID or name for the dataset within Snorkel. Example
from snorkelai.sdk.develop import Slice
slices = Slice.list(dataset=1)
list
list
- remove_x_uids(x_uids)
Removes datapoints from a slice.
Parameters
Parameters
Raises
Raises
Exception – If other server errors occur during the operation.
Return type
Return type
None
Name Type Default Info x_uids List[str]
List of UIDs of the datapoints you want to remove from the slice. Example
from snorkelai.sdk.develop import Slice
slice = Slice.get(dataset=1, slice=20)
slice.remove_x_uids(["uid1", "uid2", "uid3"])
remove\_x\_uids
remove_x_uids
- update(name=None, description=None, templates=None, graph=None)
Updates the slice properties.
Parameters
Parameters
Raises
Raises
ValueError – If there are other errors during slice update.
Return type
Return type
None
Name Type Default Info name Optional[str]
None
The new name for the slice, by default None. description Optional[str]
None
The new description for the slice, by default None. templates Optional[List[Dict[str, Any]]]
None
A list of template dictionaries for the slice, by default None. graph Optional[List[str]]
None
A list of strings representing the graph structure for the slice, by default None. Example
from snorkelai.sdk.develop import Slice
slice = Slice.get(dataset=1, slice=20)
slice.update(name="new_name", description="updated description")
update
update
- property dataset_uid: int
Return the UID of the dataset that the slice belongs to
- property description: str | None
Return the description of the slice
- property name: str
Return the name of the slice
- property slice_uid: int
Return the UID of the slice
- property uid: int
Return the UID of the slice