Esempio n. 1
0
    def import_propensities_from_flows(self, flows_df, inplace=False):
        """
        Calculate a set of import propensities from `flows_df`

        The input DataFrame must be indexed with ['sector', 'from_country',
        'to_country'].  Any ['sector', 'to_country'] combinations which sum to
        less than 1 have the remainder assigned to RoW
        """
        sum_y = flows_df.groupby(level=['to_country', 'sector']).sum()
        some_ip = dataframe.broadcast(flows_df, sum_y, binary_operator='div')
        # Re-include any sectors dropped due to divide by zero or not included
        # in flows_df at all:
        all_ip = some_ip.reindex(self._import_propensities.index)
        all_ip[all_ip.isnull()] = self._import_propensities
        # Set from RoW propensity to 1 anywhere the total over to_country and
        # sector is zero
        # TODO: This doesn't work yet!
        #sums = all_ip.groupby(level=['sector', 'to_country']).sum()
        #fixed_ip = all_ip.unstack('from_country')
        #fixed_ip['RoW'] = 1 - sums
        #fixed_ip.loc[pd.IndexSlice[:, 'RoW'], 'RoW'] = 0 # Set RoW/RoW to zero
        #fixed_ip = fixed_ip.stack()
        #fixed_ip = fixed_ip.reorder_levels(['sector', 'from_country', 'to_country']).sortlevel()
        if inplace:
            self._import_propensities = all_ip
        return all_ip
 def _split_flows_by_import_propensities(self, flows,
                                         country_field='country',
                                         country_names=None,
                                         with_RoW=False):
     """
     Calculate country to country flows using
     the given flows vector and the import propensities
     """
     ip = self._import_propensities.astype(float)
     return dataframe.broadcast(self._import_propensities, flows)
Esempio n. 3
0
def _import_propensities_from_flows(flows_df):
    """
    Calculate y_ijs / sum_i(y_ijs) for every flow in flows_df
    """
    sum_y = flows_df.groupby(level=["to_country", "sector"]).sum()
    ip = dataframe.broadcast(flows_df, sum_y, binary_operator="div")
    # Re-include any sectors dropped due to divide by zero:
    ip = ip.reindex(flows_df.index).fillna(0)
    # Set from RoW propensity to 1 anywhere the total over to_country and sector is zero
    sums = ip.groupby(level=["to_country", "sector"]).sum()
    zero_sums = sums[sums == 0]
    ip.loc[[("RoW",) + idx for idx in zero_sums.index.values]] = 1
    return ip.reorder_levels(["sector", "from_country", "to_country"]).sortlevel()