コード例 #1
0
 def __init__(self, f, **kwargs):
     self.f = f
     self.input_list = utils.input_list(
         f) if "input_list" not in kwargs else kwargs["input_list"]
     self.output_list = utils.output_list(
         f) if "output_list" not in kwargs else kwargs["output_list"]
     self.inputs = set(self.input_list)
     self.outputs = set(self.output_list)
コード例 #2
0
def main():
    """ Main function that takes in the input file of border crossing entry data and returns the desired statistics. """

    # Input and Output files Error-Handling
    args = parse_args()
    if args.input is None:
        raise ImportError('Did not specify the correct input file!')
    if args.output is None:
        raise ImportError('Did not specify the correct output file!')

    # Read in the border_crossing data
    with open(args.input, mode='r') as csv_file:

        # Read the CSV data into a list of lists
        # https://stackoverflow.com/questions/15990456/list-of-lists-vs-dictionary
        csv_reader = csv.reader(csv_file, delimiter=',')

        # Sort the list by Border, Date, and Measure in descending order
        sorted_list = sorted(csv_reader, key=itemgetter(3, 5))

        # Make sure the sorted_list rows are not empty
        if check_all_there(sorted_list):
            pass

        # Let's group the sorted list via the keys--border names, dates, and measures, so
        # that we have a bunch of rows with the same border name, date, measure, but different values!
        # In each row, check if the 6th index (this is our value) is a number and is not 0!
        # If this is true, then add that those values together and create a new list
        # which holds this aggregated summation of values for each border name, date, and measure
        list_with_agg_values = [key + [sum([int(r[6]) for r in rows if r[6].isdigit() and int(r[6]) != 0])]
                                for key, rows in groupby(sorted_list, key=lambda x: x[3:6])]

        # x number of months -- could be a dictionary or int
        num_of_months = count_the_months(list_with_agg_values)

        # calculate the average crossing per month and per measure
        list_with_avg = calculate_average_crossing_per_month_and_measure(num_of_months, list_with_agg_values)

        # Sort the list by Date, Value, Measure, Border in descending order
        sorted_list_with_val_border_measure = sorted(list_with_avg, key=itemgetter(3, 2, 0), reverse=True)
        final_sorted_list = sorted(sorted_list_with_val_border_measure,
                                   key=lambda x: datetime.strptime(x[1], '%d/%m/%Y %H:%M:%S %p'), reverse=True)

    # Write out to the output csv file
    with open(args.output, mode='w') as csv_outfile:
        outfile_writer = csv.writer(csv_outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONE)

        # Column headers--Don't quote them
        outfile_writer.writerow(['Border', 'Date', 'Measure', 'Value', 'Average'])

        outfile_writer = csv.writer(csv_outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

        # for each row in the final list, remove the list of list and create one list
        for row in final_sorted_list:
            outfile_writer.writerow(output_list(row))
コード例 #3
0
    def attach_hetinput(self, hetinput):
        """Make new HetBlock that first processes inputs through function hetinput.
        Assumes 'self' currently does not have hetinput."""
        if self.hetinput is not None:
            raise ValueError('Trying to attach hetinput when it is already there!')

        newself = copy.deepcopy(self)
        newself.hetinput = hetinput
        newself.hetinput_inputs = set(utils.input_list(hetinput))
        newself.hetinput_outputs_order = utils.output_list(hetinput)

        # modify inputs to include hetinput's additional inputs, remove outputs
        newself.inputs |= newself.hetinput_inputs
        newself.inputs -= set(newself.hetinput_outputs_order)
        return newself
コード例 #4
0
    def __init__(self, back_step_fun, exogenous, policy, backward):
        """Construct HetBlock from backward iteration function.

        Parameters
        ----------
        back_step_fun : function
            backward iteration function
        exogenous : str
            names of Markov transition matrix for exogenous variable
            (now only single allowed for simplicity; use Kronecker product for more)
        policy : str or sequence of str
            names of policy variables of endogenous, continuous state variables
            e.g. assets 'a', must be returned by function
        backward : str or sequence of str
            variables that together comprise the 'v' that we use for iterating backward
            must appear both as outputs and as arguments

        It is assumed that every output of the function (except possibly backward), including policy,
        will be on a grid of dimension 1 + len(policy), where the first dimension is the exogenous
        variable and then the remaining dimensions are each of the continuous policy variables, in the
        same order they are listed in 'policy'.

        The Markov transition matrix between the current and future period and backward iteration
        variables should appear in the backward iteration function with '_p' subscripts ("prime") to
        indicate that they come from the next period.

        Currently, we only support up to two policy variables.
        """

        self.back_step_fun = back_step_fun

        self.all_outputs_order = utils.output_list(back_step_fun)
        all_outputs = set(self.all_outputs_order)
        self.all_inputs = set(utils.input_list(back_step_fun))

        self.exogenous = exogenous
        self.policy, self.backward = (utils.make_tuple(x) for x in (policy, backward))

        if len(self.policy) > 2:
            raise ValueError(f"More than two endogenous policies in {back_step_fun.__name__}, not yet supported")

        self.inputs_p = {self.exogenous} | set(self.backward)

        # input checking
        if self.exogenous + '_p' not in self.all_inputs:
            raise ValueError(f"Markov matrix '{self.exogenous}_p' not included as argument in {back_step_fun.__name__}")
        
        for pol in self.policy:
            if pol not in all_outputs:
                raise ValueError(f"Policy '{pol}' not included as output in {back_step_fun.__name__}")

        for back in self.backward:
            if back + '_p' not in self.all_inputs:
                raise ValueError(f"Backward variable '{back}_p' not included as argument in {back_step_fun.__name__}")

            if back not in all_outputs:
                raise ValueError(f"Backward variable '{back}' not included as output in {back_step_fun.__name__}")

        self.non_back_outputs = all_outputs - set(self.backward)
        for out in self.non_back_outputs:
            if out.isupper():
                raise ValueError("Output '{out}' is uppercase in {back_step_fun.__name__}, not allowed")

        # aggregate outputs and inputs for utils.block_sort
        self.inputs = self.all_inputs - {k + '_p' for k in self.backward}
        self.inputs.remove(exogenous + '_p')
        self.inputs.add(exogenous)
        
        self.outputs = {k.upper() for k in self.non_back_outputs}

        # start without a hetinput
        self.hetinput = None
        self.hetinput_inputs = set()
        self.hetinput_outputs_order = tuple()

        # 'saved' arguments start empty
        self.saved = {}
        self.prelim_saved = {}
        self.saved_shock_list = []
        self.saved_output_list = []
コード例 #5
0
 def __init__(self, f):
     self.f = f
     self.input_list = utils.input_list(f)
     self.output_list = utils.output_list(f)
     self.inputs = set(self.input_list)
     self.outputs = set(self.output_list)