Skip to content

Params

Parameter dataclasses and utilities.

AssayParams dataclass

Parameters for assay data generation.

Source code in snailz/params.py
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
62
63
64
65
66
@dataclass
class AssayParams:
    '''Parameters for assay data generation.'''

    assay_staff: list
    assay_types: list
    assay_duration: list
    assay_plates: list
    control_val: float = 5.0
    controls: List[str] = field(default_factory=list)
    enddate: str = None
    experiments: int = 1
    filename_length: int = 8
    fraction: float = None
    invalid: float = 0.1
    seed: int = None
    startdate: str = None
    stdev: float = 3.0
    treated_val: float = 8.0
    treatment: str = None

    def __post_init__(self):
        '''Fill in missing dates and convert to standard format.'''
        if self.startdate is None:
            self.startdate = DEFAULT_START_DATE
        elif isinstance(self.startdate, str):
            self.startdate = datetime.strptime(self.startdate, DATE_FORMAT).replace(tzinfo=pytz.UTC)
        elif not self.startdate.tzinfo:
            self.startdate = self.startdate.replace(tzinfo=pytz.UTC)

        if self.enddate is None:
            self.enddate = DEFAULT_END_DATE
        elif isinstance(self.enddate, str):
            self.enddate = datetime.strptime(self.enddate, DATE_FORMAT).replace(tzinfo=pytz.UTC)
        elif not self.enddate.tzinfo:
            self.enddate = self.enddate.replace(tzinfo=pytz.UTC)

__post_init__()

Fill in missing dates and convert to standard format.

Source code in snailz/params.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __post_init__(self):
    '''Fill in missing dates and convert to standard format.'''
    if self.startdate is None:
        self.startdate = DEFAULT_START_DATE
    elif isinstance(self.startdate, str):
        self.startdate = datetime.strptime(self.startdate, DATE_FORMAT).replace(tzinfo=pytz.UTC)
    elif not self.startdate.tzinfo:
        self.startdate = self.startdate.replace(tzinfo=pytz.UTC)

    if self.enddate is None:
        self.enddate = DEFAULT_END_DATE
    elif isinstance(self.enddate, str):
        self.enddate = datetime.strptime(self.enddate, DATE_FORMAT).replace(tzinfo=pytz.UTC)
    elif not self.enddate.tzinfo:
        self.enddate = self.enddate.replace(tzinfo=pytz.UTC)

GenomeParams dataclass

Gene sequence parameters.

Source code in snailz/params.py
69
70
71
72
73
74
75
76
77
@dataclass
class GenomeParams:
    '''Gene sequence parameters.'''
    snp_probs: list
    length: int
    num_genomes: int
    num_snp: int
    prob_other: float
    seed: int = None

GridParams dataclass

Invasion percolation parameters.

Source code in snailz/params.py
80
81
82
83
84
85
86
@dataclass
class GridParams:
    '''Invasion percolation parameters.'''
    depth: int
    height: int
    seed: int
    width: int

SampleParams dataclass

Sampled snail parameters.

Source code in snailz/params.py
89
90
91
92
93
94
95
96
@dataclass
class SampleParams:
    '''Sampled snail parameters.'''
    min_snail_size: float = None
    max_snail_size: float = None
    mutant: float = None
    normal: float = None
    seed: int = None

StaffParams dataclass

Staff parameters.

Source code in snailz/params.py
 99
100
101
102
103
104
@dataclass
class StaffParams:
    '''Staff parameters.'''
    locale: str = 'et_EE'
    num: int = None
    seed: int = None

export_params(options)

Export parameter files.

This function is used to write default parameter files when the package is installed.

  • options.outdir: directory to write to.
Source code in snailz/params.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def export_params(options: Namespace) -> None:
    '''Export parameter files.

    This function is used to write default parameter files when the package is installed.

    -   options.outdir: directory to write to.
    '''
    outdir = Path(options.outdir)
    outdir.mkdir(parents=True, exist_ok=True)
    root = files(__name__.split('.')[0])
    for filename in PARAMETER_FILES:
        src = root.joinpath(filename)
        dst = outdir.joinpath(Path(filename).name)
        dst.write_bytes(src.read_bytes())

load_params(cls, filename)

Load parameters from file and return as object.

Parameters:

Name Type Description Default
cls type

class to instantiate.

required
filename str

file containing JSON representation of parameters.

required

Returns:

Type Description
object

Populated instance of the given class.

Source code in snailz/params.py
123
124
125
126
127
128
129
130
131
132
133
def load_params(cls: type, filename: str) -> object:
    '''Load parameters from file and return as object.

    Args:
        cls: class to instantiate.
        filename: file containing JSON representation of parameters.

    Returns:
        Populated instance of the given class.
    '''
    return cls(**json.loads(Path(filename).read_text()))