def test_float_timestamp(self): d = Pendulum(1970, 1, 1, 0, 0, 0, 123456) self.assertEqual(0.123456, d.float_timestamp)
class RandomAgent(object): """The world's simplest agent!""" def __init__(self, action_space): self.action_space = action_space def act(self, observation, reward, done): return self.action_space.sample() if __name__ == '__main__': logging.basicConfig() log = logging.getLogger("pendulum") log.setLevel(level='INFO') pendulum = Pendulum() # specify which agent you want to use, # BonsaiAgent that uses trained Brain or # RandomAgent that randomly selects next action agent = BonsaiAgent() episode_count = 100 try: for i in range(episode_count): #start a new episode and get the new state pendulum.episode_start() state = pendulum.get_state()
def test_add_month_with_overflow(self): self.assertEqual(2, Pendulum(2012, 1, 31).add(months=1).month)
def test_start_of_week(self): d = Pendulum(1980, 8, 7, 12, 11, 9).start_of('week') self.assertPendulum(d, 1980, 8, 4, 0, 0, 0)
def test_end_of_week(self): d = Pendulum(1980, 8, 7, 12, 11, 9).end_of('week') self.assertPendulum(d, 1980, 8, 10, 23, 59, 59)
async def test_vlobs_updated_event_realm_created_after_subscribe( backend, alice_backend_sock, alice, alice2, realm, realm_created_by_self): realm_id = UUID("0000000000000000000000000000000A") await events_subscribe(alice_backend_sock) # New realm, should get events anyway with backend.event_bus.listen() as spy: realm_creator = alice if realm_created_by_self else alice2 # Create the realm await backend.realm.create( organization_id=realm_creator.organization_id, self_granted_role=RealmGrantedRole( realm_id=realm_id, user_id=realm_creator.user_id, certificate=b"<dummy>", role=RealmRole.OWNER, granted_by=realm_creator.device_id, granted_on=Pendulum(2000, 1, 2), ), ) # Create vlob in realm await backend.vlob.create( organization_id=realm_creator.organization_id, author=realm_creator.device_id, realm_id=realm_id, encryption_revision=1, vlob_id=VLOB_ID, timestamp=NOW, blob=b"v1", ) # Update vlob in realm await backend.vlob.update( organization_id=alice2.organization_id, author=alice2.device_id, encryption_revision=1, vlob_id=VLOB_ID, version=2, timestamp=NOW, blob=b"v2", ) # Wait for events to be processed by the backend await spy.wait_multiple_with_timeout([ "realm.roles_updated", "realm.vlobs_updated", "realm.vlobs_updated" ]) # Realm access granted rep = await events_listen_nowait(alice_backend_sock) assert rep == { "status": "ok", "event": "realm.roles_updated", "realm_id": realm_id, "role": RealmRole.OWNER, } # Create vlob in realm event if not realm_created_by_self: rep = await events_listen_nowait(alice_backend_sock) assert rep == { "status": "ok", "event": "realm.vlobs_updated", "realm_id": realm_id, "checkpoint": 1, "src_id": VLOB_ID, "src_version": 1, } # Update vlob in realm event rep = await events_listen_nowait(alice_backend_sock) assert rep == { "status": "ok", "event": "realm.vlobs_updated", "realm_id": realm_id, "checkpoint": 2, "src_id": VLOB_ID, "src_version": 2, } rep = await events_listen_nowait(alice_backend_sock) assert rep == {"status": "no_events"}
def test_pendulum(self): bob = Pendulum(30, 30, (30, 30)) bob.recompute_angle() self.assertAlmostEqual(bob.theta, 27.815, 3) self.assertAlmostEqual(bob.d_theta, -2.166, 3)
def test_max_with_instance(self): d1 = Pendulum(2012, 1, 1, 0, 0, 0) d2 = Pendulum(2099, 12, 31, 23, 59, 59).max_(d1) self.assertPendulum(d2, 2099, 12, 31, 23, 59, 59) d2 = Pendulum(2099, 12, 31, 23, 59, 59).maximum(d1) self.assertPendulum(d2, 2099, 12, 31, 23, 59, 59)
def test_not_equal_to_none(self): d1 = Pendulum(2000, 1, 1, 1, 2, 3) self.assertNotEqual(d1, None)
def test_min_with_instance(self): d1 = Pendulum(2013, 12, 31, 23, 59, 59) d2 = Pendulum(2012, 1, 1, 0, 0, 0).min_(d1) self.assertPendulum(d2, 2012, 1, 1, 0, 0, 0) d2 = Pendulum(2012, 1, 1, 0, 0, 0).minimum(d1) self.assertPendulum(d2, 2012, 1, 1, 0, 0, 0)
def test_max_with_now(self): d = Pendulum(2099, 12, 31, 23, 59, 59).max_() self.assertPendulum(d, 2099, 12, 31, 23, 59, 59) d = Pendulum(2099, 12, 31, 23, 59, 59).maximum() self.assertPendulum(d, 2099, 12, 31, 23, 59, 59)
def test_min_with_now(self): d = Pendulum(2012, 1, 1, 0, 0, 0).min_() self.assertPendulum(d, 2012, 1, 1, 0, 0, 0) d = Pendulum(2012, 1, 1, 0, 0, 0).minimum() self.assertPendulum(d, 2012, 1, 1, 0, 0, 0)
def test_int_timestamp_accuracy(self): self.skip_if_32bit() d = Pendulum(3000, 10, 1, 12, 23, 10, 999999) self.assertEqual(32527311790, d.int_timestamp)
def test_int_timestamp(self): d = Pendulum(1970, 1, 1, 0, 0, 0) self.assertEqual(0, d.int_timestamp) self.assertEqual(60, d.add(minutes=1, microseconds=123456).int_timestamp)
def test_set_test_now_pendulum_instance(self): test_now = Pendulum(2000, 11, 10, 12, 34, 56, 123456) Date.set_test_now(test_now) self.assertDate(Date.today(), 2000, 11, 10)
async def alice_nd_invitation(backend, alice): invitation = DeviceInvitation(DeviceID(f"{alice.user_id}@new_device"), alice.device_id, Pendulum(2000, 1, 2)) await backend.user.create_device_invitation(alice.organization_id, invitation) return invitation
# Parsec Cloud (https://parsec.cloud) Copyright (c) AGPLv3 2019 Scille SAS import pytest from uuid import UUID from pendulum import Pendulum from parsec.api.protocol import RealmRole from parsec.backend.realm import RealmGrantedRole from tests.backend.test_events import events_subscribe, events_listen_nowait NOW = Pendulum(2000, 1, 1) VLOB_ID = UUID("00000000000000000000000000000001") OTHER_VLOB_ID = UUID("00000000000000000000000000000002") YET_ANOTHER_VLOB_ID = UUID("00000000000000000000000000000003") REALM_ID = UUID("0000000000000000000000000000000A") @pytest.mark.trio async def test_vlobs_updated_event_ok(backend, alice_backend_sock, alice, alice2, realm, other_realm): # Not listened events with backend.event_bus.listen() as spy: await backend.vlob.create( organization_id=alice.organization_id, author=alice.device_id, realm_id=realm, encryption_revision=1, vlob_id=VLOB_ID, timestamp=NOW, blob=b"v1",
def test_format_with_locale(self): d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='Europe/Paris') f = ClassicFormatter() self.assertEqual( u'jeudi 25e jour de décembre 1975 02:15:16 +01:00', f.format(d, '%A %d%_t jour de %B %Y %I:%M:%S %p %_z', locale='fr'))
from time import sleep from numpy.linalg import inv,norm import math import time import random from pendulum import Pendulum import matplotlib.pylab as plt from specpath import acadoPath,dataRootPath,acadoTxtPath RANDOM_SEED = 999 # int((time.time()%10)*1000) print "Seed = %d" % RANDOM_SEED np .random.seed (RANDOM_SEED) random.seed (RANDOM_SEED) #env = Pendulum(2,withDisplay=True) # Continuous pendulum env = Pendulum(2,length=.5,mass=3.0,armature=.2,withDisplay=False) env.withSinCos = False # State is dim-3: (cosq,sinq,qdot) ... NX = env.nobs # ... training converges with q,qdot with 2x more neurones. NU = env.nu # Control is dim-1: joint torque env.vmax = 100. env.Kf = np.diagflat([ 0.2, 2. ]) env.modulo = False env.DT = 0.15 env.NDT = 1 #env.umax = 15. #env.umax = (15.,15.) env.umax = np.matrix([5.,10.]).T NSTEPS = 32
def test_unlocalizable_directive(self): d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local') f = ClassicFormatter() self.assertRaises(ValueError, f._localize_directive, d, '%8', 'en')
# x, y = np.meshgrid(np.linspace(-width, width, size), np.linspace(-width, width, size)) x, y = np.meshgrid(np.linspace(-width, width, size), np.linspace(-4, 3, size)) W, dW = np.zeros(x.shape), np.zeros(x.shape) for i in range(x.shape[0]): print(f'i = {i}') for j in range(y.shape[1]): phi = x[i, j] * np.ones(1) dphi = y[i, j] * np.ones(1) wave = { 'phi': phi, 'dphi': dphi, } pending_pendulum = Pendulum(wave, attributes) # find the LF value of each point W[i, j] = compute_total_energy_LF(pending_pendulum) dW[i, j] = compute_total_energy_LF_derivative_f(pending_pendulum) LF = {'width': width, 'size': size, 'LF': W, 'LFdot': dW} io.savemat('LF_total_energy.mat', LF) # plot fig = plt.figure(figsize=[9, 9]) plt.grid() plt.gca().set_aspect('equal', adjustable='box') mu = plt.contour(x, y, W, levels=30, colors='gray') cbar = plt.colorbar(mu) plt.xlabel(r'$\phi(t)$', size=20)
def test_strftime(self): f = ClassicFormatter() d = Pendulum(2016, 8, 28) m = re.match('(.*)', '%_TTT') self.assertRaises(ValueError, f._strftime, d, m, 'fr')
def test_start_of_week_from_week_start(self): d = Pendulum(1980, 8, 4).start_of('week') self.assertPendulum(d, 1980, 8, 4, 0, 0, 0)
def test_custom_formatters(self): d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local') f = ClassicFormatter() self.assertEqual('Thursday 25th of December 1975 02:15:16 PM -05:00', f.format(d, '%A %d%_t of %B %Y %I:%M:%S %p %_z'))
def test_end_of_week_from_week_end(self): d = Pendulum(1980, 8, 10).end_of('week') self.assertPendulum(d, 1980, 8, 10, 23, 59, 59)
def main(): direction_file_path = Path( "/RECH2/huziy/BC-MH/bc_mh_044deg/Samples/bc_mh_044deg_198001/pm1980010100_00000000p" ) sim_label = "mh_0.44" start_year = 1981 end_year = 2010 streamflow_internal_name = "streamflow" selected_staion_ids = constants.selected_station_ids_for_streamflow_validation # ====================================================== day = timedelta(days=1) t0 = datetime(2001, 1, 1) stamp_dates = [t0 + i * day for i in range(365)] print("stamp dates range {} ... {}".format(stamp_dates[0], stamp_dates[-1])) lake_fraction = None # establish the correspondence between the stations and model grid points with RPN(str(direction_file_path)) as r: assert isinstance(r, RPN) fldir = r.get_first_record_for_name("FLDR") flow_acc_area = r.get_first_record_for_name("FAA") lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec() # lake_fraction = r.get_first_record_for_name("LF1") cell_manager = CellManager(fldir, lons2d=lons, lats2d=lats, accumulation_area_km2=flow_acc_area) stations = stfl_stations.load_stations_from_csv( selected_ids=selected_staion_ids) station_to_model_point = cell_manager.get_model_points_for_stations( station_list=stations, lake_fraction=lake_fraction, nneighbours=8) # Update the end year if required max_year_st = -1 for station in station_to_model_point: y = max(station.get_list_of_complete_years()) if y >= max_year_st: max_year_st = y if end_year > max_year_st: print("Updated end_year to {}, because no obs data after...".format( max_year_st)) end_year = max_year_st # read model data mod_data_manager = DataManager( store_config={ "varname_mapping": { streamflow_internal_name: "STFA" }, "base_folder": str(direction_file_path.parent.parent), "data_source_type": data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT, "level_mapping": { streamflow_internal_name: VerticalLevel(-1, level_type=level_kinds.ARBITRARY) }, "offset_mapping": vname_to_offset_CRCM5, "filename_prefix_mapping": { streamflow_internal_name: "pm" } }) station_to_model_data = defaultdict(list) for year in range(start_year, end_year + 1): start = Pendulum(year, 1, 1) p_test = Period(start, start.add(years=1).subtract(microseconds=1)) stfl_mod = mod_data_manager.read_data_for_period( p_test, streamflow_internal_name) # convert to daily stfl_mod = stfl_mod.resample("D", "t", how="mean", closed="left", keep_attrs=True) assert isinstance(stfl_mod, xr.DataArray) for station, model_point in station_to_model_point.items(): assert isinstance(model_point, ModelPoint) ts1 = stfl_mod[:, model_point.ix, model_point.jy].to_series() station_to_model_data[station].append( pd.Series(index=stfl_mod.t.values, data=ts1)) # concatenate the timeseries for each point, if required if end_year - start_year + 1 > 1: for station in station_to_model_data: station_to_model_data[station] = pd.concat( station_to_model_data[station]) else: for station in station_to_model_data: station_to_model_data[station] = station_to_model_data[station][0] # calculate observed climatology station_to_climatology = OrderedDict() for s in sorted(station_to_model_point, key=lambda st: st.latitude, reverse=True): assert isinstance(s, Station) print(s.id, len(s.get_list_of_complete_years())) # Check if there are continuous years for the selected period common_years = set(s.get_list_of_complete_years()).intersection( set(range(start_year, end_year + 1))) if len(common_years) > 0: _, station_to_climatology[ s] = s.get_daily_climatology_for_complete_years_with_pandas( stamp_dates=stamp_dates, years=common_years) _, station_to_model_data[ s] = pandas_utils.get_daily_climatology_from_pandas_series( station_to_model_data[s], stamp_dates, years_of_interest=common_years) else: print( "Skipping {}, since it does not have enough data during the period of interest" .format(s.id)) # ---- Do the plotting ---- ncols = 4 nrows = len(station_to_climatology) // ncols nrows += int(not (len(station_to_climatology) % ncols == 0)) axes_list = [] plot_utils.apply_plot_params(width_cm=8 * ncols, height_cm=8 * nrows, font_size=8) fig = plt.figure() gs = GridSpec(nrows=nrows, ncols=ncols) for i, (s, clim) in enumerate(station_to_climatology.items()): assert isinstance(s, Station) row = i // ncols col = i % ncols print(row, col, nrows, ncols) # normalize by the drainage area if s.drainage_km2 is not None: station_to_model_data[ s] *= s.drainage_km2 / station_to_model_point[ s].accumulation_area if s.id in constants.stations_to_greyout: ax = fig.add_subplot(gs[row, col], facecolor="0.45") else: ax = fig.add_subplot(gs[row, col]) assert isinstance(ax, Axes) ax.plot(stamp_dates, clim, color="k", lw=2, label="Obs.") ax.plot(stamp_dates, station_to_model_data[s], color="r", lw=2, label="Mod.") ax.xaxis.set_major_formatter(FuncFormatter(format_month_label)) ax.xaxis.set_major_locator(MonthLocator(bymonthday=15)) ax.xaxis.set_minor_locator(MonthLocator(bymonthday=1)) ax.grid() ax.annotate(s.get_pp_name(), xy=(1.02, 1), xycoords="axes fraction", horizontalalignment="left", verticalalignment="top", fontsize=8, rotation=-90) last_date = stamp_dates[-1] last_date = last_date.replace( day=calendar.monthrange(last_date.year, last_date.month)[1]) ax.set_xlim(stamp_dates[0].replace(day=1), last_date) ymin, ymax = ax.get_ylim() ax.set_ylim(0, ymax) if s.drainage_km2 is not None: ax.set_title( "{}: ({:.1f}$^\circ$E, {:.1f}$^\circ$N, DA={:.0f} km$^2$)". format(s.id, s.longitude, s.latitude, s.drainage_km2)) else: ax.set_title( "{}: ({:.1f}$^\circ$E, {:.1f}$^\circ$N, DA not used)".format( s.id, s.longitude, s.latitude)) axes_list.append(ax) # plot the legend axes_list[-1].legend() if not img_folder.exists(): img_folder.mkdir() fig.tight_layout() img_file = img_folder / "{}_{}-{}_{}.png".format( sim_label, start_year, end_year, "-".join( sorted(s.id for s in station_to_climatology))) print("Saving {}".format(img_file)) fig.savefig(str(img_file), bbox_inches="tight", dpi=300)
async def test_new_reencryption_trigger_event(alice_core, bob_core, running_backend): with freeze_time("2000-01-02"): wid = await create_shared_workspace("w", alice_core, bob_core) with alice_core.event_bus.listen() as aspy, bob_core.event_bus.listen( ) as bspy: with freeze_time("2000-01-03"): await alice_core.user_fs.workspace_start_reencryption(wid) # Each workspace participant should get the message await aspy.wait_with_timeout( "sharing.updated", { "new_entry": WorkspaceEntry( name="w", id=wid, key=ANY, encryption_revision=2, encrypted_on=Pendulum(2000, 1, 3), role_cached_on=ANY, role=WorkspaceRole.OWNER, ), "previous_entry": WorkspaceEntry( name="w", id=wid, key=ANY, encryption_revision=1, encrypted_on=Pendulum(2000, 1, 2), role_cached_on=ANY, role=WorkspaceRole.OWNER, ), }, ) await bspy.wait_with_timeout( "sharing.updated", { "new_entry": WorkspaceEntry( name="w (shared by alice)", id=wid, key=ANY, encryption_revision=2, encrypted_on=Pendulum(2000, 1, 3), role_cached_on=ANY, role=WorkspaceRole.MANAGER, ), "previous_entry": WorkspaceEntry( name="w (shared by alice)", id=wid, key=ANY, encryption_revision=1, encrypted_on=Pendulum(2000, 1, 2), role_cached_on=ANY, role=WorkspaceRole.MANAGER, ), }, )
from pendulum import Pendulum from network import ActorCritic import numpy as np import pickle import os.path import random actorCritic = ActorCritic(Pendulum.state_size, Pendulum.action_size) experiences = [] if os.path.exists('experiences.p'): experiences = pickle.load(open("experiences.p", "rb")) print('experiences', len(experiences)) pendulum = Pendulum(Pendulum.random_theta()) round = 0 score = 1 iteration = 0 cumulative_iterations = 0 action0 = False while round < 27: state0 = pendulum.state() actions = actorCritic.run_actor([state0]) if random.random() < 0.25: action1 = np.random.choice(Pendulum.action_size, 1)[0] else: action1 = np.argmax(actions)
def test_add_days_positive(self): self.assertEqual(1, Pendulum(1975, 5, 31).add(days=1).day)
def test_days_in_month(self): d = Pendulum(2012, 5, 7) self.assertEqual(31, d.days_in_month)