Exemple #1
0
def test_add():
    asm = ADD_Operation().resolve()
    print(asm)
    expected = ASM('''
      // MACRO=LOAD_SP
      @SP
      A=M
      // MACRO_END
      // pop y into D
      A=A-1
      D=M
      // pop x into M
      A=A-1
      // do the operation
      M=M+D
      // MACRO=DEC_SP
      @SP
      M=M-1
      // MACRO_END
        ''')
    assert str(asm) == str(expected)

    # make sure it output valid assembly
    assembler = Assembler()
    assembler.assemble(str(asm))
Exemple #2
0
def experiment01():
    """
    Compute the quantity of interest, it's expectation and variance
    """
    #
    # FE Discretization
    #

    # Computational mesh
    mesh = Mesh1D(resolution=(64, ))

    # Element
    element = QuadFE(mesh.dim(), 'DQ0')
    dofhandler = DofHandler(mesh, element)
    dofhandler.distribute_dofs()

    # Linear Functional
    mesh.mark_region('integrate',
                     lambda x: x > 0.75,
                     entity_type='cell',
                     strict_containment=False)
    phi = Basis(dofhandler)
    assembler = Assembler(Form(1, test=phi, flag='integrate'))
    assembler.assemble()
    L = assembler.get_vector()
Exemple #3
0
    def test_assemble_iiform(self):

        mesh = Mesh1D(resolution=(1, ))

        Q1 = QuadFE(1, 'DQ1')
        dofhandler = DofHandler(mesh, Q1)
        dofhandler.distribute_dofs()

        phi = Basis(dofhandler, 'u')

        k = Explicit(lambda x, y: x * y, n_variables=2, dim=1)
        kernel = Kernel(k)

        form = IIForm(kernel, test=phi, trial=phi)

        assembler = Assembler(form, mesh)
        assembler.assemble()
        Ku = Nodal(lambda x: 1 / 3 * x, basis=phi)

        #af = assembler.af[0]['bilinear']
        M = assembler.get_matrix().toarray()

        u = Nodal(lambda x: x, basis=phi)
        u_vec = u.data()
        self.assertTrue(np.allclose(M.dot(u_vec), Ku.data()))
Exemple #4
0
    def test_assemble_ipform(self):
        # =====================================================================
        # Test 7: Assemble Kernel
        # =====================================================================
        mesh = Mesh1D(resolution=(10, ))

        Q1 = QuadFE(1, 'DQ1')
        dofhandler = DofHandler(mesh, Q1)
        dofhandler.distribute_dofs()

        phi = Basis(dofhandler, 'u')

        k = Explicit(lambda x, y: x * y, n_variables=2, dim=1)
        kernel = Kernel(k)
        form = IPForm(kernel, test=phi, trial=phi)

        assembler = Assembler(form, mesh)
        assembler.assemble()

        #af = assembler.af[0]['bilinear']
        M = assembler.get_matrix().toarray()

        u = Nodal(lambda x: x, basis=phi)
        v = Nodal(lambda x: 1 - x, basis=phi)

        u_vec = u.data()
        v_vec = v.data()

        I = v_vec.T.dot(M.dot(u_vec))
        self.assertAlmostEqual(I[0, 0], 1 / 18)
Exemple #5
0
def sensitivity_sample_qoi(exp_q, dofhandler):
    """
    Sample QoI by means of Taylor expansion
    
        J(q+dq) ~= J(q) + dJdq(q)dq
    """
    # Basis
    phi = Basis(dofhandler, 'v')
    phi_x = Basis(dofhandler, 'vx')

    # Define problem
    exp_q_fn = Nodal(data=exp_q, basis=phi)

    primal = [Form(exp_q_fn, test=phi_x, trial=phi_x), Form(1, test=phi)]
    adjoint = [Form(exp_q_fn, test=phi_x, trial=phi_x), Form(0, test=phi)]
    qoi = [Form(exp_q_fn, test=phi_x)]
    problems = [primal, adjoint, qoi]

    # Define assembler
    assembler = Assembler(problems)

    #
    # Dirichlet conditions for primal problem
    #
    assembler.add_dirichlet('left', 0, i_problem=0)
    assembler.add_dirichlet('right', 1, i_problem=0)

    # Dirichlet conditions for adjoint problem
    assembler.add_dirichlet('left', 0, i_problem=1)
    assembler.add_dirichlet('right', -1, i_problem=1)

    # Assemble system
    assembler.assemble()

    # Compute solution and qoi at q (primal)
    u = assembler.solve(i_problem=0)

    # Compute solution of the adjoint problem
    v = assembler.solve(i_problem=1)

    # Evaluate J
    J = u.dot(assembler.get_vector(2))

    #
    # Assemble gradient
    #
    ux_fn = Nodal(data=u, basis=phi_x)
    vx_fn = Nodal(data=v, basis=phi_x)

    k_int = Kernel(f=[exp_q_fn, ux_fn, vx_fn],
                   F=lambda exp_q, ux, vx: exp_q * ux * vx)

    problem = [Form(k_int, test=phi)]

    assembler = Assembler(problem)
    assembler.assemble()
    dJ = -assembler.get_vector()
    return dJ
Exemple #6
0
def sample_qoi(q, dofhandler):
    """
    Sample total energy of output for a given sample of q's 
    """
    # 
    # Set up weak form 
    # 
    
    # Basis 
    phi   = Basis(dofhandler, 'v')
    phi_x = Basis(dofhandler, 'vx')
       
    # Elliptic problem
    problems = [[Form(q, test=phi_x, trial=phi_x), Form(1, test=phi)],
                [Form(1, test=phi, trial=phi)]]
    
    # Assemble
    assembler = Assembler(problems, mesh)
    assembler.assemble()
    
    # System matrices
    A = assembler.af[0]['bilinear'].get_matrix()
    b = assembler.af[0]['linear'].get_matrix()
    M = assembler.af[1]['bilinear'].get_matrix()
    
    # Define linear system
    system = LS(phi)
    system.add_dirichlet_constraint('left',1)
    system.add_dirichlet_constraint('right',0)
    
    n_samples = q.n_samples()
    y_smpl = []
    QoI_smpl = []
    for i in range(n_samples):
        # Sample system 
        if n_samples > 1:
            Ai = A[i]
        else:
            Ai = A
        system.set_matrix(Ai)
        system.set_rhs(b.copy())
        
        # Solve system
        system.solve_system()
        
        # Record solution and qoi
        y = system.get_solution(as_function=False)
        y_smpl.append(y)
        QoI_smpl.append(y.T.dot(M.dot(y)))
    
    # Convert to numpy array    
    y_smpl = np.concatenate(y_smpl,axis=1)
    QoI = np.concatenate(QoI_smpl, axis=1).ravel()
    
    return y_smpl, QoI
    def test02_1d_dirichlet_higher_order(self):
        mesh = Mesh1D()
        for etype in ['Q2', 'Q3']:
            element = QuadFE(1, etype)
            dofhandler = DofHandler(mesh, element)
            dofhandler.distribute_dofs()

            # Basis functions
            ux = Basis(dofhandler, 'ux')
            u = Basis(dofhandler, 'u')

            # Exact solution
            ue = Nodal(f=lambda x: x * (1 - x), basis=u)

            # Define coefficient functions
            one = Constant(1)
            two = Constant(2)

            # Define forms
            a = Form(kernel=Kernel(one), trial=ux, test=ux)
            L = Form(kernel=Kernel(two), test=u)
            problem = [a, L]

            # Assemble problem
            assembler = Assembler(problem, mesh)
            assembler.assemble()

            A = assembler.get_matrix()
            b = assembler.get_vector()

            # Set up linear system
            system = LinearSystem(u, A=A, b=b)

            # Boundary functions
            bnd_left = lambda x: np.abs(x) < 1e-9
            bnd_right = lambda x: np.abs(1 - x) < 1e-9

            # Mark mesh
            mesh.mark_region('left', bnd_left, entity_type='vertex')
            mesh.mark_region('right', bnd_right, entity_type='vertex')

            # Add Dirichlet constraints to system
            system.add_dirichlet_constraint('left', 0)
            system.add_dirichlet_constraint('right', 0)

            # Solve system
            system.solve_system()
            system.resolve_constraints()

            # Compare solution with the exact solution
            ua = system.get_solution(as_function=True)
            self.assertTrue(np.allclose(ua.data(), ue.data()))
Exemple #8
0
    def test01_solve_2d(self):
        """
        Solve a simple 2D problem with no hanging nodes
        """
        mesh = QuadMesh(resolution=(5, 5))

        # Mark dirichlet boundaries
        mesh.mark_region('left',
                         lambda x, dummy: np.abs(x) < 1e-9,
                         entity_type='half_edge')

        mesh.mark_region('right',
                         lambda x, dummy: np.abs(x - 1) < 1e-9,
                         entity_type='half_edge')

        Q1 = QuadFE(mesh.dim(), 'Q1')
        dQ1 = DofHandler(mesh, Q1)
        dQ1.distribute_dofs()

        phi = Basis(dQ1, 'u')
        phi_x = Basis(dQ1, 'ux')
        phi_y = Basis(dQ1, 'uy')

        problem = [
            Form(1, test=phi_x, trial=phi_x),
            Form(1, test=phi_y, trial=phi_y),
            Form(0, test=phi)
        ]

        assembler = Assembler(problem, mesh)
        assembler.add_dirichlet('left', dir_fn=0)
        assembler.add_dirichlet('right', dir_fn=1)
        assembler.assemble()

        # Get matrix dirichlet correction and right hand side
        A = assembler.get_matrix().toarray()
        x0 = assembler.assembled_bnd()
        b = assembler.get_vector()

        ua = np.zeros((phi.n_dofs(), 1))
        int_dofs = assembler.get_dofs('interior')
        ua[int_dofs, 0] = np.linalg.solve(A, b - x0)

        dir_bc = assembler.get_dirichlet()
        dir_vals = np.array([dir_bc[dof] for dof in dir_bc])
        dir_dofs = [dof for dof in dir_bc]
        ua[dir_dofs] = dir_vals

        ue_fn = Nodal(f=lambda x: x[:, 0], basis=phi)
        ue = ue_fn.data()
        self.assertTrue(np.allclose(ue, ua))
        self.assertTrue(np.allclose(x0 + A.dot(ua[int_dofs, 0]), b))
def deobfuscate(codestring):
    # Instructions are stored as a string, we need
    # to convert it to an array of the raw bytes
    insBytes = bytearray(codestring)

    oep = find_oep(insBytes)
    logger.info('Original code entrypoint at {}'.format(oep))

    logger.info('Starting control flow analysis...')
    disasm = Disassembler(insBytes, oep)
    disasm.find_leaders()
    disasm.construct_basic_blocks()
    disasm.build_bb_edges()
    logger.info('Control flow analysis completed.')
    logger.info('Starting simplication of basic blocks...')
    render_graph(disasm.bb_graph, 'before.svg')
    simplifier = Simplifier(disasm.bb_graph)
    simplifier.eliminate_forwarders()
    render_graph(simplifier.bb_graph, 'after_forwarder.svg')
    simplifier.merge_basic_blocks()
    logger.info('Simplification of basic blocks completed.')
    simplified_graph = simplifier.bb_graph
    render_graph(simplified_graph, 'after.svg')
    logger.info('Beginning verification of simplified basic block graph...')

    if not verify_graph(simplified_graph):
        logger.error('Verification failed.')
        raise SystemExit

    logger.info('Verification succeeded.')
    logger.info('Assembling basic blocks...')
    asm = Assembler(simplified_graph)
    codestring = asm.assemble()
    logger.info('Successfully assembled. ')
    return codestring
Exemple #10
0
def compile_pascal(source, dest, is_debug = False, is_interpret = False, out_stream = sys.stdout, output_tokens = False, output_bytecodes = False, lib = ['.'], in_stream = sys.stdin):
  '''
  DID YOU KNOW that compile() is a built in function?
  '''
  set_debug(is_debug)
  debug("Compiling %s into %s" % (source, dest))
  scanner = Scanner(source)
  tokens = scanner.scan()
  if output_tokens:
    write(tokens, source + "_tokenized")
  debug('scanning complete')
  parser = Parser(tokens, source, lib = lib)
  bytecodes, success = parser.parse()
  if output_bytecodes:
    if is_debug:
      write(prettify(bytecodes), source + "_unassembled")
    else:
      write(bytecodes, source + "_unassembled")
  if not success:
    print 'Parsing error'
    return
  debug('parsing complete')
  assembler = Assembler(bytecodes)
  assembled = assembler.assemble()
  if is_debug:
    write(prettify(assembled), dest + '_debug')
  write(assembled, dest)
  debug('assembly complete.' )
  if is_interpret:
    interp = Interpreter(out_stream, in_stream, code = assembled)
    interp.interpret()
  else:
    debug('run program now with `python interpreter.py %s`' % dest)
Exemple #11
0
    def start(self):
        print("Starting MRI_FID_Widget")

        # send 1 as signal to start MRI_FID_Widget
        gsocket.write(struct.pack('<I', 1))

        # enable/disable GUI elements
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)
        self.applyFreqButton.setEnabled(True)
        self.gradOffset_x.setEnabled(True)
        self.gradOffset_y.setEnabled(True)
        self.gradOffset_z.setEnabled(True)
        self.gradOffset_z2.setEnabled(True)
        self.acquireButton.setEnabled(True)
        self.saveShimButton.setEnabled(True)
        self.loadShimButton.setEnabled(True)
        self.zeroShimButton.setEnabled(True)
        self.openFlipangletoolBtn.setEnabled(True)

        # setup global socket for receive data
        gsocket.setReadBufferSize(8 * self.size)
        gsocket.readyRead.connect(self.read_data)

        # send the sequence to the backend
        ass = Assembler()
        seq_byte_array = ass.assemble(self.seq_filename)
        print(len(seq_byte_array))
        gsocket.write(struct.pack('<I', len(seq_byte_array)))
        gsocket.write(seq_byte_array)

        self.load_shim()
        self.idle = False
Exemple #12
0
    def start(self):
        print("Starting MRI_SE_Widget")

        # send 2 as signal to start MRI_SE_Widget
        gsocket.write(struct.pack('<I', 2))

        # enable/disable GUI elements
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)
        self.gradOffset_x.setEnabled(True)
        self.gradOffset_y.setEnabled(True)
        self.gradOffset_z.setEnabled(True)
        self.gradOffset_z2.setEnabled(True)
        self.acquireButton.setEnabled(True)
        self.saveShimButton.setEnabled(True)
        self.loadShimButton.setEnabled(True)
        self.zeroShimButton.setEnabled(True)
        self.zoomCheckBox.setEnabled(True)
        self.peakWindowCheckBox.setEnabled(True)

        self.cycAcqBtn.setEnabled(False)
        self.cyclesValue.setEnabled(False)

        # setup global socket for receive data
        gsocket.readyRead.connect(self.read_data)

        # send the sequence to the backend
        ass = Assembler()
        seq_byte_array = ass.assemble(self.seq_filename)
        print(len(seq_byte_array))
        gsocket.write(struct.pack('<I', len(seq_byte_array)))
        gsocket.write(seq_byte_array)

        self.load_shim()
        self.idle = False
Exemple #13
0
 def test_assemble(self):
     from assembler import Assembler
     assbl = Assembler(sequences, identifiers)
     assbl._find_matching_pairs = MagicMock(return_value=(map_top_bottom,
                                                          map_bottom_top))
     assbl._determine_order = MagicMock(return_value=order)
     self.assertEqual(assbl.assemble(), assembled_seq)
Exemple #14
0
def sampling_error():
    """
    Test the sampling error by comparing the accuracy of the quantities of 
    interest q1 = E[|y|] and q2 = E[y(0.5)]
        
    """
    c = Verbose()
    mesh = Mesh1D(resolution=(1026,))
    mesh.mark_region('left', lambda x:np.abs(x)<1e-10)
    mesh.mark_region('right', lambda x:np.abs(x-1)<1e-10)
    
    element = QuadFE(1,'Q1')
    dofhandler = DofHandler(mesh, element)
    dofhandler.distribute_dofs()
    dofhandler.set_dof_vertices()
    
    phi = Basis(dofhandler,'u')
    phi_x = Basis(dofhandler,'ux')
    
    ns_ref = 10000
    z = get_points(n_samples=ns_ref)
    q = set_diffusion(dofhandler,z)
    
    problems = [[Form(q, test=phi_x, trial=phi_x), Form(1, test=phi)],
                [Form(1, test=phi, trial=phi)]]
    
    c.tic('assembling')
    assembler = Assembler(problems, mesh)
    assembler.assemble()
    c.toc()
    
    A = assembler.af[0]['bilinear'].get_matrix()
    b = assembler.af[0]['linear'].get_matrix()
    M = assembler.af[0]['bilinear'].get_matrix()
    
    system = LS(phi)
    system.add_dirichlet_constraint('left')
    system.add_dirichlet_constraint('right')
    
    c.tic('solving')
    for n in range(ns_ref):
        system.set_matrix(A[n])
        system.set_rhs(b.copy())    
        system.solve_system()
        
    c.toc()    
Exemple #15
0
def touch(f):
    click.echo(click.format_filename(f))
    fasta_parser = FASTAparser(f)
    parsed_sequences = fasta_parser.parse()
    assembler = Assembler(parsed_sequences)
    result = assembler.assemble()
    with open("result_assembled_sequence.txt", "w") as text_file:
        text_file.write(result)
Exemple #16
0
def main(args):
   if len(args) != 2:
      print "USAGE:", args[0], "program.asm"
      print "\tprogram.asm is the source asm file"
      print "\tA hack file will be created from the source file."
      print "\tThe output file will use the same prefix as the source, but"
      print "\tthe extension will be .hack"
      sys.exit(1)

   # the source filename
   source_filename = args[1]

   # create the assembler and assemble the code
   try:
      assembler = Assembler(source_filename)
      assembler.assemble()
   except IOError:
      print "ERROR: Could not open source file or error writing to destination"
Exemple #17
0
 def send_pulse(self, inp_file):
     ''' Sends the pulse sequence to the server '''
     # write a 3 to signal that the button has been pushed
     gsocket.write(struct.pack('<I', 3 << 28))
     ass = Assembler()
     btye_array = ass.assemble(inp_file)
     print("Byte array = {}".format(btye_array))
     print("Length of byte array = {}".format(len(btye_array)))
     gsocket.write(btye_array)
     print("Sent byte array")
Exemple #18
0
def touch(fasta_file, output_file, show):
    click.echo(click.format_filename(fasta_file))
    fasta_parser = FastaParser(fasta_file)
    fragments, fragment_ids = fasta_parser.parse()
    assembler = Assembler(fragments,
                          fragment_ids,
                          print_fragment_id_order=show)
    assembled = assembler.assemble()
    with open(output_file, "w") as text_file:
        text_file.write(assembled)
Exemple #19
0
def sample_state(mesh,dQ,z,mflag,reference=False):
    """
    Compute the sample output corresponding to a given input
    """
    n_samples = z.shape[0]
    q = set_diffusion(dQ,z)
    
    phi = Basis(dQ,'u', mflag)
    phi_x = Basis(dQ, 'ux', mflag)
    
    if reference:
        problems = [[Form(q,test=phi_x,trial=phi_x), Form(1,test=phi)], 
                    [Form(1,test=phi, trial=phi)],
                    [Form(1,test=phi_x, trial=phi_x)]]
    else:
        problems = [[Form(q,test=phi_x,trial=phi_x), Form(1,test=phi)]]
    
    assembler = Assembler(problems, mesh, subforest_flag=mflag)
    assembler.assemble()
    
    A = assembler.af[0]['bilinear'].get_matrix()
    b = assembler.af[0]['linear'].get_matrix()
    if reference:
        M = assembler.af[1]['bilinear'].get_matrix()
        K = assembler.af[2]['bilinear'].get_matrix()
        
    system = LS(phi)
    system.add_dirichlet_constraint('left')
    system.add_dirichlet_constraint('right')
    
    n_dofs = dQ.n_dofs(subforest_flag=mflag)
    y = np.empty((n_dofs,n_samples))
    for n in range(n_samples):
        system.set_matrix(A[n])
        system.set_rhs(b.copy())
        system.solve_system()
        y[:,n] = system.get_solution(as_function=False)[:,0]
    y_fn = Nodal(dofhandler=dQ,subforest_flag=mflag,data=y)
    
    if reference:
        return y_fn, M, K
    else:
        return y_fn
Exemple #20
0
def test02_variance():
    """
    Compute the variance of J(q) for different mesh refinement levels
    and compare with MC estimates. 
    """
    l_max = 8
    for i_res in np.arange(2, l_max):

        # Computational mesh
        mesh = Mesh1D(resolution=(2**i_res, ))

        # Element
        element = QuadFE(mesh.dim(), 'DQ0')
        dofhandler = DofHandler(mesh, element)
        dofhandler.distribute_dofs()

        # Linear Functional
        mesh.mark_region('integrate',
                         lambda x: x >= 0.75,
                         entity_type='cell',
                         strict_containment=False)
        phi = Basis(dofhandler)
        assembler = Assembler(Form(4, test=phi, flag='integrate'))
        assembler.assemble()
        L = assembler.get_vector()

        # Define Gaussian random field
        C = Covariance(dofhandler, name='gaussian', parameters={'l': 0.05})
        C.compute_eig_decomp()

        eta = GaussianField(dofhandler.n_dofs(), K=C)
        eta.update_support()

        n_samples = 100000
        J_paths = L.dot(eta.sample(n_samples=n_samples))
        var_mc = np.var(J_paths)
        lmd, V = C.get_eig_decomp()
        LV = L.dot(V)
        var_an = LV.dot(np.diag(lmd).dot(LV.transpose()))

        print(var_mc, var_an)
Exemple #21
0
class Streamer:
	#Global variables
	fo = open('authentication.txt')
	lines = [str(line.rstrip('\n')) for line in fo]
	consumer_key = lines[0]
	consumer_secret = lines[1]
	access_token = lines[2]
	access_token_secret = lines[3]
	fo.close()

	# OAuth process, using the keys and tokens
	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)

	# Creation of the actual interface, using authentication
	api = tweepy.API(auth)

	def __init__(self):
		self.assembler = Assembler()
		self.wrapper = Wrapper()
	
	def priceupdate(self, base, mid, quote):
		timestamp = datetime.fromtimestamp(time.time())
		rates = self.assembler.assemble(base, mid, quote)
		base_per_mid, mid_per_quote = rates[0], rates[1]
		last_update = self.wrapper.c.execute("SELECT * FROM Prices ORDER BY year DESC, month DESC, day DESC, hour DESC, minute DESC").fetchone()
		new_price = base_per_mid*mid_per_quote
		last_price = last_update[5]*last_update[6]
		delta = new_price/last_price - 1
		self.wrapper.update_price_db(timestamp, base_per_mid, mid_per_quote)
		if delta >= 0:
			return '[%s CST]: The average #dogecoin price is now $%.6f, +%.1f%% growth wow (%.2f bits)' \
			% (timestamp.strftime('%m-%d %H:%M'), new_price, delta*100, base_per_mid*(1000000))
		else:
			return '[%s CST]: The average #dogecoin price is now $%.6f, %.1f%% decline (%.2f bits)' \
			% (timestamp.strftime('%m-%d %H:%M'), new_price, delta*100, base_per_mid*(1000000))	
		
		#return '[%s CST]: The average #dogecoin price is now $%.6f (%.2f bits)' \
		#% (timestamp.strftime('%m-%d %H:%M'), new_price, base_per_mid*(1000000))
		#return '[%s CST]: The average dogecoin price is now %.2f bits ($%.6f).\n$1 = Ð%.2f\n1BTC = Ð%d' % (datetime.fromtimestamp(time.time()).strftime('%m-%d %H:%M:%S'), dogebtcprice, dogeusdprice, 1/dogeusdprice, dogebtcprice*100000)

	#Continuous price stream
	def stream(self):
		print 'Initiating Dogecoin Price Stream --------------------------'
		while True:
			try:
				tweet = self.priceupdate('DOGE', 'BTC', 'USD')
				self.api.update_status(tweet)
				print tweet
				print 'Tweeted successfully'
			except Exception, e:
				print str(e)
			time.sleep(3570)
Exemple #22
0
    def test01_solve_1d(self):
        """
        Test solving 1D systems
        """
        mesh = Mesh1D(resolution=(20, ))
        mesh.mark_region('left', lambda x: np.abs(x) < 1e-9)
        mesh.mark_region('right', lambda x: np.abs(x - 1) < 1e-9)

        Q1 = QuadFE(1, 'Q1')
        dQ1 = DofHandler(mesh, Q1)
        dQ1.distribute_dofs()

        phi = Basis(dQ1, 'u')
        phi_x = Basis(dQ1, 'ux')

        problem = [Form(1, test=phi_x, trial=phi_x), Form(0, test=phi)]
        assembler = Assembler(problem, mesh)
        assembler.add_dirichlet('left', dir_fn=0)
        assembler.add_dirichlet('right', dir_fn=1)
        assembler.assemble()

        # Get matrix dirichlet correction and right hand side
        A = assembler.get_matrix().toarray()
        x0 = assembler.assembled_bnd()
        b = assembler.get_vector()

        ua = np.zeros((phi.n_dofs(), 1))
        int_dofs = assembler.get_dofs('interior')
        ua[int_dofs, 0] = np.linalg.solve(A, b - x0)

        dir_bc = assembler.get_dirichlet()
        dir_vals = np.array([dir_bc[dof] for dof in dir_bc])
        dir_dofs = [dof for dof in dir_bc]
        ua[dir_dofs] = dir_vals

        ue_fn = Nodal(f=lambda x: x[:, 0], basis=phi)
        ue = ue_fn.data()
        self.assertTrue(np.allclose(ue, ua))
        self.assertTrue(np.allclose(x0 + A.dot(ua[int_dofs, 0]), b))
Exemple #23
0
 def upload_seq(self):
     ''' Takes an input text file, compiles it to machine code '''
     dialog = QFileDialog()  # open a Dialog box to take the file
     fname = dialog.getOpenFileName(None, "Import Pulse Sequence", "",
                                    "Text files (*.txt)")
     # returns a tuple, fname[0] is filename(including path), fname[1] is file type
     inp_file = fname[0]
     print("Uploading sequence to server")
     try:
         ass = Assembler()
         self.upload_seq_byte_array = ass.assemble(inp_file)
         self.uploadSeq.setText(inp_file)
     except IOError as e:
         print("Error: required txt file doesn't exist")
         return
     print("Uploaded successfully to server")
Exemple #24
0
        self.pc = 2

        while self.pc < end and program[self.pc] != opset.HALT:
            instruction = program[self.pc]
            self.pc += 1
            if opset.nullary(instruction):
                self.debug("%02d %s", self.pc, opset.byte2name[instruction])
                self.nullary[instruction]()
            elif opset.unary(instruction):
                argument = program[self.pc]
                self.pc += 1
                self.debug("%02d %s %s", self.pc, opset.byte2name[instruction], argument)
                self.unary[instruction - opset.LIT](argument)

if __name__ == "__main__":
    from sys import argv
    from assembler import Assembler
    from argparser import parseArgs

    # Grab command line arguments
    args = parseArgs()

    try:
        asm = Assembler(args.debug)
        vm = VirtualMachine(args.debug)
        vm.run(asm.assemble(open(args.filename)))
    except Exception as e:
        print(e)

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Exemple #25
0
 def set_pulse(self, inp_file):
     ''' Sends the default pulse sequence to the server '''
     ass = Assembler()
     self.default_seq_byte_array = ass.assemble(inp_file)
     gsocket.write(self.default_seq_byte_array)
     return
Exemple #26
0
def main():
    totalTime = 0.0
    startTime = time.time()

    parser = OptionParser("usage: %prog [options] src_file [src_file...]")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose output.")
    parser.add_option("-l", "--log", dest="logLevel", default=0, help="Print detailed log information.")
    parser.add_option("-t", "--test", action="store_true", dest="test", default=False, help="Run assembler test code.")
    parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Turn on assembler debugging code.")
    parser.add_option("-s", "--syntax-only", action="store_true", dest="syntaxOnly", default=False, help="Exit after checking syntax.")
    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.error("At least one source file must be supplied!")
        sys.exit(1)

    sources = []
    for arg in args:
        sources.append(arg)
        if not os.path.isfile(arg):
            parser.error("File \"%s\" does not exist" % arg)
            sys.exit(1)

    buildname = os.path.basename(os.getcwd())
    
    firstfile = args[0]
    firstfilename = args[0].split('.')[0]
    
    listfile = open(firstfile + ".lst", 'w')
    symtabfile = open(firstfile + ".symtab", 'w')
    binfile = open(firstfile + ".bin", 'wb')
    logfile = None
    if options.logLevel > 0:
        logfile = open(firstfilename + ".log", 'w')

    context = Context(Architecture.AGC4_B2, listfile, binfile, options, int(options.logLevel), logfile)
    assembler = Assembler(context)
    context.assembler = assembler

    if options.debug:
        print "Build:", buildname 
        endTime = time.time()
        delta = endTime - startTime
        totalTime += delta
        print "Initialisation: %3.2f seconds" % delta

    assembler.info("Simple AGC Assembler, v0.1", source=False)
    assembler.info("", source=False)

    startTime = time.time()
    for arg in args:
        try:
            assembler.assemble(arg)
        except:
            print >>sys.stderr
            print >>sys.stderr, "EXCEPTION:"
            traceback.print_exc(file=sys.stderr)
            print >>sys.stderr, "Context:"
            print >>sys.stderr, context
            raise
    if options.debug:
        endTime = time.time()
        delta = endTime - startTime
        totalTime += delta
        print "Pass 1: %3.2f seconds" % delta

    context.saveCurrentBank()

    if options.syntaxOnly == False and context.errors == 0:
        assembler.info("Resolving symbols...", source=False)
        startTime = time.time()
        try:
            assembler.resolve()
        except:
            assembler.log(1, "EXCEPTION:\n%s" % context)
            raise
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta

    for record in assembler.context.records:
        record.printMessages()

    assembler.info("Writing listing...", source=False)
    startTime = time.time()
    print >>listfile
    print >>listfile, "Listing"
    print >>listfile, "-------"
    for record in assembler.context.records:
        print >>listfile, record
    if options.debug:
        endTime = time.time()
        delta = endTime - startTime
        totalTime += delta
        print "Write listing: %3.2f seconds" % delta

    if not options.syntaxOnly:
        assembler.info("Writing symbol table listing...", source=False)
        startTime = time.time()
        print >>listfile
        print >>listfile, "Symbol Table"
        print >>listfile, "------------"
        assembler.context.symtab.printTable(listfile)
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta
            print "Write symbol table listing: %3.2f seconds" % delta

    if not options.syntaxOnly and context.errors == 0:
        assembler.info("Writing symbol table...", source=False)
        startTime = time.time()
        assembler.context.symtab.write(symtabfile)
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta
            print "Write symbol table: %3.2f seconds" % delta

    if context.errors == 1:
        msg = "1 error, "
    else:
        msg = "%d errors, " % (context.errors)
    if context.warnings == 1:
        msg += "1 warning, "
    else:
        msg += "%d warnings, " % (context.warnings)
    assembler.info(msg, source=False)
    print msg

    if not options.syntaxOnly:
        if options.test:
            startTime = time.time()

            # FIXME: Temporary hack
            # Check generated symbols against the symtab generated by yaYUL.

            assembler.info("Checking symbol table against yaYUL version...", source=False)
            from artemis072_symbols import ARTEMIS_SYMBOLS
            from memory import MemoryType

            nsyms = assembler.context.symtab.getNumSymbols()
            check_nsyms = len(ARTEMIS_SYMBOLS.keys())
            assembler.info("Number of symbols: yaYUL=%d pyagc=%d" % (check_nsyms, nsyms), source=False)

            my_syms = []
            other_syms = []
            common_syms = []

            for sym in assembler.context.symtab.keys():
                if sym in ARTEMIS_SYMBOLS.keys():
                    common_syms.append(sym)
                else:
                    if sym != "FIXED":
                        my_syms.append(sym)

            for sym in ARTEMIS_SYMBOLS.keys():
                if sym not in assembler.context.symtab.keys():
                    if not sym.startswith('$') and sym != "'":
                        other_syms.append(sym)

            if len(my_syms) != 0 or len(other_syms) != 0:
                assembler.error("incorrect number of symbols, expected %d, got %d" % (check_nsyms, nsyms), source=False)

            if len(my_syms) > 0:
                assembler.error("symbols defined that should not be defined: %s" % my_syms, source=False)

            if len(other_syms) > 0:
                assembler.error("symbols not defined that should be defined: %s" % other_syms, source=False)

            errcount = 0
            bad_syms = {}

            for sym in common_syms:
                entry = assembler.context.symtab.lookup(sym)
                if entry == None:
                    assembler.error("symbol %-8s not defined" % entry, source=False)
                pa = entry.value
                aval = ARTEMIS_SYMBOLS[sym]
                if ',' in aval:
                    bank = aval.split(',')[0]
                    type = MemoryType.FIXED
                    if bank.startswith('E'):
                        bank = bank[1:]
                        type = MemoryType.ERASABLE
                    bank = int(bank, 8)
                    offset = int(aval.split(',')[1], 8)
                    check_pa = context.memmap.segmentedToPseudo(type, bank, offset, absolute=True)
                else:
                    check_pa = int(aval, 8)
                if pa != check_pa:
                    errcount += 1
                    bad_syms[pa] = (sym, check_pa)

            if errcount > 0:
                bad_addrs = bad_syms.keys()
                bad_addrs.sort()
                for pa in bad_addrs:
                    sym = bad_syms[pa][0]
                    check_pa = bad_syms[pa][1]
                    assembler.error("symbol %-8s defined as %06o %s, expected %06o %s" % (sym, pa, context.memmap.pseudoToSegmentedString(pa), check_pa, context.memmap.pseudoToSegmentedString(check_pa)), source=False)
                assembler.error("%d/%d symbols incorrectly defined" % (errcount, len(common_syms)), source=False)

            if options.debug:
                endTime = time.time()
                delta = endTime - startTime
                totalTime += delta
                print "Symbol checking: %3.2f seconds" % delta

            # FIXME: End of temporary hack

    if not options.syntaxOnly and context.errors == 0:
        assembler.info("Writing binary output...", source=False)
        startTime = time.time()
        ocode = ObjectCode(context)
        ocode.generateBuggers()
        ocode.write(binfile)
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta
            print "Binary generation: %3.2f seconds" % delta

        assembler.info("Writing rope usage...", source=False)
        startTime = time.time()
        print >>listfile
        print >>listfile
        print >>listfile, "Bank Usage"
        print >>listfile, "----------"
        print >>listfile
        ocode.writeUsage(listfile)
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta
            print "Rope usage: %3.2f seconds" % delta

        assembler.info("Writing rope image listing...", source=False)
        startTime = time.time()
        print >>listfile
        print >>listfile
        print >>listfile, "Rope Image Listing"
        print >>listfile, "------------------"
        print >>listfile
        ocode.writeListing(listfile)
        if options.debug:
            endTime = time.time()
            delta = endTime - startTime
            totalTime += delta
            print "Rope image listing: %3.2f seconds" % delta

    assembler.info("Done.", source=False)
    listfile.close()
    symtabfile.close()
    binfile.close()
    if logfile:
        logfile.close()

    if options.debug:
        print "Total time: %3.2f seconds" % totalTime

    print "Done."
Exemple #27
0
def main():
	args = parser.parse_args()
	a = Assembler(args.debug)
	a.assemble(args.asm_path)
Exemple #28
0
class Converter:
	#Global variables
	fo = open('authentication.txt')
	lines = [str(line.rstrip('\n')) for line in fo]
	consumer_key = lines[0]
	consumer_secret = lines[1]
	access_token = lines[2]
	access_token_secret = lines[3]
	fo.close()

	# OAuth process, using the keys and tokens
	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)

	# Creation of the actual interface, using authentication
	api = tweepy.API(auth)

	#All 168 foreign currencies able to be converted via bitcoinaverage
	#Bypasses openexchangerates.org
	currency_codes = ['AED' , 'AFN' , 'ALL' , 'AMD' , 'ANG' , 'AOA' , 'ARS' , 'AUD' , 'AWG' ,\
				      'AZN' , 'BAM' , 'BBD' , 'BDT' , 'BGN' , 'BHD' , 'BIF' , 'BMD' , 'BND' ,\
				      'BOB' , 'BRL' , 'BSD' , 'BTC' , 'BTN' , 'BWP' , 'BYR' , 'BZD' , 'CAD' ,\
				      'CDF' , 'CHF' , 'CLF' , 'CLP' , 'CNY' , 'COP' , 'CRC' , 'CUP' , 'CVE' ,\
				      'CZK' , 'DJF' , 'DKK' , 'DOP' , 'DZD' , 'EEK' , 'EGP' , 'ERN' , 'ETB' ,\
				      'EUR' , 'FJD' , 'FKP' , 'GBP' , 'GEL' , 'GGP' , 'GHS' , 'GIP' , 'GMD' ,\
				      'GNF' , 'GTQ' , 'GYD' , 'HKD' , 'HNL' , 'HRK' , 'HTG' , 'HUF' , 'IDR' ,\
				      'ILS' , 'IMP' , 'INR' , 'IQD' , 'IRR' , 'ISK' , 'JEP' , 'JMD' , 'JOD' ,\
				      'JPY' , 'KES' , 'KGS' , 'KHR' , 'KMF' , 'KPW' , 'KRW' , 'KWD' , 'KYD' ,\
				      'KZT' , 'LAK' , 'LBP' , 'LKR' , 'LRD' , 'LSL' , 'LTL' , 'LVL' , 'LYD' ,\
				      'MAD' , 'MDL' , 'MGA' , 'MKD' , 'MMK' , 'MNT' , 'MOP' , 'MRO' , 'MTL' ,\
				      'MUR' , 'MVR' , 'MWK' , 'MXN' , 'MYR' , 'MZN' , 'NAD' , 'NGN' , 'NIO' ,\
				      'NOK' , 'NPR' , 'NZD' , 'OMR' , 'PAB' , 'PEN' , 'PGK' , 'PHP' , 'PKR' ,\
				      'PLN' , 'PYG' , 'QAR' , 'RON' , 'RSD' , 'RUB' , 'RWF' , 'SAR' , 'SBD' ,\
				      'SCR' , 'SDG' , 'SEK' , 'SGD' , 'SHP' , 'SLL' , 'SOS' , 'SRD' , 'STD' ,\
				      'SVC' , 'SYP' , 'SZL' , 'THB' , 'TJS' , 'TMT' , 'TND' , 'TOP' , 'TRY' ,\
				      'TTD' , 'TWD' , 'TZS' , 'UAH' , 'UGX' , 'USD' , 'UYU' , 'UZS' , 'VEF' ,\
				      'VND' , 'VUV' , 'WST' , 'XAF' , 'XAG' , 'XAU' , 'XCD' , 'XDR' , 'XOF' ,\
				      'XPF' , 'YER' , 'ZAR' , 'ZMK' , 'ZMW' , 'ZWL']

	#Expanding to other cryptocurrencies in v1.7
	cryptocurrency_codes = []

	def __init__(self):
		self.assembler = Assembler()
		self.wrapper = Wrapper()

	def populate_db(self):
		for mention in self.api.mentions_timeline():
			self.wrapper.update_mentions_db(mention.user.screen_name, mention.id, mention.text)
	
	#Return True if mention is a duplicate, False otherwise
	def no_duplicates(self, mention):
		command = "SELECT * FROM Mentions WHERE id = %d" % (mention.id)
		duplicates = self.wrapper.c.execute(command).fetchall()
		if len(duplicates) == 0:
			return True
		return False

	def insert_into_db(self, mention):
		self.wrapper.update_mentions_db(mention.user.screen_name, mention.id, mention.text)

	def convert(self, mention):
		if self.no_duplicates(mention):
			user = mention.user.screen_name
			#Textual trigger
			if '@dogepricebot convert' in mention.text.lower():
				print "Found conversion request"
				print user+" : "+mention.text
				words = mention.text.lower().split(" ")
				command_start = words.index('@dogepricebot')
				amount, base, quote = float(words[command_start+2]), words[command_start+3], words[command_start+5]
				if base.lower() == "dogecoin" or base.lower() == "doge":
					rates = self.assembler.assemble(quote.upper())
					rate = rates[0]*rates[1]
					tweet = '@%s wow such convert: %.1f #dogecoin = %.2f %s' % (user, amount, amount*rate, quote.upper())
					print tweet
					self.api.update_status(tweet)
				else:
					rates = self.assembler.assemble(base.upper())
					rate = rates[0]*rates[1]
					tweet = '@%s wow such convert: %.1f %s = %.2f #dogecoin' % (user, amount, base.upper(), amount/rate)
					print tweet
					self.api.update_status(tweet)
			self.insert_into_db(mention)
		else:
			print "duplicate"

	#Continuous price stream
	def listen(self):
		print 'Listening for @dogepricebot convert requests --------------------------'
		while True:
			for mention in self.api.mentions_timeline(count=10):
				try:
					self.convert(mention)
				except Exception, e:
					print "In exception"
					print str(e)
			time.sleep(300)
Exemple #29
0
def reference_qoi(f, tht, basis, region, n=1000000, verbose=True):
    """
    Parameters
    ----------
    f : lambda function,
        Function of θ to be integrated.
        
    tht : GaussianField, 
        Random field θ defined on mesh in terms of its mean and covariance
        
    basis : Basis, 
        Basis function defining the nodal interpolant. It incorporates the
        mesh, the dofhandler, and the derivative.
        
    region : meshflag, 
        Flag indicating the region of integration
    
    n : int, default=1000000 
        Sample size
    
    Returns
    -------
    Q_ref : double, 
        Reference quantity of interest
        
    err : double, 
        Expected RMSE given by var(Q)/n. 
    """

    #
    # Assemble integral
    #
    batch_size = 100000
    n_batches = n // batch_size + (0 if (n % batch_size) == 0 else 1)

    if verbose:
        print('Computing Reference Quantity of Interest')
        print('========================================')
        print('Sample size: ', n)
        print('Batch size: ', batch_size)
        print('Number of batches: ', n_batches)

    Q_smpl = np.empty(n)
    for k in range(n_batches):

        # Determine sample sizes for each batch
        if k < n_batches - 1:
            n_sample = batch_size
        else:
            # Last sample may be smaller than batch_size
            n_sample = n - k * batch_size

        if verbose:
            print(' - Batch Number ', k)
            print(' - Sample Size: ', n_sample)
            print(' - Sampling random field')

        # Sample from field
        tht_smpl = tht.sample(n_sample)

        # Define kernel
        tht_n = Nodal(data=tht_smpl, basis=basis)
        kf = Kernel(tht_n, F=f)

        # Define forms
        if k == 0:
            problems = [[Form(kernel=kf, flag=region)], [Form(flag=region)]]
        else:
            problems = [Form(kernel=kf, flag=region)]

        if verbose:
            print(' - Assembling.')

        # Compute the integral
        assembler = Assembler(problems, basis.mesh())
        assembler.assemble()

        #
        # Compute statistic
        #

        # Get samples
        if k == 0:
            dx = assembler.get_scalar(i_problem=1)

        if verbose:
            print(' - Updating samples \n')

        batch_sample = assembler.get_scalar(i_problem=0, i_sample=None)
        Q_smpl[k * batch_size:k * batch_size + n_sample] = batch_sample / dx

    # Compute mean and MSE
    Q_ref = np.mean(Q_smpl)
    err = np.var(Q_smpl) / n

    # Return reference
    return Q_ref, err
Exemple #30
0
def plot_heuristics(f, tht, basis, region, condition):
    """
    Parameters
    ----------
    f : lambda, 
        Function of θ to be integrated.
        
    n : int, 
        Sample size for Monte Carlo sample
    
    """
    tht.update_support()
    #
    # Plot samples of the random field
    #
    n = 10000
    tht_sample = tht.sample(n)
    tht_fn = Nodal(data=tht_sample, basis=basis)

    #
    # Compute the quantity of interest
    #

    # Define the kernel
    kf = Kernel(tht_fn, F=f)

    # Assemble over the mesh
    problems = [[Form(kernel=kf, flag=region)], [Form(flag=region)]]
    assembler = Assembler(problems, basis.mesh())
    assembler.assemble()

    # Extract sample
    dx = assembler.get_scalar(i_problem=1)
    q_sample = assembler.get_scalar(i_sample=None) / dx

    #
    # Compute correlation coefficients of q with spatial data
    #

    plot = Plot(quickview=False)
    fig, ax = plt.subplots()
    plt_args = {'linewidth': 0.5, 'color': 'k'}
    ax = plot.line(tht_fn,
                   axis=ax,
                   i_sample=list(range(100)),
                   plot_kwargs=plt_args)
    fig.savefig('ex01_sample_paths.eps')

    fig, ax = plt.subplots()
    ftht = Nodal(data=f(tht_sample), basis=basis)
    ax = plot.line(ftht,
                   axis=ax,
                   i_sample=list(range(100)),
                   plot_kwargs=plt_args)
    fig.savefig('ex01_integrand.eps')

    fig, ax = plt.subplots(1, 1)
    plt.hist(q_sample, bins=50, density=True)
    ax.set_title(r'Histogram $Q(\theta)$')
    fig.savefig('ex01_histogram.eps')
    plt.close('all')

    dh = basis.dofhandler()
    n_dofs = dh.n_dofs()

    # Extract the region on which we condition
    cnd_dofs = dh.get_region_dofs(entity_flag=condition)
    I = np.eye(n_dofs)
    I = I[cnd_dofs, :]

    # Measured tht
    tht_msr = tht_sample[cnd_dofs, 0][:, None]

    n_cnd = 30
    cnd_tht = tht.condition(I, tht_msr, n_samples=n_cnd)

    #cnd_tht_data = np.array([tht_sample[:,0] for dummy in range(n_cnd)])
    #cnd_tht_data[cnd_dofs,:] = cnd_tht

    cnd_tht_fn = Nodal(data=f(cnd_tht), basis=basis)
    fig, ax = plt.subplots()
    ax = plot.line(cnd_tht_fn,
                   axis=ax,
                   i_sample=np.arange(n_cnd),
                   plot_kwargs=plt_args)
    fig.tight_layout()
    plt.show()
    def test09_1d_inverse(self):
        """
        Compute the inverse of a matrix and apply it to a vector/matrix.
        """
        #
        # Mesh
        #
        mesh = Mesh1D(resolution=(1, ))
        mesh.mark_region('left', lambda x: np.abs(x) < 1e-9, on_boundary=True)
        mesh.mark_region('right',
                         lambda x: np.abs(1 - x) < 1e-9,
                         on_boundary=True)

        #
        # Elements
        #
        Q3 = QuadFE(1, 'Q3')
        dofhandler = DofHandler(mesh, Q3)
        dofhandler.distribute_dofs()

        #
        # Basis
        #
        u = Basis(dofhandler, 'u')
        ux = Basis(dofhandler, 'ux')

        #
        # Define sampled right hand side and exact solution
        #
        xv = dofhandler.get_dof_vertices()
        n_points = dofhandler.n_dofs()

        n_samples = 6
        a = np.arange(n_samples)

        ffn = lambda x, a: a * x
        ufn = lambda x, a: a / 6 * (x - x**3) + x
        fdata = np.zeros((n_points, n_samples))
        udata = np.zeros((n_points, n_samples))
        for i in range(n_samples):
            fdata[:, i] = ffn(xv, a[i]).ravel()
            udata[:, i] = ufn(xv, a[i]).ravel()

        # Define sampled function
        fn = Nodal(data=fdata, basis=u)
        ue = Nodal(data=udata, basis=u)

        #
        # Forms
        #
        one = Constant(1)
        a = Form(Kernel(one), test=ux, trial=ux)
        L = Form(Kernel(fn), test=u)
        problem = [[a], [L]]

        #
        # Assembler
        #
        assembler = Assembler(problem, mesh)
        assembler.assemble()

        A = assembler.get_matrix()
        b = assembler.get_vector(i_problem=1)

        #
        # Linear System
        #
        system = LinearSystem(u, A=A)

        # Set constraints
        system.add_dirichlet_constraint('left', 0)
        system.add_dirichlet_constraint('right', 1)
        system.solve_system(b)

        # Extract finite element solution
        ua = system.get_solution(as_function=True)

        system2 = LinearSystem(u, A=A, b=b)

        # Set constraints
        system2.add_dirichlet_constraint('left', 0)
        system2.add_dirichlet_constraint('right', 1)
        system2.solve_system()
        u2 = system2.get_solution(as_function=True)

        # Check that the solution is close
        self.assertTrue(np.allclose(ue.data()[:, 0], ua.data()[:, 0]))
        self.assertTrue(np.allclose(ue.data()[:, [0]], u2.data()))
    def test08_1d_sampled_rhs(self):
        #
        # Mesh
        #
        mesh = Mesh1D(resolution=(1, ))
        mesh.mark_region('left', lambda x: np.abs(x) < 1e-9, on_boundary=True)
        mesh.mark_region('right',
                         lambda x: np.abs(1 - x) < 1e-9,
                         on_boundary=True)

        #
        # Elements
        #
        Q3 = QuadFE(1, 'Q3')
        dofhandler = DofHandler(mesh, Q3)
        dofhandler.distribute_dofs()

        #
        # Basis
        #
        v = Basis(dofhandler, 'u')
        vx = Basis(dofhandler, 'ux')

        #
        # Define sampled right hand side and exact solution
        #
        xv = dofhandler.get_dof_vertices()
        n_points = dofhandler.n_dofs()

        n_samples = 6
        a = np.arange(n_samples)

        f = lambda x, a: a * x
        u = lambda x, a: a / 6 * (x - x**3) + x
        fdata = np.zeros((n_points, n_samples))
        udata = np.zeros((n_points, n_samples))
        for i in range(n_samples):
            fdata[:, i] = f(xv, a[i]).ravel()
            udata[:, i] = u(xv, a[i]).ravel()

        # Define sampled function
        fn = Nodal(data=fdata, basis=v)
        ue = Nodal(data=udata, basis=v)

        #
        # Forms
        #
        one = Constant(1)
        a = Form(Kernel(one), test=vx, trial=vx)
        L = Form(Kernel(fn), test=v)
        problem = [a, L]

        #
        # Assembler
        #
        assembler = Assembler(problem, mesh)
        assembler.assemble()
        A = assembler.get_matrix()
        b = assembler.get_vector()
        #
        # Linear System
        #
        system = LinearSystem(v, A=A, b=b)

        # Set constraints
        system.add_dirichlet_constraint('left', 0)
        system.add_dirichlet_constraint('right', 1)
        #system.set_constraint_relation()
        #system.incorporate_constraints()

        # Solve and resolve constraints
        system.solve_system()
        #system.resolve_constraints()

        # Extract finite element solution
        ua = system.get_solution(as_function=True)

        # Check that the solution is close
        print(ue.data()[:, [0]])
        print(ua.data())
        self.assertTrue(np.allclose(ue.data()[:, [0]], ua.data()))
    def test05_2d_dirichlet(self):
        """
        Two dimensional Dirichlet problem with hanging nodes
        """
        #
        # Define mesh
        #
        mesh = QuadMesh(resolution=(1, 2))
        mesh.cells.get_child(1).mark(1)
        mesh.cells.refine(refinement_flag=1)
        mesh.cells.refine()

        #
        # Mark left and right boundaries
        #
        bm_left = lambda x, dummy: np.abs(x) < 1e-9
        bm_right = lambda x, dummy: np.abs(1 - x) < 1e-9
        mesh.mark_region('left', bm_left, entity_type='half_edge')
        mesh.mark_region('right', bm_right, entity_type='half_edge')

        for etype in ['Q1', 'Q2', 'Q3']:
            #
            # Element
            #
            element = QuadFE(2, etype)
            dofhandler = DofHandler(mesh, element)
            dofhandler.distribute_dofs()

            #
            # Basis
            #
            u = Basis(dofhandler, 'u')
            ux = Basis(dofhandler, 'ux')
            uy = Basis(dofhandler, 'uy')

            #
            # Construct forms
            #
            ue = Nodal(f=lambda x: x[:, 0], basis=u)
            ax = Form(kernel=Kernel(Constant(1)), trial=ux, test=ux)
            ay = Form(kernel=Kernel(Constant(1)), trial=uy, test=uy)
            L = Form(kernel=Kernel(Constant(0)), test=u)
            problem = [ax, ay, L]

            #
            # Assemble
            #
            assembler = Assembler(problem, mesh)
            assembler.assemble()

            #
            # Get system matrices
            #
            A = assembler.get_matrix()
            b = assembler.get_vector()

            #
            # Linear System
            #
            system = LinearSystem(u, A=A, b=b)

            #
            # Constraints
            #
            # Add dirichlet conditions
            system.add_dirichlet_constraint('left', ue)
            system.add_dirichlet_constraint('right', ue)

            #
            # Solve
            #
            system.solve_system()
            #system.resolve_constraints()

            #
            # Check solution
            #
            ua = system.get_solution(as_function=True)
            self.assertTrue(np.allclose(ua.data(), ue.data()))
Exemple #34
0
class data(QObject):

    # Init signal thats emitted when readout is processed
    readout_finished = pyqtSignal()
    t1_finished = pyqtSignal()
    t2_finished = pyqtSignal()
    uploaded = pyqtSignal(bool)

    def __init__(self):
        super(data, self).__init__()
        self.initVariables()

        # Read sequence files
        self.seq_fid = 'sequence/FID.txt'
        self.seq_se = 'sequence/SE_te.txt'
        self.seq_ir = 'sequence/IR_ti.txt'
        self.seq_sir = 'sequence/SIR_ti.txt'
        self.seq_2dSE = 'sequence/img/2DSE.txt'

        # Define Gradients
        self.GR_x = 0
        self.GR_y = 1
        self.GR_z = 2
        self.GR_z2 = 3
#_______________________________________________________________________________
#   Establish host connection and disconnection

    def conn_client(self, ip):

        socket.connectToHost(ip, 1001)
        socket.waitForConnected(1000)

        if socket.state() == connected:
            print("Connection to server esteblished.")
        elif socket.state() == unconnected:
            print("Conncection to server failed.")
            return False
        else:
            print("TCP socket in state : ", socket.state())
            return socket.state()

        self.set_at(params.at)
        self.set_freq(params.freq)

        return True

    def disconn_client(self):
        try:
            socket.disconnectFromHost()
        except:
            pass
        if socket.state() == unconnected:
            print("Disconnected from server.")
        else:
            print("Connection to server still established.")

#_______________________________________________________________________________
#   Functions for initialization of variables

    def initVariables(self):
        # Flags
        self.ir_flag = False
        self.se_flag = False
        self.fid_flag = False

        # Variables
        self.size = 50000  # total data received (defined by the server code)
        self.buffer = bytearray(8 * self.size)
        self.data = np.frombuffer(self.buffer, np.complex64)

        # Variables
        #self.time = 20
        self.freq_range = 250000
#_______________________________________________________________________________
#   Functions for Setting up sequence

# Function to set default FID sequence

    def set_FID(
        self
    ):  # Function to init and set FID -- only acquire call is necessary afterwards

        self.assembler = Assembler()
        byte_array = self.assembler.assemble(self.seq_fid)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        socket.setReadBufferSize(8 * self.size)
        self.ir_flag = False
        self.se_flag = False
        self.fid_flag = True
        print("\nFID sequence uploaded.")

    # Function to set default FID sequence
    def set_2dSE(
        self
    ):  # Function to init and set FID -- only acquire call is necessary afterwards

        self.assembler = Assembler()
        byte_array = self.assembler.assemble(self.seq_2dSE)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        socket.setReadBufferSize(8 * self.size)
        print("\n2D SE sequence uploaded.")

    # Function to set default SE sequence
    def set_SE(self,
               TE=10):  # Function to modify SE -- call whenever acquiring a SE

        # Change TE in sequence and push to server
        params.te = TE
        self.change_TE(params.te)  #, REC)

        self.assembler = Assembler()
        byte_array = self.assembler.assemble(self.seq_se)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten(): break

        socket.setReadBufferSize(8 * self.size)
        self.ir_flag = False
        self.se_flag = True
        self.fid_flag = False
        print("\nSE sequence uploaded with TE = ", TE, " ms.")

    # Function to change TE in sequence
    def change_TE(
        self, TE
    ):  # Function for changing TE value in sequence -- called inside set_SE
        # Open sequence and read lines
        f = open(self.seq_se, 'r+')
        lines = f.readlines()
        if TE < 2: TE = 2
        # Modify TE time in the 8th last line
        lines[-10] = 'PR 3, ' + str(int(TE / 2 * 1000 - 112)) + '\t// wait&r\n'
        lines[-6] = 'PR 3, ' + str(int(TE / 2 * 1000 - 975)) + '\t// wait&r\n'
        # Close and write/save modified sequence
        f.close()
        with open(self.seq_se, "w") as out_file:
            for line in lines:
                out_file.write(line)

    # Function to set default IR sequence
    def set_IR(
        self,
        TI=15
    ):  #, REC=1000): # Function to modify SE -- call whenever acquiring a SE

        params.ti = TI
        self.change_IR(params.ti, self.seq_ir)  #, REC)

        self.assembler = Assembler()
        byte_array = self.assembler.assemble(self.seq_ir)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten(): break

        socket.setReadBufferSize(8 * self.size)
        self.ir_flag = True
        self.se_flag = False
        self.fid_flag = False
        print("\nIR sequence uploaded with TI = ", TI,
              " ms.")  #" and REC = ", REC, " ms.")

    # Function to change TI in IR sequence
    def change_IR(self, TI, seq):
        f = open(seq, 'r+')  # Open sequence and read lines
        lines = f.readlines()
        # Modify TI time in the 8th last line
        lines[-14] = 'PR 3, ' + str(int(TI * 1000 - 198)) + '\t// wait&r\n'
        f.close()  # Close and write/save modified sequence
        with open(seq, "w") as out_file:
            for line in lines:
                out_file.write(line)

    # Function to set default SIR sequence
    def set_SIR(self, TI=15):

        params.ti = TI
        #self.change_SIR(params.ti, self.seq_sir)
        self.assembler = Assembler()
        byte_array = self.assembler.assemble(self.seq_sir)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten(): break

        socket.setReadBufferSize(8 * self.size)
        print("\nSIR sequence uploaded with TI = ", TI,
              " ms.")  #" and REC = ", REC, " ms.")

    # Function to change TI in SIR sequence
    def change_SIR(self, TI, seq):
        f = open(seq, 'r+')  # Open sequence and read lines
        lines = f.readlines()
        # Modify TI time in the 8th last line
        #ines[-14] = 'PR 3, ' + str(int(TI * 1000 - 198)) + '\t// wait&r\n'
        #lines[-18] = 'PR 3, ' + str(int(TI * 1000 - 198)) + '\t// wait&r\n'
        lines[-9] = 'PR 3, ' + str(int(TI * 1000 - 198)) + '\t// wait&r\n'
        lines[-13] = 'PR 3, ' + str(int(TI * 1000 - 198)) + '\t// wait&r\n'
        f.close()  # Close and write/save modified sequence
        with open(seq, "w") as out_file:
            for line in lines:
                out_file.write(line)

    # Set uploaded sequence
    def set_uploaded_seq(self, seq):
        print("Set uploaded Sequence.")
        self.assembler = Assembler()
        byte_array = self.assembler.assemble(seq)
        socket.write(struct.pack('<I', 4 << 28))
        socket.write(byte_array)

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        # Multiple function calls
        socket.setReadBufferSize(8 * self.size)
        print(byte_array)
        print("Sequence uploaded to server.")
        self.uploaded.emit(True)

#_______________________________________________________________________________
#   Functions to Control Console

# Function to triffer acquisition and perform single readout

    def acquire(self, ts=20):
        t0 = time.time()  # calculate time for acquisition
        socket.write(struct.pack('<I', 1 << 28))

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten(): break

        while True:  # Read data
            socket.waitForReadyRead()
            datasize = socket.bytesAvailable()
            # print(datasize)
            time.sleep(0.0001)
            if datasize == 8 * self.size:
                print("Readout finished : ", datasize)
                self.buffer[0:8 * self.size] = socket.read(8 * self.size)
                t1 = time.time()  # calculate time for acquisition
                break
            else:
                continue

        print("Start processing readout.")
        self.process_readout(ts)
        print("Start analyzing data.")
        self.analytics()
        print('Finished acquisition in {:.3f} ms'.format((t1 - t0) * 1000.0))
        # Emit signal, when data was read
        self.readout_finished.emit()

    def acquireImage(self, npe=16, ts=20):

        socket.write(struct.pack('<I', 6 << 28 | npe))
        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        for n in range(npe):
            while True:  # Read data
                socket.waitForReadyRead()
                datasize = socket.bytesAvailable()
                time.sleep(0.0001)
                if datasize == 8 * self.size:
                    print("Readout finished : ", datasize)
                    self.buffer[0:8 * self.size] = socket.read(8 * self.size)
                    break
                else:
                    continue

            self.process_readout(ts)
            self.readout_finished.emit()

    def acquireProjection(self, idx, ts=20):

        print("Acquire projection along axis: ", idx, "\n")
        socket.write(struct.pack('<I', 7 << 28 | idx))

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        while True:  # Read data
            socket.waitForReadyRead()
            datasize = socket.bytesAvailable()
            # print(datasize)
            time.sleep(0.0001)
            if datasize == 8 * self.size:
                print("Readout finished : ", datasize)
                self.buffer[0:8 * self.size] = socket.read(8 * self.size)
                t1 = time.time()  # calculate time for acquisition
                break
            else:
                continue

        print("Start processing readout.")
        self.process_readout(ts)

        self.readout_finished.emit()

    # Function to set frequency
    def set_freq(self, freq):
        params.freq = freq
        socket.write(struct.pack('<I', 2 << 28 | int(1.0e6 * freq)))
        print("Set frequency!")

    # Function to set attenuation
    def set_at(self, at):
        params.at = at
        socket.write(struct.pack('<I', 3 << 28 | int(abs(at) / 0.25)))
        print("Set attenuation!")

    # Function to set gradient offsets
    def set_gradients(self, gx=None, gy=None, gz=None, gz2=None):

        if not gx == None:
            if np.sign(gx) < 0: sign = 1
            else: sign = 0
            socket.write(
                struct.pack('<I',
                            5 << 28 | self.GR_x << 24 | sign << 20 | abs(gx)))
        if not gy == None:
            if np.sign(gy) < 0: sign = 1
            else: sign = 0
            socket.write(
                struct.pack('<I',
                            5 << 28 | self.GR_y << 24 | sign << 20 | abs(gy)))
        if not gz == None:
            if np.sign(gz) < 0: sign = 1
            else: sign = 0
            socket.write(
                struct.pack('<I',
                            5 << 28 | self.GR_z << 24 | sign << 20 | abs(gz)))
        if not gz2 == None:
            if np.sign(gz2) < 0: sign = 1
            else: sign = 0
            #socket.write(struct.pack('<I', 5 << 28 | self.GR_z2 << 24 | sign << 20 | abs(gz2)))

        while (True):  # Wait until bytes written
            if not socket.waitForBytesWritten():
                break

        #self.acquire()
#_______________________________________________________________________________
#   Process and analyse acquired data

# Function to process the readout: extract spectrum, real, imag and magnitude data

    def process_readout(self,
                        ts):  # Read buffer part of interest and perform FFT

        # Max. data index and crop data
        self.data_idx = int(ts * 250)
        self.dclip = self.data[0:self.data_idx] * 2000.0
        # +-1V -> ultiply by 2000 to obtain mV
        timestamp = datetime.now()

        self.mag = np.abs(self.data)
        self.pha = np.angle(self.data)

        # Time domain data
        self.mag_t = np.abs(self.dclip)
        self.imag_t = np.imag(self.dclip)
        self.real_t = np.real(self.dclip)
        self.mag_con = np.convolve(self.mag_t,
                                   np.ones((50, )) / 50,
                                   mode='same')
        self.real_con = np.convolve(self.real_t,
                                    np.ones((50, )) / 50,
                                    mode='same')
        self.time_axis = np.linspace(0, ts, self.data_idx)

        # Frequency domain data
        self.freqaxis = np.linspace(-self.freq_range / 2, self.freq_range / 2,
                                    self.data_idx)  # 5000 points ~ 20ms
        self.fft = np.fft.fftshift(
            np.fft.fft(np.fft.fftshift(self.dclip),
                       n=self.data_idx,
                       norm='ortho'))  # Normalization through 1/sqrt(n)
        self.fft_mag = abs(self.fft)

        params.dataTimestamp = timestamp.strftime('%m/%d/%Y, %H:%M:%S')
        params.data = self.dclip
        params.freqaxis = self.freqaxis
        params.fft = self.fft_mag

        # Ampltiude and phase plot
        #fig, ax = plt.subplots(2,1)
        #ax[0].plot(self.time_axis, self.real_t)
        #ax[0].plot(self.time_axis, np.convolve(self.real_t, np.ones((50,))/50, mode='same'))
        #ax[1].plot(self.fft)
        #ax[1].psd(self.dclip, Fs=250, Fc=int(1.0e6 * params.freq))
        #fig.tight_layout(); plt.show()

        print("\tReadout processed.")

    # Function to calculate parameter like snr and peak values
    def analytics(self):  # calculate output parameters

        # Determine peak/maximum value:
        self.peak_value = round(np.max(self.fft_mag), 2)
        self.max_index = np.argmax(self.fft_mag)

        # Declarations
        win = int(self.data_idx / 20)
        N = 50
        p_idx = self.max_index

        # Full with half maximum (fwhm):
        # Determine candidates inside peak window by substruction of half peakValue
        candidates = np.abs([
            x - self.peak_value / 2
            for x in self.fft_mag[p_idx - win:p_idx + win]
        ])
        # Calculate index difference by findind indices of minima, calculate fwhm in Hz thereafter
        fwhm_idx = np.argmin(candidates[win:]) + win - np.argmin(
            candidates[:win])
        self.fwhm_value = fwhm_idx * (abs(np.min(self.freqaxis)) + abs(
            np.max(self.freqaxis))) / self.data_idx
        # Verification of fwhm calculation
        #plt.plot(candidates)
        #plt.show()

        # Signal to noise ratio (SNR):
        # Determine noise by substruction of moving avg from signal
        movAvg = np.convolve(self.fft_mag, np.ones((N, )) / N, mode='same')
        noise = np.subtract(self.fft_mag, movAvg)
        # Calculate sdt. dev. outside a window, that depends on fwhm
        self.snr = round(
            self.peak_value /
            np.std([*noise[:p_idx - win], *noise[p_idx + win:]]), 2)
        # Verification of snr calculation
        #plt.plot(self.freqaxis, self.fft_mag); plt.plot(self.freqaxis, movAvg);
        #plt.plot([*noise[:p_idx-win], *noise[p_idx+win:]])
        #plt.show()

        # Center frequency calculation:
        self.center_freq = round(
            params.freq + ((self.max_index - self.data_idx / 2) *
                           self.freq_range / self.data_idx) / 1.0e6, 6)

        print("\tData analysed.")

#_______________________________________________________________________________
#   T1 Measurement

# Acquires one or multiple T1 values through multiple IR's

    def T1_measurement(self, values, freq, recovery, **kwargs):
        print('T1 Measurement')

        avgPoint = kwargs.get('avgP', 1)
        avgMeas = kwargs.get('avgM', 1)
        seq_type = kwargs.get('seqType', 1)

        self.idxM = 0
        self.idxP = 0
        self.T1 = []
        self.R2 = []
        self.set_freq(freq)

        while self.idxM < avgMeas:
            print("Measurement : ", self.idxM + 1, "/", avgMeas)
            self.measurement = []

            for self.ti in values:
                self.peaks = []

                if seq_type == 'sir':
                    self.set_SIR(self.ti)
                else:
                    self.set_IR(self.ti)

                while self.idxP < avgPoint:
                    print("Datapoint : ", self.idxP + 1, "/", avgPoint)
                    time.sleep(recovery / 1000)
                    socket.write(struct.pack('<I', 1 << 28))

                    while True:  # Readout data
                        socket.waitForReadyRead()
                        datasize = socket.bytesAvailable()
                        print(datasize)
                        time.sleep(0.1)
                        if datasize == 8 * self.size:
                            print("IR readout finished : ", datasize)
                            self.buffer[0:8 * self.size] = socket.read(
                                8 * self.size)
                            break
                        else:
                            continue

                    print("Start processing IR readout.")
                    self.process_readout()
                    print("Start analyzing IR data.")
                    self.analytics()
                    self.peaks.append(
                        np.max(self.mag_con) *
                        np.sign(self.real_con[np.argmin(self.real_con[0:50])]))
                    #self.peaks.append(self.peak_value*np.sign(self.real_con[np.argmin(self.real_con[0:50])]))
                    self.readout_finished.emit()
                    self.idxP += 1

                self.measurement.append(np.mean(self.peaks))
                self.idxP = 0

            def func(x):
                return p[0] - p[1] * np.exp(-p[2] * x)

            try:
                p, cov = curve_fit(self.T1_fit, values, self.measurement)
                # Calculate T1 value and error
                self.T1.append(
                    round(1.44 * brentq(func, values[0], values[-1]), 2))
                self.R2.append(
                    round(
                        1 - (np.sum(
                            (self.measurement - self.T1_fit(values, *p))**2) /
                             (np.sum(
                                 (self.measurement - np.mean(self.measurement))
                                 **2))), 5))
                self.x_fit = np.linspace(0, int(1.2 * values[-1]), 1000)
                self.y_fit = self.T1_fit(self.x_fit, *p)
                self.fit_params = p
            except:  # in case no fit found
                self.T1.append(float('nan'))
                self.R2.append(float('nan'))
                self.x_fit = float('nan')
                self.y_fit = float('nan')
                self.fit_params = float('nan')
            self.t1_finished.emit()
            self.idxM += 1

        return np.nanmean(self.T1), np.nanmean(self.R2)

    # Calculates fit for multiple IR's to determine t0
    def T1_fit(self, x, A, B, C):
        return A - B * np.exp(-C * x)

#_______________________________________________________________________________
#   T2 Measurement

# Acquires one or multiple T2 values through multiple SE's

    def T2_measurement(self, values, freq, recovery, **kwargs):
        print('T1 Measurement')

        avgPoint = kwargs.get('avgP', 1)
        avgMeas = kwargs.get('avgM', 1)
        self.idxM = 0
        self.idxP = 0
        self.T2 = []
        self.R2 = []
        self.measurement = []

        self.set_freq(freq)

        while self.idxM < avgMeas:
            print("Measurement : ", self.idxM + 1, "/", avgMeas)
            self.measurement = []

            for self.te in values:
                self.peaks = []
                self.set_SE(self.te)

                while self.idxP < avgPoint:
                    print("Datapoint : ", self.idxP + 1, "/", avgPoint)
                    time.sleep(recovery / 1000)
                    socket.write(struct.pack('<I', 1 << 28))

                    while True:
                        socket.waitForReadyRead()
                        datasize = socket.bytesAvailable()
                        print(datasize)
                        time.sleep(0.1)
                        if datasize == 8 * self.size:
                            print("IR readout finished : ", datasize)
                            self.buffer[0:8 * self.size] = socket.read(
                                8 * self.size)
                            break
                        else:
                            continue

                    print("Start processing SE readout.")
                    self.process_readout()
                    print("Start analyzing SE data.")
                    self.analytics()
                    self.peaks.append(np.max(self.mag_con))

                    self.readout_finished.emit()
                    self.idxP += 1

                self.measurement.append(np.mean(self.peaks))
                self.idxP = 0

            # Calculate T2 value and error
            try:
                p, cov = curve_fit(self.T2_fit,
                                   values,
                                   self.measurement,
                                   bounds=([0, self.measurement[0],
                                            0], [10, 50000, 0.5]))
                # Calculation of T2: M(T2) = 0.37*(func(0)) = 0.37(A+B), T2 = -1/C * ln((M(T2)-A)/B)
                self.T2.append(
                    round(
                        -(1 / p[2]) * np.log(
                            ((0.37 * (p[0] + p[1])) - p[0]) / p[1]), 5))
                self.R2.append(
                    round(
                        1 - (np.sum(
                            (self.measurement - self.T2_fit(values, *p))**2) /
                             (np.sum(
                                 (self.measurement - np.mean(self.measurement))
                                 **2))), 5))
                self.x_fit = np.linspace(0, int(1.2 * values[-1]), 1000)
                self.y_fit = self.T2_fit(self.x_fit, *p)
                self.fit_params = p
                self.t2_finished.emit()
                self.idxM += 1
            except:
                self.T2.append(float('nan'))
                self.R2.append(float('nan'))
                self.x_fit = float('nan')
                self.y_fit = float('nan')
                self.fit_params = float('nan')
                self.t2_finished.emit()
                self.idxM += 1

        return np.nanmean(self.T2), np.nanmean(self.R2)

    # Calculates fit for multiple SE's to determine drop of Mxy
    def T2_fit(self, x, A, B, C):
        return A + B * np.exp(-C * x)
Exemple #35
0
 def test_assemble(self):
     from assembler import Assembler
     assbl = Assembler(sequences)
     assbl._find_matching_pairs = MagicMock(return_value=(top_seq_dict, bottom_seq_dict, top_ranges_dict, bottom_ranges_dict))
     assbl._determine_order = MagicMock(return_value=order)
     self.assertEqual(assbl.assemble(), assembled_seq)
class AssemblerTest(unittest.TestCase):
    def setUp(self):
        self.assembler = Assembler()

    def test_instance(self):
        thing = object()
        self.assembler.instance('thing', thing)
        result = self.assembler.assemble('thing')
        self.assertEqual(thing, result)

    def test_factory(self):
        def thing_maker():
            return object()
        self.assembler.factory('thing', thing_maker)
        result1 = self.assembler.assemble('thing')
        self.assertTrue(isinstance(result1, object))
        result2 = self.assembler.assemble('thing')
        self.assertTrue(isinstance(result2, object))
        self.assertNotEqual(result1, result2)

    def test_assemble_with_args(self):
        def thing_maker(one, two):
            return [one, two]
        self.assembler.factory('thing', thing_maker)
        result = self.assembler.assemble('thing', 'one', 'two')
        self.assertEqual(['one', 'two'], result)

    def test_assemble_with_kwargs(self):
        def thing_maker(one, two):
            return [one, two]
        self.assembler.factory('thing', thing_maker)
        result = self.assembler.assemble('thing', two='one', one='two')
        self.assertEqual(['two', 'one'], result)

    def test_assemble_with_args_and_kwargs(self):
        def thing_maker(one, two, a=None, b=None):
            return [one, two, (a, b)]
        self.assembler.factory('thing', thing_maker)
        result = self.assembler.assemble('thing', 'one', 'two', b='b', a='a')
        self.assertEqual(['one', 'two', ('a', 'b')], result)

    def test_assemble_with_defaults(self):
        def thing_maker(one='one', two='two', a='a', b='b'):
            return [one, two, (a, b)]
        self.assembler.factory('thing', thing_maker)
        result = self.assembler.assemble('thing')
        self.assertEqual(['one', 'two', ('a', 'b')], result)

    def test_recursive_assemble(self):
        def thing1_maker(thing2):
            return thing2

        def thing2_maker():
            return 'instance of thing2'
        self.assembler.factory('thing1', thing1_maker)
        self.assembler.factory('thing2', thing2_maker)
        result = self.assembler.assemble('thing1')
        self.assertEqual('instance of thing2', result)

    def test_class_constructors(self):
        class A(object):
            def __init__(self, b, c):
                self.b = b
                self.c = c

        class B(object):
            def __init__(self):
                pass

        class C(object):
            def __init__(self, d):
                self.d = d

        class D(object):
            def __init__(self):
                pass
        self.assembler.factory('a', A)
        self.assembler.factory('b', B)
        self.assembler.factory('c', C)
        self.assembler.factory('d', D)
        a = self.assembler.assemble('a')
        self.assertTrue(isinstance(a, A))
        self.assertTrue(hasattr(a, 'b'))
        self.assertNotEqual(None, a.b)
        self.assertTrue(isinstance(a.b, B))
        self.assertTrue(hasattr(a, 'c'))
        self.assertNotEqual(None, a.c)
        self.assertTrue(isinstance(a.c, C))
        self.assertTrue(hasattr(a.c, 'd'))
        self.assertNotEqual(None, a.c.d)
        self.assertTrue(isinstance(a.c.d, D))
Exemple #37
0
# 
a_qe  = Form(kernel=Kernel(qe), trial=ux, test=ux)
a_one = Form(kernel=Kernel(one), trial=ux, test=ux)

L = Form(kernel=Kernel(one), test=u)

# 
# Problems
# 
problems = [[a_qe,L], [a_one]]

#
# Assembly
# 
assembler = Assembler(problems, mesh)
assembler.assemble()

# =============================================================================
# Linear system for generating observations
# =============================================================================
system = LinearSystem(assembler,0)
f = system.get_rhs()

#
# Incorporate constraints
# 
system.add_dirichlet_constraint('left',0)
system.add_dirichlet_constraint('right',0)

#
# Compute model output
Exemple #38
0
#!/usr/bin/env python
import os
from assembler import Assembler
from optionresolver import OptionResolver
from patchers import PatchManager, PatchPatcher

# Get the user supplied options
options = OptionResolver()

# Assemble the source code
assembler = Assembler(options.manifest()["projects"], options.destination(), options.extra_parameters())
assembler.assemble()

# Apply any patches
patchmanager = PatchManager(options.manifest()["patches"], options.destination())
patch_base = os.path.dirname(options.manifest_location())
patchmanager.add_patcher("patch_file", PatchPatcher(patch_base))
patchmanager.patch()