-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
107 lines (94 loc) · 3.5 KB
/
utils.py
File metadata and controls
107 lines (94 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from datetime import datetime
import random
import string
import numpy as np
import math
from random import uniform, gauss, triangular
def get_name(length):
timestamp = datetime.now().strftime("%H%M%S")
unique = ""
for _ in range(length):
unique += random.choice(string.ascii_letters)
return f"{timestamp}{unique}"
def get_random(bits: int, distribution='uniform', samples=1, **kwargs):
'''
Generates samples of integer randomly distributed data.
Parameters
----------
bits: int
Number of bits for the integer data.
distribution: string
Name of the desired random distribution. Could be:
"gaussian" or "normal" for a normal distribution.
"uniform" or "rectangular" for a uniform distribution.
"triangular" for a triangular distribution.
TODO: Add more distributions
samples: int
Number of samples.
**kwargs (optional)
median: int
The center of the distribution (works only for certain distributions)
std: int
Standard deviation of the destribution (only gaussian/normal distribution)
limits: int tuple
Lower and upper limit of the dataset. By default it takes the whole range of numbers: [0,2^n-1]
Returns
-------
data
Randomized data sampled from the specified random distribution
'''
'''Pasing kwargs'''
if 'low_limit' not in kwargs:
low_limit=0 #Lower threshold for generated numbers
else:
low_limit=np.min([kwargs['low_limit'],2**bits])
if 'high_limit' not in kwargs:
high_limit=2**bits #Upper threshold for the generated data
else:
high_limit=np.min([kwargs['high_limit'],2**bits])
if 'median' not in kwargs:
median=(high_limit+low_limit)/2 #by default is centered at the mean
else:
median=kwargs['median']
if 'variance' not in kwargs:
variance=1
else:
variance=kwargs['variance']
'''Distributions case'''
data=[]
if distribution in {'uniform', 'rectangular'}:
data=(int(math.floor(uniform(low_limit,high_limit))) for _ in range(samples))
elif distribution=='triangular':
data=(int(math.floor(triangular(low_limit,high_limit,mode=median))) for _ in range(samples))
elif distribution in {'normal', 'gaussian'}:
while len(data)<samples:
random_value=int(math.floor(gauss(median,variance)))
if low_limit<=random_value<=high_limit:
data.append(random_value)
else:
raise ValueError(f'{distribution} is not a valid distribution name')
return data
def read_dataset(filename, base, max_lines=None):
"""
Reads a dataset or circuit output file like those generated by the
`Circuit.generate_dataset` file or `Circuit.exact_output`.
TODO: This function should be used by circuiterror.py.
Parameters
----------
filename : string
The path to the file to be read.
base : int
The base of the numbers used by the file, common values are 16
(hexadecimal) and 10 (decimal).
max_lines : None | int
The maximum amount of lines to read, in case the user doesn't want to
use the entire dataset.
"""
with open(filename, "r") as f:
if max_lines is not None:
return [
[int(x, base) for x in line.split()]
for _, line in zip(range(max_lines), f)
]
else:
return [[int(x, base) for x in line.split()] for line in f]