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
Exemple #2
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
    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