text:
#!/usr/bin/env python3
import math
from bisect import bisect_right

import numpy as np

import cereal.messaging as messaging
import common.transformations.coordinates as coord
from common.transformations.orientation import (ecef_euler_from_ned,
                                                euler2quat,
                                                ned_euler_from_ecef,
                                                quat2euler,
                                                rotations_from_quats)
from selfdrive.locationd.kalman.helpers import ObservationKind, KalmanError
from selfdrive.locationd.kalman.models.live_kf import LiveKalman, States
from selfdrive.swaglog import cloudlog

SENSOR_DECIMATION = 1  # No decimation

class Localizer():
  def __init__(self, disabled_logs=[], dog=None):
    self.kf = LiveKalman()
    self.reset_kalman()
    self.max_age = .2  # seconds
    self.disabled_logs = disabled_logs

  def liveLocationMsg(self, time):
    fix = messaging.log.LiveLocationData.new_message()

    predicted_state = self.kf.x

    fix_ecef = predicted_state[States.ECEF_POS]
    fix_pos_geo = coord.ecef2geodetic(fix_ecef)
    fix.lat = float(fix_pos_geo[0])
    fix.lon = float(fix_pos_geo[1])
    fix.alt = float(fix_pos_geo[2])

    fix.speed = float(np.linalg.norm(predicted_state[States.ECEF_VELOCITY]))

    orientation_ned_euler = ned_euler_from_ecef(fix_ecef, quat2euler(predicted_state[States.ECEF_ORIENTATION]))
    fix.roll = math.degrees(orientation_ned_euler[0])
    fix.pitch = math.degrees(orientation_ned_euler[1])
    fix.heading = math.degrees(orientation_ned_euler[2])

    fix.gyro = [float(predicted_state[10]), float(predicted_state[11]), float(predicted_state[12])]
    fix.accel = [float(predicted_state[19]), float(predicted_state[20]), float(predicted_state[21])]

    local_vel = rotations_from_quats(predicted_state[States.ECEF_ORIENTATION]).T.dot(predicted_state[States.ECEF_VELOCITY])
    fix.pitchCalibration = math.degrees(math.atan2(local_vel[2], local_vel[0]))
    fix.yawCalibration = math.degrees(math.atan2(local_vel[1], local_vel[0]))

    #fix.imuFrame = [(180/np.pi)*float(predicted_state[23]), (180/np.pi)*float(predicted_state[24]), (180/np.pi)*float(predicted_state[25])]
    return fix

  def update_kalman(self, time, kind, meas):
    if self.filter_ready:
      try:
        self.kf.predict_and_observe(time, kind, meas)
      except KalmanError:
        cloudlog.error("Error in predict and observe, kalman reset")
        self.reset_kalman()
    #idx = bisect_right([x[0] for x in self.observation_buffer], time)
    #self.observation_buffer.insert(idx, (time, kind, meas))
    #while len(self.observation_buffer) > 0 and self.observation_buffer[-1][0] - self.observation_buffer[0][0] > self.max_age:
    #  else:
    #    self.observation_buffer.pop(0)

  def handle_gps(self, current_time, log):
    converter = coord.LocalCoord.from_geodetic([log.latitude, log.longitude, log.altitude])
    fix_ecef = converter.ned2ecef([0, 0, 0])

    # TODO initing with bad bearing not allowed, maybe not bad?
    if not self.filter_ready and log.speed > 5:
      self.filter_ready = True
      initial_ecef = fix_ecef
      gps_bearing = math.radians(log.bearing)
      initial_pose_ecef = ecef_euler_from_ned(initial_ecef, [0, 0, gps_bearing])
      initial_pose_ecef_quat = euler2quat(initial_pose_ecef)
      gps_speed = log.speed
      quat_uncertainty = 0.2**2
      initial_pose_ecef_quat = euler2quat(initial_pose_ecef)

      initial_state = LiveKalman.initial_x
      initial_covs_diag = LiveKalman.initial_P_diag

      initial_state[States.ECEF_POS] = initial_ecef
      initial_state[States.ECEF_ORIENTATION] = initial_pose_ecef_quat
      initial_state[States.ECEF_VELOCITY] = rotations_from_quats(initial_pose_ecef_quat).dot(np.array([gps_speed, 0, 0]))

      initial_covs_diag[States.ECEF_POS_ERR] = 10**2
      initial_covs_diag[States.ECEF_ORIENTATION_ERR] = quat_uncertainty
      initial_covs_diag[States.ECEF_VELOCITY_ERR] = 1**2
      self.kf.init_state(initial_state, covs=np.diag(initial_covs_diag), filter_time=current_time)
      cloudlog.info("Filter initialized")
    elif self.filter_ready:
      self.update_kalman(current_time, ObservationKind.ECEF_POS, fix_ecef)
      gps_est_error = np.sqrt((self.kf.x[0] - fix_ecef[0])**2 +
                              (self.kf.x[1] - fix_ecef[1])**2 +
                              (self.kf.x[2] - fix_ecef[2])**2)
      if gps_est_error > 50:
        cloudlog.error("Locationd vs ubloxLocation difference too large, kalman reset")
        self.reset_kalman()

  def handle_car_state(self, current_time, log):
    self.speed_counter += 1

    if self.speed_counter % SENSOR_DECIMATION == 0:
      self.update_kalman(current_time, ObservationKind.ODOMETRIC_SPEED, [log.vEgo])
      if log.vEgo == 0:
        self.update_kalman(current_time, ObservationKind.NO_ROT, [0, 0, 0])

  def handle_cam_odo(self, current_time, log):
    self.update_kalman(current_time,
                       ObservationKind.CAMERA_ODO_ROTATION,
                       np.concatenate([log.rot, log.rotStd]))
    self.update_kalman(current_time,
                       ObservationKind.CAMERA_ODO_TRANSLATION,
                       np.concatenate([log.trans, log.transStd]))

  def handle_sensors(self, current_time, log):
    # TODO does not yet account for double sensor readings in the log
    for sensor_reading in log:
      # Gyro Uncalibrated
      if sensor_reading.sensor == 5 and sensor_reading.type == 16:
        self.gyro_counter += 1
        if self.gyro_counter % SENSOR_DECIMATION == 0:
          if max(abs(self.kf.x[States.IMU_OFFSET])) > 0.07:
            cloudlog.info('imu frame angles exceeded, correcting')
            self.update_kalman(current_time, ObservationKind.IMU_FRAME, [0, 0, 0])

          v = sensor_reading.gyroUncalibrated.v
          self.update_kalman(current_time, ObservationKind.PHONE_GYRO, [-v[2], -v[1], -v[0]])

      # Accelerometer
      if sensor_reading.sensor == 1 and sensor_reading.type == 1:
        self.acc_counter += 1
        if self.acc_counter % SENSOR_DECIMATION == 0:
          v = sensor_reading.acceleration.v
          self.update_kalman(current_time, ObservationKind.PHONE_ACCEL, [-v[2], -v[1], -v[0]])

  def reset_kalman(self):
    self.filter_time = None
    self.filter_ready = False
    self.observation_buffer = []

    self.gyro_counter = 0
    self.acc_counter = 0
    self.speed_counter = 0

def locationd_thread(sm, pm, disabled_logs=[]):
  if sm is None:
    sm = messaging.SubMaster(['gpsLocationExternal', 'sensorEvents', 'cameraOdometry'])
  if pm is None:
    pm = messaging.PubMaster(['liveLocation'])

  localizer = Localizer(disabled_logs=disabled_logs)

  while True:
    sm.update()

    for sock, updated in sm.updated.items():
      if updated:
        t = sm.logMonoTime[sock] * 1e-9
        if sock == "sensorEvents":
          localizer.handle_sensors(t, sm[sock])
        elif sock == "gpsLocationExternal":
          localizer.handle_gps(t, sm[sock])
        elif sock == "carState":
          localizer.handle_car_state(t, sm[sock])
        elif sock == "cameraOdometry":
          localizer.handle_cam_odo(t, sm[sock])

    if localizer.filter_ready and sm.updated['gpsLocationExternal']:
      t = sm.logMonoTime['gpsLocationExternal']
      msg = messaging.new_message()
      msg.logMonoTime = t

      msg.init('liveLocation')
      msg.liveLocation = localizer.liveLocationMsg(t * 1e-9)

      pm.send('liveLocation', msg)

def main(sm=None, pm=None):
  locationd_thread(sm, pm)

if __name__ == "__main__":
  import os
  os.environ["OMP_NUM_THREADS"] = "1"
  main()

max_stars_repo_name: rdamron/openpilot

max_issues_repo_name: rdamron/openpilot

max_forks_repo_name: rdamron/openpilot


text:
import numpy as np
import shutil, sys, os, pickle
sys.path.append("..")

from SwitchVAE import *
from VAE import *
from utils import save_volume, data_IO, arg_parser, model

ConFig=tf.ConfigProto()
ConFig.gpu_options.allow_growth=True
session=tf.Session(config=ConFig)

def main():

    latent_dims = 128

    interpolation_save_path = '/home/zmy/Downloads/allCategory_uniLoss1/voxel_latent_dict/'
    latent_vector_file1 = open('/home/zmy/Downloads/allCategory_uniLoss1/voxel_latent_dict/latent_dict_airplane_test_sub.pkl', 'rb')
    latent_vector_file2 = open('/home/zmy/Downloads/allCategory_uniLoss1/voxel_latent_dict/latent_dict_car_test_sub.pkl', 'rb')
    weights_path = '/home/zmy/Downloads/allCategory_uniLoss1/weightsEnd_voxDecoder.h5'

    object1 = '4b4fd540cab0cdf3f38bce64a8733419'
    object2 = '117c0e0aafc0c3f81015cdff13e6d9f3'

    # Get the latent vector of two objects
    latent_vector_dict1 = pickle.load(latent_vector_file1)
    latent_vector_dict2 = pickle.load(latent_vector_file2)
    p1, p2 = latent_vector_dict1[object1+'_z'], latent_vector_dict2[object2+'_z']
    latent_vectors = np.linspace(p1,p2,11)

    # Define the decoder model
    decoder = model.get_voxel_decoder(latent_dims)
    decoder.load_weights(weights_path,by_name=True)
    reconstructions = decoder.predict(latent_vectors)

    reconstructions[reconstructions > 0] = 1
    reconstructions[reconstructions < 0] = 0

    if not os.path.exists(interpolation_save_path):
        os.makedirs(interpolation_save_path)

    shutil.copy2(__file__, interpolation_save_path)
    for i in range(reconstructions.shape[0]):
        name = str(i)
        save_volume.save_binvox_output(reconstructions[i, 0, :], name, interpolation_save_path, '_gen', save_bin= True, save_img= True)

if __name__ == '__main__':
    main()

ext: py

reason: import numpy


text:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager
fontprop = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")
plt.rcParams['font.family'] = 'IPAPGothic'

class Graph:
    def __init__(self, title, style):
        self.title = title
        self.style = style
        self.fig, self.ax = plt.subplots()
        self.x = []
        self.y = []
        self.xlabel = None
        self.ylabel = None
        self.legend = None

    def add_data(self, x, y, name=None):
        if len(x) == len(y):
            self.x.append(x)
            self.y.append(y)
            return 1
        else:
            print("ERROR: The data length of x and y is different.")
            return -1

    def draw(self, output=None, show=False):
        try:
            for i in range(len(self.x)):
                self.ax.plot(self.x[i], self.y[i])
            self.ax.set_title(self.title)
            self.ax.set_xlabel(self.xlabel)
            self.ax.set_ylabel(self.ylabel)

            if output: plt.savefig(output)
            if show: plt.show()
            return 1
        except:
            return -1


text:
import numpy as np
import pytest

from nilspodlib.utils import convert_little_endian, validate_existing_overlap

@pytest.mark.parametrize(
    "byte_vals, ints",
    [([0], 0), ([0, 0, 0, 0], 0), ([1, 0], 1), ([0, 1], 256), ([1, 1], 257)],
)
def test_little_endian_simple(byte_vals, ints):
    assert convert_little_endian(byte_vals) == ints

@pytest.mark.parametrize("bytes, ints", [(np.array([[0, 1], [1, 0]]), np.array([256, 1]))])
def test_little_endian_array(bytes, ints):
    assert np.array_equal(convert_little_endian(bytes), ints)

@pytest.mark.parametrize(
    "starts, stops, result",
    [
        ([0, 1], [1, 2], False),
        ([0, 0], [1, 1], True),
        ([0, 0.5], [1, 1.5], True),
        ([0, 0, 0], [1, 1, 1], True),
        ([0, 0, 1], [1, 1, 2], False),
    ],
)
def test_validate_overlap(starts, stops, result):
    assert validate_existing_overlap(np.array(starts), np.array(stops)) == result

@pytest.mark.parametrize(
    "starts, stops",
    [([1, 1], [0, 0]), ([0, 0], [-1, 1])],
)
def test_validate_overlap_error(starts, stops):
    with pytest.raises(ValueError):
        validate_existing_overlap(np.array(starts), np.array(stops))

max_stars_repo_name: mad-lab-fau/NilsPodPythonLib

max_issues_repo_name: mad-lab-fau/NilsPodPythonLib

max_forks_repo_name: mad-lab-fau/NilsPodPythonLib


text:
import numpy as np
import cv2, os

from faceApi.alignment import align
from faceApi import settings
from faceApi.recognition import Recognizer
from faceApi.emotions import EmotionExtractor
from faceApi.utils import get_labels
from faceApi.static_pathes import known_faces_encodes_npy_file_path, known_faces_ids_npy_file_path
from faceApi.static_pathes import emotion_model_path, faceTageModelPath
from faceApi.ultra_light_Detector import UltraLightOnnxFaceDetector
from face_recognition.api import _raw_face_landmarks

# initialize the face detection model
ultraLightFD = UltraLightOnnxFaceDetector(target_model=640)# target_model = 320*240/640*480
print("\n[INFO]loaded face detector Model successfully!")
# Initialize face recognition class
recognizer = Recognizer(known_faces_encodes_npy_file_path, known_faces_ids_npy_file_path, face_tag_model_path=faceTageModelPath)
print("\n[INFO]loaded face recognizer Model successfully!")
# emotion extractor
emotions_labels = get_labels('fer2013') # fer2013 : is the dataset name
emotion_extractor = EmotionExtractor(emotion_model_path,emotions_labels)
print("\n[INFO]loaded emotion_extractor Model successfully!")

#-----------------------------------------------------------------------------------

def face_detection(frame, detection_threshold = 0.9):
    '''
    detected faces in given frame using flobal model ultraLightFD
        parameters :
            frame :               (numpy 2d array)            rgb opencv / numpy fram/image.

            detection_threshold : (float between 0.0 and 1.0) detection threshold that any detected face with less confidence than threshold will be ignored.

        returns:
            faces_locations: (numpy array) contains each detected faces bounding boxes in format (x1, y1, x2, y2) or (left, top, right, bottom), where (x1, y1) is the upper left corner of the box, (x2, y2) is the lower right corner of the box.
            faces_confidences: (1d numpy array of floar between 0.0 and 1.0) contains probability/confidences of detected boxes
            width : (1d numpy array of int) contains the width of each detected face.

    '''
    # tensorflow model in ONNX framework to detect faces
    faces_locations, faces_confidences, widths =  ultraLightFD.detect(frame, threshold=detection_threshold)
    return faces_locations, faces_confidences, widths

# -----------------------------------------------------------------------------------

def select_topK_faces(faces_container,dicarded_faces_container, k=10):
    '''
    sort faces by its confidences and select the top K faces of them
        parameters :
            faces_container :           (list of list) contains each face [location, confidence, width].

            dicarded_faces_container :  (list of list) contains the discarded faces location to append to it the remains faces that does not in top selected k.

            k : the number of top faces to be selected.

        returns :
            selected_faces_locations :    (list of [x1,y1,x2,y2])                 the top k selected faces locations
            selected_faces_confidences :  (list of floats between 0.0 and 1.0)    the top k selected faces confidences
            selected_faces_widths :       (list of int)                           the top k selected faces widths
            dicarded_faces_container :    (list of [x1,y1,x2,y2])                 list of the remaining faces that haven't been selected with the top k faces.

    '''
    # sort faces by confidences
    confidence_index = 1
    width_index = 2
    # sort the faces by confidence and if 2 is equal in confidence sort them by width.
    sorted_faces  = sorted(faces_container, key=lambda x: (x[confidence_index],x[width_index]), reverse=True)
    # select top k faces or all faces if faces count is less than top k required
    top_picked_faces_count = min(len(sorted_faces), k)
    # initailize the return variables
    selected_faces_locations, selected_faces_confidences, selected_faces_widths = [], [], []
    for t in range(top_picked_faces_count):
        selected_faces_locations.append(sorted_faces[t][0])
        selected_faces_confidences.append(sorted_faces[t][1])
        selected_faces_widths.append(sorted_faces[t][2])

    # add the remaining faces to the discarded list.
    for j in range(top_picked_faces_count,len(faces_container)):
        dicarded_faces_container.append((sorted_faces[j][0], sorted_faces[j][1], sorted_faces[j][2]))

    return selected_faces_locations, selected_faces_confidences, selected_faces_widths, dicarded_faces_container

# -------------------------------------------------------------------------------------

def filter_faces_with_minimum_width(faces_locations, faces_confidences, faces_widths, minimum_face_width=48):
    '''
    filter list of faces by its width
        parameters:
            faces_locations: (numpy array/list) contains each detected faces bounding boxes in format (x1, y1, x2, y2) or (left, top, right, bottom), where (x1, y1) is the upper left corner of the box, (x2, y2) is the lower right corner of the box.

            faces_confidences: (1d numpy array of floar between 0.0 and 1.0) contains probability/confidences of detected boxes

            width : (1d numpy array of int) contains the width of each detected face.

            minimum_face_width : minimum face width which discard anyface with width less than minimum.

        returns:
            filtered_faces : (list of [face_location, confidenc, width]) of filted faces
            dicarded_faces : (list of [face_location, confidenc, width]) of discarded faces.
    '''
    filtered_faces = [(face_loc, confidence, width) for (face_loc, confidence, width) in zip(faces_locations, faces_confidences, faces_widths) if width>= minimum_face_width]
    dicarded_faces = [(face_loc, confidence, width) for (face_loc, confidence, width) in zip(faces_locations, faces_confidences, faces_widths) if width< minimum_face_width]

    return filtered_faces ,dicarded_faces

# -------------------------------------------------------------------------------------

def get_faces_landmarks(frame, faces_locations):
    '''
    get landmarks for each detected face
        parameters :
            frame : (numpy 2d array)            rgb opencv / numpy fram/image.
            faces_locations: (numpy array/list) contains each detected faces bounding boxes in format (x1, y1, x2, y2) or (left, top, right, bottom), where (x1, y1) is the upper left corner of the box, (x2, y2) is the lower right corner of the box.

        returns :
            faces_landmarks : (list of dlib full_object_detection object) that contains the facial landmarks for each face.

    '''
    faces_landmarks = _raw_face_landmarks(frame, faces_locations, model="small") # face_recogintion.api lib
    return faces_landmarks

# -------------------------------------------------------------------------------------

def get_faces_batches(frame, faces_locations, faces_landmarks, models_target_sizes=[]):
    '''
    get faces batches with different size for different models.
        parameters :
            frame :                 (numpy 2d array)   rgb opencv / numpy fram/image.

            faces_locations:        (numpy array/list) contains each detected faces bounding boxes in format (x1, y1, x2, y2) or (left, top, right, bottom), where (x1, y1) is the upper left corner of the box, (x2, y2) is the lower right corner of the box.

            faces_landmarks :       (list of dlib full_object_detection object) that contains the facial landmarks for each face.

            models_target_sizes :   (list of 2d tuple ex: [(width, height), (...),... ]) : to return batches for each model with its suitable input shape

        returns :
            model_faces_batches :   (list of numpy arrays as batches) list of batches each batch contains faces with same shape [[batch_size, w, h, channels]]
    '''
    model_faces_batches = align(frame, faces_locations, faces_landmarks,target_sizes=models_target_sizes)
    return model_faces_batches

# --------------------------------------------------------------------------------------

def get_face_ids(frame,faces_locations, recognition_threshold_distance = 0.4, save_rate=5, with_gender_race = False):
    '''
    try to recognize given faces and assign new id for those who r not known or seen before.
        parameters :
            frame : (numpy 2d array) rgb opencv / numpy fram/image.

            recognition_threshold_distance : (float between 0.0 and 1.0) verfy 2 faces if the distance between them less than or equal the given threshold, the allowed distance between known face encodes and detected one , the lower the more accurate.

            save_rate : (int) the number of new faces encodes with its new ids that will be saved to the disk after added them to the known faces list.

            with_gender_race :  (boolen) true if want to use face encodings that predicted by recoginition net to feed to trained MLP to classify the gender and race.

        returns:

            detected_faces_ids :       (list of int) contains the ids of recognized faces if the recognize paramter flag is True, otherwise will return an empty list because there is no recognition process.

            faces_landmarks :          (list of dlib full_object_detection object) that contains the facial land marks for each face

            faces_encodes :            (list of numpy array with shape of (128,)) contains each face estimated encodes.

            genders :                  (list of strings) contains the gender(Male/Female) of each face.

            races :                    (list of strings) contains the race (white, black, asian, etc..) of each face.

    '''
    detected_faces_ids, faces_landmarks, faces_encodes, genders, races = recognizer.recognize(frame, faces_locations, recognition_threshold_distance=recognition_threshold_distance, save_rate=save_rate, with_gender_race=with_gender_race)
    return detected_faces_ids, faces_landmarks, faces_encodes, genders, races

# --------------------------------------------------------------------------------------

def face_analysis(frame, detection_threshold = 0.6 , recognition_threshold_distance = 0.4, save_new_ids_rate=5,
                    minimum_face_width=40, top_picked_faces=10, recognize = True,
                    recogniion_with_race_gender=True, anaylize_emotions=False):

    '''
    face detection and recognition with gender, race and emotions detection
        parameters :
            frame :                             (numpy 2d array)            rgb opencv / numpy fram/image.

            detection_threshold :               (float between 0.0 and 1.0) detection threshold that any detected face with less confidence than threshold will be ignored.

            recognition_threshold_distance :    (float between 0.0 and 1.0) verfy 2 faces if the distance between them less than or equal the given threshold,
                                            the allowed distance between known face encodes and detected one , the lower the more accurate.

            save_new_ids_rate :                 (int) the number of new faces encodes with its new ids that will be saved to the disk after added them to the known faces list.

            minimum_face_width :                (int) the minmum width of acceptable detected face, ignore any detected face with width less than the minimum.

            top_picked_faces :                  (int) sort faces by confidence and select only the top K faces to work with.

            recognize :                         (boolen) true if face recogonition process is required, false other wise

            recogniion_with_race_gender :       (boolen) true if want to use face encodings that predicted by recoginition net to feed to trained MLP to classify the gender and race. (NOTE: recognize parameter should be true to use the predicted encodes to feed to the MLP(facetag model) other wise this parameter will be ignored.

            anaylize_emotions :                 (boolen) true if u want to analyize faces emotions.

        returns:

            selected_faces_locations : (list of list contains 4 elements (x1, y1, x2, y2)), ex:[[x1,y1,x2,y2], [...], ...] the faces location in given frame after been filterd by minimum width and selected the top k faces of all faces.

            faces_confidences :        (list of floats between 0.0 and 1.0) contains the confidence of prediction of each face.

            detected_faces_ids :       (list of int) contains the ids of recognized faces if the recognize paramter flag is True, otherwise will return an empty list because there is no recognition process.

            faces_landmarks :          (list of dlib full_object_detection object) that contains the facial land marks for each face

            genders :                  (list of strings) contains the gender(Male/Female) of each face.
            races :                    (list of strings) contains the race (white, black, asian, etc..) of each face.
            emotions :                 (list of strings) contains the emotion (happy, angry, sad, fear, etc..) of each face.
            dicarded_faces :           (list of list contains 4 elements (x1, y1, x2, y2)), ex:[[x1,y1,x2,y2], [...], ...] the discarded faces on the two filtering proces(minimum width and top k selected).
    '''

    # initialize all returned data to be empty.
    selected_faces_locations,faces_confidences, detected_faces_ids, emotions = [], [], [], []
    faces_landmarks, genders, races, dicarded_faces = [], [], [], []

    if settings.Debug:
        print("\n[INFO][face_analysis_agent] recived frame with shape = ", frame.shape)

    # step one detect faces
    faces_locations, faces_confidences, faces_widths = face_detection(frame, detection_threshold=detection_threshold)

    # check if no faces found in the given frame return empty lists, mean nothing found.
    if len(faces_locations)==0:
        return -1

    # filter the founded faces with minimum width (filter phase 1)
    filtered_faces, dicarded_faces = filter_faces_with_minimum_width(faces_locations, faces_confidences, faces_widths, minimum_face_width=minimum_face_width)

    # if there are remaining faces after width filtering process (continue to filter phase 2)
    if len(filtered_faces) > 0:
        # choose K top faces from all detected faces (filter phase 2)
        selected_faces_locations, faces_confidences, _, dicarded_faces = select_topK_faces(filtered_faces, dicarded_faces, k=top_picked_faces)

        # check if recogonize process required.
        if recognize:
            # will return the genders and races if with_gender_race flage is equal to true.
            detected_faces_ids, faces_landmarks, _, genders, races = get_face_ids(frame,selected_faces_locations, recognition_threshold_distance = recognition_threshold_distance, save_rate=save_new_ids_rate, with_gender_race = recogniion_with_race_gender)

        # if emotion anaylise required
        if anaylize_emotions:

            if len(faces_landmarks)==0: #if faces land marks not detectd before, detect them now!
                # reformating the faces bounding box to fit dlib format
                face_locations_for_landMarks = [(top, right, bottom, left) for [left, top, right, bottom] in selected_faces_locations]
                # use face_recognition api to get facial landmarks (5 points model)
                faces_landmarks = _raw_face_landmarks(frame, face_locations_for_landMarks, model='small')

            # get aligned face batches to feed to emotion extractor model.
            faces_batch = align(frame, selected_faces_locations, faces_landmarks,target_sizes=[emotion_extractor.get_input_shape()])

            emotions_batch = faces_batch[0]
            emotions = emotion_extractor.predict(emotions_batch)

    return selected_faces_locations,faces_confidences, detected_faces_ids, faces_landmarks, genders, races, emotions, dicarded_faces

max_stars_repo_name: OmarSayedMostafa/Face-analysis

max_issues_repo_name: OmarSayedMostafa/Face-analysis

max_forks_repo_name: OmarSayedMostafa/Face-analysis


text:
import unittest
from pydre import project
from pydre import core
from pydre import filters
from pydre import metrics
import os
import glob
import contextlib
import io
from tests.sample_pydre import project as samplePD
from tests.sample_pydre import core as c
import pandas
import numpy as np
from datetime import timedelta
import logging
import sys

class WritableObject:
    def __init__(self):
        self.content = []

    def write(self, string):
        self.content.append(string)

# Test cases of following functions are not included:
# Reason: unmaintained
# in common.py:
# tbiReaction()
# tailgatingTime() & tailgatingPercentage()
# ecoCar()
# gazeNHTSA()
#
# Reason: incomplete
# in common.py:
# findFirstTimeOutside()
# brakeJerk()

class TestPydre(unittest.TestCase):

    ac_diff = 0.000001
    # the acceptable difference between expected & actual results when testing scipy functions

    def setUp(self):
        # self.whatever to access them in the rest of the script, runs before other scripts
        self.projectlist = ["honda.json"]
        self.datalist = ["Speedbump_Sub_8_Drive_1.dat", "ColTest_Sub_10_Drive_1.dat"]
        self.zero = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        funcName = ' [ ' + self._testMethodName + ' ] ' # the name of test function that will be executed right after this setUp()
        print(' ')
        print (funcName.center(80,'#'))
        print(' ')

    def tearDown(self):
        print(' ')
        print('[ END ]'.center(80, '#'))
        print(' ')

    # ----- Helper Methods -----
    def projectfileselect(self, index: int):
        projectfile = self.projectlist[index]
        fullpath = os.path.join("tests/test_projectfiles/", projectfile)
        return fullpath

    def datafileselect(self, index: int):
        datafile = self.datalist[index]
        fullpath = glob.glob(os.path.join(os.getcwd(), "tests/test_datfiles/", datafile))
        return fullpath

    def secs_to_timedelta(self, secs):
        return timedelta(weeks=0, days=0, hours=0, minutes=0, seconds=secs)

    def compare_cols(self, result_df, expected_df, cols):
        result = True
        for names in cols:
            result = result and result_df[names].equals(expected_df[names])
            if not result:
                print(names)
                print(result_df[names])
                print("===")
                print(expected_df[names])
                return False
        return result

    # convert a drivedata object to a str
    def dd_to_str(self, drivedata: core.DriveData):
        output = ""
        output += str(drivedata.PartID)
        output += str(drivedata.DriveID)
        output += str(drivedata.roi)
        output += str(drivedata.data)
        output += str(drivedata.sourcefilename)
        return output

    # ----- Test Cases -----
    def test_datafile_exist(self):
        datafiles = self.datafileselect(0)
        self.assertFalse(0 == len(datafiles))
        for f in datafiles:
            self.assertTrue(os.path.isfile(f))

    def test_reftest(self):

        desiredproj = self.projectfileselect(0)
        p = project.Project(desiredproj)

        results = p.run(self.datafileselect(0))
        results.Subject.astype('int64')

        sample_p = samplePD.Project(desiredproj)
        expected_results = (sample_p.run(self.datafileselect(0)))
        self.assertTrue(self.compare_cols(results, expected_results, ['ROI', 'getTaskNum']))

    def test_columnMatchException_excode(self):
        f = io.StringIO()
        with self.assertRaises(SystemExit) as cm:
            desiredproj = self.projectfileselect(0)
            p = project.Project(desiredproj)
            result = p.run(self.datafileselect(1))
        self.assertEqual(cm.exception.code, 1)

    def test_columnMatchException_massage(self):
        d3 = {'DatTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184]}

        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        handler = logging.FileHandler(filename='tests\\temp.log')
        filters.logger.addHandler(handler)
        with self.assertRaises(core.ColumnsMatchError):
            result = filters.smoothGazeData(data_object)
        expected_console_output = "Can't find needed columns {'FILTERED_GAZE_OBJ_NAME'} in data file ['test_file3.csv'] | function: smoothGazeData"

        temp_log = open('tests\\temp.log')
        msg_list = temp_log.readlines()
        msg = ' '.join(msg_list)
        filters.logger.removeHandler(handler)
        #self.assertIn(expected_console_output, msg)

    #Isolate this test case No more sliceByTime Function in pydre.core
    def test_core_sliceByTime_1(self):
        d = {'col1': [1, 2, 3, 4, 5, 6], 'col2': [7, 8, 9, 10, 11, 12]}
        df = pandas.DataFrame(data=d)
        result = (c.sliceByTime(1, 3, "col1", df).to_string()).lstrip()
        expected_result = "col1  col2\n0     1     7\n1     2     8\n2     3     9"
        self.assertEqual(result, expected_result)

    #Isolate this test case No more sliceByTime Function in pydre.core
    def test_core_sliceByTime_2(self):
        d = {'col1': [1, 1.1, 3, 4, 5, 6], 'col2': [7, 8, 9, 10, 11, 12]}
        df = pandas.DataFrame(data=d)
        result = (c.sliceByTime(1, 2, "col1", df).to_string()).lstrip()
        expected_result = "col1  col2\n0   1.0     7\n1   1.1     8"
        self.assertEqual(result, expected_result)

    def test_core_mergeBySpace(self):
        d1 = {'SimTime': [1, 2], 'XPos': [1, 3], 'YPos': [4, 3]}
        df1 = pandas.DataFrame(data=d1)

        d2 = {'SimTime': [3, 4], 'XPos': [10, 12], 'YPos': [15, 16]}
        df2 = pandas.DataFrame(data=d2)

        data_object1 = core.DriveData.initV2(PartID=0,DriveID=1, data=df1, sourcefilename="test_file.csv")
        data_object2 = core.DriveData.initV2(PartID=0, DriveID=2, data=df2, sourcefilename="test_file.csv")

        param = []
        param.append(data_object1)
        param.append(data_object2)
        result = self.dd_to_str(core.mergeBySpace(param))
        expected_result = "01None   SimTime  XPos  YPos\n0        1     1     4\n1        2     3     3\n0        2    10    15\n1        3    12    16test_file.csv"
        self.assertEqual(result, expected_result)

    def test_filter_numberSwitchBlocks_1(self):
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'TaskStatus': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object3 = core.DriveData( data=df, sourcefilename="test_file3.csv")
        result = filters.numberSwitchBlocks(drivedata=data_object3)

        expected = {'DatTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
        'TaskStatus': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        'taskblocks': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]}
        expected_result_df = pandas.DataFrame(data=expected)
        expected_result = core.DriveData( data=expected_result_df, sourcefilename="test_file3.csv")

        print(result.data)
        print(expected_result.data)

        self.assertEqual(len(result.data), len(expected_result.data))
        self.assertTrue((self.compare_cols(expected_result.data, result.data, ['DatTime', 'TaskStatus', 'taskblocks'])))
        self.assertEqual(result.sourcefilename, expected_result.sourcefilename)

    def test_filter_numberSwitchBlocks_2(self):
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'TaskStatus': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} #input
        df = pandas.DataFrame(data=d)
        data_object3 = core.DriveData( data=df, sourcefilename="test_file3.csv")
        result = filters.numberSwitchBlocks(drivedata=data_object3)
        #print(result.to_string())
        expected = {'DatTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
        'TaskStatus': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        'taskblocks': [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]}
        expected_result_df = pandas.DataFrame(data=expected)
        expected_result = core.DriveData( data=expected_result_df, sourcefilename="test_file3.csv")
        self.assertEqual(len(result.data), len(expected_result.data))
        self.assertTrue((self.compare_cols(expected_result.data, result.data, ['DatTime', 'TaskStatus', 'taskblocks'])))
        self.assertEqual(result.sourcefilename, expected_result.sourcefilename)

    def test_filter_numberSwitchBlocks_3(self):
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'TaskStatus': [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object3 = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = filters.numberSwitchBlocks(drivedata=data_object3)
        #print(result.to_string())
        expected = {'DatTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
        'TaskStatus': [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
        'taskblocks': [np.nan, np.nan, np.nan, np.nan, np.nan, 1.0, 1.0, 1.0, 1.0, np.nan, np.nan]}
        expected_result_df = pandas.DataFrame(data=expected)
        expected_result = core.DriveData( data=expected_result_df, sourcefilename="test_file3.csv")
        self.assertEqual(len(result.data), len(expected_result.data))
        self.assertTrue((self.compare_cols(expected_result.data, result.data, ['DatTime', 'TaskStatus', 'taskblocks'])))
        self.assertEqual(result.sourcefilename, expected_result.sourcefilename)

    def test_filter_smoothGazeData_1(self):
        d3 = {'DatTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'FILTERED_GAZE_OBJ_NAME': ['localCS.CSLowScreen', 'localCS.CSLowScreen', 'localCS.CSLowScreen',
                          'localCS.CSLowScreen', 'localCS.CSLowScreen', 'localCS.CSLowScreen',
                           'localCS.CSLowScreen', 'localCS.CSLowScreen', 'localCS.CSLowScreen',
                             'localCS.CSLowScreen', 'localCS.CSLowScreen']}
        # the func should be able to identify this in-valid input and returns None after prints
        # "Bad gaze data, not enough variety. Aborting"
        print("expected console output: Bad gaze data, not enough variety. Aborting")

        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = filters.smoothGazeData(data_object)
        #print(result.to_string())
        self.assertEqual(None, result)

    def test_filter_smoothGazeData_2(self):
        d3 = {'DatTime': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
        1.9, 2.0, 2.1, 2.2, 2.3, 2.4],
        'FILTERED_GAZE_OBJ_NAME': ['localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane']}

        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = filters.smoothGazeData(data_object, latencyShift=0)

        dat_time_col = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
        1.9, 2.0, 2.1, 2.2, 2.3, 2.4]
        timedelta_col = []
        for t in dat_time_col:
            timedelta_col.append(self.secs_to_timedelta(t))
        expected = {'timedelta': timedelta_col, 'DatTime': dat_time_col,
        'FILTERED_GAZE_OBJ_NAME': ['localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.WindScreen', 'localCS.WindScreen', 'localCS.WindScreen',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane'],
        'gaze': ["offroad", "offroad", "offroad", "offroad", "onroad", "onroad", "onroad", "onroad", "onroad", "onroad", "onroad",
        "onroad", "onroad", "onroad", "onroad", "offroad", "offroad", "offroad", "offroad", "offroad", "offroad", "offroad", "offroad",
        "offroad"],
        'gazenum': np.array([1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3], dtype=np.int32)}

        expected_result_df = pandas.DataFrame(data=expected)

        self.assertTrue(expected_result_df.equals(result.data));
        #self.assertTrue(self.compare_cols(result.data[0], expected_result_df, ['DatTime', 'FILTERED_GAZE_OBJ_NAME', 'gaze', 'gazenum']))

    def test_filter_smoothGazeData_3(self):
        # --- Construct input ---
        dat_time_col = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
        1.9, 2.0, 2.1, 2.2, 2.3, 2.4]
        gaze_col = ['localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.WindScreen', 'localCS.dashPlane',
        'localCS.WindScreen', 'localCS.dashPlane', 'localCS.WindScreen',
        'localCS.dashPlane', 'localCS.WindScreen', 'localCS.dashPlane',
        'localCS.WindScreen', 'localCS.dashPlane', 'localCS.WindScreen',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane',
        'localCS.dashPlane', 'localCS.dashPlane', 'localCS.dashPlane']

        d3 = {'DatTime': dat_time_col, 'FILTERED_GAZE_OBJ_NAME': gaze_col}

        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # ----------------------
        result = filters.smoothGazeData(data_object, latencyShift=0)
        print(result.data)

        timedelta_col = []
        for t in dat_time_col:
            timedelta_col.append(self.secs_to_timedelta(t))
        expected = {'timedelta': timedelta_col, 'DatTime': dat_time_col,
        'FILTERED_GAZE_OBJ_NAME': gaze_col,
        'gaze': ["offroad", "offroad", "offroad", "offroad", "offroad", "offroad",
        "offroad", "offroad", "offroad", "offroad", "offroad", "offroad",
        "offroad", "offroad", "offroad", "offroad", "offroad", "offroad",
        "offroad", "offroad", "offroad", "offroad", "offroad", "offroad"],
        'gazenum': np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.int32)}

        expected_result_df = pandas.DataFrame(data=expected)

        self.assertTrue(expected_result_df.equals(result.data));
        #self.assertTrue(self.compare_cols(result.data[0], expected_result_df, ['DatTime', 'FILTERED_GAZE_OBJ_NAME', 'gaze', 'gazenum']))

    def test_metrics_findFirstTimeAboveVel_1(self):
    	# --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [-0.000051, -0.000051, -0.000041, -0.000066, -0.000111, -0.000158, -0.000194, -0.000207, 0.000016, 0.000107, 0.000198]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.findFirstTimeAboveVel(data_object)
        expected_result = -1
        self.assertEqual(expected_result, result)

    def test_metrics_findFirstTimeAboveVel_2(self):
    	# --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.findFirstTimeAboveVel(data_object)
        expected_result = -1
        self.assertEqual(expected_result, result)

    def test_metrics_findFirstTimeAboveVel_3(self):
    	# --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.findFirstTimeAboveVel(data_object)
        expected_result = 5
        self.assertEqual(expected_result, result)

    def test_metrics_findFirstTimeAboveVel_4(self):
    	# --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.findFirstTimeAboveVel(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_findFirstTimeOutside_1(self):
        pass
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        #result = metrics.common.findFirstTimeOutside(data_object)
        #expected_result = 0
        #self.assertEqual(expected_result, result)

        #err: NameError: name 'pos' is not defined --------------------------------------------------------!!!!!!!!!

    def test_metrics_colMean_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMean(data_object, 'position')
        expected_result = 5
        self.assertEqual(expected_result, result)

    def test_metrics_colMean_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMean(data_object, 'position', 3)
        expected_result = 6.5
        self.assertEqual(expected_result, result)

    def test_metrics_colMean_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMean(data_object, 'position', 3)
        expected_result = np.nan
        #self.assertEqual(expected_result, result)
        np.testing.assert_equal(expected_result, result)

    def test_metrics_colSD_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colSD(data_object, 'position')
        expected_result = 3.1622776601683795
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_colSD_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colSD(data_object, 'position', 3)
        expected_result = 2.29128784747792
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_colSD_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colSD(data_object, 'position')
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_colMax_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMax(data_object, 'position')
        expected_result = 10
        self.assertEqual(expected_result, result)

    def test_metrics_colMax_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMax(data_object, 'position')
        expected_result = 9
        self.assertEqual(expected_result, result)

    def test_metrics_colMax_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMax(data_object, 'position')
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_colMin_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMin(data_object, 'position')
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_colMin_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'position': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.colMin(data_object, 'position')
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_timeAboveSpeed_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.timeAboveSpeed(data_object, 0, True)
        expected_result = 1.002994011976048
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_timeAboveSpeed_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData(data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.timeAboveSpeed(data_object, 0, False)
        expected_result = 0.1675
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_timeAboveSpeed_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'Velocity': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.timeAboveSpeed(data_object, 0, False)
        expected_result = 0.0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_roadExits_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'RoadOffset': [1.7679, 1.7679, 1.5551, 1.5551, 1.5551, 1.667174, 1.667174, 1.668028, 1.668028, 1.668028, 1.786122],
               'Velocity': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExits(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_roadExits_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
           'RoadOffset': [7.3, 7.4, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2],
             'Velocity': [0, 15.1, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExits(data_object)
        expected_result = 0.034
        self.assertEqual(expected_result, result)

    def test_metrics_roadExits_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
           'RoadOffset': [-1, -1, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2],
             'Velocity': [15.1, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExits(data_object)
        expected_result = 0.034
        self.assertEqual(expected_result, result)

    def test_metrics_roadExitsY_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
              'YPos': [1.7679, 1.7679, 1.5551, 1.5551, 1.5551, 1.667174, 1.667174, 1.668028, 1.668028, 1.668028, 1.786122],
               'Velocity': [0, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExitsY(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_roadExitsY_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
           'YPos': [7.3, 7.4, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2],
             'Velocity': [0, 15.1, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExitsY(data_object)
        expected_result = 0.184
        self.assertEqual(expected_result, result)

    def test_metrics_roadExitsY_3(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
           'YPos': [-1, -1, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2, 7.2],
             'Velocity': [15.1, 20.1, 21.0, 22.0, 23.12, 25.1, 26.3, 27.9, 30.1036, 31.3, 32.5]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.roadExitsY(data_object)
        expected_result = 0.184
        self.assertEqual(expected_result, result)

    def test_metrics_steeringEntropy_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'Steer': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.steeringEntropy(data_object)
        expected_result = 0.0
        self.assertEqual(expected_result, result)

    def test_metrics_steeringEntropy_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'Steer': [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.steeringEntropy(data_object)
        expected_result = 0.17147549009906388
        self.assertEqual(expected_result, result)

    def test_metrics_numOfErrorPresses_1(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'TaskFail': [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.numOfErrorPresses(data_object)
        expected_result = 1
        self.assertEqual(expected_result, result)

    def test_metrics_numOfErrorPresses_2(self):
        # --- construct input ---
        d = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'TaskFail': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.numOfErrorPresses(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_firstOccurance_1(self):
        # --- construct input ---

        df = pandas.DataFrame({"Col1": [10, 20, 15, 30, 45],
                   "Col2": [13, 23, 18, 33, 48],
                   "Col3": [17, 27, 22, 37, 52]},
                  index=[1, 2, 3, 4, 5])

        # -----------------------

        result = metrics.common.firstOccurrence(df, 'Col1')
        expected_result = 1
        print("======", result)
        self.assertEqual(expected_result, result)

    def test_metrics_lanePosition_sdlp_1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="sdlp")
        expected_result = 0.050418838619299945
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_mean_1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="mean")
        expected_result = 1.712675909090909
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_mean_2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="mean")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp")
        expected_result = 0.11609391266118639
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_noiseremove1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp", noisy="true")
        expected_result = 0.3609804008333242
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_noiseremove2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp", noisy="true")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_filtfilt1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp", filtfilt="true")
        expected_result = 0.04134629214979945
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_filtfilt2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="msdlp", filtfilt="true")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_violationDuration1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="violation_duration")
        expected_result = 0.17000000000000004
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_violationDuration2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="violation_duration")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_exits1(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [1.7679, 1.7679, 1.7679, 1.7679, 1.7679, 1.667689, 1.667689, 1.666987,
           1.666987, 1.665995, 1.664588],
           'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="exits")
        expected_result = 0.15000000000000002
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_msdlp_exits2(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          'Lane': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="exits")
        expected_result = 0
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_lanePosition_violationCount(self):
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
          'LaneOffset': [11.1, 11.1, 0, 0, 11.5, 11.667689, 11.7, 11.8, 11.9, 12.0, 12.1],
            'Lane': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")

        result = metrics.common.lanePosition(data_object, laneInfo="violation_count")
        expected_result = 1
        self.assertEqual(expected_result, result)

    def test_metrics_crossCorrelate_1(self):
        # --- construct input ---
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'HeadwayTime': [11.1, 11.1, 0, 0, 11.5, 11.667689, 11.7, 11.8, 11.9, 12.0, 12.1],
         'XPos': self.zero}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        result = metrics.common.crossCorrelate(data_object)
        # -----------------------
        expected_result = 0

        self.assertEqual(expected_result, result)

    def test_metrics_getTaskNum_1(self):
        # --- construct input ---
        d = {'TaskNum': [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.getTaskNum(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_boxMetrics_count_1(self):
        # --- Construct input ---
        d3 = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.084, 0.117, 0.134, 0.149, 0.166, 0.184],
             'FeedbackButton': [0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0],
             'BoxAppears': [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0]}
        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.boxMetrics(data_object)
        expected_result = 2
        self.assertEqual(expected_result, result)

    def test_metrics_boxMetrics_count_2(self):
        # --- Construct input ---
        d3 = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.084, 0.117, 0.134, 0.149, 0.166, 0.184],
             'FeedbackButton': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             'BoxAppears': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.boxMetrics(data_object)
        expected_result = 0
        self.assertEqual(expected_result, result)

    def test_metrics_boxMetrics_mean_1(self):
        # --- Construct input ---
        d3 = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.084, 0.117, 0.134, 0.149, 0.166, 0.184],
             'FeedbackButton': [0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0],
            'BoxAppears': [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0]}
        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.boxMetrics(data_object, stat='mean')
        expected_result = 0.015999999999999993
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_boxMetrics_sd_1(self):
        # --- Construct input ---
        d3 = {'SimTime': [0.017, 0.034, 0.05, 0.067, 0.084, 0.084, 0.117, 0.134, 0.149, 0.166, 0.184],
             'FeedbackButton': [0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0],
             'BoxAppears': [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0]}
        df = pandas.DataFrame(data=d3)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.boxMetrics(data_object, stat='sd')
        expected_result = 0.0010000000000000078
        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_crossCorrelate_1(self):
        # --- construct input ---
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'HeadwayTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'XPos': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'OwnshipVelocity': [10, 11, 13, 14, 15, 17, 20, 23, 26, 29, 31],
         'LeadCarVelocity': [13, 16, 18, 21, 25, 30, 32, 32.5, 34, 35, 37]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.crossCorrelate(data_object)
        expected_result = 0.9988164711609935

        self.assertTrue(self.ac_diff > abs(expected_result - result))

    def test_metrics_crossCorrelate_2(self):
        # --- construct input ---
        d = {'SimTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'DatTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'HeadwayTime': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'XPos': [0.52, 0.54, 0.55, 0.57, 0.59, 0.59, 0.6, 0.62, 0.64, 0.65, 0.67],
         'OwnshipVelocity': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         'LeadCarVelocity': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.crossCorrelate(data_object)
        expected_result = 0

        self.assertEqual(expected_result, result)

    def test_metrics_speedbumpHondaGaze_1(self):
        # --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'gaze': ['onroad', 'onroad', 'onroad', 'onroad',
             'offroad', 'offroad', 'offroad', 'offroad',
             'onroad', 'onroad', 'onroad'],
             'gazenum': [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3],
             'TaskFail': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             'TaskID': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'TaskNum':  [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'taskblocks': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'PartID': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.speedbumpHondaGaze(data_object)
        expected_result = [0.085, 0.5089820359281438, 0.05, 0.0425]
        i = 0
        while (i < 4):
            self.assertTrue(self.ac_diff > abs(expected_result[i] - result[i]))
            i = i + 1

    def test_metrics_speedbumpHondaGaze_2(self):
        # --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'gaze': ['onroad', 'onroad', 'onroad', 'onroad',
             'onroad', 'onroad', 'onroad', 'onroad',
             'onroad', 'onroad', 'onroad'],
             'gazenum': [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3],
             'TaskFail': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             'TaskID': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'TaskNum':  [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'taskblocks': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'PartID': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.speedbumpHondaGaze(data_object)
        expected_result = [0.135, 0.8083832335329343, np.nan, 0.045000000000000005]
        i = 0
        while (i < 4):
            if (np.isnan(result[i])):
                np.testing.assert_equal(expected_result[i], result[i])
            else:
                self.assertTrue(self.ac_diff > abs(expected_result[i] - result[i]))
            i = i + 1

    def test_metrics_speedbumpHondaGaze_3(self):
        # --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'gaze': ['onroad', 'onroad', 'onroad', 'onroad',
             'onroad', 'onroad', 'onroad', 'onroad',
             'onroad', 'onroad', 'onroad'],
             'gazenum': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
             'TaskFail': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             'TaskID': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'TaskNum':  [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'taskblocks': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'PartID': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.speedbumpHondaGaze(data_object)
        expected_result = [0.16699999999999998, 1.0, np.nan, 0.16699999999999998]
        i = 0
        while (i < 4):
            if (np.isnan(result[i])):
                np.testing.assert_equal(expected_result[i], result[i])
            else:
                self.assertTrue(self.ac_diff > abs(expected_result[i] - result[i]))
            i = i + 1

    def test_metrics_speedbumpHondaGaze_4(self):
        # --- construct input ---
        d = {'DatTime': [0.017, 0.034, 0.050, 0.067, 0.084, 0.1, 0.117, 0.134, 0.149, 0.166, 0.184],
             'gaze': ['offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad', 'offroad'],
             'gazenum': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
             'TaskFail': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             'TaskID': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'TaskNum':  [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'taskblocks': [1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 6],
             'PartID': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} #input
        df = pandas.DataFrame(data=d)
        data_object = core.DriveData( data=df, sourcefilename="test_file3.csv")
        # -----------------------

        result = metrics.common.speedbumpHondaGaze(data_object)
        expected_result = [0.0, 0.0, 0.16699999999999998, np.nan]
        i = 0
        while (i < 4):
            if (np.isnan(result[i])):
                np.testing.assert_equal(expected_result[i], result[i])
            else:
                self.assertTrue(self.ac_diff > abs(expected_result[i] - result[i]))
            i = i + 1

if __name__ == '__main__':
    #print("list: ", glob.glob(os.path.join(os.getcwd(),  "ExampleProject_Sub_1_Drive_1.dat")))

    unittest.main()

max_stars_repo_name: tkerwin/pydre

max_issues_repo_name: tkerwin/pydre

max_forks_repo_name: tkerwin/pydre


text:
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Choose a Layout by finding the most connected subset of qubits."""

import numpy as np
import scipy.sparse as sp
import scipy.sparse.csgraph as cs

from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
from qiskit.transpiler.exceptions import TranspilerError

class DenseLayout(AnalysisPass):
    """Choose a Layout by finding the most connected subset of qubits.

    This pass associates a physical qubit (int) to each virtual qubit
    of the circuit (Qubit).

    Note:
        Even though a 'layout' is not strictly a property of the DAG,
        in the transpiler architecture it is best passed around between passes
        by being set in `property_set`.
    """

    def __init__(self, coupling_map, backend_prop=None):
        """DenseLayout initializer.

        Args:
            coupling_map (Coupling): directed graph representing a coupling map.
            backend_prop (BackendProperties): backend properties object
        """
        super().__init__()
        self.coupling_map = coupling_map
        self.backend_prop = backend_prop
        self.cx_mat = None
        self.meas_arr = None
        self.num_cx = 0
        self.num_meas = 0

    def run(self, dag):
        """Run the DenseLayout pass on `dag`.

        Pick a convenient layout depending on the best matching
        qubit connectivity, and set the property `layout`.

        Args:
            dag (DAGCircuit): DAG to find layout for.

        Raises:
            TranspilerError: if dag wider than self.coupling_map
        """
        num_dag_qubits = sum([qreg.size for qreg in dag.qregs.values()])
        if num_dag_qubits > self.coupling_map.size():
            raise TranspilerError('Number of qubits greater than device.')

        # Get avg number of cx and meas per qubit
        ops = dag.count_ops()
        if 'cx' in ops.keys():
            self.num_cx = ops['cx']
        if 'measure' in ops.keys():
            self.num_meas = ops['measure']

        # Compute the sparse cx_err matrix and meas array
        device_qubits = self.coupling_map.size()
        if self.backend_prop:
            rows = []
            cols = []
            cx_err = []

            for edge in self.coupling_map.get_edges():
                for gate in self.backend_prop.gates:
                    if gate.qubits == edge:
                        rows.append(edge[0])
                        cols.append(edge[1])
                        cx_err.append(gate.parameters[0].value)
                        break
                else:
                    continue

            self.cx_mat = sp.coo_matrix((cx_err, (rows, cols)),
                                        shape=(device_qubits,
                                               device_qubits)).tocsr()

            # Set measurement array
            meas_err = []
            for qubit_data in self.backend_prop.qubits:
                for item in qubit_data:
                    if item.name == 'readout_error':
                        meas_err.append(item.value)
                        break
                else:
                    continue
            self.meas_arr = np.asarray(meas_err)

        best_sub = self._best_subset(num_dag_qubits)
        layout = Layout()
        map_iter = 0
        for qreg in dag.qregs.values():
            for i in range(qreg.size):
                layout[qreg[i]] = int(best_sub[map_iter])
                map_iter += 1
        self.property_set['layout'] = layout

    def _best_subset(self, num_qubits):
        """Computes the qubit mapping with the best connectivity.

        Args:
            num_qubits (int): Number of subset qubits to consider.

        Returns:
            ndarray: Array of qubits to use for best connectivity mapping.
        """
        if num_qubits == 1:
            return np.array([0])
        if num_qubits == 0:
            return []

        device_qubits = self.coupling_map.size()

        cmap = np.asarray(self.coupling_map.get_edges())
        data = np.ones_like(cmap[:, 0])
        sp_cmap = sp.coo_matrix((data, (cmap[:, 0], cmap[:, 1])),
                                shape=(device_qubits, device_qubits)).tocsr()
        best = 0
        best_map = None
        best_error = np.inf
        best_sub = None
        # do bfs with each node as starting point
        for k in range(sp_cmap.shape[0]):
            bfs = cs.breadth_first_order(sp_cmap, i_start=k, directed=False,
                                         return_predecessors=False)

            connection_count = 0
            sub_graph = []
            for i in range(num_qubits):
                node_idx = bfs[i]
                for j in range(sp_cmap.indptr[node_idx],
                               sp_cmap.indptr[node_idx + 1]):
                    node = sp_cmap.indices[j]
                    for counter in range(num_qubits):
                        if node == bfs[counter]:
                            connection_count += 1
                            sub_graph.append([node_idx, node])
                            break

            if self.backend_prop:
                curr_error = 0
                # compute meas error for subset
                avg_meas_err = np.mean(self.meas_arr)
                meas_diff = np.mean(
                    self.meas_arr[bfs[0:num_qubits]])-avg_meas_err
                if meas_diff > 0:
                    curr_error += self.num_meas*meas_diff

                cx_err = np.mean([self.cx_mat[edge[0], edge[1]]
                                  for edge in sub_graph])
                if self.coupling_map.is_symmetric:
                    cx_err /= 2
                curr_error += self.num_cx*cx_err
                if connection_count >= best and curr_error < best_error:
                    best = connection_count
                    best_error = curr_error
                    best_map = bfs[0:num_qubits]
                    best_sub = sub_graph

            else:
                if connection_count > best:
                    best = connection_count
                    best_map = bfs[0:num_qubits]
                    best_sub = sub_graph

        # Return a best mapping that has reduced bandwidth
        mapping = {}
        for edge in range(best_map.shape[0]):
            mapping[best_map[edge]] = edge
        new_cmap = [[mapping[c[0]], mapping[c[1]]] for c in best_sub]
        rows = [edge[0] for edge in new_cmap]
        cols = [edge[1] for edge in new_cmap]
        data = [1]*len(rows)
        sp_sub_graph = sp.coo_matrix((data, (rows, cols)),
                                     shape=(num_qubits, num_qubits)).tocsr()
        perm = cs.reverse_cuthill_mckee(sp_sub_graph)
        best_map = best_map[perm]
        return best_map

max_stars_repo_name: romainfd/qiskit-terra

max_issues_repo_name: romainfd/qiskit-terra

max_forks_repo_name: romainfd/qiskit-terra

reason: import numpy,import scipy


text:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import patches
import matplotlib.patches as mpatches
import scipy.io as sio

# plotting configuration
ratio = 1.5
figure_len, figure_width = 15*ratio, 12*ratio
font_size_1, font_size_2 = 36*ratio, 36*ratio
legend_size = 18*ratio
line_width, tick_len = 3*ratio, 10*ratio
marker_size = 15*ratio
marker_edge_width = 3 * ratio
plot_line_width = 5*ratio
hfont = {'fontname': 'Arial'}
pal = sns.color_palette("deep")

b_summary_plot = True
b_example_frozen_inhibition = True
b_example_paradoxical_effect = True

if b_summary_plot:
    # simulation setup
    dt = 0.0001
    T = int(9 / dt)

    # neuronal parameters
    tau_e, tau_i = 0.020, 0.010
    alpha_e, alpha_i = 2, 2

    # short-term depression
    x, u_d = 1, 1
    tau_x = 0.20

    # network connectivity
    Jee = 1.8
    Jie = 1.0
    Jei = 1.0
    Jii = 0.6

    # input
    l_g_e_temp = np.arange(1.5, 1.8 + 0.001, 0.02)
    print(len(l_g_e_temp))
    l_ISN_index = []  # the largest eigenvalue of the Jacobian matrix
    lambda_1, lambda_2 = 0, 0

    for g_e_idx in range(len(l_g_e_temp)):
        g_e_temp = l_g_e_temp[g_e_idx]

        r_e, r_i = 0, 0
        z_e, z_i = 0, 0
        l_r_e, l_r_i, l_x = [], [], []
        Jacobian_mat = np.zeros((2, 2)) * np.nan

        for i in range(T):
            if 50000 <= i < 70000:
                g_e, g_i = g_e_temp, 2
            else:
                g_e, g_i = 1.55, 2

            g_e = g_e * (g_e > 0)
            g_i = g_i * (g_i > 0)

            # SSN part
            z_e = Jee * x * r_e - Jei * r_i + g_e
            z_i = Jie * r_e - Jii * r_i + g_i

            z_e = z_e * (z_e > 0)
            z_i = z_i * (z_i > 0)

            r_e = r_e + (-r_e + np.power(z_e, alpha_e)) / tau_e * dt
            r_i = r_i + (-r_i + np.power(z_i, alpha_i)) / tau_i * dt

            r_e = r_e * (r_e > 0)
            r_i = r_i * (r_i > 0)

            x = x + ((1 - x) / tau_x - u_d * x * r_e) * dt
            x = np.clip(x, 0, 1)

            l_r_e.append(r_e)
            l_r_i.append(r_i)
            l_x.append(x)

        Jacobian_mat[0, 0] = 1.0 / tau_e * (
                    l_x[60000] * Jee * alpha_e * np.power(l_r_e[60000], (alpha_e - 1.0) / alpha_e) - 1)
        Jacobian_mat[0, 1] = 1.0 / tau_e * Jee * alpha_e * np.power(l_r_e[60000], (2 * alpha_e - 1.0) / alpha_e)
        Jacobian_mat[1, 0] = - u_d * l_x[60000]
        Jacobian_mat[1, 1] = -1.0 / tau_x - u_d * l_r_e[60000]
        lambda_1 = np.linalg.eig(Jacobian_mat)[0][0]
        lambda_2 = np.linalg.eig(Jacobian_mat)[0][1]
        l_ISN_index.append(np.max([lambda_1.real, lambda_2.real]))

    plt.figure(figsize=(figure_len, figure_width))
    ax = plt.gca()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(True)
    ax.spines['left'].set_visible(True)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax.spines[axis].set_linewidth(line_width)
    plt.tick_params(width=line_width, length=tick_len)
    plt.yscale('symlog', linthreshy=10)

    for i in range(len(l_ISN_index)):
        plt.plot(i, l_ISN_index[i], linestyle='none', marker='o', fillstyle='full', markeredgewidth=marker_edge_width * 2,
                 markersize=marker_size * 2, markeredgecolor='black', markerfacecolor='none')

    plt.xticks([0, 3, 6, 9], [3, 6, 9, 12], fontsize=font_size_1, **hfont)
    plt.plot(l_ISN_index, color='black', linewidth=plot_line_width)

    plt.xticks([0, 5, 10, 15], [1.5, 1.6, 1.7, 1.8], fontsize=font_size_1, **hfont)
    plt.yticks([-10000, -100, 0, 100, 10000], fontsize=font_size_1, **hfont)
    plt.xlabel('$g_E$', fontsize=font_size_1, **hfont)
    plt.ylabel('ISN index', fontsize=font_size_1, **hfont)
    plt.xlim([-0.5, len(l_g_e_temp) - 0.5])
    plt.ylim([-10000, 10000])
    plt.hlines(y=0, xmin=0, xmax=len(l_g_e_temp) - 1, colors='k', linestyles=[(0, (6, 6, 6, 6))], linewidth=line_width)
    plt.vlines(x=7 + 0.0044 / 0.02, ymin=-10000, ymax=10000, colors='k', linestyles=[(0, (6, 6, 6, 6))],
               linewidth=line_width)

    # ax.add_patch(patches.Rectangle((7 + 0.0044 / 0.02, 0), -10000, 10000, facecolor="grey", alpha=0.2, edgecolor="none"))

    plt.savefig('paper_figures/png/Revision_Fig_Point_2_8_ISN_index_inputs.png')
    plt.savefig('paper_figures/pdf/Revision_Fig_Point_2_8_ISN_index_inputs.pdf')

if b_example_frozen_inhibition:
    # simulation setup
    dt = 0.0001
    T = int(9 / dt)

    # neuronal parameters
    tau_e, tau_i = 0.020, 0.010
    alpha_e, alpha_i = 2, 2

    # adaptation
    x, u_d = 1, 1
    tau_x = 0.20

    # network connectivity
    Jee = 1.8
    Jie = 1.0
    Jei = 1.0
    Jii = 0.6

    x = 1
    r_e, r_i = 0, 0
    z_e, z_i = 0, 0

    l_r_e, l_r_i, l_x = [], [], []

    for i in range(T):
        g_e, g_i = 1.62, 2

        if 42000 <= i < 42001:
            r_e = r_e + 0.01
        else:
            pass

        g_e = g_e * (g_e > 0)
        g_i = g_i * (g_i > 0)

        # SSN part
        z_e = Jee * x * r_e - Jei * r_i + g_e
        z_i = Jie * r_e - Jii * r_i + g_i

        z_e = z_e * (z_e > 0)
        z_i = z_i * (z_i > 0)

        r_e = r_e + (-r_e + np.power(z_e, alpha_e)) / tau_e * dt

        if 42000 < i:
            pass
        else:
            r_i = r_i + (-r_i + np.power(z_i, alpha_i)) / tau_i * dt

        r_e = r_e * (r_e > 0)
        r_i = r_i * (r_i > 0)

        x = x + ((1 - x) / tau_x - u_d * x * r_e) * dt
        x = np.clip(x, 0, 1)

        l_r_e.append(r_e)
        l_r_i.append(r_i)
        l_x.append(x)

    l_r_e = np.asarray(l_r_e)
    l_r_i = np.asarray(l_r_i)
    l_x = np.asarray(l_x)

    plt.figure(figsize=(figure_len, figure_width))
    ax = plt.gca()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(True)
    ax.spines['left'].set_visible(True)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax.spines[axis].set_linewidth(line_width)
    plt.tick_params(width=line_width, length=tick_len)
    plt.yscale('symlog', linthreshy=0.1)

    plt.plot(l_r_e, color='blue', linewidth=plot_line_width)
    plt.plot(l_r_i, color='red', linewidth=plot_line_width)

    plt.xticks(np.arange(30000, 90000 + 5000, 20000), np.arange(0, 6 + 0.5, 2), fontsize=font_size_1, **hfont)
    plt.yticks([0, 1, 100, 10000], fontsize=font_size_1, **hfont)
    plt.xlabel('Time (s)', fontsize=font_size_1, **hfont)
    plt.ylabel('Firing rate (Hz)', fontsize=font_size_1, **hfont)
    plt.xlim([30000, 90000])
    plt.ylim([0, 10000])
    plt.legend(['Exc', 'Inh'], prop={"family": "Arial", 'size': font_size_1}, loc='upper right')
    plt.vlines(x=42001, ymin=0, ymax=10000, colors='k', linestyles=[(0, (6, 6, 6, 6))], linewidth=line_width)

    plt.savefig(
        'paper_figures/png/Revision_Fig_Point_2_8_Supralinear_network_2D_EE_STP_normalized_activity_frozen_inhibition_ISN_no_paradoxical_effect_a.png')
    plt.savefig(
        'paper_figures/pdf/Revision_Fig_Point_2_8_Supralinear_network_2D_EE_STP_normalized_activity_frozen_inhibition_ISN_no_paradoxical_effect_a.pdf')

if b_example_paradoxical_effect:
    # simulation setup
    dt = 0.0001
    T = int(9 / dt)

    # neuronal parameters
    tau_e, tau_i = 0.020, 0.010
    alpha_e, alpha_i = 2, 2

    # short-term depression
    x, u_d = 1, 1
    tau_x = 0.20

    # network connectivity
    Jee = 1.8
    Jie = 1.0
    Jei = 1.0
    Jii = 0.6

    x = 1
    r_e, r_i = 0, 0
    z_e, z_i = 0, 0

    l_r_e, l_r_i, l_x = [], [], []

    for i in range(T):
        g_e, g_i = 1.62, 2

        if 42000 < i <= 49000:
            g_i = 2.1
        else:
            pass

        g_e = g_e * (g_e > 0)
        g_i = g_i * (g_i > 0)

        # SSN part
        z_e = Jee * x * r_e - Jei * r_i + g_e
        z_i = Jie * r_e - Jii * r_i + g_i

        z_e = z_e * (z_e > 0)
        z_i = z_i * (z_i > 0)

        r_e = r_e + (-r_e + np.power(z_e, alpha_e)) / tau_e * dt
        r_i = r_i + (-r_i + np.power(z_i, alpha_i)) / tau_i * dt

        r_e = r_e * (r_e > 0)
        r_i = r_i * (r_i > 0)

        x = x + ((1 - x) / tau_x - u_d * x * r_e) * dt
        x = np.clip(x, 0, 1)

        l_r_e.append(r_e)
        l_r_i.append(r_i)
        l_x.append(x)

    l_r_e = np.asarray(l_r_e)
    l_r_i = np.asarray(l_r_i)
    l_x = np.asarray(l_x)

    plt.figure(figsize=(figure_len, figure_width))
    ax = plt.gca()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(True)
    ax.spines['left'].set_visible(True)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax.spines[axis].set_linewidth(line_width)
    plt.tick_params(width=line_width, length=tick_len)

    mean_e = l_r_e / np.mean(l_r_e[40000:42000])
    mean_i = l_r_i / np.mean(l_r_i[40000:42000])

    plt.plot(mean_e, color='blue', linewidth=plot_line_width)
    plt.plot(mean_i, color='red', linewidth=plot_line_width)

    plt.xticks([40000, 42000, 44000, 46000, 48000], [1.0, 1.2, 1.4, 1.6, 1.8], fontsize=font_size_1, **hfont)
    plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2], fontsize=font_size_1, **hfont)
    plt.xlabel('Time (s)', fontsize=font_size_1, **hfont)
    plt.ylabel('Normalized firing rate', fontsize=font_size_1, **hfont)
    plt.xlim([40000, 48000])
    plt.ylim([0, 1.2])
    plt.legend(['Exc', 'Inh'], prop={"family": "Arial", 'size': font_size_1})
    plt.hlines(y=1, xmin=42000, xmax=50000, colors='k', linestyles=[(0, (6, 6, 6, 6))], linewidth=line_width)
    plt.savefig(
        'paper_figures/png/Revision_Fig_Point_2_8_Supralinear_network_2D_EE_STP_normalized_activity_ISN_no_paradoxical_effect_b.png')
    plt.savefig(
        'paper_figures/pdf/Revision_Fig_Point_2_8_Supralinear_network_2D_EE_STP_normalized_activity_ISN_no_paradoxical_effect_b.pdf')