def filter_db_by_crime(db, key='Primary Type'): """ Receives a dataframe, asks user which crimes is interested in, filters and returns the database accordingly. key optional argument indicates which is the column where the crimes are listed. :param db :return db """ if not isinstance(db, pd.DataFrame): raise TypeError('Invalid object received. Expecting pandas.DataFrame') if key not in db.columns: raise ValueError('Database does not contain "{}" label to filter.'.format(key)) # Show available crime types, receive selections from user and filter accordingly. selected_crimes = get_options_from_user(db['Primary Type'].unique(), multiple=True) db = db[db['Primary Type'].isin(selected_crimes) == True] return db
def filter_db_by_crime(db, key='Primary Type'): """ Receives a dataframe, asks user which crimes is interested in, filters and returns the database accordingly. key optional argument indicates which is the column where the crimes are listed. :param db :return db """ if not isinstance(db, pd.DataFrame): raise TypeError('Invalid object received. Expecting pandas.DataFrame') if key not in db.columns: raise ValueError( 'Database does not contain "{}" label to filter.'.format(key)) # Show available crime types, receive selections from user and filter accordingly. selected_crimes = get_options_from_user(db['Primary Type'].unique(), multiple=True) db = db[db['Primary Type'].isin(selected_crimes) == True] return db
def main(): # Maximum number of addresses for analysis allowed MAX_ADDRESSES = 10 interface_utils.display_welcome_screen() # Load DB of crimes. If Database is not clean, the program will clean it and the user will have the option to save it crimes_database = databases_utils.get_crimes_db() os.system('clear') # Ask for user to choose a range of years and filter the Database accordingly crimes_database = databases_utils.filter_db_by_timeframe(crimes_database) # Ask for user to choose one or more types of crime and filter the Database accordingly crimes_database = databases_utils.filter_db_by_crime(crimes_database) # Create an instance of the CrimesDataFrame class with the filtered Database. This class is in charge of Statistics crimes_database = CrimesDataFrame(crimes_database) # Create lists to save addresses and addresses names of interest user_addresses = [] user_addresses_names = [] # Create interface Options interface_options = ['Add another address','Delete saved address', 'View saved address', 'Comparative analysis','Quit'] option = 'Add another address' # Main loop of the program. It will ask for user input and show maps and analyses until 'Quit' is introduced while 1: if (not user_addresses) or option == 'Add another address': try: # Create an instance of Address class. It will ask user to input an address, validate it through # Google Maps, and calculate/save several attributes of that address. address = Address() except NoInternetAccess: print('No Internet connection found') if interface_utils.yesno_question('Continue?'): address = None # Continue with the normal loop without saving the address else: raise QuitProgram # Create a District Heat Map with maps_builder class and show it. if address: interface_utils.computing_statistics_message() address.summary = address_analysis(crimes_database, address) address_analysis_output(address, crimes_database) # User will have the option to save/discard the address after reviewing the Map and Statistics. if interface_utils.yesno_question('Keep this address for comparative analysis?'): if len(user_addresses) == MAX_ADDRESSES: print 'Maximum number of addresses to save reached, delete one location before adding a new address' else: if address not in user_addresses: user_addresses.append(address) user_addresses_names.append(address.formatted_address) # This option will allow the user to review the Map and Stats of an address that was saved. elif option == 'View saved address': option = interface_utils.get_options_from_user(user_addresses_names + ['Back'], multiple=False) if option != 'Back': address = user_addresses[user_addresses_names.index(option)] interface_utils.computing_statistics_message() address_analysis_output(address, crimes_database) # This option will allow the user to delete one of the saved addresses. elif option == 'Delete saved address': option = interface_utils.get_options_from_user(user_addresses_names + ['Back'], multiple=False) if option != 'Back': user_addresses.pop(user_addresses_names.index(option)) user_addresses_names.pop(user_addresses_names.index(option)) # This option will create a comparative Map and statistics to analyze and compare the saved addresses. elif option == 'Comparative analysis': comparative_analysis_output(crimes_database, user_addresses) # This option is the only way to end the loop and finish the program elif option == 'Quit': raise QuitProgram # If user_addresses is empty start the loop again directly to ask for an address # If there are addresses in user_addresses, offer options on how to continue if user_addresses: option = interface_utils.get_options_from_user(interface_options, multiple=False)
def main(): # Maximum number of addresses for analysis allowed MAX_ADDRESSES = 10 interface_utils.display_welcome_screen() # Load DB of crimes. If Database is not clean, the program will clean it and the user will have the option to save it crimes_database = databases_utils.get_crimes_db() os.system('clear') # Ask for user to choose a range of years and filter the Database accordingly crimes_database = databases_utils.filter_db_by_timeframe(crimes_database) # Ask for user to choose one or more types of crime and filter the Database accordingly crimes_database = databases_utils.filter_db_by_crime(crimes_database) # Create an instance of the CrimesDataFrame class with the filtered Database. This class is in charge of Statistics crimes_database = CrimesDataFrame(crimes_database) # Create lists to save addresses and addresses names of interest user_addresses = [] user_addresses_names = [] # Create interface Options interface_options = [ 'Add another address', 'Delete saved address', 'View saved address', 'Comparative analysis', 'Quit' ] option = 'Add another address' # Main loop of the program. It will ask for user input and show maps and analyses until 'Quit' is introduced while 1: if (not user_addresses) or option == 'Add another address': try: # Create an instance of Address class. It will ask user to input an address, validate it through # Google Maps, and calculate/save several attributes of that address. address = Address() except NoInternetAccess: print('No Internet connection found') if interface_utils.yesno_question('Continue?'): address = None # Continue with the normal loop without saving the address else: raise QuitProgram # Create a District Heat Map with maps_builder class and show it. if address: interface_utils.computing_statistics_message() address.summary = address_analysis(crimes_database, address) address_analysis_output(address, crimes_database) # User will have the option to save/discard the address after reviewing the Map and Statistics. if interface_utils.yesno_question( 'Keep this address for comparative analysis?'): if len(user_addresses) == MAX_ADDRESSES: print 'Maximum number of addresses to save reached, delete one location before adding a new address' else: if address not in user_addresses: user_addresses.append(address) user_addresses_names.append(address.formatted_address) # This option will allow the user to review the Map and Stats of an address that was saved. elif option == 'View saved address': option = interface_utils.get_options_from_user( user_addresses_names + ['Back'], multiple=False) if option != 'Back': address = user_addresses[user_addresses_names.index(option)] interface_utils.computing_statistics_message() address_analysis_output(address, crimes_database) # This option will allow the user to delete one of the saved addresses. elif option == 'Delete saved address': option = interface_utils.get_options_from_user( user_addresses_names + ['Back'], multiple=False) if option != 'Back': user_addresses.pop(user_addresses_names.index(option)) user_addresses_names.pop(user_addresses_names.index(option)) # This option will create a comparative Map and statistics to analyze and compare the saved addresses. elif option == 'Comparative analysis': comparative_analysis_output(crimes_database, user_addresses) # This option is the only way to end the loop and finish the program elif option == 'Quit': raise QuitProgram # If user_addresses is empty start the loop again directly to ask for an address # If there are addresses in user_addresses, offer options on how to continue if user_addresses: option = interface_utils.get_options_from_user(interface_options, multiple=False)