text:
import os
import shlex
import subprocess

import h5py
import numpy as np
import torch
import torch.utils.data as data

from lib.sparse_voxelization import SparseVoxelizer

# BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def _get_data_files(list_filename):
    with open(list_filename) as f:
        return [line.rstrip() for line in f]

def _load_data_file(name):
    f = h5py.File(name, "r")
    data = f["data"][:]
    label = f["label"][:]
    return data, label

class S3DIS(data.Dataset):
    def __init__(self, config, train=True, download=False, data_precent=1.0, whole_scene=True):
        super().__init__()
        if not whole_scene:
            self.data_precent = data_precent
            self.folder = "indoor3d_sem_seg_hdf5_data"
            self.data_path = "/data/eva_share_users/zhaotianchen/"
            self.data_dir = os.path.join(self.data_path, self.folder)
            self.url = (
                "https://shapenet.cs.stanford.edu/media/indoor3d_sem_seg_hdf5_data.zip"
            )
            self.train = train
            self.split = "train" if self.train else "val"

            self.NUM_IN_CHANNEL=9
            self.NUM_LABELS=13
            self.IGNORE_LABELS = [-1]  # labels that are not evaluated
            self.NEED_PRED_POSTPROCESSING = False

            if self.train:
                self.data_dict = torch.load(os.path.join(self.data_dir, "s3dis_train.pth"),'cpu')
            else:
                self.data_dict = torch.load(os.path.join(self.data_dir, "s3dis_val.pth"), 'cpu')

            # self.data_dict = torch.load(os.path.join(self.data_dir, "s3dis_debug.pth"), 'cpu')

            # DEBUG: dirty fixing
            self.data_dict['data'] = np.concatenate(self.data_dict['data'], axis=0)    # [batches, 4096, 9]
            self.data_dict['label'] = np.concatenate(self.data_dict['label'], axis=0)    # [batches, 4096, 9]

            self.sparse_voxelizer = SparseVoxelizer(
                voxel_size=config.voxel_size,
                clip_bound=None,
                use_augmentation=False,
                scale_augmentation_bound=None,
                rotation_augmentation_bound=None,
                translation_augmentation_ratio_bound=None,
                rotation_axis=0, # this isn't actually used
                ignore_label=-1
            )
        else:
            self.whole_scene = whole_scene   # load-whole scene

            self.NUM_IN_CHANNEL=9
            self.NUM_LABELS=13
            self.IGNORE_LABELS = [-1]  # labels that are not evaluated
            self.NEED_PRED_POSTPROCESSING = False

            self.train = train
            self.split = "train" if self.train else "val"

            if self.train:
                area_ids = [1,2,3,4,6]
            else:
                area_ids = [5]

            # area_ids = [1,2,3,4,5,6]

            self.data_path = "/home/zhaotianchen/project/point-transformer/pvcnn/data/s3dis/pointcnn"
            area_names = ["Area_{}".format(i) for i in area_ids]

            self.filelists = []
            for area_name in area_names:
                scene_names = os.listdir(os.path.join(self.data_path, area_name))
                for scene_name in scene_names:
                    self.filelists.append(os.path.join(area_name, scene_name))

            self.sparse_voxelizer = SparseVoxelizer(
                voxel_size=config.voxel_size,
                clip_bound=None,
                use_augmentation=False,
                scale_augmentation_bound=None,
                rotation_augmentation_bound=None,
                translation_augmentation_ratio_bound=None,
                rotation_axis=0, # this isn't actually used
                ignore_label=-1
            )

        # if download and not os.path.exists(self.data_dir):
            # zipfile = os.path.join(self.data_dir, os.path.basename(self.url))
            # subprocess.check_call(
                # shlex.split("curl {} -o {}".format(self.url, zipfile))
            # )

            # subprocess.check_call(
                # shlex.split("unzip {} -d {}".format(zipfile, self.data_dir))
            # )

            # subprocess.check_call(shlex.split("rm {}".format(zipfile)))

        # all_files = _get_data_files(os.path.join(self.data_dir, "all_files.txt"))
        # room_filelist = _get_data_files(
            # os.path.join(self.data_dir, "room_filelist.txt")
        # )

        # data_batchlist, label_batchlist = [], []
        # for f in all_files:
            # data, label = _load_data_file(os.path.join(self.data_path, f))
            # data_batchlist.append(data)
            # label_batchlist.append(label)

        # data_batches = np.concatenate(data_batchlist, 0)
        # labels_batches = np.concatenate(label_batchlist, 0)

        # test_area = "Area_5"
        # train_idxs, test_idxs = [], []

        # d = {}
        # d_val = {}
        # for i,room_name in enumerate(room_filelist):
            # if room_name not in d.keys():
                # d[room_name] = []
            # d[room_name].append(i)

        # test_keys = []
        # for k in d.keys():
            # if test_area in k:
                # test_keys.append(k)

        # for k in test_keys:
            # d_val[k] = d.pop(k)

        # train_names = d.keys()
        # val_names = d_val.keys()

        # d2save_train = {}
        # d2save_train['data'] = []
        # d2save_train['label'] = []
        # d2save_train['scene_name'] = list(train_names)
        # d2save_val = {}
        # d2save_val['data'] = []
        # d2save_val['label'] = []
        # d2save_val['scene_name'] = list(val_names)

        # for _, idx in d.items():
            # d2save_train['data'].append(data_batches[idx])
            # d2save_train['label'].append(labels_batches[idx])

        # for _, idx in d_val.items():
            # d2save_val['data'].append(data_batches[idx])
            # d2save_val['label'].append(labels_batches[idx])

        # ======================================================

        # # if self.train:
            # # self.points = data_batches[train_idxs, ...]
            # # self.labels = labels_batches[train_idxs, ...]
        # # else:
            # # self.points = data_batches[test_idxs, ...]
            # # self.labels = labels_batches[test_idxs, ...]

        # torch.save(d2save_train, os.path.join(self.data_dir, 's3dis_train.pth'))
        # torch.save(d2save_val, os.path.join(self.data_dir, 's3dis_val.pth'))
        # d_debug = {}
        # for k in d2save_train.keys():
            # d_debug[k] = d2save_train[k][:3]
        # torch.save(d_debug, os.path.join(self.data_dir, 's3dis_debug.pth'))

    def __getitem__(self, index):

        if not self.whole_scene:
            data = self.data_dict['data'][index].reshape([-1,9])
            target = self.data_dict['label'][index].reshape([-1])
            data_ = data

            if 'train' in self.split:
                theta = np.random.uniform(0, 2 * np.pi)
                scale_factor = np.random.uniform(0.95, 1.05)
                rot_mat = np.array([[np.cos(theta), np.sin(theta), 0],
                                    [-np.sin(theta),
                                     np.cos(theta), 0], [0, 0, 1]])

                data[:, :3] = np.dot(data_[:, :3], rot_mat) * scale_factor
            else:
                pass

            coords = data[:,:3] - (data[:,:3]).min(0)
            outs = self.sparse_voxelizer.voxelize(
                coords, # debug, not sure
                data_,
                target,
                center=None,
                rotation_angle=None,
                return_transformation=False
                )

            # d = {}
            # d['label'] = target
            # d['origin_pc'] = data_[:,:3]
            # d['v_coord'] = outs[0]
            # d['v_label'] = outs[2]
            # torch.save(d, "./plot/data-s3dis.pth")
            # import ipdb; ipdb.set_trace()

            (coords, feats, labels, unique_map, inverse_map) = outs
        else:
            root_path = os.path.join(self.data_path, self.filelists[index])
            data = np.load(os.path.join(root_path, "xyzrgb.npy")).astype(np.float32)

            # Apply AUG
            if 'train' in self.split:
                theta = np.random.uniform(0, 2 * np.pi)
                scale_factor = np.random.uniform(0.95, 1.05)
                rot_mat = np.array([[np.cos(theta), np.sin(theta), 0],
                                    [-np.sin(theta),
                                     np.cos(theta), 0], [0, 0, 1]])

                data[:, :3] = np.dot(data[:, :3], rot_mat) * scale_factor
            else:
                pass

            label = np.load(os.path.join(root_path, "label.npy")).astype(np.float32)
            label = label.squeeze(-1)
            normalized_xyz = (data[:,:3] / data[:,:3].max() - 0.5)
            data = np.concatenate([data, normalized_xyz], axis=-1)
            coords = data[:,:3]
            coords[:,:3] -= np.expand_dims(coords[:,:3].min(0), axis=0)
            outs = self.sparse_voxelizer.voxelize(
                coords,
                data,
                label,
                center=None,
                rotation_angle=None,
                return_transformation=False
                )
            # d = {}
            # d['label'] = label
            # d['origin_pc'] = coords[:,:3]
            # d['v_coord'] = outs[0]
            # d['v_label'] = outs[2]
            # torch.save(d, "./plot/data-s3dis-whole.pth")
            # print(label.shape, outs[2].shape)

        assert isinstance(outs, tuple)
        return outs

    def __len__(self):
        if not self.whole_scene:
            return len(self.data_dict['data'])
        else:
            return len(self.filelists)

    def cleanup(self):
        self.sparse_voxelizer.cleanup()

    def reorder_result(self, result):
        return result

if __name__ == "__main__":
    dset = Indoor3DSemSeg(16)
    print(dset[0])
    print(len(dset))
    dloader = torch.utils.data.DataLoader(dset, batch_size=32, shuffle=True)
    for i, data in enumerate(dloader, 0):
        inputs, labels = data
        if i == len(dloader) - 1:
            print(inputs.size())


text:
import cv2
import numpy as np

from code.features import convert_color, get_hog_features, bin_spatial, color_hist

def find_cars(img, y_start, y_stop, scale, svc, X_scaler,
              color_space, spatial_size, hist_bins,
              orient, pix_per_cell, cell_per_block, hog_channel):
    """ Uses the trained model to detect cars and returns and image with the detections drawn on top """

    draw_img = np.copy(img)
    img = img.astype(np.float32) / 255

    img_tosearch = img[y_start:y_stop, :, :]
    ctrans_to_search = convert_color(img_tosearch, color_space=color_space)
    if scale != 1:
        shape = ctrans_to_search.shape
        ctrans_to_search = cv2.resize(ctrans_to_search, (np.int(shape[1] / scale), np.int(shape[0] / scale)))

    ch1 = ctrans_to_search[:, :, 0]
    ch2 = ctrans_to_search[:, :, 1]
    ch3 = ctrans_to_search[:, :, 2]

    # Define blocks and steps as above
    nx_blocks = (ch1.shape[1] // pix_per_cell) - 1
    ny_blocks = (ch1.shape[0] // pix_per_cell) - 1
    # 64 was the orginal sampling rate, with 8 cells and 8 pix per cell
    window = 64
    nblocks_per_window = (window // pix_per_cell) - 1
    cells_per_step = 2  # Instead of overlap, define how many cells to step
    nx_steps = (nx_blocks - nblocks_per_window) // cells_per_step
    ny_steps = (ny_blocks - nblocks_per_window) // cells_per_step

    # Compute individual channel HOG features for the entire image
    if hog_channel == 'ALL':
        hog1 = get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False)
        hog2 = get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False)
        hog3 = get_hog_features(ch3, orient, pix_per_cell, cell_per_block, feature_vec=False)
    else:
        hog = get_hog_features(hog_channel, orient, pix_per_cell, cell_per_block, feature_vec=False)

    for xb in range(nx_steps):
        for yb in range(ny_steps):
            y_pos = yb * cells_per_step
            x_pos = xb * cells_per_step
            # Extract HOG for this patch

            if hog_channel == 'ALL':
                hog_feat1 = hog1[y_pos:y_pos + nblocks_per_window, x_pos:x_pos + nblocks_per_window].ravel()
                hog_feat2 = hog2[y_pos:y_pos + nblocks_per_window, x_pos:x_pos + nblocks_per_window].ravel()
                hog_feat3 = hog3[y_pos:y_pos + nblocks_per_window, x_pos:x_pos + nblocks_per_window].ravel()
                hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3))
            else:
                hog_features = hog[y_pos:y_pos + nblocks_per_window, x_pos:x_pos + nblocks_per_window].ravel()

            x_left = x_pos * pix_per_cell
            y_top = y_pos * pix_per_cell

            # Extract the image patch
            subimg = cv2.resize(ctrans_to_search[y_top:y_top + window, x_left:x_left + window], (64, 64))

            # Get color features
            spatial_features = bin_spatial(subimg, size=spatial_size)
            hist_features = color_hist(subimg, nbins=hist_bins)

            # Scale features and make a prediction
            test_features = X_scaler.transform(
                np.hstack((spatial_features, hist_features, hog_features)).reshape(1, -1))
            test_prediction = svc.predict(test_features)

            if test_prediction == 1:
                xbox_left = np.int(x_left * scale)
                ytop_draw = np.int(y_top * scale)
                win_draw = np.int(window * scale)
                cv2.rectangle(draw_img, (xbox_left, ytop_draw + y_start),
                              (xbox_left + win_draw, ytop_draw + win_draw + y_start), (0, 0, 255), 4)

    return draw_img

ext: py

max_stars_repo_name: GitHK/CarND-Vehicle-Detection-

max_issues_repo_name: GitHK/CarND-Vehicle-Detection-

max_forks_repo_name: GitHK/CarND-Vehicle-Detection-

reason: import numpy


text:
import sys, os
sys.path.insert(0, os.path.abspath('..'))
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import yolk
from yolk.parser import parse_args

def main(args=None):
    if args is None:
        args = sys.argv[1:]

    args = parse_args(args)

    image = Image.open('./000000008021.jpg')
    image, scale = yolk.detector.preprocessing_image(image)

    model_path = os.path.join('..', 'resnet50_coco_best_v2.1.0.h5')
    model = yolk.detector.load_inference_model(model_path, args)

    model_output = model.predict_on_batch(np.expand_dims(image, axis=0))

    print(model_output)

    #shape = image.shape
    #boxes, scores, labels = yolk.detector.postprocessing(model_output, original_shape=image.shape, args)
    #print(boxes)

if __name__ == '__main__':
    main()


text:
#!/usr/bin/env python

import rospy
from race.msg import drive_param
from soni_gap_finding.msg import lidar_gaps
from std_msgs.msg import Float64
from sensor_msgs.msg import LaserScan
import numpy as np
from geometry_msgs.msg import Vector3
np.set_printoptions(threshold=np.inf)

#from sklearn.cluster import DBSCAN

#this publishes the drive command to run the car.
drive_pub = rospy.Publisher('drive_parameters', drive_param, queue_size=1)

#this publishes the center of the gap in(x,y,z) coordinate system
gap_center_pub = rospy.Publisher("gap_center", Vector3, queue_size=1)

gap_pub = rospy.Publisher("lidar_gaps", lidar_gaps, queue_size=1)
#Implement your own message to publish relevant information about gaps
#gaps_pub = rospy.Publisher("gaps", <your message type>, queue_size=1)

# Callback that receives LIDAR data on the /scan topic.
# data: the LIDAR data, published as sensor_msgs::LaserScan
def scan_callback(data):
	#Implement some clustering algorithm(e.g K-means,DBSCAN,your own custom algorithm) to cluster points and find possible gaps
    #Publish the center of the gaps as well as the endpoints of the gaps.
	#angle_min = np.rad2deg(data.angle_min)
	#angle_max = np.rad2deg(data.angle_max)
	angle_diff = data.angle_increment
	range_min = data.range_min
	ranges = np.asarray(data.ranges)

	for i in range(len(ranges)):
		if ranges[i] <= 1.5:
			ranges[i] = 0

	##############
	##Clustering##
	##############
	Num_Cluster = 0
	len_cluster = []
	Clusters = []
	cluster_center = []
	distance = 0.1
	count = True
	i = 0
	while count:
		cluster_local = []
		cluster_local.append(i)
		while (abs(ranges[i]-ranges[i+1]) <= distance):
			cluster_local.append(i+1)
			i += 1
			if i >= len(ranges)-1:
				break
		i += 1
		minimum = int(3/np.rad2deg(angle_diff))
		maximum = int(90/np.rad2deg(angle_diff))

		#Lidar Step size is 0.25 degree considering minimum 3 degree and 45 degree maximum
		if len(cluster_local) >= minimum and len(cluster_local) <= maximum:
			Clusters.append(cluster_local)
			gap_pub.publish(cluster_local)
			Num_Cluster += 1
			len_cluster.append(len(cluster_local))
			cluster_center.append(int(len(cluster_local)/2))
		if i >= len(ranges)-1:
			break
	temp = 0
	clus_num = 0
	clus_cen_index = 540
	for j in range(Num_Cluster):
		clus1 = Clusters[j]
		clus_cen_index = clus1[cluster_center[j]]
		if temp < ranges[clus_cen_index]:
			temp = ranges[clus_cen_index]
			clus_num = j

	pos = clus_cen_index - 540

	angle = angle_diff*pos
	gap = Vector3()
	gap.x = np.sin(angle)*temp
	gap.y = np.cos(angle)*temp
	gap.z = 0

	gap_center_pub.publish(gap)
	#clustering = DBSCAN(eps=0.1, min_samples=2).fit(ranges)
	#rospy.loginfo("Clusters" +str(Clusters))

    #Based on the center of the gap, you can also implement a simple PID controller to follow it. Be careful about the lookahead distance you use.
	msg = drive_param()
	msg.velocity = 0.1  # TODO: implement PID for velocity
	msg.angle = 0.0    # TODO: implement PID for steering angle
	drive_pub.publish(msg)

# Boilerplate code to start this ROS node.
if __name__ == '__main__':
	rospy.init_node('soni_gap_finding_node', anonymous=True)
	rospy.Subscriber("scan", LaserScan, scan_callback)
	rospy.spin()


text:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot3d(pixels, colors_rgb,
        axis_labels=list("RGB"), axis_limits=((0, 255), (0, 255), (0, 255))):
    """Plot pixels in 3D."""

    # Create figure and 3D axes
    fig = plt.figure(figsize=(8, 8))
    ax = Axes3D(fig)

    # Set axis limits
    ax.set_xlim(*axis_limits[0])
    ax.set_ylim(*axis_limits[1])
    ax.set_zlim(*axis_limits[2])

    # Set axis labels and sizes
    ax.tick_params(axis='both', which='major', labelsize=14, pad=8)
    ax.set_xlabel(axis_labels[0], fontsize=16, labelpad=16)
    ax.set_ylabel(axis_labels[1], fontsize=16, labelpad=16)
    ax.set_zlabel(axis_labels[2], fontsize=16, labelpad=16)

    # Plot pixel values with colors given in colors_rgb
    ax.scatter(
        pixels[:, :, 0].ravel(),
        pixels[:, :, 1].ravel(),
        pixels[:, :, 2].ravel(),
        c=colors_rgb.reshape((-1, 3)), edgecolors='none')

    return ax  # return Axes3D object for further manipulation

# Read a color image
img = cv2.imread("../../../../test_images/test1.jpg")

# Select a small fraction of pixels to plot by subsampling it
scale = max(img.shape[0], img.shape[1], 64) / 64  # at most 64 rows and columns
img_small = cv2.resize(img, (np.int(img.shape[1] / scale), np.int(img.shape[0] / scale)), interpolation=cv2.INTER_NEAREST)

# Convert subsampled image to desired color space(s)
img_small_RGB = cv2.cvtColor(img_small, cv2.COLOR_BGR2RGB)  # OpenCV uses BGR, matplotlib likes RGB
img_small_HSV = cv2.cvtColor(img_small, cv2.COLOR_BGR2HSV)
img_small_rgb = img_small_RGB / 255.  # scaled to [0, 1], only for plotting

# Plot and show
plot3d(img_small_RGB, img_small_rgb)
plt.show()

plot3d(img_small_HSV, img_small_rgb, axis_labels=list("HSV"))
plt.show()


text:
import sesame
import numpy as np

L = 3e-4 # length of the system in the x-direction [cm]

# Mesh
x = np.concatenate((np.linspace(0,1.2e-4, 100, endpoint=False),
                    np.linspace(1.2e-4, L, 50)))

# Create a system
sys = sesame.Builder(x)

# Dictionary with the material parameters
material = {'Nc':8e17, 'Nv':1.8e19, 'Eg':1.5, 'affinity':3.9, 'epsilon':9.4,
        'mu_e':100, 'mu_h':100, 'tau_e':10e-9, 'tau_h':10e-9, 'Et':0}

# Add the material to the system
sys.add_material(material)

junction = 50e-7 # extent of the junction from the left contact [m]

def n_region(pos):
    x = pos
    return x < junction

def p_region(pos):
    x = pos
    return x >= junction

# Add the donors
nD = 1e17 # [cm^-3]
sys.add_donor(nD, n_region)

# Add the acceptors
nA = 1e15 # [cm^-3]
sys.add_acceptor(nA, p_region)

# Define Ohmic contacts
sys.contact_type('Ohmic', 'Ohmic')

# Define the surface recombination velocities for electrons and holes [cm/s]
Sn_left, Sp_left, Sn_right, Sp_right = 1e7, 0, 0, 1e7
sys.contact_S(Sn_left, Sp_left, Sn_right, Sp_right)

# Define a function for the generation rate
phi = 1e17         # photon flux [1/(cm^2 s)]
alpha = 2.3e4      # absorption coefficient [1/cm]

# Define a function for the generation rate
def gfcn(x,y):
    return phi * alpha * np.exp(-alpha * x)

# add generation to system
sys.generation(gfcn)

# IV curve
voltages = np.linspace(0, 0.95, 10)
j = sesame.IVcurve(sys, voltages, '1dhomo_V')

# convert dimensionless current to dimension-ful current
j = j * sys.scaling.current
# save voltage and current values to dictionary
result = {'v':voltages, 'j':j}

# save data to python data file
np.save('IV_values', result)

# save data to an ascii txt file
np.savetxt('IV_values.txt', (voltages, j))

# save data to a matlab data file
try:
    from scipy.io import savemat
    savemat('IV_values.mat', result)
# no scipy installed
except ImportError:
    print("Scipy not installed, can't save to .mat file")

# plot I-V curve
try:
    import matplotlib.pyplot as plt
    plt.plot(voltages, j,'-o')
    plt.xlabel('Voltage [V]')
    plt.ylabel('Current [A/cm^2]')
    plt.grid()     # add grid
    plt.show()     # show the plot on the screen
# no matplotlib installed
except ImportError:
    print("Matplotlib not installed, can't make plot")

max_stars_repo_name: usnistgov/sesame

max_issues_repo_name: usnistgov/sesame

max_forks_repo_name: usnistgov/sesame

reason: import numpy,from scipy


text:
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from typing import Union

import sympy

def _name(letter: str, *subscripts: Union[str, int]) -> str:
    return letter + ''.join('_{}'.format(subscript)
                            for subscript in subscripts)

class LetterWithSubscripts(sympy.Symbol):

    def __new__(cls, letter: str, *subscripts: Union[str, int]):
        return super().__new__(cls, _name(letter, *subscripts))

    def __init__(self,
                 letter: str,
                 *subscripts: Union[str, int]) -> None:
        self.letter = letter
        self.subscripts = subscripts
        super().__init__()

    def __eq__(self, other):
        if not isinstance(other, sympy.Symbol):
            return NotImplemented
        return str(self) == str(other)

    def __ne__(self, other):
        return not self == other

    def __hash__(self):
        return hash(sympy.Symbol(str(self)))

    def __repr__(self):
        return (
            'ofc.variational.letter_with_subscripts.'
            'LetterWithSubscripts({!r}, {})'.format(
                self.letter,
                ', '.join(str(e) for e in self.subscripts)))

    def _subs(self, old, new, **hints):
        # HACK: work around sympy not recognizing child classes as symbols.
        if old == self:
            return new
        return super()._subs(old, new, **hints)

max_stars_repo_name: viathor/OpenFermion-Cirq

max_issues_repo_name: viathor/OpenFermion-Cirq

max_forks_repo_name: viathor/OpenFermion-Cirq

reason: import sympy


text:
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid
import numpy as np
from functools import partial

from mars.actors import create_actor_pool
from mars.compat import six
from mars.config import options
from mars.utils import get_next_port, calc_data_size
from mars import promise
from mars.errors import StoreFull, NoDataToSpill
from mars.scheduler.kvstore import KVStoreActor
from mars.tests.core import patch_method
from mars.worker.tests.base import WorkerCase
from mars.worker import *
from mars.worker.chunkstore import PlasmaKeyMapActor
from mars.worker.utils import WorkerActor, WorkerClusterInfoActor

class CacheTestActor(WorkerActor):
    def __init__(self):
        super(CacheTestActor, self).__init__()
        self._exc_info = None
        self._finished = False

    def run_test_cache(self):
        session_id = str(uuid.uuid4())

        chunk_holder_ref = self.promise_ref(ChunkHolderActor.default_uid())
        chunk_store = self._chunk_store

        data_list = []
        for _ in range(9):
            data_id = str(uuid.uuid4())
            data = np.random.randint(0, 32767, (655360,), np.int16)
            data_list.append((data_id, data))

        def _put_chunk(data_key, data, *_):
            def _handle_reject(*exc):
                if issubclass(exc[0], NoDataToSpill):
                    return
                six.reraise(*exc)

            try:
                ref = chunk_store.put(session_id, data_key, data)
                chunk_holder_ref.register_chunk(session_id, data_key)
                self.ctx.sleep(0.5)
                del ref
            except StoreFull:
                return chunk_holder_ref.spill_size(calc_data_size(data) * 2, _promise=True) \
                    .then(partial(_put_chunk, data_key, data), _handle_reject)

        data_promises = []
        for data_id, data in data_list:
            data_promises.append(promise.finished()
                                 .then(partial(_put_chunk, data_id, data)))

        def assert_true(v):
            assert v

        last_id = data_list[-1][0]
        p = promise.all_(data_promises) \
            .then(lambda *_: assert_true(chunk_store.contains(session_id, last_id))) \
            .then(lambda *_: ensure_chunk(self, session_id, last_id)) \
            .then(lambda *_: assert_true(chunk_store.contains(session_id, last_id)))

        first_id = data_list[0][0]
        p = p.then(lambda *_: assert_true(not chunk_store.contains(session_id, first_id))) \
            .then(lambda *_: ensure_chunk(self, session_id, first_id)) \
            .then(lambda *_: assert_true(chunk_store.contains(session_id, first_id)))

        p = p.then(lambda *_: chunk_holder_ref.unregister_chunk(session_id, first_id)) \
            .then(lambda *_: self._plasma_client.evict(128)) \
            .then(lambda *_: assert_true(not chunk_store.contains(session_id, first_id)))

        p = p.then(lambda *_: chunk_holder_ref.unregister_chunk(session_id, last_id)) \
            .then(lambda *_: self._plasma_client.evict(128)) \
            .then(lambda *_: assert_true(not chunk_store.contains(session_id, last_id)))

        p.catch(lambda *exc: setattr(self, '_exc_info', exc)) \
            .then(lambda *_: setattr(self, '_finished', True))

    def run_test_ensure_timeout(self):
        from mars.worker.chunkholder import ensure_chunk
        promises = [
            ensure_chunk(self, str(uuid.uuid4()), str(uuid.uuid4())),
            ensure_chunk(self, str(uuid.uuid4()), str(uuid.uuid4())),
        ]
        promise.all_(promises) \
            .then(lambda *_: setattr(self, '_exc_info', None), lambda *exc: setattr(self, '_exc_info', exc)) \
            .then(lambda *_: setattr(self, '_finished', True))

    def get_exc_info(self):
        return self._finished, self._exc_info

class Test(WorkerCase):
    def setUp(self):
        options.worker.min_spill_size = 0

    def tearDown(self):
        options.worker.min_spill_size = '5%'

    def testHolder(self):
        pool_address = '127.0.0.1:%d' % get_next_port()
        with create_actor_pool(n_process=1, backend='gevent', address=pool_address) as pool:
            pool.create_actor(PlasmaKeyMapActor, uid=PlasmaKeyMapActor.default_uid())
            pool.create_actor(WorkerClusterInfoActor, [pool_address],
                              uid=WorkerClusterInfoActor.default_uid())
            pool.create_actor(KVStoreActor, uid=KVStoreActor.default_uid())
            pool.create_actor(DispatchActor, uid=DispatchActor.default_uid())
            pool.create_actor(QuotaActor, 1024 * 1024 * 10, uid=MemQuotaActor.default_uid())
            cache_ref = pool.create_actor(ChunkHolderActor, self.plasma_storage_size,
                                          uid=ChunkHolderActor.default_uid())
            pool.create_actor(SpillActor)

            try:
                test_ref = pool.create_actor(CacheTestActor)
                test_ref.run_test_cache()
                while not test_ref.get_exc_info()[0]:
                    pool.sleep(0.1)
                exc_info = test_ref.get_exc_info()[1]
                if exc_info:
                    six.reraise(*exc_info)
            finally:
                pool.destroy_actor(cache_ref)

    @patch_method(SpillActor.load)
    def testEnsureTimeout(self, *_):
        from mars.errors import PromiseTimeout

        pool_address = '127.0.0.1:%d' % get_next_port()
        with create_actor_pool(n_process=1, backend='gevent', address=pool_address) as pool:
            pool.create_actor(PlasmaKeyMapActor, uid=PlasmaKeyMapActor.default_uid())
            pool.create_actor(WorkerClusterInfoActor, [pool_address],
                              uid=WorkerClusterInfoActor.default_uid())
            pool.create_actor(KVStoreActor, uid=KVStoreActor.default_uid())
            pool.create_actor(DispatchActor, uid=DispatchActor.default_uid())
            pool.create_actor(QuotaActor, 1024 * 1024 * 10, uid=MemQuotaActor.default_uid())
            pool.create_actor(SpillActor, uid=SpillActor.default_uid())
            cache_ref = pool.create_actor(ChunkHolderActor, self.plasma_storage_size,
                                          uid=ChunkHolderActor.default_uid())

            try:
                options.worker.prepare_data_timeout = 2
                test_ref = pool.create_actor(CacheTestActor)
                test_ref.run_test_ensure_timeout()
                while not test_ref.get_exc_info()[0]:
                    pool.sleep(0.1)
                exc_info = test_ref.get_exc_info()[1]
                self.assertIsNotNone(exc_info)
                self.assertIsInstance(exc_info[1], PromiseTimeout)
            finally:
                options.worker.prepare_data_timeout = 600
                pool.destroy_actor(cache_ref)

max_stars_repo_name: wdkwyf/mars

max_issues_repo_name: wdkwyf/mars

max_forks_repo_name: wdkwyf/mars


text:
# Author: Tao Hu <taohu620@gmail.com>

# -*- coding: utf-8 -*-
# File: rect.py

import numpy as np
import cv2

__all__ = ["draw_det_box", "draw_boxes"]

class BoxBase(object):
    __slots__ = ["x1", "y1", "x2", "y2"]

    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

    def copy(self):
        new = type(self)()
        for i in self.__slots__:
            setattr(new, i, getattr(self, i))
        return new

    def __str__(self):
        return "{}(x1={}, y1={}, x2={}, y2={})".format(
            type(self).__name__, self.x1, self.y1, self.x2, self.y2
        )

    __repr__ = __str__

    def area(self):
        return self.w * self.h

    def is_box(self):
        return self.w > 0 and self.h > 0

class IntBox(BoxBase):
    def __init__(self, x1, y1, x2, y2):
        for k in [x1, y1, x2, y2]:
            assert isinstance(k, int)
        super(IntBox, self).__init__(x1, y1, x2, y2)

    @property
    def w(self):
        return self.x2 - self.x1 + 1

    @property
    def h(self):
        return self.y2 - self.y1 + 1

    def is_valid_box(self, shape):
        """
        Check that this rect is a valid bounding box within this shape.

        Args:
            shape: int [h, w] or None.
        Returns:
            bool
        """
        if min(self.x1, self.y1) < 0:
            return False
        if min(self.w, self.h) <= 0:
            return False
        if self.x2 >= shape[1]:
            return False
        if self.y2 >= shape[0]:
            return False
        return True

    def clip_by_shape(self, shape):
        """
        Clip xs and ys to be valid coordinates inside shape

        Args:
            shape: int [h, w] or None.
        """
        self.x1 = np.clip(self.x1, 0, shape[1] - 1)
        self.x2 = np.clip(self.x2, 0, shape[1] - 1)
        self.y1 = np.clip(self.y1, 0, shape[0] - 1)
        self.y2 = np.clip(self.y2, 0, shape[0] - 1)

    def roi(self, img):
        assert self.is_valid_box(img.shape[:2]), "{} vs {}".format(
            self, img.shape[:2]
        )
        return img[self.y1 : self.y2 + 1, self.x1 : self.x2 + 1]

class FloatBox(BoxBase):
    def __init__(self, x1, y1, x2, y2):
        for k in [x1, y1, x2, y2]:
            assert isinstance(k, float), "type={},value={}".format(type(k), k)
        super(FloatBox, self).__init__(x1, y1, x2, y2)

    @property
    def w(self):
        return self.x2 - self.x1

    @property
    def h(self):
        return self.y2 - self.y1

    @staticmethod
    def from_intbox(intbox):
        return FloatBox(intbox.x1, intbox.y1, intbox.x2 + 1, intbox.y2 + 1)

    def clip_by_shape(self, shape):
        self.x1 = np.clip(self.x1, 0, shape[1])
        self.x2 = np.clip(self.x2, 0, shape[1])
        self.y1 = np.clip(self.y1, 0, shape[0])
        self.y2 = np.clip(self.y2, 0, shape[0])

def draw_det_box(img, boxes_list, class_name, color=(255, 0, 0)):
    # boxes, shape:(4,)
    height, width, channels = img.shape
    images_cv = np.copy(img)
    for boxes in boxes_list:
        min_x = boxes[0]
        min_y = boxes[1]
        max_x = boxes[2]
        max_y = boxes[3]

        min_x = float(min_x) * width  # normalize 1
        max_x = float(max_x) * width
        min_y = float(min_y) * height
        max_y = float(max_y) * height

        floatBox = FloatBox(min_x, min_y, max_x, max_y)
        floatBox.clip_by_shape((width, height))
        images_cv = draw_boxes(
            images_cv,
            np.reshape(
                np.asarray(
                    [floatBox.x1, floatBox.y1, floatBox.x2, floatBox.y2]
                ),
                (1, 4),
            ),
            color=color,
            labels=[class_name],
        )
    return images_cv

def draw_det_box_seg(img, boxes_list, class_name, seg_mask, color=(255, 0, 0)):
    # boxes, shape:(4,)
    height, width, channels = img.shape
    images_cv = np.copy(img)
    for boxes in boxes_list:
        min_x = boxes[0]
        min_y = boxes[1]
        max_x = boxes[2]
        max_y = boxes[3]

        min_x = float(min_x) * width  # normalize 1
        max_x = float(max_x) * width
        min_y = float(min_y) * height
        max_y = float(max_y) * height

        floatBox = FloatBox(min_x, min_y, max_x, max_y)
        floatBox.clip_by_shape((width, height))
        images_cv = draw_boxes(
            images_cv,
            np.reshape(
                np.asarray(
                    [floatBox.x1, floatBox.y1, floatBox.x2, floatBox.y2]
                ),
                (1, 4),
            ),
            color=color,
            labels=[class_name],
        )

    def apply_mask(image, mask, color, alpha=0.5):
        """Apply the given mask to the image.
        """
        for c in range(3):
            image[:, :, c] = np.where(
                mask == 1,
                image[:, :, c] * (1 - alpha) + alpha * color[c],
                image[:, :, c],
            )
        return image

    # seg_mask = np.broadcast_to(seg_mask.reshape(height, width, 1),(height, width, 3))
    images_cv = apply_mask(images_cv, seg_mask, color=color)

    return images_cv

def draw_text(img, pos, text, color, font_scale=0.5):
    """
    Draw text on an image.
    Args:
        pos (tuple): x, y; the position of the text
        text (str):
        font_scale (float):
        color (tuple): a 3-tuple BGR color in [0, 255]
    """
    img = img.astype(np.uint8)
    x0, y0 = int(pos[0]), int(pos[1])
    # Compute text size.
    font = cv2.FONT_HERSHEY_SIMPLEX
    ((text_w, text_h), _) = cv2.getTextSize(text, font, font_scale, 1)
    # Place text background.
    if x0 + text_w > img.shape[1]:
        x0 = img.shape[1] - text_w
    if y0 - int(1.15 * text_h) < 0:
        y0 = int(1.15 * text_h)
    back_topleft = x0, y0 - int(1.3 * text_h)
    back_bottomright = x0 + text_w, y0
    cv2.rectangle(img, back_topleft, back_bottomright, color, -1)
    # Show text.
    text_bottomleft = x0, y0 - int(0.25 * text_h)
    cv2.putText(
        img,
        text,
        text_bottomleft,
        font,
        font_scale,
        (222, 222, 222),
        lineType=cv2.LINE_AA,
    )
    return img

def draw_boxes(im, boxes, labels=None, color=None, thickness=2):
    """
    Args:
        im (np.ndarray): a BGR image in range [0,255]. It will not be modified.
        boxes (np.ndarray): a numpy array of shape Nx4 where each row is [x1, y1, x2, y2].
        labels: (list[str] or None)
        color: a 3-tuple BGR color (in range [0, 255])
    Returns:
        np.ndarray: a new image.
    """
    boxes = np.asarray(boxes, dtype="int32")
    if labels is not None:
        assert len(labels) == len(boxes), "{} != {}".format(
            len(labels), len(boxes)
        )
    areas = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)
    sorted_inds = np.argsort(-areas)  # draw large ones first
    assert areas.min() > 0, areas.min()
    # allow equal, because we are not very strict about rounding error here
    assert (
        boxes[:, 0].min() >= 0
        and boxes[:, 1].min() >= 0
        and boxes[:, 2].max() <= im.shape[1]
        and boxes[:, 3].max() <= im.shape[0]
    ), "Image shape: {}\n Boxes:\n{}".format(str(im.shape), str(boxes))

    im = im.copy()
    if color is None:
        color = (15, 128, 15)
    if im.ndim == 2 or (im.ndim == 3 and im.shape[2] == 1):
        im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
    for i in sorted_inds:
        box = boxes[i, :]
        if labels is not None:
            im = draw_text(im, (box[0], box[1]), labels[i], color=color)
        cv2.rectangle(
            im,
            (box[0], box[1]),
            (box[2], box[3]),
            color=color,
            thickness=thickness,
        )
    return im

if __name__ == "__main__":
    x = IntBox(2, 1, 3, 3)
    img = np.random.rand(3, 3)
    print(img)


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
print (sys.version)
import time

import numpy as np
my_dir = os.path.abspath(os.path.dirname(__file__))

#  ---------------------------------------------------------  #
#  functions                                                  #
#  ---------------------------------------------------------  #
my_library = os.path.expanduser('~/.pylib')
sys.path.append(my_library)
# mpl_moving_average
# mpl_forcequench
# mpl_worm
from plot.SETTINGS import *

#  ---------------------------------------------------------  #
#  Start matplotlib (1/4)                                     #
#  ---------------------------------------------------------  #
import matplotlib
# default - Qt5Agg
# print matplotlib.rcsetup.all_backends
# matplotlib.use('GTKAgg')
# matplotlib.use('TkAgg')
print 'backend:',matplotlib.get_backend()
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure(0)

gs = GridSpec(1,1)
ax1 = plt.subplot(gs[0,:])
# ax2 = plt.subplot(gs[1,:-1])
ax = [ax1]

#  ---------------------------------------------------------  #
#  Import Data! (2/4)                                         #
#  ---------------------------------------------------------  #
result_type = 'emol' # sop | sopnucleo | gsop | namd
plot_type = 'contacts' # fe | tension | rmsd | rdf

#  ---------------------------------------------------------  #
#  mpl_myargs_begin                                           #
#  ---------------------------------------------------------  #
import argparse

def parse_arguments():
    ''' Parse script's arguments.
    '''
    parser = argparse.ArgumentParser()
    parser.add_argument("-o","--option",help="select None,publish,show")
    parser.add_argument("-d","--dataname",help="data name: run280, 76n")
    args = vars(parser.parse_args())
    return args

args = parse_arguments()
option = args['option']
data_name = args['dataname']

#  ---------------------------------------------------------  #
#  Import Data! (3/4)                                         #
#  ---------------------------------------------------------  #

data = np.loadtxt('emol_contacts_EX1.dat')
print data.shape

mtdata = np.reshape(data,(data.shape[0],156,data.shape[1]/156))
print mtdata.shape

# for f in mtdata.shape[0]:
# Slice:

# alphas = mtdata[::,::,0]
# betas = mtdata[::,::,1]

# for a in range(alphas.shape[1]):
#     adata = alphas[::,a]
#     plt.plot(adata)

# save_fig(my_dir,0,'fig','%s_%s_%s_alpha' % (result_type,plot_type,data_name),option)
# plt.clf()

# for a in range(betas.shape[1]):
#     adata = betas[::,a]
#     plt.plot(adata)

# save_fig(my_dir,0,'fig','%s_%s_%s_beta' % (result_type,plot_type,data_name),option)
# plt.clf()

# internals = mtdata[::,::,2]

# for a in range(internals.shape[1]):
#     adata = internals[::,a]
#     plt.plot(adata)

# save_fig(my_dir,0,'fig','%s_%s_%s_internals' % (result_type,plot_type,data_name),option)
# plt.clf()

def plot_contacts(tup):
    mdata = mtdata[::,::,tup[1]]

    for a in range(mdata.shape[1]):
        plt.plot(mdata[::,a])

    save_fig(my_dir,0,'fig','%s_%s_%s_%s' % (result_type,plot_type,data_name,tup[0]),option)
    plt.clf()

lst = [("alpha",0),("beta",1),("intra",2),("a-east",3),
       ("a-west",4),("a-south",5),("b-east",6),("b-west",7),("b-north",8)]

for l in lst:
    plot_contacts(l)

# be = mtdata[::,::,1]

# for a in range(alphas.shape[1]):
#     adata = mtdata[::,a]
#     plt.plot(adata)

# save_fig(my_dir,0,'fig','%s_%s_%s_alpha' % (result_type,plot_type,data_name),option)
# plt.clf()

#  ---------------------------------------------------------  #
#  Make final adjustments: (4/4)                              #
#  mpl - available expansions                                 #
#  ---------------------------------------------------------  #
# mpl_rc
# mpl_font
# mpl_label
# mpl_xy
# mpl_ticks
# mpl_tick
# mpl_minorticks
# mpl_legend
# combined_name = '%s_%s_%s' % (result_type, plot_type, data_name)
# save_fig

# mpl_myargs_end


text:
# Copyright 2017 Cristian Mattarei
#
# Licensed under the modified BSD (3-clause BSD) License.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import numpy
from six.moves import range

from ecmasab.execution import Thread, Program, Block, Memory_Event, Executions, Execution, Relation, For_Loop, ITE_Statement
from ecmasab.execution import READ, WRITE, MODIFY, ADD, AND, SUB, OR, XOR, EXC, INIT, SC, UNORD, WTEAR, NTEAR, MAIN
from ecmasab.execution import HB, RF, RBF, MO, SW
from ecmasab.exceptions import UnreachableCodeException
from ecmasab.utils import values_from_int, int_from_values, float_from_values

from pyparsing import ParseException, Word, nums, alphas, LineEnd, restOfLine, Literal, ZeroOrMore, Empty, \
    operatorPrecedence, opAssoc, Combine, Optional, White, Group

T_AADD = "Atomics.add"
T_AAND = "Atomics.and"
T_AEXC = "Atomics.exchange"
T_ALOAD = "Atomics.load"
T_AND = "AND"
T_AOR = "Atomics.or"
T_ASTORE = "Atomics.store"
T_ASUB = "Atomics.sub"
T_AXOR = "Atomics.xor"
T_BEQ = "=="
T_CCB = "}"
T_CM = ","
T_COM = "//"
T_CP = ")"
T_CSB = "]"
T_DIV = "/"
T_DONE = "DONE"
T_DOT = "."
T_DOTS = ".."
T_ELSE = "else"
T_EQ = "="
T_FLO32 = "-F32"
T_FLO64 = "-F64"
T_FOR = "for"
T_GEQ = ">="
T_GT = ">"
T_IF = "if"
T_INT16 = "-I16"
T_INT32 = "-I32"
T_INT8 = "-I8"
T_LEQ = "<="
T_LT = "<"
T_MIN = "-"
T_MUL = "*"
T_NEW = "new"
T_NL = "\n"
T_OCB = "{"
T_OP = "("
T_OPE = "op_"
T_OSB = "["
T_PARAMS = "Params"
T_PR = "print"
T_SAB = "SharedArrayBuffer"
T_SEMI = ";"
T_SUM = "+"
T_THREAD = "Thread"
T_US = "_"
T_VAL = "val_"
T_VAR = "var"

P_ACCESS = "access"
P_ADD = "oadd"
P_ADDR = "address"
P_ADDRSET = "address-set"
P_AND = "oand"
P_ASS = "assign"
P_BCOND = "bcond"
P_BIREL = "bi-relation"
P_COMMENT = "comment"
P_CSCOPE = "closescope"
P_ELSE = "else"
P_EMPTY = "empty"
P_EMREL = "em-relation"
P_EXC = "oexchange"
P_EXPR = "expr"
P_FLOOP = "floop"
P_IF = "if"
P_INIT = "init"
P_INTER = "interval"
P_LIST = "list"
P_LOAD = "load"
P_OP = "operator"
P_OR = "oor"
P_PARAM = "param"
P_PARAMS = "params"
P_PRINT = "print"
P_RANGE = "range"
P_READ = "read"
P_SABASS = "sab-assign"
P_SABDEF = "sabdef"
P_SIZE = "varsize"
P_STORE = "store"
P_SUB = "osub"
P_TRDB = "thread-begin"
P_TRREL = "tr-relation"
P_TYPEOP = "typeop"
P_VALUE = "value"
P_VLEFT = "vleft"
P_VNAME = "varname"
P_VRIGHT = "vright"
P_WRITE = "write"
P_XOR = "oxor"

class ParsingErrorException(Exception):
    pass

class BeParser(object):
    program_parser = None
    execution_parser = None

    DEBUG = False

    def __init__(self):
        Memory_Event.reset_unique_names()
        ITE_Statement.reset_unique_names()

        self.program_parser = self.__init_program_parser()
        self.execution_parser = self.__init_execution_parser()

    def __init_execution_parser(self):
        varname = Word(alphas+nums+T_US)
        bitup = T_OP + varname + T_CM + varname + T_CP
        trtup = T_OP + T_OP + varname + T_CM + varname + T_CP + T_CM + varname + T_CP

        birel = bitup + ZeroOrMore(T_CM + bitup)
        trrel = trtup + ZeroOrMore(T_CM + trtup)

        trrelation = (varname + T_EQ + T_OCB + (trrel) + T_CCB)(P_TRREL)
        birelation = (varname + T_EQ + T_OCB + (birel) + T_CCB)(P_BIREL)
        emrelation = (varname + T_EQ + T_OCB + (Empty()) + T_CCB)(P_EMREL)

        varassign = (T_OP + varname + T_EQ + varname + T_CP)(P_ASS)

        done = Literal(T_DONE)

        assign = trrelation | birelation | emrelation | varassign | done
        assigns = assign + ZeroOrMore(T_AND + assign)

        return assigns

    def __init_program_parser(self):
        ivalue = Word(nums)
        fvalue = Combine(ivalue + Optional(Literal(T_DOT) + Optional(ivalue)))
        nvalue = fvalue | ivalue
        strname = Word(alphas+nums+T_US)

        parval = (Combine(Literal(T_LT) + Literal(T_VAL) + Word(alphas+nums+T_US) + Literal(T_GT)))(P_PARAM)
        parop = (Literal(T_LT) + Literal(T_OPE) + Word(alphas+nums+T_US) + Literal(T_GT))(P_PARAM)

        varname = (strname)(P_VNAME)

        typeop = (Literal(T_FLO64) | Literal(T_FLO32) | Literal(T_INT8) | Literal(T_INT16) | Literal(T_INT32))(P_TYPEOP)
        sabname = varname + typeop

        operand = nvalue | parval | strname

        signop = Literal(T_SUM) | Literal(T_MIN)
        multop = Literal(T_MUL) | Literal(T_DIV)
        plusop = Literal(T_SUM) | Literal(T_MIN)

        expr = operatorPrecedence( operand,
                                   [(signop, 1, opAssoc.RIGHT),
                                    (multop, 2, opAssoc.LEFT),
                                    (plusop, 2, opAssoc.LEFT),]
        )

        interval = (Word(nums) + T_DOTS + Word(nums))(P_INTER)
        nrange = (nvalue + T_CM + nvalue + T_CM + nvalue)(P_RANGE)
        nlist = (Literal(T_OSB) + Word(nums) + ZeroOrMore(T_CM + Word(nums)) + Literal(T_CSB))(P_LIST)
        addr = (T_OSB + expr + T_CSB)(P_ADDR)

        emptyline = (ZeroOrMore(White(' \t')) + LineEnd())(P_EMPTY)
        comment = (T_COM + restOfLine + LineEnd())(P_COMMENT)

        sab_def = (T_VAR + varname + T_EQ + T_NEW + T_SAB + T_OP + T_CP)(P_SABDEF)

        value = (expr)(P_VALUE)

        ssaddr = (T_CM + expr)(P_ADDR)
        sabstore = (T_ASTORE + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_STORE)
        sabadd = (T_AADD + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_ADD)
        sabxor = (T_AXOR + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_XOR)
        sabexc = (T_AEXC + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_EXC)
        sabor = (T_AOR + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_OR)
        saband = (T_AAND + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_AND)
        sabsub = (T_ASUB + T_OP + sabname + ssaddr + T_CM + value + T_CP)(P_SUB)

        sabaccess = (sabname + addr)(P_ACCESS)
        sabload  = (T_ALOAD + T_OP + sabname + ssaddr + T_CP)(P_LOAD)

        sabread = (sabload | sabaccess | sabadd | sabsub | saband | sabxor | sabor | sabexc)

        sabassign = (sabaccess + T_EQ + value)(P_SABASS)
        printv = (T_PR + T_OP + sabread + T_CP)(P_PRINT)

        threaddef = (T_THREAD + varname + Literal(T_OCB))(P_TRDB)
        closescope = (Literal(T_CCB))(P_CSCOPE)

        floop = (Literal(T_FOR) + T_OP + varname + T_EQ + interval + T_CP + T_OCB)(P_FLOOP)

        op = (Literal(T_BEQ) | Literal(T_GEQ) | Literal(T_LEQ) | Literal(T_LT) | Literal(T_GT))(P_OP)
        oplist = (Literal(T_OSB) + op + ZeroOrMore(T_CM + op) + Literal(T_CSB))(P_LIST)

        lft_val = Group(sabread | expr).setResultsName(P_VLEFT)
        rgt_val = Group(sabread | expr).setResultsName(P_VRIGHT)

        bcond = ((lft_val) + (parop | op) + (rgt_val))(P_BCOND)
        ite = (Literal(T_IF) + T_OP + bcond + T_CP + T_OCB)(P_IF)
        els = (T_CCB + Literal(T_ELSE) + T_OCB)(P_ELSE)

        command = ((sabstore | sabadd | sab_def | sabassign | printv) + T_SEMI) | threaddef | floop | \
                  ite | els | closescope | comment | emptyline

        pardef = (strname + T_EQ + (interval | nrange | nlist | oplist) + T_SEMI)(P_PARAM)
        params = (Literal(T_PARAMS) + Literal(T_OCB))(P_PARAMS)

        return pardef | params | ZeroOrMore(command)

    def program_from_string(self, strinput):
        self.__init__()
        commands = self.__parse_program(strinput)
        program = self.__populate_program(commands)
        return program

    def executions_from_string(self, strinput, program=None):
        (models, done) = self.__parse_executions(strinput)
        executions = self.__populate_executions(models, program, done)

        if program:
            program.expand_events()
            self.__compute_reads_values(executions)

        return executions

    def program_from_execution(self, execution):
        program = Program()
        program.blocks = execution.program.blocks
        ao = execution.get_AO()
        threads = []

        for el in ao.tuples:
            added = False
            for thread in threads:
                if (el[0] in thread[0]) or (el[1] in thread[0]):
                    thread[0].add(el[0])
                    thread[0].add(el[1])
                    thread[1].append(el)
                    added = True
                    break
            if not added:
                thread = [set([]), []]
                thread[0].add(el[0])
                thread[0].add(el[1])
                thread[1].append(el)
                threads.append(thread)

        sevents = set([])

        ind = 1
        for thread in threads:
            rel = Relation("")
            rel.tuples = thread[1]
            lstev = self.__list_from_relation(rel)
            if len([x for x in thread[0] if x.is_init()]) > 0:
                name = "main"
            else:
                name = "t%s"%ind
                ind += 1
            n_thread = Thread(name)
            n_thread.events = lstev
            program.add_thread(n_thread)
            sevents = sevents | set(thread[0])

        for ev in execution.program.get_events():
            if ev.is_init():
                continue
            if (len(sevents) == 0) or (ev not in sevents):
                n_thread = Thread("t%s"%ind)
                ind += 1
                n_thread.append(ev)
                program.add_thread(n_thread)

        return program

    def __list_from_relation(self, relation):
        events = {}
        for tup in relation.tuples:
            if not tup[0] in events: events[tup[0]] = 0
            if not tup[1] in events: events[tup[1]] = 0
            events[tup[0]] += 1

        events = [(events[x], x) for x in events]
        events.sort()
        events.reverse()
        events = [x[1] for x in events]
        return events

    def __compute_reads_values(self, executions):
        for exe in executions.executions:
            events = exe.get_events()
            ev_map = dict((x.name, x) for x in events)

            read_evs = [x for x in events if x.is_modify()]

            rbf_map = dict(((x[0], int(x[2])), x[1]) for x in exe.get_RBF().tuples)
            for read_event in read_evs:
                values = []
                for i in read_event.address:
                    write_event = ev_map[rbf_map[(read_event.name, i)]]
                    value = write_event.get_values()[i]
                    values.append(value)
                new_read_event = copy.deepcopy(read_event)
                new_read_event.set_values(values)
                exe.add_read_values(new_read_event)

            mod_map = dict((x.name, x) for x in exe.reads_values)
            read_evs = [x for x in events if x.is_read()]

            rbf_map = dict(((x[0], int(x[2])), x[1]) for x in exe.get_RBF().tuples)
            for read_event in read_evs:
                values = []
                for i in read_event.address:
                    write_event = ev_map[rbf_map[(read_event.name, i)]]
                    if write_event.is_modify():
                        is_float = False

                        if write_event.is_add() or write_event.is_sub() or write_event.is_exchange():
                            is_float = write_event.is_wtear()

                        updated_values = self.__op_values(is_float, \
                                                          mod_map[write_event.name], \
                                                          write_event, \
                                                          write_event.get_operator_fun())
                        value = updated_values[i]
                    else:
                        value = write_event.get_values()[i]
                    values.append(value)
                new_read_event = copy.deepcopy(read_event)
                new_read_event.set_values(values)
                exe.add_read_values(new_read_event)

    def __op_values(self, is_float, ev1, ev2, fun):
        val2 = ev2.values
        val1 = ev1.values

        begin = ev1.offset
        end = len(val2)
        if is_float:
            val2 = float_from_values(val2[ev1.offset:])
            val1 = float_from_values(ev1.values)
        else:
            val2 = int_from_values(val2[ev1.offset:])
            val1 = int_from_values(ev1.values)

        return values_from_int(fun(val1,val2), begin, end)

    def __parse_executions(self, strinput):
        models = []
        done = False
        for line in strinput.split(T_NL):
            if line == "": continue
            if T_DONE in line:
                done = True
                continue
            rels = []
            for relation in line.split(T_AND):
                if relation == "": continue
                rels.append(self.execution_parser.parseString(relation))
            models.append(rels)

        return (models, done)

    def __populate_executions(self, models, program, done):
        executions = Executions()
        executions.allexecs = done
        executions.program = program
        for model in models:
            execution = Execution()
            for assign in model:
                if assign.getName() == P_ASS:
                    variable = assign[1]
                    value = assign[3]
                    execution.add_condition(variable, value)
                else:
                    rel = Relation(assign[0])
                    size = 9 if assign.getName() == P_TRREL else \
                           6 if assign.getName() == P_BIREL else 0
                    if size == 0: continue
                    for i in range(3, len(assign), size):
                        rel.add_tuple(self.__get_tuple(size, assign[i:i+size]))

                    self.__add_relation(execution, rel)
            executions.add_execution(execution)

        return executions

    def __add_relation(self, exe, rel):

        if rel.name == HB:
            exe.set_HB(rel)
        elif rel.name == MO:
            exe.set_MO(rel)
        elif rel.name == RBF:
            exe.set_RBF(rel)
        elif rel.name == RF:
            exe.set_RF(rel)
        elif rel.name == SW:
            exe.set_SW(rel)
        else:
            raise UnreachableCodeException()

    def __get_tuple(self, size, tup):
        if size == 6:
            ev1 = tup[1]
            ev2 = tup[3]
            return Relation.get_bi_tuple(ev1, ev2)
        if size == 9:
            ev1 = tup[1]
            ev2 = tup[3]
            addr = tup[6]
            return Relation.get_tr_tuple(ev1, ev2, addr)

        raise UnreachableCodeException()

    def __parse_program(self, strinput):
        commands = []
        for line in strinput.split(T_NL):
            try:
                pline = self.program_parser.parseString(line, parseAll=True)
            except ParseException:
                if self.DEBUG: raise
                raise ParsingErrorException("ERROR (L%s): unhandled command \"%s\""%(len(commands)+1, line.strip()))
            commands.append(pline)

        return commands

    def __get_var_size(self, typeop):
        if typeop == T_INT8:
            return 1
        elif typeop == T_INT16:
            return 2
        elif typeop == T_INT32:
            return 4
        elif typeop == T_FLO32:
            return 4
        elif typeop == T_FLO64:
            return 8
        else:
            raise UnreachableCodeException("Type %s not valid"%str(typeop))

    def __var_type_is_int(self, typeop):
        if typeop in [T_INT8, T_INT16, T_INT32]:
            return True
        return False

    def __var_type_is_float(self, typeop):
        if typeop in [T_FLO32, T_FLO64]:
            return True
        return False

    def __gen_memory_event(self, command, ctype, parametric, thread, blocks):

        ordering = None
        operation = None
        operator = None

        optypes = dict([(P_STORE, (SC, WRITE, None)), \
                        (P_LOAD, (SC, READ, None)), \
                        (P_ACCESS, (UNORD, READ, None)), \
                        (P_SABASS, (UNORD, WRITE, None)), \
                        (P_ADD, (SC, MODIFY, ADD)), \
                        (P_SUB, (SC, MODIFY, SUB)), \
                        (P_AND, (SC, MODIFY, AND)), \
                        (P_XOR, (SC, MODIFY, XOR)), \
                        (P_OR, (SC, MODIFY, OR)), \
                        (P_EXC, (SC, MODIFY, EXC))])

        if ctype not in optypes:
            raise UnreachableCodeException("Type \"%s\" is invalid"%ctype)

        ordering, operation, operator = optypes[ctype]

        block_name = command.varname
        opsize = self.__get_var_size(command.typeop)
        varsize = None

        address = None
        offset = None

        try:
            addr = int(command.address[1])
            baddr = opsize*addr
            eaddr = (opsize*(addr+1))-1
            address = range(baddr, eaddr+1, 1)
            varsize = eaddr+1
        except Exception:
            pass

        if parametric:
            offset = list(command.address.asList())[1:]
            if ctype == P_SABASS:
                offset = offset[:-1]
            offset = "".join(offset[0])

        me = Memory_Event()

        me.name = "%s_%s_%s"%(Memory_Event.get_unique_name(), operation, thread.name)
        me.ordering = SC if self.__var_type_is_float(command.typeop) else ordering
        me.tear = WTEAR if self.__var_type_is_float(command.typeop) else NTEAR
        me.operation = operation
        me.operator = operator
        me.address = address

        if block_name not in blocks:
            raise ParsingErrorException("block \"%s\" is not defined"%(block_name))
        me.block = blocks[block_name]

        blocks[block_name].update_size(varsize)

        for check_addr in [offset, address]:
            if check_addr:
                if T_VAL in str(check_addr):
                    raise ParsingErrorException("Parametric address definition is not supported")

        value = command.value

        if parametric:
            me.size = opsize
            me.value = list(value)
            me.offset = offset
            me.tear = WTEAR if self.__var_type_is_float(command.typeop) else NTEAR
        elif me.is_write_or_modify():
            try:
                if self.__var_type_is_float(command.typeop):
                    me.set_values_from_float(float("".join(value)), baddr, eaddr)
                if self.__var_type_is_int(command.typeop):
                    me.set_values_from_int(int("".join(value)), baddr, eaddr)
            except:
                if self.DEBUG: raise
                raise ParsingErrorException("value \"%s\" cannot be encoded into %s bytes"%("".join(value), varsize))
        else:
            pass

        return me

    def __populate_program(self, commands):
        program = Program()
        thread = Thread(MAIN)
        program.add_thread(thread)
        floop = None
        params = False
        used_params = []
        defined_params = {}
        ite = None
        blocks = {}
        sab_defs = []
        linenum = 0
        name = ""
        while len(commands):
            linenum += 1
            command = commands[0]
            commands = commands[1:]
            command_name = command.getName()

            if command_name == P_TRDB:
                thread_name = command[1]
                if thread:
                    program.add_thread(thread)
                if thread_name in [x.name for x in program.threads]:
                    raise ParsingErrorException("ERROR (L%s): thread \"%s\" already defined"%(linenum, thread_name))
                thread = Thread(thread_name)

            elif command_name == P_SABDEF:
                block_name = command.varname
                block = Block(block_name)
                blocks[block_name] = block

                name = "%s_%s_%s"%(Memory_Event.get_unique_name(), WRITE, thread.name)
                me = Memory_Event()

                me.name = name
                me.operation = WRITE
                me.tear = NTEAR
                me.ordering = INIT
                me.block = block

                # Added to the SAB definitions since the size of the
                # write will depend on the other writes and reads
                sab_defs.append(me)
                thread.append(me)

            elif command_name in [P_STORE, P_SABASS, P_ACCESS, P_LOAD, P_ADD, P_SUB, P_AND, P_XOR, P_OR, P_EXC]:
                block_name = command.varname

                if block_name not in blocks:
                    raise ParsingErrorException("ERROR (L%s): SAB \"%s\" not defined"%(linenum, block_name))

                if self.__var_type_is_float(command.typeop) and (command_name not in [P_SABASS, P_ACCESS]):
                    raise ParsingErrorException("ERROR (L%s): Atomic operations not support the float type"%linenum)

                param = False

                if type(command.value) == str:
                    command.value = [command.value]

                for i in range(len(command.value)):
                    if T_VAL in command.value[i]:
                        param = True
                        pname = command.value[i][1:-1]
                        command.value[i] = pname
                        used_params.append(pname)

                if floop:
                    param = True

                try:
                    me = self.__gen_memory_event(command, command_name, param, thread, blocks)
                except ParsingErrorException as e:
                    if self.DEBUG: raise
                    raise ParsingErrorException("ERROR (L%s): %s"%(linenum, str(e)))

                if floop:
                    floop.append(me)
                elif ite:
                    if ite.has_else():
                        ite.append_else(me)
                    else:
                        ite.append_then(me)
                else:
                    thread.append(me)

            elif command_name == P_PRINT:

                reads = [x for x in [command.access, \
                                     command.load, \
                                     command.oadd, \
                                     command.osub, \
                                     command.oand, \
                                     command.oxor, \
                                     command.oor, \
                                     command.oexchange] \
                         if x]

                if len(reads) < 1:
                    raise ParsingErrorException("ERROR (L%s): cannot print command \"%s\""%(linenum, "".join(command)))

                commands.insert(0, reads[0])

            elif command_name == P_CSCOPE:
                if floop:
                    floop.get_uevents()
                    thread.append(floop)
                    floop = None
                elif ite:
                    thread.append(ite)
                    ite = None
                elif params:
                    params = False
                    diff = set(used_params) - set([x for x in defined_params])
                    if len(diff):
                        raise ParsingErrorException("ERROR: parameters not defined \"%s\""%(", ".join(list(diff))))

                    for par in used_params:
                        program.add_param(par, defined_params[par])

                else:
                    if thread not in program.threads:
                        program.add_thread(thread)

            elif command_name == P_FLOOP:
                if ite or floop:
                    raise ParsingErrorException("ERROR (L%s): nested ifs or for-loops are not supported"%(linenum))

                floop = For_Loop()
                frind = int(command.interval[0])
                toind = int(command.interval[2])
                cname = command.varname
                floop.set_values(cname, frind, toind)

            elif command_name == P_IF:
                if ite or floop:
                    raise ParsingErrorException("ERROR (L%s): nested ifs or for-loops are not supported"%(linenum))

                ite = ITE_Statement()
                condition = command.bcond
                op = None

                if P_PARAM in dict(command):
                    command.param = "".join(command.param[1:3])
                    command.operator = command.param
                    used_params.append(command.param)

                ldict = dict(condition.vleft)
                rdict = dict(condition.vright)

                lval = None
                rval = None

                try:
                    if len([k for k in ldict if k in (P_LOAD, P_ACCESS)]):
                        op = P_LOAD if P_LOAD in ldict else P_ACCESS
                        lval = self.__gen_memory_event(condition.vleft, op, False, thread, blocks)
                    else:
                        lval = "".join(condition.vleft)
                        if P_PARAM in ldict:
                            lval = lval[1:-1]
                            used_params.append(lval)

                    if len([k for k in rdict if k in (P_LOAD, P_ACCESS)]):
                        op = P_LOAD if P_LOAD in rdict else P_ACCESS
                        rval = self.__gen_memory_event(condition.vright, op, False, thread, blocks)
                    else:
                        rval = "".join(condition.vright)
                        if P_PARAM in rdict:
                            rval = rval[1:-1]
                            used_params.append(rval)

                except ParsingErrorException as e:
                    if self.DEBUG: raise
                    raise ParsingErrorException("ERROR (L%s): %s"%(linenum, str(e)))

                ite.append_condition(lval, command.operator, rval)

            elif command_name == P_ELSE:
                ite.else_events = []

            elif command_name == P_PARAM:
                params = True

                if P_INTER in dict(command):
                    values = range(int(command.interval[0]), int(command.interval[2])+1, 1)
                elif P_RANGE in dict(command):
                    start = float(command.range[0])
                    end = float(command.range[2])
                    step = float(command.range[4])
                    values = [float(x) for x in numpy.arange(start, end+step, step) if x <=end]
                elif P_LIST in dict(command):
                    values = [x for x in command.list[1:-1] if x != T_CM]

                defined_params[command.param[0]] = values

            elif command_name == P_PARAMS:
                pass

            elif (command_name == P_COMMENT) or (command_name == P_EMPTY):
                pass
            else:
                raise ParsingErrorException("ERROR (L%s): unhandled command \"%s\" (%s)"%(linenum, " ".join(command), name))

        for sdef in sab_defs:
            sdef.set_init_values()

        return program

max_stars_repo_name: FMJS/emme

max_issues_repo_name: FMJS/emme

max_forks_repo_name: FMJS/emme


text:
import numpy as np

def threshold(coords, min_, max_):
    return np.maximum(np.minimum(coords, max_), min_)

def clip_boxes(boxes, im_shape):
    """
    Clip boxes to image boundaries.
    """
    boxes[:, 0::2]=threshold(boxes[:, 0::2], 0, im_shape[1]-1)
    boxes[:, 1::2]=threshold(boxes[:, 1::2], 0, im_shape[0]-1)
    return boxes

class Graph:
    def __init__(self, graph):
        self.graph=graph

    def sub_graphs_connected(self):
        sub_graphs=[]
        for index in range(self.graph.shape[0]):
            if not self.graph[:, index].any() and self.graph[index, :].any():
                v=index
                sub_graphs.append([v])
                while self.graph[v, :].any():
                    v=np.where(self.graph[v, :])[0][0]
                    sub_graphs[-1].append(v)
        return sub_graphs


text:
# /usr/bin/python

# PYTHON SCRIPT TO ADD FULL PRECISION TO A GRO FILE GIVEN A TINKER XYZ FILE
# param x - The name of the .gro file
# param n - The scaling factor to apply to each xyz coordinate

import numpy as np
from optparse import OptionParser

# =============================================================================================
# READ IN USER INPUTS
# =============================================================================================
parser = OptionParser()
parser.add_option('-g', dest='grofile', help='Input gro file', default='frame.gro')
parser.add_option('-n', dest='num', help='scaling factor', default='0.1')
(options, args) = parser.parse_args()
scaling_factor = float(options.num)
# Create the space buffer
spaces = []
spaces.append('     ')
spaces.append('  ')
spaces.append('     ')
spaces.append('    ')
spaces.append('    ')
spaces.append('     ')
spaces.append('    ')
spaces.append('    ')
spaces.append('    ')
rjustvect = np.ones(10, int)
rjustvect = ['8', '7', '5', '15', '15', '15', '6', '6', '6']

fname = options.grofile

infile = open(fname, 'r')
lines = infile.readlines()
infile.close()

outfile = open(fname, 'w')

linecount = 1
for line in lines:
    if linecount < 3 or linecount == len(lines):
        outfile.write('    ' + line)
    else:
        tokens = line.split()
        linestring=""
        for i, token in enumerate(tokens):
            if i == 3 or i == 4 or i == 5:
                token = str("%.8f" % round(float(token) * scaling_factor, 8))
            linestring = linestring + str(token).rjust(int(rjustvect[i]))
        outfile.write(linestring + '\n')
    linecount = linecount + 1
outfile.close()


text:
import numpy as np
import matplotlib.pyplot as plt
import os

path = "0.17_restitution_200_healthy_pacemaker__100000.txt"

filenames = []

for f in os.listdir(os.getcwd()):
    if not f.startswith('.'):
        filenames.append( "/" + f )

horizontal = 100
L = 200 #system size
total_time = 100000.
# = os.listdir(path)
runs = 4
num_nu = np.size(filenames)/runs
nu_min = 0.1
nu_max = .22

fraction = np.zeros((num_nu,runs))

cells = np.zeros((200,200))

j = 0
fib_time = np.zeros(np.size(filenames))
full = np.zeros((2,40))

data = np.genfromtxt(os.getcwd()+"/average_time_in_af.txt")
data1 = np.genfromtxt(os.getcwd()+"/average_time_in_af_res1.txt")
"""np.insert(data,0,np.zeros(8))
np.insert(data,-1,np.zeros(8))
np.insert(data1,0,np.zeros(8))
np.insert(data1,-1,np.zeros(8))"""

np.append(data,np.zeros(3))

nu = np.linspace(.09,.22,13)
plt.figure()
plt.plot(data,"o",label = "no restitution")
plt.plot(data1,"o", label = "moderate restitution")
plt.legend()
plt.xlabel("nu")
plt.ylabel("average duration of AF")
#plt.title( "Fracion of time spent in excited regime for nu = " )
plt.grid()
plt.show()
#LOOK FOR:
#average duration of episode (counter)
#P risk: add column and divide by time (length of array)"""


text:
"""
Preprocessing functions for images, labels
"""

#from torch.nn.functional import pad
from typing import Tuple, Union

import numpy as np
import torch
import torchvision
from skimage.transform import resize
from torch import Tensor, from_numpy

def normalize(x:'Union[torch.Tensor,np.ndarray]') -> 'Union[torch.Tensor,np.ndarray]':
    """Min-max normalization (0-1):

    Args:
      x('Union[torch.Tensor,np.ndarray]'): Can be tensor or ndarray to be converted

    Returns:
      Union[Tensor,ndarray]: * Return same type as input but scaled between 0 - 1

    Raises:

    """
    return (x - x.min())/(x.max()-x.min())

def batchisize_to_5D(x:'Union[torch.Tensor,np.ndarray]') -> 'Union[torch.Tensor,np.ndarray]':
    """Takes an input with lower dimensions than 5 and pad the dimensions to 5. len(x.shape) < 5 -> len(x.shape) == 5

    Args:
      x('Union[torch.Tensor,np.ndarray]'): Input vector

    Returns:
      Union[torch.Tensor,np.ndarray]: x with len(x.shape) == 5

    Raises:

    """
    if isinstance(type(x), Tensor):
        return x.expand((*[1]*(5-len(x.shape)),*[-1]*len(x.shape)))
    else:
        return np.expand_dims(x,[i for i,_ in enumerate(range(5-len(x.shape)))])

def folder2labels(images:list, classes:dict) -> 'np.ndarray': #, delimiter:str
    """Extract labels from filename based on delimiter.
    Expects a label before filename

    Example: /AD/image_file.png

    Args:
      images(list): list containing all filenames
      classes(dict): The allowed classes that can be used for labels

    Returns:
      np.ndarray: A list of labels based on the given classes from the image filenames.

    Raises:

    """
    #if delimiter not in images[0]: raise ValueError(f"The defined delimiter could not be found in image input! Image input: {images[0]}")
    return np.array([classes[img_path.rsplit("/",2)[1]] for img_path in images])#np.array([classes[img_path.rsplit("/",1)[1].split(delimiter,1)[0]] for img_path in images])

def mask_threshold(image:'np.ndarray', threshold:float) -> None:
    """Place a threshold on image and remove all values that are below the threshold.
    All values under the threshold is set to 0

    Args:
      image('np.ndarray'): Input image that threshold should be applied to.
      threshold(float): Value between 0-1 at which procent of the max value that should be taken as a mask

    Returns:

    Raises:

    """
    img_threshold = image.max()*threshold
    image[image < img_threshold] = 0
    return image #np.ma.masked_where(image <= img_threshold,image)

def set_outside_of_scan_pixels_2_zero(image:'np.ndarray', mask:'np.ndarray') -> 'np.ndarray':
    """Everything that is zero outside of image is set to zero on the mask. Returns the modified mask

    Args:
      image('np.ndarray'):
      mask('np.ndarray'):

    Returns:

    Raises:

    """
    return np.ma.masked_where(image == 0,mask)

def image2axial(image:'np.ndarray') -> 'np.ndarray':
    """Modify image to display axial view. Expects an image of (B,H,W) or (B,C,H,W)

    Args:
      image('np.ndarray'):

    Returns:

    Raises:

    """
    if not isinstance(image, np.ndarray): raise TypeError(f"Expected 'np.ndarray'. Got: {type(image)}")
    if len(image.shape) == 3:
        return np.flip(image.transpose(2,1,0),axis=1).copy()
    #elif len(image.shape) == 4:
    #    return np.flip(image.transpose(3,1,2,0))
    else:
        raise ValueError(f"Expected length of 3 or 4. Got: {len(image.shape)}")

def tensor2numpy(data:'torch.Tensor') -> 'torch.Tensor':
    """Send to CPU. If computational graph is connected then detach it as well.

    Args:
      data('torch.Tensor'):

    Returns:

    Raises:

    """
    if data.requires_grad:
        return data.detach().cpu().numpy()
    else:
        return data.cpu().numpy()

def uint8(image:'np.ndarray')->'np.ndarray':
    """Convert to uint8

    Args:
      image('np.ndarray'):

    Returns:

    Raises:

    """
    # Change range 0-1 to 0-255 and change type to uint8
    return (image*255).astype(np.uint8)

def preprocess_image(image:'np.ndarray', input_shape:Tuple=(79,95,79), normalized=True) -> 'torch.Tensor':
    """Resize, normalize between 0-1 and convert to uint8

    Args:
      image('np.ndarray'):
      input_shape(Tuple, optional): (Default value = (79,95,79))
      normalized: (Default value = True)

    Returns:

    Raises:

    """

    if input_shape != image.shape:
        image = resize(image,input_shape)

    if normalized:
        image = normalize(image)

    return image

def to_grid(image:'np.ndarray', max_num_slices:int=None,pad_value:float=0.5, nrow:int=10, padding:int=2) -> 'torch.Tensor':
    """Create grid from image based on maximum number of slices.

    Args:
      image('np.ndarray'): Image with multiple slices of shape Tuple[D,H,W]
      max_num_slices(int, optional): The number of slices the image should be reduced. (Default value = None)
      pad_value(float, optional): (Default value = 0.5)
      nrow(int, optional): (Default value = 10)
      padding(int, optional): (Default value = 2)

    Returns:
      'torch.Tensor': A grid image with reduced number of slices.

    Raises:

    """
    assert len(image.shape) == 3

    image=normalize(image)

    if max_num_slices != None:
        image = np.stack([np.mean(x,axis=0) for x in np.array_split(image, max_num_slices)]) #greedy_split(image,max_num_slices)

    plt_image = from_numpy(image).float().unsqueeze(1)

    # Convert to grid
    grid_image = torchvision.utils.make_grid(plt_image, nrow=nrow,pad_value=pad_value,padding=2)[0]

    return grid_image