Skip to content

Model

Key randomization functions in model.

assay_performed(params)

Number of days between collection and assay being performed.

Parameters:

Name Type Description Default
params AssayParams

assay parameters

required

Returns:

Type Description
timedelta

Number of days.

Source code in src/snailz/model.py
25
26
27
28
29
30
31
32
33
34
def assay_performed(params: AssayParams) -> timedelta:
    """Number of days between collection and assay being performed.

    Parameters:
        params: assay parameters

    Returns:
        Number of days.
    """
    return timedelta(days=random.randint(0, params.delay))

assay_reading(params, specimen, treatment, performed)

Calculate individual assay reading.

Parameters:

Name Type Description Default
params AssayParams

assay parameters

required
specimen object

specimen being assayed

required
treatment str

"C" for control or "S" for sample

required
performed date

date assay performed

required

Returns:

Type Description
float

Reading value.

Source code in src/snailz/model.py
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
62
def assay_reading(
    params: AssayParams, specimen: object, treatment: str, performed: date
) -> float:
    """
    Calculate individual assay reading.

    Parameters:
        params: assay parameters
        specimen: specimen being assayed
        treatment: "C" for control or "S" for sample
        performed: date assay performed

    Returns:
        Reading value.
    """
    degradation = max(
        0.0, 1.0 - (params.degrade * (performed - specimen.collected).days)
    )
    if treatment == "C":
        base_value = 0.0
    elif specimen.is_mutant:
        base_value = params.mutant * degradation
    else:
        base_value = params.baseline * degradation

    return base_value + random.uniform(0.0, params.reading_noise)

assay_specimens(params, specimens)

Generate list of specimens to be assayed.

Parameters:

Name Type Description Default
params AssayParams

assay parameters

required
specimens BaseModel

all available specimens

required

Returns:

Type Description
list

List of specimens (possibly containing duplicates).

Source code in src/snailz/model.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def assay_specimens(params: AssayParams, specimens: BaseModel) -> list:
    """Generate list of specimens to be assayed.

    Parameters:
        params: assay parameters
        specimens: all available specimens

    Returns:
        List of specimens (possibly containing duplicates).
    """
    extra = random.choices(
        specimens.items,
        k=math.floor(params.p_duplicate_assay * len(specimens.items)),
    )
    subjects = specimens.items + extra
    random.shuffle(subjects)
    return subjects

days_to_next_survey(params)

Choose the number of days between surveys.

Parameters:

Name Type Description Default
params SurveyParams

specimen generation parameters

required

Returns:

Type Description
timedelta

Days to the next survey.

Source code in src/snailz/model.py
84
85
86
87
88
89
90
91
92
93
def days_to_next_survey(params: SurveyParams) -> timedelta:
    """Choose the number of days between surveys.

    Parameters:
        params: specimen generation parameters

    Returns:
        Days to the next survey.
    """
    return timedelta(days=random.randint(1, params.max_interval))

image_noise(params, img, img_size)

Add noise effects to image.

Parameters:

Name Type Description Default
img Image

pristine image

required

Returns:

Type Description
Image

Distorted image.

Source code in src/snailz/model.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def image_noise(params: AssayParams, img: PilImage, img_size: int) -> PilImage:
    """Add noise effects to image.

    Parameters:
        img: pristine image

    Returns:
        Distorted image.
    """
    # Add uniform noise (not provided by pillow).
    for x in range(img_size):
        for y in range(img_size):
            noise = random.randint(-params.image_noise, params.image_noise)
            old_val = img.getpixel((x, y))
            assert isinstance(old_val, int)  # for type checking
            val = max(utils.BLACK, min(utils.WHITE, old_val + noise))
            img.putpixel((x, y), val)

    # Blur.
    img = img.filter(ImageFilter.GaussianBlur(BLUR_RADIUS))

    return img

machine_brightness(params)

Choose relative brightness of this machine's camera.

Parameters:

Name Type Description Default
params MachineParams

machine parameters

required

Returns:

Type Description
float

Brightness level in that range.

Source code in src/snailz/model.py
120
121
122
123
124
125
126
127
128
129
130
def machine_brightness(params: MachineParams) -> float:
    """Choose relative brightness of this machine's camera.

    Parameters:
        params: machine parameters

    Returns:
        Brightness level in that range.
    """

    return random.uniform(1.0 - params.variation, 1.0 + params.variation)

mutation_loci(params)

Make a list of mutable loci positions.

Parameters:

Name Type Description Default
params SpecimenParams

specimen generation parameters

required

Returns:

Type Description
list[int]

Randomly selected positions that can be mutated.

Source code in src/snailz/model.py
133
134
135
136
137
138
139
140
141
142
def mutation_loci(params: SpecimenParams) -> list[int]:
    """Make a list of mutable loci positions.

    Parameters:
        params: specimen generation parameters

    Returns:
        Randomly selected positions that can be mutated.
    """
    return list(sorted(random.sample(list(range(params.length)), params.num_mutations)))

specimen_collection_date(survey)

Choose a collection date for a specimen.

Parameters:

Name Type Description Default
survey BaseModel

survey that specimen belongs to

required

Returns:

Type Description
date

Date specimen was collected.

Source code in src/snailz/model.py
145
146
147
148
149
150
151
152
153
154
155
156
def specimen_collection_date(survey: BaseModel) -> date:
    """Choose a collection date for a specimen.

    Parameters:
        survey: survey that specimen belongs to

    Returns:
        Date specimen was collected.
    """
    return date.fromordinal(
        random.randint(survey.start_date.toordinal(), survey.end_date.toordinal())
    )

specimen_genome(specimens)

Generate genome for a particular specimen.

Parameters:

Name Type Description Default
specimens BaseModel

all specimens

required

Returns:

Type Description
str

Random genome produced by mutating reference genome.

Source code in src/snailz/model.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def specimen_genome(specimens: BaseModel) -> str:
    """Generate genome for a particular specimen.

    Parameters:
        specimens: all specimens

    Returns:
        Random genome produced by mutating reference genome.
    """
    genome = list(specimens.reference)
    num_mutations = random.randint(1, len(specimens.loci))
    for loc in random.sample(specimens.loci, num_mutations):
        candidates = list(sorted(set(utils.BASES) - set(specimens.reference[loc])))
        genome[loc] = candidates[random.randrange(len(candidates))]
    return "".join(genome)

specimen_locations(params, size)

Generate locations for specimens.

  • Initialize a set of all possible (x, y) points.
  • Repeatedly choose one at random and add to the result.
  • Remove all points within a random radius of that point.

Parameters:

Name Type Description Default
params SpecimenParams

specimen generation parameters

required
size int

grid size

required

Returns:

Type Description
list[Point]

A list of specimen locations.

Source code in src/snailz/model.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def specimen_locations(params: SpecimenParams, size: int) -> list[Point]:
    """Generate locations for specimens.

    - Initialize a set of all possible (x, y) points.
    - Repeatedly choose one at random and add to the result.
    - Remove all points within a random radius of that point.

    Parameters:
        params: specimen generation parameters
        size: grid size

    Returns:
        A list of specimen locations.
    """

    # Generate points by repeated spatial subtraction.
    available = {(x, y) for x in range(size) for y in range(size)}
    result = []
    while available:
        loc = utils.choose_one(list(available))
        result.append(loc)
        radius = random.uniform(params.spacing / 4, params.spacing)
        span = math.ceil(radius)
        for x in _calculate_span(size, loc[0], span):
            for y in _calculate_span(size, loc[1], span):
                available.discard((x, y))

    # Replace some points with markers for missing data
    missing = Point(x=-1, y=-1)
    return [
        missing
        if random.uniform(0.0, 1.0) < params.p_missing_location
        else Point(x=r[0], y=r[1])
        for r in result
    ]

specimen_mass(params, max_pollution, collected, pollution_level, is_mutant)

Generate mass of a specimen.

Parameters:

Name Type Description Default
params SpecimenParams

specimen generation parameters

required
max_pollution float

maximum pollution level across all surveys

required
collected date

specimen collection date

required
pollution_level float | None

this specimen's pollution level

required
is_mutant bool

whether this specimen is a mutant

required

Returns:

Type Description
float

Random mass.

Source code in src/snailz/model.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def specimen_mass(
    params: SpecimenParams,
    max_pollution: float,
    collected: date,
    pollution_level: float | None,
    is_mutant: bool,
) -> float:
    """Generate mass of a specimen.

    Parameters:
        params: specimen generation parameters
        max_pollution: maximum pollution level across all surveys
        collected: specimen collection date
        pollution_level: this specimen's pollution level
        is_mutant: whether this specimen is a mutant

    Returns:
        Random mass.
    """

    # Initial mass
    mass_scale = params.mut_mass_scale if is_mutant else 1.0
    max_mass = mass_scale * params.max_mass
    mass = random.uniform(max_mass / 2.0, max_mass)

    # Growth effects
    days_passed = (collected - params.start_date).days
    mass += params.daily_growth * days_passed * mass

    # Pollution effects if location known
    if (pollution_level is not None) and (pollution_level > 0.0):
        scaling = 1.0 + 2.0 * utils.sigmoid(pollution_level / max_pollution)
        mass *= scaling

    return mass

_calculate_span(size, coord, span)

Calculate axial range of cells close to a center point.

Parameters:

Name Type Description Default
size int

grid size

required
coord int

X or Y coordinate

required
span int

maximum width on either side

required

Returns:

Type Description
range

Endpoint coordinates of span.

Source code in src/snailz/model.py
250
251
252
253
254
255
256
257
258
259
260
261
262
def _calculate_span(size: int, coord: int, span: int) -> range:
    """
    Calculate axial range of cells close to a center point.

    Parameters:
        size: grid size
        coord: X or Y coordinate
        span: maximum width on either side

    Returns:
        Endpoint coordinates of span.
    """
    return range(max(0, coord - span), 1 + min(size, coord + span))