text:
import process_sequence_fasta as pro_seq_fasta
import sequence2vector as s2v_tools
import numpy as np
import pickle
from sklearn.model_selection import train_test_split
from keras.layers import LSTM, Dense
from keras.models import Sequential
from keras.layers import Embedding
from keras.preprocessing import sequence

"""
Author: Carlos Garcia-Perez
Date: 25.06.2019 final version of model setting.
                 add new option to train all data set no splitting.
                 save the model and the weight separated
      17.06.2019 fix the model definition
      14.06.2019 split data set in train, validation and test sets in option 1
      13.06.2019 create data set in one-hot-encoding and save as object.pkl in option 2
                 first version of the script
"""
opt = 2

type = 'nuc' # aa

if opt == 1:
    print('processing all...')
    x_data_name = '/data/sequence_dataset.pkl'
    y_data_name = '/data/label_dataset.pkl'

    X = pickle.load(open(x_data_name, 'rb'))
    Y = pickle.load(open(y_data_name, 'rb'))

    classes = pickle.load(open("/data/classes.pkl", 'rb'))

    print('defining model:')

    features = 20
    num_classes = classes

    print('features: ', features)
    print('clases: ', num_classes)
    print('nodes: ', 128)
    print('bacth size: ', 2000)
    print('epochs: ', 50)

    print('reshaping data...')
    max_len = max([len(s) for s in X])
    X_train = sequence.pad_sequences(X, maxlen=max_len)

    print('training dataset: ', X_train.shape)
    print('max_length:', max_len)

    model = Sequential()
    model.add(Embedding(len(X_train), features, input_length=max_len))
    model.add(LSTM(128))  # 32
    model.add(Dense(num_classes, activation='softmax'))

    print('compiling the model...')
    # compile the model
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    print('training the model...')
    # metric
    model.fit(X_train, Y,
              epochs=50,
              batch_size=2000)

    results_eval = model.evaluate(X_train, Y, batch_size=2000)

    print("%s: %.2f%%" % (model.metrics_names[1], results_eval[1] * 100))

    pickle.dump(results_eval, open("/data/results_eval.pkl", 'wb'), protocol=4)

    # serialize model to JSON
    model_json = model.to_json()
    with open("/data/model.json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights("/data/model.h5")
    print("Saved model to disk...")

elif opt == 2:
    print('loading data...')
    info = pickle.load(open("/data/info.pkl", 'rb'))
    X_train = pickle.load(open("/data/x_train.pkl", 'rb'))
    #X_test = pickle.load(open("/data/x_test.pkl", 'rb'))

    Y_train = pickle.load(open("/data/y_train.pkl", 'rb'))
    #Y_test = pickle.load(open("/data/y_test.pkl", 'rb'))

    print('defining model:')

    features = 20
    num_classes = info[0]
    max_len = info[1]
    ly = 128 # layer
    btch_size = 250
    epch = 50

    print('features: ', features)
    print('clases: ', num_classes)
    print('layer nodes: ', ly) # 128
    print('bacth size: ', btch_size) # 2000
    print('epochs: ', epch)

    print('reshaping data...')

    X_train = sequence.pad_sequences(X_train, maxlen=max_len)
    #X_test = sequence.pad_sequences(X_test, maxlen=max_len)

    print('training dataset: ', X_train.shape)
    #print('test dataset: ', X_test.shape)
    print('max_length', max_len)

    model = Sequential()
    model.add(Embedding(len(X_train), features, input_length=max_len))
    model.add(LSTM(ly))  # 128
    model.add(Dense(num_classes, activation='softmax'))

    print('compiling the model...')
    # compile the model
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    print('training the model...')
    # metric
    network = model.fit(X_train, Y_train,
                        epochs=epch,
                        batch_size=btch_size,
                        validation_split=0.2) # 0.2

    X_test = pickle.load(open("/data/x_test.pkl", 'rb'))
    Y_test = pickle.load(open("/data/y_test.pkl", 'rb'))

    X_test = sequence.pad_sequences(X_test, maxlen=max_len)

    results_eval = model.evaluate(X_test, Y_test, batch_size=btch_size)

    print('training the model... done!!!')
    print('savinig the history...')
    pickle.dump(network, open("/data/history.pkl", 'wb'), protocol=4)
    pickle.dump(results_eval, open("/data/results_eval.pkl", 'wb'), protocol=4)
    print('done...')

elif opt == 3:
    x_data_name = '/data/sequence_dataset.pkl'
    y_data_name = '/data/label_dataset.pkl'

    X = pickle.load(open(x_data_name, 'rb'))
    Y = pickle.load(open(y_data_name, 'rb'))

    # spliting data into train and test sets
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)

    # saving the train, validation and test datasets
    pickle.dump(X_train, open("/data/x_train.pkl", 'wb'), protocol=4)
    pickle.dump(X_test, open("/data/x_test.pkl", 'wb'), protocol=4)
    pickle.dump(Y_train, open("/data/y_train.pkl", 'wb'), protocol=4)
    pickle.dump(Y_test, open("/data/y_test.pkl", 'wb'), protocol=4)

elif opt == 4:

    fname = '/data/subdataset_RDP_nucl.fasta'

    sequence_df = pro_seq_fasta.process_fasta(fname, type)

    Y = np.array(sequence_df['bacteria'])
    X = np.array(sequence_df['sequence'])

    max_len = max([len(s) for s in X])
    classes = len(np.unique(Y))
    info = (classes, max_len)

    Y = s2v_tools.label2one_hot_encoding(Y)

    pickle.dump(X, open("/data/sequence_dataset.pkl", 'wb'), protocol=4)
    pickle.dump(Y, open("/data/label_dataset.pkl", 'wb'), protocol=4)
    pickle.dump(info, open("/data/info.pkl", 'wb'), protocol=4)

ext: py

reason: import numpy


text:
import cv2
import numpy as np
from copy import deepcopy
from numba import jit

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

class Visualization(object):
    __vehicle = np.array([])
    __vehicle_size = np.array([], dtype=np.int)
    __vehicle_img_size = np.array([], dtype=np.int)
    __vehicle_img_centre = np.array([], dtype=np.int)

    __bg = np.array([])
    __bg_size = np.array([], dtype=np.int)
    __bg_img_size = np.array([], dtype=np.int)
    __bg_img_centre = np.array([], dtype=np.int)

    __thickness = np.array([], dtype=np.int)
    __r = int()

    __nv_color = (1, 1, 1)
    __iv_color = (1, 0, 1)
    __bg_color = (0.2, 0.2, 0.2)

    __title = ""

    def __init__(self, car_size=(15, 6), car_img_size=(64, 64, 3), bg_size=(640, 640), loop_radius=256,
                 width=16, title="The Elephant Racing\n\n->[ENTER TO PLAY]  \n[Setting]\n[Help?]"):

        self.__vehicle_size = np.array(car_size, dtype=np.int)

        cig = (48, 48, 3)
        if car_img_size[0] <= 48:
            self.__vehicle_img_size = np.array(cig, dtype=np.int)
            self.__vehicle_img_centre = np.array(np.array(cig[0:2]) / 2, dtype=np.int)
        else:
            self.__vehicle_img_size = np.array(car_img_size, dtype=np.int)
            self.__vehicle_img_centre = np.array(np.array(car_img_size[0:2]) / 2, dtype=np.int)
            pass

        self.__bg_size = np.array(bg_size, dtype=np.int)
        self.__bg_img_size = np.array([bg_size[0], bg_size[1], 3], dtype=np.int)
        self.__bg_img_centre = np.array(np.array(bg_size) / 2, dtype=np.int)

        self.__r = loop_radius
        self.__thickness = width

        self.__vehicle = np.zeros(self.__vehicle_img_size)
        self.__bg = np.zeros(self.__bg_img_size)

        self.__title = title

        self.__init_graph(self.__bg_color, self.__title)
        pass

    # @jit
    def __draw_car(self, theta: np.array([]), vehicle_type=np.array([])):
        have_color = len(vehicle_type) != 0
        img = deepcopy(self.__bg)
        mask = np.zeros(self.__bg_img_size)
        for i in range(0, len(theta)):
            t = theta[i]
            h = int(320 - np.cos(np.pi * theta[i] / 180) * self.__r)
            w = int(320 + np.sin(np.pi * theta[i] / 180) * self.__r)
            a = 360 - t

            vehicle = deepcopy(self.__vehicle)
            r_car = cv2.getRotationMatrix2D(tuple(self.__vehicle_img_centre), a, 1)
            vehicle = cv2.warpAffine(vehicle, r_car, tuple(self.__vehicle_img_size[0:2]))

            h0 = h - self.__vehicle_img_centre[0]
            h1 = h + self.__vehicle_img_centre[0]
            w0 = w - self.__vehicle_img_centre[1]
            w1 = w + self.__vehicle_img_centre[1]

            if have_color:
                if vehicle_type[i] == 0:
                    pass
                elif vehicle_type[i] == 1:
                    vehicle[:, :, 0] = vehicle[:, :, 0] * self.__iv_color[0]
                    vehicle[:, :, 1] = vehicle[:, :, 1] * self.__iv_color[1]
                    vehicle[:, :, 2] = vehicle[:, :, 2] * self.__iv_color[2]
            else:
                pass

            mask[h0:h1, w0:w1, :] = mask[h0:h1, w0:w1, :] + vehicle[:, :, :]
            pass

        temp = np.sign(np.sum(mask, 2))
        temp = np.abs(temp - 1)

        img[:, :, 0] = img[:, :, 0] * temp
        img[:, :, 1] = img[:, :, 1] * temp
        img[:, :, 2] = img[:, :, 2] * temp
        img = img + mask
        return img

    # @jit
    def __draw_tags(self, img, theta: np.array([])):
        img = deepcopy(img)
        mask = np.zeros(self.__bg_img_size)
        tag = np.zeros(self.__vehicle.shape)
        cv2.rectangle(tag, (0, 0), (18, 18), (1, 1, 1), -1)
        cv2.rectangle(tag, (0, 0), (18, 18), (1, 0, 0), 1)
        for j in range(0, len(theta)):
            tag_ = deepcopy(tag)
            t = theta[j]
            h = int(320 - np.cos(np.pi * theta[j] / 180) * self.__r)
            w = int(320 + np.sin(np.pi * theta[j] / 180) * self.__r)
            a = 360 - t

            re = (cv2.getTextSize(str(j+1), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1))
            org = np.array([9-re[0][0]/2, 9+re[0][1]/2], dtype=np.int)

            cv2.putText(tag_, str(j+1), tuple(org), cv2.FONT_HERSHEY_PLAIN, 1, (1, 0, 0), 1, cv2.LINE_AA)
            tg = tag.sum(2) == 0

            h0 = h - self.__vehicle_img_centre[0]
            h1 = h + self.__vehicle_img_centre[0]
            w0 = w - self.__vehicle_img_centre[1]
            w1 = w + self.__vehicle_img_centre[1]

            mask[h0:h1, w0:w1, 0] = mask[h0:h1, w0:w1, 0] * tg
            mask[h0:h1, w0:w1, 1] = mask[h0:h1, w0:w1, 1] * tg
            mask[h0:h1, w0:w1, 2] = mask[h0:h1, w0:w1, 2] * tg
            mask[h0:h1, w0:w1, :] = mask[h0:h1, w0:w1, :] + tag_[:, :, :]
            pass

        temp = np.sign(np.sum(mask, 2))
        temp = np.abs(temp - 1)

        img[:, :, 0] = img[:, :, 0] * temp
        img[:, :, 1] = img[:, :, 1] * temp
        img[:, :, 2] = img[:, :, 2] * temp
        img = img + mask
        return img

    def __draw_stat(self):
        pass

    def __draw_shell(self):
        pass

    # @jit
    def __init_graph(self, bg_color, title):
        cv2.circle(self.__bg, tuple(self.__bg_img_centre), self.__r, bg_color, self.__thickness, cv2.LINE_AA)
        cv2.circle(self.__bg, tuple(self.__bg_img_centre), self.__r, (0.5, 0.5, 0.5), int(self.__thickness/25), cv2.LINE_8)
        self.__bg = self.__beautification(self.__bg)

        p0 = tuple(np.array(self.__vehicle_img_centre[0:2] - self.__vehicle_size / 2, dtype=np.int))
        p1 = tuple(np.array(self.__vehicle_img_centre[0:2] + self.__vehicle_size / 2, dtype=np.int))
        cv2.rectangle(self.__vehicle, p0, p1, self.__nv_color, -1, cv2.LINE_AA)

        pts = np.array([
            tuple([
                self.__vehicle_img_centre[0] + self.__vehicle_size[0] / 2,
                self.__vehicle_img_centre[1] - self.__vehicle_size[1] / 2
            ]),
            tuple([
                self.__vehicle_img_centre[0] + self.__vehicle_size[0] / 2 + self.__vehicle_size[1] / 3,
                self.__vehicle_img_centre[1]
            ]),
            tuple([
                self.__vehicle_img_centre[0] + self.__vehicle_size[0] / 2,
                self.__vehicle_img_centre[1] + self.__vehicle_size[1] / 2
            ])
        ], dtype=np.int)
        cv2.fillConvexPoly(self.__vehicle, pts, (1, 0, 0), cv2.LINE_AA)
        self.__put_title(title)
        pass

    # @jit
    def __beautification(self, img: np.array([])):
        img0 = deepcopy(img)
        lawn = cv2.imread("material/lawn.jpg")
        lawn = cv2.resize(lawn, tuple(self.__bg_img_size[0:2]))
        mask = img0.sum(2)
        mask = mask <= 0
        mask = np.array(mask, dtype=np.float)
        for i in range(3):
            img0[:, :, i] = lawn[:, :, i] * mask / 255 + img0[:, :, i]
            pass
        return img0

    # @jit
    def __put_title(self, title: str):
        line_spacing = 2
        font_scale = 0.9
        thickness = 1
        tts = title.split('\n')
        size = []
        for t in tts:
            text_size = np.array(cv2.getTextSize(t, cv2.FONT_HERSHEY_DUPLEX, font_scale, thickness))
            text_size[0] = np.array(text_size[0])
            text_size[0][1] = text_size[0][1] + text_size[1]
            size.append(tuple(text_size[0]))
            pass
        meta_high = size[0][1]
        total_centre_high = int((len(tts) - 1) * meta_high * line_spacing)
        for i in range(0, len(tts)):
            cv2.putText(self.__bg, tts[i],
                        tuple(np.array(self.__bg_img_centre
                                       - np.array([0, total_centre_high/2-i*meta_high*line_spacing])
                                       - np.array([size[i][0]/2, 0]),
                                       dtype=np.int)),
                        cv2.FONT_HERSHEY_DUPLEX, font_scale, (1, 1, 0), thickness, lineType=cv2.LINE_AA)
            pass
        pass

    # @jit
    def refresh(self, theta: np.array([]), vehicle_type=np.array([])):
        img = self.__draw_car(theta, vehicle_type)
        img = self.__draw_tags(img, theta)
        return img

    pass

class LaneVis(object):
    pass

if __name__ == '__main__':
    import pandas as pd
    import time

    car_num = 50
    td = 30
    pe = 0.4
    rl = 1000 * 50 / td
    radius = rl / (2 * np.pi)
    # cv2.namedWindow("Test", cv2.WINDOW_KEEPRATIO)

    title = "Max of Velocity:{v:.2f}km/h\nNumber of Vehicles:{nv}\nTraffic Density:{l:.2f}\nPermeability:{p:.2f}". \
        format(nv=car_num,
               v=120,
               p=pe,
               l=td)

    vis = Visualization(car_size=(13, 5), width=12, title=title)
    # vis = Visualization(car_size=(10, 3), width=9, title=title)

    data = pd.read_csv("data_TD_30.00_PE_0.40.csv",header=0, index_col=0, low_memory=False)

    vehicle_type = np.array((data.iloc[0 * car_num: (0 + 1) * car_num]).loc[:, ['type']]).T[0] != 10

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('data_TD_30.00_PE_0.40.csv.avi', fourcc, 10.0, (640, 640))

    for i in range(int(len(data)/car_num)):
        theta = np.array((data.iloc[i*car_num: (i+1)*car_num]).loc[:, ['pos']] / radius).T[0] * 180 / np.pi
        img = vis.refresh(theta, vehicle_type)
        # cv2.imshow("Test", img)
        img = img * 255
        img = np.int8(img)
        out.write(img)
        # if cv2.waitKey(1) == 27:
        #     break

    out.release()
    cv2.destroyAllWindows()

max_stars_repo_name: JiaChenwei/OpenTrafficSimulation

max_issues_repo_name: JiaChenwei/OpenTrafficSimulation

max_forks_repo_name: JiaChenwei/OpenTrafficSimulation

reason: import numpy,from numba


text:
import os
import numpy as np
import imgaug as ia
from imgaug.augmentables.segmaps import SegmentationMapsOnImage
import torch
from torch.utils.data import DataLoader

class LungTumorDataset(torch.utils.data.Dataset):
    def __init__(self, data_dir, masks_dir, transform=None):
        self.data_dir = data_dir
        self.masks_dir = masks_dir
        self.all_file_names = os.listdir(self.data_dir)
        self.transform = transform

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

    def augment(self, data, mask):
        # Fix to lack of randomness problem when using something other than pytorch
        random_seed = torch.randint(0, 1000000, (1,)).item()
        ia.seed(random_seed)

        mask = SegmentationMapsOnImage(mask, shape=mask.shape)
        aug_data, aug_mask = self.transform(image=data, segmentation_maps=mask)
        return aug_data, aug_mask.get_arr()

    def __getitem__(self, idx):
        data_path = os.path.join(self.data_dir, self.all_file_names[idx])
        mask_path = os.path.join(self.masks_dir, self.all_file_names[idx])
        data = np.load(data_path)
        mask = np.load(mask_path)
        if self.transform:
            data, mask = self.augment(data, mask)
        return np.moveaxis(data, -1, 0), np.expand_dims(mask, axis=0)

def get_dataset(preprocessed_input_dir, aug_pipeline=None, data_type='train'):
    data_dir = os.path.join(preprocessed_input_dir, data_type, 'data')
    label_dir = os.path.join(preprocessed_input_dir, data_type, 'mask')
    return LungTumorDataset(data_dir, label_dir, transform=aug_pipeline)

max_stars_repo_name: rayansamy/lung-tumor-segmentation

max_issues_repo_name: rayansamy/lung-tumor-segmentation

max_forks_repo_name: rayansamy/lung-tumor-segmentation


text:
""" test automol.geom
"""

# import pytest
import numpy
import automol
from automol import geom

C2H2CLF_GEO = (('F', (2.994881276150, -1.414434615111, -0.807144415388)),
               ('C', (1.170155936996, 0.359360756989, -0.513323178859)),
               ('C', (-1.201356763194, -0.347546894407, -0.3408392500119)),
               ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
               ('H', (1.731596406235, 2.324260256203, -0.4292070203467)),
               ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
C2H6_GEO = (
    ('C', (-1.4283035320563338, 0.013425343735546437, -0.030302158896694683)),
    ('C', (1.4283027358735494, -0.013425597530894248, 0.0303022919384165)),
    ('H', (-2.1972722614281355, -0.19229727219177065, 1.8778380427620682)),
    ('H', (-2.121310184939721, 1.792702413487708, -0.8231106338374065)),
    ('H', (-2.1448124562913287, -1.5396513482615042, -1.191852168914227)),
    ('H', (2.1448121742707795, 1.539654946791746, 1.1918517388178247)),
    ('H', (2.1972712765396953, 0.1922944277301287, -1.8778395029874426)),
    ('H', (2.121312248031497, -1.7927029137609576, 0.8231123911174519)))
C2H6_GEO_2 = (
    ('C', (-1.4283035320563338, 0.013425343735546437, -0.030302158896694683)),
    ('C', (1.4283027358735494, -0.013425597530894248, 0.0303022919384165)),
    ('H', (-2.1448124562913287, -1.5396513482615042, -1.191852168914227)),
    ('H', (2.1448121742707795, 1.539654946791746, 1.1918517388178247)),
    ('H', (2.1972712765396953, 0.1922944277301287, -1.8778395029874426)),
    ('H', (2.121312248031497, -1.7927029137609576, 0.8231123911174519)),
    ('H', (-2.1972722614281355, -0.19229727219177065, 1.8778380427620682)),
    ('H', (-2.121310184939721, 1.792702413487708, -0.8231106338374065)))
C2H5_GEO = (
    ('C', (1.609011843391662, -0.13776060061651263, -0.04009265860394636)),
    ('C', (-1.1834240897842176, 0.11044868962990169, -0.032202756665620995)),
    ('H', (2.789386604699099, 1.4908722862660362, 0.3439484428110317)),
    ('H', (2.4787984007354567, -1.9869974801329033, -0.1705361344367882)),
    ('H', (-1.7411967628258649, 1.9663729732966975, -0.7497840216103133)),
    ('H', (-2.036027793494113, -1.334976675748384, -1.2381516164697437)),
    ('H', (-1.9165482027220222, -0.10795919269481989, 1.8868187449753886)))
H2O_GEO = (('H', (-1.0375307411, 1.1972456627, 0.1366217261)),
           ('O', (-0.3546124207, 0.6455743406, 0.5947534694)),
           ('H', (-0.8777308204, -0.0012671711, 1.1319188373)))
H_GEO = (('H', (-0.9827048283, 0.061897979239, 2.02901783816)),)
H2_GEO = (('H', (0.6595322581760189, 0.0, 0.0)),
          ('H', (-0.6595322581760189, 0.0, 0.0)))
HCCH_GEO = (('C', (-1.13372239064879, 0.0082038553557789, 0.268047200455629)),
            ('C', (1.133722408498827, -0.0082033046582439, 0.210166207903664)),
            ('H', (-3.14709200757641, 0.0227720059492319, 0.319442062072525)),
            ('H', (3.147091989726373, -0.022772556646775, 0.158770666805506)))
BAD_GEO = ((2.994881276150, -1.414434615111),
           (1.170155936996, 0.359360756989),
           (-1.201356763194, -0.347546894407))

GEO_STR = """4
Energy:  1.25828 kJ/mol (UFF)
O       -0.9741512421      0.1822933377     -0.1126377411
O        0.3289735103      0.1763176636     -0.3024822120
H       -1.2826135304     -0.6998243267     -0.4438019186
H        0.4659939252      0.6943122175     -1.1366628384
"""

HABS_ZRXN_STR1 = """
reaction class: hydrogen abstraction
forward TS atoms:
  1: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  2: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  3: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  4: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  5: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  6: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  7: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  8: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  9: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  10: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  11: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  12: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  13: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  14: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  15: {symbol: X, implicit_hydrogen_valence: 0, stereo_parity: null}
  16: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  17: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
forward TS bonds:
  1-2: {order: 1, stereo_parity: null}
  1-3: {order: 1, stereo_parity: null}
  2-4: {order: 1, stereo_parity: null}
  2-5: {order: 1, stereo_parity: null}
  2-6: {order: 1, stereo_parity: null}
  4-7: {order: 1, stereo_parity: null}
  4-8: {order: 1, stereo_parity: null}
  4-9: {order: 1, stereo_parity: null}
  7-10: {order: 1, stereo_parity: null}
  7-11: {order: 1, stereo_parity: null}
  10-12: {order: 0.9, stereo_parity: null}
  10-13: {order: 1, stereo_parity: null}
  10-14: {order: 1, stereo_parity: null}
  12-15: {order: 0, stereo_parity: null}
  12-16: {order: 0.1, stereo_parity: null}
  16-17: {order: 1, stereo_parity: null}
reactants keys:
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- [16, 17]
backward TS atoms:
  1: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  2: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  3: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  4: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  5: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  6: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  7: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  8: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  9: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  10: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  11: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  12: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  13: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  14: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  15: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  16: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
backward TS bonds:
  1-2: {order: 0.9, stereo_parity: null}
  1-3: {order: 1, stereo_parity: null}
  2-4: {order: 0.1, stereo_parity: null}
  4-7: {order: 1, stereo_parity: null}
  4-10: {order: 1, stereo_parity: null}
  4-11: {order: 1, stereo_parity: null}
  5-6: {order: 1, stereo_parity: null}
  5-7: {order: 1, stereo_parity: null}
  5-12: {order: 1, stereo_parity: null}
  5-13: {order: 1, stereo_parity: null}
  6-8: {order: 1, stereo_parity: null}
  6-14: {order: 1, stereo_parity: null}
  6-15: {order: 1, stereo_parity: null}
  7-9: {order: 1, stereo_parity: null}
  8-16: {order: 1, stereo_parity: null}
products keys:
- [1, 2, 3]
- [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
"""
HABS_GEO1 = (
    ('O', (0.0, 0.0, 0.0)),
    ('C', (0.0, 0.0, 2.6914294153467657)),
    ('H', (0.0, 1.7534707783960422, -0.5517555484489822)),
    ('C', (-2.623299394112204, 0.6566385034518767, 3.6480149247207985)),
    ('H', (0.5636237182797816, -1.88605768213913, 3.3289129545424223)),
    ('H', (1.4260767249009725, 1.345507128950089, 3.35470487715432)),
    ('C', (-2.746065179408126, 0.6724245480726039, 6.5028326846462985)),
    ('H', (-3.1961339563014963, 2.518941659280532, 2.9483507693607525)),
    ('H', (-3.988069056064281, -0.7224120831906182, 2.9241998624297434)),
    ('C', (-5.340762043167491, 0.8914938898502375, 7.652631925159079)),
    ('O', (-0.845554426010156, 0.5689858109064159, 7.8373344196418655)),
    ('H', (-6.502001079557306, -0.7071271436769692, 7.052333079032717)),
    ('H', (-6.21125009870658, 2.6681404185712547, 7.061161472892827)),
    ('H', (-5.194262776062822, 0.8701632805783928, 9.713822470820245)),
    ('O', (-8.5007619646225, -2.928405157276118, 6.591025292968647)),
    ('H', (-9.48421314672397, -1.6400034467104858, 5.7038001145820685))
)

HABS_ZRXN_STR2 = """
reaction class: hydrogen abstraction
forward TS atoms:
  1: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  2: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  3: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  4: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  5: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  6: {symbol: X, implicit_hydrogen_valence: 0, stereo_parity: null}
  7: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  8: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
forward TS bonds:
  1-2: {order: 1, stereo_parity: null}
  1-3: {order: 1, stereo_parity: null}
  1-4: {order: 1, stereo_parity: null}
  1-5: {order: 0.9, stereo_parity: null}
  5-6: {order: 0, stereo_parity: null}
  5-7: {order: 0.1, stereo_parity: null}
  7-8: {order: 1, stereo_parity: null}
reactants keys:
- [1, 2, 3, 4, 5, 6]
- [7, 8]
backward TS atoms:
  1: {symbol: O, implicit_hydrogen_valence: 0, stereo_parity: null}
  2: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  3: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  4: {symbol: C, implicit_hydrogen_valence: 0, stereo_parity: null}
  5: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  6: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
  7: {symbol: H, implicit_hydrogen_valence: 0, stereo_parity: null}
backward TS bonds:
  1-2: {order: 1, stereo_parity: null}
  1-3: {order: 0.9, stereo_parity: null}
  3-4: {order: 0.1, stereo_parity: null}
  4-5: {order: 1, stereo_parity: null}
  4-6: {order: 1, stereo_parity: null}
  4-7: {order: 1, stereo_parity: null}
products keys:
- [1, 2, 3]
- [4, 5, 6, 7]
"""
HABS_GEO2 = (
    ('C', (0.0, 0.0, 0.0)),
    ('H', (0.0, 0.0, 2.0639575202459945)),
    ('H', (0.0, 1.9459185282046763, -0.6879842041977405)),
    ('H', (-1.6852142339096723, -0.9729596887682943, -0.687986206970089)),
    ('H', (1.6852153438276314, -0.9729582966864149, -0.687984673708243)),
    ('O', (3.875541447997114, -2.4707898221289817, -2.1374322380005975)),
    ('H', (4.3062792658506215, -0.8227905834100768, -2.8535934936888445))
)

def test__from_data():
    """ test getters
    """
    assert C2H2CLF_GEO == geom.from_data(
        symbs=geom.symbols(C2H2CLF_GEO),
        xyzs=geom.coordinates(C2H2CLF_GEO),
    )

def test__is_valid():
    """ test geom.is_valid
    """

    # Check validity
    assert geom.is_valid(C2H2CLF_GEO)
    # with pytest.raises(ValueError):
    #     geom.is_valid(BAD_GEO)

    # Check empty geom returns empty information
    assert not geom.symbols(())
    assert not geom.coordinates(())

def test__struct_check():
    """ test geom.is_atom
        test geom.is_linear
    """

    assert geom.is_atom(H_GEO)
    assert not geom.is_atom(C2H2CLF_GEO)

    assert not geom.is_linear(H_GEO)
    assert geom.is_linear(H2_GEO)
    assert not geom.is_linear(C2H2CLF_GEO)
    assert geom.is_linear(HCCH_GEO)

def test__set_coordinates():
    """ test geom.set_coordinates
    """
    ref_geo = (('F', (0., 0., 0.)),
               ('C', (1.170155936996, 0.359360756989, -0.513323178859)),
               ('C', (-1.201356763194, -0.347546894407, -0.3408392500119)),
               ('Cl', (1., 1., 1.)),
               ('H', (1.731596406235, 2.324260256203, -0.4292070203467)),
               ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    geo = geom.set_coordinates(C2H2CLF_GEO, {0: [0., 0., 0.],
                                             3: [1., 1., 1.]})
    assert geom.almost_equal(geo, ref_geo)

def test__from_string():
    """ test geom.from_string
    """
    assert geom.almost_equal(
        geom.from_string(geom.string(C2H2CLF_GEO)), C2H2CLF_GEO)

def test__from_xyz_string():
    """ test geom.from_xyz_string
    """
    assert geom.almost_equal(
        geom.from_xyz_string(geom.xyz_string(C2H2CLF_GEO)), C2H2CLF_GEO)

    assert geom.xyz_string_comment(GEO_STR) == 'Energy:  1.25828 kJ/mol (UFF)'

def test__formula():
    """ test geom.formula
    """
    assert geom.formula(C2H2CLF_GEO) == {'F': 1, 'C': 2, 'Cl': 1, 'H': 2}

def test__atom_indices():
    """ test geom.atom_indices
        test geom.atom_indices
    """

    hidxs = geom.atom_indices(C2H2CLF_GEO, 'H', match=True)
    heavyidxs = geom.atom_indices(C2H2CLF_GEO, 'H', match=False)

    assert hidxs == (4, 5)
    assert heavyidxs == (0, 1, 2, 3)

    h_count = geom.atom_count(C2H2CLF_GEO, 'H', match=True)
    heavy_count = geom.atom_count(C2H2CLF_GEO, 'H', match=False)

    assert h_count == 2
    assert heavy_count == 4

def test__dist_analysis():
    """ test geom.coulomb_spectrum
    """
    ref_coul_spec = (
        0.23373850982000086, 0.26771181015927226, 21.472418990888897,
        38.92412503488664, 104.1418603738336, 456.00384141400343)

    assert numpy.allclose(geom.coulomb_spectrum(C2H2CLF_GEO), ref_coul_spec)

    for _ in range(10):
        axis = numpy.random.rand(3)
        angle = numpy.random.rand()
        geo = geom.rotate(C2H2CLF_GEO, axis, angle)
        assert numpy.allclose(geom.coulomb_spectrum(geo), ref_coul_spec)

    ref_dist_mat = (
        (0.0000000, 2.5616993, 4.3547794, 6.6877445, 3.9644122, 4.7627773),
        (2.5616993, 0.0000000, 2.4806333, 4.3481308, 2.0452679, 3.8991098),
        (4.3547794, 2.4806333, 0.0000000, 2.5392899, 3.9684476, 2.0228115),
        (6.6877445, 4.3481308, 2.5392899, 0.0000000, 4.8648485, 3.9664779),
        (3.9644122, 2.0452679, 3.9684476, 4.8648485, 0.0000000, 5.7501111),
        (4.7627773, 3.8991098, 2.0228115, 3.9664779, 5.7501111, 0.0000000))
    assert numpy.allclose(geom.distance_matrix(geo), ref_dist_mat)

def test__argunique_coulomb_spectrum():
    """ test geom.argunique_coulomb_spectrum
    """
    ref_idxs = (0, 3, 5, 8)

    geo = C2H2CLF_GEO
    natms = len(geom.symbols(geo))

    geos = []
    for idx in range(10):
        axis = numpy.random.rand(3)
        angle = numpy.random.rand()
        geo = geom.rotate(geo, axis, angle)

        if idx in ref_idxs and idx != 0:
            idx_to_change = numpy.random.randint(0, natms)
            new_xyz = numpy.random.rand(3)
            geo = geom.set_coordinates(geo, {idx_to_change: new_xyz})

        geos.append(geo)

    idxs = geom.argunique_coulomb_spectrum(geos)
    assert idxs == ref_idxs

def test__mass():
    """ test geom.masses
        test geom.center_of_mass()
        test geom.mass_centered()
    """

    ref_masses = (1.00782503223, 15.99491461957, 1.00782503223)
    assert numpy.allclose(geom.masses(H2O_GEO, amu=True), ref_masses)

    ref_masses2 = (1837.1526464817923, 29156.94568388855, 1837.1526464817923)
    assert numpy.allclose(geom.masses(H2O_GEO, amu=False), ref_masses2)

    ref_red_mass = 0.9544182343494726
    assert numpy.isclose(geom.reduced_mass(H2O_GEO, H_GEO), ref_red_mass)

    # make sure the COM for an uncentered geometry is non-zero
    cm_xyz = automol.geom.center_of_mass(C2H2CLF_GEO)
    assert not numpy.allclose(cm_xyz, 0.)

    # now make sure centering it yields a COM at the origin
    geo = automol.geom.mass_centered(C2H2CLF_GEO)
    cm_xyz = automol.geom.center_of_mass(geo)
    assert numpy.allclose(cm_xyz, 0.)

def test__rotation_properties():
    """ test geom.principal_axes()
        test geom.rotational_constants()
    """

    ref_axes = (
        (-0.10203856485038033, 0.99478044375795, 1.0460242302053948e-07),
        (0.765298080540252, 0.07849958398718046, 0.6388713980413003),
        (-0.6355367646365401, -0.06518960063212392, 0.7693135490583429))
    axes = automol.geom.principal_axes(H2O_GEO)
    assert numpy.allclose(axes, ref_axes)

    ref_cons = (
        2.9448201404714373e-06, 1.566795638659629e-07, 1.4876452660293155e-07)
    cons = geom.rotational_constants(C2H2CLF_GEO)
    assert numpy.allclose(cons, ref_cons)

def test__swap_coordinates():
    """ test geom.swap_coordinates
    """
    ref_geo = (('F', (2.994881276150, -1.414434615111, -0.807144415388)),
               ('H', (1.731596406235, 2.324260256203, -0.4292070203467)),
               ('C', (-1.201356763194, -0.347546894407, -0.3408392500119)),
               ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
               ('C', (1.170155936996, 0.359360756989, -0.513323178859)),
               ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    swp_geo = automol.geom.swap_coordinates(C2H2CLF_GEO, 1, 4)
    assert swp_geo == ref_geo

def test__reflect_coordinates():
    """ test geom.reflect_coordinates
    """
    ref_geo1 = (('F', (2.99488127615, -1.414434615111, -0.807144415388)),
                ('C', (-1.170155936996, 0.359360756989, -0.513323178859)),
                ('C', (-1.201356763194, -0.347546894407, -0.3408392500119)),
                ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
                ('H', (1.731596406235, 2.324260256203, -0.4292070203467)),
                ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    ref_geo2 = (('F', (2.99488127615, -1.414434615111, -0.807144415388)),
                ('C', (-1.170155936996, -0.359360756989, 0.513323178859)),
                ('C', (-1.201356763194, -0.347546894407, -0.3408392500119)),
                ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
                ('H', (1.731596406235, 2.324260256203, -0.4292070203467)),
                ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    ref_geo3 = (('F', (2.99488127615, -1.414434615111, -0.807144415388)),
                ('C', (-1.170155936996, -0.359360756989, -0.513323178859)),
                ('C', (1.201356763194, 0.347546894407, -0.3408392500119)),
                ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
                ('H', (-1.731596406235, -2.324260256203, -0.4292070203467)),
                ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    geo1 = automol.geom.reflect_coordinates(C2H2CLF_GEO, [1], ['x'])
    geo2 = automol.geom.reflect_coordinates(C2H2CLF_GEO, [1], ['x', 'y', 'z'])
    geo3 = automol.geom.reflect_coordinates(C2H2CLF_GEO, [1, 2, 4], ['x', 'y'])

    assert automol.geom.almost_equal_dist_matrix(geo1, ref_geo1, thresh=0.001)
    assert automol.geom.almost_equal_dist_matrix(geo2, ref_geo2, thresh=0.001)
    assert automol.geom.almost_equal_dist_matrix(geo3, ref_geo3, thresh=0.001)

def test__remove():
    """ test geom.remove
    """
    ref_geo1 = (('C', (1.170155936996, 0.359360756989, -0.513323178859)),
                ('Cl', (-3.027970874978, 1.39211904938, -0.0492290974807)),
                ('H', (-1.66730598121, -2.31375855306, -0.433949091252)))
    geo1 = automol.geom.remove(C2H2CLF_GEO, [0, 2, 4])

    assert automol.geom.almost_equal_dist_matrix(geo1, ref_geo1, thresh=0.001)

def test__translate():
    """ test geom.translate
    """
    ref_geo1 = (('F', (12.994881276150, -1.414434615111, -0.807144415388)),
                ('C', (11.170155936996, 0.359360756989, -0.513323178859)),
                ('C', (8.79864323681, -0.347546894407, -0.3408392500119)),
                ('Cl', (6.97202912502, 1.39211904938, -0.0492290974807)),
                ('H', (11.731596406235, 2.324260256203, -0.4292070203467)),
                ('H', (8.33269401879, -2.31375855306, -0.433949091252)))
    geo1 = automol.geom.translate(C2H2CLF_GEO, (10.0, 0.0, 0.0))

    assert automol.geom.almost_equal_dist_matrix(geo1, ref_geo1, thresh=0.001)

def test__rotate():
    """ test geom.euler_rotate
    """
    ref_geo1 = (('F', (-3.0558758007514, -1.1509331162337, -0.9789776801786)),
                ('C', (-1.0681262247754, -0.5805345601543, 0.532908762282)),
                ('C', (1.1776424945939, -0.26506900172108, -0.4723503888912)),
                ('Cl', (3.163419853813, 0.30082019331320, 1.0056384097658)),
                ('H', (-1.3983240017954, -0.4010882123173, 2.5433538391085)),
                ('H', (1.4140728611330, -0.46140633610660, -2.471679960480)))

    theta, phi, psi = 15.0/numpy.pi, 30.0/numpy.pi, 20.0/numpy.pi
    geo1 = automol.geom.euler_rotate(C2H2CLF_GEO, theta, phi, psi)

    assert automol.geom.almost_equal_dist_matrix(geo1, ref_geo1, thresh=0.001)

def test__closest_unbonded_atoms():
    """ test geom.closest_unbonded_atoms
    """

    ref_bnd_key = frozenset({1, 5})
    ref_dist_val = 3.8991097995323956
    bnd_key, dist_val = automol.geom.closest_unbonded_atoms(C2H2CLF_GEO)

    assert bnd_key == ref_bnd_key
    assert numpy.isclose(dist_val, ref_dist_val)

def test__permutations():
    """ test geom.rot_permutated_geoms
        test.geom.permutation
    """

    ref_perm_geo = (
        ('C', (1.609011843391662, -0.13776060061651263, -0.04009265860394636)),
        ('C', (-1.183424089784217, 0.1104486896299016, -0.03220275666562099)),
        ('H', (2.789386604699099, 1.4908722862660362, 0.3439484428110317)),
        ('H', (2.4787984007354567, -1.9869974801329033, -0.1705361344367882)),
        ('H', (-1.9165482027220222, -0.10795919269481989, 1.8868187449753886)),
        ('H', (-1.7411967628258649, 1.9663729732966975, -0.7497840216103133)),
        ('H', (-2.036027793494113, -1.334976675748384, -1.2381516164697437)))
    perm_geos = automol.geom.rot_permutated_geoms(C2H5_GEO)
    assert any(automol.geom.almost_equal_dist_matrix(geo, ref_perm_geo)
               for geo in perm_geos)

    ref_perm_idxs = (0, 1, 4, 5, 6, 7, 2, 3)
    perm_idxs = automol.geom.permutation(C2H6_GEO_2, C2H6_GEO)
    assert perm_idxs == ref_perm_idxs

def test__traj():
    """ test geom.from_xyz_trajectory_string
    """

    ref_traj_str = """ 3
comment 1
C    0.000000   0.000000   0.000000
C    0.000000   0.000000   1.000000
C    0.000000   0.000000   2.000000
 3
comment 2
C    0.000000   0.000000   3.000000
C    0.000000   0.000000   4.000000
C    0.000000   0.000000   5.000000
 3
comment 3
C    0.000000   0.000000   6.000000
C    0.000000   0.000000   7.000000
C    0.000000   0.000000   8.000000"""

    traj = automol.geom.from_xyz_trajectory_string(ref_traj_str)
    geoms = tuple(geo for geo, _ in traj)
    comments = tuple(comment for _, comment in traj)
    traj_str = automol.geom.xyz_trajectory_string(geoms, comments=comments)
    assert ref_traj_str == traj_str

def test__insert_dummies():
    """ test geom.insert_dummies
    """
    # 1. Generate a z-matrix to start with
    ich = automol.smiles.inchi('CC#CC#CCCCC#CC')
    zma = automol.geom.zmatrix(automol.inchi.geometry(ich))

    # 2. Convert to cartesians and remove dummy atoms
    geo = automol.zmat.geometry(zma)

    # Assume this geometry was changed somehow, such as by optimization, and we
    # want to update the z-matrix from it. The problem is that the geometry
    # doesn't contain the dummy atoms in the z-matrix.

    # 3. Determine dummy atom keys from the original z-matrix
    dummy_key_dct = automol.zmat.dummy_key_dictionary(zma)

    # 4. Insert dummy atoms to the new geometry at the appropriate positions
    geo_wdummy = automol.geom.insert_dummies(geo, dummy_key_dct)
    assert automol.geom.symbols(geo_wdummy) == automol.zmat.symbols(zma)

    # 5. Update the z-matrix from the geometry.
    zma_new = automol.zmat.from_geometry(zma, geo_wdummy)

    # 6. Check that the geometry was correctly encoded by converting the new
    # z-matrix back and comparing.
    geo_new = automol.zmat.geometry(zma_new)
    assert automol.geom.almost_equal_dist_matrix(geo, geo_new)

def test__hydrogen_bonded_structure():
    """ test automol.geom.hydrogen_bonded_strcure
    """

    ich = automol.smiles.inchi('CCCC[O]')
    geo = automol.inchi.geometry(ich)
    print(automol.geom.hydrogen_bonded_structure(geo, grxn=None))

    ich = automol.smiles.inchi('CC(=O)CO')
    geo = automol.inchi.geometry(ich)
    print(automol.geom.hydrogen_bonded_structure(geo, grxn=None))

    # H-abstraction with OH. (1) Has internal H-Bond, (2) No internal H-Bond
    zrxn = automol.reac.from_string(HABS_ZRXN_STR1)
    grxn = automol.reac.relabel_for_geometry(zrxn)
    print(automol.geom.hydrogen_bonded_structure(HABS_GEO1, grxn=grxn))

    zrxn = automol.reac.from_string(HABS_ZRXN_STR2)
    grxn = automol.reac.relabel_for_geometry(zrxn)
    print(automol.geom.hydrogen_bonded_structure(HABS_GEO2, grxn=grxn))

def test__change_zmatrix_row_values():
    """ test automol.geom.change_zmatrix_row_values
    """
    ref_geo = (('O', (0.0, 0.0, 0.0)),
               ('C', (0.0, 0.0, 2.6100437640951895)),
               ('H', (0.0, 1.795784435, -0.9004532847666584)),
               ('N', (2.149523799, 1.6103368939, 3.8411840715311034)),
               ('H', (3.101869570, 0.04614797866, 4.810112185573969)),
               ('H', (1.052147147, 2.2771937846, 5.430572110076959)))
    ref_zma = geom.zmatrix(ref_geo)
    ref_vals = geom.zmatrix_row_values(ref_geo, 3, idx1=1, idx2=0, idx3=2)

    geo = geom.change_zmatrix_row_values(ref_geo, 3,
                                         dist=1.0, idx1=1,
                                         ang=120., idx2=0,
                                         dih=0., idx3=2)

    # First, check that the change resulted in these values
    vals = geom.zmatrix_row_values(geo, 3, idx1=1, idx2=0, idx3=2)
    assert numpy.allclose(vals, (1., 120., 0.))

    # Now, check that the other z-matrix values weren't affected. To do so,
    # change back to the original values and compare z-matrices.
    geo = geom.change_zmatrix_row_values(geo, 3,
                                         dist=ref_vals[0], idx1=1,
                                         ang=ref_vals[1], idx2=0,
                                         dih=ref_vals[2], idx3=2)
    zma = geom.zmatrix(geo)
    assert automol.zmat.almost_equal(zma, ref_zma)

def __align():
    """ test automol.geom.align
    """

    geo_to_align = (
        ('O', (4.086645332783601, -0.36560905722875314, 1.9674675821390657)),
        ('H', (-3.1954897459756855, 1.410644267833961, -1.9862581016465297)),
        ('H', (0.8593162300475073, 0.5566427082660068, 3.8838481626935937)),
        ('C', (-2.318366740031556, 0.6246331977930337, -0.3075158448225017)),
        ('H', (0.8695454413416718, -2.532034809669027, 2.4839148729322207)),
        ('H', (-3.51469182439878, -0.09175981338750572, 1.1982037017376532)),
        ('C', (1.453081848189258, -0.5309000086082638, 2.1899070931886166)),
        ('H', (1.341025025249128, 1.279348036441019, -1.652984415735705)),
        ('H', (4.618732841698623, -1.7570808221744014, 0.8262771634598443)),
        ('C', (0.19562374326160434, 0.5478619879207052, -0.11405311034385705)))

    geo_to_align_to = (
        ('C', (-2.138784469888668, 0.2694957324776663, -0.5586880803609796)),
        ('C', (0.15217964488311891, 0.9488522745797555, 0.22028348471849357)),
        ('C', (1.5375076318382452, -0.28592123196014574, 2.3702305668302412)),
        ('H', (0.376762256537029, -1.8123853043212155, 3.1796871937656)),
        ('H', (1.8913947536266074, 1.0862977248626797, 3.878337839608614)),
        ('O', (3.956752025184486, -1.1651578858041605, 1.6363421979258153)),
        ('H', (-3.0787682597616497, 1.2122215149586877, -2.120888763480582)),
        ('H', (-3.1760664785092225, -1.2516884450888743, 0.3580955418697566)),
        ('H', (1.167030604394489, 2.4678746054587726, -0.7285782384918892)),
        ('H', (3.707415891013204, -2.3278440098362223, 0.25398675016603395)))

    ref_aligned_geo = (
        ('C', (-2.318366740031556, 0.6246331977930337, -0.3075158448225017)),
        ('C', (0.19562374326160434, 0.5478619879207052, -0.11405311034385705)),
        ('C', (1.453081848189258, -0.5309000086082638, 2.1899070931886166)),
        ('H', (0.8695454413416718, -2.532034809669027, 2.4839148729322207)),
        ('H', (0.8593162300475073, 0.5566427082660068, 3.8838481626935937)),
        ('O', (4.086645332783601, -0.36560905722875314, 1.9674675821390657)),
        ('H', (-3.1954897459756855, 1.410644267833961, -1.9862581016465297)),
        ('H', (-3.51469182439878, -0.09175981338750572, 1.1982037017376532)),
        ('H', (1.341025025249128, 1.279348036441019, -1.652984415735705)),
        ('H', (4.618732841698623, -1.7570808221744014, 0.8262771634598443)))

    aligned_geo = automol.geom.align(geo_to_align, geo_to_align_to)
    assert automol.geom.almost_equal_dist_matrix(ref_aligned_geo, aligned_geo)

if __name__ == '__main__':
    __align()
    # test__hydrogen_bonded_structure()
    # test__change_zmatrix_row_values()

max_stars_repo_name: sjklipp/automol

max_issues_repo_name: sjklipp/automol

max_forks_repo_name: sjklipp/automol


text:
# File : data_collection.py
# Description : intended for dataset preparation
#
# Libraries requirements :
#   <os> : standard library
#		- Provides a portable way of using operating system dependent functionality.
#   <sys> : standard library
#		- provides access to some variables used or maintained by the interpreter and
#         to functions that interact strongly with the interpreter.
#   <json> : standard library
#		- Provides functionalites for data storing and serialization
#   <nltk> : standard library
#		- Provides comprehesive natural language toolkit, with models and training data in multiples languages
#   <torch> : standard library
#		- Provides multidimensional arrays and linear algebra tools, optimized for speed
#   <numpy> : standard library
#		- Provides multidimensional arrays and linear algebra tools, optimized for speed
#
# File history :
# Afondiel  |  23.02.2021 | Last modification

import os
import sys
import json
import numpy as np
import torch
import torch.nn as nn

from module.data_processing.nlp_functions import tokenize, stem, clean_data, bag_of_words

def data_prep():
    """
        - DESC : Collect training data
        - INPUT :
        - OUTPUT :
    """
    json_path = '/mnt/d/Lab/Project/self-driving-taxi-chatbot/data/serialization/chat.json'
    with open(json_path, 'r') as f:
        conversation = json.load(f)

    all_words = []
    tags = []
    xy = []
    bags_of_words = []

    # performing actions for each intent
    for intent in conversation['intents']:
        tag = intent['tag']
        tags.append(tag)
        for pattern in intent['patterns']:
            pattern_tok = tokenize(pattern)
            all_words.extend(pattern_tok)
            xy.append((pattern_tok, tag))

    # cleaning ponctuation charaters
    all_words = clean_data(words=all_words, tok=pattern_tok)
    # clean dublitaed elements
    all_words = sorted(set(all_words))
    tags = sorted(set(tags))

    # create training data for the model
    X_train = []
    y_train = []
    # create bag of words for input training data
    for (pattern_sentence, tag) in xy:
        bag = bag_of_words(tokenized_setence=pattern_sentence, all_words=all_words)
        X_train.append(bag)
        # create output label for the training data
        label = tags.index(tag)
        # used for cross entropy
        y_train.append(label)

    X_train = np.array(X_train)
    y_train = np.array(y_train)

    # print(y_train)
    return X_train, y_train, tags, all_words

max_stars_repo_name: afondiel/chatbot

max_issues_repo_name: afondiel/test

max_forks_repo_name: afondiel/test


text:
# First try to start Cloud ML uing MNIST example.
import tensorflow as tf
import numpy as np

from .utils import load_data
##########################################################################
#Factor into config:
IMAGE_SHAPE = (28,28)
N_PIXEL = 28 * 28
NUM_LABELS = 10

BATCH_SIZE = 128
EPOCHS = 5
##########################################################################
def parse_images(x):
    return x.reshape(len(x), -1).astype('float32')

def parse_labels(y):
    return y.astype('int32')

def numpy_input_fn(images: np.ndarray,
                   labels: np.ndarray,
                   mode=tf.estimator.ModeKeys.EVAL,
                   epochs=EPOCHS,
                   batch_size=BATCH_SIZE):
    """
    Return depending on the `mode`-key an Interator which can be use to
    feed into the Estimator-Model.

    Alternative if a `tf.data.Dataset` named `dataset` would be created:
    `dataset.make_one_shot_iterator().get_next()`
    """
    if mode == tf.estimator.ModeKeys.TRAIN:
        _epochs = epochs
        _shuffle = True
        _num_threads = 1 # This leads to doubling the number of epochs
    else:
        _epochs = 1
        _shuffle = False
        _num_threads = 1

    return tf.estimator.inputs.numpy_input_fn(
        {'x': images},
        y=labels,
        batch_size=batch_size,
        num_epochs=_epochs,
        shuffle=_shuffle, # Boolean, if True shuffles the queue. Avoid shuffle at prediction time.
        queue_capacity=1000, # Integer, number of threads used for reading
        # and enqueueing. To have predicted order of reading and enqueueing,
        # such as in prediction and evaluation mode, num_threads should be 1.
        num_threads=_num_threads
    )

def serving_input_fn():
    feature_placeholders = {
        'x': tf.placeholder(tf.float32, shape=[None, N_PIXEL])
    }
    features = feature_placeholders
    return tf.estimator.export.ServingInputReceiver(
         features=features,
         receiver_tensors=feature_placeholders,
         receiver_tensors_alternatives=None
         )

def train_and_evaluate(args):
    """
    Utility function for distributed training on ML-Engine
    www.tensorflow.org/api_docs/python/tf/estimator/train_and_evaluate
    """
    ##########################################
    # Load Data in Memoery
    # ToDo: replace numpy-arrays
    print('## load data, specified path to try: {}'.format(args['data_path']))
    (x_train, y_train), (x_test, y_test) = load_data(
        path=args['data_path'])

    x_train = parse_images(x_train)
    x_test = parse_images(x_test)

    y_train = parse_labels(y_train)
    y_test = parse_labels(y_test)

    model = tf.estimator.DNNClassifier(
        hidden_units= args['hidden_units'],  #[256, 128, 64],
        feature_columns=[tf.feature_column.numeric_column(
            'x', shape=[N_PIXEL, ])],
        model_dir=args['output_dir'],
        n_classes=NUM_LABELS,
        optimizer=tf.train.AdamOptimizer(learning_rate=args['learning_rate']),
        # activation_fn=,
        dropout=0.2,
        batch_norm=False,
        loss_reduction='weighted_sum',
        warm_start_from=None,
        config=tf.estimator.RunConfig(# save_summary_steps=200,
                                      save_checkpoints_steps=400,
                                      keep_checkpoint_max=5,
                                      keep_checkpoint_every_n_hours=1,
                                      #log_step_count_steps=100,
                                      train_distribute=None,
                                      )
    )
    train_spec = tf.estimator.TrainSpec(
        input_fn=numpy_input_fn(
            x_train, y_train, mode=tf.estimator.ModeKeys.TRAIN,
            batch_size = args['train_batch_size']),
        max_steps=args['train_steps'],
        # hooks = None
    )
    # use `LatestExporter` for regular model exports:
    exporter = tf.estimator.LatestExporter('exporter', serving_input_fn)
    eval_spec = tf.estimator.EvalSpec(
        input_fn=numpy_input_fn(
            x_test, y_test, mode=tf.estimator.ModeKeys.EVAL),
        # steps=100,
        start_delay_secs=args['eval_delay_secs'],
        throttle_secs=args['min_eval_frequency'],
        exporters=exporter
    )
    print(
        "## start training and evaluation\n"
        "### save model, ckpts, etc. to: {}".format(args['output_dir'])

    )

    tf.estimator.train_and_evaluate(
        estimator=model, train_spec=train_spec, eval_spec=eval_spec)


text:
from sympy.core import Basic, S, Symbol
from sympy.core.function import Function

from sympy import sin, cos, cot, tan
from sympy import sinh, cosh, tanh, coth

from sympy.solvers import solve
from sympy.polynomials import quo, gcd, lcm, roots, factor_
from sympy.simplify import normal, simplify, trigsimp, together

def components(expr):
    """Returns a set of all functional components of the given expression
       with symbols, functions applications and compositions. All integer
       powers are being skipped however fractional and functional powers
       are collected as well.

       >>> from sympy import *
       >>> x, y = symbols('xy')

       >>> components(x*y)
       set([y, x])

       >>> components(sin(x))
       set([sin(x), x])

       >>> components(sin(x)*cos(x)**2)
       set([sin(x), cos(x), x])

       >>> components(sin(x)*sqrt(log(x)))
       set([log(x), sin(x), log(x)**(1/2), x])

       >>> components(x*sin(exp(x)*y))
       set([y, sin(y*exp(x)), x, exp(x)])

    """
    result = set()

    if isinstance(expr, Basic.Symbol):
        result.add(expr)
    elif isinstance(expr, Function):
        for obj in expr:
            result |= components(obj)

        result.add(expr)
    elif isinstance(expr, Basic.Pow):
        result |= components(expr.base)

        if not isinstance(expr.exp, Basic.Integer):
            result |= components(expr.exp) | set([expr])
    else:
        for obj in expr:
            result |= components(obj)

    return result

def monomials(variables, degree):
    """Generate monomials set of the given total degree or less.

       >>> from sympy import *
       >>> x, y = symbols('xy')

       >>> sorted(monomials([x, y], 2))
       [1, x, y, x**2, y**2, x*y]

       >>> sorted(monomials([x, y], 3))
       [1, x, y, x**2, x**3, y**2, y**3, x*y, x*y**2, y*x**2]

    """
    if not variables:
        return set([S.One])
    else:
        x, tail = variables[0], variables[1:]

        monoms = monomials(tail, degree)

        for i in range(1, degree+1):
            monoms |= set([ x**i * m for m in monomials(tail, degree-i) ])

        return monoms

def factorization(poly, linear=False):
    """Returns a list with polynomial factors over rationals or, if
       'linear' flag is set over Q(i). This simple handler should
       be merged with original factor() method.

       >>> from sympy import *
       >>> x, y = symbols('xy')

       >>> factorization(x**2 - y**2)
       set([1, x - y, x + y])

       >>> factorization(x**2 + 1)
       set([1, 1 + x**2])

       >>> factorization(x**2 + 1, linear=True)
       set([1, x - I, I + x])

    """
    factored = set([ q.as_basic() for q in factor_.factor(poly) ])

    if not linear:
        return factored
    else:
        factors = []

        for factor in factored:
            symbols = factor.atoms(Symbol)

            if len(symbols) == 1:
                x = symbols.pop()
            else:
                factors += [ factor ]
                continue

            linearities = [ x - r for r in roots(factor, x) ]

            if not linearities:
                factors += [ factor ]
            else:
                unfactorable = quo(factor, Basic.Mul(*linearities))

                if not isinstance(unfactorable, Basic.Number):
                    factors += [ unfactorable ]

                factors += linearities

        return set(factors)

def risch_norman(f, x, rewrite=False):
    """Computes indefinite integral using extended Risch-Norman algorithm,
       also known as parallel Risch. This is a simplified version of full
       recursive Risch algorithm. It is designed for integrating various
       classes of functions including transcendental elementary or special
       functions like Airy, Bessel, Whittaker and Lambert.

       The main difference between this algorithm and the recursive one
       is that rather than computing a tower of differential extensions
       in a recursive way, it handles all cases in one shot. That's why
       it is called parallel Risch algorithm. This makes it much faster
       than the original approach.

       Another benefit is that it doesn't require to rewrite expressions
       in terms of complex exponentials. Rather it uses tangents and so
       antiderivatives are being found in a more familliar form.

       Risch-Norman algorithm can also handle special functions very
       easily without any additional effort. Just differentiation
       method must be known for a given function.

       Note that this algorithm is not a decision procedure. If it
       computes an antiderivative for a given integral then it's a
       proof that such function exists. However when it fails then
       there still may exist an antiderivative and a fallback to
       recurrsive Risch algorithm would be necessary.

       The question if this algorithm can be made a full featured
       decision procedure still remains open.

       For more information on the implemented algorithm refer to:

       [1] K. Geddes, L.Stefanus, On the Risch-Norman Integration
           Method and its Implementation in Maple, Proceedings of
           ISSAC'89, ACM Press, 212-217.

       [2] J. H. Davenport, On the Parallel Risch Algorithm (I),
           Proceedings of EUROCAM'82, LNCS 144, Springer, 144-157.

       [3] J. H. Davenport, On the Parallel Risch Algorithm (III):
           Use of Tangents, SIGSAM Bulletin 16 (1982), 3-6.

       [4] J. H. Davenport, B. M. Trager, On the Parallel Risch
           Algorithm (II), ACM Transactions on Mathematical
           Software 11 (1985), 356-362.

    """
    f = Basic.sympify(f)

    if not f.has(x):
        return f * x

    rewritables = {
        (sin, cos, cot)     : tan,
        (sinh, cosh, coth)  : tanh,
    }

    if rewrite:
        for candidates, rule in rewritables.iteritems():
            f = f.rewrite(candidates, rule)
    else:
        for candidates in rewritables.iterkeys():
            if f.has(*candidates):
                break
        else:
            rewrite = True

    terms = components(f)

    for g in set(terms):
        h = g.diff(x)

        if not isinstance(h, Basic.Zero):
            terms |= components(h)

    terms = [ g for g in terms if g.has(x) ]

    V, in_terms, out_terms = [], [], {}

    for i, term in enumerate(terms):
        V += [ Symbol('x%s' % i) ]

        N = term.count_ops(symbolic=False)
        in_terms += [ (N, term, V[-1]) ]

        out_terms[V[-1]] = term

    in_terms.sort(lambda u, v: int(v[0] - u[0]))

    def substitute(expr):
        for _, g, symbol in in_terms:
            expr = expr.subs(g, symbol)

        return expr

    diffs = [ substitute(g.diff(x)) for g in terms ]

    denoms = [ g.as_numer_denom()[1] for g in diffs ]
    denom = reduce(lambda p, q: lcm(p, q, V), denoms)

    numers = [ normal(denom * g, *V) for g in diffs ]

    def derivation(h):
        return Basic.Add(*[ d * h.diff(v) for d, v in zip(numers, V) ])

    def deflation(p):
        for y in p.atoms(Basic.Symbol):
            if not isinstance(derivation(p), Basic.Zero):
                c, q = p.as_polynomial(y).as_primitive()
                return deflation(c) * gcd(q, q.diff(y))
        else:
            return p

    def splitter(p):
        for y in p.atoms(Basic.Symbol):
            if not isinstance(derivation(y), Basic.Zero):
                c, q = p.as_polynomial(y).as_primitive()

                q = q.as_basic()

                h = gcd(q, derivation(q), y)
                s = quo(h, gcd(q, q.diff(y), y), y)

                c_split = splitter(c)

                if s.as_polynomial(y).degree() == 0:
                    return (c_split[0], q * c_split[1])

                q_split = splitter(normal(q / s, *V))

                return (c_split[0]*q_split[0]*s, c_split[1]*q_split[1])
        else:
            return (S.One, p)

    special = []

    for term in terms:
        if isinstance(term, Basic.Function):
            if isinstance(term, Basic.tan):
                special += [ (1 + substitute(term)**2, False) ]
            elif isinstance(term.func, tanh):
                special += [ (1 + substitute(term), False),
                             (1 - substitute(term), False) ]
            #elif isinstance(term.func, Basic.LambertW):
            #    special += [ (substitute(term), True) ]

    ff = substitute(f)

    P, Q = ff.as_numer_denom()

    u_split = splitter(denom)
    v_split = splitter(Q)

    s = u_split[0] * Basic.Mul(*[ g for g, a in special if a ])
    a, b, c = [ p.as_polynomial(*V).degree() for p in [s, P, Q] ]

    candidate_denom = s * v_split[0] * deflation(v_split[1])
    monoms = monomials(V, 1 + a + max(b, c))

    linear = False

    while True:
        coeffs, candidate, factors = [], S.Zero, set()

        for i, monomial in enumerate(monoms):
            coeffs += [ Symbol('A%s' % i, dummy=True) ]
            candidate += coeffs[-1] * monomial

        candidate /= candidate_denom

        polys = [ v_split[0], v_split[1], u_split[0]] + [ s[0] for s in special ]

        for irreducibles in [ factorization(p, linear) for p in polys ]:
            factors |= irreducibles

        for i, irreducible in enumerate(factors):
            if not isinstance(irreducible, Basic.Number):
                coeffs += [ Symbol('B%s' % i, dummy=True) ]
                candidate += coeffs[-1] * Basic.log(irreducible)

        h = together(ff - derivation(candidate) / denom)

        numerator = h.as_numer_denom()[0].expand()

        if not isinstance(numerator, Basic.Add):
            numerator = [numerator]

        collected = {}

        for term in numerator:
            coeff, depend = term.as_independent(*V)

            if depend in collected:
                collected[depend] += coeff
            else:
                collected[depend] = coeff

        solutions = solve(collected.values(), coeffs)

        if solutions is None:
            if linear:
                break
            else:
                linear = True
        else:
            break

    if solutions is not None:
        antideriv = candidate.subs_dict(solutions)

        for C in coeffs:
            if C not in solutions:
                antideriv = antideriv.subs(C, S.Zero)

        antideriv = simplify(antideriv.subs_dict(out_terms)).expand()

        if isinstance(antideriv, Basic.Add):
            return Basic.Add(*antideriv.as_coeff_factors()[1])
        else:
            return antideriv
    else:
        if not rewrite:
            return risch_norman(f, x, rewrite=True)
        else:
            return None

max_stars_repo_name: certik/sympy-oldcore

max_issues_repo_name: certik/sympy-oldcore

max_forks_repo_name: certik/sympy-oldcore

reason: from sympy


text:
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------------------------
import platform
import unittest

import numpy as np
from nimbusml.datasets import get_dataset
from nimbusml.ensemble import EnsembleRegressor
from nimbusml.ensemble.feature_selector import RandomFeatureSelector
from nimbusml.ensemble.output_combiner import RegressorMedian
from nimbusml.ensemble.subset_selector import RandomPartitionSelector
from nimbusml.ensemble.sub_model_selector import RegressorBestDiverseSelector
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.utils.testing import assert_greater, assert_less

class TestEnsembleRegressor(unittest.TestCase):

    def test_ensembleregressor(self):
        np.random.seed(0)

        df = get_dataset("airquality").as_df().fillna(0)
        df = df[df.Ozone.notnull()]

        X_train, X_test, y_train, y_test = train_test_split(
            df.loc[:, df.columns != 'Ozone'], df['Ozone'])

        # Train a model and score
        ensemble = EnsembleRegressor(num_models=3).fit(X_train, y_train)
        scores = ensemble.predict(X_test)

        r2 = r2_score(y_test, scores)
        assert_greater(r2, 0.105, "should be greater than %s" % 0.105)
        assert_less(r2, 0.13, "sum should be less than %s" % 0.13)

        ensemble_with_options = EnsembleRegressor(
            num_models=3,
            sampling_type = RandomPartitionSelector(
                feature_selector=RandomFeatureSelector(
                    features_selction_proportion=0.7)),
            sub_model_selector_type=RegressorBestDiverseSelector(),
            output_combiner=RegressorMedian()).fit(X_train, y_train)
        scores = ensemble_with_options.predict(X_test)

        r2 = r2_score(y_test, scores)
        assert_greater(r2, 0.07, "R-Squared  should be greater than %s" % 0.07)
        assert_less(r2, 0.08, "R-Squared should be less than %s" % 0.08)

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

max_stars_repo_name: michaelgsharp/NimbusML

max_issues_repo_name: michaelgsharp/NimbusML

max_forks_repo_name: michaelgsharp/NimbusML


text:
# Advent of Code 2019
#
# From https://adventofcode.com/2019/day/6
#
import matplotlib.pyplot as plt
import networkx as nx

with open('../inputs/Advent2019_06.txt', 'r') as f:
    inputs = [line.strip().split(')') for line in f]

G = nx.Graph()
G.add_edges_from(inputs)

lengths = nx.shortest_path_length(G, source='COM')
print(f'AoC 2019 Day 6 Part 1: {sum(lengths.values())}')

transfers = nx.shortest_path_length(G, source="YOU", target="SAN") - 2
print(f'AoC 2019 Day 6 Part 2: {transfers}')

plt.figure(figsize=(14, 14))
pos = nx.spring_layout(G)
nx.draw_networkx(G, node_size=4, pos=pos, font_size=6, arrows=False)
nx.draw_networkx(G.subgraph('COM'), pos=pos, node_size=180, node_color='black', font_size=6, font_color='yellow')
nx.draw_networkx(G.subgraph('YOU'), pos=pos, node_size=180, node_color='black', font_size=6, font_color='yellow')
nx.draw_networkx(G.subgraph('SAN'), pos=pos, node_size=180, node_color='black', font_size=6, font_color='yellow')
plt.show()

reason: import networkx


text:
#!/usr/bin/env python

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.io import loadmat
from skimage import color
from skimage import io
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import h5py
import tensorflow as tf
#import torch

plt.rcParams['figure.figsize'] = (16.0, 4.0)
###############################################################################
###############################################################################

#device = torch.device("cpu")

###############################################################################
###############################################################################

def load_data(path):
    """ Helper function for loading a MAT-File"""
    data = loadmat(path)
    return data['X'], data['y']

def convert_labels_10to0(labels):
    labels[labels ==10] = 0

def rgb2gray(images):
    """converting in human readable format
       grayscale conversion to reduce the amount of data to be processed
       Y =  0.2990R + 0.5870G + 0.1140B"""
    return np.expand_dims(np.dot(images, [0.2990, 0.5870, 0.1140]), axis=3)

def normalize(train_greyscale, test_greyscale, val_greyscale):
    """Normalization refers to normalizing the data dimensions so that they
       are of approximately the same scale. Divide each dimension by its
       standard deviation, once it has been zero-centered.

       Normalization done using mean and std on training data
    """

    # Calculate the mean on the training data
    train_mean = np.mean(train_greyscale, axis=0)

    # Calculate the std on the training data
    train_std = np.std(train_greyscale, axis=0)

    # Subtract it equally from all splits
    train_greyscale_norm = (train_greyscale - train_mean) / train_std
    test_greyscale_norm = (test_greyscale - train_mean)  / train_std
    val_greyscale_norm = (val_greyscale - train_mean) / train_std
    return train_greyscale_norm, test_greyscale_norm, val_greyscale_norm

def one_hot_labels(y_train, y_test, y_val ):
    enc = OneHotEncoder().fit(y_train.reshape(-1,1))
    y_train = enc.transform(y_train.reshape(-1, 1)).toarray()
    y_test = enc.transform(y_test.reshape(-1, 1)).toarray()
    y_val = enc.transform(y_val.reshape(-1, 1)).toarray()
    return y_train, y_test, y_val

def store_data(filename,
                output_dir,
                train_greyscale_norm, test_greyscale_norm, val_greyscale_norm,
                y_train, y_test, y_val  ):
    h5f = h5py.File(os.path.join(output_dir, filename), 'w')

    # Store the datasets
    h5f.create_dataset('X_train', data=train_greyscale_norm)
    h5f.create_dataset('y_train', data=y_train)
    h5f.create_dataset('X_test', data=test_greyscale_norm)
    h5f.create_dataset('y_test', data=y_test)
    h5f.create_dataset('X_val', data=val_greyscale_norm)
    h5f.create_dataset('y_val', data=y_val)

    # Close the file
    h5f.close()

def load_preprocessed_data(filename, input_dir):
    # Open the file as readonly
    h5f = h5py.File(os.path.join(input_dir, filename), 'r')

    # Load the training, test and validation set
    X_train = h5f['X_train'][:]
    y_train = h5f['y_train'][:]
    X_test = h5f['X_test'][:]
    y_test = h5f['y_test'][:]
    X_val = h5f['X_val'][:]
    y_val = h5f['y_val'][:]

    # Close this file
    h5f.close()
    return X_train, y_train, X_test, y_test, X_val, y_val

def prepare_log_dir(TENSORBOARD_SUMMARIES_DIR):
    '''Clears the log files then creates new directories to place
        the tensorbard log file.'''
    if tf.gfile.Exists(TENSORBOARD_SUMMARIES_DIR):
        tf.gfile.DeleteRecursively(TENSORBOARD_SUMMARIES_DIR)
    tf.gfile.MakeDirs(TENSORBOARD_SUMMARIES_DIR)

def get_batch(X, y, batch_size=512):
    """
    Dynamically get batches
    """
    for i in np.arange(0, y.shape[0], batch_size):
        end = min(X.shape[0], i + batch_size)
        yield(X[i:end],y[i:end])

def flatten(lists):
    """
    Convert Lists of List to numpy array¶
    """
    results = []
    for numbers in lists:
        for x in numbers:
            results.append(x)
    return np.asarray(results)

max_stars_repo_name: bithikajain/House-Number-Detection

max_issues_repo_name: bithikajain/House-Number-Detection

max_forks_repo_name: bithikajain/House-Number-Detection

reason: import numpy,from scipy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#------------------------------------------------------
# @ File       : misc.py
# @ Description:
# @ Author     : Alex Chung
# @ Contact    : yonganzhong@outlook.com
# @ License    : Copyright (c) 2017-2018
# @ Time       : 2020/11/5 下午7:19
# @ Software   : PyCharm
#-------------------------------------------------------

import torch
import torchvision

import numpy as np
import matplotlib.pyplot as plt

_all__ = ['read_class_names', 'plt_imshow', 'plot_image_class']

def read_class_names(class_file_name):
    '''loads class name from a file'''
    names = {}
    with open(class_file_name, 'r') as data:
        for ID, name in enumerate(data):
            names[ID] = name.strip('\n')
    return names

def plt_imshow(image, title=None):
    """

    :param image:
    :param title:
    :return:
    """
    image = image.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    image = std * image + mean
    image = np.clip(image, 0, 1)  # clip to (0, 1)
    plt.imshow(image)
    if title is not None:
        plt.title(title)

def plot_image_class(images, labels, index_class):
    """

    :param images:
    :param labels:
    :return:
    """
    labels = [index_class[index] for index in labels.numpy()]

    fig, axs = plt.subplots(6, 6, figsize=(18, 18))
    for n, (img, label) in enumerate(zip(images, labels)):
        plt.subplot(6, 6, n + 1)
        plt_imshow(img, label)
        plt.axis('off')
    plt.show()


text:
import numpy as np
import scipy.interpolate as interpolate
import os
import subprocess
import shutil
import multiprocessing
import sys
import copy

from pyfoamsetup.coreLibrary import *
import pyfoamsetup.coreLibrary.CaseSetup as CaseSetup

class FoilSimulation(CaseSetup.CaseSetup):
	def __init__(self, runName, U, c = 1, threeD = False, vilje = False, meshSetting='medium', fluid='air', timeStepSetting='medium'):
		if fluid == 'air':
			nu  = 1.45e-5
			rho = 1.226
		elif fluid == 'water':
			nu  = 1.19e-6
			rho = 999.1
		else:
			print('Unknown fluid! Assuming air...')
			nu  = 1.45e-5
			rho = 1.226

		A = c

		patchList = ['foil']
		self.nrFoils = 1

		self.wallFunction = True
		self.uniformWallRefinement = False

		# Call init from base class
		self.homePath = os.path.dirname(os.path.realpath(__file__))
		super().__init__(runName, patchList, c, U, A, nu, rho, 'FoilSimulation')
		self.splitPatches = True

		# Input variables
		self.threeD = threeD

		# Default mesh settings
		self.baseSize     = 1*self.L
		self.domainFront  = 5*self.L
		self.domainWake   = 10*self.L
		self.domainWidth  = 0.25*self.L

		# Default simulation settings
		self.setSolver('pimpleFoam')
		#self.timeStepScheme = 'CrankNicolson'
		self.endTime = np.round(50*self.L/self.U, decimals=6)
		self.writeInterval = self.endTime/10

		self.turbulenceModel = 'kOmegaSST'
		self.turbulenceType  = 'RAS'

		self.schemeType = 'robustAndAccurate'

		self.writeWallPressure = False

		# List conating motion information
		self.motion     = [False]
		self.motionData = [False]

		self.createPatch = False

	def setDefaultCellLengths(self):
		super().setDefaultCellLengths()

		self.maxBaseSize     = 0.1*self.L   # Ensure fine enough background mesh
		self.maxSmallestSize = 0.001*self.L # Ensure that the geometry is captured
		self.viscousLength   = 0.01*self.L  # Resolution in the 'viscous wake'

	def setMeshSettings(self):
		super().setMeshSettings()
		if self.uniformWallRefinement:
			self.finalLayerFactor = 0.75
		else:
			self.finalLayerFactor = 1.0

	def addFoil(self, c = 1):
		if self.nrFoils == 1:
			self.patchList = ['foil1']

		self.nrFoils += 1
		self.patchList.append('foil{:.0f}'.format(self.nrFoils))
		self.motion.append(False)
		self.motionData.append(False)

	def addMotion(self, t, x, y, omega, origin, foilIndex = 0):
		if foilIndex > self.nrFoils:
			print('Too large foil index. Ignoring your request for motion!')

		if self.solver != 'pimpleDyMFoam':
			self.setSolver('pimpleDyMFoam')
			self.timeStepScheme   = 'Euler'
			self.adjustTimeStep   = True
			self.maxCo            = 10

			self.forceOrigo = np.array([origin[0], 0, 0])

			self.purgeWrite = 0

		self.motion[foilIndex] = True
		self.motionData[foilIndex] = motion(t, x, y, omega, origin)

		# Write more time steps
		self.writeInterval = np.round(1/48, decimals = 6)

	def writeBlockMesh(self):
		blockMesh = BlockMesh.Dict()

		# Calculate minimum values for domain size
		xBack  =  self.domainWake
		xFront = -self.domainFront

		yRight =  self.domainFront
		yLeft =  -self.domainFront

		if self.threeD:
			width     = np.round(self.domainWidth, decimals=6)
			z_nrCells = np.round(width/self.baseSize)
		else:
			width = np.round(self.baseSize, decimals=6)
			z_nrCells = 1

		# Calculate number of cells in each direction
		x_nrCells = np.ceil((xBack - xFront)/self.baseSize)
		y_nrCells = np.ceil((yRight - yLeft)/self.baseSize)

		# Readjust domain size to fit nr cells
		xLength = self.baseSize*x_nrCells
		yLength = self.baseSize*y_nrCells
		zLength = self.baseSize*z_nrCells

		wakeFraction  = (self.domainWake/(self.domainWake + self.domainFront))
		frontFraction = (self.domainFront/(self.domainWake + self.domainFront))
		xFront = np.round(-xLength*frontFraction, decimals=6)
		xBack  = np.round(xLength*wakeFraction, decimals=6)

		yRight = np.round(yLength/2, decimals=6)
		yLeft  = np.round(-yLength/2, decimals=6)

		# Add data to blockmesh and write
		if self.threeD:
			blockMesh.addVertex([xFront, yLeft,  -width/2])
			blockMesh.addVertex([xBack,  yLeft,  -width/2])
			blockMesh.addVertex([xBack,  yRight, -width/2])
			blockMesh.addVertex([xFront, yRight, -width/2])

			blockMesh.addVertex([xFront, yLeft,  width/2])
			blockMesh.addVertex([xBack,  yLeft,  width/2])
			blockMesh.addVertex([xBack,  yRight, width/2])
			blockMesh.addVertex([xFront, yRight, width/2])

			blockMesh.addBlock([x_nrCells, y_nrCells, z_nrCells])
		else:
			blockMesh.addBlock([x_nrCells, y_nrCells, 1])

			blockMesh.addVertex([xFront, yLeft,  -width])
			blockMesh.addVertex([xBack,  yLeft,  -width])
			blockMesh.addVertex([xBack,  yRight, -width])
			blockMesh.addVertex([xFront, yRight, -width])

			blockMesh.addVertex([xFront, yLeft,  0])
			blockMesh.addVertex([xBack,  yLeft,  0])
			blockMesh.addVertex([xBack,  yRight, 0])
			blockMesh.addVertex([xFront, yRight, 0])

		blockMesh.addBoundary('inlet',  'patch', [[0, 4, 7, 3], [3, 7, 6, 2], [0, 1, 5, 4]])
		blockMesh.addBoundary('outlet', 'patch', [[2, 6, 5, 1]])

		if self.threeD:
			blockMesh.addBoundary('leftMesh',  'patch', [[0, 3, 2, 1]])
			blockMesh.addBoundary('rightMesh', 'patch', [[4, 5, 6, 7]])
		else:
			blockMesh.addBoundary('left',  'empty', [[0, 3, 2, 1]])
			blockMesh.addBoundary('right', 'empty', [[4, 5, 6, 7]])

		blockMesh.write(self.systemFolder)

	def writeMesh(self):
		self.calculateBaseSize()
		self.writeBlockMesh()

		if self.uniformWallRefinement:
			minWallRefinement = self.maxRefinementLevel
		else:
			minWallRefinement = self.maxRefinementLevel-1

		# Initialize snappy hex mesh with foil
		if self.nrFoils == 1:
			self.snappyDict.addGeometry('foil.obj', 'triSurfaceMesh', {'name':'foil'})
			self.snappyDict.addRefinementSurface('foil', minWallRefinement, self.maxRefinementLevel, self.nrLayers)
			self.snappyDict.addFeature('foil.eMesh', self.maxRefinementLevel)
		else:
			for i in range(self.nrFoils):
				self.snappyDict.addGeometry('foil{:.0f}.obj'.format(i+1), 'triSurfaceMesh', {'name':'foil{:.0f}'.format(i+1)})
				self.snappyDict.addRefinementSurface('foil{:.0f}'.format(i+1), minWallRefinement, self.maxRefinementLevel, self.nrLayers)
				self.snappyDict.addFeature('foil{:.0f}.eMesh'.format(i+1), self.maxRefinementLevel)

		# Set up layer settings
		self.snappyDict.addLayersControls['relativeSizes']       = 'false'
		self.snappyDict.addLayersControls['finalLayerThickness'] = self.t_final
		self.snappyDict.addLayersControls['minThickness']        = 0.1*self.t_final
		self.snappyDict.addLayersControls['expansionRatio']      = self.layerExpansion

		self.snappyDict.castellatedMeshControls['nCellsBetweenLevels'] = int(self.nCellsBetweenLevels)

		# Set point in mesh
		if self.threeD:
			self.snappyDict.castellatedMeshControls['locationInMesh'] = '(-2.02 1.01 {:.6f})'.format(0.1*self.domainWidth)
		else:
			self.snappyDict.castellatedMeshControls['locationInMesh'] = '(-2.02 1.01 {:.6f})'.format(-0.1*self.baseSize)

			self.snappyDict.actionControl['snap'] = 'false'
			self.snappyDict.actionControl['addLayers'] = 'false'

			# Create second snappyHexMeshDict for snapping and layer stage
			snappyDict2 = copy.deepcopy(self.snappyDict)

			snappyDict2.actionControl['castellatedMesh'] = 'false'
			snappyDict2.actionControl['snap']            = 'true'
			snappyDict2.actionControl['addLayers']       = 'true'

			snappyDict2.meshQualityControls['minDeterminant'] = 1e-6

			snappyDict2.write(self.systemFolder, ending='.snap')

		self.snappyDict.write(self.systemFolder)

		if self.nrFoils == 1:
			self.snappyDict.writeSurfaceFeatureExtractDict(self.systemFolder, 'foil.obj')
		else:
			geoemtryFileList = []
			for i in range(self.nrFoils):
				geoemtryFileList.append('foil{:.0f}.obj'.format(i+1))
			self.snappyDict.writeSurfaceFeatureExtractDict(self.systemFolder, geoemtryFileList)

	def writeCaseFiles(self):
		# Update reference area
		if self.threeD:
			self.A = self.L*self.domainWidth

		super().writeCaseFiles()

		# Adjust boundary condition in case files
		if self.threeD:
			boundaryFiles = os.listdir(self.boundaryFolder)

			for i in range(len(boundaryFiles)):
				boundaryFile = boundaryFiles[i]
				filePath = self.boundaryFolder + boundaryFile

				if os.path.isfile(filePath):
					FileHandling.changeBoundary(filePath, 'left',  '\t\ttype    cyclicAMI;\n')
					FileHandling.changeBoundary(filePath, 'right', '\t\ttype    cyclicAMI;\n')

			createPatchDict = createPatch.Dict()
			createPatchDict.addTranslationalPatch('left', ('leftMesh'), 'right', [0, 0, self.domainWidth])
			createPatchDict.addTranslationalPatch('right', ('rightMesh'), 'left', [0, 0, -self.domainWidth])
			createPatchDict.write(self.systemFolder)

		# Right numerical schemes
		if self.turbulenceType == 'LES':
			os.remove(self.systemFolder+'fvSchemes')
			shutil.move(self.systemFolder+'fvSchemes.LES', self.systemFolder+'fvSchemes')
			os.remove(self.systemFolder+'fvSolution')
			shutil.move(self.systemFolder+'fvSolution.LES', self.systemFolder+'fvSolution')

		if self.nrFoils > 1:
			boundaryFiles = os.listdir(self.boundaryFolder)

			for i in range(len(boundaryFiles)):
				boundaryFile = boundaryFiles[i]
				filePath = self.boundaryFolder + boundaryFile

				if os.path.isfile(filePath):
					FileHandling.changeLine(filePath, 'foil',  '\tfoil1')

					for j in range(1, self.nrFoils):
						FileHandling.copyBoundary(filePath, 'foil1', 'foil{:.0f}'.format(j+1))

		if True in self.motion:
			f = open(self.systemFolder+'controlDict', 'a')
			f.write('\nlibs\n(\n\t"libmyfvMotionSolvers.so"\n);\n')
			f.close()

			dynamicMeshDict = dynamicMesh.displacement(self.patchList, farFieldPatch ='inlet')
			dynamicMeshDict.write(self.constantFolder)

			for i in range(self.nrFoils):
				if self.motion[i]:
					if self.nrFoils == 1:
						foilName = 'foil'
					else:
						foilName = 'foil{:.0f}'.format(i+1)

					self.motionData[i].write(self.runFolder+'motion_'+foilName)

					boundaryString = '\t\ttype\ttwoDimMotion;\n\t\torigin\t({:.6f} {:.6f} {:.6f});\n\t\ttimeDataFileName\t"$FOAM_CASE/motion_'.format(self.motionData[i].origin[0], self.motionData[i].origin[1], self.motionData[i].origin[2])+foilName+'";\n\t\tvalue\tuniform (0 0 0);\n'
					FileHandling.changeBoundary(self.boundaryFolder+'pointDisplacement', foilName, boundaryString)

					FileHandling.changeBoundary(self.boundaryFolder+'U', foilName, '\t\ttype    movingWallVelocity;\n\t\tvalue    uniform (0 0 0);\n')

		if self.createPatch:
			self.createPatchDict.write(self.systemFolder)

	def writeScripts(self, boundaryLayer=True):
		# Mesh
		f = open(self.runFolder+'/mesh.sh', 'w')

		f.write('#!/bin/bash\n\n')

		f.write('surfaceFeatureExtract\n')
		f.write('blockMesh\n')

		# Execute castellation in parallel
		f.write('mv system/decomposeParDict system/decomposeParDict.sim\n')
		f.write('mv system/decomposeParDict.mesh system/decomposeParDict\n')

		f.write('decomposePar\n')

		f.write('mpirun -np {:.0f} snappyHexMesh -overwrite -parallel\n'.format(self.nCPUs_mesh))

		f.write('reconstructParMesh -constant\n')
		f.write('rm -fr processor*\n')

		f.write('mv system/decomposeParDict system/decomposeParDict.mesh\n')
		f.write('mv system/decomposeParDict.sim system/decomposeParDict\n')

		if self.threeD:
			f.write('createPatch -overwrite\n')
		else:
			f.write('extrudeMesh\n')

			f.write('mv system/snappyHexMeshDict system/snappyHexMeshDict.castellation\n')
			f.write('mv system/snappyHexMeshDict.snap system/snappyHexMeshDict\n')

			f.write('snappyHexMesh -overwrite\n')

		if len(self.topoSetList) > 0:
			f.write('topoSet\n')

		if self.createPatch:
			f.write('createPatch -overwrite\n')

		f.write('renumberMesh -overwrite\n')

		f.close()

		# Simulation
		f = open(self.runFolder + '/runSim.sh', 'w')

		f.write('#!/bin/bash\n\n')

		if self.solver == 'simpleFoam':
			self.calculateBoundaryLayerHeight()
			f.write('applyBoundaryLayer -ybl {:.6f}\n\n'.format(self.boundaryLayerHeight))

		f.write('decomposePar\n')

		if self.vilje:
			f.write('mpiexec ' + self.solver + ' -parallel\n')
		else:
			if True in self.motion:
				f.write('mpirun -np {:.0f} '.format(self.nCPUs) + 'pimpleDyMFoam' + ' -parallel\n')
			else:
				f.write('mpirun -np {:.0f} '.format(self.nCPUs) + self.solver + ' -parallel\n')

		f.write('reconstructPar\n')

		f.write('rm -fr processor*\n')

		f.close()

	def addViscousWake(self, x0, y0, lengthFactor = 2, widthFactor = 0.5, expansion=1.5):
		nrLayers = copy.deepcopy(self.nrLayers)
		self.calculateBaseSize()
		self.nrLayers = nrLayers

		length0 = self.L*lengthFactor
		width0  = self.L*widthFactor

		maxLevel = self.computeCellLevel(self.viscousLength)

		if isinstance(x0, list):
			nrWakes = len(x0)
		else:
			nrWakes = 1

		for j in range(nrWakes):
			level = maxLevel
			for i in range(maxLevel):
				name = 'viscBox_wake{:.0f}_{:.0f}'.format(j+1, i+1)
				length = length0*expansion**(i)
				cellLength = self.baseSize/(2**level+1)

				if i == 0:
					width  = width0
				else:
					width  += 2*cellLength*self.nCellsBetweenLevels

				width  = width0*expansion**(i)

				distance = width/2

				if isinstance(x0, list):
					minString = '({:.6f} {:.6f} -999)'.format(x0[j],          y0[j] - 1e-6)
					maxString = '({:.6f} {:.6f} 999)'.format( x0[j] + length, y0[j] + 1e-6)
				else:
					minString = '({:.6f} {:.6f} -999)'.format(x0,          y0 - 1e-6)
					maxString = '({:.6f} {:.6f} 999)'.format( x0 + length, y0 + 1e-6)

				self.snappyDict.addGeometry(name, 'searchableBox', {'min':minString, 'max':maxString})
				self.snappyDict.addRefinementRegion(name, 'distance', np.array([distance, level]))

				level -= 1

		distance = width0/2
		if self.nrFoils == 1:
			self.snappyDict.addRefinementRegion('foil', 'distance', np.array([distance, maxLevel]))
		else:
			for i in range(self.nrFoils):
				self.snappyDict.addRefinementRegion('foil{:.0f}'.format(i+1), 'distance', np.array([distance, maxLevel]))

	def addRotation(self, omega):
		boundaryString = '\t\ttype rotatingWallVelocity;\n\t\torigin (0.5 0 0);\n\t\taxis (0 0 -1);\n\t\tomega {};\n'.format(omega)

		FileHandling.changeBoundary(self.boundaryFolder+'U', 'foil', boundaryString)

def writeRunScript(caseNameList, foilSim):
	# Write run script
	f = open('run.sh', 'w')
	f.write('#!/bin/bash\n\n')

	if sys.platform == 'darwin':
		f = open(foilSim.foamPath + '/run.sh', 'w')
	else:
		f = open('run.sh', 'w')

	f.write('#!/bin/bash\n\n')

	if sys.platform == 'darwin':
		f.write('cd $HOME/workingDir/OpenFOAM/run/FoilSimulation\n\n')
	else:
		f.write('cd $FOAM_RUN/FoilSimulation\n\n')

	for i in range(len(caseNameList)):
		f.write('cd {0}\n'.format(caseNameList[i]))
		f.write('sh mesh.sh\n')

		f.write('sh runSim.sh\n')

		if sys.platform == 'darwin':
			f.write('cd $HOME/workingDir/OpenFOAM/run/FoilSimulation\n\n')
		else:
			f.write('cd $FOAM_RUN/FoilSimulation\n\n')

	f.close()

max_stars_repo_name: BeaAnn/pyfoamsetup

reason: import numpy,import scipy


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

"""Functions for generating sub-graphs appropriate for learning."""

import bz2
import logging

import networkx as nx
import pandas as pd
from tqdm import tqdm

__all__ = [
    'generate_subgraph'
]

logger = logging.getLogger(__name__)

def generate_subgraph(
    path,
    graph,
    *,
    n_positive: int,
    n_negative: int,
    max_simple_path_length: int = 3,
):
    """

    :param path:
    :param graph:
    :param n_positive:
    :param n_negative:
    :param max_simple_path_length: Maximum simple path length
    """
    logger.info(f'opening {path}')
    with bz2.BZ2File(path, 'r') as file:
        df_features = pd.read_csv(file, sep='\t', low_memory=False)

    subgraph_nodes = set()

    positive_list = []
    positive_labels = [1] * n_positive
    df_positive = df_features.loc[df_features['status'] == 1][:n_positive]
    it = tqdm(
        df_positive.iterrows(),
        total=len(df_positive.index),
        desc=f'Get positive simple paths < {max_simple_path_length}',
    )
    for _, row in it:
        source = 'Compound::' + row['compound_id']
        target = 'Disease::' + row['disease_id']
        positive_list.append([source, target])
        for path in nx.all_simple_paths(graph, source=source, target=target, cutoff=max_simple_path_length):
            subgraph_nodes.update(path)

    negative_list = []
    negative_labels = [0] * n_negative
    df_negative = df_features.loc[df_features['status'] == 0][:n_negative]
    it = tqdm(
        df_negative.iterrows(),
        total=len(df_negative.index),
        desc=f'Get negative simple paths < {max_simple_path_length}',
    )
    for _, row in it:
        source = 'Compound::' + row['compound_id']
        target = 'Disease::' + row['disease_id']
        negative_list.append([source, target])
        for path in nx.all_simple_paths(graph, source=source, target=target, cutoff=max_simple_path_length):
            subgraph_nodes.update(path)

    subgraph = graph.subgraph(subgraph_nodes)

    return subgraph, positive_list, positive_labels, negative_list, negative_labels


text:
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
import os
import sys
from PIL import Image
import numpy as np
from tqdm import tqdm
import time
from functools import wraps

def fn_timer(function):
    @wraps(function)
    def function_timer(*args, **kwargs):
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        print("Running [%s] consumes %.3f seconds" %
              (function.__name__, (t1 - t0))
              # (function.func_name, str(t1 - t0)) #for python2.x
              )
        return result

    return function_timer

class Img2Vec():
    def __init__(self, cuda=False, model='resnet-50', layer='default', layer_output_size=512):
        """ Img2Vec
        :param cuda: If set to True, will run forward pass on GPU
        :param model: String name of requested model
        :param layer: String or Int depending on model.  See more docs: https://github.com/christiansafka/img2vec.git
        :param layer_output_size: Int depicting the output size of the requested layer
        """
        self.device = torch.device("cuda" if cuda else "cpu")
        self.layer_output_size = layer_output_size
        self.model_name = model
        self.layer = layer

        self.model, self.extraction_layer = self._get_model_and_layer(model, layer)
        if self.layer in ('second_last', 'third_last'):
            self.feature_extractor = nn.Sequential(*self.extraction_layer)

        self.model = self.model.to(self.device)

        self.model.eval()

        self.scaler = transforms.Scale((224, 224))
        self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                              std=[0.229, 0.224, 0.225])
        self.to_tensor = transforms.ToTensor()

    def get_none_last_vec(self, img):
        image = self.normalize(self.to_tensor(self.scaler(img))).unsqueeze(0).to(self.device)
        with torch.no_grad():
            out_features = self.feature_extractor(image)
        out_features = out_features.permute(0, 2, 3, 1)
        bs_size = out_features.size()[0]
        feature_size = out_features.size()[-1]
        out_features = out_features.view(bs_size, -1, feature_size)
        return out_features

    def get_vec(self, img, tensor=False):
        """ Get vector embedding from PIL image
        :param img: PIL Image
        :param tensor: If True, get_vec will return a FloatTensor instead of Numpy array
        :returns: Numpy ndarray
        """
        image = self.normalize(self.to_tensor(self.scaler(img))).unsqueeze(0).to(self.device)

        if self.model_name == 'alexnet':
            my_embedding = torch.zeros(1, self.layer_output_size)
        else:
            my_embedding = torch.zeros(1, self.layer_output_size, 1, 1)

        def copy_data(m, i, o):
            # print(o.shape)
            my_embedding.copy_(o.data)

        h = self.extraction_layer.register_forward_hook(copy_data)

        # img2vec = Img2Vec(cuda=True)
        # Read in an image
        # img = Image.open(sys.argv[1])
        # print(image)

        h_x = self.model(image)
        h.remove()

        if tensor:
            return my_embedding
        else:
            if self.model_name == 'alexnet':
                return my_embedding.numpy()[0, :]
            else:
                return my_embedding.numpy()[0, :, :, :]

    def _get_model_and_layer(self, model_name, layer):
        """ Internal method for getting layer from model
        :param model_name: model name such as 'resnet-18'
        :param layer: layer as a string for resnet-18 or int for alexnet
        :returns: pytorch model, selected layer
        """
        if model_name == 'resnet-18':
            model = models.resnet18(pretrained=True)
            if layer == 'default':
                layer = model._modules.get('avgpool')
                self.layer_output_size = 512
            elif layer == 'second_last':
                layer = list(model.children())[:-2]
            elif layer == 'third_last':
                layer = list(model.children())[:-3]
            else:
                layer = model._modules.get(layer)

            return model, layer

        elif model_name == 'alexnet':
            model = models.alexnet(pretrained=True)
            if layer == 'default':
                layer = model.classifier[-2]
                self.layer_output_size = 4096
            else:
                layer = model.classifier[-layer]

            return model, layer

        elif model_name == 'resnet-50':
            model = models.resnet50(pretrained=True)
            if layer == 'default':
                layer = model._modules.get('avgpool')
                self.layer_output_size = 512 * 4
            elif layer == 'second_last':
                layer = list(model.children())[:-2]
            elif layer == 'third_last':
                layer = list(model.children())[:-3]
            else:
                layer = model._modules.get(layer)

            return model, layer

        elif model_name == 'resnet-101':
            model = models.resnet101(pretrained=True)
            if layer == 'default':
                layer = model._modules.get('avgpool')
                self.layer_output_size = 512 * 4
            elif layer == 'second_last':
                layer = list(model.children())[:-2]
            elif layer == 'third_last':
                layer = list(model.children())[:-3]
            else:
                layer = model._modules.get(layer)

            return model, layer

        else:
            raise KeyError('Model %s was not found' % model_name)

@fn_timer
def get_pooling_vector_dir():
    input_dir = sys.argv[1]
    if input_dir[-1] == "/":
        input_name = input_dir[:-1]
    else:
        input_name = input_dir
    input_name = input_name.split("/")[-1]
    vec_mat = []
    img2vec = Img2Vec(cuda=True)
    fout = open("image_fc_names_" + input_name, "w")
    for f in tqdm(os.listdir(input_dir)):
        try:
            img = Image.open(os.path.join(input_dir, f))
            vec = img2vec.get_vec(img)
            vec_mat.append(vec)
            fout.write(f + "\n")
        except:
            print(f)
    vec_mat = np.array(vec_mat).squeeze()
    fout.close()
    np.save("image_fc_vectors_" + input_name, vec_mat)

@fn_timer
def get_patch_vector_dir():
    input_dir = sys.argv[1]
    if input_dir[-1] == "/":
        input_name = input_dir[:-1]
    else:
        input_name = input_dir
    input_name = input_name.split("/")[-1]
    vec_mat = []
    img2vec = Img2Vec(cuda=True, model='resnet-101', layer='second_last')

    fout = open(input_dir + "/../image_patch_names_" + input_name, "w")
    for f in tqdm(os.listdir(input_dir)):
        try:
            img_file = os.path.join(input_dir, f)
            vec = get_patch_vector_file(img_file, img2vec)
            vec_mat.append(vec)
            fout.write(f + "\n")
        except:
            print(f)
    vec_mat = np.array(vec_mat).squeeze()
    fout.close()
    np.save(input_dir + "/../image_patch_vectors_" + input_name, vec_mat)
    sys.stdout.write("The Task Is Finished! Got Numpy Data: {}\n".format(vec_mat.shape))

@fn_timer
def get_patch_vector_dir_single():
    input_dir = sys.argv[1]
    if input_dir[-1] == "/":
        input_name = input_dir[:-1]
    else:
        input_name = input_dir
    input_name = input_name.split("/")[-1]
    vec_mat = []
    img2vec = Img2Vec(cuda=True, model='resnet-101', layer='second_last')

    fout = open(input_dir + "/../image_patch_names_single_" + input_name, "w")
    for f in tqdm(os.listdir(input_dir)):
        try:
            img_file = os.path.join(input_dir, f)
            vec = get_patch_vector_file(img_file, img2vec)
            np.save(input_dir + "/../image_patch_vectors/" + str(f.split('.')[0]), vec)
            # vec_mat.append(vec)
            fout.write(f + "\n")
        except:
            print(f)
    vec_mat = np.array(vec_mat).squeeze()
    fout.close()
    sys.stdout.write("The Task Is Finished! Got Numpy Data: {}\n".format(vec_mat.shape))

@fn_timer
def get_patch_vector_file(img_file, img2vec):
    img = Image.open(img_file)
    vec = img2vec.get_none_last_vec(img)
    vec_np = vec.cpu().numpy()
    return vec_np

@fn_timer
def get_pooling_vector_file(img_file, img2vec):
    img = Image.open(img_file)
    vec = img2vec.get_vec(img)
    return vec

@fn_timer
def get_numpy_vec(input_file, idx=None):
    vecs = np.load(input_file)
    if idx is not None:
        return vecs[idx]

@fn_timer
def get_numpy_vec_single(input_file):
    vec = np.load(input_file)
    return vec

if __name__ == "__main__":
    # test
    # img_file = sys.argv[1]
    #
    # img2vec = Img2Vec(cuda=True, model='resnet-101', layer='second_last')
    # vec_file = get_patch_vector_file(img_file, img2vec)
    # print(np.shape(vec_file[0]), vec_file[0])

    # vec_np = get_numpy_vec(input_file=sys.argv[2], idx=int(sys.argv[3]))
    # print(np.shape(vec_np), vec_np)
    #
    # print(vec_file[0] == vec_np)
    # print((vec_file[0] == vec_np).all())

    vec_np_single = get_numpy_vec_single(input_file=sys.argv[1])
    print(np.shape(vec_np_single), vec_np_single)