def setup_crimes_db(path=CRIMES_DB_PATH):
    """
    Checks if the database is present and clean.
    If not clean, pre-processes the database and returns it.
    If the database is present and clean, returns None.
    :param  path
    :return db_crimes_clean

    """

    db_name = CRIMES_DB_PATH.split('/')[-1]
    orig_db_name = ORIGINAL_CRIMES_DB_PATH.split('/')[-1]

    # Check if the Database is clean, and start the cleaning process if required.
    if not db_name in os.listdir('./Databases/'):
        if orig_db_name in os.listdir('./Databases/'):

            print('Crimes database needs to be pre-processed before start running the program. This process might take several minutes. Please wait.')
            db_crimes_clean = clean_crimes_db(ORIGINAL_CRIMES_DB_PATH)
            print('Pre-processing completed.')

            # Offer the user the possibility to save the pre-processed DB
            if yesno_question('Do you want to save the pre-processed to {} DB for the next time?'.format(CRIMES_DB_PATH)):
                print('Please wait while saving...')
                db_crimes_clean.to_csv(CRIMES_DB_PATH)
                
            return db_crimes_clean

        # If the Database is not in the Databases folder inside the program directory, raise exception
        else:
            raise defined_exceptions.FatalError('Crimes database not found in {} \nYou can download the database from https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD and try again \nDatabase should be saved in the "Databases" folder inside the program Directory '.format(ORIGINAL_CRIMES_DB_PATH))

    return None
Exemple #2
0
def setup_crimes_db(path=CRIMES_DB_PATH):
    """
    Checks if the database is present and clean.
    If not clean, pre-processes the database and returns it.
    If the database is present and clean, returns None.
    :param  path
    :return db_crimes_clean

    """

    db_name = CRIMES_DB_PATH.split('/')[-1]
    orig_db_name = ORIGINAL_CRIMES_DB_PATH.split('/')[-1]

    # Check if the Database is clean, and start the cleaning process if required.
    if not db_name in os.listdir('./Databases/'):
        if orig_db_name in os.listdir('./Databases/'):

            print(
                'Crimes database needs to be pre-processed before start running the program. This process might take several minutes. Please wait.'
            )
            db_crimes_clean = clean_crimes_db(ORIGINAL_CRIMES_DB_PATH)
            print('Pre-processing completed.')

            # Offer the user the possibility to save the pre-processed DB
            if yesno_question(
                    'Do you want to save the pre-processed to {} DB for the next time?'
                    .format(CRIMES_DB_PATH)):
                print('Please wait while saving...')
                db_crimes_clean.to_csv(CRIMES_DB_PATH)

            return db_crimes_clean

        # If the Database is not in the Databases folder inside the program directory, raise exception
        else:
            raise defined_exceptions.FatalError(
                'Crimes database not found in {} \nYou can download the database from https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD and try again \nDatabase should be saved in the "Databases" folder inside the program Directory '
                .format(ORIGINAL_CRIMES_DB_PATH))

    return None
Exemple #3
0
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)
Exemple #4
0
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)