Sample - Snailz Docs

Skip to content

# Sample

Samples.

## `Sample`

Bases: `BaseModel`

Represent a single sample.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`id` | `str` | unique ID | required
`grid` | `str` | grid ID | required
`x` | `int` | X coordinate | required
`y` | `int` | Y coordinate | required
`pollution` | `int` | pollution reading at grid cell | required
`person` | `str` | collector | required
`when` | `date` | when sample was collected | required
`mass` | `float` | sample mass | required

Source code in `src/snailz/sample.py`

    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    class Sample(BaseModel):
        """Represent a single sample."""

        id_stem: ClassVar[str] = "S"
        id_digits: ClassVar[int] = 4

        id: str = Field(min_length=1, description="unique ID")
        grid: str = Field(min_length=1, description="grid ID")
        x: int = Field(ge=0, description="X coordinate")
        y: int = Field(ge=0, description="Y coordinate")
        pollution: int = Field(ge=0, description="pollution reading at grid cell")
        person: str = Field(description="collector")
        when: date = Field(description="when sample was collected")
        mass: float = Field(gt=0.0, description="sample mass")

        @staticmethod
        def make(params, grids, persons):
            """Make a sample."""

            utils.ensure_id_generator(Sample)
            grid = random.choice(grids)
            x = random.randint(0, grid.size - 1)
            y = random.randint(0, grid.size - 1)
            pollution = grid[x, y]
            person = random.choice(persons)
            when = utils.random_date(params)
            mass = utils.random_mass(params)
            return Sample(
                id=next(Sample._id_gen),
                grid=grid.id,
                x=x,
                y=y,
                pollution=pollution,
                person=person.id,
                when=when,
                mass=mass,
            )

        @staticmethod
        def csv_header():
            """Generate header for CSV file."""

            return "sample_id,grid_id,x,y,pollution,person,when,mass"

        def __str__(self):
            """Convert to CSV string."""

            return f"{self.id},{self.grid},{self.x},{self.y},{self.pollution},{self.person},{self.when},{self.mass}"

### `make(params, grids, persons)` `staticmethod`

Make a sample.

Source code in `src/snailz/sample.py`

    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    @staticmethod
    def make(params, grids, persons):
        """Make a sample."""

        utils.ensure_id_generator(Sample)
        grid = random.choice(grids)
        x = random.randint(0, grid.size - 1)
        y = random.randint(0, grid.size - 1)
        pollution = grid[x, y]
        person = random.choice(persons)
        when = utils.random_date(params)
        mass = utils.random_mass(params)
        return Sample(
            id=next(Sample._id_gen),
            grid=grid.id,
            x=x,
            y=y,
            pollution=pollution,
            person=person.id,
            when=when,
            mass=mass,
        )

### `csv_header()` `staticmethod`

Generate header for CSV file.

Source code in `src/snailz/sample.py`

    50
    51
    52
    53
    54
    @staticmethod
    def csv_header():
        """Generate header for CSV file."""

        return "sample_id,grid_id,x,y,pollution,person,when,mass"

### `__str__()`

Convert to CSV string.

Source code in `src/snailz/sample.py`

    56
    57
    58
    59
    def __str__(self):
        """Convert to CSV string."""

        return f"{self.id},{self.grid},{self.x},{self.y},{self.pollution},{self.person},{self.when},{self.mass}"