def __init__(self, csv_file_name):
        """
        :param csv_file_name: csv file where the parameter samples are located
        assumes that the first row of this csv file contains the parameter names
        to be used as the keys of the dictionary of parameters it creates
        """

        # create a dictionary of parameter samples
        self.dictOfParams = IO.read_csv_cols_to_dictionary(
            csv_file_name, ',', True)
예제 #2
0
    def __init__(self, csvfile_param_values=None, columns_to_be_deleted=()):
        """
        :param csvfile_param_values: (string) csv file where the parameter values are located
            assumes that the first row of this csv file contains the parameter names and each
            column contains the parameter values
        :param columns_to_be_deleted: (list of string) list of column names to be deleted from analysis
        """

        # dictionary of parameter samples with parameter names as keys
        self.dictOfParamValues = {}

        if csvfile_param_values is not None:
            self.dictOfParamValues = IO.read_csv_cols_to_dictionary(file_name=csvfile_param_values,
                                                                    if_convert_float=True)
            for name in columns_to_be_deleted:
                del(self.dictOfParamValues[name])
예제 #3
0
import SimPy.InOutFunctions as IO
from apace import ScenariosClasses as Sce

scenario_keys = [
    'Base', '75% PTFU | No >1 FU | Drop % | No IPT',
    '75% PTFU | With >1 FU | Drop 15% | No IPT',
    '75% PTFU | No >1 FU | Drop % | With IPT',
    '75% PTFU | With >1 FU | Drop 15% | With IPT'
]

# data frame for scenario analysis
scenario_df = Sce.ScenarioDataFrame('csv_files\TBScenarios.csv')

# read parameter samples
parameter_values = IO.read_csv_cols_to_dictionary(
    file_name='csv_files/SampledParams.csv',
    delimiter=',',
    if_convert_float=True)

# create a dictionaries of DALYs and cost
dict_DALY = {}
dict_cost = {}
for key in scenario_keys:
    dict_DALY[key] = scenario_df.scenarios[key].outcomes['DALY']
    dict_cost[key] = scenario_df.scenarios[key].outcomes['Total Cost']

# create dictionaries for dDALYS and dCost
dict_dDALY = {}
dict_dCost = {}
for key in scenario_keys:
    if key != 'Base':
        dict_dDALY[key] = scenario_df.scenarios['Base'].outcomes['DALY'] \
예제 #4
0
from SimPy import InOutFunctions as InOutSupport

# test reading by rows
rows = InOutSupport.read_csv_rows('myCSV',
                                  if_del_first_row=True,
                                  if_convert_float=True)
print('Testing reading by rows:')
for row in rows:
    print(sum(row))

# test reading by columns
cols = InOutSupport.read_csv_cols('myCSV',
                                  n_cols=3,
                                  if_ignore_first_row=True,
                                  if_convert_float=True)
print('Testing reading by columns:')
for j in range(0, 3):
    print(sum(cols[j]))

# rest reading by columns into a dictionary
dict_cols = InOutSupport.read_csv_cols_to_dictionary('myCSV',
                                                     if_convert_float=True)
print('Testing reading by columns into a dictionary:')
print(dict_cols)
예제 #5
0
from SimPy import InOutFunctions as io

# ---------------
# first run the TestCSVWritter.py to produce the csv file
# ---------------

# test reading by rows
rows = io.read_csv_rows('CSVFolder/myCSV.csv',
                        if_ignore_first_row=True,
                        if_convert_float=True)
print('Testing reading by rows:')
for row in rows:
    print(sum(row[1:]))

# test reading by columns
cols = io.read_csv_cols('CSVFolder/myCSV.csv',
                        n_cols=4,
                        if_ignore_first_row=True,
                        if_convert_float=True)
print('Testing reading by columns:')
for j in range(1, 4):
    print(sum(cols[j]))

# rest reading by columns into a dictionary
dict_cols = io.read_csv_cols_to_dictionary('CSVFolder/myCSV.csv',
                                           if_convert_float=True)

print('Testing reading by columns into a dictionary:')
print(dict_cols)
예제 #6
0
    def resample_param_values(self, csvfile_param_values_and_weights,
                              n, weight_col, csvfile_resampled_params,
                              sample_by_weight=True, columns_to_be_deleted=(), seed=0):
        """
        :param csvfile_param_values_and_weights: (string) csv file where the values of parameters
            along with their weights are provided.
            It assumes that
                1) the first row contains the name of all parameters
                2) rows contains the weight and the parameter values
                3) rows are in decreasing order of parameter weights
        :param n: (int) number of parameter values to resamples
        :param weight_col: (int) index of the columns where the weights of parameter values are located.
        :param csvfile_resampled_params: (string) csvfile where the resampled parameter values will be stored.
            The first row will be the names of parameters.
        :param sample_by_weight: (bool) set to true to sample parameters by weight.
            If set to False, the first n parameters will be selected.
        :param columns_to_be_deleted: (list of string) list of column names to be deleted from analysis
        :param seed: (int) seed of the random number generator to resample parameters
        """

        # read parameter weights and values
        rows_of_weights_and_parameter_values = IO.read_csv_rows(
            file_name=csvfile_param_values_and_weights,
            if_ignore_first_row=False,
            if_convert_float=True)

        # store header
        rows_of_selected_param_values = [rows_of_weights_and_parameter_values[0]]

        if not sample_by_weight:
            # choose the first n parameter values
            for i, row in enumerate(rows_of_weights_and_parameter_values):
                if i > n: # first rwo is the header
                    break
                elif i > 0 and row[weight_col] > 0:
                    rows_of_selected_param_values.append(row)
        else:
            # weight
            weights = []
            for row in rows_of_weights_and_parameter_values:
                weights.append(row[weight_col])
            del(weights[0]) # header

            # sample rows
            rng = np.random.RandomState(seed=seed)
            sampled_indices = rng.choice(a=range(len(weights)), size=n, p=weights)

            # build sampled rows
            for i in sampled_indices:
                rows_of_selected_param_values.append(rows_of_weights_and_parameter_values[i+1])

        IO.write_csv(rows=rows_of_selected_param_values, file_name=csvfile_resampled_params)

        self.dictOfParamValues = IO.read_csv_cols_to_dictionary(file_name=csvfile_resampled_params,
                                                                if_convert_float=True)
        # check if parameters have values
        for key, values in self.dictOfParamValues.items():
            if len(values) == 0:
                raise ValueError('Parameter values are not provided in '+csvfile_resampled_params+'.')

        for name in columns_to_be_deleted:
            del (self.dictOfParamValues[name])