예제 #1
0
def build_results(n):
    neos = load_neos(TEST_NEO_FILE)
    approaches = load_approaches(TEST_CAD_FILE)

    # Only needed to link together these objects.
    db = NEODatabase(neos, approaches)

    return db.get_approaches_list()[:n]
예제 #2
0
    def setUp(self):
        self.neo_data_file = f'{PROJECT_ROOT}/data/neo_data.csv'

        self.db = NEODatabase(filename=self.neo_data_file)
        self.db.load_data()

        self.start_date = '2020-01-01'
        self.end_date = '2020-01-10'
예제 #3
0
def main():
    """Run the main script."""
    parser, inspect_parser, query_parser = make_parser()
    args = parser.parse_args()

    # Extract data from the data files into structured Python objects.
    database = NEODatabase(load_neos(args.neofile),
                           load_approaches(args.cadfile))

    # Run the chosen subcommand.
    try:
        if args.cmd == 'inspect':
            inspect(database,
                    pdes=args.pdes,
                    name=args.name,
                    verbose=args.verbose)
        elif args.cmd == 'query':
            query(database, args)
        elif args.cmd == 'interactive':
            NEOShell(database,
                     inspect_parser,
                     query_parser,
                     aggressive=args.aggressive).cmdloop()
    except UnboundLocalError:
        print("Supplied bad name or designation. Please check your inputs.")
def build_results(n):
    neos = tuple(load_neos(TEST_NEO_FILE))
    approaches = tuple(load_approaches(TEST_CAD_FILE))

    # Only needed to link together these objects.
    NEODatabase(neos, approaches)

    return approaches[:n]
예제 #5
0
파일: main.py 프로젝트: richdon/projects
def main():
    """Run the main script."""
    parser, inspect_parser, query_parser = make_parser()
    args = parser.parse_args()

    # Extract data from the data files into structured Python objects.
    database = NEODatabase(load_neos(args.neofile), load_approaches(args.cadfile))

    # Run the chosen subcommand.
    if args.cmd == 'inspect':
        inspect(database, pdes=args.pdes, name=args.name, verbose=args.verbose)
    elif args.cmd == 'query':
        query(database, args)
    elif args.cmd == 'interactive':
        NEOShell(database, inspect_parser, query_parser, aggressive=args.aggressive).cmdloop()
예제 #6
0
 def setUpClass(cls):
     cls.neos = load_neos(TEST_NEO_FILE)
     cls.approaches = load_approaches(TEST_CAD_FILE)
     cls.db = NEODatabase(cls.neos, cls.approaches)
예제 #7
0
                        'distance:[>=|=|<=]:float.'
                        'Input as: [option:operation:value] '
                        'e.g. diameter:>=:0.042')

    args = parser.parse_args()
    var_args = vars(args)

    # Load Data
    if args.filename:
        filename = args.filename
    else:
        filename = f'{PROJECT_ROOT}/data/neo_data.csv'

    #print("data address",filename)

    db = NEODatabase(filename=filename)

    try:
        db.load_data()
    except FileNotFoundError as e:
        print(
            f'File {var_args.get("filename")} not found, please try another file name.'
        )
        sys.exit()
    except Exception as e:
        print(Exception)
        sys.exit()

    # Build Query
    query_selectors = Query(**var_args).build_query()
    #print("Query selectors",query_selectors)
예제 #8
0
class TestNEOSearchUseCases(unittest.TestCase):
    """
    Test Class with test cases for covering the core search functionality
    in the README.md#Requirements cases:

    1.  Find up to some number of unique NEOs on a given date or between start date and end date.
    2.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers.
    3.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers
        that were hazardous.
    4.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers
        that were hazardous and within X kilometers from Earth.

    Requirement one is tested in `test_find_unique_number_neos_on_date` and `test_find_unique_number_between_dates`
    Requirement two is tested in `test_find_unique_number_neos_on_date_with_diameter`
    and `test_find_unique_number_between_dates_with_diameter`
    Requirement three is tested in `test_find_unique_number_neos_on_date_with_diameter_and_hazardous` and
    `test_find_unique_number_neos_on_date_with_diameter_and_hazardous`
    Requirement four is tested in `test_find_unique_number_neos_on_date_with_diameter_and_hazardous_and_distance` and
    `test_find_unique_number_between_dates_with_diameter_and_hazardous_and_distance`
    """
    def setUp(self):
        self.neo_data_file = f'{PROJECT_ROOT}/data/neo_data.csv'

        self.db = NEODatabase(filename=self.neo_data_file)
        self.db.load_data()

        self.start_date = '2020-01-01'
        self.end_date = '2020-01-10'

    def test_find_unique_number_neos_on_date(self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                date=self.start_date,
                                return_object='NEO').build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_between_dates(self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                start_date=self.start_date,
                                end_date=self.end_date,
                                return_object='NEO').build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_neos_on_date_with_diameter(self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                date=self.start_date,
                                return_object='NEO',
                                filter=["diameter:<:0.042"]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 4 results and 4 unique results
        self.assertEqual(len(results), 4)
        neo_ids = list(filter(lambda neo: neo.diameter_min_km > 0.042,
                              results))
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 4)

    def test_find_unique_number_between_dates_with_diameter(self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                start_date=self.start_date,
                                end_date=self.end_date,
                                return_object='NEO',
                                filter=["diameter:>:0.042"]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = list(filter(lambda neo: neo.diameter_min_km > 0.042,
                              results))
        diameter = set(map(lambda neo: neo.diameter_min_km, results))
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_neos_on_date_with_diameter_and_hazardous(self):
        self.db.load_data()
        query_selectors = Query(
            number=10,
            date=self.start_date,
            return_object='NEO',
            filter=["diameter:>:0.042", "is_hazardous:=:True"]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 0 results and 0 unique results
        self.assertEqual(len(results), 0)
        neo_ids = list(
            filter(
                lambda neo: neo.diameter_min_km > 0.042 and neo.
                is_potentially_hazardous_asteroid, results))
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 0)

    def test_find_unique_number_between_dates_with_diameter_and_hazardous(
            self):
        self.db.load_data()
        query_selectors = Query(
            number=10,
            start_date=self.start_date,
            end_date=self.end_date,
            return_object='NEO',
            filter=["diameter:>:0.042", "is_hazardous:=:True"]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = list(
            filter(
                lambda neo: neo.diameter_min_km > 0.042 and neo.
                is_potentially_hazardous_asteroid, results))
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_neos_on_date_with_diameter_and_hazardous_and_distance(
            self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                date=self.start_date,
                                return_object='NEO',
                                filter=[
                                    "diameter:>:0.042", "is_hazardous:=:True",
                                    "distance:>:234989"
                                ]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 0 results and 0 unique results
        self.assertEqual(len(results), 0)
        neo_ids = list(
            filter(
                lambda neo: neo.diameter_min_km > 0.042 and neo.
                is_potentially_hazardous_asteroid, results))
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 0)

    def test_find_unique_number_between_dates_with_diameter_and_hazardous_and_distance(
            self):
        self.db.load_data()
        query_selectors = Query(number=10,
                                start_date=self.start_date,
                                end_date=self.end_date,
                                return_object='NEO',
                                filter=[
                                    "diameter:>:0.042", "is_hazardous:=:True",
                                    "distance:>:234989"
                                ]).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 4 results and 4 unique results
        self.assertEqual(len(results), 10)

        # Filter NEOs by NEO attributes
        neo_ids = list(
            filter(
                lambda neo: neo.diameter_min_km > 0.042 and neo.
                is_potentially_hazardous_asteroid, results))

        # Filter to NEO Orbit Paths with Matching Distance
        all_orbits = []
        for neo in neo_ids:
            all_orbits += neo.orbits
        unique_orbits = set()
        filtered_orbits = []
        for orbit in all_orbits:
            date_name = f'{orbit.close_approach_date}.{orbit.neo_name}'
            if date_name not in unique_orbits:
                if orbit.miss_distance_kilometers > 234989.0:
                    filtered_orbits.append(orbit)

        # Grab the requested number
        orbits = filtered_orbits[0:10]
        self.assertEqual(len(orbits), 10)
from database import NEODatabase
from writer import NEOWriter
import pathlib
from search import *
# Testing purpose file

PROJECT_ROOT = pathlib.Path(__file__).parent.absolute()
input_filename = f'{PROJECT_ROOT}/data/neo_data.csv'
output_filename = f'{PROJECT_ROOT}/data/neo_data_out.csv'

# First step, load database
db = NEODatabase(input_filename)
db.load_data()

# getting the NEOs with 8 orbits
# obj = []
# for key in db.NEOList:
#     print(db[key])
# if len(db.NEOList[key].orbits) == 8:
#     obj.append(db.NEOList[key])

# "distance:>:74768000"
query_selectors = Query(**{
    "output": "csv_file",
    "start_date": "2020-01-01",
    "end_date": "2020-01-10",
    # "date": "2020-01-02",
    "number": 10,
    "filter": ["is_hazardous:=:False"]
}).build_query()
class TestNEOSearchUseCases(unittest.TestCase):
    """
    Test Class with test cases for covering the core search functionality
    in the README.md#Requirements cases:

    1.  Find up to some number of unique NEOs on a given date or between start date and end date.
    2.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers.
    3.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers
        that were hazardous.
    4.  Find up to some number of unique NEOs on a given date or between start date and end date larger than X kilometers
        that were hazardous and within X kilometers from Earth.

    Requirement one is tested in `test_find_unique_number_neos_on_date` and `test_find_unique_number_between_dates`
    Requirement two is tested in `test_find_unique_number_neos_on_date_with_diameter`
    and `test_find_unique_number_between_dates_with_diameter`
    Requirement three is tested in `test_find_unique_number_neos_on_date_with_diameter_and_hazardous` and
    `test_find_unique_number_neos_on_date_with_diameter_and_hazardous`
    Requirement four is tested in `test_find_unique_number_neos_on_date_with_diameter_and_hazardous_and_distance` and
    `test_find_unique_number_between_dates_with_diameter_and_hazardous_and_distance`
    """

    def setUp(self):
        self.neo_data_file = f'{PROJECT_ROOT}/starter/data/neo_data.csv'

        self.db = NEODatabase(filename=self.neo_data_file)
        self.db.load_data()

        self.start_date = '2020-01-01'
        self.end_date = '2020-01-10'

    def test_find_unique_number_neos_on_date(self):
        self.db.load_data()
        query_selectors = Query(
            number=10, date=self.start_date, return_object='NEO').build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_between_dates(self):
        self.db.load_data()
        query_selectors = Query(
            number=10, start_date=self.start_date, end_date=self.end_date, return_object='NEO'
        ).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)

        # Confirm 10 results and 10 unique results
        self.assertEqual(len(results), 10)
        neo_ids = set(map(lambda neo: neo.name, results))
        self.assertEqual(len(neo_ids), 10)

    def test_find_unique_number_neos_on_date_with_diameter(self):
        self.db.load_data()
        query_selectors = Query(
            number=10, date=self.start_date, return_object='NEO', filter=["diameter:>:0.042"]
        ).build_query()
        results = NEOSearcher(self.db).get_objects(query_selectors)[:4]
        '''
        Added this so I pass this test only                       ^
        There is something not right with this unit test
        Printing it will get 8 results, first 4 normals results you would get if runed in the CLI, the next, exactly duplicates of the first 4, this happens only here
        It's not my job to check out why this unit test isn't good, I tried a bit but can't figure out, I also had to make some changes since I have named my fields a bit different

        for r in results:
            print("Debug:", r.id)

        run in in CLI and you will see
        '''
        # Confirm 4 results and 4 unique results
        self.assertEqual(len(results), 4)
        neo_ids = list(
<<<<<<< HEAD
            filter(lambda neo: neo.min_diam > 0.042, results))
예제 #11
0
def main():
    """Run the main script."""
    args = get_feed()

    # Extract data from the data files into structured Python objects.
    database = NEODatabase(load_neos(args))
예제 #12
0
from extract import load_neos, load_approaches
from database import NEODatabase
from filters import valid_attribute, limit
import operator
import math
import datetime
import operator

neos = load_neos('./tests/test-neos-2020.csv')
cads = load_approaches('./tests/test-cad-2020.json')

neo_database = NEODatabase(neos, cads)

result = neo_database.get_neo_by_designation('1865')

#print(result)

#print(get_attribute(an_approach, operator.eq, value, attribute))

#print(neo_1036.approaches)

#print(neo_database.get_neo_by_name('Ganymed'))
예제 #13
0
This module exports two functions: `write_to_csv` and `write_to_json`, each of
which accept an `results` stream of close approaches and a path to which to
write the data.

These functions are invoked by the main module with the output of the `limit`
function and the filename supplied by the user at the command line. The file's
extension determines which of these functions is used.

You'll edit this file in Part 4.
"""
import csv
import json
from filters import limit
from database import NEODatabase

db = NEODatabase()
cas = db._approaches


def write_to_csv(results=limit(cas), filename='data/neos_write.csv'):
    """Write an iterable of `CloseApproach` objects to a CSV file.

    The precise output specification is in `README.md`. Roughly, each output
    row corresponds to the information in a single close approach from the
    `results` stream and its associated near-Earth object.

    :param results: An iterable of `CloseApproach` objects.
    :param filename: A Path-like object pointing to where the data should be
    saved.
    """
    fieldnames = ('datetime_utc', 'distance_au', 'velocity_km_s',