easyvvuq.sampling.csv_sampler
A CSV file based sampler.
Useful for cases where you want to evaluate a sampling plan generated by other software. Will take a CSV file with an appropriate header and will output it row by row.
1"""A CSV file based sampler. 2 3Useful for cases where you want to evaluate a sampling plan generated by other software. 4Will take a CSV file with an appropriate header and will output it row by row. 5""" 6 7from .base import BaseSamplingElement 8import csv 9 10__license__ = "LGPL" 11 12 13class CSVSampler(BaseSamplingElement, sampler_name="csv_sampler"): 14 15 def __init__(self, filename, counter=0): 16 """ 17 Expects dict of var names, and their corresponding distributions 18 """ 19 self.data = [] 20 self.filename = filename 21 self.counter = counter 22 try: 23 with open(filename, 'r') as fd: 24 reader = csv.DictReader(fd) 25 for row in reader: 26 self.data.append(row) 27 except FileNotFoundError: 28 raise RuntimeError("CSV file you specified ({}) does not exist".format(filename)) 29 30 def is_finite(self): 31 return True 32 33 def n_samples(self): 34 """Returns the number of samples in this sampler. 35 Returns 36 ------- 37 if the user specifies maximum number of samples than return that, otherwise - error 38 """ 39 return len(self.data) 40 41 def __next__(self): 42 try: 43 return self.data[self.counter] 44 finally: 45 if self.counter < self.n_samples(): 46 self.counter += 1 47 else: 48 raise StopIteration
14class CSVSampler(BaseSamplingElement, sampler_name="csv_sampler"): 15 16 def __init__(self, filename, counter=0): 17 """ 18 Expects dict of var names, and their corresponding distributions 19 """ 20 self.data = [] 21 self.filename = filename 22 self.counter = counter 23 try: 24 with open(filename, 'r') as fd: 25 reader = csv.DictReader(fd) 26 for row in reader: 27 self.data.append(row) 28 except FileNotFoundError: 29 raise RuntimeError("CSV file you specified ({}) does not exist".format(filename)) 30 31 def is_finite(self): 32 return True 33 34 def n_samples(self): 35 """Returns the number of samples in this sampler. 36 Returns 37 ------- 38 if the user specifies maximum number of samples than return that, otherwise - error 39 """ 40 return len(self.data) 41 42 def __next__(self): 43 try: 44 return self.data[self.counter] 45 finally: 46 if self.counter < self.n_samples(): 47 self.counter += 1 48 else: 49 raise StopIteration
Baseclass for all EasyVVUQ sampling elements.
Attributes
- sampler_name (str): Name of the particular sampler.
CSVSampler(filename, counter=0)
16 def __init__(self, filename, counter=0): 17 """ 18 Expects dict of var names, and their corresponding distributions 19 """ 20 self.data = [] 21 self.filename = filename 22 self.counter = counter 23 try: 24 with open(filename, 'r') as fd: 25 reader = csv.DictReader(fd) 26 for row in reader: 27 self.data.append(row) 28 except FileNotFoundError: 29 raise RuntimeError("CSV file you specified ({}) does not exist".format(filename))
Expects dict of var names, and their corresponding distributions
def
n_samples(self):
34 def n_samples(self): 35 """Returns the number of samples in this sampler. 36 Returns 37 ------- 38 if the user specifies maximum number of samples than return that, otherwise - error 39 """ 40 return len(self.data)
Returns the number of samples in this sampler.
Returns
- if the user specifies maximum number of samples than return that, otherwise - error