Example #1
0
    def initiate_active_shapes(self):
        logging.info(' reading data for:')

        if cfg.cfgfile.get('case', 'parallel_process').lower() == 'true':
            shapes = helper_multiprocess.safe_pool(helper_multiprocess.shapes_populate, self.data.values())
            self.data = dict(zip(self.data.keys(), shapes))
        else:
            for id in self.active_shape_ids:
                shape = self.data[id]
                logging.info('    shape: ' + shape.name)
                if hasattr(shape, 'raw_values'):
                    return
                shape.read_timeseries_data()

        for id in self.active_shape_ids:
            shape = self.data[id]
            if shape.shape_type=='weather date':
                shape.convert_index_to_datetime('raw_values', 'weather_datetime')
                date_position = util.position_in_index(shape.raw_values, 'weather_datetime')
                
                shape.start_date, shape.end_date = min(shape.raw_values.index.levels[date_position]), max(shape.raw_values.index.levels[date_position])
                
                self.start_date = shape.start_date if self.start_date is None else max(shape.start_date, self.start_date)
                self.end_date = shape.end_date if self.end_date is None else min(shape.end_date, self.end_date)
        self.set_active_dates()
Example #2
0
    def initiate_active_shapes(self):
        print ' reading data for:'
        for id in self.active_shape_ids:
            shape = self.data[id]
            print '     shape: ' + shape.name
            if hasattr(shape, 'raw_values'):
                return

            shape.read_timeseries_data()

            if shape.shape_type == 'weather date':
                shape.convert_index_to_datetime('raw_values',
                                                'weather_datetime')
                date_position = util.position_in_index(shape.raw_values,
                                                       'weather_datetime')

                shape.start_date, shape.end_date = min(
                    shape.raw_values.index.levels[date_position]), max(
                        shape.raw_values.index.levels[date_position])

                self.start_date = shape.start_date if self.start_date is None else max(
                    shape.start_date, self.start_date)
                self.end_date = shape.end_date if self.end_date is None else min(
                    shape.end_date, self.end_date)
        self.set_active_dates()
Example #3
0
    def remap(self, map_from='raw_values', map_to='values', drivers=None, time_index_name='year',
              time_index=None, fill_timeseries=True, interpolation_method='missing', extrapolation_method='missing',
              converted_geography=None, current_geography=None, current_data_type=None, fill_value=0., lower=0, upper=None):
        """ Map data to drivers and geography
        Args:
            map_from (string): starting variable name (defaults to 'raw_values')
            map_to (string): ending variable name (defaults to 'values')
            drivers (list of or single dataframe): drivers for the remap
            input_type_override (string): either 'total' or 'intensity' (defaults to self.type)
        """
        converted_geography = cfg.cfgfile.get('case', 'primary_geography') if converted_geography is None else converted_geography
        current_data_type = self.input_type if current_data_type is None else current_data_type
        current_geography = self.geography if current_geography is None else current_geography
        # TODO fix pluralization
        if time_index is None:
            time_index = getattr(self, time_index_name + "s") if hasattr(self, time_index_name + "s") else cfg.cfgfile.get('case', 'years')
        
        setattr(self, map_to, getattr(self, map_from).copy())
        
        mapf = getattr(self, map_from)
        if current_geography not in (mapf.index.names if mapf.index.nlevels > 1 else [mapf.index.name]):
            raise ValueError('current geography does not match the geography of the dataframe in remap')
        else:
            current_geography_index_levels = mapf.index.levels[util.position_in_index(mapf, current_geography)] if mapf.index.nlevels > 1 else mapf.index.tolist()

        if (drivers is None) or (not len(drivers)):
            if fill_timeseries:     
                self.clean_timeseries(attr=map_to, inplace=True, time_index=time_index, time_index_name=time_index_name, interpolation_method=interpolation_method, extrapolation_method=extrapolation_method, lower=lower, upper=upper)
            if current_geography != converted_geography:
                self.geo_map(converted_geography, attr=map_to, inplace=True, current_geography=current_geography,
                             current_data_type=current_data_type, fill_value=fill_value)
                current_geography = converted_geography
        else:
            total_driver = DfOper.mult(util.put_in_list(drivers))
            
            if len(current_geography_index_levels) > 1 and current_geography != converted_geography:
                # While not on primary geography, geography does have some information we would like to preserve
                self.geo_map(converted_geography, attr=map_to, inplace=True, current_geography=current_geography,
                             current_data_type=current_data_type, fill_value=fill_value)
                current_geography = converted_geography

            if current_data_type == 'total':
                # Divide by drivers to turn a total to intensity. multindex_operation will aggregate to common levels.
                df_intensity = DfOper.divi((getattr(self, map_to), total_driver), expandable=(False, True), collapsible=(False, True))
                setattr(self, map_to, df_intensity)
            # Clean the timeseries as an intensity
            if fill_timeseries:
                # print getattr(self,map_to)
                # print time_index
                self.clean_timeseries(attr=map_to, inplace=True, time_index=time_index, interpolation_method=interpolation_method, extrapolation_method=extrapolation_method)
            if current_data_type == 'total':
                setattr(self, map_to, DfOper.mult((getattr(self, map_to), total_driver)))
            else:
                setattr(self, map_to, DfOper.mult((getattr(self, map_to), total_driver), expandable=(True, False),
                                                  collapsible=(False, True)))
            self.ensure_correct_geography(map_to, converted_geography, current_geography, current_data_type)
    def min_year(self):
        """calculates the minimum or start year of data in the technology specification.
        Used to determine start year of subsector for analysis."""

        attributes = vars(self)
        self.min_year = cfg.cfgfile.get('case', 'current_year')
        for att in attributes:
            obj = getattr(self, att)
            if inspect.isclass(type(obj)) and hasattr(obj, '__dict__') and hasattr(obj, 'raw_values'):
                try:
                    att_min_year = min(obj.raw_values.index.levels[util.position_in_index(obj.raw_values, 'vintage')])
                except:
                    att_min_year = self.min_year
                if att_min_year < self.min_year:
                    self.min_year = att_min_year
                else:
                    pass
Example #5
0
    def min_year(self):
        """calculates the minimum or start year of data in the technology specification.
        Used to determine start year of subsector for analysis."""

        attributes = vars(self)
        self.min_year = cfg.cfgfile.get('case', 'current_year')
        for att in attributes:
            obj = getattr(self, att)
            if inspect.isclass(type(obj)) and hasattr(obj, '__dict__') and hasattr(obj, 'raw_values'):
                try:
                    att_min_year = min(obj.raw_values.index.levels[util.position_in_index(obj.raw_values, 'vintage')])
                except:
                    att_min_year = self.min_year
                if att_min_year < self.min_year:
                    self.min_year = att_min_year
                else:
                    pass
Example #6
0
 def initiate_active_shapes(self):
     for id in self.active_shape_ids:
         shape = self.data[id]
         
         if hasattr(shape, 'raw_values'):
             return
         
         shape.read_timeseries_data()
         
         if shape.shape_type=='weather date':
             date_position = util.position_in_index(shape.raw_values, 'weather_datetime')
             shape.start_date = DT.datetime.strptime(shape.raw_values.index.levels[date_position][0], '%m/%d/%y %H:%M')
             shape.end_date = DT.datetime.strptime(shape.raw_values.index.levels[date_position][-1], '%m/%d/%y %H:%M')
 
             self.start_date = shape.start_date if self.start_date is None else max(shape.start_date, self.start_date)
             self.end_date = shape.end_date if self.end_date is None else min(shape.end_date, self.end_date)
     
     self.set_active_dates()
Example #7
0
 def initiate_active_shapes(self):
     print ' reading data for:'
     for id in self.active_shape_ids:
         shape = self.data[id]
         print '     shape: ' + shape.name
         if hasattr(shape, 'raw_values'):
             return
         
         shape.read_timeseries_data()
         
         if shape.shape_type=='weather date':
             shape.convert_index_to_datetime('raw_values', 'weather_datetime')
             date_position = util.position_in_index(shape.raw_values, 'weather_datetime')
             
             shape.start_date, shape.end_date = min(shape.raw_values.index.levels[date_position]), max(shape.raw_values.index.levels[date_position])
             
             self.start_date = shape.start_date if self.start_date is None else max(shape.start_date, self.start_date)
             self.end_date = shape.end_date if self.end_date is None else min(shape.end_date, self.end_date)
     self.set_active_dates()