"""This file implements the gym environment of minitaur standing with four legs.

"""
import math
import operator
import random
import time
from gym import spaces
import numpy as np
from pybullet_envs.minitaur.envs import minitaur_gym_env
from pybullet_envs.minitaur.envs import minitaur_logging

INIT_EXTENSION_POS = 2.0
INIT_SWING_POS = 0.0
DESIRED_PITCH = 0
NUM_LEGS = 4
NUM_MOTORS = 2 * NUM_LEGS
STEP_PERIOD = 1.0 / 3.0  # Three steps per second.
STEP_AMPLITUDE = 0.75
PERTURBATION_TOTAL_STEP = 100
MOVING_FLOOR_TOTAL_STEP = 100
DERPY_V0_URDF_VERSION = minitaur_gym_env.DERPY_V0_URDF_VERSION
RAINBOW_DASH_V0_URDF_VERSION = minitaur_gym_env.RAINBOW_DASH_V0_URDF_VERSION

class MinitaurFourLegStandEnv(minitaur_gym_env.MinitaurGymEnv):
  """The gym environment for the minitaur.

  It simulates the a minitaur standing with four legs. The state space
  include the orientation of the torso, and the action space is the desired
  motor angle for each motor. The reward function is based on how close the
  action to zero and the height of the robot base. It prefers a similar pose to
  the signal while keeping balance.
  """
  metadata = {"render.modes": ["human", "rgb_array"], "video.frames_per_second": 166}

  def __init__(self,
               urdf_version=None,
               hard_reset=True,
               remove_default_joint_damping=True,
               control_latency=0.0,
               pd_latency=0.0,
               on_rack=False,
               motor_kp=1.0,
               motor_kd=0.02,
               render=False,
               env_randomizer=None,
               use_angular_velocity_in_observation=False,
               use_motor_angle_in_observation=False,
               control_time_step=0.006,
               action_repeat=6,
               log_path=None):
    """Initialize the minitaur alternating legs gym environment.

    Args:
      urdf_version: [DEFAULT_URDF_VERSION, DERPY_V0_URDF_VERSION,
        RAINBOW_DASH_V0_URDF_VERSION] are allowable
        versions. If None, DEFAULT_URDF_VERSION is used. DERPY_V0_URDF_VERSION
        is the result of first pass system identification for derpy.
        We will have a different URDF and related Minitaur class each time we
        perform system identification. While the majority of the code of the
        class remains the same, some code changes (e.g. the constraint location
        might change). __init__() will choose the right Minitaur class from
        different minitaur modules based on urdf_version.
      hard_reset: Whether to wipe the simulation and load everything when reset
        is called. If set to false, reset just place the minitaur back to start
        position and set its pose to initial configuration.
      remove_default_joint_damping: Whether to remove the default joint damping.
      control_latency: It is the delay in the controller between when an
        observation is made at some point, and when that reading is reported
        back to the Neural Network.
      pd_latency: latency of the PD controller loop. PD calculates PWM based on
        the motor angle and velocity. The latency measures the time between when
        the motor angle and velocity are observed on the microcontroller and
        when the true state happens on the motor. It is typically (0.001-
        0.002s).
      on_rack: Whether to place the minitaur on rack. This is only used to debug
        the walking gait. In this mode, the minitaur's base is hung midair so
        that its walking gait is clearer to visualize.
      motor_kp: The P gain of the motor.
      motor_kd: The D gain of the motor.
      render: Whether to render the simulation.
      env_randomizer: An instance (or a list) of EnvRanzomier(s) that can
        randomize the environment during when env.reset() is called and add
        perturbations when env.step() is called.
      use_angular_velocity_in_observation: Whether to include roll_dot and
        pitch_dot of the base in the observation.
      use_motor_angle_in_observation: Whether to include motor angles in the
        observation.
      control_time_step: The time step between two successive control signals.
      action_repeat: The number of simulation steps before actions are applied.
      log_path: The path to write out logs. For the details of logging, refer to
        minitaur_logging.proto.
    """
    # _swing_offset and _extension_offset is to mimic the motor zero-calibration
    # errors.
    self._swing_offset = np.zeros(NUM_LEGS)
    self._extension_offset = np.zeros(NUM_LEGS)
    self._use_angular_velocity_in_observation = use_motor_angle_in_observation
    self._use_motor_angle_in_observation = use_motor_angle_in_observation
    super(MinitaurFourLegStandEnv,
          self).__init__(urdf_version=urdf_version,
                         control_time_step=control_time_step,
                         action_repeat=action_repeat,
                         remove_default_joint_damping=remove_default_joint_damping,
                         accurate_motor_model_enabled=True,
                         motor_overheat_protection=True,
                         hard_reset=hard_reset,
                         motor_kp=motor_kp,
                         motor_kd=motor_kd,
                         control_latency=control_latency,
                         pd_latency=pd_latency,
                         on_rack=on_rack,
                         render=render,
                         env_randomizer=env_randomizer,
                         reflection=False,
                         log_path=log_path)

    action_dim = 4
    action_low = np.array([-1.0] * action_dim)
    action_high = -action_low
    self.action_space = spaces.Box(action_low, action_high)

    self._cam_dist = 1.0
    self._cam_yaw = 30
    self._cam_pitch = -30
    self._perturbation_magnitude = 0.0
    self._sign = 1.0
    self._cur_ori = [0, 0, 0, 1]
    self._goal_ori = [0, 0, 0, 1]

  def reset(self):
    self.desired_pitch = DESIRED_PITCH
    # In this environment, the actions are
    # [swing leg 1, swing leg 2, swing leg 3, swing leg 4,
    #  extension leg 1, extension leg 2, extension leg 3, extension leg 4]
    init_pose = [
        INIT_SWING_POS + self._swing_offset[0], INIT_SWING_POS + self._swing_offset[1],
        INIT_SWING_POS + self._swing_offset[2], INIT_SWING_POS + self._swing_offset[3],
        INIT_EXTENSION_POS + self._extension_offset[0],
        INIT_EXTENSION_POS + self._extension_offset[1],
        INIT_EXTENSION_POS + self._extension_offset[2],
        INIT_EXTENSION_POS + self._extension_offset[3]
    ]
    initial_motor_angles = self._convert_from_leg_model(init_pose)
    self._pybullet_client.resetBasePositionAndOrientation(0, [0, 0, 0], [0, 0, 0, 1])
    super(MinitaurFourLegStandEnv, self).reset(initial_motor_angles=initial_motor_angles,
                                               reset_duration=0.5)
    return self._get_observation()

  def step(self, action):
    """Step forward the simulation, given the action.

    Args:
      action: A list of desired motor angles for eight motors.

    Returns:
      observations: Roll, pitch of the base, and roll, pitch rate.
      reward: The reward for the current state-action pair.
      done: Whether the episode has ended.
      info: A dictionary that stores diagnostic information.

    Raises:
      ValueError: The action dimension is not the same as the number of motors.
      ValueError: The magnitude of actions is out of bounds.
    """
    if self._is_render:
      # Sleep, otherwise the computation takes less time than real time,
      # which will make the visualization like a fast-forward video.
      time_spent = time.time() - self._last_frame_time
      self._last_frame_time = time.time()
      time_to_sleep = self.control_time_step - time_spent
      if time_to_sleep > 0:
        time.sleep(time_to_sleep)
      base_pos = self.minitaur.GetBasePosition()
      # Keep the previous orientation of the camera set by the user.
      [yaw, pitch, dist] = self._pybullet_client.getDebugVisualizerCamera()[8:11]
      self._pybullet_client.resetDebugVisualizerCamera(dist, yaw, pitch, base_pos)
    action = self._transform_action_to_motor_command(action)
    t = self._env_step_counter % MOVING_FLOOR_TOTAL_STEP
    if t == 0:
      self.seed()
      orientation_x = random.uniform(-0.2, 0.2)
      self.seed()
      orientation_y = random.uniform(-0.2, 0.2)
      _, self._cur_ori = self._pybullet_client.getBasePositionAndOrientation(0)
      self._goal_ori = self._pybullet_client.getQuaternionFromEuler(
          [orientation_x, orientation_y, 0])
    t = float(float(t) / float(MOVING_FLOOR_TOTAL_STEP))
    ori = map(operator.add, [x * (1.0 - t) for x in self._cur_ori],
              [x * t for x in self._goal_ori])
    ori = list(ori)
    print("ori=", ori)
    self._pybullet_client.resetBasePositionAndOrientation(0, [0, 0, 0], ori)
    if self._env_step_counter % PERTURBATION_TOTAL_STEP == 0:
      self._perturbation_magnitude = random.uniform(0.0, 0.0)
      if self._sign < 0.5:
        self._sign = 1.0
      else:
        self._sign = 0.0
    self._pybullet_client.applyExternalForce(
        objectUniqueId=1,
        linkIndex=-1,
        forceObj=[self._sign * self._perturbation_magnitude, 0.0, 0.0],
        posObj=[0.0, 0.0, 0.0],
        flags=self._pybullet_client.LINK_FRAME)
    self.minitaur.Step(action)
    self._env_step_counter += 1
    done = self._termination()
    obs = self._get_true_observation()
    reward = self._reward()
    if self._log_path is not None:
      minitaur_logging.update_episode_proto(self._episode_proto, self.minitaur, action,
                                            self._env_step_counter)
    if done:
      self.minitaur.Terminate()
    return np.array(self._get_observation()), reward, done, {}

  def _convert_from_leg_model(self, leg_pose):
    motor_pose = np.zeros(NUM_MOTORS)
    for i in range(NUM_LEGS):
      motor_pose[2 * i] = leg_pose[NUM_LEGS + i] - (-1)**(i / 2) * leg_pose[i]
      motor_pose[2 * i + 1] = leg_pose[NUM_LEGS + i] + (-1)**(i / 2) * leg_pose[i]
    return motor_pose

  def _signal(self, t):
    initial_pose = np.array([
        INIT_SWING_POS, INIT_SWING_POS, INIT_SWING_POS, INIT_SWING_POS, INIT_EXTENSION_POS,
        INIT_EXTENSION_POS, INIT_EXTENSION_POS, INIT_EXTENSION_POS
    ])
    signal = initial_pose
    return signal

  def _transform_action_to_motor_command(self, action):
    # Add swing_offset and extension_offset to mimick the motor zero-calibration
    # errors.
    real_action = np.array([0.0] * 8)
    real_action[4:8] = action + self._extension_offset
    real_action[0:4] = self._swing_offset
    real_action += self._signal(self.minitaur.GetTimeSinceReset())
    real_action = self._convert_from_leg_model(real_action)
    return real_action

  def is_fallen(self):
    """Decide whether the minitaur has fallen.

    # TODO(yunfeibai): choose the fallen option for force perturbation and
    moving floor, and update the comments.

    If the up directions between the base and the world is large (the dot
    product is smaller than 0.85), or the robot base is lower than 0.24, the
    minitaur is considered fallen.

    Returns:
      Boolean value that indicates whether the minitaur has fallen.
    """
    orientation = self.minitaur.GetBaseOrientation()
    rot_mat = self._pybullet_client.getMatrixFromQuaternion(orientation)
    local_up = rot_mat[6:]
    _, _, height = self.minitaur.GetBasePosition()
    local_global_up_dot_product = np.dot(np.asarray([0, 0, 1]), np.asarray(local_up))
    return local_global_up_dot_product < 0.85 or height < 0.15

  def _reward(self):
    roll, pitch, _ = self.minitaur.GetBaseRollPitchYaw()
    return 1.0 / (0.001 + math.fabs(roll) + math.fabs(pitch))

  def _get_observation(self):
    """Get the true observations of this environment.

    It includes the roll, pitch, roll dot, pitch dot of the base, and the motor
    angles.

    Returns:
      The observation list.
    """
    roll, pitch, _ = self.minitaur.GetBaseRollPitchYaw()
    observation = [roll, pitch]
    if self._use_angular_velocity_in_observation:
      roll_rate, pitch_rate, _ = self.minitaur.GetBaseRollPitchYawRate()
      observation.extend([roll_rate, pitch_rate])
    if self._use_motor_angle_in_observation:
      observation.extend(self.minitaur.GetMotorAngles().tolist())
    self._observation = np.array(observation)
    return self._observation

  def _get_true_observation(self):
    """Get the true observations of this environment.

    It includes the roll, pitch, roll dot, pitch dot of the base, and the motor
    angles.

    Returns:
      The observation list.
    """
    roll, pitch, _ = self.minitaur.GetBaseRollPitchYaw()
    observation = [roll, pitch]
    if self._use_angular_velocity_in_observation:
      roll_rate, pitch_rate, _ = self.minitaur.GetBaseRollPitchYawRate()
      observation.extend([roll_rate, pitch_rate])
    if self._use_motor_angle_in_observation:
      observation.extend(self.minitaur.GetMotorAngles().tolist())

    self._observation = np.array(observation)
    return self._observation

  def _get_observation_upper_bound(self):
    """Get the upper bound of the observation.

    Returns:
      The upper bound of an observation. See GetObservation() for the details
        of each element of an observation.
    """
    upper_bound = [2 * math.pi] * 2  # Roll, pitch the base.
    if self._use_angular_velocity_in_observation:
      upper_bound.extend([2 * math.pi / self._time_step] * 2)
    if self._use_motor_angle_in_observation:
      upper_bound.extend([2 * math.pi] * 8)
    return np.array(upper_bound)

  def _get_observation_lower_bound(self):
    lower_bound = -self._get_observation_upper_bound()
    return lower_bound

  def set_swing_offset(self, value):
    """Set the swing offset of each leg.

    It is to mimic the motor zero-calibration errors.

    Args:
      value: A list of four values.
    """
    self._swing_offset = value

  def set_extension_offset(self, value):
    """Set the extension offset of each leg.

    It is to mimic the motor zero-calibration errors.

    Args:
      value: A list of four values.
    """
    self._extension_offset = value

  def set_desired_pitch(self, value):
    """Set the desired pitch of the base, which is a user input.

    Args:
      value: A scalar.
    """
    self.desired_pitch = value


import numpy as np
import read_sph_data as rsd
import warnings
from constants import *
import matplotlib.pyplot as plt

warnings.filterwarnings('ignore')

simdir = "/home/hatfull/data/ulfhednar/dynamical/d003u_b002u_1.195M_0.32M_40k_1_1.2G0.32C0.32D_ncooling1/"

t = np.load("data/t.npy")
erad = np.load("data/erad.npy")

erad = erad / G / Ms**2. * Rs
t = t / codeunits * days

plt.plot(t,erad/1.e45,color='k')
plt.xlabel("time [days]")
plt.ylabel("$E_{\\rm{rad}}$ [$10^{45}$ ergs s$^{-1}$]")

plt.title(simdir.split("/")[-2])
plt.tight_layout()

plt.savefig(simdir.split("/")[-2].split("_")[0]+"_erad_t.png")

plt.show()


import numpy as np
import random
import glob
from pydub import AudioSegment
import matplotlib.pyplot as plt

def create_data():
    plot = "Y" == input("Show FFT visualizations of data? (Y/N) ")
    directory_num = int(input("How many directories to process? "))
    data_list = []
    for x in range(directory_num):
        directory_path = input("Enter directory for label " + str(x) + ": ")
        data_list.extend(preprocess(directory_path, x, plot))
    random.shuffle(data_list)

    # calculate 60-20-20 split of data between train/validation/test sets
    total = len(data_list)
    train_num = total * 6 // 10
    validation_num = total * 2 // 10

    # create and write train/validation/test sets
    trainFile = open("train.dat", "w")
    for x in data_list[:train_num]:
        trainFile.write(x)
    trainFile.close()

    validationFile = open("validation.dat", "w")
    for x in data_list[train_num:train_num + validation_num]:
        validationFile.write(x)
    validationFile.close()

    testFile = open("test.dat", "w")
    for x in data_list[train_num + validation_num:]:
        testFile.write(x)
    testFile.close()

def preprocess(directory_path, label, plot):
    """ Performs the FFT on all mp3 files in the specified directory and
    returns a list of data features + labels.
    directory_path: str path to directory (no ending forward slash)
    label: the (integer) label to be appended to each data example
    plot: boolean condition whether to visualize FFT
    (actual + data representation) """
    # obtain file paths for each data example
    file_paths = glob.glob(directory_path + "/*")
    data_list = []

    # set top frequency in frequency range
    freq_top = 4000

    # set the number of bins / partitions
    bins = 50

    # process each audio file
    for file_path in file_paths:
        # extract audio / related information
        audio = AudioSegment.from_mp3(file_path)
        sample_rate = audio.frame_rate
        raw_data = audio.raw_data
        y = np.fromstring(raw_data, dtype=np.int16)
        samples = len(y)

        # determine partitions in frequency range
        freq_res = sample_rate / samples
        x = int(np.ceil(freq_top * samples / sample_rate))
        x = x - (x % bins)

        # isolate desired frequency range
        y = np.abs(np.fft.fft(y)[:x])

        # take average over each bin / partition
        a = np.arange(y.size) // (x // bins)
        y = np.bincount(a, y) / np.bincount(a)

        # normalize amplitudes to range [0, 1]
        y = y / max(y)
        data_list.append(' '.join(str(i) for i in y) + ' ' + str(label) + '\n')

        # plot
        if plot:
            print(file_path)
            fig = plt.figure()
            ax1 = fig.add_subplot(211)
            ax2 = fig.add_subplot(212)

            x_var = np.arange(len(y)) * freq_res * (x // bins)
            ax1.plot(x_var, y)

            y = np.fromstring(raw_data, dtype=np.int16)
            y = np.abs(np.fft.fft(y)[:x])
            y = y / max(y)
            x_var = np.arange(len(y)) * freq_res
            ax2.plot(x_var, y)

            plt.show()

    return data_list

def test_audio(file_path):
    # set top frequency in frequency range
    freq_top = 4000

    # set the number of bins / partitions
    bins = 50

    # extract audio / related information
    audio = AudioSegment.from_mp3(file_path)
    sample_rate = audio.frame_rate
    raw_data = audio.raw_data
    y = np.fromstring(raw_data, dtype=np.int16)
    samples = len(y)

    # determine partitions in frequency range
    x = int(np.ceil(freq_top * samples / sample_rate))
    x = x - (x % bins)

    # isolate desired frequency range
    y = np.abs(np.fft.fft(y)[:x])

    # take average over each bin / partition
    a = np.arange(y.size) // (x // bins)
    y = np.bincount(a, y) / np.bincount(a)

    # normalize amplitudes to range [0, 1]
    y = y / max(y)
    return ' '.join(str(i) for i in y)


import json
import csv
import serial
import time
import threading
import tkinter as tk
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from scipy.spatial.transform import Rotation as R
from collections import deque
import os

# Define the serial port and baud rate
port = '/dev/cu.usbserial-02898D2B'  # Change this to the appropriate serial port
baud_rate = 115200

# Define the output CSV file
output_file = 'quaternion_data.csv'

# Initialize the serial connection
ser = serial.Serial(port, baud_rate)

data = deque(maxlen=1000)
angles = deque(maxlen=100)
dataout = deque(maxlen=1000)

euler_sequence = 'xyz'  # Default Euler sequence

def choose_folder():
    global file_path
    default_path = os.path.join(os.path.expanduser('~'), 'Desktop')
    file_path = filedialog.askdirectory(initialdir=default_path)

def save_data():
    try:
        subject_name = subject_entry.get()  # Get the subject name from the input box
        log_text("Saved data for subject: {}".format(subject_name))
        write_data_to_csv(dataout, subject_name + 'data')  # Use subject_name as the filename
    except NameError as e:
        log_text("Error: {}".format(e))

def write_data_to_csv(dataout, subject_name):
    global file_path
    file_name = subject_name + '.csv'  # Create the file name by appending subject_name with '.csv'
    file_path_name = os.path.join(file_path, file_name)  # Combine the directory path with the file name
    header_row = ['Time', 'w1', 'x1', 'y1', 'z1', 'w2', 'x2', 'y2', 'z2', 'x_diff', 'y_diff', 'z_diff']
    with open(file_path_name, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(header_row)
        for row in dataout:
            writer.writerow(row)

def save_snapshot():
    try:
        subject_name = subject_entry.get()  # Get the subject name from the input box
        log_text("Saved snapshot for subject: {}".format(subject_name))
        write_snapshot_to_csv(dataout_row, subject_name + 'snap')
    except NameError as e:
        log_text("Error: {}".format(e))

def write_snapshot_to_csv(dataout_row, subject_name):
    global file_path
    file_name = subject_name + '.csv'  # Create the file name by appending subject_name with '.csv'
    file_path_name = os.path.join(file_path, file_name)  # Combine the directory path with the file name
    header_row = ['Time', 'w1', 'x1', 'y1', 'z1', 'w2', 'x2', 'y2', 'z2', 'x_diff', 'y_diff', 'z_diff']
    with open(file_path_name, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(dataout_row)

def clear_data():
    data.clear()
    dataout.clear()
    angles.clear()
    ax.clear()
    canvas.draw()
    counter.clear()

def calculate_angular_difference(quat1, quat2):
    r1 = R.from_quat(quat1)
    r2 = R.from_quat(quat2)
    r = r2 * r1.inv()
    return r.as_euler(euler_sequence, degrees=True)

def read_serial_data():
    global angles, calib1, calib2
    global dataout
    global dataout_row
    global counter

    quat1 = None
    quat2 = None
    counter = 0

    while True:
        data_row = []
        data_row.append(time.time())  # Append timestamp
        line = ser.readline().decode().strip()

        try:
            data = json.loads(line)

            # Extract quaternion data from sensors
            if data['key'] == '/sensor/1':
                quat1 = data['value']
                calib1 = data['calibration']
            if data['key'] == '/sensor/2':
                quat2 = data['value']
                calib2 = data['calibration']

            if quat1 is not None and quat2 is not None:
                # Calculate and store angular differences every 10 iterations
                if counter % 10 == 0:
                    euler_angles = calculate_angular_difference(quat1, quat2)
                    angles.append(euler_angles)
                # Update calibration text box
                    calibration_text = f"sensor1={calib1} sensor2={calib2}"
                    text_box.delete(1.0, tk.END)
                    text_box.insert(tk.END, calibration_text)

                counter += 1
                # Prepare formatted data for output
                dataout_row = [data_row[0]]
                dataout_row.extend(quat1)
                dataout_row.extend(quat2)
                dataout_row.extend(euler_angles)  # The euler_angles variable is used here
                dataout.append(dataout_row)

        except (ValueError, KeyError):
            continue

def update_plot():
    global angles

    if len(angles) > 0:
        ax.clear()
        ax.plot(angles)
        ax.legend(['1', '2', '3'], bbox_to_anchor=(1.05, 1), loc='upper left')  ### check Euler sequence!
        ax.set_xlabel('Time')
        ax.set_ylabel('Angular difference (degrees)')
        canvas.draw()
    root.after(200, update_plot)  # Update plot every 200 milliseconds

def log_text(message):
    log_box.insert(tk.END, message + "\n")
    log_box.see(tk.END)

def set_euler_sequence(sequence):
    global euler_sequence
    euler_sequence = sequence

# Create GUI
root = tk.Tk()
root.title("Serial Data Recorder and Plotter")

file_button = tk.Button(root, text="Choose Folder", command=choose_folder)
file_button.pack()

subject_label = tk.Label(root, text="Name:")
subject_label.pack()

subject_entry = tk.Entry(root)
subject_entry.pack()

# Create a frame for the buttons
button_frame = tk.Frame(root)
button_frame.pack()

# Save Data button
save_data_button = tk.Button(button_frame, text="Save Data", command=save_data)
save_data_button.pack(side='left', padx=5)

# Save Snapshot button
snapshot_button = tk.Button(button_frame, text="Save Snapshot", command=save_snapshot)
snapshot_button.pack(side='left', padx=5)

clear_button = tk.Button(root, text="Clear Data", command=clear_data)
clear_button.pack()

# Create text box
text_box = tk.Text(root, height=1, width=160)
text_box.pack(side=tk.BOTTOM)

log_box = tk.Text(root, height=2, width=80)
log_box.pack(side=tk.BOTTOM)

# Create plot
fig = Figure(figsize=(6, 4), dpi=100)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()

# Create radio buttons for Euler sequence selection
euler_label = tk.Label(root, text="Euler Sequence:")
euler_label.pack(side=tk.LEFT)

euler_frame = tk.Frame(root)
euler_frame.pack(side=tk.LEFT)

sequence_var = tk.StringVar()

# Create a grid to organize the radio buttons
row_num = 0
col_num = 0

xyz_radio = tk.Radiobutton(euler_frame, text="xyz", variable=sequence_var, value="xyz", command=lambda: set_euler_sequence("xyz"))
xyz_radio.grid(row=row_num, column=col_num, sticky="w")

yxz_radio = tk.Radiobutton(euler_frame, text="yxz", variable=sequence_var, value="yxz", command=lambda: set_euler_sequence("yxz"))
yxz_radio.grid(row=row_num, column=col_num+1, sticky="w")

zxy_radio = tk.Radiobutton(euler_frame, text="zyx", variable=sequence_var, value="zyx", command=lambda: set_euler_sequence("zyx"))
zxy_radio.grid(row=row_num, column=col_num+2, sticky="w")

row_num = 1

zyx_radio = tk.Radiobutton(euler_frame, text="xzy", variable=sequence_var, value="xzy", command=lambda: set_euler_sequence("xzy"))
zyx_radio.grid(row=row_num, column=col_num, sticky="w")

xzy_radio = tk.Radiobutton(euler_frame, text="yzx", variable=sequence_var, value="yzx", command=lambda: set_euler_sequence("yzx"))
xzy_radio.grid(row=row_num, column=col_num+1, sticky="w")

yzx_radio = tk.Radiobutton(euler_frame, text="zxy", variable=sequence_var, value="zxy", command=lambda: set_euler_sequence("zxy"))
yzx_radio.grid(row=row_num, column=col_num+2, sticky="w")

# Set default Euler sequence
sequence_var.set(euler_sequence)
set_euler_sequence(sequence_var.get())

# Start serial data reading in a separate thread
serial_thread = threading.Thread(target=read_serial_data)
serial_thread.daemon = True
serial_thread.start()

# Start plot updating in a separate thread
plot_thread = threading.Thread(target=update_plot)
plot_thread.daemon = True
plot_thread.start()

# Start GUI loop
root.mainloop()


import sqlite3
import settings
import discord
from utils import logger

conn = sqlite3.connect(settings.SQL_DB)
cursor = conn.cursor()
conn.commit()

async def create_leaderboard_embed(ctx):
    logger.info("IN create leaderboard")

    cursor = conn.cursor()
    cursor.execute("SELECT title, user_id, upvotes, downvotes, keywords, author, cover, pageCount FROM suggested_books ORDER BY (upvotes - downvotes) DESC")
    suggested_books = cursor.fetchall()

    if suggested_books:
        embed = discord.Embed(title="Vote Results", color=discord.Color.gold())
        details = ""
        first_place_points = suggested_books[0][2] - suggested_books[0][3]
        next_placement = 0
        pin_thumbnail = False  # Track if we should pin the thumbnail

        for i, (title, user_id, upvotes, downvotes, keywords, author, cover, pageCount) in enumerate(suggested_books):
            user = ctx.bot.get_user(user_id)
            if user is not None:
                user_name = user.name
            else:
                user_name = "Unknown User"

            # Determine medal
            score = upvotes - downvotes
            # medal = "ðŸ¥‡" if score == first_place_points else (next_placement if score == previous_score else (i+1))

            if score == first_place_points:
                medal = "ðŸ¥‡"
            elif score == previous_score:
                medal = next_placement
            else:
                # medal = (i+1)

                if (i+1) == 2:
                    medal = "2nd"
                elif (i+1) == 3:
                    medal = "3rd"
                else:
                    medal = f"{(i+1)}th"
                next_placement = medal

            # if score != first_place_points and score != previous_score:
            #     next_placement += 1  # Increment the placement for ties in the score
            details += f"{medal} **{title}**\n *{author}* \n" \
                       f"**Total Points**: *{upvotes-downvotes}*\n" \
                       f"**Page Count**: {pageCount}\n" \
                       f"**Suggested By**: {user_name}\n\n"

            previous_score = score

            # If it's the first place suggestion, add the cover as an inline image
            if medal == "ðŸ¥‡" and cover and not pin_thumbnail:
                embed.set_thumbnail(url=cover)
                pin_thumbnail = True  # Set to True after the first-place suggestion's image is added

        # Set the details in the embed's description
        embed.description = details

        return embed
    else:
        return None


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Created By  : Kangjoo Lee (kangjoo.lee@yale.edu)
# Created Date: 10/26/2022
# Last Updated: 10/26/2022
# version ='0.0'
# ---------------------------------------------------------------------------

# =========================================================================
#                   --   Post CAP pipeline template  --
#      Analysis of Co-Activation Patterns(CAP) in fMRI data (HCP-YA)
# =========================================================================

# Imports
import h5py
import os
import glob
import nibabel as nib
import numpy as np
import argparse
import itertools
import pandas as pd
import logging
import ptitprince as pt
import matplotlib.collections as clt
import matplotlib.pyplot as plt
from pycap_functions.pycap_postcap_loaddata import *
from pycap_functions.pycap_createWBimages import *
from pycap_functions.pycap_cap_splitcorrelation import *
from pycap_functions.pycap_cap_timemetrics import *
import seaborn as sns
from pandas.api.types import CategoricalDtype
from joypy import joyplot
from scipy import stats
import copy
from matplotlib.colors import to_rgb
from matplotlib.collections import PolyCollection
from mpl_toolkits import mplot3d

savefigs = True

def dir_path(path):
    if os.path.isdir(path):
        return path
    else:
        raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")

parser = argparse.ArgumentParser()
parser.add_argument("-ci", "--savecapimg", type=str, help="Save CAP images or not (y/n)")
parser.add_argument("-g", "--gsr", type=str, help="(y/n)")
parser.add_argument("-hd", "--homedir", type=dir_path, help="Home directory path")
parser.add_argument("-od", "--outdir", type=dir_path, help="Output directory path")
parser.add_argument("-dd", "--datadirtag", type=str, help="CAP output directory path")
parser.add_argument("-pl", "--minperm", type=int, help="Miminum index of permutation")
parser.add_argument("-pu", "--maxperm", type=int, help="Maximum index of permutation")
parser.add_argument("-s", "--seedtype", type=str, help="(seedfree/seedbased)")
parser.add_argument("-si", "--seedname", type=str, help="Seed name")
parser.add_argument("-sp", "--seedthreshtype", type=str, help="(T/P)")
parser.add_argument("-sr", "--seedthresholdrange", nargs='+',
                    type=int, help="Signal threshold range")
parser.add_argument("-sl", "--sublistfilen", dest="sublistfilen", required=True,
                    help="Subject list filename", type=lambda f: open(f))
parser.add_argument("-tr", "--randTthresholdrange", nargs='+',
                    type=int, help="Random timeframe threshold range")
parser.add_argument("-kr", "--basis_k_range", nargs='+',
                    type=int, help="basis k range")
parser.add_argument("-p", "--pscalarfilen", dest="pscalarfile", required=True,
                    help="Pscalar filename", type=lambda f: open(f))
parser.add_argument("-u", "--unit", type=str, help="(p/d)")  # parcel or dense
args = parser.parse_args()  # Read arguments from command line

# -------------------------------------------
#           Setup input parameters
# -------------------------------------------

class Param:
    pass

param = Param()

# - parameters for data selection
if args.gsr == "y":
    param.gsr = "gsr"
elif args.gsr == "n":
    param.gsr = "nogsr"
param.unit = args.unit
if param.unit == "d":
    param.sdim = 91282
elif param.unit == "p":
    param.sdim = 718
param.tdim = 4400

# - parameters for seed signal selection
param.seedtype = args.seedtype
if param.seedtype == "seedfree":
    param.seedIDname = param.seedtype
    param.randTthresholdrange = args.randTthresholdrange

# - parameters for post-cap analysis
param.minperm = args.minperm
param.maxperm = args.maxperm
param.basis_k_range = args.basis_k_range

# -------------------------------------------
#              Setup input data
# -------------------------------------------

class FileIn:
    pass

filein = FileIn()
filein.homedir = args.homedir
sl = pd.read_csv(args.sublistfilen.name, header=0).values.tolist()
filein.sublist = list(itertools.chain.from_iterable(sl))
filein.pscalar_filen = args.pscalarfile.name

# -------------------------------------------
#            Setup output directory
# -------------------------------------------

param.datadirtag = args.datadirtag
filein.outdir = args.outdir + param.gsr + "_" + param.seedIDname + "/"
isExist = os.path.exists(filein.outdir)
if not isExist:
    os.makedirs(filein.outdir)

# -------------------------------------------
#            Setup log file directory
# -------------------------------------------
filein.logdir = args.outdir + param.gsr + "_" + param.seedIDname + "/logs/"
isExist = os.path.exists(filein.logdir)
if not isExist:
    os.makedirs(filein.logdir)

# -------------------------------------------
#          Setup logging variables
# -------------------------------------------

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(message)s',
                    filename=filein.logdir + 'output_postcap_step7.log',
                    filemode='w')
console = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

# -------------------------------------------
#          Define functions.
# -------------------------------------------

def select_daydata(inputdata, day_n):

    dayidx=np.where(inputdata.frame_day == day_n)[0]
    msg = ">> ---- Day " + str(day_n) + " index: " + str(len(dayidx))
    logging.info(msg)

    daydata = copy.deepcopy(inputdata)
    daydata.frame_clusterID = inputdata.frame_clusterID[dayidx,]
    daydata.frame_subID = inputdata.frame_subID[dayidx,]
    daydata.frame_day = inputdata.frame_day[dayidx,]

    msg = ">> ---- Day " + str(day_n) + " data: clmean " + str(daydata.clmean.shape) + \
          ", frame_clusterID " + str(daydata.frame_clusterID.shape) + \
          ", frame_subID " + str(daydata.frame_subID.shape) + \
          ", frame_day " + str(daydata.frame_day.shape)
    logging.info(msg)

    return daydata

def add_subgroup_data(dataset, filein, cTh):

    cThstring = '_' + str(cTh) + 'SD'
    cThcol = ['subgroup' + cThstring]

    # Load subgroup defitnitions
    subgroup1 = pd.read_csv(filein.outdir + 'subgroup1' + cThstring + '.csv')
    subgroup2 = pd.read_csv(filein.outdir + 'subgroup2' + cThstring + '.csv')
    subgroup3 = pd.read_csv(filein.outdir + 'subgroup3' + cThstring + '.csv')

    # Assign subgroup defitnitions
    dataset.loc[:,cThcol]=np.nan
    sgidx1= dataset.index[dataset["subID"].isin(subgroup1["subID"])]
    dataset.loc[sgidx1,cThcol]=1
    sgidx2= dataset.index[dataset["subID"].isin(subgroup2["subID"])]
    dataset.loc[sgidx2,cThcol]=2
    sgidx3= dataset.index[dataset["subID"].isin(subgroup3["subID"])]
    dataset.loc[sgidx3,cThcol]=3
    del sgidx1, sgidx2,sgidx3

    return dataset

def f_test(x, y):
    x = np.array(x)
    y = np.array(y)
    f = np.var(x, ddof=1)/np.var(y, ddof=1) #calculate F test statistic
    dfn = x.size-1 #define degrees of freedom numerator
    dfd = y.size-1 #define degrees of freedom denominator
    p = 1-stats.f.cdf(f, dfn, dfd) #find p-value of F test statistic
    return f, p

# -------------------------------------------
#          Define classes.
# -------------------------------------------

class bcm_dur_tt:
    pass

bcm_dur_tt = bcm_dur_tt()

class bcv_dur_tt:
    pass

bcv_dur_tt = bcv_dur_tt()

class opt:
    pass

opt = opt()

# -------------------------------------------
#          Main analysis starts here.
# -------------------------------------------

param.splittype = "Ssplit"

if param.seedtype == "seedfree":
    pthrange = param.randTthresholdrange

for pth in pthrange:

    if param.seedtype == "seedfree":
        param.randTthreshold = pth

    msg = "=========== signal threshold P = " + str(pth) + "% ==========="
    logging.info(msg)

    # -------------------------------------------
    #            Setup input directory
    # -------------------------------------------

    if param.seedtype == "seedfree":
        filein.indir = args.outdir + param.gsr + "_" + param.seedIDname + \
            "/P" + str(param.randTthreshold) + ".0/"

    for stdk in param.basis_k_range:

        for sp in [1, 2]:
            if sp == 1:
                spdatatag = "training_data"
                sptitletag = "split_1"
            elif sp == 2:
                spdatatag = "test_data"
                sptitletag = "split_2"

            msg = "     <<  " + sptitletag + "  >>"
            logging.info(msg)

            # -------------------------------------------
            # - Load information about the new order of CAPs in the sorted CAP matrix
            # -------------------------------------------

            neworderpath1 = filein.outdir + "sorted_testcaps_neworder_" + sptitletag + "_allperms_basis" + str(stdk) + "_testk4.csv"
            neworderpath2 = filein.outdir + "sorted_testcaps_neworder_" + sptitletag + "_allperms_basis" + str(stdk) + "_testk5.csv"
            df1 = pd.read_csv(neworderpath1,index_col=0)
            df2 = pd.read_csv(neworderpath2,index_col=0)
            neworder_allperm=pd.concat([df1, df2], axis=0, ignore_index=True)
            neworder_allperm = neworder_allperm.sort_values(by=['perm'], ascending=True, ignore_index=True)
            neworder_allperm_filen = filein.outdir + 'P' + str(pth) + "_" + sptitletag + '_SortedCAP_neworder_allperm.csv'
            neworder_allperm.to_csv(neworder_allperm_filen)
            msg = "Collect and save the new orders of sorted CAPs in all permutations in " + neworder_allperm_filen
            logging.info(msg)
            del df1, df2, neworderpath1, neworderpath2

            # -----------------------------------
            # - For each permutation
            # -----------------------------------

            for index in range(len(neworder_allperm)):

                # -----------------------------------
                # - Get the new order of CAPs to match with 5-basis CAPs
                # -----------------------------------
                perm = neworder_allperm["perm"][index]
                neworder = neworder_allperm['neworder'][index]
                neworder = neworder.replace(" ", "," )
                neworder = neworder[1:-1]
                neworder = list(map(int, neworder.split(",")))

                msg = "\n"
                logging.info(msg)
                msg = "     <<  perm " + str(perm) + ": " + str(len(neworder)) + " CAPs with new order " + str(neworder) + "  >>"
                logging.info(msg)
                msg = "\n"
                logging.info(msg)

                # -----------------------------------
                # - Load CAP maps (by default, it unfortunately loads both training/test datasets)
                # -----------------------------------

                if param.seedtype == "seedbased":
                    filein.datapath = param.datadirtag + "/" + param.splittype + \
                        str(perm) + "/" + param.gsr + "_" + param.seedIDname + "/" + \
                        param.sig_thresholdtype + str(param.sig_threshold) + ".0/"
                elif param.seedtype == "seedfree":
                    filein.datapath = param.datadirtag + "/" + param.splittype + \
                        str(perm) + "/" + param.gsr + "_" + param.seedIDname + \
                        "/P" + str(param.randTthreshold) + ".0/"
                training_data, test_data = load_capoutput(filein=filein, param=param)

                # -----------------------------------
                # - Load split subject ID info
                # -----------------------------------
                msg = "============================================"
                logging.info(msg)
                msg = "Start loading sublist data from n=" + str(len(filein.sublist))
                logging.info(msg)

                splitdata_filen = filein.datapath + "subsplit_datalist.hdf5"
                f = h5py.File(splitdata_filen, 'r')
                training_sublist_idx = f['training_sublist_idx']
                test_sublist_idx = f['test_sublist_idx']

                training_data.splitlist = []
                for loaddata_perm in training_sublist_idx:
                    training_data.splitlist.append(filein.sublist[loaddata_perm])
                test_data.splitlist = []
                for loaddata_perm in test_sublist_idx:
                    test_data.splitlist.append(filein.sublist[loaddata_perm])
                del f, training_sublist_idx, test_sublist_idx, loaddata_perm

                msg = "Load the sublist data in " + splitdata_filen
                logging.info(msg)
                msg = "    #(training list) = " + str(len(training_data.splitlist)) + \
                    ", #(test list) = " + str(len(test_data.splitlist))
                logging.info(msg)
                msg = "Training data list : " + str(np.unique(training_data.splitlist))
                logging.info(msg)
                msg = "Test data list : " + str(np.unique(test_data.splitlist))
                logging.info(msg)

                # -----------------------------------
                # - Select Split-half data
                # -----------------------------------
                if sp == 1:
                    inputdata = copy.deepcopy(training_data)
                elif sp == 2:
                    inputdata = copy.deepcopy(test_data)
                del training_data, test_data

                msg = "============================================\n"
                logging.info(msg)
                msg = ">> Selected CAP maps in " + sptitletag + " perm " + str(perm) + " : clmean " + str(inputdata.clmean.shape) + \
                          ", frame_clusterID " + str(inputdata.frame_clusterID.shape) + ", frame_subID " + \
                          str(inputdata.frame_subID.shape) + ", frame_day " + str(inputdata.frame_day.shape)
                logging.info(msg)

                # -----------------------------------
                # - Select day 1 or 2 data
                # -----------------------------------
                for day_n in [1, 2]:

                    daydata = select_daydata(inputdata=inputdata, day_n=day_n)

                    # - Compute CAP time metrics
                    msg = "============================================\n"
                    logging.info(msg)
                    msg = ">> Compute CAP metrics for " + sptitletag + ".."
                    logging.info(msg)

                    # Between-CAP variance of fractional occupancy - group level
                    grp_capdur, bcm_dur, bcv_dur = cap_occupancy_btwcap(data=daydata, param=param)

                    # Within-CAP between-subject variance of fractional occupancy - individual level
                    capdur_ind_allcap, wcbsm_dur, wcbsv_dur = cap_occupancy_withincap_btwsubj(data=daydata, dataname=sptitletag, filein=filein, param=param)

                    # Within-CAP within-subject mean/variance of dwell time - individual level
                    capdwt_wsm_allcap, capdwt_wsv_allcap = cap_dwelltime_withincap_withinsubj(data=daydata, dataname=sptitletag, filein=filein, param=param)

                    # -----------------------------------
                    # - Reorder CAPs in the group level outputs to match with k-basis CAPs
                    # -----------------------------------
                    linorder = capdur_ind_allcap.columns.values.tolist()
                    linorder = linorder[1:]
                    grp_capdur_r = grp_capdur[neworder]
                    grp_capdur_r = np.array(grp_capdur_r).reshape(1,len(grp_capdur_r))
                    grp_capdur_r=pd.DataFrame(grp_capdur_r, columns=linorder)
                    df_warg = {'seedID': [param.seedIDname], 'seedthr': [param.randTthreshold], 'half': [sptitletag], 'stdk': [stdk], 'permidx': [perm], 'day': [day_n]}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    grp_capdur_r = pd.concat([grp_capdur_r, df_info], axis=1)

                    msg = "[Group-level] Between-CAP variance of occupancy: Recordered the output of CAPs to match with k-basis CAPs. \n" + str(grp_capdur_r)
                    logging.info(msg)

                    # -----------------------------------
                    # - Reorder CAPs in the individual level outputs to match with k-basis CAPs - wcbsv_dur
                    # -----------------------------------
                    wcbsv_dur_r = wcbsv_dur[neworder]
                    wcbsv_dur_r = np.array(wcbsv_dur_r).reshape(1,len(wcbsv_dur_r))
                    wcbsv_dur_r=pd.DataFrame(wcbsv_dur_r, columns=linorder)
                    df_warg = {'seedID': [param.seedIDname], 'seedthr': [param.randTthreshold], 'half': [sptitletag], 'stdk': [stdk], 'permidx': [perm], 'day': [day_n]}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    wcbsv_dur_r = pd.concat([wcbsv_dur_r, df_info], axis=1)

                    msg = "[Individual-level] Between-subject variance of occupancy in each CAP: Recordered the output of CAPs to match with k-basis CAPs. \n" + str(wcbsv_dur_r)
                    logging.info(msg)

                    # -----------------------------------
                    # - Reorder CAPs in the individual level outputs to match with k-basis CAPs - wcbsm_dur
                    # -----------------------------------
                    wcbsm_dur_r = wcbsm_dur[neworder]
                    wcbsm_dur_r = np.array(wcbsm_dur_r).reshape(1,len(wcbsm_dur_r))
                    wcbsm_dur_r=pd.DataFrame(wcbsm_dur_r, columns=linorder)
                    df_warg = {'seedID': [param.seedIDname], 'seedthr': [param.randTthreshold], 'half': [sptitletag], 'stdk': [stdk], 'permidx': [perm], 'day': [day_n]}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    wcbsm_dur_r = pd.concat([wcbsm_dur_r, df_info], axis=1)

                    msg = "[Individual-level] Mean of occupancy in each CAP: Recordered the output of CAPs to match with k-basis CAPs. \n" + str(wcbsm_dur_r)
                    logging.info(msg)

                    # -----------------------------------
                    # - Reorder CAPs in the individual level outputs to match with k-basis CAPs - capdur_ind_allcap
                    # -----------------------------------

                    neworder2 = [str(x) for x in neworder]
                    newcols = ['subID'] + neworder2
                    capdur_ind_allcap_r = capdur_ind_allcap[newcols]
                    msg = "[Individual-level] Distribution of individual occupancy in each CAP: Recordered the output of CAPs to match with k-basis CAPs."
                    logging.info(msg)
                    # Rename the estimated CAP columns
                    capdur_ind_allcap_r.columns=capdur_ind_allcap.columns.values.tolist()

                    # - Generate dataframe info columns
                    permstdk = [stdk] * len(capdur_ind_allcap_r)
                    permstdk = np.array(permstdk)
                    permhalf = [sptitletag] * len(capdur_ind_allcap_r)
                    permhalf = np.array(permhalf)
                    permseedID = [param.seedIDname] * len(capdur_ind_allcap_r)
                    permseedID = np.array(permseedID)
                    permpermidx = [perm] * len(capdur_ind_allcap_r)
                    permpermidx = np.array(permpermidx)
                    permday = [day_n] * len(capdur_ind_allcap_r)
                    permday = np.array(permday)
                    if param.seedtype == "seedbased":
                        permthresh = [str(int(param.sig_threshold))] * len(capdur_ind_allcap_r)
                    elif param.seedtype == "seedfree":
                        permthresh = [str(int(param.randTthreshold))] * len(capdur_ind_allcap_r)
                    permthresh = np.array(permthresh)
                    df_warg = {'seedID': permseedID, 'seedthr': permthresh, 'half': permhalf, 'stdk': permstdk, 'permidx': permpermidx, 'day': permday}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    # - Add to DataFrame
                    capdur_ind_allcap_r = pd.concat([capdur_ind_allcap_r, df_info], axis=1)

                    msg = "\n" + str(capdur_ind_allcap_r)
                    logging.info(msg)

                    # -----------------------------------
                    # - Reorder CAPs in the individual level outputs to match with k-basis CAPs - capdwt_wsm_allcap
                    # -----------------------------------

                    neworder2 = [str(x) for x in neworder]
                    newcols = ['subID'] + neworder2
                    capdwt_wsm_allcap_r = capdwt_wsm_allcap[newcols]
                    msg = "[Individual-level] Distribution of individual within-subject mean of dwell time in each CAP: Recordered the output of CAPs to match with k-basis CAPs."
                    logging.info(msg)
                    # Rename the estimated CAP columns
                    capdwt_wsm_allcap_r.columns=capdwt_wsm_allcap.columns.values.tolist()

                    # - Generate dataframe info columns
                    permstdk = [stdk] * len(capdwt_wsm_allcap_r)
                    permstdk = np.array(permstdk)
                    permhalf = [sptitletag] * len(capdwt_wsm_allcap_r)
                    permhalf = np.array(permhalf)
                    permseedID = [param.seedIDname] * len(capdwt_wsm_allcap_r)
                    permseedID = np.array(permseedID)
                    permpermidx = [perm] * len(capdwt_wsm_allcap_r)
                    permpermidx = np.array(permpermidx)
                    permday = [day_n] * len(capdur_ind_allcap_r)
                    permday = np.array(permday)
                    if param.seedtype == "seedbased":
                        permthresh = [str(int(param.sig_threshold))] * len(capdwt_wsm_allcap_r)
                    elif param.seedtype == "seedfree":
                        permthresh = [str(int(param.randTthreshold))] * len(capdwt_wsm_allcap_r)
                    permthresh = np.array(permthresh)
                    df_warg = {'seedID': permseedID, 'seedthr': permthresh, 'half': permhalf, 'stdk': permstdk, 'permidx': permpermidx, 'day': permday}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    # - Add to DataFrame
                    capdwt_wsm_allcap_r = pd.concat([capdwt_wsm_allcap_r, df_info], axis=1)

                    msg = "\n" + str(capdwt_wsm_allcap_r)
                    logging.info(msg)

                    # -----------------------------------
                    # - Reorder CAPs in the individual level outputs to match with k-basis CAPs - capdwt_wsv_allcap
                    # -----------------------------------

                    neworder2 = [str(x) for x in neworder]
                    newcols = ['subID'] + neworder2
                    capdwt_wsv_allcap_r = capdwt_wsv_allcap[newcols]
                    msg = "[Individual-level] Distribution of individual within-subject variance of dwell time in each CAP: Recordered the output of CAPs to match with k-basis CAPs."
                    logging.info(msg)
                    # Rename the estimated CAP columns
                    capdwt_wsv_allcap_r.columns=capdwt_wsv_allcap.columns.values.tolist()

                    # - Generate dataframe info columns
                    permstdk = [stdk] * len(capdwt_wsv_allcap_r)
                    permstdk = np.array(permstdk)
                    permhalf = [sptitletag] * len(capdwt_wsv_allcap_r)
                    permhalf = np.array(permhalf)
                    permseedID = [param.seedIDname] * len(capdwt_wsv_allcap_r)
                    permseedID = np.array(permseedID)
                    permpermidx = [perm] * len(capdwt_wsv_allcap_r)
                    permpermidx = np.array(permpermidx)
                    permday = [day_n] * len(capdur_ind_allcap_r)
                    permday = np.array(permday)
                    if param.seedtype == "seedbased":
                        permthresh = [str(int(param.sig_threshold))] * len(capdwt_wsv_allcap_r)
                    elif param.seedtype == "seedfree":
                        permthresh = [str(int(param.randTthreshold))] * len(capdwt_wsv_allcap_r)
                    permthresh = np.array(permthresh)
                    df_warg = {'seedID': permseedID, 'seedthr': permthresh, 'half': permhalf, 'stdk': permstdk, 'permidx': permpermidx, 'day': permday}
                    col_warg = ['seedID', 'seedthr', 'half', 'stdk', 'permidx', 'day']
                    df_info = pd.DataFrame(df_warg, columns=col_warg)
                    # - Add to DataFrame
                    capdwt_wsv_allcap_r = pd.concat([capdwt_wsv_allcap_r, df_info], axis=1)

                    msg = "\n" + str(capdwt_wsv_allcap_r)
                    logging.info(msg)

                    del df_info, permstdk, permhalf, permseedID, permthresh, df_warg, col_warg, permday

                    # -----------------------------------
                    #- Combine df over two days
                    # -----------------------------------

                    if 'grp_capdur_r_allday' in locals():
                        grp_capdur_r_allday = pd.concat([grp_capdur_r_allday, grp_capdur_r], axis=0, ignore_index=True)
                    else:
                        grp_capdur_r_allday = grp_capdur_r

                    if 'wcbsv_dur_r_allday' in locals():
                        wcbsv_dur_r_allday = pd.concat([wcbsv_dur_r_allday, wcbsv_dur_r], axis=0, ignore_index=True)
                    else:
                        wcbsv_dur_r_allday = wcbsv_dur_r

                    if 'wcbsm_dur_r_allday' in locals():
                        wcbsm_dur_r_allday = pd.concat([wcbsm_dur_r_allday, wcbsm_dur_r], axis=0, ignore_index=True)
                    else:
                        wcbsm_dur_r_allday = wcbsm_dur_r

                    if 'capdur_ind_allcap_r_allday' in locals():
                        capdur_ind_allcap_r_allday = pd.concat([capdur_ind_allcap_r_allday, capdur_ind_allcap_r], axis=0, ignore_index=True)
                    else:
                        capdur_ind_allcap_r_allday = capdur_ind_allcap_r

                    if 'capdwt_wsm_allcap_r_allday' in locals():
                        capdwt_wsm_allcap_r_allday = pd.concat([capdwt_wsm_allcap_r_allday, capdwt_wsm_allcap_r], axis=0, ignore_index=True)
                    else:
                        capdwt_wsm_allcap_r_allday = capdwt_wsm_allcap_r

                    if 'capdwt_wsv_allcap_r_allday' in locals():
                        capdwt_wsv_allcap_r_allday = pd.concat([capdwt_wsv_allcap_r_allday, capdwt_wsv_allcap_r], axis=0, ignore_index=True)
                    else:
                        capdwt_wsv_allcap_r_allday = capdwt_wsv_allcap_r

                    del grp_capdur_r,wcbsv_dur_r,wcbsm_dur_r,capdur_ind_allcap_r,capdwt_wsm_allcap_r,capdwt_wsv_allcap_r

                # -----------------------------------
                #- Combine df over all permutations
                # -----------------------------------

                if 'grp_capdur_r_allday_allperm' in locals():
                    grp_capdur_r_allday_allperm = pd.concat([grp_capdur_r_allday_allperm, grp_capdur_r_allday], axis=0, ignore_index=True)
                else:
                    grp_capdur_r_allday_allperm = grp_capdur_r_allday

                if 'wcbsv_dur_r_allday_allperm' in locals():
                    wcbsv_dur_r_allday_allperm = pd.concat([wcbsv_dur_r_allday_allperm, wcbsv_dur_r_allday], axis=0, ignore_index=True)
                else:
                    wcbsv_dur_r_allday_allperm = wcbsv_dur_r_allday

                if 'wcbsm_dur_r_allday_allperm' in locals():
                    wcbsm_dur_r_allday_allperm = pd.concat([wcbsm_dur_r_allday_allperm, wcbsm_dur_r_allday], axis=0, ignore_index=True)
                else:
                    wcbsm_dur_r_allday_allperm = wcbsm_dur_r_allday

                if 'capdur_ind_allcap_r_allday_allperm' in locals():
                    capdur_ind_allcap_r_allday_allperm = pd.concat([capdur_ind_allcap_r_allday_allperm, capdur_ind_allcap_r_allday], axis=0, ignore_index=True)
                else:
                    capdur_ind_allcap_r_allday_allperm = capdur_ind_allcap_r_allday

                if 'capdwt_wsm_allcap_r_allday_allperm' in locals():
                    capdwt_wsm_allcap_r_allday_allperm = pd.concat([capdwt_wsm_allcap_r_allday_allperm, capdwt_wsm_allcap_r_allday], axis=0, ignore_index=True)
                else:
                    capdwt_wsm_allcap_r_allday_allperm = capdwt_wsm_allcap_r_allday

                if 'capdwt_wsv_allcap_r_allday_allperm' in locals():
                    capdwt_wsv_allcap_r_allday_allperm = pd.concat([capdwt_wsv_allcap_r_allday_allperm, capdwt_wsv_allcap_r_allday], axis=0, ignore_index=True)
                else:
                    capdwt_wsv_allcap_r_allday_allperm = capdwt_wsv_allcap_r_allday

                msg = "grp_capdur_r_allday_allperm " + str(grp_capdur_r_allday_allperm.shape)
                logging.info(msg)
                msg = "wcbsv_dur_r_allday_allperm " + str(wcbsv_dur_r_allday_allperm.shape)
                logging.info(msg)
                msg = "wcbsm_dur_r_allday_allperm " + str(wcbsm_dur_r_allday_allperm.shape)
                logging.info(msg)
                msg = "capdur_ind_allcap_r_allday_allperm " + str(capdur_ind_allcap_r_allday_allperm.shape)
                logging.info(msg)
                msg = "capdwt_wsm_allcap_r_allday_allperm " + str(capdwt_wsm_allcap_r_allday_allperm.shape)
                logging.info(msg)
                msg = "capdwt_wsv_allcap_r_allday_allperm " + str(capdwt_wsv_allcap_r_allday_allperm.shape)
                logging.info(msg)

                del grp_capdur_r_allday,wcbsv_dur_r_allday,wcbsm_dur_r_allday,capdur_ind_allcap_r_allday,capdwt_wsm_allcap_r_allday,capdwt_wsv_allcap_r_allday

    # Fill zero occupancy for 5th CAP
    grp_capdur_r_allday_allperm['4'] = grp_capdur_r_allday_allperm['4'].fillna(0)
    wcbsv_dur_r_allday_allperm['4'] = wcbsv_dur_r_allday_allperm['4'].fillna(0)
    wcbsm_dur_r_allday_allperm['4'] = wcbsm_dur_r_allday_allperm['4'].fillna(0)
    capdur_ind_allcap_r_allday_allperm['4'] = capdur_ind_allcap_r_allday_allperm['4'].fillna(0)
    capdwt_wsm_allcap_r_allday_allperm['4'] = capdwt_wsm_allcap_r_allday_allperm['4'].fillna(0)
    capdwt_wsv_allcap_r_allday_allperm['4'] = capdwt_wsv_allcap_r_allday_allperm['4'].fillna(0)

    # Save results
    grp_capdur_filen = filein.outdir + 'P' + str(pth) + '_Occupancy_group_trt.csv'
    grp_capdur_r_allday_allperm.to_csv(grp_capdur_filen)
    wcbsv_dur_filen = filein.outdir + 'P' + str(pth) + '_Occupancy_wcbsv_trt.csv'
    wcbsv_dur_r_allday_allperm.to_csv(wcbsv_dur_filen)
    wcbsm_dur_filen = filein.outdir + 'P' + str(pth) + '_Occupancy_wcbsm_trt.csv'
    wcbsm_dur_r_allday_allperm.to_csv(wcbsm_dur_filen)
    capdur_ind_filen = filein.outdir + 'P' + str(pth) + '_Occupancy_individual_trt.csv'
    capdur_ind_allcap_r_allday_allperm.to_csv(capdur_ind_filen)
    capdwt_wsm_filen = filein.outdir + 'P' + str(pth) + '_DwellTime_individual_WSmean_trt.csv'
    capdwt_wsm_allcap_r_allday_allperm.to_csv(capdwt_wsm_filen)
    capdwt_wsv_filen = filein.outdir + 'P' + str(pth) + '_DwellTime_individual_WSstd_trt.csv'
    capdwt_wsv_allcap_r_allday_allperm.to_csv(capdwt_wsv_filen)

    msg="Saving the results from temporal CAP metrics in \n" + grp_capdur_filen + "\n" + wcbsv_dur_filen + "\n" + wcbsm_dur_filen + "\n" + capdur_ind_filen + "\n" + capdwt_wsm_filen + "\n" + capdwt_wsv_filen
    logging.info(msg)

# - Notify job completion
msg = "\n========== The jobs are all completed. ==========\n"
logging.info(msg)


import pandas as pd
import numpy as np
import scipy
import time
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
from config import Configuration
from utils import *
from model import *
class DR_Markowitz_Experiment(object):

    def __init__(self, data_path):
        self.data_path = data_path
        self.window = 12 # Try to evaluate annual return
        self.n = 109 # self.n: length of training periods
        self.d = 20 # self.d: total number of assets

    def load_returns(self, seed, month):
        window = self.window
        train_path = self.data_path + 'Data/DataW/Data/seed' + str(seed) + '/NDDataPlay' + str(month) + '.csv'
        test_path = self.data_path + 'Data/DataW/Datatest/seed' + str(seed) + '/NDDataPlay' + str(month) + '.csv'
        prices_train_pd = pd.read_csv(train_path, delimiter=',', index_col='Unnamed: 0')
        prices_test_pd = pd.read_csv(test_path, delimiter=',', index_col='Unnamed: 0')
        n = prices_train_pd.shape[0]  # total number of instances of returns
        prices_train = prices_train_pd.astype(np.float32).values
        prices_test = prices_test_pd['x'].astype(np.float32).values
        returns_train = (prices_train[window:,:] - prices_train[:n-window,:])\
            / prices_train[:n-window, :] # compute the annual return
        returns_test = (prices_test - prices_train[-1,:]) \
            / prices_train[-1,:]
        self.n, self.d = returns_train.shape
        return returns_train, returns_test

    def load_implied_vol(self, month):
        window = self.window
        path = self.data_path + 'Data/VIX.csv'
        vol_data = pd.read_csv(path, delimiter=',')
        vol_open_data = vol_data['Open']
        vol_train = vol_open_data[(month+window):month+121]
        return vol_train

    def get_mahalanobis_matrix(self, seed, month, method = 'local'):
        vol_train = self.load_implied_vol(month)
        returns_train, returns_test = self.load_returns(seed, month)

        if method == 'local':
        	A = np.stack([(np.mean(vol_train)/vol) * np.eye(self.d) for vol in vol_train], axis = 2)
        elif method == 'covariance':
            A = np.stack([(np.mean(vol_train)/vol) * np.linalg.inv(np.cov(returns_train.T)) for vol in vol_train], axis = 2)
        else:
        	A = np.stack([np.eye(self.d) for vol in vol_train], axis = 2)
        return A

    def one_step_portfolio_return(self, seed, month, method = 'local'):
        returns_train, returns_test = self.load_returns(seed, month)
        A = self.get_mahalanobis_matrix(seed, month, method = method)
        model = DistRobustMarkowitz(X = returns_train, A = A)
        model.optimize(verbose = False, grad_method = 'analytic')
        one_step_return = returns_test.dot(model.beta)
        return one_step_return

    def portfolio_return(self, method = 'local'):
        seed_df = pd.read_csv(self.data_path+'Data/LinHistogramDataTangent.csv', delimiter=',')
        seeds = seed_df['Seed'][0:100]
        cores = cpu_count()
        p = Pool(cores)
        result = []
        # Testmonth is the number of month in the time period 2000-2017. In our case testmonth=12*17=204.
        testmonth = pd.read_csv(self.data_path+"Data/Nummonth.csv", delimiter=',')
        testmonth = testmonth['x'][0]
        pbar = tqdm(total=(len(seeds) * testmonth), leave = False)
        def update_pbar(*a):
            pbar.update()
        for idx,seed in enumerate(seeds):
            for month in range(1, testmonth + 1):
                result.append(p.apply_async(self.one_step_portfolio_return, args=(seed, month, method),
                                            callback = update_pbar))
        p.close()
        p.join()
        pbar.close()
        returns_mat = []
        for res in result:
            returns_mat.append(res.get())
        returns_mat = np.array(returns_mat).reshape(len(seeds), testmonth)
        return returns_mat

    def one_step_simulated_return(self, mean, cov, method = 'constant'):
        A = self.get_mahalanobis_matrix(1, 1, method = method)
        np.random.seed(int(time.time() * 1000) % (2 ** 31))
        exp_sqrt_cov = scipy.linalg.sqrtm(cov)
        exp_train = exp_sqrt_cov.dot(np.random.randn(20,109)).T + mean
        exp_test = exp_sqrt_cov.dot(np.random.randn(20)).T + mean
        model = DistRobustMarkowitz(X = exp_train, A = A)
        model.optimize(verbose = False, grad_method = 'analytic')
        one_step_return = exp_test.dot(model.beta)
        return one_step_return

    def simulated_portfolio_return(self, mean, cov, method = 'constant'):
        num_exp = 1000
        cores = cpu_count()
        p = Pool(cores)
        result = []
        pbar = tqdm(total=num_exp, leave = False)
        def update_pbar(*a):
            pbar.update()
        for i in range(num_exp):
            result.append(p.apply_async(self.one_step_simulated_return, args=(mean, cov, method),
                                        callback = update_pbar))
        p.close()
        p.join()
        pbar.close()
        returns_mat = []
        for res in result:
            returns_mat.append(res.get())
        return returns_mat


"""
Tests for simulating a real-time client.
"""

import unittest
import time
import logging

from obspy import UTCDateTime
from obspy.clients.fdsn import Client

from rt_eqcorrscan.streaming.clients.obspy import RealTimeClient, StreamClient

SLEEP_STEP = 20

logging.basicConfig(
    level="INFO",
    format="%(asctime)s\t[%(processName)s:%(threadName)s]: %(name)s\t%(levelname)s\t%(message)s")

class TestStreamBuffer(unittest.TestCase):
    def test_maintain(self):
        client = Client("GEONET")
        buffer = StreamClient(client=client, buffer_length=60, min_buffer_fraction=0.25)
        buffer.initiate_buffer(["NZ.WVZ.10.HHZ", "NZ.RPZ.10.HHZ"], UTCDateTime(2020, 1, 1))
        time.sleep(1)
        initial_stream = buffer.stream
        print(f"Initialised buffer as: \n{initial_stream}")
        buffer.maintain_buffer()
        st = buffer.get_waveforms_bulk(
            [("NZ", "WVZ", "10", "HHZ",
              UTCDateTime(2020, 1, 1), UTCDateTime(2020, 1, 1, 0, 0, 6)),
             ("NZ", "RPZ", "10", "HHZ",
              UTCDateTime(2020, 1, 1), UTCDateTime(2020, 1, 1, 0, 0, 6))])
        self.assertEqual(len(st), 2)
        trimmed_stream = buffer.stream
        print(f"Cut out first 6 seconds to give buffer: \n{trimmed_stream}")
        self.assertLess(trimmed_stream[0].stats.npts, initial_stream[0].stats.npts)
        # remove more than 75% of buffer
        st2 = buffer.get_waveforms_bulk(
            [("NZ", "WVZ", "10", "HHZ",
              UTCDateTime(2020, 1, 1), UTCDateTime(2020, 1, 1, 0, 0, 50)),
             ("NZ", "RPZ", "10", "HHZ",
              UTCDateTime(2020, 1, 1), UTCDateTime(2020, 1, 1, 0, 0, 50))])
        self.assertEqual(len(st), 2)
        print("Sleeping to allow buffer to refill")
        time.sleep(20)  # Sleep for more than half the min buffer fraction
        out_buffer = buffer.stream
        for tr in out_buffer:
            self.assertGreaterEqual(tr.stats.endtime - tr.stats.starttime, 60)
        buffer.background_stop()

class FDSNTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.client = Client("GEONET")
        cls.buffer = StreamClient(
            client=cls.client, buffer_length=60, min_buffer_fraction=0.25)
        cls.rt_client = RealTimeClient(
            server_url="Unreal-streamer",
            client=cls.buffer, buffer_capacity=10,
            starttime=UTCDateTime(2018, 1, 1), speed_up=2., query_interval=5.,
            pre_empt_data=True)

    def test_background_streaming(self):
        rt_client = self.rt_client.copy()
        rt_client.select_stream(net="NZ", station="JCZ", selector="HHZ")
        rt_client.select_stream(net="NZ", station="RPZ", selector="HHZ")
        rt_client.background_run()
        try:
            self.assertFalse(rt_client.buffer_full)
        except Exception as e:
            rt_client.background_stop()
            raise e
        print(f"Sleeping for {SLEEP_STEP}s")
        time.sleep(SLEEP_STEP)
        print("Stopping buffer")
        rt_client.background_stop()
        print("Running checks")
        self.assertTrue(rt_client.buffer_full)
        self.assertEqual(rt_client.buffer_length,
                         rt_client.buffer_capacity)

    def test_always_started(self):
        rt_client = self.rt_client.copy()
        rt_client.start()
        self.assertTrue(rt_client.started)
        rt_client.stop()
        self.assertFalse(rt_client.started)

    def test_can_add_streams(self):
        self.assertTrue(self.rt_client.can_add_streams)

    # def test_copy_with_buffer(self):
    #     rt_client = self.rt_client.copy()
    #     rt_client.select_stream(net="NZ", station="FOZ", selector="HHZ")
    #     rt_client.background_run()
    #     time.sleep(SLEEP_STEP)
    #     rt_client.background_stop()
    #     new_client = rt_client.copy(empty_buffer=False)
    #     new_client.select_stream(net="NZ", station="FOZ", selector="HHZ")
    #     self.assertEqual(new_client.get_stream(), rt_client.stream)
    #     new_client.background_run()
    #     time.sleep(SLEEP_STEP * 2)
    #     new_client.background_stop()
    #     # Make sure that we don't change the old buffer.
    #     self.assertNotEqual(new_client.get_stream(), rt_client.stream)

    def test_reprint(self):
        print_str = self.rt_client.__repr__()
        self.assertEqual(print_str, "Client at http://service.geonet.org.nz, "
                                    "status: Stopped, buffer capacity: 10.0s\n	"
                                    "Current Buffer:\nBuffer(0 traces, "
                                    "maxlen=10)")

if __name__ == "__main__":
    unittest.main()


# $Id: utils_tags.py,v 1.1 2013-06-30 17:08:56-07 dmf - $
# Derek Frank
#
# NAME
#   utils_tags.py - utils
#
# DESCRIPTION
#   Custom template tags and filters.
#

from django import template
register = template.Library()

####
## TAGS

# djangosnippets.org/snippets/2196/
@register.tag
def collect(parser, token):
    bits = list(token.split_contents())
    if len(bits) > 3 and bits[-2] == 'as':
        varname = bits[-1]
        items = bits[1:-2]
        return CollectNode(items, varname)
    else:
        raise template.TemplateSyntaxError('%r expected format is "item [item ...] as varname"' % bits[0])

class CollectNode(template.Node):
    def __init__(self, items, varname):
        self.items = map(template.Variable, items)
        self.varname = varname

    def render(self, context):
        context[self.varname] = [ i.resolve(context) for i in self.items ]
        return ''


from setuptools import setup, find_packages

classifiers = [
  'Development Status :: 5 - Production/Stable',
  'Intended Audience :: Education',
  'Operating System :: Ubuntu',
  'License :: OSI Approved :: MIT License',
  'Programming Language :: Python :: 3'
]

setup(
  name='ZAPZAPPY',
  version='0.0.1',
  description='zapzappy is a simple Whatsapp API',
  long_description=open('README.txt').read() + '\n\n' + open('CHANGELOG.txt').read(),
  url='',
  author='Lucas Ribeiro',
  author_email='Lucas Ribeiro',
  license='MIT',
  classifiers=classifiers,
  keywords='Whatsapp API',
  packages=find_packages(),
  install_requires=['']
)


import pygame as pg
from settings import *
from menu import Menu
from colorpalette import *

class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((Width, Height))
        pg.display.set_caption('Menu')
        self.rgb = RGBColors()
        self.clock = pg.time.Clock()
        self.frameRate = frameRate
        self.frameCounter = 0
        self.running = True
        self.loadAssets()

    def loadAssets(self):
        self.all_sprites = pg.sprite.LayeredUpdates()
        self.Menu = Menu(self)
        self.Menu.Items.items = ['item1','item2','item3','VeryNiceMenu!']

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.running = False
            if event.type == pg.KEYDOWN and event.key == pg.K_DOWN:
                self.Menu.Cursor.down()
            if event.type == pg.KEYDOWN and event.key == pg.K_UP:
                self.Menu.Cursor.up()
            if event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
                self.Menu.Cursor.select()

    def update(self):
        self.all_sprites.update()

    def draw(self):
        self.screen.fill(self.rgb.black)
        self.all_sprites.draw(self.screen)
        self.Menu.Items.draw()

    def run(self):
        while self.running:
            self.frameCounter += 1
            self.clock.tick(self.frameRate)
            self.events()
            self.update()
            self.draw()
            pg.display.flip()
            if self.frameCounter == self.frameRate:
                self.frameCounter = 0

g = Game()
while g.running:
    g.run()
pg.quit()


# boj 17086 아기상어2
from collections import deque

n, m = map(int, input().split())
graph = []
queue = deque()
dx = [1, -1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, 1, -1, -1]
for i in range(n):
    input_data = list(map(int, input().split()))
    for j in range(m):
        if input_data[j] == 1:
            queue.append((i, j))
    graph.append(input_data)

def bfs():
    max_dist = 0
    while queue:
        x, y = queue.popleft()
        for i in range(8):
            nx = x+dx[i]
            ny = y+dy[i]
            if nx < 0 or ny < 0 or nx >=n or ny >= m:
                continue
            if graph[nx][ny] == 0:
                graph[nx][ny] = graph[x][y]+1
                queue.append((nx, ny))
    for i in range(n):
        for j in range(m):
            max_dist = max(max_dist, graph[i][j])
    return max_dist-1

print(bfs())


# 导入必要的软件包
import cv2

# 视频文件输入初始化
filename = "E:\JJ\Download\p1.mp4"
camera = cv2.VideoCapture(filename)
#camera = cv2.VideoCapture(0)

# 视频文件输出参数设置
out_fps = 10.0  # 输出文件的帧率
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
out1 = cv2.VideoWriter('E:/video/v1.avi', fourcc, out_fps, (500, 400))
#out2 = cv2.VideoWriter('E:/video/v2.avi', fourcc, out_fps, (500, 400))

# 初始化当前帧的前帧
lastFrame = None

# 遍历视频的每一帧
while camera.isOpened():

    # 读取下一帧
    (ret, frame) = camera.read()

    # 如果不能抓取到一帧，说明我们到了视频的结尾
    if not ret:
        break

        # 调整该帧的大小
    frame = cv2.resize(frame, (500, 400), interpolation=cv2.INTER_CUBIC)

    # 如果第一帧是None，对其进行初始化
    if lastFrame is None:
        lastFrame = frame
        continue

        # 计算当前帧和前帧的不同
    frameDelta = cv2.absdiff(lastFrame, frame)

    # 当前帧设置为下一帧的前帧
    lastFrame = frame.copy()

    # 结果转为灰度图
    thresh = cv2.cvtColor(frameDelta, cv2.COLOR_BGR2GRAY)

    # 图像二值化
    thresh = cv2.threshold(thresh, 25, 255, cv2.THRESH_BINARY)[1]

    '''
    #去除图像噪声,先腐蚀再膨胀(形态学开运算)
    thresh=cv2.erode(thresh,None,iterations=1)
    thresh = cv2.dilate(thresh, None, iterations=2)
    '''

    # 阀值图像上的轮廓位置
    binary, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # 遍历轮廓
    for c in cnts:
        # 忽略小轮廓,排除大轮廓，排除误差
        if cv2.contourArea(c) < 300 or cv2.contourArea(c)>1000:
            continue

            # 计算轮廓的边界框，在当前帧中画出该框
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # 显示当前帧
    cv2.imshow("frame", frame)
    cv2.imwrite('frame.png', frame)
    cv2.imshow("frameDelta", frameDelta)
    cv2.imwrite('frameDelta.png', frameDelta)
    cv2.imshow("thresh", thresh)
    cv2.imwrite('thresh.png', thresh)

    # 保存视频
    out1.write(frame)
    #out2.write(frameDelta)

    # 如果q键被按下，跳出循环
    if cv2.waitKey(200) & 0xFF == ord('q'):
        break

        # 清理资源并关闭打开的窗口
out1.release()
out2.release()
camera.release()
cv2.destroyAllWindows()


#
# @lc app=leetcode.cn id=543 lang=python3
#
# [543] 二叉树的直径
#
from typing import Optional
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.
class Solution:

    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        self.maxDiameter = 0
        self.maxDepth(root)
        return self.maxDiameter

    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        left = self.maxDepth(root.left)
        right = self.maxDepth(root.right)

        self.maxDiameter = max(self.maxDiameter, left+right)

        return 1 + max(right,left)

# @lc code=end

二叉树/543.二叉树的直径.py


from flask import Flask

app = Flask(__name__)

DATA = [
    {
    "first_name": "Reuben",
    "last_name": "Osborne",
    "age": 26
    },
    {
    "first_name": "Ed",
    "last_name": "Cox",
    "age": 26
    }
]

@app.route('/data')
def get_data():
    return DATA

@app.route('/data/<name>')
def get_by_name(name):
    results = []
    for person in DATA:
        if person['first_name'] == name:
            results.append(person)
        elif person['last_name'] == name:
            results.append(person)
    return results

if __name__ == "__main__":
    app.run(debug=True)


from aiogram import types
from loguru import logger

from keyboards.inline.callback_datas import buy_callback
from keyboards.inline.choise_buttons import choise, pear_keyboard
from loader import dp, bot
from aiogram.dispatcher.filters import Command

@dp.message_handler(Command('items'))
async def show_items(message: types.Message):
    await message.answer(text='For sale we have 2 pieces: 5 apples an 2 pear.\n'
                              'If you want some, press button, if not Cancel',
                         reply_markup=choise)

@dp.callback_query_handler(buy_callback.filter(item_name='pear'))
async def buying_pear(call: types.CallbackQuery, callback_data: dict):
    # await bot.answer_callback_query(callback_query_id=call.id)
    await call.answer(cache_time=60)
    logger.info(f'callback_data = {call.data}')
    logger.info(f'callback_data dict = {call}')
    quantity = callback_data.get('quantity')
    await call.message.answer(f'You selected to buy pear. Pears count {quantity}. Thx',
                              reply_markup=pear_keyboard)

@dp.callback_query_handler(buy_callback.filter(item_name='apple'))
async def buying_apple(call: types.CallbackQuery, callback_data: dict):
    # await bot.answer_callback_query(callback_query_id=call.id)
    await call.answer(cache_time=60)
    logger.info(f'callback_data = {call.data}')
    logger.info(f'callback_data dict = {call}')
    quantity = callback_data.get('quantity')
    await call.message.answer(f'You selected to buy apple. Apples count {quantity}. Thx')

@dp.callback_query_handler(text="cancel")
async def cancel_buying(call: types.CallbackQuery):
    await call.answer("Canceled!", show_alert=True)

    await call.message.edit_reply_markup(reply_markup=None)

    # await bot.edit_message_reply_markup(chat_id=call.from_user.id,
    #                                     message_id=call.message.message_id,
    #                                     reply_markup=None)