text:
import numpy as np

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

from flask import Flask, jsonify

# Database Setup
engine = create_engine("sqlite:///Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()

# reflect the tables
Base.prepare(engine, reflect=True)

# Save reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station

# Flask Setup
app = Flask(__name__)

# Flask Routes

# Route 1

@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/<start>
"
        f"/api/v1.0/<start>/<end>
"
    )

# Route 2

@app.route("/api/v1.0/precipitation")
def precipitation():
    # Create our session (link) from Python to the DB
    session = Session(engine)

    """Return a list of all passenger names"""
    # Query last year of precipitation data
    prcp_results = session.query(Measurement.date, Measurement.prcp).filter(Measurement.date > '2016-08-23').order_by(Measurement.date).all()

    session.close()

    # Convert list of tuples into normal list
    latest_prcp = list(np.ravel(prcp_results))

    return jsonify(latest_prcp)

# Route 3

@app.route("/api/v1.0/stations")

def stations():
    session = Session(engine)
    stations_results = session.query(Station.station).all()
    session.close
    stations_list = list(np.ravel(stations_results))
    return jsonify(stations_list)

# Route 4

@app.route("/api/v1.0/tobs")

def tobs():
    session = Session(engine)
    tobs_results = session.query(Measurement.date, Measurement.tobs).filter(Measurement.date > '2016-08-23').all()
    session.close
    tobs_list = list(np.ravel(tobs_results))
    return jsonify(tobs_list)

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

max_stars_repo_name: SumitaJain/SJ-SQLAlchemy-Challenge

max_issues_repo_name: SumitaJain/SJ-SQLAlchemy-Challenge

max_forks_repo_name: SumitaJain/SJ-SQLAlchemy-Challenge


text:
#!/usr/bin/env python

import rospy
import numpy as np
import numpy.random as rand
import tf
from race.msg import drive_param
from geometry_msgs.msg import PoseStamped, Pose
from gazebo_msgs.msg import ModelStates
from gazebo_msgs.srv import GetModelState
from std_msgs.msg import Header

pub = rospy.Publisher("pf/viz/inferred_pose", PoseStamped, queue_size=1)

racecar_pose = Pose()

def timer_callback(event):
    global racecar_pose
    msg = PoseStamped()
    msg.pose = racecar_pose
    pub.publish(msg)

# Gets the racecar pose from gazebo/model_states topic. Since Gazebo has multiple
# models (racecar, ground plane) we have to index for the "racecar".
def robot_pose_update(data):
    global racecar_pose
    names = data.name
    # Break out of this callback if racecar not properly initialized in Gazebo yet so that we don't see red error statements in Terminal about no index of "racecar" in names.
    if len(names) < 2:
        return
    racecar_index = names.index("racecar")
    racecar_pose = data.pose[racecar_index]

if __name__ == "__main__":
    rospy.init_node("remap_gazebo_pose")
    # Set the update rate
    rospy.Timer(rospy.Duration(.025), timer_callback) # 40hz
    # Set subscribers
    rospy.Subscriber("gazebo/model_states", ModelStates, robot_pose_update)
    rospy.spin()

ext: py

reason: import numpy


text:
#!/usr/bin/python3
import scipy.fftpack
from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt
import math

import clientFFT

# Number of samplepoints -- FFT size
N = 256
# sampling frequency : fs
# maximum frequency that can be measured "fs/2" Nyquist's theorem
fs= 9000 #Hz
#fs=Fs
# sampling time period = 1/fs
T = 1/fs
# length of the signal in time
L = N*T

def downSampling(fsIn,fsOut,dataIn,N):
	dataOut = []
	dataOut.append(dataIn[0])
	assert fsIn >= fsOut, "Original sampling is lessthan required"
	r = fsIn/fsOut
	for j in range(1,N):
		d,i = math.modf(r*j)
		if(int(d) == 0):
			dataOut.append(dataIn[int(i)])
		else :
			d1,i1 = math.modf(r*(j+1))
			n = dataIn[int(i)]
			m = dataIn[int(i1)]
			p = (n+m)/2
			dataOut.append(p)

	return dataOut

def inputDataFFT():
	Fs, audstrm = wavfile.read('cow.wav')

	actual_N = len(audstrm.T[0])
	actual_t = np.linspace(0.0,actual_N/Fs,actual_N)
	# input signal - downSampled
	downSampled = np.array(downSampling(Fs,fs,audstrm.T[0],N))
	#print(downSampled)
	# 16-bit data, normalized [-1,1]
	y = (downSampled/(2**16))*2
	return y

def runFFT(y):
	return scipy.fftpack.fft(y)

def runIFFT(yf):
	return scipy.fftpack.ifft(yf)

def plotFFT(y,yfft,yCnstr):
	fig, (ploty,plotfft,plotyCnstr) = plt.subplots(nrows=1,ncols=3,sharex=False,sharey=False)

	# Vector with sampling time-stamps
	t = np.linspace(0.0,L,N) # input time-scale
	# Vector with fft-output frequency scale
	feqScale = np.array(list(range(0,int(N/2)))).astype(float) * fs/N
	# yfabs[0] gives the DC component of the input signal
	# If the FFT is input is just real numbers (true for all real world apps.)
	#   second half of fft-out is complex conjugate of first half of the fft-out
	#<VVI> Why are we scaling the output ??
	yfftabs = np.abs(yfft[0:int(N/2)]) * 2/N
	yfftabs[0] = 0 #ignore dc component
	#print(yfft)
	#print(yfftabs)
	yifft = yCnstr.real

	#datain.plot(actual_t,audstrm.T[0])
	ploty.plot(t,y)
	plotfft.plot(feqScale,yfftabs)
	plotyCnstr.plot(t,yifft)

	plt.show()

if __name__ == "__main__" :
	x = inputDataFFT()
	#fft = runFFT(x)
	fft = clientFFT.outSourceFFT(x,N)
	ifft= runIFFT(fft)
	plotFFT(x,fft,ifft)

max_stars_repo_name: madhavakrishna/libStubs

max_issues_repo_name: madhavakrishna/libStubs

max_forks_repo_name: madhavakrishna/libStubs

reason: import numpy,import scipy,from scipy


text:
#!/usr/bin/env python
# coding: utf-8

# In[1]:

#read in ipsilateral breast labelmap/volume
#mask this patient's breast
#generate histogram of intensity
#DIR to new patient's breast
#expand/dilate region (might need to be manual)
#mask new patient's breast
#generate histogram of intensity

# In[2]:

#import modules
import SimpleITK as sitk

from platipy.imaging.visualisation.tools import ImageVisualiser
from platipy.imaging.utils.tools import get_com
import matplotlib.pyplot as plt
import numpy as np

get_ipython().run_line_magic('matplotlib', 'notebook')

# In[3]:

R_breast=sitk.ReadImage("/home/alicja/Downloads/Segmentation.nii.gz")

# In[4]:

WES_010_4_B50T=sitk.ReadImage("/home/alicja/Documents/WES_010/IMAGES/WES_010_4_20180829_MR_EP2D_DIFF_TRA_SPAIR_ZOOMIT_EZ_B50T_EP2D_DIFF_TRA_SPAIR_ZOOMIT_TRACEW_DFC_MIX_5.nii.gz")
WES_010_4_B800T=sitk.ReadImage("/home/alicja/Documents/WES_010/IMAGES/WES_010_4_20180829_MR_EP2D_DIFF_TRA_SPAIR_ZOOMIT_EZ_B800T_EP2D_DIFF_TRA_SPAIR_ZOOMIT_TRACEW_DFC_MIX_5.nii.gz")

# In[5]:

masked_R_breast = sitk.Mask(WES_010_4_B50T, R_breast)

# In[10]:

values = sitk.GetArrayViewFromImage(masked_R_breast).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(200,900,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[7]:

#Use these values to do thresholding
def estimate_tumour_vol(img_mri, lowerthreshold=300, upperthreshold=3000, hole_size=1):
    label_threshold = sitk.BinaryThreshold(img_mri, lowerThreshold=lowerthreshold, upperThreshold=upperthreshold)
    label_threshold_cc = sitk.RelabelComponent(sitk.ConnectedComponent(label_threshold))
    label_threshold_cc_x = (label_threshold_cc==1)
    label_threshold_cc_x_f = sitk.BinaryMorphologicalClosing(label_threshold_cc_x, (hole_size,hole_size,hole_size))
    return(label_threshold_cc_x_f)

# In[12]:

image_mri=WES_010_4_B50T
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=720, upperthreshold=3000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_010_4_B50T_hist.nii.gz") #works well

# In[35]:

np.max(label_threshold_cc_x_f)

# In[60]:

masked_R_breast_B800T = sitk.Mask(WES_010_4_B800T, R_breast)
values = sitk.GetArrayViewFromImage(masked_R_breast_B800T).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(1,600,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[61]:

image_mri=WES_010_4_B800T
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=300, upperthreshold=3000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_010_4_B800T_hist.nii.gz") #works super well

# In[15]:

WES_010_4_T2w=sitk.ReadImage("/home/alicja/Documents/WES_010/IMAGES/WES_010_4_20180829_MR_T2_TSE_TRA_SPAIR_TSE2D1_11_T2_TSE_TRA_SPAIR_3.nii.gz")

# In[63]:

WES_010_4_T2w=sitk.Resample(WES_010_4_T2w, WES_010_4_B50T)
masked_R_breast_T2w = sitk.Mask(WES_010_4_T2w, R_breast)
values = sitk.GetArrayViewFromImage(masked_R_breast_T2w).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(1,300,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[64]:

image_mri=WES_010_4_T2w
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=170, upperthreshold=3000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_010_4_T2w_hist.nii.gz") #works well too

# In[13]:

WES_010_4_MPE=sitk.ReadImage("MPE_sub_WES_010_4.nii.gz")

# In[17]:

WES_010_4_MPE=sitk.Resample(WES_010_4_MPE, WES_010_4_B50T)
masked_R_breast_MPE = sitk.Mask(WES_010_4_MPE, R_breast)
values = sitk.GetArrayViewFromImage(masked_R_breast_MPE).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(150,450,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[20]:

image_mri=WES_010_4_MPE
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=230, upperthreshold=3000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_010_4_MPE_hist.nii.gz") #good

# In[ ]:

# In[65]:

from platipy.imaging.visualisation.tools import ImageVisualiser

from platipy.imaging.registration.registration import (
    initial_registration,
    fast_symmetric_forces_demons_registration,
    transform_propagation,
    apply_field
)

# In[66]:

#DIR to Patient 8
WES_008_4_B50T=sitk.ReadImage("/home/alicja/Documents/WES_008/IMAGES/WES_008_4_20180619_MR_EP2D_DIFF_TRA_SPAIR_ZOOMIT_EZ_B50T_EP2D_DIFF_TRA_SPAIR_ZOOMIT_TRACEW_DFC_MIX_5.nii.gz")
image_to_0_rigid, tfm_to_0_rigid = initial_registration(
    WES_008_4_B50T,
    WES_010_4_B50T,
    options={
        'shrink_factors': [8,4],
        'smooth_sigmas': [0,0],
        'sampling_rate': 0.5,
        'final_interp': 2,
        'metric': 'mean_squares',
        'optimiser': 'gradient_descent_line_search',
        'number_of_iterations': 25},
    reg_method='Rigid')

image_to_0_dir, tfm_to_0_dir = fast_symmetric_forces_demons_registration(
    WES_008_4_B50T,
    image_to_0_rigid,
    resolution_staging=[4,2],
    iteration_staging=[10,10]
)

R_breast_to_0_rigid = transform_propagation(
    WES_008_4_B50T,
    R_breast,
    tfm_to_0_rigid,
    structure=True
)

R_breast_to_0_dir = apply_field(
    R_breast_to_0_rigid,
    tfm_to_0_dir,
    structure=True
)

# In[67]:

vis = ImageVisualiser(WES_008_4_B50T, axis='z', cut=get_com(R_breast_to_0_dir), window=[-250, 500])
vis.add_contour(R_breast_to_0_dir, name='BREAST', color='g')
fig = vis.show()

# In[78]:

breast_contour_dilate=sitk.BinaryDilate(R_breast_to_0_dir, (2,2,2))

# In[79]:

vis = ImageVisualiser(WES_008_4_B50T, axis='z', cut=get_com(R_breast_to_0_dir), window=[-250, 500])
vis.add_contour(breast_contour_dilate, name='BREAST', color='g')
fig = vis.show()

# In[80]:

masked_R_breast = sitk.Mask(WES_008_4_B50T, breast_contour_dilate)

# In[92]:

values = sitk.GetArrayViewFromImage(masked_R_breast).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(200,3000,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[93]:

image_mri=WES_008_4_B50T
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=1400, upperthreshold=5000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_008_4_B50T_hist.nii.gz") #good but seems to contain
#fibroglandular tissue as well

# In[95]:

WES_008_4_B800T=sitk.ReadImage("/home/alicja/Documents/WES_008/IMAGES/WES_008_4_20180619_MR_EP2D_DIFF_TRA_SPAIR_ZOOMIT_EZ_B800T_EP2D_DIFF_TRA_SPAIR_ZOOMIT_TRACEW_DFC_MIX_5.nii.gz")
WES_008_4_T2w=sitk.ReadImage("/home/alicja/Documents/WES_008/IMAGES/WES_008_4_20180619_MR_T2_TSE_TRA_SPAIR_TSE2D1_11_T2_TSE_TRA_SPAIR_3.nii.gz")

# In[96]:

masked_R_breast_B800T = sitk.Mask(WES_008_4_B800T, breast_contour_dilate)
values = sitk.GetArrayViewFromImage(masked_R_breast_B800T).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(1,600,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[99]:

image_mri=WES_008_4_B800T
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=480, upperthreshold=5000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_008_4_B800T_hist.nii.gz") #good

# In[104]:

WES_008_4_T2w=sitk.Resample(WES_008_4_T2w,WES_008_4_B800T)

masked_R_breast_T2w = sitk.Mask(WES_008_4_T2w, breast_contour_dilate)
values = sitk.GetArrayViewFromImage(masked_R_breast_T2w).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(1,250,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[105]:

image_mri=WES_008_4_T2w
arr_mri = sitk.GetArrayFromImage(image_mri)
arr_mri[:,:,arr_mri.shape[2]//2:] = 0
image_mri_masked=sitk.GetImageFromArray(arr_mri)
image_mri_masked.CopyInformation(image_mri)

label_threshold_cc_x_f=estimate_tumour_vol(image_mri_masked, lowerthreshold=197, upperthreshold=5000, hole_size=1)

sitk.WriteImage(label_threshold_cc_x_f,"test_label_threshold_008_4_T2w_hist.nii.gz") #okay but picks up
#fibroglandular tissue

# In[106]:

L_breast=sitk.ReadImage("contralateral_segmentation.nii.gz")

# In[107]:

L_breast_to_0_rigid = transform_propagation(
    WES_008_4_B50T,
    L_breast,
    tfm_to_0_rigid,
    structure=True
)

L_breast_to_0_dir = apply_field(
    L_breast_to_0_rigid,
    tfm_to_0_dir,
    structure=True
)

# In[110]:

L_breast_contour_dilate=sitk.BinaryDilate(L_breast_to_0_dir, (4,4,4))
vis = ImageVisualiser(WES_008_4_B50T, axis='z', cut=get_com(L_breast_to_0_dir), window=[-250, 500])
vis.add_contour(L_breast_contour_dilate, name='BREAST', color='g')
fig = vis.show()

# In[111]:

masked_L_breast = sitk.Mask(WES_008_4_B50T, L_breast_contour_dilate)

values = sitk.GetArrayViewFromImage(masked_L_breast).flatten()

fig, ax = plt.subplots(1,1)
ax.hist(values, bins=np.linspace(200,3000,50), histtype='stepfilled', lw=2)
#ax.set_yscale('log')
ax.grid()
ax.set_axisbelow(True)
ax.set_xlabel('Intensity')
ax.set_ylabel('Frequency')
fig.show()

# In[ ]:

max_stars_repo_path: Old MRI segmentation code/Histogram-segmentation.py


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

import os
import subprocess
import sys
from os.path import abspath
from time import sleep
from typing import Optional

import numpy as np
import torch

from pytorch_lightning.utilities.distributed import find_free_network_port
from pytorch_lightning.accelerators.ddp_base_backend import DDPBase

try:
    from hydra.utils import to_absolute_path, get_original_cwd
    from hydra.core.hydra_config import HydraConfig
except ImportError:
    HYDRA_AVAILABLE = False
else:
    HYDRA_AVAILABLE = True

class DDPBackend(DDPBase):

    def __init__(self, trainer, mode: str = 'ddp'):
        super().__init__(trainer)
        self.task_idx = None
        self._has_spawned_children = False
        self.mode = mode

    def setup(self, model):
        if self.mode == 'ddp':
            self.__ddp_script_mode_setup()
        elif self.mode == 'slurm_ddp':
            self.__slurm_setup()
        elif self.mode == 'torchelastic_ddp':
            self.__torchelastic_setup()

        self.trainer.model = model

    def __slurm_setup(self):
        self.task_idx = int(os.environ['SLURM_LOCALID'])

    def __torchelastic_setup(self):
        self.task_idx = int(os.environ['LOCAL_RANK'])

    def __ddp_script_mode_setup(self):
        assert self.trainer.global_rank == 0
        self._check_can_spawn_children()
        self._has_spawned_children = True

        os.environ['MASTER_ADDR'] = os.environ.get('MASTER_ADDR', '127.0.0.1')
        os.environ['MASTER_PORT'] = os.environ.get('MASTER_PORT', str(find_free_network_port()))

        # allow the user to pass the node rank
        node_rank = '0'
        node_rank = os.environ.get('NODE_RANK', node_rank)
        node_rank = os.environ.get('GROUP_RANK', node_rank)
        os.environ['NODE_RANK'] = node_rank
        os.environ['LOCAL_RANK'] = '0'

        # when user is using hydra find the absolute path
        path_lib = abspath if not HYDRA_AVAILABLE else to_absolute_path

        # pull out the commands used to run the script and resolve the abs file path
        command = sys.argv
        try:
            full_path = path_lib(command[0])
        except Exception as e:
            full_path = abspath(command[0])

        command[0] = full_path
        # use the same python interpreter and actually running
        command = [sys.executable] + command

        # the visible devices tell us how many GPUs we want to use.
        # when the trainer script was called the device has already been scoped by the time
        # code reaches this point. so, to call the scripts, we need to leave cuda visible devices alone
        # but forward the GPUs selected via environment variables
        os.environ['PL_TRAINER_GPUS'] = ','.join([str(i) for i in self.trainer.data_parallel_device_ids])
        os.environ['PL_IN_DDP_SUBPROCESS'] = '1'

        if self.trainer.logger is not None:
            os.environ['PL_EXP_VERSION'] = str(self.trainer.logger.version)

        gpu_ids = os.environ.get('CUDA_VISIBLE_DEVICES', '')
        if len(gpu_ids) == 1:
            gpu_ids = f'{gpu_ids},'

        num_gpus = max(1, len(gpu_ids.split(',')))

        os.environ['WORLD_SIZE'] = f'{num_gpus * self.trainer.num_nodes}'

        self.trainer.interactive_ddp_procs = []
        for local_rank in range(1, self.trainer.num_processes):
            env_copy = os.environ.copy()
            env_copy['LOCAL_RANK'] = f'{local_rank}'
            env_copy['PL_DDP_PID'] = str(self.trainer.data_parallel_device_ids[local_rank])

            # start process
            # if hydra is available and initialized, make sure to set the cwd correctly
            cwd: Optional[str] = None
            if HYDRA_AVAILABLE:
                if HydraConfig.initialized():
                    cwd = get_original_cwd()
            proc = subprocess.Popen(command, env=env_copy, cwd=cwd)
            self.trainer.interactive_ddp_procs.append(proc)

            # starting all processes at once can cause issues
            # with dataloaders delay between 1-10 seconds
            delay = np.random.uniform(1, 5, 1)[0]
            sleep(delay)

        self.task_idx = 0

    def train(self):
        model = self.trainer.model
        if self.mode == 'ddp':
            results = self.ddp_train_tmp(process_idx=self.task_idx, mp_queue=None, model=model, is_master=True)
            del os.environ['WORLD_SIZE']
            return results
        else:
            self.ddp_train_tmp(process_idx=self.task_idx, mp_queue=None, model=model)

    def _check_can_spawn_children(self):
        if self._has_spawned_children:
            raise RuntimeError(
                "You tried to run `.fit` or `.test` multiple times in the same script."
                " This is not supported in DDP mode, switch to `distributed_backend='ddp_spawn'` instead."
            )

    def set_world_ranks(self, process_idx):
        self.trainer.local_rank = process_idx
        self.trainer.global_rank = self.trainer.node_rank * self.trainer.num_processes + process_idx
        self.trainer.world_size = self.trainer.num_nodes * self.trainer.num_processes

    def model_to_device(self, model, process_idx, is_master):
        gpu_idx = process_idx

        # when using ddp, the master process (proc 0) continues running as the main one
        # this means that the local rank will always be 0
        # (even if cuda visible devices has other visible gpus)
        # this means that the master process needs to pull the 0th visible index as the device number
        if is_master:
            available_gpus = os.environ['CUDA_VISIBLE_DEVICES'].split(',')
            gpu_idx = int(available_gpus[self.trainer.local_rank])

        gpu_idx = int(os.environ.get('PL_DDP_PID', gpu_idx))

        self.trainer.root_gpu = gpu_idx
        torch.cuda.set_device(self.trainer.root_gpu)
        model.cuda(self.trainer.root_gpu)

    def get_device_ids(self):
        device_ids = [self.trainer.root_gpu]
        return device_ids

max_stars_repo_name: chiragraman/pytorch-lightning

max_issues_repo_name: chiragraman/pytorch-lightning

max_forks_repo_name: chiragraman/pytorch-lightning


text:
#!/usr/bin/env python
r"""Test :py:class:`~solarwindpy.core.ions.Ion`.
"""
import pdb

# import re as re
import numpy as np
import pandas as pd
import unittest

# import sys
# import itertools

# from numbers import Number
# from pandas import MultiIndex as MI

# import numpy.testing as npt
import pandas.testing as pdt

from abc import ABC, abstractproperty

# from abc import abstractmethod, abstractstaticmethod, abstractclassmethod
# from unittest import TestCase

from scipy import constants
from scipy.constants import physical_constants

# try:
#     import test_base as base
# except ImportError:
#     from . import test_base as base
from solarwindpy.tests import test_base as base

from solarwindpy import vector
from solarwindpy import tensor
from solarwindpy import ions

pd.set_option("mode.chained_assignment", "raise")

class IonTestBase(ABC):
    @classmethod
    def set_object_testing(cls):
        # print(cls.__class__, "set_object_testing", flush=True)
        # print("Data", cls.data, sep="\n")
        data = cls.data.xs(cls().species, axis=1, level="S")

        w = data.w
        coeff = pd.Series({"par": 1.0, "per": 2.0}) / 3.0
        kwargs = dict(axis=1, level="C")
        scalar = w.pow(2).multiply(coeff, **kwargs).sum(axis=1).pipe(np.sqrt)
        scalar.name = ("w", "scalar")
        data = pd.concat([data, scalar], axis=1, sort=True)
        data.columns = pd.MultiIndex.from_tuples(data.columns, names=["M", "C"])

        ion = ions.Ion(data, cls().species)
        cls.object_testing = ion
        cls.data = data
        # print("Done with", cls.__class__, flush=True)

    @abstractproperty
    def species(self):
        pass

    @classmethod
    def ion(cls):
        return cls._ion

    @property
    def mass(self):
        trans = {
            "a": "alpha particle",
            "p": "proton",
            "p1": "proton",
            "p2": "proton",
            "e": "electron",
        }
        m = physical_constants["%s mass" % trans[self.species]][0]
        return m

    @property
    def mass_in_mp(self):
        trans = {
            "a": physical_constants["alpha particle-proton mass ratio"][0],
            "p": 1,
            "p1": 1,
            "p2": 1,
            "e": physical_constants["electron-proton mass ratio"][0],
        }
        return trans[self.species]

    def test_species(self):
        self.assertEqual(self.species, self.object_testing.species)

    def test_n(self):
        n = self.data.loc[:, ("n", "")]
        if not isinstance(n, pd.Series):
            assert n.shape[1] == 1
            n = n.iloc[:, 0]
        n.name = "n"
        ot = self.object_testing
        pdt.assert_series_equal(n, ot.n)
        pdt.assert_series_equal(n, ot.number_density)
        pdt.assert_series_equal(ot.number_density, ot.n)

    def test_mass_density(self):
        mmp = self.mass_in_mp
        rho = self.data.loc[:, pd.IndexSlice["n", ""]] * mmp
        rho.name = self.species
        if not isinstance(rho, pd.Series):
            assert rho.shape[1] == 1
            rho = rho.iloc[:, 0]
        rho.name = "rho"
        ot = self.object_testing
        pdt.assert_series_equal(ot.rho, ot.mass_density)
        pdt.assert_series_equal(rho, ot.rho)
        pdt.assert_series_equal(rho, ot.mass_density)

    def test_v(self):
        v = vector.Vector(self.data.v)
        ot = self.object_testing
        self.assertEqual(v, ot.velocity)
        self.assertEqual(v, ot.v)
        self.assertEqual(ot.velocity, ot.v)

    def test_w(self):
        w = tensor.Tensor(self.data.w)
        ot = self.object_testing
        self.assertEqual(w, ot.thermal_speed)
        self.assertEqual(w, ot.w)
        self.assertEqual(ot.w, ot.thermal_speed)

    def test_anisotropy(self):
        w = self.data.w
        ani = (w.per / w.par).pow(2)
        ani.name = "RT"
        ot = self.object_testing
        pdt.assert_series_equal(ani, ot.anisotropy)

    def test_pth(self):
        # pth = nkT = 0.5 * n * m * w^2
        n = self.data.n * 1e6
        w = self.data.w * 1e3
        m = self.mass
        p = w.pow(2).multiply(0.5 * n * m, axis=0) / 1e-12
        pdt.assert_frame_equal(p, self.object_testing.pth)

    def test_temperature(self):
        # 0.5 mw^2 = kT
        # T = 0.5 mw^2 / k
        m = self.mass
        w = self.data.w * 1e3
        kb = physical_constants["Boltzmann constant"][0]
        t = (0.5 * m / kb) * w.pow(2) / 1e5
        pdt.assert_frame_equal(t, self.object_testing.temperature)

    def test_specific_entropy(self):
        rho = self.mass * self.data.n * 1e6
        w = self.data.w.xs("scalar", axis=1) * 1e3
        pth = w.pow(2).multiply(0.5 * rho, axis=0)

        #         ln_pth = np.log(pth)
        #         ln_rho = np.log(rho)
        #
        gamma = 5.0 / 3.0
        units = 1e4 / constants.e
        S = pth.multiply(rho.pow(-gamma)) / units
        S.name = "S"
        #         print(
        #             "<specific_entropy>",
        #             "<s>",
        #             self.species,
        #             "<test>",
        #             "<ln_pth>",
        #             ln_pth,
        #             "<ln_rho>",
        #             ln_rho,
        #             "<lnS>",
        #             lnS,
        #             sep="\n",
        #         )

        ot = self.object_testing
        pdt.assert_series_equal(S, ot.specific_entropy)
        pdt.assert_series_equal(S, ot.S)
        pdt.assert_series_equal(ot.S, ot.specific_entropy)

class TestIonA(base.AlphaTest, IonTestBase, base.SWEData):
    pass

class TestIonP1(base.P1Test, IonTestBase, base.SWEData):
    pass

class TestIonP2(base.P2Test, IonTestBase, base.SWEData):
    pass

class TestIonSpecificsOptions(base.TestData):
    @classmethod
    def setUpClass(cls):
        r"""
        Override `setUpClass` because this set of tests
        doesn't rely on `object_testing`.
        """
        # print("SWEData.setUpClass", flush=True)
        super(TestIonSpecificsOptions, cls).setUpClass()
        # print(cls.data.iloc[:, :7])
        # print(cls.data.columns.values)
        cls.data = cls.data.xs("", axis=1, level="N")

    #     def test_init_with_species(self):
    #         species = "a"
    #         data = self.data.xs(species, axis=1, level="S", drop_level=False)
    #         ion = ions.Ion(data, species)
    #         self.assertIsInstance(ion, ions.Ion)
    #         self.assertEqual(species, ion.species)
    #         pdt.assert_frame_equal(data, ion.data)
    #
    #         species = "p1"
    #         data = self.data.xs(species, axis=1, level="S", drop_level=False)
    #         ion = ions.Ion(data, species)
    #         self.assertIsInstance(ion, ions.Ion)
    #         self.assertEqual(species, ion.species)
    #         pdt.assert_frame_equal(data, ion.data)
    #
    #         species = "p1"
    #         data = self.data.xs(species, axis=1, level="S", drop_level=False)
    #         ion = ions.Ion(data, species)
    #         self.assertIsInstance(ion, ions.Ion)
    #         self.assertEqual(species, ion.species)
    #         pdt.assert_frame_equal(data, ion.data)

    def test_init_with_species(self):
        species = "a"
        data = self.data.xs(species, axis=1, level="S")
        ion = ions.Ion(data, species)
        self.assertIsInstance(ion, ions.Ion)
        self.assertEqual(species, ion.species)
        pdt.assert_frame_equal(data, ion.data)

        species = "p1"
        data = self.data.xs(species, axis=1, level="S")
        ion = ions.Ion(data, species)
        self.assertIsInstance(ion, ions.Ion)
        self.assertEqual(species, ion.species)
        pdt.assert_frame_equal(data, ion.data)

        species = "p2"
        data = self.data.xs(species, axis=1, level="S")
        ion = ions.Ion(data, species)
        self.assertIsInstance(ion, ions.Ion)
        self.assertEqual(species, ion.species)
        pdt.assert_frame_equal(data, ion.data)

    def test_init_version_comparison(self):
        species = "a"
        data_with_species = self.data.xs(species, axis=1, level="S", drop_level=False)
        data_without_species = self.data.xs(species, axis=1, level="S")
        ion_with_species = ions.Ion(data_with_species, species)
        ion_without_species = ions.Ion(data_without_species, species)
        self.assertEqual(ion_with_species, ion_without_species)

        species = "p1"
        data_with_species = self.data.xs(species, axis=1, level="S", drop_level=False)
        data_without_species = self.data.xs(species, axis=1, level="S")
        ion_with_species = ions.Ion(data_with_species, species)
        ion_without_species = ions.Ion(data_without_species, species)
        self.assertEqual(ion_with_species, ion_without_species)

        species = "p2"
        data_with_species = self.data.xs(species, axis=1, level="S", drop_level=False)
        data_without_species = self.data.xs(species, axis=1, level="S")
        ion_with_species = ions.Ion(data_with_species, species)
        ion_without_species = ions.Ion(data_without_species, species)
        self.assertEqual(ion_with_species, ion_without_species)

    def test_eq(self):
        s0 = "a"
        s1 = "a"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertEqual(i0, i1)

        s0 = "p1"
        s1 = "p1"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertEqual(i0, i1)

        s0 = "p2"
        s1 = "p2"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertEqual(i0, i1)

        s0 = "a"
        s1 = "p1"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertNotEqual(i0, i1)

        s0 = "p1"
        s1 = "a"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertNotEqual(i0, i1)

        s0 = "a"
        s1 = "p2"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertNotEqual(i0, i1)

        s0 = "p2"
        s1 = "p1"
        i0 = ions.Ion(self.data, s0)
        i1 = ions.Ion(self.data, s1)
        self.assertNotEqual(i0, i1)

if __name__ == "__main__":
    # Just make recursion stacks smaller in Terminal.
    # Comment this line if it causes problems with other
    # tests or decrease the denominator.
    # sys.setrecursionlimit(sys.getrecursionlimit() // 10)

    try:
        run_this_test = None
        run_this_test = "TestIonA"
        unittest.main(verbosity=2, defaultTest=run_this_test, failfast=True)

    except (  # noqa: F841
        AssertionError,
        AttributeError,
        ValueError,
        TypeError,
        IndexError,
    ) as e:
        import sys
        import traceback as tb

        exc_info = sys.exc_info()
        tb.print_exception(*exc_info)
        pdb.post_mortem(exc_info[-1])

max_stars_repo_name: blalterman/SolarWindPy

max_issues_repo_name: blalterman/SolarWindPy

max_forks_repo_name: blalterman/SolarWindPy

reason: import numpy,from scipy


text:
import os
import numpy as np
from pathlib import Path
import linecache

class Class_Converter(object):
	######################################################################################################
	#Formats the file into a desired format
	def __init__(self,Aixlib_path):
		self.Aixlib_path =Aixlib_path

	## Read the modelica code and formats it to a better reading order.
	#The parser will work with the newly formatted files later.
	def Model_Converter(self, filename_input, output_path, RelationModel, Aixlib_path):
		Data = DataCheck(Aixlib_path)
		outputpath_temp = Data.set_temp_directory(output_path)
		self.RelationModel = RelationModel.split(".")
		self.RelationModel = self.RelationModel[len(self.RelationModel) - 1]
		RelationModel = self.RelationModel
		filename_output = Data.set_filename_output(filename_input, output_path, Aixlib_path, RelationModel)

		readfile_in = open(filename_input, 'r')
		tempFilename_output = Data.set_tempFilename_output(output_path, filename_input, Aixlib_path, RelationModel)
		readfile_out = open(filename_output, "w")
		self.readfile_in = open(filename_input, "r")
		self.readfile_out = open(tempFilename_output, "w")
		count = 0
		counterlist = []
		for line in self.readfile_in.readlines():
			count = count +1
			line = line.lstrip()
			x = line.split()
			x_array = np.asarray(x)
			if line.find("/*")>-1:
				counterlist.append(count)
				continue
			if line.find("*/")>-1:
				counterlist.append(count)
				continue
			if len(line) > 1 and len(counterlist)%2==0:
				if line[0:2] != "//" :
					if line.find("=") > -1 :
						line = line.replace("=", " = ")
						self.readfile_out.write(line)
						continue
					if line.find("/*")>-1:
						continue
					if line.find("*/")>-1:
						continue
					if line.find("(") > -1 :
						line = line.replace("(", " ( ")
						self.readfile_out.write(line)
						continue
					if line.find(")") > -1 :
						line = line.replace(")", " ) ")
						self.readfile_out.write(line)
						continue
					if line.find(":") > -1:  #
						line = line.replace(";", " ; ")
						self.readfile_out.write(line)
						continue
					self.readfile_out.write(line)
		self.readfile_in.close()
		self.readfile_out.close()
		self.readfile_in = open(tempFilename_output, "r")
		self.readfile_out = open(filename_output, "w")
		content = self.readfile_in.read()
		content = content.split(';')
		array_Packager = []
		for i in range(0, len(content) - 1, 1):
			content[i] = content[i].replace("*/", "")
			content[i] = content[i].replace("\n", " ")
			content[i] = content[i].replace(";", " ; ")
			content[i] = content[i].replace("(", " ( ")
			content[i] = content[i].replace(")", " ) ")
			content[i] = content[i].lstrip()
			if content[i][0:2] == "/*":
				continue
			if content[i][0:7] == "package":
				array_Packager.append(1)
				array3 = []
				array31 = []
				for w in range(0, len(content[i]), 1):
					find0 = content[i][w].find('"')
					find1 = content[i][w].find(" ")
					if find0 > -1:
						array3.append(w)
					if find1 > -1:
						array31.append(w)
				if len(array3) > 1:
					X = array3[1]
					Package = "\n" + content[i][0:X + 1] + "\n" + content[i][X + 1:]
					Package = Package.lstrip()
					self.readfile_out.write("\n" + Package)
				else:
					Y = array31[1]
					Package = "\n" + "\n" + content[i][0:Y + 1] + "\n" + content[i][Y + 1:] + "\n"
					Package = Package.lstrip()
					self.readfile_out.write("\n" + Package)
				continue
			if content[i][0:9] == "protected":
				protected = "\n" + content[i][0:9] + "\n" + content[i][10:] + "\n"
				self.readfile_out.write(protected)
				continue
			if content[i][0:16]=="initial equation":
				initialequation ="\n"+content[i][0:16] +"\n"+ content[i][17:]+"\n"
				self.readfile_out.write(initialequation)
				continue
			if content[i][0:8] == "equation":
				equation = "\n" + content[i][0:8] + "\n" + content[i][9:] + "\n"
				self.readfile_out.write(equation)
				continue
			if content[i][0:6] == "record":
				array1 = []
				for y in range(0, len(content[i])):
					find0 = content[i][y].find('"')
					if find0 > -1:
						array1.append(y)
				if len(array1)>1:
					stringModel = array1[1]
					record = "\n" + content[i][0:stringModel+1] + "\n" + content[i][stringModel+2:] + "\n"
				else:
					T = (content[i].split())
					TList=""
					for w in T[2:]:
						TList=TList+" "+w
					TList=(TList.lstrip())
					record = "\n" + T[0] +" "+T[1]+" "+ "\n" + TList
				self.readfile_out.write(record)
				continue
			if content[i][0:8] == "function":
				array1 = []
				for y in range(0, len(content[i])):
					find0 = content[i][y].find('"')
					if find0 > -1:
						array1.append(y)
				if len(array1)>1:
					stringModel = array1[1]
					functions = "\n" + content[i][0:stringModel+1] + "\n" + content[i][stringModel+2:] + "\n"
				else:
					T = (content[i].split())
					TList=""
					for w in T[2:]:
						TList=TList+" "+w
					TList=(TList.lstrip())
					functions = "\n" + T[0] +" "+T[1]+" "+ "\n" + TList
				self.readfile_out.write(functions)
				continue
			if content[i][0:5] == "block":
				array1 = []
				for y in range(0, len(content[i])):
					find0 = content[i][y].find('"')
					if find0 > -1:
						array1.append(y)
				if len(array1)>1:
					stringModel = array1[1]
					block = "\n" + content[i][0:stringModel+1] + "\n" + content[i][stringModel+2:] + "\n"
				else:
					T = (content[i].split())
					TList=""
					for w in T[2:]:
						TList=TList+" "+w
					TList=(TList.lstrip())
					block = "\n" + T[0] +" "+T[1]+" "+ "\n" + TList
				self.readfile_out.write(block)
				continue
			if content[i][0:7] == "partial":
				array0 = []
				for w in range(0, len(content[i])):
					find0 = content[i][w].find('"')
					if find0 > -1:
						array0.append(w)
				if len(array0)>1:
					stringPartial = array0[1]
					partial = "\n" + content[i][0:stringPartial + 1] + "\n" + content[i][stringPartial + 2:]
					self.readfile_out.write(partial)
					continue
				else:
					T = (content[i].split())
					TList=""
					for w in T[3:]:
						TList=TList+" "+w
					TList=(TList.lstrip())
					partial = "\n" + T[0]+" "+T[1]+" "+T[2]+" "+"\n"+TList
					self.readfile_out.write(partial)
					continue
			if content[i][0:5] == "model" :
				array1 = []
				for y in range(0, len(content[i])):
					find0 = content[i][y].find('"')
					if find0 > -1:
						array1.append(y)
				if len(array1)>1:
					stringModel = array1[1]
					model = "\n" + content[i][0:stringModel + 1] + "\n" + content[i][stringModel + 2:].lstrip()
				else:
					T = (content[i].split())
					TList=""
					for w in T[2:]:
						TList=TList+" "+w
					model = "\n" + T[0] +" "+T[1]+" "+ "\n" + TList.lstrip()
				self.readfile_out.write(model)
				continue
			if content[i][0:9] == "connector":
				array2 = []
				for z in range(0, len(content[i])):
					find0 = content[i][z].find('"')
					if find0 > -1:
						array2.append(z)
				stringConnector = array2[1]
				connector = "\n" + content[i][0:stringConnector + 1] + "\n" + content[i][stringConnector + 2:]
				self.readfile_out.write(connector)
				continue
			self.readfile_out.write("\n" + content[i] + ";")
		self.readfile_in.close()
		self.readfile_out.close()

		###################################################################################################
		#If the file is a packages, the package searches for the searched model and stores it in a new file
		if len(array_Packager) > 0:
			readfile_in = open(filename_output, "r")
			T = filename_output.split("\\")
			T = T[len(T) - 1]
			T   = T[:-5]
			self.RelationModel = self.RelationModel.split()
			self.RelationModel = self.RelationModel[len(self.RelationModel)-1]
			self.RelationModel = self.RelationModel.replace(";","")
			file = outputpath_temp + "\\" + T + "." + self.RelationModel + ".java"

			file= file.replace(".mo","")
			readfile_out = open(file, "w")
			array = []
			arraytype = []
			count = 0
			for line in readfile_in.readlines():
				count = count + 1
				x = line.split()
				x_array = np.asarray(x)
				if len(x_array) > 0:
					if len(x_array) > 1:
						if x_array[0] == "end" and x_array[1] == self.RelationModel + ";":

							array.append(count)
							pass
						if x_array[1] == self.RelationModel and x_array[0] !="input" and x_array[0] != "output":

							array.append(count)
							pass
						if x_array[1] == self.RelationModel and x_array[0]=="type":

							arraytype.append(count)
							continue
							pass
						"""if  len(arraytype)==1 and x_array[0]=="annotation":
							print(line)
							arraytype.append(count)
							pass"""
						pass
					if len(x_array) > 2:
						if x_array[0] == "partial" and x_array[2] == self.RelationModel:
							array.append(count)
							pass
						pass
					pass
				continue
			pass

			if len(arraytype) > 1:
				for i in range(arraytype[0], arraytype[1] , 1):
					line = linecache.getline(filename_output, i)
					readfile_out.write(line)
					continue
			elif len(arraytype) > 0:
				for i in range(arraytype[0], arraytype[0]+1 , 1):
					line = linecache.getline(filename_output, i)
					readfile_out.write(line)
					continue

			if len(array) > 1:
				for i in range(array[0], array[1] + 1, 1):
					line = linecache.getline(filename_output, i)
					readfile_out.write(line)
					continue
			elif len(array) > 0 :
				for i in range(array[0], array[0] + 1, 1):
					line = linecache.getline(filename_output, i)
					readfile_out.write(line)
					continue

			filename_output = file
		return filename_output

## check paths and files for existence
class DataCheck(object):
	def __init__(self,AixLib_path):
		self.AixLib_path = AixLib_path
		print("test")

	# Set folder directory in AixLib
	def set_Folder_directory(self, AixLib_path):
		destination = AixLib_path
		suffixes = []
		for folder in os.listdir(AixLib_path):
			yield folder

	######################################################################################################
	#Search for the text file
	def set_relationmodel_path (self, AixLib_path, RelationModel, OriginalLibrary):
		OriginalLibrary = OriginalLibrary.split(".")
		OriginalLibrary = OriginalLibrary[0].split("\\")
		ModelicaLibraries = DataCheck.allModelicaLibrary(self,AixLib_path)
		destination = AixLib_path
		RelationModel.split("=")
		if RelationModel.find("=") > -1:
			RelationModel = RelationModel[RelationModel.find("=") + 1:]
			RelationModel = RelationModel.replace(" ", "")
		RelationModel = RelationModel.split(".")
		RelationModel2=RelationModel[len(RelationModel)-1]
		RelationModel3 = RelationModel[:]
		suffixes = []
		for i in range(0, len(ModelicaLibraries), 1):
			if ModelicaLibraries[i] == RelationModel[0]:
				for x in range(1,len(RelationModel)-1):
					del RelationModel[len(RelationModel)-1]
					RelationModelStr=""
					for z in RelationModel:
						RelationModelStr = RelationModelStr+"\\"+z
						destination2= destination + "\\"
						destination2 = destination+RelationModelStr

					for root, dirs, files in os.walk(destination2):
						for w in range(len(RelationModel3)-1,0,-1):
							for file in Path(str(root)).iterdir():
								if file.suffix == '.mo' and file.stem == RelationModel3[w] :
									RelationModel_Path = root + '\\' + RelationModel3[w]+ file.suffix
									return RelationModel_Path
								else:
									continue
					"""for root, dirs, files in os.walk(destination2):
						for file in Path(str(root)).iterdir():
							if file.suffix == '.mo' and file.stem == "package" :
									RelationModel_Path = root + '\\' + file.stem+ file.suffix
									return RelationModel_Path   """
		OriginalLibraryList = ""
		for w in OriginalLibrary[:len(OriginalLibrary)]:
			OriginalLibraryList = OriginalLibraryList + "\\" + w
		destination = (destination + OriginalLibraryList)
		for i in range(0, len(ModelicaLibraries), 1):
			if ModelicaLibraries[i] != RelationModel[0]:
				for root, dirs, files in os.walk(destination):
					for file in Path(str(root)).iterdir():
						for i in range(0, len(RelationModel), 1):
							if file.suffix == '.mo' and file.stem == RelationModel[len(RelationModel) - 1 - i] :
								RelationModel_Path = root + '\\' + RelationModel[len(RelationModel) - 1 - i] + file.suffix
								return RelationModel_Path
							elif file.suffix == '.mo' and file.stem == OriginalLibrary[2]:
								RelationModel_Path = root + '\\' + OriginalLibrary[2] + file.suffix
								return RelationModel_Path
							else:
								continue
		destination = AixLib_path
		OriginalLibraryList = ""
		for w in OriginalLibrary[:len(OriginalLibrary)-1]:
			OriginalLibraryList = OriginalLibraryList + "\\" + w
		destination = (destination + OriginalLibraryList)
		for i in range(0, len(ModelicaLibraries), 1):
			if ModelicaLibraries[i] != RelationModel[0]:
				for root, dirs, files in os.walk(destination):
					for file in Path(str(root)).iterdir():
						for i in range(0, len(RelationModel), 1):
							if file.suffix == '.mo' and file.stem == RelationModel[len(RelationModel) - 1 - i] :
								RelationModel_Path = root + '\\' + RelationModel[len(RelationModel) - 1 - i] + file.suffix
								return RelationModel_Path
							elif file.suffix == '.mo' and file.stem == OriginalLibrary[2]:
								RelationModel_Path = root + '\\' + OriginalLibrary[2] + file.suffix
								return RelationModel_Path
							else:
								break
		destination = AixLib_path

	# set outputfile from inputfile
	def setUMLModell(self, filename_input, output_path):
		readfile_in = open(filename_input, "r")
		filename_output = filename_input.split("\\")
		filename_output = filename_output[len(filename_output) - 1]
		filename_output = output_path + "\\" + filename_output
		return filename_output

	### Set the temporary file, where the formated text is located
	def set_temp_directory(self, output_path):
		outputpath_temp = output_path + "\\" + "TempData"
		if not os.path.exists(outputpath_temp):
			os.makedirs(outputpath_temp)
		return outputpath_temp

	# Gives all libraries in the Library folder
	def allModelicaLibrary(self,Aixlib_path):
		#Aixlib_path = self.Aixlib_path
		self.Library = Aixlib_path
		self.ListLibrary = os.listdir(self.Library)
		return self.ListLibrary

	# Set temp outputfiles for formated text
	def set_tempFilename_output(self, output_path, filename_input, Aixlib_path, RelationModel):
		Library = DataCheck.allModelicaLibrary(self,Aixlib_path)
		filename_output = filename_input.split(".")
		filename_output = filename_output[0].split("\\")
		for i in range(0, len(Library), 1):
			for w in range(0, len(filename_output) - 1, 1):
				if filename_output[w] == Library[i]:
					filename_output = filename_output[w:]
					break
		StringList = ""
		count = 0
		for x in filename_output:
			count = count + 1
			if count == len(filename_output):
				StringList = StringList + str(x)
				break
			StringList = StringList + str(x) + "."
		T = StringList.split(".")
		T = T[len(T) - 1]
		tempFilename_output = output_path + "\\" + "tempdata\\" + StringList + ".txt"
		return tempFilename_output

	def set_filename_output(self, filename_input, output_path, Aixlib_path, RelationModel):
		Library = DataCheck.allModelicaLibrary(self,Aixlib_path)
		filename_output = filename_input.split(".")
		filename_output = filename_output[0].split("\\")
		for i in range(0, len(Library), 1):
			for w in range(0, len(filename_output) - 1, 1):
				if filename_output[w] == Library[i]:
					filename_output = filename_output[w:]
					break
		StringList = ""
		count = 0
		for x in filename_output:
			count = count + 1
			if count == len(filename_output):
				StringList = StringList + str(x)
				break
			StringList = StringList + str(x) + "."
		T = StringList.split(".")
		T = T[len(T) - 1]
		filename_output = output_path + "\\" + "tempdata\\" + StringList + ".java"
		return filename_output

	# Check whether the file exist
	def filename_exist(self, filename_input):
		try:
			with open(filename_input) as file:
				print("File exist!")
				file.close()
				pass
		except IOError:
			print ("Unable to open file \nFile don`t exist!")

	# Check whether the aixlib path exist
	def AixLibPath_exist(self, AixLib_path):
		file_path = AixLib_path
		directory = os.path.dirname(file_path)
		try:
			os.stat(directory)
			print("AixLib directory exist!")
		except:
			print("Aixlib directory doesn`t exist!")

	# Append all translated models and used model in PlantUML Syntax in a single file
	def Appender(self, output_path, finalData):
		destination = output_path
		suffixes = []
		for path in Path(destination).iterdir():
			if path.is_file():
				suffixes.append(output_path + "\\" + path.stem + path.suffix)
		filename_input = open(finalData, "w")
		for i in range(0, len(suffixes)):
			input = open(suffixes[i], "r")
			for line in input:
				line = (line.replace("@startuml", ""))
				line = (line.replace("@enduml", ""))
				filename_input.write(line)
			input.close()
		filename_input.close()

	# List models in a library package
	def get_Modelsinpackages(self, filename_input , output_path):
		readfile_in = open(filename_input, "r")
		RelationModel = []
		for line in readfile_in.readlines():
			x = line.split()
			x_array = np.asarray(x)
			if len(x_array) > 1 :
				if x_array[0] == "end" and x_array[1] != "if;":
					x_array[1]=x_array[1].replace(";","")
					RelationModel.append(filename_input[0:-5]+"."+x_array[1]+".java")
					continue
					pass
		readfile_in.close()
		return RelationModel
	# Translate models in library package
	def set_ModelsinPackages(self,filename_input , output_path):
		readfile_in = open(filename_input, "r")
		counter = 0
		array = []
		array2 = []
		Model = DataCheck.get_Modelsinpackages(self,filename_input, output_path)
		ModelList = []
		filename_outputList = []
		for w in Model:
			ModelList.append(w)
		if len(ModelList) >0:
			for line in readfile_in.readlines():
				x = line.split()
				x_array = np.asarray(x)
				counter = counter + 1
				if len(x_array) > 1 :
					for ModelPackage in ModelList:
						T = ModelPackage.split(".")
						if len(x_array)>2:
							if x_array[1] == T[-2] and x_array[0]!="package" and len(array)==0 :
								array.append(counter)
								Name = x_array[1]
								continue
							if x_array[2] == T[-2] and x_array[0]!="package" and len(array2)==0 :
								array2.append(counter)
								Name2 = x_array[2]
								continue
						if x_array[0] == "end" and x_array[1]==T[-2]+";":
							if len(array)>0 and Name== x_array[1].replace(";","") :
								array.append(counter)
								filename_output=ModelPackage
								readfile_out = open(filename_output, "w")
								filename_outputList.append(filename_output)
								for w in range(array[0],array[1]+1,1):
									line = linecache.getline(filename_input, w)
									readfile_out.write(line)
								array = []
								continue
							if len(array2)>0 and  Name2 == x_array[1].replace(";","") :

								array2.append(counter)
								filename_output=ModelPackage
								readfile_out = open(filename_output, "w")
								filename_outputList.append(filename_output)
								for w in range(array2[0],array2[1]+1,1):
									line = linecache.getline(filename_input, w)
									readfile_out.write(line)
								array2 = []
								continue
						if x_array[0] == "type" and x_array[1]==T[-2] :
							array.append(counter)
						if x_array[0] == "type" and len(array)==1 :
							array.append(counter)
							if len(array)>1:
								for w in range(array[0],array[1]+1,1):
									line = linecache.getline(filename_input, w)
									readfile_out.write(line)
									array = []
			return filename_outputList

	# List Packages in a library package
	def getPackagesinPackages(self,filename_input , output_path):
		readfile_in = open(filename_input, "r")
		counter = 0
		array = []
		array2 = []
		NameList = []
		filename_outputList = []
		for line in readfile_in.readlines():
			x = line.split()
			x_array = np.asarray(x)
			counter = counter + 1
			if len(x_array) > 1 :
				if x_array[0]=="package":
					NameList.append(filename_input[0:-5]+"."+x_array[1]+".java" )
					continue
		return NameList

	# Seperate a package in a package
	def getrowPackagesinPackages(self,filename_input , output_path):
		readfile_in = open(filename_input, "r")
		counter = 0
		array = []
		array2 = []
		NameList = []
		filename_outputList = []
		for line in readfile_in.readlines():
			x = line.split()
			x_array = np.asarray(x)
			counter = counter + 1
			if len(x_array) > 1 :
				if x_array[0]=="package":
					NameList.append((counter))
					continue
		return NameList

	# Seperate a package in a package and write it as a single file
	def setPackagesinPackages(self,filename_input , output_path):
		readfile_in = open(filename_input, "r")
		NameList= DataCheck.getPackagesinPackages(filename_input , output_path)
		RowList = DataCheck.getrowPackagesinPackages(filename_input , output_path)
		array = []
		Path = filename_input.split(".")
		PackageList = []
		counter = 0
		if len(NameList)>0:
			for line in readfile_in.readlines():
				x = line.split()
				x_array =np.asarray(x)
				counter = counter +1
				if len(x_array)>0:
					for Packages in NameList:
						T =Packages.split(".")
						if len(x_array)>1:
							if x_array[1].replace(";","")==T[-2] and x_array[0] =="end":
								array.append(counter)
								filename_output = Packages
								readfile_out = open(filename_output,"w")
								PackageList.append(filename_output)
								for w in range(RowList[0],array[0],1):
									line = linecache.getline(filename_input,w)
									readfile_out.write(line)

	## Give the path of the model in a lower hierarchical level
	def showModel(self,ChildClass,RelationModel):
		destination = ChildClass
		for root, dirs, files in os.walk(destination):
			for file in Path(str(root)).iterdir():
				if file.suffix == '.mo' and file.stem==RelationModel:
					RelationModel_Path = root + '\\' + file.stem+ file.suffix

					return RelationModel_Path
					continue

if __name__ == "__main__":
	#DataCheck.set_ModelsinPackages(filename_input, output_path)
	from General import DataCheck
	DataCheck.showModel(ChildClass)

max_stars_repo_name: ErnestoVivas/X-HD

max_issues_repo_name: ErnestoVivas/X-HD

max_forks_repo_name: ErnestoVivas/X-HD


text:
from sklearn.preprocessing import LabelEncoder
from tensorflow.python.keras.utils.np_utils import to_categorical
from audio_pipeline import pipeline_config
import numpy as np
import pickle
import os

"""
Build and save an encoder that maps labels onto numerical values
* Numerical labels = [0,1,2]
* One hot encoding labels = [1,0,0],[0,1,0],[0,0,1]
"""

class ModelLabelEncoder:
    def __init__(self, labels):
        self._le = LabelEncoder()
        label_array = np.array(labels).ravel()
        self.encoded_labels = to_categorical(self._le.fit_transform(label_array))

    def inverse_transform(self, data):
        return self._le.inverse_transform(data)

    @staticmethod
    def load():
        cwd = os.path.join(os.path.dirname(__file__), pipeline_config.sound_label_file)
        with open(cwd, 'rb') as fin:
            return pickle.load(fin)

max_stars_repo_name: AlexWimpory/video-caption

max_issues_repo_name: AlexWimpory/video-caption

max_forks_repo_name: AlexWimpory/video-caption


text:
import pandas as pd
import numpy as np

import skimage.io

from typing import List

import matplotlib.pyplot as plt

def display_instance(row: pd.Series,
                     ax,
                     title_column: str = 'category_name',
                     fontsize: int = 14) -> None:
    ax.imshow(skimage.io.imread(row['image_path']), aspect='auto')
    ax.set_title(row[title_column], fontsize=fontsize)
    ax.axis('off')

def display_instances(df: pd.DataFrame,
                      display_fn=display_instance,
                      n_cols=7,
                      **display_args):
    n = len(df)
    n_rows = int(np.ceil(n/n_cols))
    # Estimate figsize
    figsize = (3*n_cols, 3.75*n_rows)
    fig, axes = plt.subplots(n_rows, n_cols, figsize=figsize)
    for i in range(n):
        row = df.iloc[i]

        if n_rows == 1:
            ax = axes[i]
        else:
            i_idx, j_idx = np.unravel_index(i, (n_rows, n_cols))
            ax = axes[i_idx][j_idx]

        display_fn(row, ax, **display_args)

    fig.tight_layout()

max_stars_repo_name: lnerenbergdev/segmentation

max_issues_repo_name: lnerenbergdev/segmentation

max_forks_repo_name: lnerenbergdev/segmentation


text:
import random as rn
import turtle
import numpy as np
import sys
import random
class Maze(turtle.Turtle):
      width=0
      height=0
      maze=""
      positions={}
      move_speed=0
      def __init__(self,speed,width,height,pen_color,background_color,pen_size):
             turtle.Turtle.__init__(self)
             window=turtle.Screen()
             self.move_speed=speed
             window.title("A Maze")
             window.setup(width,height)
             window=turtle.Screen()
             window.bgcolor(background_color)
             self.speed(speed)
             self.width=width
             self.height=height
             self.pencolor(pen_color)
             self.pensize(pen_size)

      def random_positions(self):

             self.maze=[['#' for i in range(int(self.width/15))] for j in range(int(self.height/15))]
             h=len(self.maze)
             w=len(self.maze[0])
             stack=[]
             tentative_stack=[]
             pos=(0,0)
             stack.append(pos)
             visited=[]

             while len(stack)!=0 :
                       (i,j) = pos
                       self.maze[i][j]='O'
                       if (j+1)in range(len(self.maze[0])) and (i,j+1) not in visited:
                          tentative_stack.append((i,j+1))

                       if (j-1) in range(len(self.maze[0])) and (i,j-1) not in visited:
                          tentative_stack.append((i,j-1))

                       if (i-1) in range(len(self.maze)) and (i-1,j) not in visited:
                          tentative_stack.append((i-1,j))

                       if (i+1) in range(len(self.maze)) and (i+1,j) not in visited:
                          tentative_stack.append((i+1,j))
                       if(len(tentative_stack)==0):
                          pos=stack.pop()
                       else:
                          new_pos=random.choice(tentative_stack)

                          if pos not in self.positions:
                               self.positions[pos]=[]
                               self.positions[pos].append(new_pos)
                          elif new_pos not in self.positions[pos]:
                               self.positions[pos].append(new_pos)
                          if new_pos not in self.positions:
                               self.positions[new_pos]=[]
                               self.positions[new_pos].append(pos)
                          elif pos not in self.positions[new_pos]:
                               self.positions[new_pos].append(pos)

                          pos=new_pos
                          visited.append(pos)
                          stack.append(pos)

                          tentative_stack=[]
             del stack
             del tentative_stack
             del visited
             visited_wall=[]

             for (i,j) in self.positions.keys():
                           if  ((i,j),(i,j+1)) not in visited_wall and (i,j+1) not in self.positions[(i,j)]:
                              self.draw_wall((i,j),(i,j+1),1)
                              visited_wall.append(((i,j),(i,j+1)))
                           if ((i,j-1),(i,j)) not in visited_wall and (i,j-1) not in self.positions[(i,j)]:
                              self.draw_wall((i,j-1),(i,j),1)
                              visited_wall.append(((i,j-1),(i,j)))
                           if  ((i-1,j),(i,j)) not in visited_wall and (i-1,j) not in self.positions[(i,j)]:
                              self.draw_wall((i-1,j),(i,j),0)
                              visited_wall.append(((i-1,j),(i,j)))
                           if ((i,j),(i+1,j)) not in visited_wall and (i+1,j) not in self.positions[(i,j)]:
                              self.draw_wall((i,j),(i+1,j),0)
                              visited_wall.append(((i,j),(i+1,j)))

      def draw_wall(self,point_1,point_2,flg):
                  if flg==1:
                     x=-int(self.width/2)+15*point_2[1]
                     y=int(self.height/2)-15*point_1[0]
                     point1=(x,y)
                     x=-int(self.width/2)+15*point_2[1]
                     y=int(self.height/2)-15*point_2[0]-15
                     point2=(x,y)
                     self.penup()
                     self.goto(point1)
                     self.pendown()
                     self.goto(point2)
                     self.penup()
                  else:
                     x=-int(self.width/2)+15*point_1[1]
                     y=int(self.height/2)-15*point_2[0]
                     point1=(x,y)
                     x=-int(self.width/2)+15*point_2[1]+15
                     y=int(self.height/2)-15*point_2[0]
                     point2=(x,y)
                     self.penup()
                     self.goto(point1)
                     self.pendown()
                     self.goto(point2)
                     self.penup()

max_stars_repo_name: ehabosaleh/random-maze

max_issues_repo_name: ehabosaleh/random-maze

max_forks_repo_name: ehabosaleh/random-maze


text:
#Takes fasta file of sequences and makes histogram of GC contents

#Usage: python plotGC <sequences.fasta>

import sys
from Bio import SeqIO
from Bio.SeqUtils import GC
import matplotlib.pyplot as plt
import numpy as np

def plotGC(fasta):
    GCs = []
    for record in SeqIO.parse(fasta, 'fasta'):
        GCs.append(float(GC(record.seq[:]))) #slice

    print np.mean(GCs)
    print np.median(GCs)

    plt.hist(GCs, 25, range = [20,70], facecolor='green', alpha=0.75)

    plt.xlabel('GC percentage')
    plt.ylabel('Count')
    plt.title('ALE distal UTR %GC')

    plt.show()

plotGC(sys.argv[1])

max_stars_repo_name: TaliaferroLab/AnalysisScripts

max_issues_repo_name: TaliaferroLab/AnalysisScripts

max_forks_repo_name: TaliaferroLab/AnalysisScripts