Exemple #1
0
    def run(self):
        self.started = True
        (year, day) = self.id()
        input_file = "inputs/input%02d.txt" % (day)

        print(f"Running {year} day {day}... ", end="", flush=True)
        print("[PARSING] ", end="", flush=True)
        with Timer(print_title = False) as parse_time:
            self.parsed_input = self.parse_input_file(input_file)

        print("[PART1] ", end="", flush=True)

        with Timer(print_title = False) as p1_time:
            self.part1 = self.solve1(self.parsed_input)

        print("[PART2] ", end="", flush=True)
        with Timer(print_title = False) as p2_time:
            self.part2 = self.solve2(self.parsed_input)

        self.parse_time = parse_time.elapsed
        self.p1_time = p1_time.elapsed
        self.p2_time = p2_time.elapsed
        self.total_time = self.p1_time + self.p2_time + self.parse_time

        self.result = (self.part1, self.part2)
        self.expected_result = self.expected()
        if self.expected_result == self.result:
            self.status = "OK"
        else:
            self.status = "FAILED"

        print(f"[{self.status}]")
Exemple #2
0
    def test_multiple_instances(self):
        with Timer(title="Some title") as t1:
            time.sleep(1 / 5)

        with Timer(title="Some title") as t2:
            time.sleep(1 / 5)

        self.assertIsNot(t1, t2)
Exemple #3
0
    def test_stdout(self):
        with io.StringIO() as buffer1:
            with Timer(title="Some title", print_file=buffer1):
                time.sleep(1 / 5)

            output1 = buffer1.getvalue()

        self.assertRegex(output1, re.compile(r"\[Some title\] Total time \d+\.\d+ seconds.\n", re.UNICODE))

        with io.StringIO() as buffer2:
            with Timer(title="Different title", print_file=buffer2):
                time.sleep(1 / 5)

            output2 = buffer2.getvalue()

        self.assertRegex(output2, re.compile(r"\[Different title\] Total time \d+\.\d+ seconds.\n", re.UNICODE))
Exemple #4
0
    def test_basic(self):
        with Timer(title="Some title") as t:
            time.sleep(1 / 3)

        self.assertIsInstance(t, Timer)
        self.assertIsInstance(str(t), str)
        self.assertIsInstance(repr(t), str)
        self.assertIsInstance(float(t), float)
        self.assertGreaterEqual(t.elapsed, 1 / 4)  # a >= b

        self.assertTrue(hasattr(t, 'elapsed'))
        self.assertIsInstance(t.elapsed, float)

        self.assertEqual(float(t), t.elapsed)
Exemple #5
0
    def test_elapsed(self):
        t = Timer()

        self.assertEqual(0, t.elapsed)

        t.__enter__()
        time.sleep(1 / 7)
        t.__exit__()

        self.assertGreater(t.elapsed, 0)  # a > b
Exemple #6
0
    """

    indices = [
        np.arange(4 * 9, 5 * 9, 1),
        np.arange(0 * 9, 1 * 9, 1),
        np.arange(1 * 9, 2 * 9, 1),
        np.arange(3 * 9, 4 * 9, 1),
        np.arange(2 * 9, 3 * 9, 1),
    ]

    return joints[:, indices]


if __name__ == '__main__':
    batch_size = 5

    # morph and skin
    with Timer():
        vertices = get_mano_vertices(np.zeros([batch_size, 10]), np.zeros([batch_size, 48]))
        print(vertices)

    # save obj
    save_mano_obj(vertices, './')

    # remap joints for physical proximity
    from pose_autoencoders.pose_loader import get_poses

    remapped = remap_joints(get_poses())

    print('done')
x0 = experiment.simulation["x0"]
dxdt = experiment.functions["dxdt"]
V = experiment.functions["V"]
I = experiment.functions["I"]

####

###############################################################################
#                         ODE simulation
###############################################################################

# Plot simulated memristor behaviour
for solver in args.solvers:
    # Solve ODE iteratively using Euler's method
    if solver == "EU":
        with Timer(title="Euler"):
            print("Simulating with Euler solver")
            x_euler = euler_solver(dxdt, time, dt, x0)
            x = x_euler
            t = time
            title = "Euler"

    # Solve ODE iteratively using Runge-Kutta's method
    if solver == "RK4":
        with Timer(title="RK4"):
            print("Simulating with Runge-Kutta solver")
            x_rk4 = rk4_solver(dxdt, time, dt, x0)
            x = x_rk4
            t = time
            title = "Runge-Kutta"
fig_real, _, _ = plot_memristor(df["V"], df["I"], df["t"], "real")
fig_real.show()

###############################################################################
#                         ODE fitting
###############################################################################

x0 = 0.1
memristor = Yakopcic(input=Interpolated(x=time, y=input_voltage), x0=x0)
dxdt = memristor.dxdt
V = memristor.V
I = memristor.I

# Fit parameters to real data
with Timer(title="curve_fit"):
    print("Running curve_fit")
    popt, pcov = curve_fit(
        memristor.fit(),
        time,
        real_data,
        bounds=([0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0], [1, 1, 1, 10, 10, 1, 1, 10, 10, 1, 1]),
        p0=[0.11, 0.11, 0.5, 7.5, 2, 0.5, 0.75, 1, 5, 0.3, 0.5]
        # p0=[ 0.1, 0.1, 0.1, 1000, 1000, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ],
        # maxfev=100000
    )

    fitted_params = {p: v for p, v in zip(Yakopcic.parameters(), popt)}
    print("Fitted parameters",
          [(p, np.round(v, 2)) for p, v in zip(Yakopcic.parameters(), popt)])