|
| 1 | +import json |
| 2 | +import os |
| 3 | +from os.path import exists, abspath |
| 4 | +import numpy as np |
| 5 | +from stattest.experiment._distribution_type_enum import Distribution |
| 6 | + |
| 7 | + |
| 8 | +def generate_samples(dist_type: Distribution = None, |
| 9 | + number: int = None, |
| 10 | + start_size: int = None, |
| 11 | + final_size: int = None, |
| 12 | + step: int = None, |
| 13 | + path: str = None): |
| 14 | + """ |
| 15 | + Generates samples based on parameters. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + dist_type : Distribution |
| 20 | + Enum value representing distribution type. |
| 21 | + number : int |
| 22 | + Number of samples of each size. |
| 23 | + start_size : int |
| 24 | + Start size of the samples. |
| 25 | + final_size : int |
| 26 | + Final size of the samples. |
| 27 | + step : int |
| 28 | + Step of the iteration. |
| 29 | + path : str |
| 30 | + Path to save JSON file to. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + True |
| 35 | + """ |
| 36 | + path = path if path is not None else os.getcwd() |
| 37 | + |
| 38 | + all_types = dist_type is None |
| 39 | + |
| 40 | + filename = f"{'all' if all_types else dist_type.value}_{number}_{start_size}_{final_size}_{step}" |
| 41 | + if exists(f"{path}/{filename}.json"): |
| 42 | + raise FileExistsError("Such samples already exist") |
| 43 | + |
| 44 | + samples_by_size = { |
| 45 | + size: [None for _ in range(number)] |
| 46 | + for size in range(start_size, final_size + 1, step) |
| 47 | + } |
| 48 | + samples = { |
| 49 | + type_.value: samples_by_size for type_ in Distribution |
| 50 | + } if all_types else {dist_type.value: samples_by_size} |
| 51 | + |
| 52 | + for size in range(start_size, final_size + 1, step): |
| 53 | + for i in range(number): |
| 54 | + if all_types or dist_type is Distribution.no_type: |
| 55 | + sample = np.random.random_sample(size=size) |
| 56 | + samples[dist_type.value][size][i] = list(sample) |
| 57 | + |
| 58 | + if all_types or dist_type is Distribution.normal: |
| 59 | + sample = np.random.normal(loc=0, scale=1, size=size) |
| 60 | + samples[dist_type.value][size][i] = list(sample) |
| 61 | + |
| 62 | + if all_types or dist_type is Distribution.exponential: |
| 63 | + sample = np.random.exponential(scale=1, size=size) |
| 64 | + samples[dist_type.value][size][i] = list(sample) |
| 65 | + |
| 66 | + save_file = open(f"{path}/{filename}.json", "w") |
| 67 | + json.dump(samples, save_file, indent=4) |
| 68 | + save_file.close() |
| 69 | + |
| 70 | + return True |
0 commit comments