Sample - Snailz Docs

Skip to content

# Sample

Samples.

## `Sample`

Bases: `BaseModel`

Represent a single sample.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`sample_id` | `str` | unique ID | required
`grid_id` | `str` | grid ID | required
`x` | `int` | X coordinate | `0`
`y` | `int` | Y coordinate | `0`
`person_id` | `str` | collector | required
`timestamp` | `date` | when sample was collected | required
`mass` | `float` | sample mass | required
`diameter` | `float` | sample diameter | required

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

    11
    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
    60
    61
    class Sample(BaseModel):
        """Represent a single sample."""

        _id_gen: ClassVar[utils.id_gen] = utils.id_gen("S", 4)

        model_config = ConfigDict(
            json_schema_extra={
                "foreign_key": {
                    "grid_id": ("grid", "grid_id"),
                    "person_id": ("person", "person_id"),
                }
            }
        )

        sample_id: str = Field(
            min_length=1, description="unique ID", json_schema_extra={"primary_key": True}
        )
        grid_id: str = Field(min_length=1, description="grid ID")
        x: int = Field(default=0, ge=0, description="X coordinate")
        y: int = Field(default=0, ge=0, description="Y coordinate")
        person_id: str = Field(description="collector")
        timestamp: date = Field(description="when sample was collected")
        mass: float = Field(gt=0.0, description="sample mass")
        diameter: float = Field(gt=0.0, description="sample diameter")

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

            result = []
            for _ in range(params.num_samples):
                grid = random.choice(grids)
                x = random.randint(0, grid.size - 1)
                y = random.randint(0, grid.size - 1)
                person = random.choice(persons)
                timestamp = utils.random_date(params)
                mass, diameter = utils.random_size(params)
                result.append(
                    Sample(
                        sample_id=next(Sample._id_gen),
                        grid_id=grid.grid_id,
                        x=x,
                        y=y,
                        person_id=person.person_id,
                        timestamp=timestamp,
                        mass=mass,
                        diameter=diameter,
                    )
                )

            return result

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

Make a sample.

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

    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    @staticmethod
    def make(params, grids, persons):
        """Make a sample."""

        result = []
        for _ in range(params.num_samples):
            grid = random.choice(grids)
            x = random.randint(0, grid.size - 1)
            y = random.randint(0, grid.size - 1)
            person = random.choice(persons)
            timestamp = utils.random_date(params)
            mass, diameter = utils.random_size(params)
            result.append(
                Sample(
                    sample_id=next(Sample._id_gen),
                    grid_id=grid.grid_id,
                    x=x,
                    y=y,
                    person_id=person.person_id,
                    timestamp=timestamp,
                    mass=mass,
                    diameter=diameter,
                )
            )

        return result