Commit ced7b10f authored by shinde's avatar shinde
Browse files

Corrected the number_of_buildings_calculator

parent 12af051c
Pipeline #26758 failed with stage
in 1 minute and 8 seconds
...@@ -77,48 +77,45 @@ class NumberOfBuildings(SpatialiteDatabase): ...@@ -77,48 +77,45 @@ class NumberOfBuildings(SpatialiteDatabase):
return model return model
@staticmethod @staticmethod
def weighted_average(density_closest): def weighted_average(i, density_closest):
density_closest['weighted_average'] = np.nan sum_numbers = density_closest.loc[(density_closest['building_type_id'] == i[1][0]) &
unique_attr = density_closest.groupby(['building_type_id', 'construction_material_id', 'story_number_id'], (density_closest['construction_material_id'] == i[1][1])
as_index=False).count() & (density_closest['story_number_id'] == i[1][2]),
for row in unique_attr.iterrows(): 'numbers_calculated'].sum()
sum_numbers = density_closest.loc[(density_closest['building_type_id'] == row[1][0]) & logger.info(sum_numbers)
(density_closest['construction_material_id'] == row[1][1]) if sum_numbers != 0:
& (density_closest['story_number_id'] == row[1][2]), unique_attr_density_closest = density_closest.loc[(density_closest['building_type_id'] == i[1][0]) &
'numbers_calculated'].sum() (density_closest['construction_material_id'] == i[1][1])
& (density_closest['story_number_id'] == i[1][2])]
density_closest.loc[(density_closest['building_type_id'] == row[1][0]) & unique_attr_density_closest['weighted_average'] = (density_closest['proportion_calculated'] * density_closest['numbers_calculated']) / sum_numbers
(density_closest['construction_material_id'] == row[1][1])
& (density_closest['story_number_id'] == row[1][2]), 'weighted_average'] = \ return unique_attr_density_closest['weighted_average'].values
(density_closest['proportion_calculated'] * density_closest['numbers_calculated']) / sum_numbers else:
return density_closest['weighted_average'] unique_attr_density_closest = density_closest.loc[(density_closest['building_type_id'] == i[1][0]) &
(density_closest['construction_material_id'] == i[1][1])
& (density_closest['story_number_id'] == i[1][2])]
unique_attr_density_closest['weighted_average'] = 0
return unique_attr_density_closest['weighted_average'].values
@staticmethod @staticmethod
def numbers_building_classes(dataframe_with_info, dataframe_without_info): def numbers_building_classes(dataframe_with_info, row, attributes_with_bc):
for row in dataframe_without_info.iterrows(): pop_density = row[1][1]
pop_density = row[1][1] density_closest = dataframe_with_info.loc[(dataframe_with_info.pop_density <= pop_density)]
density_closest = pd.DataFrame() #if density_closest.empty:
density_closest = dataframe_with_info.loc[(dataframe_with_info.pop_density <= pop_density)] # density_closest = dataframe_with_info.loc[(dataframe_with_info.pop_density <= (pop_density + 10))]
density_closest['weighted_average'] = NumberOfBuildings.weighted_average(density_closest) logger.info(density_closest)
unique_attr = density_closest.groupby(['building_type_id', 'construction_material_id', 'story_number_id'], unique_attr = density_closest.groupby(['building_type_id', 'construction_material_id', 'story_number_id'],
as_index=False).size() as_index=False).size()
for i in unique_attr.iterrows(): logger.info(unique_attr)
sum_prop = density_closest.loc[(density_closest['building_type_id'] == i[1][0]) & for i in unique_attr.iterrows():
(density_closest['construction_material_id'] == i[1][1]) logger.info(i)
& (density_closest['story_number_id'] == i[1][2]), 'weighted_average'].sum() weighted_avg = NumberOfBuildings.weighted_average(i, density_closest)
logger.info(weighted_avg)
dataframe_without_info.loc[dataframe_without_info.district_id == row[1][0], 'building_type_id'] = \ sum_prop = sum(weighted_avg)
i[1][0] logger.info(sum_prop)
dataframe_without_info.loc[dataframe_without_info.district_id == row[1][0], attributes_with_bc = attributes_with_bc.append({'district_id': row[1][0], 'bldg_density': row[1][2], 'numbers': row[1][3], 'building_type_id': i[1][0], 'construction_material_id': i[1][1], 'story_number_id': i[1][2],
'construction_material_id'] = i[1][1] 'proportion_calculated': sum_prop}, ignore_index=True)
dataframe_without_info.loc[dataframe_without_info.district_id == row[1][0], 'story_number_id'] = \ return attributes_with_bc
i[1][2]
dataframe_without_info.loc[(dataframe_without_info.district_id == row[1][0]) &
(density_closest['building_type_id'] == i[1][0]) &
(density_closest['construction_material_id'] == i[1][1]) &
(density_closest['story_number_id'] == i[1][2]), 'proportion_calculated'] \
= sum_prop
return dataframe_without_info
def calculate_numbers(self, database_filepath): def calculate_numbers(self, database_filepath):
""" """
...@@ -144,7 +141,7 @@ class NumberOfBuildings(SpatialiteDatabase): ...@@ -144,7 +141,7 @@ class NumberOfBuildings(SpatialiteDatabase):
# Add pop_density column in attributes with info # Add pop_density column in attributes with info
attributes_with_info['pop_density'] = np.nan attributes_with_info['pop_density'] = np.nan
logger.info(attributes_with_info)
# Select population density from the Population table # Select population density from the Population table
population = pd.read_sql_query("SELECT * FROM Population", cnx) population = pd.read_sql_query("SELECT * FROM Population", cnx)
...@@ -158,7 +155,7 @@ class NumberOfBuildings(SpatialiteDatabase): ...@@ -158,7 +155,7 @@ class NumberOfBuildings(SpatialiteDatabase):
attributes_with_info.loc[attributes_with_info['district_id'] == district_id_pop, 'pop_density'] = pop_density attributes_with_info.loc[attributes_with_info['district_id'] == district_id_pop, 'pop_density'] = pop_density
if (attributes_with_info['district_id'] != district_id_pop).all(): if (attributes_with_info['district_id'] != district_id_pop).all():
attributes_with_no_info = attributes_with_no_info.append({'district_id': district_id_pop, 'pop_density': pop_density}, ignore_index=True) attributes_with_no_info = attributes_with_no_info.append({'district_id': district_id_pop, 'pop_density': pop_density}, ignore_index=True)
logger.info(attributes_with_no_info) #logger.info(attributes_with_no_info)
# Relationship model between pop_density and bldg_density # Relationship model between pop_density and bldg_density
# Select only non null values # Select only non null values
...@@ -177,27 +174,43 @@ class NumberOfBuildings(SpatialiteDatabase): ...@@ -177,27 +174,43 @@ class NumberOfBuildings(SpatialiteDatabase):
logger.info(sql_statement) logger.info(sql_statement)
for row in database.cursor: for row in database.cursor:
district_id = row[0] district_id = row[0]
logger.info(district_id)
area = row[1] area = row[1]
bldg_density = attributes_with_no_info.loc[attributes_with_no_info['district_id'] == district_id, 'bldg_density'] bldg_density = attributes_with_no_info.loc[attributes_with_no_info['district_id'] == district_id, 'bldg_density']
attributes_with_no_info.loc[attributes_with_no_info['district_id'] == district_id, 'numbers'] = \ attributes_with_no_info.loc[attributes_with_no_info['district_id'] == district_id, 'numbers'] = \
area * bldg_density area * bldg_density
# Reduce attributes with info to only include values which are required
attributes_with_info_reduced = attributes_with_info[attributes_with_info.numbers_calculated.notnull()]
# Expand for building classes # Expand for building classes
# Add columns with NA values for building type, construction material and storey number for row in attributes_with_no_info.iterrows():
attributes_with_no_info['building_type_id'] = np.nan logger.info(row)
attributes_with_no_info['construction_material_id'] = np.nan # Add columns with NA values for building type, construction material and storey number
attributes_with_no_info['story_number_id'] = np.nan attributes_with_bc = pd.DataFrame()
attributes_with_no_info['proportion_calculated'] = np.nan attributes_with_bc['district_id'] = np.nan
attributes_with_no_info['numbers_calculated'] = np.nan attributes_with_bc['bldg_density'] = np.nan
attributes_with_bc['numbers'] = np.nan
attributes_with_no_info = self.numbers_building_classes(attributes_with_info, attributes_with_no_info) attributes_with_bc['building_type_id'] = np.nan
attributes_with_bc['construction_material_id'] = np.nan
attributes_with_no_info['numbers_calculated'] = \ attributes_with_bc['story_number_id'] = np.nan
attributes_with_no_info['numbers'] * attributes_with_no_info['proportion_calculated'] attributes_with_bc['proportion_calculated'] = np.nan
logger.info(attributes_with_no_info) attributes_with_bc['numbers_calculated'] = np.nan
for row in attributes_with_no_info.itterrows(): # Find the numbers per building class
district_id = attributes_with_bc = self.numbers_building_classes(attributes_with_info_reduced, row, attributes_with_bc)
insert_building_number logger.info(attributes_with_bc)
if not attributes_with_bc.empty:
for j in attributes_with_bc.iterrows():
district_id = j[1][0]
logger.info(district_id)
building_type_id = j[1][3]
construction_material_id = j[1][4]
story_number_id = j[1][5]
proportion_calculated = j[1][6]
numbers = j[1][2]
numbers_calculated = (numbers * proportion_calculated)
logger.info(numbers_calculated)
#Insert the numbers into the database
self.insert_building_number(district_id, building_type_id, construction_material_id, story_number_id, proportion_calculated, numbers, numbers_calculated)
else:
continue
self.connection.commit() self.connection.commit()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment