<commit_before>#!/usr/local/bin/python
# --2 and 3--
__author__ = "gray"
__date__ = "20170920"
__version__ = "1.0.1"
__aim__ = """
_3_miRDeep2Work.py

(apply mirDeep2 pipeline)

#1: rename sampleMarker_QC_collapse.fasta [content fasta header rename]

#>1-43234 => seq_1_x43234

[cd to dir]
#2: mapper.pl <input> -c -r <20 bowtie -m> -i -j -o 10 -p <genome> -s <outFa>
    -t <outArf> -v 2> report_mapper_<SampleMarker>.log

[cd to dir]
#3: miRDeep2.pl <input: outFa> <genomeFasta> <input: outArf> <mature_fa>
                <mature_out_fa> <hairpin_fa> -t <species Name: Human>
                -P -u 2> report_miRDeep2_<SampleMarker>.log

"""
__Config__ = """
SAMPLESHEET\t{GZ\tSampleMarker}
QCWORK_DIR\t{Path}
mirDeep2_DIR\t{Path}
miRDeep2_Config\t{miRDeep2Config_fp}
"""

import sys
import os
import subprocess as sup
import multiprocessing as mul

def ConFigSplit(ConFig):
    reDict = {}
    with open(ConFig, "r") as Fr:
        content = Fr.readlines()
    for line in content:
        if line.strip() != "":
            item = line.strip().split("\t")
            reDict[item[0]] = item[1]
    return reDict

#---------------
def createEnv(SampleMarker, WorkDir="./miRDeep2Work/"): #in config file
    print(" ".join(["mkdir", "-p", WorkDir+"/"+SampleMarker+"/Data/"]))
    sup.call(["mkdir", "-p", WorkDir+"/"+SampleMarker+"/Data/"])

def renameFunction(SampleMarker, QCworkDir, WorkDir="./miRDeep2Work/"):
    oriFile = QCworkDir+"/"+SampleMarker+"_QC_collapse.fasta"
    outFile = WorkDir+"/"+SampleMarker+"/Data/"+SampleMarker+"_rename.fasta"
    #do it
    with open(oriFile, "r") as Fr:
        with open(outFile, "w") as Fw:
            lineCount = 0
            for line in Fr:
                lineCount += 1
                if lineCount%2 == 1:#header #>1-423232 => >seq_1_x423232
                    item = line.strip(">\n").split("-")
                    header = ">seq_"+item[0]+"_x"+item[1]+"\n"
                    Fw.write(header)
                else:
                    Fw.write(line)

def DoRename(parameter):
    SampleMarker = parameter["SampleMarker"]
    WorkDir = parameter["miRDeep2WorkDir"]
    QCDir = parameter["QC_Dir"]
    createEnv(SampleMarker, WorkDir)
    print("RenameWork:"+SampleMarker)
    renameFunction(SampleMarker, QCDir, WorkDir)

#---------------------------------
#mirdeep2
def mapper(ori_data, marker, miRDeep2config):
    #Config:
    miRDeep2Dict = ConFigSplit(miRDeep2config)
    #
    comd = ["mapper.pl", ori_data, "-c",  #fasta
     "-r", "20", #bowtie -m (multiple map
     "-i", "-j", #-i convert to dna, -j remove other letters
     "-o", "10", #bowtie threads (10 *N) < 60
     "-p", miRDeep2Dict["genome"], #bowtie genome
     "-s", marker+"_reads.fa",            #out1 outFa
     "-t", marker+"_reads_vs_mapper.arf", #out2 outArf
     "-v"]#, "2>", "report_mapper_"+marker+".log"]
    p = sup.Popen(comd, stderr=sup.PIPE)
    p.wait()
    p_stderr = p.communicate()[1]
    with open("report_mapper_"+marker+".log", "wb") as Fw:
        Fw.write(p_stderr)

def miRDeep2(marker, miRDeep2config):
    #Config:
    miRDeep2Dict = ConFigSplit(miRDeep2config)
    #
    comd =["miRDeep2.pl", marker+"_reads.fa", #outFa
     miRDeep2Dict["genomeFa"],
     marker+"_reads_vs_mapper.arf",     #outArf
     miRDeep2Dict["mature_fa"],         #miRNA mature
     miRDeep2Dict["mature_out_fa"],     #out spec miRNA
     miRDeep2Dict["hairpin_fa"],        #hairpin
     "-t", miRDeep2Dict["miRDeep2Spec"],#via quantifier.pl
     "-P", "-u"]#, "2>", "report_miRDeep2_"+marker+".log" ]
    p = sup.Popen(comd, stderr=sup.PIPE)
    p.wait()
    p_stderr = p.communicate()[1]
    with open("report_miRDeep2_"+marker+".log", "wb") as Fw:
        Fw.write(p_stderr)

def miRDeep2env(SampleSheet ,miRDeep2Dir, miRDeep2Config):
    paraList = []
    with open(SampleSheet, "r") as Fsheet:
        content = Fsheet.readlines()
    for line in content: #No header
        Marker = line.strip("\n").split("\t")[1]
        #outFa = Marker+"_read.fa"
        #outArf = Marker+"_reads_vs_mapper.arf"

        #need fork?
        para_mapper = (Marker, miRDeep2Dir, miRDeep2Config)#, outFa, outArf)
        paraList.append(para_mapper)
    return paraList

#Multiple use
def DomiRDeep2(parameter):
    marker = parameter[0]
    miRDeep2Dir = parameter[1]
    miRDeep2config = parameter[2]
    #outFa = parameter[3]
    #outArf = parameter[4]
    #chdir
    workDir = miRDeep2Dir+"/"+marker
    ori_data = workDir+"/Data/"+marker+"_rename.fasta"
    os.chdir(workDir)
    print(os.getcwd())

    mapper(ori_data, marker, miRDeep2config)
    miRDeep2(marker, miRDeep2config)

if __name__ == "__main__":
    if os.path.exists(sys.argv[1]):
        pass
    else:
        print(___Config__)
        sys.exit(1)

    ConfigDict = ConFigSplit(sys.argv[1])
    SampleSheet = ConfigDict["SAMPLESHEET"]
    QCdir = ConfigDict["QCWORK_DIR"]
    miRDeep2Dir = ConfigDict["miRDeep2_DIR"]
    #-rename
    renameDictList = []
    with open(SampleSheet, "r") as Fsheet:
        content = Fsheet.readlines()
    for line in content: #no header
        item = line.strip("\n").split("\t")
        Marker = item[1]
        renameDictList.append( {"SampleMarker":Marker, "QC_Dir":QCdir,
                                "miRDeep2WorkDir":miRDeep2Dir})
    if len(renameDictList) < 20:
        pool = mul.Pool(len(renameDictList))
        pool.map(DoRename, renameDictList)
    else:
        pool = mul.Pool(20)
        pool.map(DoRename, renameDictList)

    #-miRDeep2
    miRDeep2Config = ConfigDict["miRDeep2_Config"]
    miRDeep2Paralist = miRDeep2env(SampleSheet ,miRDeep2Dir, miRDeep2Config)

    pool.map(DomiRDeep2, miRDeep2Paralist)

<commit_msg>Update _3_miRDeep2Work.py<commit_after>#!/usr/local/bin/python
# --2 and 3--
__author__ = "gray"
__date__ = "20170920"
__version__ = "1.0.2"
__aim__ = """
_3_miRDeep2Work.py

(apply mirDeep2 pipeline)

#1: rename sampleMarker_QC_collapse.fasta [content fasta header rename]

#>1-43234 => seq_1_x43234

[cd to dir]
#2: mapper.pl <input> -c -r <20 bowtie -m> -i -j -o 10 -p <genome> -s <outFa>
    -t <outArf> -v 2> report_mapper_<SampleMarker>.log

[cd to dir]
#3: miRDeep2.pl <input: outFa> <genomeFasta> <input: outArf> <mature_fa>
                <mature_out_fa> <hairpin_fa> -t <species Name: Human>
                -P -u 2> report_miRDeep2_<SampleMarker>.log

"""
__Config__ = """
SAMPLESHEET\t{GZ\tSampleMarker}
QCWORK_DIR\t{Path}
mirDeep2_DIR\t{Path}
miRDeep2_Config\t{miRDeep2Config_fp}
"""

import sys
import os
import subprocess as sup
import multiprocessing as mul

def ConFigSplit(ConFig):
    reDict = {}
    with open(ConFig, "r") as Fr:
        content = Fr.readlines()
    for line in content:
        if line.strip() != "":
            item = line.strip().split("\t")
            reDict[item[0]] = item[1]
    return reDict

#---------------
def createEnv(SampleMarker, WorkDir="./miRDeep2Work/"): #in config file
    print(" ".join(["mkdir", "-p", WorkDir+"/"+SampleMarker+"/Data/"]))
    sup.call(["mkdir", "-p", WorkDir+"/"+SampleMarker+"/Data/"])

def renameFunction(SampleMarker, QCworkDir, WorkDir="./miRDeep2Work/"):
    oriFile = QCworkDir+"/"+SampleMarker+"_QC_collapse.fasta"
    outFile = WorkDir+"/"+SampleMarker+"/Data/"+SampleMarker+"_rename.fasta"
    #do it
    with open(oriFile, "r") as Fr:
        with open(outFile, "w") as Fw:
            lineCount = 0
            for line in Fr:
                lineCount += 1
                if lineCount%2 == 1:#header #>1-423232 => >seq_1_x423232
                    item = line.strip(">\n").split("-")
                    header = ">seq_"+item[0]+"_x"+item[1]+"\n"
                    Fw.write(header)
                else:
                    Fw.write(line)

def DoRename(parameter):
    SampleMarker = parameter["SampleMarker"]
    WorkDir = parameter["miRDeep2WorkDir"]
    QCDir = parameter["QC_Dir"]
    createEnv(SampleMarker, WorkDir)
    print("RenameWork:"+SampleMarker)
    renameFunction(SampleMarker, QCDir, WorkDir)

#---------------------------------
#mirdeep2
def mapper(ori_data, marker, miRDeep2config):
    #Config:
    miRDeep2Dict = ConFigSplit(miRDeep2config)
    #
    comd = ["mapper.pl", ori_data, "-c",  #fasta
     "-r", "20", #bowtie -m (multiple map
     "-i", "-j", #-i convert to dna, -j remove other letters
     "-o", "10", #bowtie threads (10 *N) < 60
     "-p", miRDeep2Dict["genome"], #bowtie genome
     "-s", marker+"_reads.fa",            #out1 outFa
     "-t", marker+"_reads_vs_mapper.arf", #out2 outArf
     "-v"]#, "2>", "report_mapper_"+marker+".log"]
    p = sup.Popen(comd, stderr=sup.PIPE)
    #p.wait()
    p_stderr = p.communicate()[1]
    with open("report_mapper_"+marker+".log", "wb") as Fw:
        Fw.write(p_stderr)

def miRDeep2(marker, miRDeep2config):
    #Config:
    miRDeep2Dict = ConFigSplit(miRDeep2config)
    #
    comd =["miRDeep2.pl", marker+"_reads.fa", #outFa
     miRDeep2Dict["genomeFa"],
     marker+"_reads_vs_mapper.arf",     #outArf
     miRDeep2Dict["mature_fa"],         #miRNA mature
     miRDeep2Dict["mature_out_fa"],     #out spec miRNA
     miRDeep2Dict["hairpin_fa"],        #hairpin
     "-t", miRDeep2Dict["miRDeep2Spec"],#via quantifier.pl
     "-P", "-u"]#, "2>", "report_miRDeep2_"+marker+".log" ]
    p = sup.Popen(comd, stderr=sup.PIPE)
    #p.wait()
    p_stderr = p.communicate()[1]
    with open("report_miRDeep2_"+marker+".log", "wb") as Fw:
        Fw.write(p_stderr)

def miRDeep2env(SampleSheet ,miRDeep2Dir, miRDeep2Config):
    paraList = []
    with open(SampleSheet, "r") as Fsheet:
        content = Fsheet.readlines()
    for line in content: #No header
        Marker = line.strip("\n").split("\t")[1]
        #outFa = Marker+"_read.fa"
        #outArf = Marker+"_reads_vs_mapper.arf"

        #need fork?
        para_mapper = (Marker, miRDeep2Dir, miRDeep2Config)#, outFa, outArf)
        paraList.append(para_mapper)
    return paraList

#Multiple use
def DomiRDeep2(parameter):
    marker = parameter[0]
    miRDeep2Dir = parameter[1]
    miRDeep2config = parameter[2]
    #outFa = parameter[3]
    #outArf = parameter[4]
    #chdir
    workDir = miRDeep2Dir+"/"+marker
    ori_data = workDir+"/Data/"+marker+"_rename.fasta"
    os.chdir(workDir)
    print(os.getcwd())

    mapper(ori_data, marker, miRDeep2config)
    miRDeep2(marker, miRDeep2config)

if __name__ == "__main__":
    if os.path.exists(sys.argv[1]):
        pass
    else:
        print(___Config__)
        sys.exit(1)

    ConfigDict = ConFigSplit(sys.argv[1])
    SampleSheet = ConfigDict["SAMPLESHEET"]
    QCdir = ConfigDict["QCWORK_DIR"]
    miRDeep2Dir = ConfigDict["miRDeep2_DIR"]
    #-rename
    renameDictList = []
    with open(SampleSheet, "r") as Fsheet:
        content = Fsheet.readlines()
    for line in content: #no header
        item = line.strip("\n").split("\t")
        Marker = item[1]
        renameDictList.append( {"SampleMarker":Marker, "QC_Dir":QCdir,
                                "miRDeep2WorkDir":miRDeep2Dir})
    if len(renameDictList) < 20:
        pool = mul.Pool(len(renameDictList))
        pool.map(DoRename, renameDictList)
    else:
        pool = mul.Pool(20)
        pool.map(DoRename, renameDictList)

    #-miRDeep2
    miRDeep2Config = ConfigDict["miRDeep2_Config"]
    miRDeep2Paralist = miRDeep2env(SampleSheet ,miRDeep2Dir, miRDeep2Config)

    pool.map(DomiRDeep2, miRDeep2Paralist)

<|endoftext|>


<commit_before># Missing Numbers
# Developer: Murillo Grubler
# https://www.hackerrank.com/challenges/missing-numbers/problem
# Time complexity: O(n)

import math
import os
import random
import re
import sys

# Complete the missingNumbers function below.
def missingNumbers(arr, brr):
    m = max(arr + brr) + 1
    list = [0 for _ in range(m)]
    for i in arr:
        list[i] += 1
    for i in brr:
        list[i] -= 1
    return sorted([item for item in range(len(list)) if list[item] != 0])

if __name__ == '__main__':
    # fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    m = int(input())
    brr = list(map(int, input().rstrip().split()))
    result = missingNumbers(arr, brr)
    print (' '.join(map(str, result)))
    # fptr.write(' '.join(map(str, result)))
    # fptr.write('\n')
    # fptr.close()
<commit_msg>update code<commit_after># Missing Numbers
# Developer: Murillo Grubler
# https://www.hackerrank.com/challenges/missing-numbers/problem
# Time complexity: O(n)

import math
import os
import random
import re
import sys

# Complete the missingNumbers function below.
def missingNumbers(arr, brr):
    m = max(arr + brr) + 1
    list = [0 for _ in range(m)]
    for i in arr:
        list[i] += 1
    for i in brr:
        list[i] -= 1
    return sorted([item for item in range(len(list)) if list[item] != 0])

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    m = int(input())
    brr = list(map(int, input().rstrip().split()))
    result = missingNumbers(arr, brr)
    #print (' '.join(map(str, result)))
    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')
    fptr.close()
<|endoftext|>


<commit_before>import threading
from server_globals import connections

from upload_handler import UploadHandler
from login_handler import LoginHandler
from register_handler import RegisterHandler
from retrieve_handler import  RetrieveHandler
from search_handler import SearchHandler
from delete_handler import DeleteHandler

class RequestHandler(threading.Thread):
    """ A request handler class which runs in it's own thread """
    def __init__(self, connection):
        threading.Thread.__init__(self)
        self.connection = connection

    def run(self):
        while True:
            raw_request = self.connection.recv(2048)

            if len(raw_request):
                client_option = raw_request.decode()
                if client_option == "login":
                    msg = self.connection.recv(1024)
                    print("Logging in with: " + msg.decode())
                    LoginHandler(msg)
                elif client_option == "register":
                    msg = self.connection.recv(1024)
                    print("Registering user: " + msg.decode())
                    RegisterHandler(msg)
                elif client_option == "upload":
                    msg = self.connection.recv(1024)
                    print ("Received: " + msg.decode() )
                    UploadHandler(self.connection, msg)
                elif client_option == "retrieve":
                    msg = self.connection.recv(1024)
                    print("Retrieving File: " + msg.decode())
                    RetrieveHandler(msg)
                elif client_option == "search":
                    msg = self.connection.recv(1024)
                    print("Searching for: " + msg.decode())
                    SearchHandler(msg)
                elif client_option == "delete":
                    msg = self.connection.recv(1024)
                    print("Deleting file: " + msg.decode())
                    DeleteHandler(msg)

            #request = self.parse_request(raw_request.decode())
            #self.process_request(request)
            else:
                print("Empty request body")
                self.connection.close()
                connections.remove(self.connection)
                return

    def process_request(self, request):
        if "query" not in request:
            raise RuntimeError("request is missing query field")
        else:
            print("searching for {} in Knowledge Base".format(request["query"]))

    def parse_request(self, request):
        """ Parse incoming request string and return each key, value pair as a native python dictionary. """
        return {key : val for (key, val) in (entry.split(":") for entry in request.split(";"))}<commit_msg>Add statements to determine the client's request. Creates a Lock. Uses time to put main thread to sleep.<commit_after>import threading
import time
from server_globals import connections

from upload_handler import UploadHandler
from retrieve_handler import  RetrieveHandler
from login_handler import LoginHandler
from register_handler import RegisterHandler
from search_handler import SearchHandler
from delete_handler import DeleteHandler

class RequestHandler(threading.Thread):
    """ A request handler class which runs in it's own thread """
    def __init__(self, connection):
        threading.Thread.__init__(self)
        self.connection = connection

    def run(self):
        lock = threading.Lock()
        while True:
            raw_request = self.connection.recv(2048)
            if len(raw_request):
                client_option = raw_request.decode()

                if client_option == "login":
                    msg = self.connection.recv(1024)
                    print("Logging in with: " + msg.decode())
                    LoginHandler(msg)

                elif client_option == "register":
                    msg = self.connection.recv(1024)
                    print("Registering user: " + msg.decode())
                    RegisterHandler(msg)

                elif client_option == "upload":
                    msg = self.connection.recv(1024)
                    print ("Received: " + msg.decode() )
                    UploadHandler(self.connection, msg, lock).start()
                    time.sleep(2)

                elif client_option == "retrieve":
                    msg = self.connection.recv(1024)
                    print("Retrieving File: " + msg.decode())
                    RetrieveHandler(msg)

                elif client_option == "search":
                    msg = self.connection.recv(1024)
                    print("Searching for: " + msg.decode())
                    SearchHandler(msg)

                elif client_option == "delete":
                    msg = self.connection.recv(1024)
                    print("Deleting file: " + msg.decode())
                    DeleteHandler(msg)

            #request = self.parse_request(raw_request.decode())
            #self.process_request(request)
            else:
                print("Empty request body")
                self.connection.close()
                connections.remove(self.connection)
                return

    def process_request(self, request):
        if "query" not in request:
            raise RuntimeError("request is missing query field")
        else:
            print("searching for {} in Knowledge Base".format(request["query"]))

    def parse_request(self, request):
        """ Parse incoming request string and return each key, value pair as a native python dictionary. """
        return {key : val for (key, val) in (entry.split(":") for entry in request.split(";"))}<|endoftext|>


<commit_before>import numpy as np
from scipy import sparse as sp
from matutils import mkvc, ndgrid, sub2ind, sdiag
from codeutils import asArray_N_x_Dim
from codeutils import isScalar

def exampleLrmGrid(nC, exType):
    assert type(nC) == list, "nC must be a list containing the number of nodes"
    assert len(nC) == 2 or len(nC) == 3, "nC must either two or three dimensions"
    exType = exType.lower()

    possibleTypes = ['rect', 'rotate']
    assert exType in possibleTypes, "Not a possible example type."

    if exType == 'rect':
        return list(ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False))
    elif exType == 'rotate':
        if len(nC) == 2:
            X, Y = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2)
            amt[amt < 0] = 0
            return [X + (-(Y - 0.5))*amt, Y + (+(X - 0.5))*amt]
        elif len(nC) == 3:
            X, Y, Z = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2 + (Z - 0.5)**2)
            amt[amt < 0] = 0
            return [X + (-(Y - 0.5))*amt, Y + (-(Z - 0.5))*amt, Z + (-(X - 0.5))*amt]

def meshTensor(value):
    """
        **meshTensor** takes a list of numbers and tuples that have the form::

            mT = [ float, (cellSize, numCell), (cellSize, numCell, factor) ]

        For example, a time domain mesh code needs
        many time steps at one time::

            [(1e-5, 30), (1e-4, 30), 1e-3]

        Means take 30 steps at 1e-5 and then 30 more at 1e-4,
        and then one step of 1e-3.

        Tensor meshes can also be created by increase factors::

            [(10.0, 5, -1.3), (10.0, 50), (10.0, 5, 1.3)]

        When there is a third number in the tuple, it
        refers to the increase factor, if this number
        is negative this section of the tensor is flipped right-to-left.

        .. plot::

            from SimPEG import Mesh
            tx = [(10.0,10,-1.3),(10.0,40),(10.0,10,1.3)]
            ty = [(10.0,10,-1.3),(10.0,40)]
            M = Mesh.TensorMesh([tx, ty])
            M.plotGrid(showIt=True)

    """
    if type(value) is not list:
        raise Exception('meshTensor must be a list of scalars and tuples.')

    proposed = []
    for v in value:
        if isScalar(v):
            proposed += [float(v)]
        elif type(v) is tuple and len(v) == 2:
            proposed += [float(v[0])]*int(v[1])
        elif type(v) is tuple and len(v) == 3:
            start = float(v[0])
            num = int(v[1])
            factor = float(v[2])
            pad = ((np.ones(num)*np.abs(factor))**(np.arange(num)+1))*start
            if factor < 0: pad = pad[::-1]
            proposed += pad.tolist()
        else:
            raise Exception('meshTensor must contain only scalars and len(2) or len(3) tuples.')

    return np.array(proposed)

def closestPoints(mesh, pts, gridLoc='CC'):
    """
        Move a list of points to the closest points on a grid.

        :param simpeg.Mesh.BaseMesh mesh: The mesh
        :param numpy.ndarray pts: Points to move
        :param string gridLoc: ['CC', 'N', 'Fx', 'Fy', 'Fz', 'Ex', 'Ex', 'Ey', 'Ez']
        :rtype: numpy.ndarray
        :return: nodeInds
    """

    pts = asArray_N_x_Dim(pts, mesh.dim)
    grid = getattr(mesh, 'grid' + gridLoc)
    nodeInds = np.empty(pts.shape[0], dtype=int)

    for i, pt in enumerate(pts):
        if mesh.dim == 1:
            nodeInds[i] = ((pt - grid)**2).argmin()
        else:
            nodeInds[i] = ((np.tile(pt, (grid.shape[0],1)) - grid)**2).sum(axis=1).argmin()

    return nodeInds

def readUBCTensorMesh(fileName):
    """
        Read UBC GIF 3DTensor mesh and generate 3D Tensor mesh in simpegTD

        Input:
        :param fileName, path to the UBC GIF mesh file

        Output:
        :param SimPEG TensorMesh object
        :return
    """

    # Interal function to read cell size lines for the UBC mesh files.
    def readCellLine(line):
        for seg in line.split():
            if '*' in seg:
                st = seg
                sp = seg.split('*')
                re = np.array(sp[0],dtype=int)*(' ' + sp[1])
                line = line.replace(st,re.strip())
        return np.array(line.split(),dtype=float)

    # Read the file as line strings, remove lines with comment = !
    msh = np.genfromtxt(fileName,delimiter='\n',dtype=np.str,comments='!')

    # Fist line is the size of the model
    sizeM = np.array(msh[0].split(),dtype=float)
    # Second line is the South-West-Top corner coordinates.
    x0 = np.array(msh[1].split(),dtype=float)
    # Read the cell sizes
    h1 = readCellLine(msh[2])
    h2 = readCellLine(msh[3])
    h3temp = readCellLine(msh[4])
    h3 = h3temp[::-1] # Invert the indexing of the vector to start from the bottom.
    # Adjust the reference point to the bottom south west corner
    x0[2] = x0[2] - np.sum(h3)
    # Make the mesh
    from SimPEG import Mesh
    tensMsh = Mesh.TensorMesh([h1,h2,h3],x0)
    return tensMsh

def readUBCTensorModel(fileName, mesh):
    """
        ReadUBC 3DTensor mesh model and generate 3D Tensor mesh model in simpegTD

    """
    f = open(fileName, 'r')
    model = np.array(map(float, f.readlines()))
    f.close()
    model = np.reshape(model, (mesh.nCz, mesh.nCx, mesh.nCy), order = 'F')
    model = model[::-1,:,:]
    model = np.transpose(model, (1, 2, 0))
    model = mkvc(model)

    return model

def writeUBCTensorMesh(mesh, fileName):
    """
        Writes a SimPEG TensorMesh to a UBC-GIF format mesh file.

        :param simpeg.Mesh.TensorMesh mesh: The mesh
        :param str fileName: File to write to
    """
    assert mesh.dim == 3
    s = ''
    s += '%i %i %i\n' %tuple(mesh.vnC)
    origin = mesh.x0 + np.array([0,0,mesh.hz.sum()]) # Have to it in the same operation or use mesh.x0.copy(), otherwise the mesh.x0 is updated.
    origin.dtype = float

    s += '%.2f %.2f %.2f\n' %tuple(origin)
    s += ('%.2f '*mesh.nCx+'\n')%tuple(mesh.hx)
    s += ('%.2f '*mesh.nCy+'\n')%tuple(mesh.hy)
    s += ('%.2f '*mesh.nCz+'\n')%tuple(mesh.hz[::-1])
    f = open(fileName, 'w')
    f.write(s)
    f.close()

def writeUBCTensorModel(mesh, model, fileName):
    """
        Writes a model associated with a SimPEG TensorMesh
        to a UBC-GIF format model file.

        :param simpeg.Mesh.TensorMesh mesh: The mesh
        :param numpy.ndarray model: The model
        :param str fileName: File to write to
    """

    # Reshape model to a matrix
    modelMat = mesh.r(model,'CC','CC','M')
    # Transpose the axes
    modelMatT = modelMat.transpose((2,0,1))
    # Flip z to positive down
    modelMatTR = mkvc(modelMatT[::-1,:,:])

    np.savetxt(fileName, modelMatTR.ravel())

if __name__ == '__main__':
    from SimPEG import Mesh
    import matplotlib.pyplot as plt
    tx = [(10.0,10,-1.3),(10.0,40),(10.0,10,1.3)]
    ty = [(10.0,10,-1.3),(10.0,40)]
    M = Mesh.TensorMesh([tx, ty])
    M.plotGrid()
    plt.gca().axis('tight')
    plt.show()
<commit_msg>Added a function to write VTK rectilinear file (vtr) for a Tensor mesh and model (optional) for view in an external viewer.<commit_after>import numpy as np
from scipy import sparse as sp
from matutils import mkvc, ndgrid, sub2ind, sdiag
from codeutils import asArray_N_x_Dim
from codeutils import isScalar

def exampleLrmGrid(nC, exType):
    assert type(nC) == list, "nC must be a list containing the number of nodes"
    assert len(nC) == 2 or len(nC) == 3, "nC must either two or three dimensions"
    exType = exType.lower()

    possibleTypes = ['rect', 'rotate']
    assert exType in possibleTypes, "Not a possible example type."

    if exType == 'rect':
        return list(ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False))
    elif exType == 'rotate':
        if len(nC) == 2:
            X, Y = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2)
            amt[amt < 0] = 0
            return [X + (-(Y - 0.5))*amt, Y + (+(X - 0.5))*amt]
        elif len(nC) == 3:
            X, Y, Z = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2 + (Z - 0.5)**2)
            amt[amt < 0] = 0
            return [X + (-(Y - 0.5))*amt, Y + (-(Z - 0.5))*amt, Z + (-(X - 0.5))*amt]

def meshTensor(value):
    """
        **meshTensor** takes a list of numbers and tuples that have the form::

            mT = [ float, (cellSize, numCell), (cellSize, numCell, factor) ]

        For example, a time domain mesh code needs
        many time steps at one time::

            [(1e-5, 30), (1e-4, 30), 1e-3]

        Means take 30 steps at 1e-5 and then 30 more at 1e-4,
        and then one step of 1e-3.

        Tensor meshes can also be created by increase factors::

            [(10.0, 5, -1.3), (10.0, 50), (10.0, 5, 1.3)]

        When there is a third number in the tuple, it
        refers to the increase factor, if this number
        is negative this section of the tensor is flipped right-to-left.

        .. plot::

            from SimPEG import Mesh
            tx = [(10.0,10,-1.3),(10.0,40),(10.0,10,1.3)]
            ty = [(10.0,10,-1.3),(10.0,40)]
            M = Mesh.TensorMesh([tx, ty])
            M.plotGrid(showIt=True)

    """
    if type(value) is not list:
        raise Exception('meshTensor must be a list of scalars and tuples.')

    proposed = []
    for v in value:
        if isScalar(v):
            proposed += [float(v)]
        elif type(v) is tuple and len(v) == 2:
            proposed += [float(v[0])]*int(v[1])
        elif type(v) is tuple and len(v) == 3:
            start = float(v[0])
            num = int(v[1])
            factor = float(v[2])
            pad = ((np.ones(num)*np.abs(factor))**(np.arange(num)+1))*start
            if factor < 0: pad = pad[::-1]
            proposed += pad.tolist()
        else:
            raise Exception('meshTensor must contain only scalars and len(2) or len(3) tuples.')

    return np.array(proposed)

def closestPoints(mesh, pts, gridLoc='CC'):
    """
        Move a list of points to the closest points on a grid.

        :param simpeg.Mesh.BaseMesh mesh: The mesh
        :param numpy.ndarray pts: Points to move
        :param string gridLoc: ['CC', 'N', 'Fx', 'Fy', 'Fz', 'Ex', 'Ex', 'Ey', 'Ez']
        :rtype: numpy.ndarray
        :return: nodeInds
    """

    pts = asArray_N_x_Dim(pts, mesh.dim)
    grid = getattr(mesh, 'grid' + gridLoc)
    nodeInds = np.empty(pts.shape[0], dtype=int)

    for i, pt in enumerate(pts):
        if mesh.dim == 1:
            nodeInds[i] = ((pt - grid)**2).argmin()
        else:
            nodeInds[i] = ((np.tile(pt, (grid.shape[0],1)) - grid)**2).sum(axis=1).argmin()

    return nodeInds

def readUBCTensorMesh(fileName):
    """
        Read UBC GIF 3DTensor mesh and generate 3D Tensor mesh in simpegTD

        Input:
        :param fileName, path to the UBC GIF mesh file

        Output:
        :param SimPEG TensorMesh object
        :return
    """

    # Interal function to read cell size lines for the UBC mesh files.
    def readCellLine(line):
        for seg in line.split():
            if '*' in seg:
                st = seg
                sp = seg.split('*')
                re = np.array(sp[0],dtype=int)*(' ' + sp[1])
                line = line.replace(st,re.strip())
        return np.array(line.split(),dtype=float)

    # Read the file as line strings, remove lines with comment = !
    msh = np.genfromtxt(fileName,delimiter='\n',dtype=np.str,comments='!')

    # Fist line is the size of the model
    sizeM = np.array(msh[0].split(),dtype=float)
    # Second line is the South-West-Top corner coordinates.
    x0 = np.array(msh[1].split(),dtype=float)
    # Read the cell sizes
    h1 = readCellLine(msh[2])
    h2 = readCellLine(msh[3])
    h3temp = readCellLine(msh[4])
    h3 = h3temp[::-1] # Invert the indexing of the vector to start from the bottom.
    # Adjust the reference point to the bottom south west corner
    x0[2] = x0[2] - np.sum(h3)
    # Make the mesh
    from SimPEG import Mesh
    tensMsh = Mesh.TensorMesh([h1,h2,h3],x0)
    return tensMsh

def readUBCTensorModel(fileName, mesh):
    """
        ReadUBC 3DTensor mesh model and generate 3D Tensor mesh model in simpegTD

    """
    f = open(fileName, 'r')
    model = np.array(map(float, f.readlines()))
    f.close()
    model = np.reshape(model, (mesh.nCz, mesh.nCx, mesh.nCy), order = 'F')
    model = model[::-1,:,:]
    model = np.transpose(model, (1, 2, 0))
    model = mkvc(model)

    return model

def writeUBCTensorMesh(mesh, fileName):
    """
        Writes a SimPEG TensorMesh to a UBC-GIF format mesh file.

        :param simpeg.Mesh.TensorMesh mesh: The mesh
        :param str fileName: File to write to
    """
    assert mesh.dim == 3
    s = ''
    s += '%i %i %i\n' %tuple(mesh.vnC)
    origin = mesh.x0 + np.array([0,0,mesh.hz.sum()]) # Have to it in the same operation or use mesh.x0.copy(), otherwise the mesh.x0 is updated.
    origin.dtype = float

    s += '%.2f %.2f %.2f\n' %tuple(origin)
    s += ('%.2f '*mesh.nCx+'\n')%tuple(mesh.hx)
    s += ('%.2f '*mesh.nCy+'\n')%tuple(mesh.hy)
    s += ('%.2f '*mesh.nCz+'\n')%tuple(mesh.hz[::-1])
    f = open(fileName, 'w')
    f.write(s)
    f.close()

def writeUBCTensorModel(mesh, model, fileName):
    """
        Writes a model associated with a SimPEG TensorMesh
        to a UBC-GIF format model file.

        :param simpeg.Mesh.TensorMesh mesh: The mesh
        :param numpy.ndarray model: The model
        :param str fileName: File to write to
    """

    # Reshape model to a matrix
    modelMat = mesh.r(model,'CC','CC','M')
    # Transpose the axes
    modelMatT = modelMat.transpose((2,0,1))
    # Flip z to positive down
    modelMatTR = mkvc(modelMatT[::-1,:,:])

    np.savetxt(fileName, modelMatTR.ravel())

def writeVTRFile(mesh,model=None,fileName):
    """
    Makes and saves a VTK rectilinear file (vtr) for a simpeg Tensor mesh and model.

    Input:
    :param mesh, SimPEG TensorMesh object - mesh to be transfer to VTK
    :param model, dictionary of numpy.array - Name('s) and array('s). Match number of cells
    :param model, path to the output vtk file

    """

    # Deal with dimensionalities
    if mesh.dim >= 1:
        vX = mesh.vectorNx
        xD = mesh.nNx
        yD,zD = 1,1
        vY, vZ = np.array([0,0])
    if mesh.dim >= 2:
        vY = mesh.vectorNy
        yD = mesh.nNy
    if mesh.dim == 3:
        vZ = mesh.vectorNz
        zD = mesh.nNz
    # Use rectilinear VTK grid.
    # Assign the spatial information.
    vtkObj = vtk.vtkRectilinearGrid()
    vtkObj.SetDimensions(xD,yD,zD)
    vtkObj.SetXCoordinates(npsup.numpy_to_vtk(vX,deep=1))
    vtkObj.SetYCoordinates(npsup.numpy_to_vtk(vY,deep=1))
    vtkObj.SetZCoordinates(npsup.numpy_to_vtk(vZ,deep=1))

    # Assign the model('s) to the object
    for item in model.iteritems():
        # Convert numpy array
        vtkDoubleArr = npsup.numpy_to_vtk(item[1],deep=1)
        vtkDoubleArr.SetName(item[0])
        vtkObj.GetCellData().AddArray(vtkDoubleArr)

    vtkObj.GetCellData().SetActiveScalars(model.keys()[0])
    vtkObj.Update()

    # Write the file.
    # Check the extension of the fileName
    os.path.splitext(fileName)[1]
    if ext is '':
        fileName = fileName + '.vtr'
    elif ext is not '.vtr':
        raise IOError('{:s} is an incorrect extension, has to be .vtr')
    vtrWriteFilter = vtk.vtkXMLRectilinearGridWriter()
    vtrWriteFilter.SetInput(vtkObj)
    vtrWriteFilter.SetFileName(fileName)
    vtrWriteFilter.Update()

if __name__ == '__main__':
    from SimPEG import Mesh
    import matplotlib.pyplot as plt
    tx = [(10.0,10,-1.3),(10.0,40),(10.0,10,1.3)]
    ty = [(10.0,10,-1.3),(10.0,40)]
    M = Mesh.TensorMesh([tx, ty])
    M.plotGrid()
    plt.gca().axis('tight')
    plt.show()
<|endoftext|>


<commit_before>import numpy as np
from scipy import sparse as sp
from matutils import mkvc, ndgrid, sub2ind
from sputils import sdiag

def exampleLomGird(nC, exType):
    assert type(nC) == list, "nC must be a list containing the number of nodes"
    assert len(nC) == 2 or len(nC) == 3, "nC must either two or three dimensions"
    exType = exType.lower()

    possibleTypes = ['rect', 'rotate']
    assert exType in possibleTypes, "Not a possible example type."

    if exType == 'rect':
        return ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
    elif exType == 'rotate':
        if len(nC) == 2:
            X, Y = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2)
            amt[amt < 0] = 0
            return X + (-(Y - 0.5))*amt, Y + (+(X - 0.5))*amt
        elif len(nC) == 3:
            X, Y, Z = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2 + (Z - 0.5)**2)
            amt[amt < 0] = 0
            return X + (-(Y - 0.5))*amt, Y + (-(Z - 0.5))*amt, Z + (-(X - 0.5))*amt

def meshTensors(*args):
    """
        **meshTensors** takes any number of tuples that have the form::

            h1 = ( (numPad, sizeStart [, increaseFactor]), (numCore, sizeCore), (numPad, sizeStart [, increaseFactor]) )

        .. plot::

            from SimPEG import Mesh, Utils
            M = Mesh.TensorMesh(Utils.meshTensors(((10,10),(40,10),(10,10)), ((10,10),(20,10),(0,0))))
            M.plotGrid()

    """
    def padding(num, start, factor=1.3, reverse=False):
        pad = ((np.ones(num)*factor)**np.arange(num))*start
        if reverse: pad = pad[::-1]
        return pad
    tensors = tuple()
    for i, arg in enumerate(args):
        tensors += (np.r_[padding(*arg[0],reverse=True),np.ones(arg[1][0])*arg[1][1],padding(*arg[2])],)

    return list(tensors) if len(tensors) > 1 else tensors[0]

if __name__ == '__main__':
    from SimPEG import mesh
    import matplotlib.pyplot as plt
    M = mesh.TensorMesh(meshTensors(((10,10),(40,10),(10,10)), ((10,10),(20,10),(0,0))))
    M.plotGrid()
    plt.gca().axis('tight')
    plt.show()
<commit_msg>updated docs<commit_after>import numpy as np
from scipy import sparse as sp
from matutils import mkvc, ndgrid, sub2ind
from sputils import sdiag

def exampleLomGird(nC, exType):
    assert type(nC) == list, "nC must be a list containing the number of nodes"
    assert len(nC) == 2 or len(nC) == 3, "nC must either two or three dimensions"
    exType = exType.lower()

    possibleTypes = ['rect', 'rotate']
    assert exType in possibleTypes, "Not a possible example type."

    if exType == 'rect':
        return ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
    elif exType == 'rotate':
        if len(nC) == 2:
            X, Y = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2)
            amt[amt < 0] = 0
            return X + (-(Y - 0.5))*amt, Y + (+(X - 0.5))*amt
        elif len(nC) == 3:
            X, Y, Z = ndgrid([np.cumsum(np.r_[0, np.ones(nx)/nx]) for nx in nC], vector=False)
            amt = 0.5-np.sqrt((X - 0.5)**2 + (Y - 0.5)**2 + (Z - 0.5)**2)
            amt[amt < 0] = 0
            return X + (-(Y - 0.5))*amt, Y + (-(Z - 0.5))*amt, Z + (-(X - 0.5))*amt

def meshTensors(*args):
    """
        **meshTensors** takes any number of tuples that have the form::

            mT = ( (numPad, sizeStart [, increaseFactor]), (numCore, sizeCore), (numPad, sizeStart [, increaseFactor]) )

        .. note::

            The increaseFactor is an optional input.

        .. plot::

            from SimPEG import Mesh, Utils
            M = Mesh.TensorMesh(Utils.meshTensors(((10,10),(40,10),(10,10)), ((10,10),(20,10),(0,0))))
            M.plotGrid()

    """
    def padding(num, start, factor=1.3, reverse=False):
        pad = ((np.ones(num)*factor)**np.arange(num))*start
        if reverse: pad = pad[::-1]
        return pad
    tensors = tuple()
    for i, arg in enumerate(args):
        tensors += (np.r_[padding(*arg[0],reverse=True),np.ones(arg[1][0])*arg[1][1],padding(*arg[2])],)

    return list(tensors) if len(tensors) > 1 else tensors[0]

if __name__ == '__main__':
    from SimPEG import mesh
    import matplotlib.pyplot as plt
    M = mesh.TensorMesh(meshTensors(((10,10),(40,10),(10,10)), ((10,10),(20,10),(0,0))))
    M.plotGrid()
    plt.gca().axis('tight')
    plt.show()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright (c) 2015 Angel Terrones (<angelterrones@gmail.com>)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from math import ceil
from math import log
import os.path
from myhdl import Signal
from myhdl import modbv
from myhdl import always_comb
from myhdl import always
from myhdl import instances
from myhdl import concat
from Core.memIO import MemoryOpConstant
from Core.memIO import MemPortIO

class Memory:
    def __init__(self,
                 clk:          Signal,
                 rst:          Signal,
                 imem:         MemPortIO,
                 dmem:         MemPortIO,
                 SIZE:         int,
                 HEX:          str,
                 BYTES_X_LINE: int):
        assert SIZE >= 2**12, "Memory depth must be a positive number. Min value= 4 KB."
        assert type(HEX) == str and len(HEX) != 0, "Please, indicate a valid name for the bin file."
        assert os.path.isfile(HEX), "HEX file does not exist. Please, indicate a valid name"
        assert type(BYTES_X_LINE) == int and BYTES_X_LINE > 0, "Number of bytes por line must be a positive number"
        assert not (BYTES_X_LINE & (BYTES_X_LINE - 1)), "Number of bytes por line must be a power of 2"

        aw                = int(ceil(log(SIZE, 2)))
        self.bytes_x_line = BYTES_X_LINE
        self.clk          = clk
        self.rst          = rst
        self.imem         = imem
        self.dmem         = dmem
        self.i_data_o     = Signal(modbv(0)[32:])
        self.d_data_o     = Signal(modbv(0)[32:])
        self._memory      = [None for ii in range(0, 2**(aw - 2))]  # WORDS, no bytes
        self._imem_addr   = Signal(modbv(0)[30:])
        self._dmem_addr   = Signal(modbv(0)[30:])

        depth = 2 ** (aw - 2)  # depth in words. Different from file
        self.LoadMemory(depth, HEX)

    def LoadMemory(self, depth: int, bin_file: str):
        """
        Load a HEX file. The file have (2 * NBYTES + 1) por line.
        """
        n_lines = int(os.path.getsize(bin_file) / (2 * self.bytes_x_line + 1))  # calculate the depth in words
        word_x_line = self.bytes_x_line >> 2
        assert depth >= n_lines * 4, "Depth mismatch: {0} < {1}".format(depth, n_lines)

        with open(bin_file) as f:
            lines_f = [line.strip() for line in f]
            lines = [line[8 * i:8 * (i + 1)] for line in lines_f for i in range(word_x_line - 1, -1, -1)]

        for addr in range(depth):
            data = int(lines[addr], 16)
            self._memory[addr] = Signal(modbv(data)[32:])

    def GetRTL(self):
        @always(self.clk.posedge)
        def set_ready_signal():
            self.imem.resp.valid.next = False if self.rst else self.imem.req.valid
            self.dmem.resp.valid.next = False if self.rst else self.dmem.req.valid

        @always_comb
        def assignment_data_o():
            self.imem.resp.data.next = self.i_data_o if self.imem.resp.valid else 0xDEADF00D
            self.dmem.resp.data.next = self.d_data_o if self.dmem.resp.valid else 0xDEADF00D

        @always(self.clk.posedge)
        def set_fault():
            self.imem.resp.fault.next = False
            self.dmem.resp.fault.next = False

        @always_comb
        def assignment_addr():
            # This memory is addressed by word, not byte. Ignore the 2 LSB.
            self._imem_addr.next = self.imem.req.addr[32:2]
            self._dmem_addr.next = self.dmem.req.addr[32:2]

        @always(self.clk.posedge)
        def imem_rtl():
            self.i_data_o.next = self._memory[self._imem_addr]

            if self.imem.req.fcn == MemoryOpConstant.M_WR:
                we                = self.imem.req.wr
                data              = self.imem.req.data
                self.i_data_o.next = self.imem.req.data
                self._memory[self._imem_addr].next = concat(data[8:0] if we[0] and self.imem.req.valid else self._memory[self._imem_addr][8:0],
                                                            data[16:8] if we[1] and self.imem.req.valid else self._memory[self._imem_addr][16:8],
                                                            data[24:16] if we[2] and self.imem.req.valid else self._memory[self._imem_addr][24:16],
                                                            data[32:24] if we[3] and self.imem.req.valid else self._memory[self._imem_addr][32:24])

        @always(self.clk.posedge)
        def dmem_rtl():
            self.d_data_o.next = self._memory[self._dmem_addr]

            if self.dmem.req.fcn == MemoryOpConstant.M_WR:
                we                 = self.dmem.req.wr
                data               = self.dmem.req.data
                self.d_data_o.next = self.dmem.req.data
                self._memory[self._dmem_addr].next = concat(data[8:0] if we[0] and self.dmem.req.valid else self._memory[self._dmem_addr][8:0],
                                                            data[16:8] if we[1] and self.dmem.req.valid else self._memory[self._dmem_addr][16:8],
                                                            data[24:16] if we[2] and self.dmem.req.valid else self._memory[self._dmem_addr][24:16],
                                                            data[32:24] if we[3] and self.dmem.req.valid else self._memory[self._dmem_addr][32:24])

        return instances()

# Local Variables:
# flycheck-flake8-maximum-line-length: 200
# flycheck-flake8rc: ".flake8rc"
# End:
<commit_msg>Fix the assert condition.<commit_after>#!/usr/bin/env python
# Copyright (c) 2015 Angel Terrones (<angelterrones@gmail.com>)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from math import ceil
from math import log
import os.path
from myhdl import Signal
from myhdl import modbv
from myhdl import always_comb
from myhdl import always
from myhdl import instances
from myhdl import concat
from Core.memIO import MemoryOpConstant
from Core.memIO import MemPortIO

class Memory:
    def __init__(self,
                 clk:          Signal,
                 rst:          Signal,
                 imem:         MemPortIO,
                 dmem:         MemPortIO,
                 SIZE:         int,
                 HEX:          str,
                 BYTES_X_LINE: int):
        assert SIZE >= 2**12, "Memory depth must be a positive number. Min value= 4 KB."
        assert type(HEX) == str and len(HEX) != 0, "Please, indicate a valid name for the bin file."
        assert os.path.isfile(HEX), "HEX file does not exist. Please, indicate a valid name"
        assert type(BYTES_X_LINE) == int and BYTES_X_LINE > 0, "Number of bytes por line must be a positive number"
        assert not (BYTES_X_LINE & (BYTES_X_LINE - 1)), "Number of bytes por line must be a power of 2"

        aw                = int(ceil(log(SIZE, 2)))
        self.bytes_x_line = BYTES_X_LINE
        self.clk          = clk
        self.rst          = rst
        self.imem         = imem
        self.dmem         = dmem
        self.i_data_o     = Signal(modbv(0)[32:])
        self.d_data_o     = Signal(modbv(0)[32:])
        self._memory      = [None for ii in range(0, 2**(aw - 2))]  # WORDS, no bytes
        self._imem_addr   = Signal(modbv(0)[30:])
        self._dmem_addr   = Signal(modbv(0)[30:])

        depth = 2 ** (aw - 2)  # depth in words. Different from file
        self.LoadMemory(depth, HEX)

    def LoadMemory(self, depth: int, bin_file: str):
        """
        Load a HEX file. The file have (2 * NBYTES + 1) por line.
        """
        n_lines = int(os.path.getsize(bin_file) / (2 * self.bytes_x_line + 1))  # calculate the depth in words
        word_x_line = self.bytes_x_line >> 2
        assert depth >= n_lines * 4, "Depth mismatch (1 word x line): {0} < {1}".format(depth, n_lines *4)

        with open(bin_file) as f:
            lines_f = [line.strip() for line in f]
            lines = [line[8 * i:8 * (i + 1)] for line in lines_f for i in range(word_x_line - 1, -1, -1)]

        for addr in range(depth):
            data = int(lines[addr], 16)
            self._memory[addr] = Signal(modbv(data)[32:])

    def GetRTL(self):
        @always(self.clk.posedge)
        def set_ready_signal():
            self.imem.resp.valid.next = False if self.rst else self.imem.req.valid
            self.dmem.resp.valid.next = False if self.rst else self.dmem.req.valid

        @always_comb
        def assignment_data_o():
            self.imem.resp.data.next = self.i_data_o if self.imem.resp.valid else 0xDEADF00D
            self.dmem.resp.data.next = self.d_data_o if self.dmem.resp.valid else 0xDEADF00D

        @always(self.clk.posedge)
        def set_fault():
            self.imem.resp.fault.next = False
            self.dmem.resp.fault.next = False

        @always_comb
        def assignment_addr():
            # This memory is addressed by word, not byte. Ignore the 2 LSB.
            self._imem_addr.next = self.imem.req.addr[32:2]
            self._dmem_addr.next = self.dmem.req.addr[32:2]

        @always(self.clk.posedge)
        def imem_rtl():
            self.i_data_o.next = self._memory[self._imem_addr]

            if self.imem.req.fcn == MemoryOpConstant.M_WR:
                we                = self.imem.req.wr
                data              = self.imem.req.data
                self.i_data_o.next = self.imem.req.data
                self._memory[self._imem_addr].next = concat(data[8:0] if we[0] and self.imem.req.valid else self._memory[self._imem_addr][8:0],
                                                            data[16:8] if we[1] and self.imem.req.valid else self._memory[self._imem_addr][16:8],
                                                            data[24:16] if we[2] and self.imem.req.valid else self._memory[self._imem_addr][24:16],
                                                            data[32:24] if we[3] and self.imem.req.valid else self._memory[self._imem_addr][32:24])

        @always(self.clk.posedge)
        def dmem_rtl():
            self.d_data_o.next = self._memory[self._dmem_addr]

            if self.dmem.req.fcn == MemoryOpConstant.M_WR:
                we                 = self.dmem.req.wr
                data               = self.dmem.req.data
                self.d_data_o.next = self.dmem.req.data
                self._memory[self._dmem_addr].next = concat(data[8:0] if we[0] and self.dmem.req.valid else self._memory[self._dmem_addr][8:0],
                                                            data[16:8] if we[1] and self.dmem.req.valid else self._memory[self._dmem_addr][16:8],
                                                            data[24:16] if we[2] and self.dmem.req.valid else self._memory[self._dmem_addr][24:16],
                                                            data[32:24] if we[3] and self.dmem.req.valid else self._memory[self._dmem_addr][32:24])

        return instances()

# Local Variables:
# flycheck-flake8-maximum-line-length: 200
# flycheck-flake8rc: ".flake8rc"
# End:
<|endoftext|>


<commit_before>import numpy as np
from unittest import TestCase

import VZcomp.qdef as qdef

class Quantum_definitions(TestCase):

    @classmethod
    def setUpClass(self):
        pass

    def test_pauli_matrices(self):

        XX = np.real(np.dot(qdef.X, qdef.X))
        YY = np.real(np.dot(qdef.Y, qdef.Y))
        ZZ = np.real(np.dot(qdef.Z, qdef.Z))

        self.assertAlmostEqual(np.real_if_close(XX[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(XX[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(XX[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(XX[1, 1]), 1)

        self.assertAlmostEqual(np.real_if_close(YY[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(YY[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(YY[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(YY[1, 1]), 1)

        self.assertAlmostEqual(np.real_if_close(ZZ[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(ZZ[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(ZZ[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(ZZ[1, 1]), 1)

    def test_rotation_matrix(self):

        I_0axis = qdef.qrot2mat([0, 0, 0], np.pi)

        I_0angle = qdef.qrot2mat([123, 213, 23], 0.)

        X180 = qdef.qrot2mat([1, 0, 0], np.pi)

        Y180 = qdef.qrot2mat([0, 1, 0], np.pi)

        Z180 = qdef.qrot2mat([0, 0, 1], np.pi)

        # include checks versus ideal.
<commit_msg>Keep enhancing minor complains of codacy<commit_after>import numpy as np
from unittest import TestCase

import VZcomp.qdef as qdef

class Quantum_definitions(TestCase):

    @classmethod
    def setUpClass(self):
        pass

    def test_pauli_matrices(self):

        XX = np.real(np.dot(qdef.X, qdef.X))
        YY = np.real(np.dot(qdef.Y, qdef.Y))
        ZZ = np.real(np.dot(qdef.Z, qdef.Z))

        self.assertAlmostEqual(np.real_if_close(XX[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(XX[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(XX[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(XX[1, 1]), 1)

        self.assertAlmostEqual(np.real_if_close(YY[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(YY[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(YY[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(YY[1, 1]), 1)

        self.assertAlmostEqual(np.real_if_close(ZZ[0, 0]), 1)
        self.assertAlmostEqual(np.real_if_close(ZZ[0, 1]), 0)
        self.assertAlmostEqual(np.real_if_close(ZZ[1, 0]), 0)
        self.assertAlmostEqual(np.real_if_close(ZZ[1, 1]), 1)
'''
    def test_rotation_matrix(self):

        I_0axis = qdef.qrot2mat([0, 0, 0], np.pi)

        I_0angle = qdef.qrot2mat([123, 213, 23], 0.)

        X180 = qdef.qrot2mat([1, 0, 0], np.pi)

        Y180 = qdef.qrot2mat([0, 1, 0], np.pi)

        Z180 = qdef.qrot2mat([0, 0, 1], np.pi)

        # include checks versus ideal.
'''<|endoftext|>


<commit_before>"""SPCM."""

# --- import --------------------------------------------------------------------------------------

import os
import collections
import time

import numpy as np

from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp

# --- define --------------------------------------------------------------------------------------

__all__ = ["from_spcm"]

# --- from function -------------------------------------------------------------------------------

def from_spcm(filepath, name=None, *, delimiter=",", parent=None, verbose=True):
    """Create a ``Data`` object from a Becker & Hickl `spcm`__ file (ASCII-exported, ``.asc``).

    If provided, setup parameters are stored in the ``attrs`` dictionary of the ``Data`` object.

    __ http://www.becker-hickl.com/software/spcm.htm

    Parameters
    ----------
    filepath : string
        Path to SPC-xxx .asc file.
    name : string (optional)
        Name to give to the created data object. If None, filename is used.
        Default is None.
    delimiter : string (optional)
        The string used to separate values. Default is ','.
    parent : WrightTools.Collection (optional)
        Collection to place new data object within. Default is None.
    verbose : boolean (optional)
        Toggle talkback. Default is True.

    Returns
    -------
    WrightTools.data.Data object
    """
    # check filepath
    if not filepath.endswith("asc"):
        wt_exceptions.WrongFileTypeWarning.warn(filepath, "asc")
    # parse name
    if not name:
        name = os.path.basename("filepath").split(".")[0]
    # create headers dictionary
    headers = collections.OrderedDict()
    header_lines = 0
    with open(filepath) as f:
        while True:
            line = f.readline().strip()
            header_lines += 1
            if len(line) == 0:
                break
            else:
                key, value = line.split(":", 1)
                if key.strip() == "Revision":
                    headers["resolution"] = int(value.strip(" bits ADC"))
                else:
                    headers[key.strip()] = value.strip()
        line = f.readline().strip()
        while "_BEGIN" in line:
            header_lines += 1
            section = line.split("_BEGIN")[0]
            while True:
                line = f.readline().strip()
                header_lines += 1
                if section + "_END" in line:
                    break
                if section == "SYS_PARA":
                    use_type = {
                        "B": lambda b: int(b) == 1,
                        "C": str,  # e.g. #SP [SP_OVERFL,C,N]
                        "F": float,
                        "I": int,
                        "L": int,  # e.g. #DI [DI_MAXCNT,L,128]
                        "S": str,
                        "U": int,  # unsigned int?
                    }
                    item = line[line.find("[") + 1 : line.find("]")].split(",")
                    key = item[0]
                    value = use_type[item[1]](item[2])
                    headers[key] = value
                else:
                    splitted = line.split()
                    value = splitted[-1][1:-1].split(",")
                    key = " ".join(splitted[:-1])
                    headers[key] = value
            line = f.readline().strip()
            if "END" in line:
                header_lines += 1
                break
    if "Date" in headers.keys() and "Time" in headers.keys():
        # NOTE:  reports created in local time, no-way to calculate absolute time
        created = " ".join([headers["Date"], headers["Time"]])
        created = time.strptime(created, "%Y-%m-%d %H:%M:%S")
        created = timestamp.TimeStamp(time.mktime(created)).RFC3339

    # initialize data object
    kwargs = {"name": name, "kind": "spcm", "source": filepath, **headers}
    if parent:
        data = parent.create_data(**kwargs)
    else:
        data = Data(**kwargs)
    # import data
    arr = np.genfromtxt(
        filepath, skip_header=(header_lines + 1), skip_footer=1, delimiter=delimiter, unpack=True
    )
    # construct data
    data.create_variable(name="time", values=arr[0], units="ns")
    data.create_channel(name="counts", values=arr[1])
    data.transform("time")
    # finish
    if verbose:
        print("data created at {0}".format(data.fullpath))
        print("  kind: {0}".format(data.kind))
        print("  range: {0} to {1} (ns)".format(data.time[0], data.time[-1]))
        print("  size: {0}".format(data.size))
        if "SP_COL_T" in data.attrs.keys():
            print("  collection time:  {0} sec".format(data.attrs["SP_COL_T"]))
    return data
<commit_msg>created to attrs<commit_after>"""SPCM."""

# --- import --------------------------------------------------------------------------------------

import os
import collections
import time

import numpy as np

from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp

# --- define --------------------------------------------------------------------------------------

__all__ = ["from_spcm"]

# --- from function -------------------------------------------------------------------------------

def from_spcm(filepath, name=None, *, delimiter=",", parent=None, verbose=True):
    """Create a ``Data`` object from a Becker & Hickl `spcm`__ file (ASCII-exported, ``.asc``).

    If provided, setup parameters are stored in the ``attrs`` dictionary of the ``Data`` object.

    __ http://www.becker-hickl.com/software/spcm.htm

    Parameters
    ----------
    filepath : string
        Path to SPC-xxx .asc file.
    name : string (optional)
        Name to give to the created data object. If None, filename is used.
        Default is None.
    delimiter : string (optional)
        The string used to separate values. Default is ','.
    parent : WrightTools.Collection (optional)
        Collection to place new data object within. Default is None.
    verbose : boolean (optional)
        Toggle talkback. Default is True.

    Returns
    -------
    WrightTools.data.Data object
    """
    # check filepath
    if not filepath.endswith("asc"):
        wt_exceptions.WrongFileTypeWarning.warn(filepath, "asc")
    # parse name
    if not name:
        name = os.path.basename("filepath").split(".")[0]
    # create headers dictionary
    headers = collections.OrderedDict()
    header_lines = 0
    with open(filepath) as f:
        while True:
            line = f.readline().strip()
            header_lines += 1
            if len(line) == 0:
                break
            else:
                key, value = line.split(":", 1)
                if key.strip() == "Revision":
                    headers["resolution"] = int(value.strip(" bits ADC"))
                else:
                    headers[key.strip()] = value.strip()
        line = f.readline().strip()
        while "_BEGIN" in line:
            header_lines += 1
            section = line.split("_BEGIN")[0]
            while True:
                line = f.readline().strip()
                header_lines += 1
                if section + "_END" in line:
                    break
                if section == "SYS_PARA":
                    use_type = {
                        "B": lambda b: int(b) == 1,
                        "C": str,  # e.g. #SP [SP_OVERFL,C,N]
                        "F": float,
                        "I": int,
                        "L": int,  # e.g. #DI [DI_MAXCNT,L,128]
                        "S": str,
                        "U": int,  # unsigned int?
                    }
                    item = line[line.find("[") + 1 : line.find("]")].split(",")
                    key = item[0]
                    value = use_type[item[1]](item[2])
                    headers[key] = value
                else:
                    splitted = line.split()
                    value = splitted[-1][1:-1].split(",")
                    key = " ".join(splitted[:-1])
                    headers[key] = value
            line = f.readline().strip()
            if "END" in line:
                header_lines += 1
                break
    if "Date" in headers.keys() and "Time" in headers.keys():
        # NOTE:  reports created in local time, no-way to calculate absolute time
        created = " ".join([headers["Date"], headers["Time"]])
        created = time.strptime(created, "%Y-%m-%d %H:%M:%S")
        created = timestamp.TimeStamp(time.mktime(created)).RFC3339
        headers['created'] = created

    # initialize data object
    kwargs = {"name": name, "kind": "spcm", "source": filepath, **headers}
    if parent:
        data = parent.create_data(**kwargs)
    else:
        data = Data(**kwargs)
    # import data
    arr = np.genfromtxt(
        filepath, skip_header=(header_lines + 1), skip_footer=1, delimiter=delimiter, unpack=True
    )
    # construct data
    data.create_variable(name="time", values=arr[0], units="ns")
    data.create_channel(name="counts", values=arr[1])
    data.transform("time")
    # finish
    if verbose:
        print("data created at {0}".format(data.fullpath))
        print("  kind: {0}".format(data.kind))
        print("  range: {0} to {1} (ns)".format(data.time[0], data.time[-1]))
        print("  size: {0}".format(data.size))
        if "SP_COL_T" in data.attrs.keys():
            print("  collection time:  {0} sec".format(data.attrs["SP_COL_T"]))
    return data
<|endoftext|>


<commit_before>##################################################
# VisualizerService_services_server.py
#      Generated by ZSI.generate.wsdl2dispatch.ServiceModuleWriter
#
##################################################

from VisualizerService_services import *
from ZSI.ServiceContainer import ServiceSOAPBinding

class VisualizerService(ServiceSOAPBinding):
    soapAction = {}
    root = {}
    _wsdl = """<?xml version=\"1.0\" ?>
<wsdl:definitions name=\"VisualizerService\" targetNamespace=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
	<wsdl:types>
		<xsd:schema targetNamespace=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:apollo=\"http://types.apollo.pitt.edu/\">
			<xsd:import namespace=\"http://types.apollo.pitt.edu/\" schemaLocation=\"apollo-types.xsd\">
			</xsd:import>
			<xsd:element name=\"run\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"visualizerConfiguration\" type=\"apollo:VisualizerConfiguration\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"runResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"visualizerResult\" type=\"apollo:VisualizerResult\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getRunStatus\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runId\" type=\"apollo:RunIdentification\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getRunStatusResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runStatus\" type=\"apollo:RunStatus\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getConfigurationFileForRun\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runIdentification\" type=\"apollo:RunIdentification\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getConfigurationFileForRunResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"configurationFile\" type=\"xsd:string\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name=\"runRequest\">
		<wsdl:part element=\"tns:run\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"runResponse\">
		<wsdl:part element=\"tns:runResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getRunStatusRequest\">
		<wsdl:part element=\"tns:getRunStatus\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getRunStatusResponse\">
		<wsdl:part element=\"tns:getRunStatusResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getConfigurationFileForRunRequest\">
		<wsdl:part element=\"tns:getConfigurationFileForRun\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getConfigurationFileForRunResponse\">
		<wsdl:part element=\"tns:getConfigurationFileForRunResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:portType name=\"VisualizerServiceEI\">
		<wsdl:operation name=\"run\">
			<wsdl:input message=\"tns:runRequest\"/>
			<wsdl:output message=\"tns:runResponse\"/>
		</wsdl:operation>
		<wsdl:operation name=\"getRunStatus\">
			<wsdl:input message=\"tns:getRunStatusRequest\"/>
			<wsdl:output message=\"tns:getRunStatusResponse\"/>
		</wsdl:operation>
		<wsdl:operation name=\"getConfigurationFileForRun\">
			<wsdl:input message=\"tns:getConfigurationFileForRunRequest\"/>
			<wsdl:output message=\"tns:getConfigurationFileForRunResponse\"/>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name=\"VisualizerServiceBinding\" type=\"tns:VisualizerServiceEI\">
		<soap:binding style=\"document\" transport=\"http://schemas.xmlsoap.org/soap/http\"/>
		<wsdl:operation name=\"run\">
			<soap:operation soapAction=\"http://service.apollo.pitt.edu/visualizerservice/run\"/>
			<wsdl:input>
				<soap:body use=\"literal\"/>
			</wsdl:input>
			<wsdl:output>
				<soap:body use=\"literal\"/>
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name=\"getRunStatus\">
			<soap:operation soapAction=\"http://service.apollo.pitt.edu/visualizerservice/getRunStatus\"/>
			<wsdl:input>
				<soap:body use=\"literal\"/>
			</wsdl:input>
			<wsdl:output>
				<soap:body use=\"literal\"/>
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name=\"VisualizerService\">
		<wsdl:port binding=\"tns:VisualizerServiceBinding\" name=\"VisualizerServiceEndpoint\">
			<soap:address location=\"http://localhost:8080/visualizerservice/services/visualizerservice\"/>
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>"""

    def __init__(self, post='/visualizerservice/services/visualizerservice', **kw):
        ServiceSOAPBinding.__init__(self, post)

    def soap_run(self, ps):
        self.request = ps.Parse(runRequest.typecode)
        return runResponse()

    soapAction['http://service.apollo.pitt.edu/visualizerservice/run'] = 'soap_run'
    root[(runRequest.typecode.nspname,runRequest.typecode.pname)] = 'soap_run'

    def soap_getRunStatus(self, ps):
        self.request = ps.Parse(getRunStatusRequest.typecode)
        return getRunStatusResponse()

    soapAction['http://service.apollo.pitt.edu/visualizerservice/getRunStatus'] = 'soap_getRunStatus'
    root[(getRunStatusRequest.typecode.nspname,getRunStatusRequest.typecode.pname)] = 'soap_getRunStatus'

<commit_msg>changed location of XSD<commit_after>##################################################
# VisualizerService_services_server.py
#      Generated by ZSI.generate.wsdl2dispatch.ServiceModuleWriter
#
##################################################

from VisualizerService_services import *
from ZSI.ServiceContainer import ServiceSOAPBinding

class VisualizerService(ServiceSOAPBinding):
    soapAction = {}
    root = {}
    _wsdl = """<?xml version=\"1.0\" ?>
<wsdl:definitions name=\"VisualizerService\" targetNamespace=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
	<wsdl:types>
		<xsd:schema targetNamespace=\"http://service.apollo.pitt.edu/visualizerservice/\" xmlns:apollo=\"http://types.apollo.pitt.edu/\">
			<xsd:import namespace=\"http://types.apollo.pitt.edu/\" schemaLocation=\"http://research.rods.pitt.edu/apollo-types.xsd\">
			</xsd:import>
			<xsd:element name=\"run\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"visualizerConfiguration\" type=\"apollo:VisualizerConfiguration\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"runResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"visualizerResult\" type=\"apollo:VisualizerResult\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getRunStatus\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runId\" type=\"apollo:RunIdentification\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getRunStatusResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runStatus\" type=\"apollo:RunStatus\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getConfigurationFileForRun\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"runIdentification\" type=\"apollo:RunIdentification\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name=\"getConfigurationFileForRunResponse\">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name=\"configurationFile\" type=\"xsd:string\"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name=\"runRequest\">
		<wsdl:part element=\"tns:run\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"runResponse\">
		<wsdl:part element=\"tns:runResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getRunStatusRequest\">
		<wsdl:part element=\"tns:getRunStatus\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getRunStatusResponse\">
		<wsdl:part element=\"tns:getRunStatusResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getConfigurationFileForRunRequest\">
		<wsdl:part element=\"tns:getConfigurationFileForRun\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:message name=\"getConfigurationFileForRunResponse\">
		<wsdl:part element=\"tns:getConfigurationFileForRunResponse\" name=\"parameters\"/>
	</wsdl:message>
	<wsdl:portType name=\"VisualizerServiceEI\">
		<wsdl:operation name=\"run\">
			<wsdl:input message=\"tns:runRequest\"/>
			<wsdl:output message=\"tns:runResponse\"/>
		</wsdl:operation>
		<wsdl:operation name=\"getRunStatus\">
			<wsdl:input message=\"tns:getRunStatusRequest\"/>
			<wsdl:output message=\"tns:getRunStatusResponse\"/>
		</wsdl:operation>
		<wsdl:operation name=\"getConfigurationFileForRun\">
			<wsdl:input message=\"tns:getConfigurationFileForRunRequest\"/>
			<wsdl:output message=\"tns:getConfigurationFileForRunResponse\"/>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name=\"VisualizerServiceBinding\" type=\"tns:VisualizerServiceEI\">
		<soap:binding style=\"document\" transport=\"http://schemas.xmlsoap.org/soap/http\"/>
		<wsdl:operation name=\"run\">
			<soap:operation soapAction=\"http://service.apollo.pitt.edu/visualizerservice/run\"/>
			<wsdl:input>
				<soap:body use=\"literal\"/>
			</wsdl:input>
			<wsdl:output>
				<soap:body use=\"literal\"/>
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name=\"getRunStatus\">
			<soap:operation soapAction=\"http://service.apollo.pitt.edu/visualizerservice/getRunStatus\"/>
			<wsdl:input>
				<soap:body use=\"literal\"/>
			</wsdl:input>
			<wsdl:output>
				<soap:body use=\"literal\"/>
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name=\"VisualizerService\">
		<wsdl:port binding=\"tns:VisualizerServiceBinding\" name=\"VisualizerServiceEndpoint\">
			<soap:address location=\"http://localhost:8080/visualizerservice/services/visualizerservice\"/>
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>"""

    def __init__(self, post='/visualizerservice/services/visualizerservice', **kw):
        ServiceSOAPBinding.__init__(self, post)

    def soap_run(self, ps):
        self.request = ps.Parse(runRequest.typecode)
        return runResponse()

    soapAction['http://service.apollo.pitt.edu/visualizerservice/run'] = 'soap_run'
    root[(runRequest.typecode.nspname,runRequest.typecode.pname)] = 'soap_run'

    def soap_getRunStatus(self, ps):
        self.request = ps.Parse(getRunStatusRequest.typecode)
        return getRunStatusResponse()

    soapAction['http://service.apollo.pitt.edu/visualizerservice/getRunStatus'] = 'soap_getRunStatus'
    root[(getRunStatusRequest.typecode.nspname,getRunStatusRequest.typecode.pname)] = 'soap_getRunStatus'

<|endoftext|>


<commit_before>#!/usr/bin/env python3

import copy
import importlib
import logging
import argparse
import os
import requests
import sys
import yaml

from aatest.common import setup_logger
from aatest.comhandler import ComHandler
from saml2.httputil import Response

from saml2test import metadata

from saml2test.util import collect_ec
from saml2test.util import get_check

from saml2test.idp_test.common import make_entity
from saml2test.idp_test.common import map_prof
from saml2test.idp_test.common import Trace
from saml2test.idp_test.prof_util import ProfileHandler
from saml2test.idp_test.func import factory
from saml2test.idp_test.cl_request import factory as cl_factory
from saml2test.idp_test.wb_request import factory as wb_factory

from aatest.parse_cnf import parse_json_conf
from aatest.parse_cnf import parse_yaml_conf

from saml2.saml import factory as saml_message_factory
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from saml2test import operation

from saml2test import configloader
from saml2test.webserver import staticfiles, mako

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

__author__ = 'roland'

logger = logging.getLogger("")

def load_flows(fdef, flow_spec, use):
    cls_factories = {'cl': cl_factory, 'wb': wb_factory, '': operation.factory}

    if flow_spec.endswith('.yaml'):
        spec = parse_yaml_conf(flow_spec, cls_factories, factory, use=use)
    elif flow_spec.endswith('.json'):
        spec = parse_json_conf(flow_spec, cls_factories, factory, use=use)
    else:
        raise Exception('Unknown file type')

    for param in ['Flows', 'Desc']:
        try:
            fdef[param].update(spec[param])
        except KeyError:
            pass

    fdef['Order'].extend(spec['Order'])

    return fdef

def arg(param, cargs, conf):
    try:
        return getattr(cargs, param)
    except AttributeError:
        try:
            return conf[param]
        except KeyError:
            return None

def setup(use='cl', cargs=None):
    if cargs is None:
        parser = argparse.ArgumentParser()
        parser.add_argument('-k', dest="insecure", action='store_true')
        parser.add_argument('-x', dest="break", action='store_true')
        parser.add_argument('-t', dest="testid")
        parser.add_argument('-T', dest='toolconf')   # TODO: is this really optional?
        parser.add_argument(dest="config")
        cargs = parser.parse_args()

    fdef = {'Flows': {}, 'Order': [], 'Desc': {}}

    try:
        with open(cargs.toolconf, 'r') as fd:
            conf = yaml.safe_load(fd)
    except FileNotFoundError as e:
        raise Exception('unable to open tool configuration file: cwd=' + os.getcwd() + ', ' + str(e))
    try:
        for yf in conf['flows']:
            fdef = load_flows(fdef, yf, use)
    except KeyError:
        pass # TODO: is it really OK not to have any flows?

    # Filter flows based on profile
    keep = []
    for key, val in fdef['Flows'].items():
        for p in conf['profile']:
            if p in val['profiles']:
                keep.append(key)

    for key in list(fdef['Flows'].keys()):
        if key not in keep:
            del fdef['Flows'][key]

    sys.path.insert(0, '.')

    loader = configloader.ConfigLoader()
    try:
        CONF = loader.conf_CONF()
    except configloader.ConfigFileNotReadable as e:
        configloader.exit_on_mandatory_config_file(e)

    #CONF = loader.load_file(conf['samlconf'] + ".py", 'configuration')
    spconf = copy.deepcopy(CONF.CONFIG)
    acnf = list(spconf.values())[0]
    mds = metadata.load(True, acnf, CONF.METADATA, 'sp')

    if arg('log_name', cargs, conf):
        setup_logger(logger, cargs.log_name)
    elif arg('testid', cargs, conf):
        setup_logger(logger, "{}.log".format(cargs.testid))
    else:
        setup_logger(logger)

    ch = []
    try:
        c_handler = conf['content_handler']
    except KeyError:
        comhandler = None
    else:
        for item in c_handler:
            for key, kwargs in item.items():  # should only be one
                if key == 'robobrowser':
                    from aatest.contenthandler import robobrowser
                    ch.append(robobrowser.factory(**kwargs))
        comhandler = ComHandler(ch)

    mako_path = mako.__path__[0] + os.sep
    staticfiles_path = staticfiles.__path__[0] + os.sep

    kwargs = {"base_url": copy.copy(CONF.BASE), 'spconf': spconf,
              "flows": fdef['Flows'], "order": fdef['Order'],
              "desc": fdef['Desc'], 'metadata': mds,
              "profile": conf['profile'], "msg_factory": saml_message_factory,
              "check_factory": get_check, "profile_handler": ProfileHandler,
              "cache": {},
              'map_prof': map_prof, 'make_entity': make_entity,
              'trace_cls': Trace, 'conv_args': {'entcat': collect_ec()},
              'com_handler': comhandler, 'conf': CONF, 'response_cls': Response,
              'template_root': mako_path, 'static': staticfiles_path }

    try:
        kwargs["entity_id"] = CONF.ENTITY_ID
    except KeyError:
        kwargs['disco_srv'] = conf['disco_srv']

    kwargs["insecure"] = CONF.DO_NOT_VALIDATE_TLS

    return cargs, kwargs<commit_msg>reducing test_tool.yaml<commit_after>#!/usr/bin/env python3

import copy
import importlib
import logging
import argparse
import os
import requests
import sys
import yaml

from aatest.common import setup_logger
from aatest.comhandler import ComHandler
from saml2.httputil import Response

from saml2test import metadata

from saml2test.util import collect_ec
from saml2test.util import get_check

from saml2test.idp_test.common import make_entity
from saml2test.idp_test.common import map_prof
from saml2test.idp_test.common import Trace
from saml2test.idp_test.prof_util import ProfileHandler
from saml2test.idp_test.func import factory
from saml2test.idp_test.cl_request import factory as cl_factory
from saml2test.idp_test.wb_request import factory as wb_factory

from aatest.parse_cnf import parse_json_conf
from aatest.parse_cnf import parse_yaml_conf

from saml2.saml import factory as saml_message_factory
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from saml2test import operation

from saml2test import configloader
from saml2test.webserver import staticfiles, mako

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

__author__ = 'roland'

logger = logging.getLogger("")

def load_flows(fdef, flow_spec, use):
    cls_factories = {'cl': cl_factory, 'wb': wb_factory, '': operation.factory}

    if flow_spec.endswith('.yaml'):
        spec = parse_yaml_conf(flow_spec, cls_factories, factory, use=use)
    elif flow_spec.endswith('.json'):
        spec = parse_json_conf(flow_spec, cls_factories, factory, use=use)
    else:
        raise Exception('Unknown file type')

    for param in ['Flows', 'Desc']:
        try:
            fdef[param].update(spec[param])
        except KeyError:
            pass

    fdef['Order'].extend(spec['Order'])

    return fdef

def arg(param, cargs, conf):
    try:
        return getattr(cargs, param)
    except AttributeError:
        try:
            return conf[param]
        except KeyError:
            return None

def setup(use='cl', cargs=None):
    if cargs is None:
        parser = argparse.ArgumentParser()
        parser.add_argument('-k', dest="insecure", action='store_true')
        parser.add_argument('-x', dest="break", action='store_true')
        parser.add_argument('-t', dest="testid")
        parser.add_argument('-T', dest='toolconf')   # TODO: is this really optional?
        parser.add_argument(dest="config")
        cargs = parser.parse_args()

    fdef = {'Flows': {}, 'Order': [], 'Desc': {}}

    loader = configloader.ConfigLoader()
    try:
        CONF = loader.conf_CONF()
    except configloader.ConfigFileNotReadable as e:
        configloader.exit_on_mandatory_config_file(e)

    try:
        with open(cargs.toolconf, 'r') as fd:
            conf = yaml.safe_load(fd)
    except FileNotFoundError as e:
        raise Exception('unable to open tool configuration file: cwd=' + os.getcwd() + ', ' + str(e))
    try:
        for yf in conf['flows']:
            fdef = load_flows(fdef, yf, use)
    except KeyError:
        pass # TODO: is it really OK not to have any flows?

    # Filter flows based on profile
    keep = []
    for key, val in fdef['Flows'].items():
        for p in conf['profile']:
            if p in val['profiles']:
                keep.append(key)

    for key in list(fdef['Flows'].keys()):
        if key not in keep:
            del fdef['Flows'][key]

    spconf = copy.deepcopy(CONF.CONFIG)
    acnf = list(spconf.values())[0]
    mds = metadata.load(True, acnf, CONF.METADATA, 'sp')

    if arg('log_name', cargs, conf):
        setup_logger(logger, cargs.log_name)
    elif arg('testid', cargs, conf):
        setup_logger(logger, "{}.log".format(cargs.testid))
    else:
        setup_logger(logger)

    ch = []
    try:
        c_handler = conf['content_handler']
    except KeyError:
        comhandler = None
    else:
        for item in c_handler:
            for key, kwargs in item.items():  # should only be one
                if key == 'robobrowser':
                    from aatest.contenthandler import robobrowser
                    ch.append(robobrowser.factory(**kwargs))
        comhandler = ComHandler(ch)

    mako_path = mako.__path__[0] + os.sep
    staticfiles_path = staticfiles.__path__[0] + os.sep

    kwargs = {"base_url": copy.copy(CONF.BASE), 'spconf': spconf,
              "flows": fdef['Flows'], "order": fdef['Order'],
              "desc": fdef['Desc'], 'metadata': mds,
              "profile": conf['profile'], "msg_factory": saml_message_factory,
              "check_factory": get_check, "profile_handler": ProfileHandler,
              "cache": {},
              'map_prof': map_prof, 'make_entity': make_entity,
              'trace_cls': Trace, 'conv_args': {'entcat': collect_ec()},
              'com_handler': comhandler, 'conf': CONF, 'response_cls': Response,
              'template_root': mako_path, 'static': staticfiles_path }

    try:
        kwargs["entity_id"] = CONF.ENTITY_ID
    except KeyError:
        kwargs['disco_srv'] = conf['disco_srv']

    kwargs["insecure"] = CONF.DO_NOT_VALIDATE_TLS

    return cargs, kwargs<|endoftext|>


<commit_before>__config_version__ = 1

GLOBALS = {
    'serializer': '{{major}}.{{minor}}.{{patch}}',
}

FILES = [
    'setup.py',
    'python_vuejs/__init__.py',
]

VERSION = ['major', 'minor', 'patch']

VCS = {
    'name': 'git',
    'commit_message': "Version updated from {{ current_version }} to {{ new_version }}",
    'finish_release': False,
}
<commit_msg>punch configuration<commit_after>__config_version__ = 1

GLOBALS = {
    'serializer': '{{major}}.{{minor}}.{{patch}}',
}

FILES = [
    'setup.py',
    'python_vuejs/__init__.py',
]

VERSION = ['major', 'minor', 'patch']

VCS = {
    'name': 'git',
    'commit_message': "Version updated from {{ current_version }} to {{ new_version }}",
}
<|endoftext|>