Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import similarity as sm | |
| import json | |
| def tenant_visualization(similarity_matrix, requested_tenants): | |
| #TODO VIEW COMPATIBILITY BETWEEN REQUESTED TENANTS | |
| print(f"The options for visualizatio are:\n"+ | |
| "1.Compatibility for each tenants with requested ones\n"+ | |
| "2.Mean compatibility for each tenant\n"+ | |
| "3.Most compatible tenant for the requested ones\n"+ | |
| "4.Obtain registers from most compatible tenants\n") | |
| option = int(input("What option are u choosing: ")) | |
| tenant_lines = similarity_matrix[requested_tenants].mean() | |
| match(option): | |
| case 1: | |
| tenant_lines.head(5) | |
| print(f"Compatibility for each tenants with requested ones \n{tenant_lines}") | |
| case 2: | |
| mean_compatibility = tenant_lines.mean(axis = 0) | |
| print(f"Mean compatibility for each tenant:\n {mean_compatibility} ") | |
| case 3: | |
| most_compatible = tenant_lines.sort_values(ascending = False) | |
| print(f"Max compatibility for each tenant:\n {most_compatible} ") | |
| case 4: | |
| most_compatible = tenant_lines.sort_values(ascending = False) | |
| print(f"Most compatible tenants registers\n {most_compatible}") | |
| def tenant_inference(similarity_matrix, requested_tenants,dataframe): | |
| print("Tenant inference check") | |
| #TODO THIS FUNCTION IS THE ONE USED DURING INFERENCE TIME THE MODEL WILL CALCULATE THE 4 TENANTS WITH THE HIGHER COMPATIBILITY | |
| try: | |
| similarity_matrix = similarity_matrix.astype(int) | |
| similarity_tenant = similarity_matrix[requested_tenants].sort_values(ascending = False).head(4) | |
| final_tenants = similarity_tenant.index | |
| tenant_features = dataframe.loc[final_tenants, ["names", "age", "smoking", "email"]] | |
| print(tenant_features) | |
| tenants_dict = { | |
| key : list(tenant_features[key]) for key in tenant_features.columns | |
| } | |
| tenants_dict["Similarity"] = list(similarity_tenant) | |
| return tenants_dict | |
| except Exception as error: | |
| print(f"Error: {error}") | |
| def algo_start(id): | |
| dataframe, original_dataframe = sm.data_preparing() | |
| print("Received data preparing") | |
| #sm.data_checking(dataframe) | |
| similarity_matrix = sm.encoder_matrix(dataframe, min_range = 0, max_range=100) | |
| print("Received similarity") | |
| tenant_list = tenant_inference(similarity_matrix, id,original_dataframe) | |
| print("Fin algo start check") | |
| return tenant_list | |
| #json_convert(tenant_list) | |
| #tenant_visualization(similarity_matrix, [20,40,50,18,15]) | |
| def json_convert(tenant_list): | |
| print("Entering json_convert") | |
| #THE ALGO START FUNCTION COULD BE DOING THIS DIRECTLY, IS A POSSIBLE IMPROVEMENT IF THE WEB ISN'T WORKING FAST ENOUGH | |
| tenants_dict = { | |
| str(i) : { "Names" : " ", "Age": " ", "Smoking": " ", "Email" : " ", "Compatibility" : " "} for i in range(4) | |
| } | |
| tenant_counter = 0 | |
| for tenant in tenant_list: | |
| tenant_features = tenant[0] | |
| tenant_compatibility = tenant[1] | |
| for feature in tenant_features.index: | |
| tenants_dict[str(tenant_counter)][feature] = str(tenant_features[feature]) | |
| tenants_dict[str(tenant_counter)]["Compatibility"] = str(tenant_compatibility) | |
| tenant_counter += 1 | |
| tenants_json = json.dumps(tenants_dict, indent = 4) | |
| return tenants_json | |