myco.utils¶
Helper functions used by multiple modules or don't have a clear home.
AttributeDictionary
¶
Bases: dict
Extends a dictionary to index keys as attributes.
Source code in myco/utils.py
318 319 320 321 322 323 |
|
augment_feature_patch(feature)
¶
Apply random feature augmentations (flipping and rotating)
Source code in myco/utils.py
331 332 333 334 335 336 337 338 339 340 |
|
balance_samples(samples, class_weight)
¶
Over and undersamples classification data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples |
np.array
|
array of shape (height, width, n_classes) to get samples from |
required |
class_weight |
dict
|
the proportional distribution of each class |
required |
add_samples |
??? |
required |
Returns:
Type | Description |
---|---|
list
|
a list of response data sample indices to include in model training |
Source code in myco/utils.py
208 209 210 211 212 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 |
|
compute_class_weights(response)
¶
Computes weights per class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
np.ndarray
|
one-hot encoded tensor of response data |
required |
Returns:
Type | Description |
---|---|
dict
|
a vector of weights per class |
Source code in myco/utils.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
compute_sample_weights(class_weights, response)
¶
Computes weights per sample.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_weights |
np.ndarray
|
weights of each class |
required |
response |
np.ndarray
|
one-hot encoded tensor of response data |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
2D array of sample weights |
Source code in myco/utils.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
create_classes(pred_score)
¶
Reads the scores of one hot encoded data and finds the class value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_score |
np.ndarray
|
matrix of scores |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
class-labeled array cells. dim 2 is reduced from num-classes to 1 |
Source code in myco/utils.py
195 196 197 198 199 200 201 202 203 204 205 |
|
find_yaml_files(filepath, yaml_ext)
¶
Search for yaml files affiliated with a numpy archive
Source code in myco/utils.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
load_object(path, compressed=True)
¶
Reads a python object that's been saved to disk or to GCS
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
file path of the object to load |
required |
compressed |
bool
|
whether the file was compressed prior to saving |
True
|
Returns:
Name | Type | Description |
---|---|---|
obj |
Any
|
the original python object (e.g., a fit NetworkScaler() object) |
Source code in myco/utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
n_digits(number)
¶
Counts the number of significant integer digits of a number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number |
Union[int, float]
|
the number to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
order |
int
|
number of digits required to represent a number |
Source code in myco/utils.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
read_archive_data(filepath, mode='r', ext='yml')
¶
Reads a numpy memmap archive based on an associated header yaml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
path to the numpy archive to read. automatically searches |
required |
mode |
str
|
the mode for opening the memmap (in 'r', 'r+', 'w+': see |
'r'
|
ext |
str
|
the filename extension to search for based on the input filepath. |
'yml'
|
Returns:
Type | Description |
---|---|
np.ndarray
|
The numpy array as a memmap object. |
Source code in myco/utils.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
read_archive_header(filepath, ext='yml')
¶
Reads archive metadata information from a yaml file.
Source code in myco/utils.py
167 168 169 170 |
|
read_yaml(filepath)
¶
Read a yaml file into memory as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
path to a local .yml/.yaml file. |
required |
Returns:
Type | Description |
---|---|
dict
|
A parsed dictionary with the yaml file's contents. |
Source code in myco/utils.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
revert_augmentation(pred)
¶
Inverse transformation of the augment_feature_path() function.
Source code in myco/utils.py
343 344 345 346 347 348 349 350 351 |
|
save_object(obj, path, compress=True)
¶
Writes a python object to disk or GCS for later access
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
object
|
a python object to be saved (e.g. a variable or a class) |
required |
path |
str
|
the output file path |
required |
compress |
bool
|
flag to specify whether the file should be compressed |
True
|
Source code in myco/utils.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
write_archive_header(filepath, archive=None, shape=None, dtype=None, nodata=None, tfrecords=[])
¶
Write a numpy memmap archive header yaml file.
You can either get the shape/dtype/nodata info by passing the array as the
archive
keyword or by passing shape
and dtype
as keywords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
path to an output .yml file. |
required |
archive |
np.ndarray
|
a numpy array to write the header for. |
None
|
shape |
tuple
|
dimensions of the numpy array |
None
|
dtype |
str
|
the numpy datatype of the array |
None
|
nodata |
float
|
the nodata value to ignore |
None
|
tfrecords |
list
|
list of tfrecord filepaths |
[]
|
Returns:
Type | Description |
---|---|
None
|
None. Writes a yaml file to |
Source code in myco/utils.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
write_yaml(data, filepath)
¶
Write a dictionary to a yaml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
dictionary of values to write. |
required |
filepath |
str
|
path to an output .yml/.yaml file. |
required |
Returns:
Type | Description |
---|---|
None
|
None. Writes to disk. |
Source code in myco/utils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 |
|