def run(): """Requirements for Task 2E""" # Build list of stations stations = build_station_list() # Find 5 stations at which the current level is the highest stations_highest_rel_level_list = [] N = 5 for i in range(len(stations_highest_rel_level(stations, N))): stations_highest_rel_level_list.append( stations_highest_rel_level(stations, N)[i][0]) # Plot the water level for each of these stations over the past 10 days # First fetch the time history for a station for station in stations: if station.name in stations_highest_rel_level_list: dt = 10 dates, levels = fetch_measure_levels( station.measure_id, dt=datetime.timedelta(days=dt)) # This gives list of dates and levels to be passed into a plot plot_water_level(station, dates, levels) else: pass
def test_plot(): # Build list of stations stations = build_station_list() # Find 5 stations at which the current level is the highest stations_highest_rel_level_list = [] stations_highest_rel_level_list_b = [] N = 5 for i in range(len(stations_highest_rel_level(stations, N))): stations_highest_rel_level_list.append( stations_highest_rel_level(stations, N)[i][0]) # Do the same again for 6 stations for i in range(len(stations_highest_rel_level(stations, N + 1))): stations_highest_rel_level_list_b.append( stations_highest_rel_level(stations, N + 1)[i][0]) # Check these stations have the highest water level relative_level_one = None relative_level_two = None for station in stations: if station.name == stations_highest_rel_level_list[4]: relative_level_one = station.relative_water_level() if station.name == stations_highest_rel_level_list_b[5]: relative_level_two = station.relative_water_level() else: pass assert relative_level_one > relative_level_two
def test_stations_highest_rel_level(): N = randint(1, 1000) assert len(stations_highest_rel_level(stations, N)) > 0 assert type(stations_highest_rel_level(stations, N)) == list for item in stations_highest_rel_level(stations, N): assert type(item) == tuple assert type(item[0]) == str assert type(item[1]) == float
def test_stations_highest_rel_level(): stations = generate_test_station() stations[0].typical_range, stations[0].latest_level = (0.1, 0.5), 0.25 stations[1].typical_range, stations[1].latest_level = (0.1, 1.5), 0.25 stations[2].typical_range, stations[2].latest_level = (0.0, 2.5), 3.0 stations[3].typical_range, stations[3].latest_level = (0.1, 0.5), 1.0 stations[4].typical_range, stations[4].latest_level = (0.1, 5), 10.0 stations[5].typical_range, stations[5].latest_level = (0.1, 0.5), 3.0 stations[6].typical_range, stations[6].latest_level = (1.0, 0.5), 1.0 [s1, s2, s3, s4, s5, s6, s7] = stations assert stations_highest_rel_level(stations, 2) == [s6, s4] assert len(stations_highest_rel_level(stations, 7)) != 7 assert stations_highest_rel_level(stations, 6) == [s6, s4, s5, s3, s1, s2]
def run(): N = 5 DT = 2 P = 4 stations = build_station_list() update_water_levels(stations) at_risk_stations = stations_highest_rel_level(stations, N) for station in at_risk_stations: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=DT)) # plot real data date_nums = date2num(dates) - date2num(dates[0]) plt.plot(date_nums, levels, color="orange") # plot line of best fit plot_water_level_with_fit(station, dates, levels, P) # plot high/low plt.axhline(station.typical_range[0], linestyle="dashed", color="green") plt.axhline(station.typical_range[1], linestyle="dashed", color="red") plt.legend(("Real values", "Best fit", "Typical low", "Typical high")) plt.show()
def run(): stations = stationdata.build_station_list() stationdata.update_water_levels(stations) stations = station.consistant_typical_range_stations(stations) top10Stations = flood.stations_highest_rel_level(stations, 10) topStations = [] topStationsDates = [] topStationsLevels = [] counter = 0 NUMBER_PLOTS = 5 for st in top10Stations: stationDates, stationLevels = datafetcher.fetch_measure_levels( st.measure_id, timedelta(days=2)) # only consistant ones get saved if all(isinstance(x, (int, float)) for x in stationLevels) and stationDates != []: counter += 1 topStations.append(st) topStationsDates.append(stationDates) topStationsLevels.append(stationLevels) if counter == NUMBER_PLOTS: break # single object input # plot.plot_water_level_with_fit(topStations[0], topStationsDates[0], topStationsLevels[0], 4) # list of object input plot.plot_water_level_with_fit(topStations, topStationsDates, topStationsLevels, 4)
def run(N=5): #get the stations data and sorted by latest water level stations = build_station_list() #get the six stations with the largest water level highest_five = stations_highest_rel_level(stations, N) # NB Graylingwell does not give any date data #set the length of time for wanted water level dt = 365 i = 0 for station in highest_five: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) if len(dates) == 0: print("This station:\n", station, "\ndoes not give any date data") continue plt.figure(i) plot_water_level_with_fit(station, dates, levels, 10) i += 1 print(i, ' out of ', N, ' were printed') plt.show()
def run(): #get the stations data and sorted by latest water level stations = build_station_list() #get the six stations with the largest water level highest_six = stations_highest_rel_level(stations, 6) #set the length of time for wanted water level dt = 10 #plotting water level against time of those six stations i = 0 station_name = [] date_and_level = [] for station in highest_six: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) plt.subplots() plot_water_levels(station, dates, levels) station_name.append(station.name) date_and_level.append((dates, levels)) i += 1 plt.show() print(station_name) return station_name, date_and_level
def test_highest_rel_level_lists_stations_in_order_from_the_highest_to_lowest_relative_water_level(): N = 3 s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) river = "River X" town = "My Town" trange1 = (1.1, 3.0) trange2 = (-1.2, 2.5) trange3 = (-1.67, 3.14) trange4 = (0, 1) s1 = MonitoringStation(s_id, m_id, label, coord, trange1, river, town) s2 = MonitoringStation(s_id, m_id, label, coord, trange2, river, town) s3 = MonitoringStation(s_id, m_id, label, coord, trange3, river, town) s4 = MonitoringStation(s_id, m_id, label, coord, trange4, river, town) s1.latest_level = 4.1 s2.latest_level = 1.0 s3.latest_level = 1.9 s4.latest_level = None stations = [s1, s2, s3, s4] expected_stations = [s1, s3, s2] actual_stations = stations_highest_rel_level(stations, N) assert expected_stations == actual_stations
def test_stations_level_over_threshold_and_stations_highest_rel_level(): # two functions are very similar so tested together # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) list_s = [] list_s.append(s) list_s = list_s * 3 for station in list_s: station.latest_level = 3.4445 #disable the update water level function when using this test function list_of_stations = stations_level_over_threshold(list_s, 1) assert len(list_of_stations) == 3 for station in list_s: station.latest_level = -2.3 list_of_stations = stations_level_over_threshold(list_s, 0) assert len(list_of_stations) == 3 list_of_stations = stations_highest_rel_level(list_s, 2) assert len(list_of_stations) == 2
def run(): #Building list of stations to fetch data for stations = build_station_list() update_water_levels(stations) N_stations_at_risk = stations_highest_rel_level(stations, 5) #Variables dt = 2 # Fetch data over past 2 days p = 4 #Polynomial degree i = 0 #Counter for s in N_stations_at_risk: i += 1 dates, levels = fetch_measure_levels( s[0].measure_id, dt=datetime.timedelta(days=dt)) #Compiling dates and water levels if not dates: print('Insufficient Data') else: d0, poly = polyfit(dates, levels, p) #Creating polynomial #Plotting data, polynomial and typical high/low plt.subplot(3, 3, i + 1) plt.plot(dates, levels) plt.plot(dates, poly((mpl.dates.date2num(dates) - d0))) plt.axhline(y=s[0].typical_range[0], color='y') plt.axhline(y=s[0].typical_range[1], color='r') plt.xlabel('date') plt.ylabel('water level (m)') plt.xticks(rotation=45) plt.title(s[0].name) plt.show()
def test_stations_highest_rel_level(): N = 10 a = stations_highest_rel_level(stations, N) for n in range(N - 1): assert a[n][1] >= a[n + 1][1] return
def run(): # Build list of stations stations = build_station_list() update_water_levels(stations) output_list = stations_highest_rel_level(stations, 10) print(output_list)
def run(): # Build list of stations stations = build_station_list() # Update latest level data for all stations update_water_levels(stations) #create list of names stations to be plotted with their measure_id highest_levels=stations_highest_rel_level(stations, 5) stations_to_plot = [] #retrieve the rest of station data for the high for j in highest_levels: for i in stations: if j[0] == i.name: stations_to_plot.append(i) dt = 10 #plot data for each station for i in stations_to_plot: dates, levels = fetch_measure_levels(i.measure_id, dt=timedelta(days=dt)) plot_water_levels(i.name,dates,levels) plt.show()
def run(): """Requirement for task 2G""" #build a list of stations stations = build_station_list() update_water_levels(stations) stations_at_risk = stations_highest_rel_level(stations, 10) # Fetch data over past 2 days dt = 0.5 danger_stations = [] for station in stations_at_risk: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) try: prediction = predicted_value(dates, levels) danger_level = danger_assigning(station, prediction) if danger_level is not None: if danger_level[1] == "severe" or danger_level[1] == "high": danger_stations.append(danger_level) except: pass sorted_stations = sorted_by_key(danger_stations, 1, reverse=True) for i in sorted_stations: print(i[0] + ":" + i[1])
def run(): stations = build_station_list() update_water_levels(stations) N_stations_at_risk = stations_highest_rel_level( stations, 10) #create a list of N stations at risk for a in N_stations_at_risk: print(a[0].name + ' ' + str(a[1])) #print name and station level
def test_stations_highest_rel_level(): n=stations_highest_rel_level(s1, 10) assert type(n) == list assert len(n) == 10 for i in range(len(n)): assert type(n[i][0])== str assert type(n[i][1])== float assert type(n[i])== tuple
def run(): """ Requirement for Task 1C""" stations = build_station_list() update_water_levels(stations) dangerous_stations = stations_highest_rel_level(stations, 10) for i in dangerous_stations: print(i.name, " :", i.relative_water_level())
def test_flood_2(): stations = build_station_list() stations_highest_rel_level_list = [] stations_highest_rel_level_list = stations_highest_rel_level(stations, 10) if len(stations_highest_rel_level_list) > 10: assert stations_highest_rel_level_list[9][1] == stations_highest_rel_level_list[len(stations_highest_rel_level_list)-1][1] else: assert len(stations_highest_rel_level_list) == 10
def run(): stations = build_station_list() update_water_levels(stations) first_N_stations = stations_highest_rel_level(stations, 10) for i in range(len(first_N_stations)): print(first_N_stations[i][0].name, first_N_stations[i][1])
def test_stations_highest_rel_level(): stations = build_station_list() update_water_levels(stations) Aurimas = MonitoringStation(1234, 1234, "Aurimas", (1234, 1234), (0.5, 1), "Sarva", "Vilnius", 1997 - 11 - 12, 1234) Aurimas.latest_level = 3 stations.append(Aurimas) x = stations_highest_rel_level(stations, 10) assert Aurimas in x
def test_stations_highest_rel_level(): # Build list of stations stations = build_station_list() update_water_levels(stations) shortlist = flood.stations_highest_rel_level(stations, 10) assert len(shortlist) == 10 assert shortlist[0].relative_water_level( ) >= shortlist[1].relative_water_level()
def test_stations_highest_rel_level(): stations = stationdata.build_station_list(test=True) stationdata.update_water_levels(stations, use_cache=True) for N in [3, 7, 18]: # check the size and order of the list for various values of N ordered_stations = flood.stations_highest_rel_level(stations, N) assert (len(ordered_stations) <= N) assert (all(n.relative_water_level() <= p.relative_water_level() for n, p in zip(ordered_stations[1:], ordered_stations)))
def run(): # Build list of stations stations = build_station_list() # Update water level update_water_levels(stations) # 10 Stations at which the current relative water level is highest for s in stations_highest_rel_level(stations, 10): print("{} {}".format(s.name, s.relative_water_level()))
def test_stations_highest_rel_level(): #Initialise variables stations = build_station_list() update_water_levels(stations) N = 10 #Calls for N stations with highest relative water level shrl = flood.stations_highest_rel_level(stations, N) #Checks N stations are returned if len(shrl) != N: raise ValueError("{} stations called for; {} returned".format( N, len(shrl)))
def run(): # Build list of stations stations = build_station_list() N = 5 update_water_levels(stations) list_of_5_stations_greatest_level = stations_highest_rel_level(stations, N) dt = 10 for station in list_of_5_stations_greatest_level: dates, levels = fetch_measure_levels(station.measure_id, dt=datetime.timedelta(days=dt)) plot_water_levels(station, dates, levels)
def run(): stations = build_station_list() update_water_levels(stations) stations_relative = stations_highest_rel_level(stations, 5) dt = 10 for station in stations_relative: dates, levels = fetch_measure_levels(station.measure_id, datetime.timedelta(days=dt)) plot_water_levels(station, dates, levels)
def run(): """Requrement for Task 2C""" stations = build_station_list() update_water_levels(stations) #Creates list of stations with 10 greatest relative water levels #sorted by relative water level shrl = stations_highest_rel_level(stations, 10) for station in shrl: print("{} {}".format(station[0], station[1]))
def run(): # Build list of stations stations = build_station_list() # Update latest level data for all stations update_water_levels(stations) highest_levels = stations_highest_rel_level(stations, 10) for i in highest_levels: print(i[0], i[1])
def run(): # Build list of stations stations = build_station_list() # Update latest level data for all stations update_water_levels(stations) # Print station and latest level for first 5 stations in list stations_at_risk = stations_highest_rel_level(stations, 10) for station in stations_at_risk: print(station)