text:
import numpy as np
import SimpleITK as sitk
from interpolate import sitk_interpolation
import os

def rescale(dataset, patient_id, data_type, path_to_nrrd, interpolation_type, new_size, return_type, output_dir = ""):
    """
    Rescale a given nrrd file to a given size - either downsample or upsample.
    Args:
        dataset (str): Name of dataset.
        patient_id (str): Unique patient id.
        data_type (str): Type of data (e.g., ct, pet, mri, lung(mask), heart(mask)..)
        path_to_nrrd (str): Path to nrrd file.
        interpolation_type (str): Either 'linear' (for images with continuous values), 'bspline' (also for images but will mess up the range of the values), or 'nearest_neighbor' (for masks with discrete values).
        new_size (tuple): Tuple containing 3 values for shape to resample to. (x,y,z)
        return_type (str): Either 'sitk_object' or 'numpy_array'.
        output_dir (str): Optional. If provided, nrrd file will be saved there. If not provided, file will not be saved.
    Returns:
        Either a sitk image object or a numpy array derived from it (depending on 'return_type').
    Raises:
        Exception if an error occurs.
    """
    try:
        # calculate new spacing
        img = sitk.ReadImage(path_to_nrrd)
        old_size = img.GetSize()
        old_spacing = img.GetSpacing()
        new_spacing = ((old_size[0]*old_spacing[0])/new_size[0],
                        (old_size[1]*old_spacing[1])/new_size[1],
                        (old_size[2]*old_spacing[2])/new_size[2])
        print('{} {}'.format('new spacing: ', new_spacing))
        # interpolate
        new_sitk_object = sitk_interpolation(path_to_nrrd, interpolation_type, new_spacing)
        # check if the result shape matches the target
        assert new_sitk_object.GetSize() == new_size, "oops.. The shape of the returned array does not match your requested target shape."
        # save and return
        if output_dir != "":
            # write new nrrd
            writer = sitk.ImageFileWriter()
            writer.SetFileName(os.path.join(output_dir, "{}_{}_{}_interpolated_resized_rescaled_xx.nrrd".format(dataset, patient_id, data_type)))
            writer.SetUseCompression(True)
            writer.Execute(new_sitk_object)

        if return_type == "sitk_object":
            return new_sitk_object
        elif return_type == "numpy_array":
            return sitk.GetArrayFromImage(new_sitk_object)

    except Exception as e:
        print ("Error in {}_{}, {}".format(dataset, patient_id, e))

def downsample_img(dataset, patient_id, data_type, path_to_nrrd, target_shape, return_type, output_dir=""):
    """
    Not used. Resizes a given nrrd file to a given size in all three dimensions. This is an older downsampling function that uses block reduce. It works well for images, but needs to have a flag for working with masks (to ensure a binary output is acheived). Finally, reusing the interpolate function to downsample/upsample was found to be the best approach.
    Args:
        dataset (str): Name of dataset.
        patient_id (str): Unique patient id.
        data_type (str): Type of data (e.g., ct, pet, mri, lung(mask), heart(mask)..)
        path_to_nrrd (str): Path to nrrd file.
        target_shape (str): Tuple containing 3 values for size to downsample to: (x,y,z).
        return_type (str): Either 'sitk_object' or 'numpy_array'.
        output_dir (str): Optional. If provided, nrrd file will be saved there. If not provided, file will not be saved.
    Returns:
        Either a sitk image object or a numpy array derived from it (depending on 'return_type').
    Raises:
        Exception if an error occurs.
    """
    try:
        # load image and convert to array
        img = sitk.ReadImage(path_to_nrrd)
        arr = sitk.GetArrayFromImage(img)
        # figure our downsampling block size
        block = (arr.shape[0]/target_shape[2], arr.shape[1]/target_shape[1], arr.shape[2]/target_shape[0])
        # check if all are integers
        assert block[0].is_integer(), "Dividing your input Z shape and target Z shape should produce a whole number."
        assert block[1].is_integer(), "Dividing your input Y shape and target Y shape should produce a whole number."
        assert block[2].is_integer(), "Dividing your input X shape and target X shape should produce a whole number."
        # downsample. Since we asserted above already, we can safely convert the block values to integers.
        new_arr = block_reduce(arr, tuple([int(x) for x in block]), np.average)
        # calculate new spacing based on original_spacing and target_shape. Since we are downsampling, the assumption here is that the new spacing values will be greater than original spacing values.
        original_spacing = img.GetSpacing()
        new_spacing = (original_spacing[0]*block[2], original_spacing[1]*block[1], original_spacing[2]*block[0])
        # create SITK image
        new_sitk_object = sitk.GetImageFromArray(new_arr)
        new_sitk_object.SetSpacing(new_spacing)
        new_sitk_object.SetOrigin(img.GetOrigin())
        # print
        print('Orginal shape: {}'.format(img.GetSize()))
        print('Target shape: {}'.format(target_shape))
        print('Block (Z-first) : {}'.format(block))
        print('Original spacing: {}'.format(original_spacing))
        print('Target spacing: {}'.format(new_spacing))
        print('Shape after downsample: {}'.format(new_sitk_object.GetSize()))
        # check if the result shape matches the target
        assert new_sitk_object.GetSize() == target_shape, "oops.. The shape of the returned array does not match your requested target shape."
        # save and return
        if output_dir != "":
            writer = sitk.ImageFileWriter()
            writer.SetFileName(os.path.join(output_dir, "{}_{}_{}_interpolated_resized_downsampled_xx.nrrd".format(dataset, patient_id, data_type)))
            writer.SetUseCompression(True)
            writer.Execute(new_sitk_object)
        if return_type == "sitk_object":
            return new_sitk_object
        elif return_type == "numpy_array":
            return data
    except Exception as e:
        print ("Error in {}_{}, {}".format(dataset, patient_id, e))

max_stars_repo_name: xmuyzz/IVContrast

max_issues_repo_name: xmuyzz/IVContrast

max_forks_repo_name: xmuyzz/IVContrast

reason: import numpy


text:
###########################
# Minimum Cost Field Setup#
# Author: chinmay_dd            #
###########################

# Using the local development version of pymote
import sys
sys.path.append("../pymote2.1")
from pymote.message import Message
from pymote.algorithm import NodeAlgorithm
import pdb
# Using infinity from the numpy module
# Although, we will be using a infinity-like value in the actual implementation,
# this particular logic helps in algorithm implementation.
from numpy import inf
from math import isinf

# MCF Algorithm implementation (along with optimization)
class MCFSetup(NodeAlgorithm):
    # Key for data being updated in the node.
    required_params = ('sinkKey',)
    default_params = {}

# Helper method to check if the message is an advertisement
    def check_if_adv(self, message):
        return (message.header == self.ADV or message.header == NodeAlgorithm.INI)

    # Header used to advertise the lowest cost.
    ADV = "advertisement"

    # Status initializer for each node.
    def initializer(self):
        for node in self.network.nodes():
            node.memory["Timer"] = inf
            # If the node is a sink node, broadcase the message and set it to done.
            if self.sinkKey in node.memory:
                self.network.outbox.insert(0, Message(header=NodeAlgorithm.INI,
                            source=node, data=node.memory[self.sinkKey]))
                node.status = "DONE"
            else:
                # Set all other nodes to "IDLE"
                node.status = "IDLE"
                node.memory["BCost"] = inf

    # Method to calculate link cost from A->B
    # Cost of the link maybe a composite of:
    # hop count
    # consumed wireless energy
    # delay between source and sink
    # function of received signal strength (we can use the snr component,
    # i.e signal to noise ratio
    # number of retransmissions
    def calculate_cost(self, message):
        # Energy consumed during receving the message
        # consumed_wireless_energy = message.destination.power.energy_consumption
        # Closer the SNR is to 0 ==> Better the signal received
        # SNR contains all the params. We can choose it to calculate link_cost
        signal_to_noise_ratio = message.destination.snr[0]
        # Normalizing the snr value as (0-snr/10) and adding the already existing
        # message cost(received from the message) to the same.
        link_cost = float(message.data) + (0-(signal_to_noise_ratio/10))

        return link_cost

    def calculate_other_cost(self, message):
        return float(message.data) + message.destination.distance[0]

    # After the initialization process, each node is set to an idle state.
    # On receiving the first "ADV", the node should transition itself to the
    # listening state.
    # This implies that further messages are received but not broadcasted.
    # The broadcast duration will depend on a "backoff" algorithm as mentioned in
    # the reference paper.
    def idle(self, node, message):
        # Now, depending on the header of the message we can make two choices:
        # If the header is NodeAlgorithm.INI, this implies that the node is a
        # neighbor to the sink node.
        # Otherwise, the node is just another node in the network.
        # Either way, we will have the node transition into the "LISTENING" state
        # and implement the backoff algorithm.
        if self.check_if_adv(message):
            message_cost = self.calculate_other_cost(message)
            node.memory["BCost"] = message_cost
            node.memory["Timer"] = int(message_cost/2)
            node.status = "LISTENING"
        else:
            # This implies that the algorithm is now in operational mode.
            pass

    def listening(self, node, message):
        pass

    def done(self, node, message):
        pass

    def step(self, node):
        # Standard step code
        message = node.receive()
        if message:
            if (message.destination == None or message.destination == node):
                # when destination is None it is broadcast message
                return self._process_message(node, message)
            elif (message.nexthop == node.id):
                self._forward_message(node, message)

        # Timer-like code.
        if message and node.status == "LISTENING":
                if self.check_if_adv(message):
                    message_cost = self.calculate_other_cost(message)
                # Check if the advertised cost is less than the existing.
                if message_cost < node.memory["BCost"]:
                    # Set the new cost.
                    node.memory["BCost"] = message_cost
                    # Restart the timer.
                    node.memory["Timer"] = int(message_cost/2)
        elif not isinf(node.memory["Timer"]) and node.status == "LISTENING":
            node.memory["Timer"] -= 1
            if node.memory["Timer"] == 0:
                # Broadcast the new message and set the status as done.
                node.send(Message(header=self.ADV, data=str(node.memory["BCost"])))
                node.status = "DONE"

    STATUS = {
                "IDLE": idle,
                "DONE": done,
                "LISTENING": listening
            }

ext: py

max_stars_repo_name: chinmaydd/RouteSense

max_issues_repo_name: chinmaydd/RouteSense

max_forks_repo_name: chinmaydd/RouteSense

reason: from numpy


text:
import pandas as pd
import statsmodels.api as sm
from numpy.linalg.linalg import LinAlgError

def stepwise_selection(x, y, threshold_in=0.01, threshold_out=0.05):
    """
    通过逐步回归筛选特征变量

    Args:
        x: 待筛选的特征数据框
        y: 数据框标签
        threshold_in: 进入模型的最大P值
        threshold_out: 退出模型的最小P值

    Returns:
        included：特征名称列表
    """
    included = []
    numeric_var = x.select_dtypes(exclude=['object', 'datetime64']).columns
    while True:
        changed = False
        # forward step
        excluded = list(set(numeric_var)-set(included))
        new_pval = pd.Series(index=excluded)
        for new_column in excluded:
            try:
                model = sm.Logit(y, sm.add_constant(pd.DataFrame(x[included+[new_column]]))).fit()
                new_pval[new_column] = model.pvalues[new_column]
            except LinAlgError:
                continue
        best_pval = new_pval.min()
        if best_pval < threshold_in:
            best_feature = new_pval.idxmin()
            included.append(best_feature)
            changed = True

        # backward step
        model = sm.Logit(y, sm.add_constant(pd.DataFrame(x[included]))).fit()
        # use all coefs except intercept
        p_values = model.pvalues.iloc[1:]
        # null if p-values is empty
        worst_pval = p_values.max()
        if worst_pval > threshold_out:
            changed = True
            worst_feature = p_values.idxmax()
            included.remove(worst_feature)
        if not changed:
            break
    return included

reason: from numpy,import statsmodels


text:
# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    multi_linear_model.py                              :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: ciglesia <ciglesia@student.42.fr>          +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/11/26 00:02:39 by ciglesia          #+#    #+#              #
#    Updated: 2020/11/27 13:21:19 by ciglesia         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent.parent.absolute()))
from day01.ex03.mylinearregression import MyLinearRegression as MyLR
import day00.ex07.mse as m
from sklearn.metrics import mean_squared_error
import pandas as pd
import numpy as np

import matplotlib.pyplot as pyplot

def mse_(X, Y):
    return (np.sum(np.power(np.subtract(X,Y), 2))) * (1 / len(X))

data = pd.read_csv("../resources/spacecraft_data.csv")
X = np.array(data[['Age','Thrust_power','Terameters']])
Y = np.array(data[['Sell_price']]).reshape(-1,1)

print("age")
Xage = np.array(data[['Age']]).reshape(-1,1)
myLR_age = MyLR([[1000.0], [-1.0]])
print("fitting...")
myLR_age.fit_(Xage, Y, learning_rate = 2.5e-5, n_cycle = 1000)#00
y_model1 = myLR_age.predict_(Xage)
RMSE_age = m.mse(X[:,0].reshape(-1,1), Y)
print(RMSE_age)

print("plotting...")

pyplot.scatter(Xage, Y, label='age')
pyplot.scatter(Xage, y_model1, color='g')
pyplot.xlabel('age (in years)')
pyplot.ylabel('sell price (in keuros)')
pyplot.legend()
pyplot.show()

print("thrust power")
Xthrust = np.array(data[['Thrust_power']]).reshape(-1,1)
myLR_thrust = MyLR([[1000.0], [-1.0]])
print("fitting...")
myLR_thrust.fit_(Xthrust, Y, learning_rate = 2.5e-5, n_cycle = 10000)#0
y_model2 = myLR_thrust.predict_(Xthrust)

RMSE_thrust = m.mse(Xthrust, Y)
print(RMSE_thrust)

print("plotting...")

pyplot.scatter(Xthrust, Y, label='thrust', color='g')
pyplot.scatter(Xthrust, y_model2, color='y')
pyplot.xlabel('thrust power (in 10 Km/s)')
pyplot.ylabel('sell price (in Keuros')
pyplot.legend()
pyplot.show()

Xtera = np.array(data[['Terameters']]).reshape(-1,1)
myLR_tera = MyLR([[1000.0], [-1.0]])
print("fitting...")
myLR_tera.fit_(Xtera, Y, learning_rate = 2.5e-5, n_cycle = 10000)#0
y_model3 = myLR_tera.predict_(Xtera)

RMSE_tera = m.mse(Xtera, Y)
print(RMSE_tera)

print("plotting...")

pyplot.scatter(Xtera, Y, label='tmeters', color='purple')
pyplot.scatter(Xtera, y_model3, color='r')
pyplot.xlabel('distance totalizer value of spacecraft (in Tmeters)')
pyplot.ylabel('sell price (in Keuros')
pyplot.legend()
pyplot.show()


text:
import numpy as np
import numba

import strax
export, __all__ = strax.exporter()

@export
@numba.njit(cache=True, nogil=True)
def index_of_fraction(peaks, fractions_desired):
    """Return the (fractional) indices at which the peaks reach
    fractions_desired of their area
    :param peaks: strax peak(let)s or other data-bearing dtype
    :param fractions_desired: array of floats between 0 and 1
    :returns: (len(peaks), len(fractions_desired)) array of floats
    """
    results = np.zeros((len(peaks), len(fractions_desired)), dtype=np.float32)

    for p_i, p in enumerate(peaks):
        if p['area'] <= 0:
            continue  # TODO: These occur a lot. Investigate!
        compute_index_of_fraction(p, fractions_desired, results[p_i])
    return results

@export
@numba.jit(nopython=True, nogil=True, cache=True)
def compute_index_of_fraction(peak, fractions_desired, result):
    """Store the (fractional) indices at which peak reaches
    fractions_desired of their area in result
    :param peak: single strax peak(let) or other data-bearing dtype
    :param fractions_desired: array of floats between 0 and 1
    :returns: len(fractions_desired) array of floats
    """
    area_tot = peak['area']
    fraction_seen = 0
    current_fraction_index = 0
    needed_fraction = fractions_desired[current_fraction_index]
    for i, x in enumerate(peak['data'][:peak['length']]):
        # How much of the area is in this sample?
        fraction_this_sample = x / area_tot

        # Are we passing any desired fractions in this sample?
        while fraction_seen + fraction_this_sample >= needed_fraction:

            area_needed = area_tot * (needed_fraction - fraction_seen)
            if x != 0:
                result[current_fraction_index] = i + area_needed / x
            else:
                result[current_fraction_index] = i

            # Advance to the next fraction
            current_fraction_index += 1
            if current_fraction_index > len(fractions_desired) - 1:
                break
            needed_fraction = fractions_desired[current_fraction_index]

        if current_fraction_index > len(fractions_desired) - 1:
            break

        # Add this sample's area to the area seen
        fraction_seen += fraction_this_sample

    if needed_fraction == 1:
        # Sometimes floating-point errors prevent the full area
        # from being reached before the waveform ends
        result[-1] = peak['length']

@export
def compute_widths(peaks):
    """Compute widths in ns at desired area fractions for peaks
    returns (n_peaks, n_widths) array
    """
    if not len(peaks):
        return

    desired_widths = np.linspace(0, 1, len(peaks[0]['width']))
    # 0% are width is 0 by definition, and it messes up the calculation below
    desired_widths = desired_widths[1:]

    # Which area fractions do we need times for?
    desired_fr = np.concatenate([0.5 - desired_widths / 2,
                                 0.5 + desired_widths / 2])

    # We lose the 50% fraction with this operation, let's add it back
    desired_fr = np.sort(np.unique(np.append(desired_fr, [0.5])))

    fr_times = index_of_fraction(peaks, desired_fr)
    fr_times *= peaks['dt'].reshape(-1, 1)

    i = len(desired_fr) // 2
    peaks['width'] = fr_times[:, i:] - fr_times[:, ::-1][:, i:]
    peaks['area_decile_from_midpoint'] = fr_times[:, ::2] - fr_times[:, i].reshape(-1,1)

max_stars_repo_name: JoranAngevaare/strax

max_issues_repo_name: JoranAngevaare/strax

max_forks_repo_name: JoranAngevaare/strax

reason: import numpy,import numba


text:
import unittest
from pyalink.alink import *
import numpy as np
import pandas as pd
class TestJsonValueBatchOp(unittest.TestCase):
    def test_jsonvaluebatchop(self):

        df = pd.DataFrame([
             ["{a:boy,b:{b1:[1,2],b2:2}}"],
             ["{a:girl,b:{b1:[1,3],b2:2}}"]
         ])

        data = BatchOperator.fromDataframe(df, schemaStr='str string')

        JsonValueBatchOp()\
             .setJsonPath(["$.a", "$.b.b1[0]","$.b.b2"])\
             .setSelectedCol("str")\
             .setOutputCols(["f0","f1","f2"])\
             .linkFrom(data)\
             .print()
        pass


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import math
import time
import numpy as np
from PIL import Image
import threading
from utils.manual_gps import dist_p2p,manual_gps_y, manual_gps_x,find_nn,gen_manual_gps

def avg(values):
    summ = sum(values)
    return summ/len(values)

def normal(values, is_y=False):
    #avg_value = values[0]
    if is_y:
        avg_value = (11589596.333751025+11589605.59585501)/2
    else:
        avg_value = (3047362.377753752+3047362.981469983)/2
    return [(item - avg_value) for item in values]

def filt_gps(x_list, y_list, ts_list):
    filt_x = [-x_list[0]*scale_x + x_offset]
    filt_y = [y_list[0]*scale_y + y_offset]
    last_ts = float(ts_list[0])
    for i in range(len(x_list)-1):
        #ts = float(ts_list[i])
        next_s = float(ts_list[i+1])
        dt = max(0.01,next_s - last_ts)
        dist = dist_p2p(x_list[i], y_list[i], x_list[i+1], y_list[i+1])
        v = dist/dt

        if v > 0.0:
            last_ts = next_s

        if v < 5 and v > 0.0:
            x = x_list[i]
            y = y_list[i]
            _x = -x*scale_x + x_offset
            _y = y*scale_y + y_offset
            filt_x.append(_x)
            filt_y.append(_y)
        else:
            filt_x.append(filt_x[-1])
            filt_y.append(filt_y[-1])

    return filt_x, filt_y

data_index = 4
scale_x = 6.0
scale_y = 6.0
x_offset = 3300
y_offset = 2600

class NavMaker:
    def __init__(self, reader, imu):
        self.reader = reader
        self.imu = imu
        self.x = []
        self.y = []
        self.t = []
        self.last_angle = 0.
        self.gps_x, self.gps_y = gen_manual_gps(manual_gps_x, manual_gps_y)
        self.map = Image.open('map-mark.png')
        self.size_y = 800
        self.size_x = 400
        self.stop_read_event = threading.Event()

        self.read_cyclic = threading.Thread(
            target=self.read_data, args=()
        )
        self.nav = Image.open('nav.png').convert('RGB')
        self.yaw = 0.0

    def start(self):
        self.stop_read_event.clear()
        self.read_cyclic.start()

    def close(self):
        self.stop_read_event.set()

    def read_data(self):
        while not self.stop_read_event.is_set():
            x, y, t = self.reader.get()
            #print('get data', x, y)
            ax, ay, az, yaw, pitch, roll, w = self.imu.get()
            self.yaw = yaw
            self.get_gps(x, y, t)
            self.get_nav()
            time.sleep(0.05)

    def get(self):
        return self.nav

    def get_gps(self, x, y, t):
        nx = normal([x])[0]
        ny = normal([y], True)[0]
        self.x.append(nx)
        self.y.append(ny)
        self.t.append(t)

    def make_nav(self, angle, index):
        img2 = self.map.crop((self.gps_y[index]-self.size_y, self.gps_x[index]-self.size_x, self.gps_y[index]+self.size_y, self.gps_x[index]+self.size_x))
        im_rotate = img2.rotate(angle)
        size_y2 = 100
        size_x2 = 80
        img3 = im_rotate.crop((img2.size[0]//2-size_y2, img2.size[1]//2-2*size_x2, img2.size[0]//2+size_y2, img2.size[1]//2))
        #img4 = cv2.cvtColor(np.asarray(img3),cv2.COLOR_RGB2BGR)
        #cv2.imshow("OpenCV",img4)
        #cv2.waitKey(0)
        #cv2.destroyAllWindows()
        return img3.convert('RGB')

    def get_nav(self):
        if len(self.x) < 3:
          #print('No GPS')
          return

        if len(self.x) < 50:
            nx, ny, ts = self.x, self.y, self.t
            dy = (ny[-1] - ny[-2])
            dx = (nx[-1] - nx[-2])
            self.last_angle = 180.*math.atan2(dy, dx)/math.pi
        else:
            nx, ny, ts = self.x[-100:], self.y[-100:], self.t[-100:]

        fter_x, fter_y = filt_gps(nx, ny, ts)
        nn_x, nn_y, nn_index = find_nn(fter_x[-1], fter_y[-1], self.gps_x, self.gps_y)
        #dy = (self.gps_y[min(nn_index+5,len(fter_x)-1)] - self.gps_y[max(0,nn_index-5)])
        #dx = (self.gps_x[min(nn_index+5,len(fter_x)-1)] - self.gps_x[max(0,nn_index-5)])
        #angle = 180.*math.atan2(dy, dx)/math.pi

        #input_angle = 0.5*self.last_angle+0.5*angle
        #self.last_angle = input_angle
        #print('yaw', self.yaw*180./math.pi, input_angle)
        input_angle = self.yaw*180./math.pi
        self.nav = self.make_nav(-input_angle+80., nn_index)

max_stars_repo_name: ZJU-Robotics-Lab/CICT

max_issues_repo_name: ZJU-Robotics-Lab/CICT

max_forks_repo_name: ZJU-Robotics-Lab/CICT


text:
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function

import rospy
import numpy as np
import time

# ROS packages required
from std_srvs.srv import Empty
from std_msgs.msg import Float32MultiArray
from gazebo_msgs.srv import GetLinkState
from gazebo_msgs.msg import LinkState

if __name__ == '__main__':
    rospy.init_node('cablearning_node', anonymous=True, log_level=rospy.DEBUG)
    rospy.logwarn('initializing node finished')

    force_publisher=rospy.Publisher('/gazebo_client/force', Float32MultiArray, queue_size=10)
    reset_sim_proxy=rospy.ServiceProxy('/gazebo/reset_simulation', Empty)
    reset_world_proxy=rospy.ServiceProxy('/gazebo/reset_world', Empty)
    cmd_force = Float32MultiArray()
    # rate = rospy.Rate(10)
    while True:
        for _ in range(20):
            cmd_force.data = np.random.randn(4)
            force_publisher.publish(cmd_force)
            rospy.logdebug("Force: {}".format(cmd_force.data))
            # rate.sleep()
            rospy.wait_for_service('/gazebo/reset_simulation')
            time.sleep(.1)
        try:
            reset_sim_proxy()
            rospy.logwarn("Simulation reset!")
        except rospy.ServiceException as e:
            print ("/gazebo/reset_simulation service call failed")
            rospy.logwarn("Reset Simulation!")


text:
##########################################
#    Question 02 : Grayscale(灰度化)
##########################################
import cv2 as cv
import numpy as np

# Target : gray scale
def BGR2GRAY(img):
    b = img[:, :, 0].copy()
    g = img[:, :, 1].copy()
    r = img[:, :, 2].copy()
    # Gray scale
    out = 0.2126 * r + 0.7152 * g + 0.0722 * b
    out = out.astype(np.uint8)
    return out

# Read Image
img = cv.imread("./imori.jpg")

# Show & Save origin image
cv.imshow("Origin", img)
cv.imwrite("Origin_02.jpg", img)

# Gray scale
img = BGR2GRAY(img)

# Save result
cv.imwrite("Output_02.jpg", img)
cv.imshow("Result", img)

# Esc exit
cv.waitKey(0)
cv.destroyAllWindows()


text:
import sys

print(sys.path)
# import matplotlib
# import matplotlib.pyplot as plt
import random
import torch
from torch import nn, optim
from torch.autograd import Function
import numpy as np
import os
import pickle
import data_analysis_utils
from data_analysis_utils import (prepare_text_for_lda, build_reduced_glove_dict, convert_token_2_ids, text_to_topic,
                                 compute_topic_transition_matrix, filtere_off_unk_topics, generate_latex_table)
from collections import Counter
import dae_model
from dae_model import DictionaryAutoencoder
import time
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--random_seed', type=int, default=42)
parser.add_argument('--data_path', type=str, default='./final_pooled_backup')
parser.add_argument('--model_name', type=str, default='no_filter_train_30_10.pt')
parser.add_argument('--topic_path', type=str, default='./final_pooled_backup')
parser.add_argument('--device_id', type=int, default=0)
parser.add_argument('--config', type=str, default='entry_by_character')
parser.add_argument('--freq_threshold', type=int, default=30)
parser.add_argument('--occurrence_threshold', type=int, default=10)

args = parser.parse_args()

device = f'cuda:{str(args.device_id)}'
config = args.config
identifier = 'role'
type_text = 'entry'
with open(os.path.join( args.data_path, f'topic_transition_matrix_by_world_dict_{identifier}.pkl'), 'rb') as f:
    topic_transition_matrix_by_world_dict = pickle.load(f)

# load original data info
with open( os.path.join(args.data_path, 'text_by_story_world_dict.pkl'), 'rb') as f:
    text_by_story_world_dict = pickle.load(f)
world_counter = Counter()
for world, stories in text_by_story_world_dict.items():
    world_counter.update({world: len(stories)})
uid_by_story_world_dict_path = os.path.join(args.data_path, 'uid_by_story_world_dict.pkl')
if os.path.exists( uid_by_story_world_dict_path):
    uid_by_story_world_dict = pickle.load( open( uid_by_story_world_dict_path, 'rb'))
else:
    print('Please run the world_path')
uid_topic_list_path = os.path.join(args.data_path, 'uid_topic_list')
uid_topic_list = pickle.load( open(uid_topic_list_path, 'rb'))
uid_topic_dict = dict(uid_topic_list)

num_all, num_key_not_found, num_not_entry, num_counted = 0, 0, 0, 0
num_topics = 50

world_topic_freq_list_dict = {}
overall_freq = np.zeros( (num_topics,) )
for idx, world_tuple in enumerate(world_counter.most_common()):
    # if idx == 0: # ignore the None world
    #     continue
    world_name = world_tuple[0]
    stories = uid_by_story_world_dict[world_name]
    world_topics_transition_matrix = np.zeros((num_topics, num_topics))

    world_topic_freq_list = np.zeros( (num_topics, ) )

    for story_fname, inf_meta_uid_triples_list in stories.items(): # each story contains a list of text
        topic_list_per_identifier_dict = {}
        for inf_meta_uid_triple in inf_meta_uid_triples_list:
            _, meta, uid = inf_meta_uid_triple
            id_key = meta[identifier]

            num_all += 1
            if uid not in uid_topic_dict.keys():
                # print('The uid is not in the uid_topic_dict keys')
                num_key_not_found+=1

            if meta['type'] != type_text:
                # print('The type of text is not entry')
                num_not_entry += 1

            if uid in uid_topic_dict.keys() and meta['type'] == type_text:
                world_topic_freq_list[uid_topic_dict[uid]] += 1

    world_topic_freq_list_dict[world_name] = world_topic_freq_list
    overall_freq += world_topic_freq_list
    # print(f'Done with {idx+1}  worlds')

with open( os.path.join(args.topic_path, 'topics.txt'), 'r') as f:
    lines = f.readlines()

topics_summarized = []
idx_filter_off = []
for idx, line in enumerate( lines ):
    line_split = line.strip().split(': ')[1]
    topics_summarized.append(line_split)
    if line_split[:3] == 'unk':
        idx_filter_off.append(idx)

print()
def get_top_topics(overall_freq, topics_summarized, n_words, cutoff, threshold):
    overall_norm = overall_freq / np.sum(overall_freq)
    content = []
    freq_list = []
    for idx, ind in enumerate( reversed( np.argsort(overall_norm) ) ):
        if ind == 25: ## the stop word topic
            continue
        topic_words_str = ', '.join( topics_summarized[ind].split(', ')[:n_words] )
        freq_percentage = np.around(overall_norm[ind] * 100, 2)
        # print(f' freq: { freq_percentage } % {topic_words_str }')
        # print(ind)
        if freq_percentage < threshold:
            break

        freq_list.append(freq_percentage)
        content.append( [topic_words_str] )
        # if len(freq_list) == cutoff:
        #     break

    return freq_list, content

def get_relative_important_topics(given_freq, overall_freq, topics_summarized, n_words, cutoff, threshold):
    overall_freq_norm = overall_freq / np.sum(overall_freq)
    given_freq_norm = given_freq / np.sum(given_freq)
    relative_impt = given_freq_norm / (overall_freq_norm + 0.000005)
    content = []
    freq_list = []
    impt_list = []
    for idx, ind in enumerate( reversed( np.argsort(relative_impt) ) ):
        if ind == 25: ## the stop word topic
            continue
        topic_words_str = ', '.join( topics_summarized[ind].split(', ')[:n_words] )
        freq_percentage = np.around( given_freq_norm[ind] * 100, 2)
        # print(f' freq: { freq_percentage } % {topic_words_str }')
        # print(ind)
        if freq_percentage < threshold:
            continue

        impt_list.append(relative_impt[ind])
        freq_list.append(freq_percentage)
        content.append( [topic_words_str] )
        # if len(freq_list) == cutoff:
        #     break

    return impt_list, freq_list, content

cutoff = 20
n_words = 6
threshold = 1
# plot all tables of relative importance
world_row_names = []
topic_row_list = []
for idx, world_tuple in enumerate(world_counter.most_common()):
    if idx == 0: # ignore the None world
        continue
    world_name = world_tuple[0]
    print(world_name)
    # get_top_topics(world_topic_freq_list_dict[world_name], topics_summarized, 20)
    impt_list, freq_list, content= get_relative_important_topics(world_topic_freq_list_dict[world_name], overall_freq, topics_summarized, n_words, cutoff, threshold)
    # generate_latex_table(content=content, Sci_Notn=False, row_names = freq_list, col_names = ['topic words'], row_header='freq(\%)', caption=world_name)

    # print(world_name + ': ')
    # print(f'relative importance = {impt_list[0]}, freq = {freq_list[0]}, topic words: {content[0]}')

    world_row_names.append(world_name)
    topic_row_list.append( content[0] )

    print()
    if idx == 50:
        break

    # print("\\\\")
caption = 'CAPTION HERE'
generate_latex_table(content=topic_row_list, Sci_Notn=False, row_names = world_row_names, col_names = ['topic words'], row_header='worlds', caption=caption)

topics_2w = []
idx_filter_off = []
for idx, line in enumerate( lines ):
    line_split = line.strip().split(': ')[1]
    two_words = line_split.split(', ')[:2]
    topics_2w.append(' '.join(two_words))

def get_top_2(starting, topic_transition_matrix):
    all_candidates = topic_transition_matrix[starting]
    desc_order = np.argsort(all_candidates)

    top_3 = desc_order[-3:]

    if starting in top_3:
        indx_of_starting = np.where(top_3 == starting)[0][0]
        top_2 = np.delete(top_3, indx_of_starting)
    else:
        top_2 = top_3[-2:]
    return top_2

num_topics = 50
for starting in range(num_topics):
    if starting != 20:
        continue

    print('\n\n')
    print( '=' * 100)

    starting_topic = topics_2w[starting]

    print(f'Starting from topic {starting} {topics_summarized[starting]}: ')
    for idx, (world_name, _) in enumerate(world_counter.most_common()):
        print(f'[{world_name}]: ')
        if idx == 0:
            continue
        topic_transition_matrix = topic_transition_matrix_by_world_dict[world_name]
        one_and_two = get_top_2(starting, topic_transition_matrix)

        three_and_four = get_top_2(one_and_two[0], topic_transition_matrix)

        five_and_six = get_top_2(one_and_two[1], topic_transition_matrix)

        result = [starting, one_and_two[0], one_and_two[1], three_and_four[0], three_and_four[1], five_and_six[0], five_and_six[1] ]

        print(f'            {result[0]}              ')
        print(f'            {result[1]}              {result[2]}')
        print(f'            {result[3]}              {result[4]}            {result[5]}              {result[6]}')

        print(f'            {topics_2w[result[0]]}              ')
        print(f'            {topics_2w[result[1]]}              {topics_2w[result[2]]}')
        print(f'            {topics_2w[result[3]]}              {topics_2w[result[4]]}            {topics_2w[result[5]]}              {topics_2w[result[6]]}')

        print('\n\n')
        if idx >= 7:
            break

print('Done')


text:
import os
from os.path import isfile, join
from typing import List, Tuple

import matplotlib.pyplot as plt
import numpy as np
import torch
from PIL import Image
from torch.utils.data.dataset import Dataset
from torchvision import transforms

class Carvana(Dataset):
    def __init__(
            self,
            root: str,
            transform: transforms.Compose = None,
    ) -> None:
        """
        :param root: path to the data folder
        :param transform: transforms of the images and labels
        """
        self.root = os.path.expanduser(root)
        self.transform = transform
        (self.data_path, self.labels_path) = ([], [])

        def load_images(path: str) -> List[str]:
            """
            Return a list with paths to all images

            :param path: path to the data folder
            :return: list with paths to all images
            """
            images_dir = [join(path, f) for f in os.listdir(path)
                          if isfile(join(path, f))]
            images_dir.sort()

            return images_dir

        self.data_path = load_images(self.root + "/train")
        self.labels_path = load_images(self.root + "/train_masks")

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        :param index: sample index
        :return: tuple (img, target) with the input data and its label
        """
        img = Image.open(self.data_path[index])
        target = Image.open(self.labels_path[index])

        if self.transform is not None:
            img = self.transform(img)
            target = self.transform(target)
            target = (target > 0).float()

        return img, target

    def __len__(self):
        return len(self.data_path)

def im_show(img_list: List[Tuple[str, str]]) -> None:
    """
    Plots images with corresponding segmentation mask

    :param img_list: list of paths to images
    """

    to_PIL = transforms.ToPILImage()
    if len(img_list) > 9:
        raise Exception("len(img_list) must be smaller than 10")
    fig, axes = plt.subplots(len(img_list), 2, figsize=(16, 16))
    fig.tight_layout()

    for (idx, sample) in enumerate(img_list):
        a = axes[idx][0].imshow(np.array(to_PIL(sample[0])))
        b = axes[idx][1].imshow(np.array(to_PIL(sample[1])))
        for ax in axes[idx]:
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

    plt.show()


text:
"""
    A simple, time-varying vector field plot.

    u(x,y,z,t) represents the i-th component of the vector,
    v(x,y,z,t) represents the j-th component of the vector,
    w(x,y,z,t) represents the k-th component of the vector,
    thus,

        V(x,y,z,t) = u(x,y,z,t)i + v(x,y,z,t)j + w(x,y,z,t)k,

    where i, j, k are the unit vectors to the xyz coordinate system.
"""

# necessary imports
from vis3dvf.plot import Figure
from vis3dvf.vectorfield import *
import numpy as np

# i-th component of the time varying vector field
def u(x,y,z,t):
    return 0*x                                   # function must use at leas one of the x,y,z variables (due to numpy)

# j-th component of the time varying vector field
def v(x,y,z,t):
    return 0.1*np.sin(t + x)

# k-th component of the time varying vector field
def w(x,y,z,t):
    return 0*x                                   # function must use at leas one of the x,y,z variables

# Figure(window width, window height)
fig = Figure(800, 800, "Vector Field")

# VectorFieldT(u, v, w, vector density, initial time, final time, time delta)
fig.add(VectorFieldT(u, v, w, 6, 0, 2*np.pi, 0.05))

# Render field
fig.show()


text:
#!/usr/bin/env python3
from PIL import Image
from scipy import fftpack
import os
import huffmanEncode
import numpy as np
from numpy import *
import sys
import time

DEBUG = False

jpeg_header = 'FFD8FFE000104A46494600010100000100010000' # len = 40
lumQuantTbl_header = 'FFDB004300'
lumQuantTbl_header_dynamic = 'FFDB004310'
chromQuantTbl_header = 'FFDB004301'
chromQuantTbl_header_dynamic = 'FFDB004311'
heightwidth_header = 'FFC0001108'
subsamp = '03011100021101031101'
huffmanTable = 'FFC401A20000000701010101010000000000000000040503020601000708090A0B0100020203010101010100000000000000010002030405060708090A0B1000020103030204020607030402060273010203110400052112314151061361227181143291A10715B14223C152D1E1331662F0247282F12543345392A2B26373C235442793A3B33617546474C3D2E2082683090A181984944546A4B456D355281AF2E3F3C4D4E4F465758595A5B5C5D5E5F566768696A6B6C6D6E6F637475767778797A7B7C7D7E7F738485868788898A8B8C8D8E8F82939495969798999A9B9C9D9E9F92A3A4A5A6A7A8A9AAABACADAEAFA110002020102030505040506040803036D0100021103042112314105511361220671819132A1B1F014C1D1E1234215526272F1332434438216925325A263B2C20773D235E2448317549308090A18192636451A2764745537F2A3B3C32829D3E3F38494A4B4C4D4E4F465758595A5B5C5D5E5F5465666768696A6B6C6D6E6F6475767778797A7B7C7D7E7F738485868788898A8B8C8D8E8F839495969798999A9B9C9D9E9F92A3A4A5A6A7A8A9AAABACADAEAFA'
data_header = 'FFDA000C03010002110311003F00'

zigzagOrder = np.array([0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,
                           49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63])

def loadDetection(txtFileName):
    txt = open(txtFileName, 'r')
    lines = txt.readlines()
    for i, line in enumerate(lines):
        lines[i] = list(map(int, line.strip().split(' ')[-4:]))
    return lines

def isAround(bounding_box, lefttop):
    x, y = lefttop[0], lefttop[1]

    for left, top, right, bottom in bounding_box:
        if (left - 8 < x) and (x < right + 8) and (bottom - 8 > y) and (y > top + 8) :
            return True
    return False

def getBinNum(frameStr):
    return int(frameStr[5:-4])

def binCompare(fileNameA, fileNameB):
    aNum = getBinNum(fileNameA)
    bNum = getBinNum(fileNameB)
    return aNum - bNum

def main():

    srcFileName = sys.argv[1]
    outFileName = sys.argv[2]
    prevDetections = ''
    if(len(sys.argv)>3):
        prevDetections = sys.argv[3]
        bb = loadDetection(prevDetections)
        print("{} detections loaded.".format(len(bb)))

    t0 = time.time()

    print("Decoding {}...".format(srcFileName))

    f = open(srcFileName,'rb')

    # JPEG header
    target = jpeg_header
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("JPEG header good")
    else:
        if DEBUG:
            print("JPEG header bad")
        sys.exit()

    # lumQuantTbl header & value
    target = lumQuantTbl_header
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("lumQuantTbl header good. non dynamic")
        b = f.read(64)
        luminanceQuantTbl = np.array(list(b)).reshape([8,8])
    elif b == huffmanEncode.hexToBytes(lumQuantTbl_header_dynamic):
        if DEBUG:
            print("lumQuantTbl header good. dynamic")
        b = f.read(64)
        luminanceQuantTbl = np.array(list(b)).reshape([8,8])
        b = f.read(64)
        luminanceQuantTbl_lo = np.array(list(b)).reshape([8,8])
    else:
        if DEBUG:
            print("lumQuantTbl header bad")
        sys.exit()

    # chromQuantTbl header & value
    target = chromQuantTbl_header
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("chromQuantTbl header good. non dynamic")
        b = f.read(64)
        chrominanceQuantTbl = np.array(list(b)).reshape([8,8])
    elif b == huffmanEncode.hexToBytes(chromQuantTbl_header_dynamic):
        if DEBUG:
            print("chromQuantTbl header good. dynamic")
        b = f.read(64)
        chrominanceQuantTbl = np.array(list(b)).reshape([8,8])
        b = f.read(64)
        chrominanceQuantTbl_lo = np.array(list(b)).reshape([8,8])
    else:
        if DEBUG:
            print("chromQuantTbl header bad")
        sys.exit()

    # height width header & value
    target = heightwidth_header
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("height width header good")
        b = f.read(2)
        height = int.from_bytes(b,"big")
        b = f.read(2)
        width = int.from_bytes(b,"big")
        if DEBUG:
            print("h : {}, w : {}".format(height,width))
    else:
        if DEBUG:
            print("height width header bad")
        sys.exit()

    if (width % 8 != 0):
        width = width // 8 * 8 + 8
    if (height % 8 != 0):
        height = height // 8 * 8 + 8

    target = subsamp
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("subsamp good")
    else:
        if DEBUG:
            print("subsamp bad")
        sys.exit()

    target = huffmanTable
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("huffmanTable good")
    else:
        if DEBUG:
            print("huffmanTable bad")
        sys.exit()

    target = data_header
    b = f.read(len(target)//2)
    if b == huffmanEncode.hexToBytes(target):
        if DEBUG:
            print("data header good")
    else:
        if DEBUG:
            print("data header bad")
        sys.exit()

    t1 = time.time()
    print("Before loading binary : {}".format(t1 - t0))

    # load rest of binary data
    binarys = ''
    b = f.read(1)
    cnt = 1
    while 1:
        if(b == b'\xFF'):
            if f.read(1) == b'\xD9': # remove 00 after FF
                cnt = cnt - 1
                break
        bins = bin(list(b)[0])[2:]
        while len(bins) != 8:
            bins = '0' + bins
        binarys = binarys + bins
        b = f.read(1)
        cnt = cnt + 1

        if DEBUG:
            print("Binary string of full data loaded")

    t2 = time.time()
    print("Loading binary : {}".format(t2 - t1))

    # decode them into YUV matrix(zigzag ordered)
    blockSum = width // 8 * height // 8
    YZCode = np.zeros([blockSum, 64], dtype=int)
    UZCode = np.zeros([blockSum, 64], dtype=int)
    VZCode = np.zeros([blockSum, 64], dtype=int)

    # per block, dy du dv not managed yet
    yuvZZ = np.zeros([3,64],dtype=int)

    i = 1
    blockNum = 0
    zzIndex = 0
    # stage
    #   yDC_size
    #   yDC_val
    #   yAC_runsize
    #   yAC_val
    #
    #   same for u & v
    #   blockNum++ when one block of yuv is done

    stage = 0 # [Y,U,V] = [0,1,2]
    runsize = 0

    if DEBUG:
        print("blockSum: {}".format(blockSum))

    while len(binarys) != 0:
        code = list(map(int, list(binarys[:i])))
        if zzIndex == 0:
            # DC
            size = huffmanEncode.decodeDC(code, stage)
            if (size != -1):
                if DEBUG:
                    print("DC")
                    print("(stage, zzIndex, blockNum) : {}, {}, {}".format(stage, zzIndex, blockNum))
                    print("i: {}".format(i))
                    print("size: {}".format(size))
                    print("code: {}".format(code))
                binarys = binarys[i:]
                i = 1

                if(size == 0):
                    yuvZZ[stage, zzIndex] = 0
                else:
                    val = binarys[:size]
                    binarys = binarys[size:]
                    if val[0] == '1':
                        yuvZZ[stage, zzIndex] = int(val, 2)
                        if DEBUG:
                            print("val: {}".format(int(val,2)))
                    else:
                        # complement & minus sign
                        yuvZZ[stage, zzIndex] = -int(''.join(['1' if bit == '0' else '0' for bit in list(val)]),2)
                        if DEBUG:
                            print("val: {}".format(yuvZZ[stage,zzIndex]))
                zzIndex += 1
            else:
                i += 1
        else:
            # AC
            runsize = huffmanEncode.decodeAC(code, stage)
            if (runsize != None):
                if DEBUG:
                    print()
                    print("AC")
                    print("(stage, zzIndex, blockNum) : {}, {}, {}".format(stage, zzIndex, blockNum))
                    print("i: {}".format(i))
                    print("runsize: {}".format(runsize))
                    print("code: {}".format(code))

                binarys = binarys[i:]
                i = 1
                run = int(runsize[0],16)
                size = int(runsize[1],16)

                if runsize == '00':
                    while zzIndex != 64:
                        yuvZZ[stage,zzIndex] = 0
                        zzIndex += 1
                else:
                    for _ in range(run):
                        yuvZZ[stage, zzIndex] = 0
                        zzIndex += 1

                    if(size == 0):
                        yuvZZ[stage, zzIndex] = 0
                    else:
                        val = binarys[:size]
                        binarys = binarys[size:]
                        if val[0] == '1':
                            yuvZZ[stage, zzIndex] = int(val, 2)
                            if DEBUG:
                                print("val: {}".format(yuvZZ[stage,zzIndex]))
                        else:
                            # complement & minus sign
                            yuvZZ[stage, zzIndex] = -int(''.join(['1' if bit == '0' else '0' for bit in list(val)]),2)
                            if DEBUG:
                                print("val: {}".format(yuvZZ[stage,zzIndex]))
                    zzIndex += 1

                if(zzIndex == 64):
                    zzIndex = 0
                    stage += 1
                    if(stage == 3):
                        stage = 0
                        #print("blockNum: {}".format(blockNum))
                        #print("left binaries : {}".format(len(binarys)))
                        YZCode[blockNum] = yuvZZ[0]
                        UZCode[blockNum] = yuvZZ[1]
                        VZCode[blockNum] = yuvZZ[2]

                        # handle dDC
                        if(blockNum > 0):
                            YZCode[blockNum, 0] += YZCode[blockNum - 1, 0]
                            UZCode[blockNum, 0] += UZCode[blockNum - 1, 0]
                            VZCode[blockNum, 0] += VZCode[blockNum - 1, 0]

                        blockNum += 1
                        if (blockNum == blockSum):
                            if (len(binarys) <= 8):
                                if DEBUG:
                                    print("YUV of full image loaded")
                                break
                            else:
                                print("something is wrong. {} left".format(len(binarys)))
            else:
                i += 1

    # result :
    # YZCode (shape: [blockSum, 64])
    # UZCode (shape: [blockSum, 64])
    # VZCode (shape: [blockSum, 64])
    # in zig zag order.

    t3 = time.time()
    print("Main processing : {}".format(t3 - t2))

    reverseZZ = []
    for i in range(zigzagOrder.shape[0]):
        reverseZZ.append(np.where(zigzagOrder == i)[0][0])

    YCode = YZCode[:,reverseZZ]
    UCode = UZCode[:,reverseZZ]
    VCode = VZCode[:,reverseZZ]

    YQuantMatrix = YCode.reshape([blockSum,8,8])
    UQuantMatrix = UCode.reshape([blockSum,8,8])
    VQuantMatrix = VCode.reshape([blockSum,8,8])

    YDCTMatrix = np.zeros([blockSum,8,8], dtype=int)
    UDCTMatrix = np.zeros([blockSum,8,8], dtype=int)
    VDCTMatrix = np.zeros([blockSum,8,8], dtype=int)

    blockNum = 0
    for y in range(0, height, 8):
        for x in range(0, width, 8):
            if(prevDetections):
                if(isAround(bb,[x,y])):
                    YDCTMatrix[blockNum] = YQuantMatrix[blockNum] * luminanceQuantTbl
                    UDCTMatrix[blockNum] = UQuantMatrix[blockNum] * luminanceQuantTbl
                    VDCTMatrix[blockNum] = VQuantMatrix[blockNum] * luminanceQuantTbl
                else:
                    YDCTMatrix[blockNum] = YQuantMatrix[blockNum] * luminanceQuantTbl_lo
                    UDCTMatrix[blockNum] = UQuantMatrix[blockNum] * luminanceQuantTbl_lo
                    VDCTMatrix[blockNum] = VQuantMatrix[blockNum] * luminanceQuantTbl_lo
            else:
                YDCTMatrix[blockNum] = YQuantMatrix[blockNum] * luminanceQuantTbl
                UDCTMatrix[blockNum] = UQuantMatrix[blockNum] * luminanceQuantTbl
                VDCTMatrix[blockNum] = VQuantMatrix[blockNum] * luminanceQuantTbl

            blockNum += 1

    YImageMatrix = np.zeros([height, width], dtype=int)
    UImageMatrix = np.zeros([height, width], dtype=int)
    VImageMatrix = np.zeros([height, width], dtype=int)

    blockNum = 0

    for y in range(0, height, 8):
        for x in range(0, width, 8):
            YImageMatrix[y:y+8, x:x+8] = np.rint(fftpack.idct(fftpack.idct(YDCTMatrix[blockNum], axis=0, norm='ortho'), axis=1, norm='ortho'))
            UImageMatrix[y:y+8, x:x+8] = np.rint(fftpack.idct(fftpack.idct(UDCTMatrix[blockNum], axis=0, norm='ortho'), axis=1, norm='ortho'))
            VImageMatrix[y:y+8, x:x+8] = np.rint(fftpack.idct(fftpack.idct(VDCTMatrix[blockNum], axis=0, norm='ortho'), axis=1, norm='ortho'))
            blockNum += 1

    YImageMatrix = YImageMatrix + 127
    UImageMatrix = UImageMatrix + 127
    VImageMatrix = VImageMatrix + 127

    for y in range(height):
        for x in range(width):
            YImageMatrix[y,x] = max(YImageMatrix[y,x], 0)
            UImageMatrix[y,x] = max(UImageMatrix[y,x], 0)
            VImageMatrix[y,x] = max(VImageMatrix[y,x], 0)

            YImageMatrix[y,x] = min(YImageMatrix[y,x], 255)
            UImageMatrix[y,x] = min(UImageMatrix[y,x], 255)
            VImageMatrix[y,x] = min(VImageMatrix[y,x], 255)

    YImageMatrix = YImageMatrix.astype(uint8)
    UImageMatrix = UImageMatrix.astype(uint8)
    VImageMatrix = VImageMatrix.astype(uint8)

    YImage = Image.fromarray(YImageMatrix)
    UImage = Image.fromarray(UImageMatrix)
    VImage = Image.fromarray(VImageMatrix)

    decodedImage = Image.merge('YCbCr', (YImage, UImage, VImage))

    decodedImage.save(outFileName)

    t4 = time.time()
    print("After jobs : {}".format(t4 - t3))

    print("Done.")

if __name__ == '__main__':
    main()

reason: import numpy,from numpy,from scipy


text:
# Licensed under an MIT style license -- see LICENSE.md

import numpy as np

from pesummary.utils.utils import logger, iterator
from pesummary.utils.decorators import array_input
from .spins import chi_p

__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"]

try:
    import lalsimulation
    from lalsimulation import (
        FLAG_SEOBNRv4P_HAMILTONIAN_DERIVATIVE_NUMERICAL,
        FLAG_SEOBNRv4P_EULEREXT_QNM_SIMPLE_PRECESSION,
        FLAG_SEOBNRv4P_ZFRAME_L
    )
    from lal import MSUN_SI
except ImportError:
    pass

DEFAULT_SEOBFLAGS = {
    "SEOBNRv4P_SpinAlignedEOBversion": 4,
    "SEOBNRv4P_SymmetrizehPlminusm": 1,
    "SEOBNRv4P_HamiltonianDerivative": FLAG_SEOBNRv4P_HAMILTONIAN_DERIVATIVE_NUMERICAL,
    "SEOBNRv4P_euler_extension": FLAG_SEOBNRv4P_EULEREXT_QNM_SIMPLE_PRECESSION,
    "SEOBNRv4P_Zframe": FLAG_SEOBNRv4P_ZFRAME_L,
    "SEOBNRv4P_debug": 0
}

@array_input()
def final_mass_of_merger_from_NSBH(
    mass_1, mass_2, spin_1z, lambda_2, approximant="IMRPhenomNSBH"
):
    """Calculate the final mass resulting from an NSBH merger using NSBH
    waveform models given samples for mass_1, mass_2, spin_1z and lambda_2.
    mass_1 and mass_2 should be in solar mass units.
    """
    from .tidal import _check_NSBH_approximant
    return _check_NSBH_approximant(
        approximant, mass_1, mass_2, spin_1z, lambda_2
    )[4]

@array_input()
def final_spin_of_merger_from_NSBH(
    mass_1, mass_2, spin_1z, lambda_2, approximant="IMRPhenomNSBH"
):
    """Calculate the final spin resulting from an NSBH merger using NSBH
    waveform models given samples for mass_1, mass_2, spin_1z and lambda_2.
    mass_1 and mass_2 should be in solar mass units.
    """
    from .tidal import _check_NSBH_approximant
    return _check_NSBH_approximant(
        approximant, mass_1, mass_2, spin_1z, lambda_2
    )[5]

@array_input()
def _final_from_initial_NSBH(*args, **kwargs):
    """Calculate the final mass and final spin given the initial parameters
    of the binary using the approximant directly
    """
    return [
        final_mass_of_merger_from_NSBH(*args, **kwargs),
        final_spin_of_merger_from_NSBH(*args, **kwargs)
    ]

def _wrapper_return_final_mass_and_final_spin_from_waveform(args):
    """Wrapper function to calculate the remnant properties for a given waveform
    for a pool of workers

    Parameters
    ----------
    args: np.ndarray
        2 dimensional array giving arguments to pass to
        _return_final_mass_and_final_spin_from_waveform. The first argument
        in each sublist is the keyword and the second argument in each sublist
        is the item you wish to pass
    """
    kwargs = {arg[0]: arg[1] for arg in args}
    return _return_final_mass_and_final_spin_from_waveform(**kwargs)

def _return_final_mass_and_final_spin_from_waveform(
    mass_function=None, spin_function=None, mass_function_args=[],
    spin_function_args=[], mass_function_return_function=None,
    mass_function_return_index=None, spin_function_return_function=None,
    spin_function_return_index=None, mass_1_index=0, mass_2_index=1,
    nsamples=0, approximant=None, default_SEOBNRv4P_kwargs=False
):
    """Return the final mass and final spin given functions to use

    Parameters
    ----------
    mass_function: func
        function you wish to use to calculate the final mass
    spin_function: func
        function you wish to use to calculate the final spin
    mass_function_args: list
        list of arguments you wish to pass to mass_function
    spin_function_args: list
        list of arguments you wish to pass to spin_function
    mass_function_return_function: str, optional
        function used to extract the final mass from the quantity returned from
        mass_function. For example, if mass_function returns a list and the
        final_mass is a property of the 3 arg of this list,
        mass_function_return_function='[3].final_mass'
    mass_function_return_index: str, optional
        if mass_function returns a list of parameters,
        mass_function_return_index indicates the index of `final_mass` in the
        list
    spin_function_return_function: str, optional
        function used to extract the final spin from the quantity returned from
        spin_function. For example, if spin_function returns a list and the
        final_spin is a property of the 3 arg of this list,
        spin_function_return_function='[3].final_spin'
    spin_function_return_index: str, optional
        if spin_function returns a list of parameters,
        spin_function_return_index indicates the index of `final_spin` in the
        list
    mass_1_index: int, optional
        the index of mass_1 in mass_function_args. Default is 0
    mass_2_index: int, optional
        the index of mass_2 in mass_function_args. Default is 1
    nsamples: int, optional
        the total number of samples
    approximant: str, optional
        the approximant used
    default_SEOBNRv4P_kwargs: Bool, optional
        if True, use the default SEOBNRv4P flags
    """
    if default_SEOBNRv4P_kwargs:
        mode_array, seob_flags = _setup_SEOBNRv4P_args()
        mass_function_args += [mode_array, seob_flags]
        spin_function_args += [mode_array, seob_flags]
    fm = mass_function(*mass_function_args)
    if mass_function_return_function is not None:
        fm = eval("fm{}".format(mass_function_return_function))
    elif mass_function_return_index is not None:
        fm = fm[mass_function_return_index]
    fs = spin_function(*spin_function_args)
    if spin_function_return_function is not None:
        fs = eval("fs{}".format(spin_function_return_function))
    elif spin_function_return_index is not None:
        fs = fs[spin_function_return_index]
    final_mass = fm * (
        mass_function_args[mass_1_index] + mass_function_args[mass_2_index]
    ) / MSUN_SI
    final_spin = fs
    return final_mass, final_spin

def _setup_SEOBNRv4P_args(mode=[2, 2], seob_flags=DEFAULT_SEOBFLAGS):
    """Setup the SEOBNRv4P[HM] kwargs
    """
    from lalsimulation import (
        SimInspiralCreateModeArray, SimInspiralModeArrayActivateMode
    )
    from lal import DictInsertINT4Value, CreateDict

    mode_array = SimInspiralCreateModeArray()
    SimInspiralModeArrayActivateMode(mode_array, mode[0], mode[1])
    _seob_flags = CreateDict()
    for key, item in seob_flags.items():
        DictInsertINT4Value(_seob_flags, key, item)
    return mode_array, _seob_flags

@array_input()
def _final_from_initial_BBH(
    mass_1, mass_2, spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z,
    approximant="SEOBNRv4", iota=None, luminosity_distance=None, f_ref=None,
    phi_ref=None, mode=[2, 2], delta_t=1. / 4096, seob_flags=DEFAULT_SEOBFLAGS,
    return_fits_used=False, multi_process=None
):
    """Calculate the final mass and final spin given the initial parameters
    of the binary using the approximant directly

    Parameters
    ----------
    mass_1: float/np.ndarray
        primary mass of the binary
    mass_2: float/np.ndarray
        secondary mass of the binary
    spin_1x: float/np.ndarray
        x component of the primary spin
    spin_1y: float/np.ndarray
        y component of the primary spin
    spin_1z: float/np.ndarray
        z component of the primary spin
    spin_2x: float/np.ndarray
        x component of the secondary spin
    spin_2y: float/np.ndarray
        y component of the secondary spin
    spin_2z: float/np.ndarray
        z component of the seconday spin
    approximant: str
        name of the approximant you wish to use for the remnant fits
    iota: float/np.ndarray, optional
        the angle between the total orbital angular momentum and the line of
        sight of the source. Used when calculating the remnant fits for
        SEOBNRv4PHM. Since we only need the EOB dynamics here it does not matter
        what we pass
    luminosity_distance: float/np.ndarray, optional
        the luminosity distance of the source. Used when calculating the
        remnant fits for SEOBNRv4PHM. Since we only need the EOB dynamics here
        it does not matter what we pass.
    f_ref: float/np.ndarray, optional
        the reference frequency at which the spins are defined
    phi_ref: float/np.ndarray, optional
        the coalescence phase of the binary
    mode: list, optional
        specific mode to use when calculating the remnant fits for SEOBNRv4PHM.
        Since we only need the EOB dynamics here it does not matter what we
        pass.
    delta_t: float, optional
        the sampling rate used in the analysis, Used when calculating the
        remnant fits for SEOBNRv4PHM
    seob_flags: dict, optional
        dictionary containing the SEOB flags. Used when calculating the remnant
        fits for SEOBNRv4PHM
    return_fits_used: Bool, optional
        if True, return the approximant that was used.
    multi_process: int, optional
        the number of cores to use when calculating the remnant fits
    """
    from lalsimulation import (
        SimIMREOBFinalMassSpin, SimIMREOBFinalMassSpinPrec,
        SimInspiralGetSpinSupportFromApproximant,
        SimIMRSpinPrecEOBWaveformAll, SimPhenomUtilsIMRPhenomDFinalMass,
        SimPhenomUtilsPhenomPv2FinalSpin
    )
    import multiprocessing

    def convert_args_for_multi_processing(kwargs):
        args = []
        for n in range(kwargs["nsamples"]):
            _args = []
            for key, item in kwargs.items():
                if key == "mass_function_args" or key == "spin_function_args":
                    _args.append([key, [arg[n] for arg in item]])
                else:
                    _args.append([key, item])
            args.append(_args)
        return args

    try:
        approx = getattr(lalsimulation, approximant)
    except AttributeError:
        raise ValueError(
            "The waveform '{}' is not supported by lalsimulation"
        )

    m1 = mass_1 * MSUN_SI
    m2 = mass_2 * MSUN_SI
    kwargs = {"nsamples": len(mass_1), "approximant": approximant}
    if approximant.lower() in ["seobnrv4p", "seobnrv4phm"]:
        if any(i is None for i in [iota, luminosity_distance, f_ref, phi_ref]):
            raise ValueError(
                "The approximant '{}' requires samples for iota, f_ref, "
                "phi_ref and luminosity_distance. Please pass these "
                "samples.".format(approximant)
            )
        if len(delta_t) == 1:
            delta_t = [delta_t[0]] * len(mass_1)
        elif len(delta_t) != len(mass_1):
            raise ValueError(
                "Please provide either a single 'delta_t' that is is used for "
                "all samples, or a single 'delta_t' for each sample"
            )
        mode_array, _seob_flags = _setup_SEOBNRv4P_args(
            mode=mode, seob_flags=seob_flags
        )
        args = np.array([
            phi_ref, delta_t, m1, m2, f_ref, luminosity_distance, iota,
            spin_1x, spin_1y, spin_1z, spin_2x, spin_2y, spin_2z,
            [mode_array] * len(mass_1), [_seob_flags] * len(mass_1)
        ])
        kwargs.update(
            {
                "mass_function": SimIMRSpinPrecEOBWaveformAll,
                "spin_function": SimIMRSpinPrecEOBWaveformAll,
                "mass_function_args": args,
                "spin_function_args": args,
                "mass_function_return_function": "[21].data[6]",
                "spin_function_return_function": "[21].data[7]",
                "mass_1_index": 2,
                "mass_2_index": 3,
            }
        )
    elif approximant.lower() in ["seobnrv4"]:
        spin1 = np.array([spin_1x, spin_1y, spin_1z]).T
        spin2 = np.array([spin_2x, spin_2y, spin_2z]).T
        app = np.array([approx] * len(mass_1))
        kwargs.update(
            {
                "mass_function": SimIMREOBFinalMassSpin,
                "spin_function": SimIMREOBFinalMassSpin,
                "mass_function_args": [m1, m2, spin1, spin2, app],
                "spin_function_args": [m1, m2, spin1, spin2, app],
                "mass_function_return_index": 1,
                "spin_function_return_index": 2
            }
        )
    elif "phenompv3" in approximant.lower():
        kwargs.update(
            {
                "mass_function": SimPhenomUtilsIMRPhenomDFinalMass,
                "spin_function": SimPhenomUtilsPhenomPv2FinalSpin,
                "mass_function_args": [m1, m2, spin_1z, spin_2z],
                "spin_function_args": [m1, m2, spin_1z, spin_2z]
            }
        )
        if SimInspiralGetSpinSupportFromApproximant(approx) > 2:
            # matches the waveform's internal usage as corrected in
            # https://git.ligo.org/lscsoft/lalsuite/-/merge_requests/1270
            _chi_p = chi_p(mass_1, mass_2, spin_1x, spin_1y, spin_2x, spin_2y)
            kwargs["spin_function_args"].append(_chi_p)
        else:
            kwargs["spin_function_args"].append(np.zeros_like(mass_1))
    else:
        raise ValueError(
            "The waveform '{}' is not support by this function.".format(
                approximant
            )
        )

    args = convert_args_for_multi_processing(kwargs)
    if multi_process is not None and multi_process[0] != 1:
        _multi_process = multi_process[0]
        if approximant.lower() in ["seobnrv4p", "seobnrv4phm"]:
            logger.warning(
                "Ignoring passed 'mode' and 'seob_flags' options. Defaults "
                "must be used with multiprocessing. If you wish to use custom "
                "options, please set `multi_process=None`"
            )
            _kwargs = kwargs.copy()
            _kwargs["mass_function_args"] = kwargs["mass_function_args"][:-2]
            _kwargs["spin_function_args"] = kwargs["spin_function_args"][:-2]
            _kwargs["default_SEOBNRv4P_kwargs"] = True
            args = convert_args_for_multi_processing(_kwargs)
        with multiprocessing.Pool(_multi_process) as pool:
            data = np.array(list(
                iterator(
                    pool.imap(
                        _wrapper_return_final_mass_and_final_spin_from_waveform,
                        args
                    ), tqdm=True, desc="Evaluating {} fit".format(approximant),
                    logger=logger, total=len(mass_1)
                )
            )).T
    else:
        final_mass, final_spin = [], []
        _iterator = iterator(
            range(kwargs["nsamples"]), tqdm=True, total=len(mass_1),
            desc="Evaluating {} fit".format(approximant), logger=logger
        )
        for i in _iterator:
            data = _wrapper_return_final_mass_and_final_spin_from_waveform(
                args[i]
            )
            final_mass.append(data[0])
            final_spin.append(data[1])
        data = [final_mass, final_spin]
    if return_fits_used:
        return data, [approximant]
    return data

def final_remnant_properties_from_NRSurrogate(
    *args, f_low=20., f_ref=20., model="NRSur7dq4Remnant", return_fits_used=False,
    properties=["final_mass", "final_spin", "final_kick"], approximant="SEOBNRv4PHM"
):
    """Return the properties of the final remnant resulting from a BBH merger using
    NRSurrogate fits

    Parameters
    ---------
    f_low: float/np.ndarray
        The low frequency cut-off used in the analysis. Default is 20Hz
    f_ref: float/np.ndarray
        The reference frequency used in the analysis. Default is 20Hz
    model: str, optional
        The name of the NRSurrogate model you wish to use
    return_fits_used: Bool, optional
        if True, return the approximant that was used.
    properties: list, optional
        The list of properties you wish to calculate
    approximant: str, optional
        The approximant that was used to generate the posterior samples
    """
    from .nrutils import NRSur_fit

    fit = NRSur_fit(
        *args, f_low=f_low, f_ref=f_ref, model=model, fits=properties,
        approximant=approximant
    )
    if return_fits_used:
        return fit, [model]
    return fit

def final_mass_of_merger_from_NR(
    *args, NRfit="average", final_spin=None, return_fits_used=False
):
    """Return the final mass resulting from a BBH merger using NR fits

    Parameters
    ----------
    NRfit: str
        Name of the fit you wish to use. If you wish to use a precessing fit
        please use the syntax 'precessing_{}'.format(fit_name). If you wish
        to have an average NR fit, then pass 'average'
    final_spin: float/np.ndarray, optional
        precomputed final spin of the remnant.
    return_fits_used: Bool, optional
        if True, return the fits that were used. Only used when NRfit='average'
    """
    from pesummary.gw.conversions import nrutils

    if NRfit.lower() == "average":
        func = getattr(nrutils, "bbh_final_mass_average")
    elif "panetal" in NRfit.lower():
        func = getattr(
            nrutils, "bbh_final_mass_non_spinning_Panetal"
        )
    else:
        func = getattr(
            nrutils, "bbh_final_mass_non_precessing_{}".format(NRfit)
        )
    if "healy" in NRfit.lower():
        return func(*args, final_spin=final_spin)
    if NRfit.lower() == "average":
        return func(*args, return_fits_used=return_fits_used)
    return func(*args)

def final_mass_of_merger_from_NRSurrogate(
    *args, model="NRSur7dq4Remnant", return_fits_used=False, approximant="SEOBNRv4PHM"
):
    """Return the final mass resulting from a BBH merger using NRSurrogate
    fits
    """
    data = final_remnant_properties_from_NRSurrogate(
        *args, model=model, properties=["final_mass"],
        return_fits_used=return_fits_used,
        approximant=approximant
    )
    if return_fits_used:
        return data[0]["final_mass"], data[1]
    return data["final_mass"]

def final_mass_of_merger_from_waveform(*args, NSBH=False, **kwargs):
    """Return the final mass resulting from a BBH/NSBH merger using a given
    approximant

    Parameters
    ----------
    NSBH: Bool, optional
        if True, use NSBH waveform fits. Default False
    """
    if NSBH or "nsbh" in kwargs.get("approximant", "").lower():
        return _final_from_initial_NSBH(*args, **kwargs)[1]
    return _final_from_initial_BBH(*args, **kwargs)[0]

def final_spin_of_merger_from_NR(
    *args, NRfit="average", return_fits_used=False
):
    """Return the final spin resulting from a BBH merger using NR fits

    Parameters
    ----------
    NRfit: str
        Name of the fit you wish to use. If you wish to use a precessing fit
        please use the syntax 'precessing_{}'.format(fit_name). If you wish
        to have an average NR fit, then pass 'average'
    return_fits_used: Bool, optional
        if True, return the fits that were used. Only used when NRfit='average'
    """
    from pesummary.gw.conversions import nrutils

    if NRfit.lower() == "average":
        func = getattr(nrutils, "bbh_final_spin_average_precessing")
    elif "pan" in NRfit.lower():
        func = getattr(
            nrutils, "bbh_final_spin_non_spinning_Panetal"
        )
    elif "precessing" in NRfit.lower():
        func = getattr(
            nrutils, "bbh_final_spin_precessing_{}".format(
                NRfit.split("precessing_")[1]
            )
        )
    else:
        func = getattr(
            nrutils, "bbh_final_spin_non_precessing_{}".format(NRfit)
        )
    if NRfit.lower() == "average":
        return func(*args, return_fits_used=return_fits_used)
    return func(*args)

def final_spin_of_merger_from_NRSurrogate(
    *args, model="NRSur7dq4Remnant", return_fits_used=False, approximant="SEOBNRv4PHM"
):
    """Return the final spin resulting from a BBH merger using NRSurrogate
    fits
    """
    data = final_remnant_properties_from_NRSurrogate(
        *args, model=model, properties=["final_spin"],
        return_fits_used=return_fits_used, approximant=approximant
    )
    if return_fits_used:
        return data[0]["final_spin"], data[1]
    return data["final_spin"]

def final_spin_of_merger_from_waveform(*args, NSBH=False, **kwargs):
    """Return the final spin resulting from a BBH/NSBH merger using a given
    approximant.

    Parameters
    ----------
    NSBH: Bool, optional
        if True, use NSBH waveform fits. Default False
    """
    if NSBH or "nsbh" in kwargs.get("approximant", "").lower():
        return _final_from_initial_NSBH(*args, **kwargs)[1]
    return _final_from_initial_BBH(*args, **kwargs)[1]

def final_kick_of_merger_from_NRSurrogate(
    *args, model="NRSur7dq4Remnant", return_fits_used=False, approximant="SEOBNRv4PHM"
):
    """Return the final kick of the remnant resulting from a BBH merger
    using NRSurrogate fits
    """
    data = final_remnant_properties_from_NRSurrogate(
        *args, model=model, properties=["final_kick"],
        return_fits_used=return_fits_used, approximant=approximant
    )
    if return_fits_used:
        return data[0]["final_kick"], data[1]
    return data["final_kick"]

def final_mass_of_merger(
    *args, method="NR", approximant="SEOBNRv4", NRfit="average",
    final_spin=None, return_fits_used=False, model="NRSur7dq4Remnant"
):
    """Return the final mass resulting from a BBH merger

    Parameters
    ----------
    mass_1: float/np.ndarray
        float/array of masses for the primary object
    mass_2: float/np.ndarray
        float/array of masses for the secondary object
    spin_1z: float/np.ndarray
        float/array of primary spin aligned with the orbital angular momentum
    spin_2z: float/np.ndarray
        float/array of secondary spin aligned with the orbital angular momentum
    method: str
        The method you wish to use to calculate the final mass of merger. Either
        NR, NRSurrogate or waveform
    approximant: str
        Name of the approximant you wish to use if the chosen method is waveform
        or NRSurrogate
    NRFit: str
        Name of the NR fit you wish to use if chosen method is NR
    return_fits_used: Bool, optional
        if True, return the NR fits that were used. Only used when
        NRFit='average' or when method='NRSurrogate'
    model: str, optional
        The NRSurrogate model to use when evaluating the fits
    """
    if method.lower() == "nr":
        mass_func = final_mass_of_merger_from_NR
        kwargs = {
            "NRfit": NRfit, "final_spin": final_spin,
            "return_fits_used": return_fits_used
        }
    elif "nrsur" in method.lower():
        mass_func = final_mass_of_merger_from_NRSurrogate
        kwargs = {
            "approximant": approximant, "return_fits_used": return_fits_used,
            "model": model
        }
    else:
        mass_func = final_mass_of_merger_from_waveform
        kwargs = {"approximant": approximant}

    return mass_func(*args, **kwargs)

def final_spin_of_merger(
    *args, method="NR", approximant="SEOBNRv4", NRfit="average",
    return_fits_used=False, model="NRSur7dq4Remnant"
):
    """Return the final mass resulting from a BBH merger

    Parameters
    ----------
    mass_1: float/np.ndarray
        float/array of masses for the primary object
    mass_2: float/np.ndarray
        float/array of masses for the secondary object
    a_1: float/np.ndarray
        float/array of primary spin magnitudes
    a_2: float/np.ndarray
        float/array of secondary spin magnitudes
    tilt_1: float/np.ndarray
        float/array of primary spin tilt angle from the orbital angular momentum
    tilt_2: float/np.ndarray
        float/array of secondary spin tilt angle from the orbital angular
        momentum
    phi_12: float/np.ndarray
        float/array of samples for the angle between the in-plane spin
        components
    method: str
        The method you wish to use to calculate the final mass of merger. Either
        NR, NRSurrogate or waveform
    approximant: str
        Name of the approximant you wish to use if the chosen method is waveform
        or NRSurrogate
    NRFit: str
        Name of the NR fit you wish to use if chosen method is NR
    return_fits_used: Bool, optional
        if True, return the NR fits that were used. Only used when
        NRFit='average' or when method='NRSurrogate'
    model: str, optional
        The NRSurrogate model to use when evaluating the fits
    """
    if method.lower() == "nr":
        spin_func = final_spin_of_merger_from_NR
        kwargs = {"NRfit": NRfit, "return_fits_used": return_fits_used}
    elif "nrsur" in method.lower():
        spin_func = final_spin_of_merger_from_NRSurrogate
        kwargs = {
            "approximant": approximant, "return_fits_used": return_fits_used,
            "model": model
        }
    else:
        spin_func = final_spin_of_merger_from_waveform
        kwargs = {"approximant": approximant}

    return spin_func(*args, **kwargs)

def final_kick_of_merger(
    *args, method="NR", approximant="SEOBNRv4", NRfit="average",
    return_fits_used: False, model="NRSur7dq4Remnant"
):
    """Return the final kick velocity of the remnant resulting from a BBH merger

    Parameters
    ----------
    mass_1: float/np.ndarray
        float/array of masses for the primary object
    mass_2: float/np.ndarray
        float/array of masses for the secondary object
    a_1: float/np.ndarray
        float/array of primary spin magnitudes
    a_2: float/np.ndarray
        float/array of secondary spin magnitudes
    tilt_1: float/np.ndarray
        float/array of primary spin tilt angle from the orbital angular momentum
    tilt_2: float/np.ndarray
        float/array of secondary spin tilt angle from the orbital angular
        momentum
    phi_12: float/np.ndarray
        float/array of samples for the angle between the in-plane spin
        components
    method: str
        The method you wish to use to calculate the final kick of merger. Either
        NR, NRSurrogate or waveform
    approximant: str
        Name of the approximant you wish to use if the chosen method is waveform
        or NRSurrogate
    NRFit: str
        Name of the NR fit you wish to use if chosen method is NR
    return_fits_used: Bool, optional
        if True, return the NR fits that were used. Only used when
        NRFit='average' or when method='NRSurrogate'
    model: str, optional
        The NRSurrogate model to use when evaluating the fits
    """
    if "nrsur" not in method.lower():
        raise NotImplementedError(
            "Currently you can only work out the final kick velocity using "
            "NRSurrogate fits."
        )
    velocity_func = final_kick_of_merger_from_NRSurrogate
    kwargs = {
        "approximant": approximant, "return_fits_used": return_fits_used,
        "model": model
    }
    return velocity_func(*args, **kwargs)

def peak_luminosity_of_merger(*args, NRfit="average", return_fits_used=False):
    """Return the peak luminosity of an aligned-spin BBH using NR fits

    Parameters
    ----------
    mass_1: float/np.ndarray
        float/array of masses for the primary object
    mass_2: float/np.ndarray
        float/array of masses for the secondary object
    spin_1z: float/np.ndarray
        float/array of primary spin aligned with the orbital angular momentum
    spin_2z: float/np.ndarray
        float/array of secondary spin aligned with the orbital angular momentum
    NRFit: str
        Name of the NR fit you wish to use if chosen method is NR
    return_fits_used: Bool, optional
        if True, return the NR fits that were used. Only used when
        NRFit='average'
    """
    from pesummary.gw.conversions import nrutils

    if NRfit.lower() == "average":
        func = getattr(nrutils, "bbh_peak_luminosity_average")
    else:
        func = getattr(
            nrutils, "bbh_peak_luminosity_non_precessing_{}".format(NRfit)
        )
    if NRfit.lower() == "average":
        return func(*args, return_fits_used=return_fits_used)
    return func(*args)

max_stars_repo_name: pesummary/pesummary

max_issues_repo_name: pesummary/pesummary

max_forks_repo_name: pesummary/pesummary


text:
import numpy as np
import pyturb.utils.constants as cts
import pyturb.utils.units as units
from pyturb.gas_models.perfect_ideal_gas import PerfectIdealGas
from pyturb.gas_models.thermo_properties import ThermoProperties
print('tests_1')
print('-----------------')
#print(cts.Ru, units.m_to_nm)
#print()
#tp = ThermoProperties('O2')
#print(tp)

pig = PerfectIdealGas('Air')
print(pig.Ru)
print(pig.Rg)

max_issues_repo_name: sergiodobler/pyturb

max_forks_repo_name: sergiodobler/pyturb


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 15:37:49 2018

@author: Zhesi Shen(zhesi.shen@live.com)
"""

from tkinter import Menu
from tkinter import *
import tkinter as tk
from tkinter import messagebox
from tkinter.ttk import Progressbar
from tkinter.filedialog import askopenfilename,askdirectory
from tkinter import ttk
import tkinter.font as tkFont
import difflib
import json
import pandas as pd
import numpy as np
import time

window = Tk()
window.title("Welcome to Affiliation Normalization")
window.lift()
#window.geometry('1650x450')

##--------- New or Continue Project -------------------------------------------
def ask_new_or_continue():
    new_window = Toplevel(window)
    new_window.lift(aboveThis=window)
    new_window.title('New or Continue')
#    new_window.geometry('600x450')
    user_lb = Label(new_window, text='User:')
    user_lb.pack()
    user_ety = Entry(new_window, width=20)
    user_ety.pack()
    def select_files():
        dirname = askdirectory() # show an "Open" dialog box and return the path to the selected file
        paras = {'current idx':1,
         'standard table':'./data/standard.xlsx',
         'candidate table':dirname+'/candidate.xlsx',
         'to be cleaned':dirname+'/to_cleaned.xlsx'}
        with open('./data/parameters.json', 'w') as outfile:
            json.dump(paras, outfile)
        return

    def click_new_project():
        select_files()
        new_window.destroy()
        window.lift()

        load_data()
        show()
        return 0
    new_btn = Button(new_window, text='New Project', command=click_new_project)
    new_btn.pack()

    def click_continue_project():
        new_window.destroy()
        window.lift()

        load_data()
        show()
    continue_btn = Button(new_window, text='Continue Project', command=click_continue_project)
    continue_btn.pack()

    return 0

ask_new_or_continue()
##---------- load parameters --------------------------------------------------
Clean_idx = IntVar()
Txt2Clean = StringVar()
MAX = IntVar()
LastSave = DoubleVar() # store save time
df_cand = pd.DataFrame()
df_toClean = pd.DataFrame()
paras = {}
def load_data():
    global df_cand,df_toClean,paras
    with open('./data/parameters.json') as json_file:
        paras = json.load(json_file).copy()

    Clean_idx.set(max(1,paras['current idx']))
    LastSave.set(value=time.time())
#    df_stand = pd.read_excel(paras['standard table'])
    df_cand = pd.read_excel(paras['candidate table'])
    df_toClean = pd.read_excel(paras['to be cleaned'])
    MAX.set(df_toClean.shape[0])
    if 'uid' not in df_toClean.columns:
        df_toClean['uid']=np.nan
    if 'time' not in df_cand.columns:
        df_cand['time'] = np.nan
    if 'based on' not in df_cand.columns:
        df_cand['based on'] = np.nan
    bar['maximum'] = MAX.get()
    bar['value'] = Clean_idx.get()
    bar.update()

##----------- menu bar --------------------------------------------------------

menu = Menu(window)

file_item = Menu(menu, tearoff=0)
file_item.add_command(label='New')
file_item.add_command(label='Open')
file_item.add_command(label='Edit')
menu.add_cascade(label='File', menu=file_item)

def click_help_about():
    messagebox.showinfo('About', 'Affiliation Normalization\n\nZhesi Shen(zhesi.shen@live.com)')

help_item = Menu(menu, tearoff=0)
help_item.add_command(label='Tutorial')
help_item.add_command(label='About', command=click_help_about)
menu.add_cascade(label='Help', menu=help_item)
window.config(menu=menu)

##----------- tab -------------------------------------------------------------
nb = ttk.Notebook(window)
tab_multi = ttk.Frame(nb)
tab_single = ttk.Frame(nb)
nb.add(tab_multi, text='在线')
nb.add(tab_single, text='本地')
nb.grid()

############### on tab_multi ###################################################
##----------- show compared affiliations --------------------------------------

lbl_curr = Label(tab_multi, text="Current item: ")
lbl_curr.grid(column=0, row=0,sticky=tk.E)
lbl_cand = Label(tab_multi, text="Candidate item: ")
lbl_cand.grid(column=0, row=1,sticky=tk.E)

txt_curr = Text(tab_multi,font="Macro 12", width=120, height=1)
txt_curr.grid(column=1, row=0,columnspan=8,padx=2, pady=2,stick=tk.E)
lb_cand = Listbox(tab_multi, font="Macro 12", width=120, height=15)
lb_cand.grid(column=1, row=1,columnspan=8,padx=2, pady=2,stick=tk.E)

lbl_sim_threshold = Label(tab_multi, text=f'Similarity Threshold:')
lbl_sim_threshold.grid(column=1,row=2,sticky='e')

SimThreshold = DoubleVar(value=0.5)
txt_sim_threshold = Entry(tab_multi, textvariable=SimThreshold, width=8)
txt_sim_threshold.grid(column=2, row=2,sticky='w')

def reshow(event):
    show()
txt_sim_threshold.bind('<Return>', reshow)

def show_list_of_candidates(txt_cand,df_cand,text,threshold):
    lb_cand.delete(0,END)
    cands = []
    for i in range(df_cand.shape[0]):
        aff_cand = df_cand.iloc[i]['英文+省']
        unique_id = df_cand.iloc[i]['所级机构名_直译名']
        seqm = difflib.SequenceMatcher(None, text.lower(), aff_cand.lower())
        sim = seqm.ratio()
        if sim > threshold:
            cands.append((sim,aff_cand,unique_id,i))
    cands = sorted(cands,key=lambda e:-e[0])
    for i in range(len(cands)):
        sim,aff_cand,unique_id,od = cands[i]
#        row_format ="{:<.2f}  {:<90}  {:<},{}"
#        item = [sim,aff_cand,unique_id,od]
#        txt = row_format.format(*item)
        txt = f"{sim:.2f}     {aff_cand:<60}{unique_id},{od}"
#        print(txt)
        lb_cand.insert(END,txt)

def show_tobe_cleaned(text):
    txt_curr.delete('1.0', END)
    txt_curr.insert('1.0','         '+text)

def show():
    text1 = df_toClean.loc[Clean_idx.get()-1]['值-ins2_省份']
    Txt2Clean.set(text1)
    show_tobe_cleaned(Txt2Clean.get())
    show_list_of_candidates(lb_cand,df_cand,Txt2Clean.get(),SimThreshold.get())
#show_diff(text1,text2,txt_curr,txt_cand,txt_sim)
##--------- click botton ------------------------------------------------------

def autosave():
    if time.time() - LastSave.get() > 600:
        paras['current idx'] = bar['value']
        with open('./tmp/parameters.json', 'w') as outfile:
            json.dump(paras, outfile)

#        writer = pd.ExcelWriter('./tmp/standard.xlsx')
#        df_stand.to_excel(writer,'Sheet1')
#        writer.save()

        writer = pd.ExcelWriter('./tmp/candidate.xlsx')
        df_cand.to_excel(writer,'Sheet1')
        writer.save()

        writer = pd.ExcelWriter('./tmp/to_cleaned.xlsx')
        df_toClean.to_excel(writer,'Sheet1')
        writer.save()
        LastSave.set(time.time())
        print('Auto Saved')
    print('---'*20)

def click_previous():
    if 1 < Clean_idx.get():
        Clean_idx.set(Clean_idx.get()-1)
        bar["value"] = Clean_idx.get()
        bar.update()  #this works
        autosave()

        text1 = df_toClean.loc[Clean_idx.get()-1]['值-ins2_省份']
        Txt2Clean.set(text1)
        show_tobe_cleaned(Txt2Clean.get())
        show_list_of_candidates(lb_cand,df_cand,Txt2Clean.get(),SimThreshold.get())
    else:
        messagebox.showwarning('Warning', '没有上一个')  #shows warning message

def click_next():
    if Clean_idx.get() < MAX.get():
        Clean_idx.set(Clean_idx.get()+1)
        bar["value"] = Clean_idx.get()
        bar.update()  #this works
        autosave()

        text1 = df_toClean.loc[Clean_idx.get()-1]['值-ins2_省份']
        Txt2Clean.set(text1)
        show_tobe_cleaned(Txt2Clean.get())
        show_list_of_candidates(lb_cand,df_cand,Txt2Clean.get(),SimThreshold.get())
    else:
        messagebox.showwarning('Warning', '没有下一个')  #shows warning message

def append_to_cands(cands,cleans,row,uid):
    new_row = cands.shape[0]
    print(Txt2Clean.get())
    print(cands.loc[row])
    cands.loc[new_row] = cands.loc[row]
    cands.loc[new_row,'英文+省'] = Txt2Clean.get()
    cands.loc[new_row,'机构英文写法'] = Txt2Clean.get().split('_')[0]
    cands.loc[new_row,'省（直译名机构所在省市）'] = Txt2Clean.get().split('_')[-1]
    if cands.loc[new_row,'省（直译名机构所在省市）'] != cands.loc[row,'省（直译名机构所在省市）']:
        cands.loc[new_row,'市'] = np.nan

    cands.loc[new_row,'time'] = time.strftime("%Y/%m/%d")
    cands.loc[new_row,'based on'] = row
    cleans.loc[Clean_idx.get()-1,'uid'] = uid

def click_select():
    cand = lb_cand.get(ACTIVE)
    cand1 = cand.split(' ')
    sim = float(cand1[0])

    cand1 = cand1[-1].split(',')
    uid = cand1[0]
    row = cand1[-1]

    if sim<0.5:
        res = messagebox.askyesno('Save & Close','Do you really want to This One?')
        if res==False:
            print('Please Select Again')
            return

    print(cand)
    append_to_cands(df_cand,df_toClean,int(row),uid)
    click_next()
#    if Clean_idx.get()<MAX:
#        Clean_idx.set(Clean_idx.get()+1)
#        bar["value"] = Clean_idx.get()
#        bar.update()  #this works
#        autosave()
#
#        text1 = df_toClean.loc[Clean_idx.get()-1]['值-ins2_省份']
#        Txt2Clean.set(text1)
#        show_tobe_cleaned(Txt2Clean.get())
#        show_list_of_candidates(lb_cand,df_cand,Txt2Clean.get(),SimThreshold.get())
#
#    else:
#        messagebox.showwarning('Congratulations', 'All finished')  #shows warning message

    return 0

def click_new():
    new_window = Toplevel(window)
    new_window.title('Inser New Entity')
    new_window.geometry('600x450')
    row = 0
    lbs = {}
    ets = {}
    for col in df_cand.columns:
        if col in ['所级机构名_直译名']:
            lbs[col] = Label(new_window, text=f"{col}*:")
        else:
            lbs[col] = Label(new_window, text=f"{col}:")
        lbs[col].grid(column=0, row=row,sticky=tk.E)

        ets[col] = Entry(new_window, width=50)
        ets[col].grid(column=1, row=row)

        row += 1

    ets['英文+省'].insert(0,Txt2Clean.get())
    ets['time'].insert(0,time.strftime("%Y/%m/%d"))
    ets['机构英文写法'].insert(0,Txt2Clean.get().split('_')[0])
    ets['省（直译名机构所在省市）'].insert(0,Txt2Clean.get().split('_')[-1])

    def click_comfirm_insert():
        res = messagebox.askyesnocancel('Save & Close','确定插入本条？')
        if res==True:
            new_row = df_cand.shape[0]
            print(Txt2Clean.get())
            df_cand.loc[new_row] = [ets[col].get() for col in df_cand.columns]
            df_toClean.loc[Clean_idx.get()-1,'uid'] = ets['所级机构名_直译名'].get()

            new_window.destroy()

            click_next()

        elif res==False:
            new_window.destroy()
        else:
            new_window.lift()
    bt = Button(new_window, text="Save", command=click_comfirm_insert)
    bt.grid(column=1, row=row)

    autosave()
    return 0

btn_previous = Button(tab_multi, text="Previous", command=click_previous)
btn_previous.grid(column=5, row=2,pady=2,padx=6,sticky='we')

btn_next = Button(tab_multi, text="Next", command=click_next)
btn_next.grid(column=6, row=2,pady=2,padx=6,sticky='we')

btn_select = Button(tab_multi, text="Select", command=click_select)
btn_select.grid(column=7, row=2,pady=2,padx=6,sticky='we')

btn_new = Button(tab_multi, text="New", command=click_new)
btn_new.grid(column=8, row=2,pady=2,padx=6,sticky='we')

##------------- progress bar --------------------------------------------------

lbl_probar = Label(tab_multi, text=f'Completed:')
lbl_probar.grid(column=1,row=5,sticky='e')

style = ttk.Style()
style.theme_use('clam')
style.configure("black.Horizontal.TProgressbar", background='black')
bar = Progressbar(tab_multi, maximum=MAX.get(), length=200, style='grey.Horizontal.TProgressbar')
bar['value'] = Clean_idx.get()
bar.grid(column=2, row=5,columnspan=2,sticky='w')

## -------------- Save result -------------------------------------------------

def click_save():
    paras['current idx'] = Clean_idx.get()
    with open('./data/parameters.json', 'w') as outfile:
        json.dump(paras, outfile)

#    writer = pd.ExcelWriter(paras['standard table'])
#    df_stand.to_excel(writer,'Sheet1')
#    writer.save()

    writer = pd.ExcelWriter(paras['candidate table'])
    df_cand.to_excel(writer,'Sheet1')
    writer.save()

    writer = pd.ExcelWriter(paras['to be cleaned'])
    df_toClean.to_excel(writer,'Sheet1')
    writer.save()

    print('Saved')
    return 0

def click_close():
    res = messagebox.askyesnocancel('Save & Close','Do you want to Save&Close?')
    if res==True:
        click_save()
        window.destroy()
    elif res==False:
        print('close')
        window.destroy()
    else:
        print('nothing')

btn_save = Button(tab_multi, text="Save", command=click_save)
btn_save.grid(column=3, row=6,padx=6,sticky='we')

btn_close = Button(tab_multi, text="Close", command=click_close)
btn_close.grid(column=4, row=6,padx=6,sticky='we')

############### on tab_single ###################################################
single_curr = Label(tab_single, text="Current item: ")
single_curr.grid(column=0, row=0,sticky=tk.E)
single_ety = Entry(tab_single, width=110)
single_ety.grid(column=1, row=0, columnspan=7,padx=6,sticky='we')

txt_single = Text(tab_single,font="Macro 12", width=110, height=20)
txt_single.grid(column=1, row=1, columnspan=7,pady=2,padx=6,sticky='wes')

def click_identify():
    text = single_ety.get()
    txt_single.delete(1.0,END)
    cands = []
    for i in range(df_cand.shape[0]):
        aff_cand = df_cand.iloc[i]['英文+省']
        seqm = difflib.SequenceMatcher(None, text.lower(), aff_cand.lower())
        sim = seqm.ratio()
        cands.append((sim,aff_cand,i))
    cands = sorted(cands,key=lambda e:-e[0])
    sim,aff_cand,od = cands[0]

    Length = max([len(col.encode('gbk')) for col in df_cand.columns])
    for col in df_cand.columns:
        txt_single.insert(END, ' '*(Length-len(col.encode('gbk'))) +col +':'+str(df_cand.loc[od,col])+'\n')

    return

btn_trans = Button(tab_single, text="识别", command=click_identify)
btn_trans.grid(column=8, row=0,pady=2,padx=6,sticky='we')

window.mainloop()