def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception("Cannot load data, no filename provided") filename = filename or self.filename # TODO: Load data from csv file. with open(filename, "r") as f: csvfile = csv.DictReader(f, delimiter=",") for row in csvfile: orbit_path = OrbitPath(**row) near_earth_object = NearEarthObject(**row) near_earth_object.update_orbits(orbit_path) if not self.neo_name_map.get(row["name"], None): self.neo_name_map[row["name"]] = near_earth_object if row["close_approach_date"] in self.NearEarthObjects: self.NearEarthObjects[row["close_approach_date"]].append( near_earth_object) else: self.NearEarthObjects[row["close_approach_date"]] = [ near_earth_object ] return None
def write(self, format, data, **kwargs): """ Generic write interface that, depending on the OutputFormat selected calls the appropriate instance write function :param format: str representing the OutputFormat :param data: collection of NearEarthObject or OrbitPath results :param kwargs: Additional attributes used for formatting output e.g. filename :return: bool representing if write successful or not """ # TODO: Using the OutputFormat, how can we organize our 'write' logic for output to stdout vs to csvfile # TODO: into instance methods for NEOWriter? Write instance methods that write() can call to do the necessary # TODO: output format. # Write to screen if format == OutputFormat.display.value: print(NearEarthObject.get_csv_header()) for datum in data: print(datum) # Write to CSV file elif format == OutputFormat.csv_file.value: filename = f'{PROJECT_ROOT}/data/neo_neo_data.csv' with open(filename, 'w') as f: f.writelines(NearEarthObject.get_csv_header() + '\n') for datum in data: f.writelines(datum.__repr__() + '\n') else: return False return True
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances -> dict('orbitDate': List[NearEarthObject]) - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject -> dict('name': NearEarthObject) :param filename: :return: None """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # TODO: Load data from csv file. # TODO: Where will the data be stored? """ Reading neo_data.csv as dictionary and populating NearEarthObject and OrbitPath and initialising our two databases declared in __init__ """ with open(filename, 'r') as neo_data_file: reader = csv.DictReader(neo_data_file) for entry in reader: #print entry _orbit_date = entry["close_approach_date"] _neo_name = entry["name"] _neo_object = NearEarthObject(**entry) _orbit_path_object = OrbitPath(**entry) # Update date_orbit_db (Here storing neo_objects without orbit details) if _orbit_date in self.date_neo_db: updated_neo_list = self.date_neo_db[_orbit_date] + [ _neo_object ] self.date_neo_db[_orbit_date] = updated_neo_list else: self.date_neo_db[_orbit_date] = [_neo_object] # Update neo_object_db (Here neo_objects are having orbit details) if _neo_name in self.neo_object_db: curr_neo_obj = self.neo_object_db[_neo_name] curr_neo_obj.update_orbits(_orbit_path_object, False) self.neo_object_db[_neo_name] = curr_neo_obj else: _neo_object.update_orbits(_orbit_path_object, False) self.neo_object_db[_neo_name] = _neo_object return None
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ if not neo_csv_path: raise Exception('Path is empty, no filename provided!') neos = set() with open(neo_csv_path) as f: # create reader reader = csv.DictReader(f) for row in reader: neo_data = { 'designation': row['pdes'], 'name': row['name'], 'diameter': row['diameter'], 'hazardous': True if row['pha'] in ('Y', 'y') else False } neo = NearEarthObject(**neo_data) if neo not in neos: neos.add(neo) return neos
def neo_objects(neo_lst): for neo in neo_lst: return NearEarthObject(designation=neo['pdes'], name=neo['name'], diameter=neo['diameter'], hazardous=['pha'])
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ with open(neo_csv_path) as f: reader = csv.DictReader(f) data = [] for row in reader: info = {} info['designation'] = row['pdes'] if row['name']: info['name'] = row['name'] else: info['name'] = None if row['diameter']: info['diameter'] = float(row['diameter']) else: info['diameter'] = float('nan') if row['pha'] == 'Y': info['hazardous'] = True else: info['hazardous'] = False neo = NearEarthObject(**info) data.append(neo) return data
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename neo_data_file = open(filename, 'r') neo_data = csv.DictReader(neo_data_file) for neo_row_data in neo_data: orbit_path = OrbitPath(**neo_row_data) if not self.neo_name.get(neo_row_data['name'], None): self.neo_name[neo_row_data['name']] = NearEarthObject( **neo_row_data) near_earth_object = self.neo_name.get(neo_row_data['name'], None) near_earth_object.update_orbits(orbit_path) if not self.neo_date.get(neo_row_data['close_approach_date'], None): self.neo_date[neo_row_data['close_approach_date']] = [] self.neo_date[neo_row_data['close_approach_date']].append( near_earth_object) return None
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating NearEarthObjects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # TODO: Load data from csv file. # TODO: Where will the data be stored? with open(filename, 'r') as f: reader = csv.DictReader(f) for row in reader: neo = NearEarthObject(**row) orbit = OrbitPath(**row) approach_date = row["close_approach_date"] self.neo_name_db.setdefault(neo.name, neo).update_orbits(orbit) self.neo_date_db.setdefault(approach_date, []).append(orbit)
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # Load data from csv file. df = pd.read_csv(filename) # Where will the data be stored? dict_list = df.to_dict(orient="records") for item in dict_list: if item['close_approach_date_full'] not in self.orbit_dict: self.orbit_dict[item['close_approach_date_full']] = {} if item['kilometers_per_second']+item['miss_distance_kilometers'] not in self.orbit_dict[item['close_approach_date_full']]: self.orbit_dict[item['close_approach_date_full']][item['kilometers_per_second']+item['miss_distance_kilometers']] = OrbitPath(**item) current_orbit = self.orbit_dict[item['close_approach_date_full']][item['kilometers_per_second']+item['miss_distance_kilometers']] if item['id'] not in self.neo_dict: self.neo_dict[item['id']] = NearEarthObject(**item) self.neo_dict[item['id']].update_orbits(current_orbit)
def load_neos(neo_csv_path='data/neos.csv'): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ neo_lst = [] with open(neo_csv_path, 'r') as f: reader = csv.DictReader(f) for row in reader: designation = row['pdes'] if row['name'] == '': name = None else: name = row['name'] if row['diameter'] == '': diameter = float('nan') else: diameter = float(row['diameter']) if row['pha'] == 'Y': hazardous = True else: hazardous = False neo = NearEarthObject(designation, name, diameter, hazardous) neo_lst.append(neo) return neo_lst
def load_neos(neo_csv_path): """ Returns near-Earth objects list from a CSV file. Parameters: neo_csv_path (str): A path to a CSV file containing data about near-Earth objects. Returns: neos (list): A collection of NearEarthObjects. """ neos = list() with open(neo_csv_path) as f: reader = csv.reader(f) next(reader) for row in reader: pdes = row[3] name = row[4] pha = row[7] diameter = row[15] neo = NearEarthObject(designation=pdes, name=name, diameter=diameter, hazardous=pha) neos.append(neo) return neos
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ # cols_wanted = ['full_name', 'pdes', 'name', 'neo', 'diameter', 'pha'] # with open(neo_csv_path, 'r') as f: # reader = csv.reader(f) # header = next(reader) # idx = [header.index(col) for col in cols_wanted] # res = [header] # for row in reader: # res.append([row[i] for i in idx]) # return res #res = {} res = [] with open(neo_csv_path, 'r') as f: content_dict = csv.DictReader(f) for elm in content_dict: obj = NearEarthObject(**elm) #res[elm['pdes']] = obj res.append(obj) return res
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ result = [] with open(neo_csv_path, 'r') as infile: reader = csv.DictReader(infile) for n in reader: diameterVal = float('nan') nameVal = None # checking for the value of diameter if (n['diameter'] != '' and n['diameter'] != None): diameterVal = float(n['diameter']) # checking for value of name if (n['name'] != '' and n['name'] != None): nameVal = n['name'] result.append( NearEarthObject(pdesignation=n['pdes'], name=nameVal, hazardous=n['pha'], diameter=diameterVal)) return result
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # TODO: Load data from csv file. # TODO: Where will the data be stored? csvfile = open(self.filename, "r") obj = csv.DictReader(csvfile) for ob in obj: if ob["name"] in self.data_dictionary: temp_orb = OrbitPath(**ob) self.data_dictionary[ob["name"]].update_orbits(temp_orb) else: self.data_dictionary[ob["name"]] = NearEarthObject(**ob) return None
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ # TODO: Load NEO data from the given CSV file. if not neo_csv_path: raise Exception('Cannot load data, no filename provided') """ read request csv file and get required data from each row and create neo Object and store into neos list """ filename = neo_csv_path with open(filename, 'r') as f: csvfile = csv.DictReader(f, delimiter=',') for row in csvfile: neo_data = { 'name': row['name'], 'pha': row['pha'], 'diameter': row['diameter'], 'pdes': row['pdes'], } neo = NearEarthObject(**neo_data) """ check neo is already added or not in neos list """ if neo.designation not in neos_id: neos_id[neo.designation] = len(neos) neos.append(neo) return set(neos)
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename datafile = pd.read_csv(filename) for row in datafile.itertuples(): #Reading Rows and creating instances instance = None if row.name not in self.neos_by_name: instance = NearEarthObject( neo_id=row.id, name=row.name, min_d=row.estimated_diameter_min_kilometers, max_d=row.estimated_diameter_max_kilometers, is_hazardous=row.is_potentially_hazardous_asteroid) self.neos_by_name[row.name] = instance else: instance = self.neos_by_name[row.name] orbit = OrbitPath(name=row.name, speed=row.kilometers_per_hour, miss_d=row.miss_distance_kilometers, date=row.close_approach_date, orbiting_body=row.orbiting_body) instance.update_orbits(orbit) if row.close_approach_date not in self.neos_by_date: self.neos_by_date[row.close_approach_date] = list() self.neos_by_date[row.close_approach_date].append(instance) return None
def load_to_generator(): with open(cad_json_path, 'r') as infile: data = json.load(infile)['data'] for line in data: yield CloseApproach(designation=line[0], time=line[3], distance=line[4], velocity=line[-4], neo=NearEarthObject(''))
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # TODO: Load data from csv file. # TODO: Where will the data be stored? self.df = pd.read_csv(filename) for i in range(self.df.shape[0]): name = self.df['name'][i] if name in self.neo_name: self.neo_name[name].update_orbits(self.df.iloc[i]) else: temp = NearEarthObject() temp.fill_data(self.df.iloc[i]) self.neo_name[name] = temp self.neo_name[name].update_orbits(self.df.iloc[i]) date = self.df['close_approach_date'][i] if date in self.orbit_date: temp2 = OrbitPath() temp2.update_param(self.df.iloc[i]) self.orbit_date[date].append(temp2) else: temp2 = OrbitPath() temp2.update_param(self.df.iloc[i]) self.orbit_date[date] = [temp2] return None
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename try: neo_data_file = open(filename, 'r') columns_in_data = neo_data_file.readline().split(',') line_by_line = neo_data_file.readlines() for line in line_by_line: data = line.split(',') data_dict = { columns_in_data[i]: data[i] for i in range(len(columns_in_data)) } if (data_dict['name'] in self.neos.keys()): orbit = OrbitPath(**data_dict) self.neos[orbit.neo_name].update_orbits(orbit) if (data_dict['close_approach_date'] in self.orbits.keys()): self.orbits[data_dict['close_approach_date']].append( orbit.neo_name) else: self.orbits[orbit.close_approach_date] = [ orbit.neo_name ] else: neo = NearEarthObject(**data_dict) self.neos[neo.name] = neo if (data_dict['close_approach_date'] in self.orbits.keys()): self.orbits[data_dict['close_approach_date']].append( neo.name) else: self.orbits[data_dict['close_approach_date']] = [ neo.name ] finally: neo_data_file.close() # Next step to create dicts to store orbits,neo objects # TODO: Load data from csv file. # TODO: Where will the data be stored? return None
def load_neos(neo_csv_path): infile = pathlib.Path(neo_csv_path) out_neos = list() # process the file and create the NEO with open(infile, 'r') as input: data = csv.DictReader(input) for row in data: out_neos.append(NearEarthObject( designation=row['pdes'], name=row['name'], diameter=row['diameter'], hazardous=row['pha'])) return out_neos
def load_to_generator(): with open(neo_csv_path, 'r') as infile: reader = csv.DictReader(infile) for row in reader: if row['neo'] == 'Y': yield NearEarthObject(row['pdes'], row['name'] if row['name'] != '' else None, None if row['diameter'] == '' else float(row['diameter']), row['pha'] == 'Y' )
def load_neos(neo_csv_path) -> list: """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A list of `NearEarthObject`s. """ with open(neo_csv_path, 'r') as neo_file: neos_info = csv.DictReader(neo_file) neos_objects = [NearEarthObject(**neo) for neo in neos_info] return neos_objects
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ with open(neo_csv_path, 'r') as f: result = [] for row in csv.DictReader(f): result.append(NearEarthObject(**row)) return result
def load_neos(neo_csv_path='data/neos.csv'): """Load neos and unpack them in order to build objects.""" neos_csv = [] with open(neo_csv_path) as f: reader = csv.DictReader(f) for elem in reader: neos_csv.append(elem) neos = [] for neo in neos_csv: n = NearEarthObject(neo) neos.append(n) return neos
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ neos = [] with open(neo_csv_path, 'r') as f: csvfile = csv.DictReader(f) for neo_csv in csvfile: neos.append(NearEarthObject(**neo_csv)) return tuple(neos)
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename # TODO: Load data from csv file. # TODO: Where will the data be stored? with open(filename) as csv_file: reader = csv.DictReader(csv_file) for row in reader: # List version of the row attributes_list = list(row.items()) # Dict version of the row attributes_dict = dict(attributes_list) NEO = NearEarthObject(**attributes_dict) orbit = OrbitPath(**attributes_dict) NEO.update_orbits(orbit) # Getting the close approach date of this orbit orbit_date = orbit.close_approach_date if self.date_to_NEOs.get(orbit_date) is None: self.date_to_NEOs[f'{orbit_date}'] = [NEO] else: self.date_to_NEOs[f'{orbit_date}'].append(NEO) # Making sure only unique objects are contained if self.NEOs.get(NEO.name) is None: self.NEOs[NEO.name] = NEO else: pass return None
def generate_neo_object(self, val): """ Generates Neo object from every record of the csv file """ id_, name, nasa_jpl_url, absolute_magnitude_h, estimated_diameter_min_kilometers, close_approach_date, miss_distance_kilometers, is_potentially_hazardous_asteroid = val neo = NearEarthObject(id_ = id_, \ name = name, \ nasa_jpl_url = nasa_jpl_url, \ absolute_magnitude_h = absolute_magnitude_h, \ diameter_min_km = estimated_diameter_min_kilometers, \ is_potentially_hazardous_asteroid = is_potentially_hazardous_asteroid ) return neo
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ with open(neo_csv_path) as f: reader = csv.DictReader(f) neos = [] for line in reader: neo = NearEarthObject(**line) neos.append(neo) return neos
def load_data(self, filename=None): """ Loads data from a .csv file, instantiating Near Earth Objects and their OrbitPaths by: - Storing a dict of orbit date to list of NearEarthObject instances - Storing a dict of the Near Earth Object name to the single instance of NearEarthObject :param filename: :return: """ if not (filename or self.filename): raise Exception('Cannot load data, no filename provided') filename = filename or self.filename self.neo_date_dict.clear() self.neo_name_dict.clear() self.orbit_date_dict.clear() # TODO: Load data from csv file. # TODO: Where will the data be stored? with open(filename, newline='') as csvfile: csvreader = csv.reader(csvfile) c=-1 for row in csvreader: c=c+1 if(c!=0): neo=self.neo_name_dict.get(row[2]) if(neo is None): neo=NearEarthObject(id=row[0],name=row[2],absolute_magnitude_h=row[4],estimated_diameter_min_kilometers=row[5],estimated_diameter_max_kilometers=row[6],is_potentially_hazardous_asteroid=row[13]) self.neo_name_dict[row[2]]=neo orbit=OrbitPath(name=row[2],neo_reference_id=row[1],kilometers_per_hour=row[15],close_approach_date=row[17],miss_distance_kilometers=row[21],orbiting_body=row[23]) neo.update_orbits(orbit) if(self.orbit_date_dict.get(row[17]) is None): self.orbit_date_dict[row[17]]=list() self.orbit_date_dict[row[17]].append(orbit) if(self.neo_date_dict.get(row[17]) is None): self.neo_date_dict[row[17]]=list() if(neo not in self.neo_date_dict.values()): self.neo_date_dict[row[17]].append(neo) return None
def load_neos(neo_csv_path): """Read near-Earth object information from a CSV file. :param neo_csv_path: A path to a CSV file containing data about near-Earth objects. :return: A collection of `NearEarthObject`s. """ with open(neo_csv_path) as csv_file: reader = csv.DictReader(csv_file) return tuple((NearEarthObject(pdes=row.get('pdes'), name=row.get('name'), diameter=row.get('diameter'), pha=row.get('pha')) for row in reader))