Example #1
0
def test_print_csv_list():
    """Test Q.print_csv() with list of vectors as input"""

    in_vals = [1, 2, 3, 4]
    vec1 = Q.mk_col(in_vals, Q.I1)
    vec2 = Q.mk_col(in_vals, Q.I1)
    Q.print_csv([vec1, vec2])
    print("Successfully executed Q.print_csv with list as input")
Example #2
0
def test_print_csv():
    """Test Q.print_csv() with opfile=None (default)
    This will print the vector values to stdout"""

    in_vals = [1, 2, 3, 4]
    vec = Q.mk_col(in_vals, Q.I1)
    Q.print_csv(vec)
    print("Successfully executed Q.print_csv test")
Example #3
0
def test_print_csv_str():
    """Test Q.print_csv() with opfile=""
    This will return the string representation of vector"""

    in_vals = [1, 2, 3, 4]
    vec = Q.mk_col(in_vals, Q.I1)
    result = Q.print_csv(vec, {'opfile': ""})
    sys.stdout.write(result)
    print("Successfully executed Q.print_csv test")
Example #4
0
def test_vec_concat():
    """Test the vector methods concatenation"""

    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec1 = Q.const(in_val).set_name("new_vec").eval()
    assert (isinstance(vec1, PVector))
    Q.print_csv(vec1)
    assert (vec1.get_name() == "new_vec")
    print("Successfully executed vector method concat test")
Example #5
0
def drawLines():
    for i in Q.Qr(layers - 1):
        sectiony = 600 / (nodes[i] + 1)
        for n in Q.Qr(nodes[i]):
            y = getNodeYValues(i + 1)
            for c in y:
                pygame.draw.line(screen, (255, 255, 255),
                                 (sectionx * (i + 1), int(sectiony * (n + 1))),
                                 (sectionx * (i + 2), int(c)))
Example #6
0
def drawNodes():
    for i in Q.Qr(layers):
        sectiony = 600 / (nodes[i] + 1)
        size = 60 / nodes[i]
        for n in Q.Qr(nodes[i]):
            p = (sectionx * (i + 1), int(sectiony * (n + 1)))
            pygame.draw.circle(screen, (255, 0, 0), p, int(size))
            t = getText(str(layerValues[i].A1[n]), 5, size)
            screen.blit(t,
                        (p[0] - t.get_width() / 2, p[1] - t.get_height() / 2))
Example #7
0
def test_full_without_dtype():
    """Test the Q.full() functionality without providing dtype"""

    vec = Q.full(6, 5)
    assert (isinstance(vec, PVector))
    assert (vec.qtype() == Q.int64)
    assert (vec.num_elements() == 0)
    vec.eval()
    assert (vec.num_elements() == 6)
    Q.print_csv(vec)
    print("Successfully executed Q.full without dtype test")
Example #8
0
def test_ones():
    """Test the Q.ones() functionality
    It creates a constant vector of specified length with all values as 1"""

    vec = Q.ones(6)
    assert (isinstance(vec, PVector))
    assert (vec.num_elements() == 0)
    vec.eval()  # expecting all values to be one
    assert (vec.num_elements() == 6)
    Q.print_csv(vec)
    print("Successfully executed Q.ones test")
Example #9
0
def test_full():
    """Test the Q.full() functionality, alias of Q.const()
    It creates a constant vector with specified value and length"""

    vec = Q.full(6, 5, dtype=Q.int8)
    assert (isinstance(vec, PVector))
    assert (vec.num_elements() == 0)
    vec.eval()
    assert (vec.num_elements() == 6)
    Q.print_csv(vec)
    print("Successfully executed Q.full test")
Example #10
0
def test_arange_with_start():
    """Test the Q.arange() functionality with start value specified in input"""

    stop = 5
    start = 1
    vec = Q.arange(start, stop)
    assert (isinstance(vec, PVector))
    assert (vec.num_elements() == 0)
    vec.eval()  # expecting values as 1, 2, 3, 4
    assert (vec.num_elements() == (stop - start))
    Q.print_csv(vec)
    print("Successfully executed Q.arange with start value test")
Example #11
0
def test_arange():
    """Test the Q.arange() functionality, wrapper around Q.seq()"""

    stop = 5
    vec = Q.arange(stop)
    assert (isinstance(vec, PVector))
    assert (vec.qtype() == Q.int64)
    assert (vec.num_elements() == 0)
    vec.eval()  # expecting values as 0, 1, 2, 3, 4
    assert (vec.num_elements() == stop)
    Q.print_csv(vec)
    print("Successfully executed Q.arange test")
Example #12
0
def test_add():
    """Test Q.add() functionality, it is a alias for Q.vvadd()"""

    in_vals = [1, 2, 3, 4, 5]
    vec1 = Q.mk_col(in_vals, Q.I1)
    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec2 = Q.const(in_val)
    out = Q.add(vec1, vec2)
    assert (out.num_elements() == 0)
    out.eval()
    assert (out.num_elements() == len(in_vals))
    Q.print_csv(out)
    print("Successfully executed vec_add test")
Example #13
0
def test_seq():
    """Test the Q.seq() functionality
    It creates a vector with values as sequence with specified inputs"""

    length = 6
    in_val = {'start': -1, 'by': 5, 'qtype': Q.I1, 'len': length}
    vec = Q.seq(in_val)
    assert (isinstance(vec, PVector))
    assert (vec.num_elements() == 0)
    vec.eval()
    assert (vec.num_elements() == length)
    Q.print_csv(vec)
    print("Successfully executed Q.seq test")
Example #14
0
def test_const():
    """Test the Q.const() functionality
    It creates a constant vector with specified value and length"""

    length = 6
    in_val = {'val': 5, 'qtype': Q.I1, 'len': length}
    vec = Q.const(in_val)
    assert (isinstance(vec, PVector))
    assert (vec.num_elements() == 0)
    vec.eval()
    assert (vec.num_elements() == length)
    Q.print_csv(vec)
    print("Successfully executed Q.const test")
Example #15
0
def test_arange_with_step():
    """Test the Q.arange() functionality with start and step value specified in input"""

    step = 2
    stop = 8
    start = 3
    vec = Q.arange(start, stop, step)
    assert (isinstance(vec, PVector))
    assert (vec.qtype() == Q.int64)
    assert (vec.num_elements() == 0)
    vec.eval()  # expecting values as 3, 5, 7
    # assert(vec.num_elements() == 3)
    Q.print_csv(vec)
    print("Successfully executed Q.arange with step value test")
Example #16
0
def test_print_csv_to_file():
    """Test Q.print_csv() with opfile=file
    This will write the print_csv output to file"""

    in_vals = [1, 2, 3, 4]
    file_name = "result.txt"
    vec1 = Q.mk_col(in_vals, Q.I1)
    vec2 = Q.mk_col(in_vals, Q.I1)
    Q.print_csv([vec1, vec2], {'opfile': file_name})
    assert (os.path.exists(file_name))
    os.system("cat %s" % file_name)
    os.remove(file_name)
    assert (not os.path.exists(file_name))
    print("Successfully executed Q.print_csv with opfile as file")
Example #17
0
def test_sum():
    """Test the Q operator Q.sum() - returns sum of vector"""

    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec = Q.const(in_val).eval()
    result = Q.sum(vec)
    assert (isinstance(result, PReducer))
    total_sum, total_val = result.eval()
    assert (isinstance(total_sum, PScalar))
    assert (isinstance(total_val, PScalar))
    assert (total_val.to_num() == vec.length())
    assert (total_sum.to_num() == 25)
    print("Total sum is {}".format(total_sum))
    print("Total visited values are {}".format(total_val))
    print("Successfully executed Q.sum() operator test")
def string_split(STR):
    rac = STR.split('/')
    p = int(rac[0])
    if len(rac) == 1:
        rac.append(1)
    q = int(rac[1])
    return Q.Rational(p, q)
Example #19
0
def test_scalar_arith():
    """Create two scalars and perform scalar arithmetic"""
    val1 = 5
    val2 = 10
    qtype = Q.I1
    sclr1 = PScalar(val1, qtype)
    sclr2 = PScalar(val2, qtype)

    # Add scalars
    sclr3 = sclr1 + sclr2
    assert (isinstance(sclr3, PScalar))
    assert (sclr3.to_num() == (val1 + val2))
    print(sclr3)

    # Add scalar and number
    sclr4 = sclr1 + val2
    assert (isinstance(sclr4, PScalar))
    assert (sclr4.to_num() == (val1 + val2))
    print(sclr4.qtype())

    # Add scalar and vector
    vec1 = Q.mk_col([1, 2, 3, 4], sclr1.fldtype())
    res = sclr1 + vec1
    assert (isinstance(res, PVector))
    assert (vec1.qtype() == res.qtype())

    result = (sclr3 == sclr4)
    assert (result == True)

    print("Successfully executed scalar arithmetic test")
Example #20
0
 def __truediv__(self, other):
     if type(self) != type(other):
         return tryReverseOp(self, other, '//')
     if (self % other == N(0)):
         return self // other
     else:
         return Q(self, other)
Example #21
0
 def __init__(self,
              kernel_type='Linear',
              degree=3,
              gamma='auto',
              coef0=0.0,
              epsilon=1e-4,
              C=0.03):
     self.param = Q.SVM_Param(kernel_type, degree, gamma, coef0, epsilon, C)
Example #22
0
 def __init__(self, verbose=False):
     self.verbose = verbose
     self.Q = Q.QLearner(num_states=10000,
                         num_actions=3,
                         alpha=0.2,
                         gamma=0.9,
                         rar=0.5,
                         radr=0.99,
                         dyna=50)
Example #23
0
def test_array_without_dtype():
    """Test the Q.array() functionality without providing dtype
    it should pick default dtype"""

    in_vals = [1, 2, 3, 4]
    vec = Q.array(in_vals)
    assert (isinstance(vec, PVector))
    assert (vec.length() == len(in_vals))
    assert (vec.qtype() == Q.int64)

    in_vals = [1.0, 2.0, 3, 4]
    vec = Q.array(in_vals)
    assert (isinstance(vec, PVector))
    assert (vec.length() == len(in_vals))
    assert (vec.qtype() == Q.float64)

    # Q.print_csv(vec)
    print("Successfully executed Q.array without providing dtype")
Example #24
0
def test_mk_col():
    """Test the Q.mk_col() functionality
    Input is list, output is PVector"""

    in_vals = [1, 2, 3, 4]
    vec = Q.mk_col(in_vals, Q.I1)
    assert (isinstance(vec, PVector))
    assert (vec.length() == len(in_vals))
    assert (vec.qtype() == Q.I1)
    print("Successfully executed Q.mk_col test")
Example #25
0
    def run(self):
        self.QF1 = Q()
        self.QF2 = Q()
        self.QF3 = Q()
        self.QF4 = Q()
        self.QF5 = Q()
        initiate(self.year)

        x = len(bigArray)
        for idx in range(x / 4):
            self.QF1.enqueue(idx)
        for idx2 in range(int(x / 4), int(x / 2)):
            self.QF2.enqueue(idx2)
        for idx3 in range(int(x / 2), (3 * x) / 4):
            self.QF3.enqueue(idx3)
        for idx4 in range((3 * x) / 4, x):
            self.QF4.enqueue(idx4)

        # for reinitiate

        # for idx in range( x): self.QF5.enqueue(idx)

        t = threading.Thread(target=self.F1)
        self.threads.append(t)
        t.start()
        t = threading.Thread(target=self.F2)
        self.threads.append(t)
        t.start()
        t = threading.Thread(target=self.F3)
        self.threads.append(t)
        t.start()
        t = threading.Thread(target=self.F4)
        self.threads.append(t)
        t.start()
        # t = threading.Thread(target = self.F5 )
        # self.threads.append(t)
        # t.start()

        for t in self.threads:
            t.join()
        print "Exiting " + self.name

        closeConn()
Example #26
0
def test_array():
    """Test the Q.array() functionality, alias for Q.mk_col
    Input is list, output is PVector"""

    in_vals = [1, 2, 3, 4]
    vec = Q.array(in_vals, dtype=Q.int8)
    assert (isinstance(vec, PVector))
    assert (vec.length() == len(in_vals))
    assert (vec.qtype() == Q.int8)
    # Q.print_csv(vec)
    print("Successfully executed Q.array test")
Example #27
0
def test_op_concat():
    """Test the Q operator concatenation"""

    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec1 = Q.const(in_val)
    in_val = {'val': 25, 'qtype': Q.I1, 'len': 5}
    vec2 = Q.const(in_val)
    result = Q.vveq(Q.vvsub(Q.vvadd(vec1, vec2), vec2), vec1).eval()
    assert (isinstance(result, PVector))
    Q.print_csv(result)
    print("Successfully executed Q operator concat test")
Example #28
0
def test_vec_persist():
    """Test the vec persist method"""

    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec1 = Q.const(in_val)
    in_val = {'val': 25, 'qtype': Q.I1, 'len': 5}
    vec2 = Q.const(in_val)
    result = Q.vveq(Q.vvsub(Q.vvadd(vec1, vec2), vec2),
                    vec1).persist(True).eval()
    assert (isinstance(result, PVector))
    Q.print_csv(result)
    print(
        "Successfully executed Q operator concat test with persist flag to true"
    )
Example #29
0
def test_op_concat_memo():
    """Test the Q operator concatenation with setting memo to false"""

    in_val = {'val': 5, 'qtype': Q.I1, 'len': 5}
    vec1 = Q.const(in_val).memo(False)
    in_val = {'val': 25, 'qtype': Q.I1, 'len': 5}
    vec2 = Q.const(in_val).memo(False)
    result = Q.vveq(
        Q.vvsub(Q.vvadd(vec1, vec2).memo(False), vec2).memo(False),
        vec1).memo(False).eval()
    assert (isinstance(result, PVector))
    Q.print_csv(result)
    print(
        "Successfully executed Q operator concat test with setting memo false")
def get_Matrix():
    rational = [[Q.Rational(0, 1) for x in range(n)] for y in range(n)]
    for i in range(0, m):
        for j in range(0, n):
            rational[i][j] = string_split(E[i][j].get())
            E[i][j].delete(0, END)

    for i in range(0, m):
        for j in range(0, n):
            I[i][j].delete(0, END)

    for i in range(0, m):
        for j in range(0, n):
            if (rational[i][j].den == 1):
                I[i][j].insert(0, rational[i][j].num)
            else:
                I[i][j].insert(0, rational[i][j].den)
                I[i][j].insert(0, "/")
                I[i][j].insert(0, rational[i][j].num)
Example #31
0
def readSQE( datapath, Q = 'Q', E = 'E', Sqe = 'Sqe' ):
    'read idf Q,E,Sqe and construct a S(Q,E) histogram'
    qpath = os.path.join( datapath, Q )
    import Q
    q = Q.read( qpath)[1]
    q.shape = q.size,

    epath = os.path.join( datapath, E )
    import E
    e = E.read( epath)[1]
    e.shape = e.size,

    sqepath = os.path.join( datapath, Sqe )
    import Sqe
    s = Sqe.read( sqepath)[1]

    import histogram as H
    qaxis = H.axis( 'Q', boundaries = q )
    eaxis = H.axis( 'energy', boundaries = e )
    return H.histogram(
        'S(Q,E)',
        [qaxis, eaxis],
        data = s )