def generate_unique_ids(dataset, short_names):
    if len(short_names) == 0:
        raise ValueError, "There must have at least 1 attribute."
    elif len(short_names) == 1:
        submarket_ids = dataset[short_names[0]]
    else:
        attribute_values = column_stack( [dataset.get_attribute_as_column(short_name) for short_name in short_names] )
        max_digits = digits( attribute_values.max(axis=0) )
        multipler = array([10**d for d in max_digits[1:] + [0]])
        submarket_ids = (attribute_values * multipler).sum(axis=1)
        
    return submarket_ids
Beispiel #2
0
def generate_unique_ids(dataset, short_names, multipler=[]):
    if len(short_names) == 0:
        raise ValueError, "There must have at least 1 attribute."
    elif len(short_names) == 1:
        subgroup_ids = dataset[short_names[0]]
    else:
        attribute_values = dataset.get_multiple_attributes(short_names)
        if not multipler:
            max_digits = digits( attribute_values[:,1:].max(axis=0) )
            tot_digits = concatenate( (cumsum(max_digits[::-1])[::-1], [0]) )
            multipler = 10**tot_digits
        subgroup_ids = (attribute_values * multipler).sum(axis=1)
    return subgroup_ids, multipler
def generate_unique_ids(dataset, short_names):
    if len(short_names) == 0:
        raise ValueError, "There must have at least 1 attribute."
    elif len(short_names) == 1:
        submarket_ids = dataset[short_names[0]]
    else:
        attribute_values = column_stack([
            dataset.get_attribute_as_column(short_name)
            for short_name in short_names
        ])
        max_digits = digits(attribute_values.max(axis=0))
        multipler = array([10**d for d in max_digits[1:] + [0]])
        submarket_ids = (attribute_values * multipler).sum(axis=1)

    return submarket_ids
    def run(
        self,
        developmentproject_dataset,
        building_dataset,
        label_attribute_names=["building_type_id", "zone_id"],
        quantity_attribute_names=["residential_units",
                                  "non_residential_sqft"]):
        """Modify buildings to reflect new development projects. 
        """

        project_labels = None
        building_labels = None

        if not developmentproject_dataset or developmentproject_dataset.size(
        ) == 0:
            logger.log_warning(
                "Empty development project dataset. Skip add_projects_to_buildings."
            )
            return building_dataset

        is_placed_project = ones(developmentproject_dataset.size(),
                                 dtype='bool')
        for label_attribute in label_attribute_names:
            project_label_attribute = developmentproject_dataset.get_attribute_as_column(
                label_attribute)
            is_placed_project = logical_and(is_placed_project,
                                            project_label_attribute[:, 0] > 0)

            building_lable_attribute = building_dataset.get_attribute_as_column(
                label_attribute)

            if project_labels is None:
                project_labels = project_label_attribute
            else:
                project_labels = column_stack(
                    (project_labels, project_label_attribute))

            if building_labels is None:
                building_labels = building_lable_attribute
            else:
                building_labels = column_stack(
                    (building_labels, building_lable_attribute))
        max_digits = digits(
            row_stack((project_labels, building_labels)).max(axis=0))
        multipler = array([10**d for d in max_digits[1:] + [0]])
        building_identifier = (building_labels * multipler).sum(axis=1)
        project_identifier = (project_labels * multipler).sum(axis=1)
        if not all(is_placed_project):
            logger.log_warning(
                "There are %s projects with %s less than 0; they are not being processed."
                % (logical_not(is_placed_project).sum(),
                   ",".join(label_attribute_names)))
            project_identifier = project_identifier[is_placed_project]
        unique_project_identifier = unique(project_identifier)

        for quantity_attribute in quantity_attribute_names:
            developmentproject_quantity = developmentproject_dataset.get_attribute(
                quantity_attribute)[is_placed_project]
            quantity_sum = ndimage.sum(developmentproject_quantity,
                                       labels=project_identifier,
                                       index=unique_project_identifier)
            for i in range(unique_project_identifier.size):
                if quantity_sum[i] != 0:
                    this_identifier = unique_project_identifier[i]
                    this_label = []
                    remain = this_identifier
                    for m in multipler:
                        this_label.append(remain // m)
                        remain = remain % m
                    building_index = where(
                        building_identifier == this_identifier)[0]
                    #assert building_index.size == 1
                    if building_index.size == 0:
                        logger.log_error(
                            "building with attribute (%s) = (%s) is not in building_dataset"
                            % (label_attribute_names, this_label))
                        continue
                        #for attribute in []:
                        #data = None
                        #building_dataset.add_elements(name=quantity_attribute, data = current_values+quantity_sum[i],
                        #index=building_index)
                    if building_index.size > 1:
                        logger.log_warning(
                            "There are more than 1 building with attributes (%s) = (%s)"
                            % (label_attribute_names, this_label))
                        building_index = building_index[0]

                    current_values = building_dataset.get_attribute_by_index(
                        quantity_attribute, building_index)
                    building_dataset.modify_attribute(name=quantity_attribute,
                                                      data=current_values +
                                                      quantity_sum[i],
                                                      index=building_index)
        return building_dataset
    def run(self, developmentproject_dataset, building_dataset, 
            label_attribute_names=["building_type_id", "zone_id"], 
            quantity_attribute_names = ["residential_units", "non_residential_sqft"]):
        """Modify buildings to reflect new development projects. 
        """
        
        project_labels = None
        building_labels = None
        
        if not developmentproject_dataset or developmentproject_dataset.size() == 0:
            logger.log_warning("Empty development project dataset. Skip add_projects_to_buildings.")
            return building_dataset

        is_placed_project = ones(developmentproject_dataset.size(), dtype='bool')
        for label_attribute in label_attribute_names:
            project_label_attribute = developmentproject_dataset.get_attribute_as_column(label_attribute)
            is_placed_project = logical_and(is_placed_project, project_label_attribute[:,0]>0)
            
            building_lable_attribute = building_dataset.get_attribute_as_column(label_attribute)

            if project_labels is None:
                project_labels = project_label_attribute
            else:
                project_labels = column_stack((project_labels, project_label_attribute))
                
            if building_labels is None:
                building_labels = building_lable_attribute
            else:
                building_labels = column_stack((building_labels, building_lable_attribute))
        max_digits = digits( row_stack((project_labels, building_labels)).max(axis=0) )
        multipler = array([10**d for d in max_digits[1:] + [0]])
        building_identifier = (building_labels * multipler).sum(axis=1)
        project_identifier = (project_labels * multipler).sum(axis=1)
        if not all(is_placed_project):
            logger.log_warning("There are %s projects with %s less than 0; they are not being processed." % (logical_not(is_placed_project).sum(), 
                                                                                                             ",".join(label_attribute_names)))
            project_identifier = project_identifier[is_placed_project]
        unique_project_identifier = unique(project_identifier)
        
        for quantity_attribute in quantity_attribute_names:
            developmentproject_quantity = developmentproject_dataset.get_attribute(quantity_attribute)[is_placed_project]
            if developmentproject_quantity.sum() == 0: continue
            quantity_sum = ndimage.sum(developmentproject_quantity, labels=project_identifier, index=unique_project_identifier)
            for i in range(unique_project_identifier.size):
                if quantity_sum[i] != 0:
                    this_identifier = unique_project_identifier[i]
                    this_label = []
                    remain = this_identifier
                    for m in multipler:
                        this_label.append(remain // m)
                        remain = remain % m
                    building_index = where(building_identifier==this_identifier)[0]
                    #assert building_index.size == 1
                    if building_index.size == 0:
                        logger.log_error("building with attribute (%s) = (%s) is not in building_dataset" % (label_attribute_names, this_label) )
                        continue
                        #for attribute in []:
                            #data = None
                        #building_dataset.add_elements(name=quantity_attribute, data = current_values+quantity_sum[i], 
                                                      #index=building_index)
                    if building_index.size > 1:
                        logger.log_warning("There are more than 1 building with attributes (%s) = (%s)" % (label_attribute_names, this_label) )
                        building_index = building_index[0]
                        
                    current_values = building_dataset.get_attribute_by_index(quantity_attribute, building_index)                    
                    building_dataset.modify_attribute(name=quantity_attribute, data = current_values+quantity_sum[i], 
                                                      index=building_index)
        return building_dataset