Example #1
0
    def load(self, mean_radius, private_rescaling=1, size_power_law=0):

        assert mean_radius, \
            'Please provide a reference radius for the mean school size'
        (propert_data, locations) = super().extract_locations()

        type_age_dict = {
            'SCUOLA PRIMARIA': AgeGroup.ChildPrimary,
            'SCUOLA SECONDARIA I GRADO': AgeGroup.ChildMid,
        }
        school_types = propert_data[self.type_col].unique()
        assert set(school_types) <= set(type_age_dict.keys()), \
            'Unrecognized types in input'

        unit_list = []
        for school_type in school_types:

            b_this_group = propert_data[self.type_col] == school_type
            type_data = propert_data[b_this_group].copy()
            type_locations = [
                l for i, l in enumerate(locations) if b_this_group[i]
            ]
            # analyse capacity
            capacity = type_data[self.capacity_col]
            mean_capacity = capacity.mean()
            print('Observed mean capacity %.2f for %s' %
                  (mean_capacity, school_type))

            # set the lengthscale (radius) to be proportional
            # to the chosen power law of the relative capacity

            type_data['lengthscale'] = \
                mean_radius * (capacity / mean_capacity) ** size_power_law

            for i_unit in range(type_data.shape[0]):
                row_data = type_data.iloc[i_unit, :]
                attr_dict = {
                    'level': school_type,
                    'Public': row_data['bStatale']
                }

                this_unit = ServiceUnit(self.servicetype,
                                        name=row_data[self.name_col],
                                        unit_id=row_data[self.id_col],
                                        position=type_locations[i_unit],
                                        lengthscales={
                                            type_age_dict[school_type]:
                                            row_data['lengthscale']
                                        },
                                        capacity=row_data[self.capacity_col],
                                        attributes=attr_dict)

                if not attr_dict['Public'] and private_rescaling != 1:
                    this_unit.transform_kernels_with_factor(private_rescaling)

                unit_list.append(this_unit)

        return unit_list
Example #2
0
    def load(self, mean_radius=None):

        assert mean_radius, \
            'Please provide a reference radius for the mean library size'
        (propert_data, locations) = super().extract_locations()

        # Modifica e specifica che per le fasce d'età
        possible_users = AgeGroup.all_but([AgeGroup.Newborn, AgeGroup.Kinder])
        type_age_dict = {
            'Specializzata': {group: 1
                              for group in []},
            'Importante non specializzata':
            {group: 1
             for group in possible_users},
            'Pubblica': {group: 1
                         for group in possible_users},
            'NON SPECIFICATA': {group: 1
                                for group in possible_users},
            'Scolastica': {
                group: 1
                for group in
                [AgeGroup.ChildPrimary, AgeGroup.ChildMid, AgeGroup.ChildHigh]
            },
            'Istituto di insegnamento superiore': {
                group: 1
                for group in AgeGroup.all_but([
                    AgeGroup.Newborn, AgeGroup.Kinder, AgeGroup.ChildPrimary,
                    AgeGroup.ChildMid
                ])
            },
            'Nazionale': {group: 1
                          for group in possible_users}
        }

        library_types = propert_data[self.type_col].unique()
        assert set(library_types) <= set(type_age_dict.keys()), \
            'Unrecognized types in input'

        unit_list = []

        for lib_type in library_types:
            b_this_group = propert_data[self.type_col] == lib_type
            type_data = propert_data[b_this_group]
            type_locations = [
                l for i, l in enumerate(locations) if b_this_group[i]
            ]

            for i_unit in range(type_data.shape[0]):
                row_data = type_data.iloc[i_unit, :]
                attr_dict = {'level': lib_type}
                this_unit = ServiceUnit(self.servicetype,
                                        name=row_data[self.name_col],
                                        unit_id=row_data[self.id_col],
                                        scale=mean_radius,
                                        position=type_locations[i_unit],
                                        age_diffusion=type_age_dict[lib_type],
                                        attributes=attr_dict)
                unit_list.append(this_unit)

        return unit_list
Example #3
0
    def load(self, meanRadius):

        assert meanRadius, 'Please provide a reference radius for stops'
        (propertData, locations) = super().extract_locations()
        # make unique stop code
        propertData['stopCode'] = propertData['stop_id'] + '_' + propertData['route_id']
        # append route types
        routeTypeCol = 'route_type'
        gtfsTypesDict = {0: 'Tram', 1: 'Metro', 3: 'Bus'}
        assert all(propertData[routeTypeCol].isin(gtfsTypesDict.keys())), 'Unexpected route type'
        propertData['routeDesc'] = propertData[routeTypeCol].replace(gtfsTypesDict)

        nameCol = 'stopCode'
        typeCol = 'routeDesc'

        scaleDict = {0:meanRadius, 1: 2*meanRadius, 3: meanRadius}

        unitList = []
        for iUnit in range(propertData.shape[0]):
            rowData = propertData.iloc[iUnit, :]
            attrDict = {'routeType': rowData[typeCol]}
            thisUnit = ServiceUnit(self.servicetype,
                                   name=rowData[nameCol],
                                   position=locations[iUnit],
                                   ageDiffusionIn={g:1 for g in AgeGroup.all_but(
                                       [AgeGroup.Newborn, AgeGroup.Kinder])},
                                   scaleIn=scaleDict[rowData[routeTypeCol]],
                                   attributesIn=attrDict)
            unitList.append(thisUnit)

        return unitList
Example #4
0
def get_sample_unit(coords=(40, 9)):
    """Generate a test service unit."""

    sample_unit = ServiceUnit(service=mock_service_type,
                              name='test unit',
                              unit_id=1,
                              position=geopy.Point(*coords),
                              capacity=np.nan,
                              lengthscales={mock_age_group: lengthscale},
                              kernel_thresholds=None,
                              attributes={})

    return sample_unit
Example #5
0
    def load(self, mean_radius):

        assert mean_radius, 'Please provide a reference radius for stops'
        (propert_data, locations) = super().extract_locations()
        # make unique stop code
        propert_data['stop_id'] = propert_data['stop_id'].astype(str)
        propert_data['route_id'] = propert_data['route_id'].astype(str)
        propert_data['stopCode'] = \
            propert_data['stop_id'] + '_' + propert_data['route_id']
        # append route types
        route_type_col = 'route_type'
        gtfs_types_dict = {0: 'Tram', 1: 'Metro', 3: 'Bus'}
        assert all(propert_data[route_type_col].isin(gtfs_types_dict.keys())),\
            'Unexpected route type'
        propert_data['routeDesc'] = \
            propert_data[route_type_col].replace(gtfs_types_dict)

        scale_dict = {0: mean_radius, 1: 2 * mean_radius, 3: mean_radius}
        thresholds_dict = {t: None for t in scale_dict.keys()}

        unit_list = []
        for i_unit in range(propert_data.shape[0]):
            row_data = propert_data.iloc[i_unit, :]
            unit_route_type = row_data[route_type_col]
            attr_dict = {'routeType': row_data[self.type_col]}
            # this is None by default
            cached_thresholds = thresholds_dict[unit_route_type]
            this_unit = ServiceUnit(
                self.servicetype,
                name=row_data[self.name_col],
                unit_id=row_data[self.id_col],
                position=locations[i_unit],
                scale=scale_dict[unit_route_type],
                age_diffusion={
                    g: 1
                    for g in AgeGroup.all_but(
                        [AgeGroup.Newborn, AgeGroup.Kinder])
                },
                kernel_thresholds=cached_thresholds,
                attributes=attr_dict)
            unit_list.append(this_unit)
            # if there are no provided thresholds for this unit type,
            #  cache the computed ones
            if not cached_thresholds:
                thresholds_dict[unit_route_type] = this_unit.ker_thresholds

        return unit_list
Example #6
0
    def load(self, meanRadius):
        assert meanRadius, 'Please provide a reference radius for urban green'
        (propertData, locations) = super().extract_locations()

        nameCol = 'CODICEIDENTIFICATIVOFARMACIA'
        colAttributes = {'Descrizione': 'DESCRIZIONEFARMACIA', 'PartitaIva': 'PARTITAIVA'}

        unitList = []
        for iUnit in range(propertData.shape[0]):
            rowData = propertData.iloc[iUnit, :]
            attrDict = {name: rowData[col] for name, col in colAttributes.items()}
            thisUnit = ServiceUnit(self.servicetype,
                                   name=rowData[nameCol].astype(str),
                                   position=locations[iUnit],
                                   ageDiffusionIn={g: 1 for g in AgeGroup.all()},
                                   scaleIn=meanRadius,
                                   attributesIn=attrDict)
            unitList.append(thisUnit)

        return unitList
Example #7
0
    def load(self, meanRadius):
        
        assert meanRadius, 'Please provide a reference radius for the mean school size'
        (propertData, locations) = super().extract_locations()
        
        nameCol = 'DENOMINAZIONESCUOLA'
        typeCol = 'ORDINESCUOLA'
        scaleCol = 'ALUNNI'
        
        typeAgeDict = {'SCUOLA PRIMARIA': {AgeGroup.ChildPrimary:1},
                      'SCUOLA SECONDARIA I GRADO': {AgeGroup.ChildMid:1},
                      'SCUOLA SECONDARIA II GRADO': {AgeGroup.ChildHigh:1},}
        
        schoolTypes = propertData[typeCol].unique()
        assert set(schoolTypes) <= set(typeAgeDict.keys()), 'Unrecognized types in input'
        
        # set the scale to be proportional to the square root of number of children
        scaleData = propertData[scaleCol]**.5
        scaleData = scaleData/scaleData.mean() * meanRadius #mean value is mapped to input parameter
        propertData[scaleCol] = scaleData 
        unitList = []
                
        for scType in schoolTypes:
            bThisGroup = propertData[typeCol]==scType
            typeData = propertData[bThisGroup]
            typeLocations = [l for i,l in enumerate(locations) if bThisGroup[i]]

            for iUnit in range(typeData.shape[0]):
                rowData = typeData.iloc[iUnit,:]
                attrDict = {'level':scType}
                thisUnit = ServiceUnit(self.servicetype, 
                        name=rowData[nameCol], 
                        position=typeLocations[iUnit], 
                        ageDiffusionIn=typeAgeDict[scType], 
                        scaleIn=rowData[scaleCol],
                        attributesIn=attrDict)
                unitList.append(thisUnit)
        
        return unitList
Example #8
0
    def load(self, meanRadius):
        
        assert meanRadius, 'Please provide a reference radius for the mean library size'
        (propertData, locations) = super().extract_locations()
        
        nameCol = 'denominazioni.ufficiale'
        typeCol = 'tipologia-funzionale'
        
        # Modifica e specifica che per le fasce d'età
        typeAgeDict = {'Specializzata': {group:1 for group in AgeGroup.all()},
                      'Importante non specializzata': {group:1 for group in AgeGroup.all()},
                      'Pubblica': {group:1 for group in AgeGroup.all()},
                      'NON SPECIFICATA': {AgeGroup.ChildPrimary:1},
                      'Scolastica': {AgeGroup.ChildPrimary:1},
                      'Istituto di insegnamento superiore': {AgeGroup.ChildPrimary:1},
                      'Nazionale': {AgeGroup.ChildPrimary:1},}
        
        libraryTypes = propertData[typeCol].unique()
        assert set(libraryTypes) <= set(typeAgeDict.keys()), 'Unrecognized types in input'
        
        unitList = []
                
        for libType in libraryTypes:
            bThisGroup = propertData[typeCol]==libType
            typeData = propertData[bThisGroup]
            typeLocations = [l for i,l in enumerate(locations) if bThisGroup[i]]

            for iUnit in range(typeData.shape[0]):
                rowData = typeData.iloc[iUnit,:]
                attrDict = {'level':libType}
                thisUnit = ServiceUnit(self.servicetype, 
                        name=rowData[nameCol], 
                        position=typeLocations[iUnit], 
                        ageDiffusionIn=typeAgeDict[libType],
                        attributesIn=attrDict)
                unitList.append(thisUnit)
        
        return unitList
Example #9
0
    def load(self, mean_radius=None):
        assert mean_radius, 'Please provide a reference radius for pharmacies'
        (propert_data, locations) = super().extract_locations()

        col_attributes = {
            'Descrizione': 'DESCRIZIONEFARMACIA',
            'PartitaIva': 'PARTITAIVA'
        }

        unit_list = []
        # We assume all pharmacies share the same scale, so only one
        # threshold is necessary
        cached_thresholds = None
        for i_unit in range(propert_data.shape[0]):
            row_data = propert_data.iloc[i_unit, :]
            attr_dict = {
                name: row_data[col]
                for name, col in col_attributes.items()
            }
            this_unit = ServiceUnit(
                self.servicetype,
                name=row_data[self.name_col].astype(str),
                unit_id=row_data[self.id_col],
                position=locations[i_unit],
                scale=mean_radius,
                age_diffusion={g: 1
                               for g in AgeGroup.all()},
                kernel_thresholds=cached_thresholds,
                attributes=attr_dict)

            unit_list.append(this_unit)
            # if there were no thresholds, cache the computed ones
            if not cached_thresholds:
                cached_thresholds = this_unit.ker_thresholds

        return unit_list
Example #10
0
    def load(self, mean_radius=None, private_rescaling=1, size_power_law=0):

        assert mean_radius, \
            'Please provide a reference radius for the mean school size'
        (propert_data, locations) = super().extract_locations()

        type_age_dict = {
            'SCUOLA PRIMARIA': {
                AgeGroup.ChildPrimary: 1
            },
            'SCUOLA SECONDARIA I GRADO': {
                AgeGroup.ChildMid: 1
            },
        }

        school_types = propert_data[self.type_col].unique()
        assert set(school_types) <= set(type_age_dict.keys()), \
            'Unrecognized types in input'

        attendance_proxy = propert_data[self.scale_proxy_col].copy()

        # set the scale to be proportional
        # to the square root of number of children
        scale_data = attendance_proxy**size_power_law

        # mean value is mapped to input parameter
        scale_data = scale_data / scale_data.mean() * mean_radius

        # assign to new column
        propert_data[self.kernel_scale_col] = scale_data
        unit_list = []

        for school_type in school_types:
            b_this_group = propert_data[self.type_col] == school_type
            type_data = propert_data[b_this_group]
            type_locations = [
                l for i, l in enumerate(locations) if b_this_group[i]
            ]

            for i_unit in range(type_data.shape[0]):
                row_data = type_data.iloc[i_unit, :]
                attr_dict = {
                    'level': school_type,
                    'Public': row_data['bStatale']
                }

                this_unit = ServiceUnit(
                    self.servicetype,
                    name=row_data[self.name_col],
                    unit_id=row_data[self.id_col],
                    position=type_locations[i_unit],
                    age_diffusion=type_age_dict[school_type],
                    scale=row_data[self.kernel_scale_col],
                    attributes=attr_dict)

                if not attr_dict['Public'] and private_rescaling != 1:
                    this_unit.transform_kernels_with_factor(private_rescaling)

                unit_list.append(this_unit)

        return unit_list
Example #11
0
    def load(self, mean_radius):

        assert mean_radius, 'Please provide a reference radius for stops'
        (propert_data, locations) = super().extract_locations()
        # make unique stop code
        propert_data['stop_id'] = propert_data['stop_id'].astype(str)
        propert_data['route_id'] = propert_data['route_id'].astype(str)
        propert_data['stopCode'] = \
            propert_data['stop_id'] + '_' + propert_data['route_id']
        # append route types
        route_type_col = 'route_type'
        gtfs_types_dict = {
            0: 'Tram',
            1: 'Metro',
            2: 'Rail',
            3: 'Bus',
            7: 'Funicular'
        }
        assert all(propert_data[route_type_col].isin(gtfs_types_dict.keys())),\
            'Unexpected route type'
        propert_data['routeDesc'] = \
            propert_data[route_type_col].replace(gtfs_types_dict)

        users = AgeGroup.all_but([AgeGroup.Newborn, AgeGroup.Kinder])

        lengthscales_dict = {
            0: mean_radius,  # tram, light rail
            1: 2 * mean_radius,  # underground
            2: 2 * mean_radius,  # rail
            3: mean_radius,  # bus
            7: 2 * mean_radius  # funicular
        }
        thresholds_dict = {t: None for t in lengthscales_dict}

        unit_list = []
        for i_unit in range(propert_data.shape[0]):
            row_data = propert_data.iloc[i_unit, :]
            unit_route_type = row_data[route_type_col]
            attr_dict = {'routeType': row_data[self.type_col]}
            # this is None by default

            this_unit = ServiceUnit(
                self.servicetype,
                name=row_data[self.name_col],
                unit_id=row_data[self.id_col],
                position=locations[i_unit],
                capacity=np.nan,  # we have no capacity data yet
                lengthscales={
                    g: lengthscales_dict[unit_route_type]
                    for g in users
                },
                kernel_thresholds=thresholds_dict[unit_route_type],
                attributes=attr_dict)

            unit_list.append(this_unit)
            # if there are no provided thresholds for this unit type,
            #  cache the computed ones
            if not thresholds_dict[unit_route_type]:
                thresholds_dict[unit_route_type] = this_unit.ker_thresholds

        return unit_list