Beispiel #1
0
def multilin_mult(U, T1, dims):
    """    
    Performs the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T, where dims = T.shape. We need the first unfolding
    T1 of T to start the computations.

    Inputs
    ------
    U: list of 2-D arrays
    T1: 2-D array
        First unfolding of T.
    dims: list of ints
        Dimension of T.

    Outputs
    -------
    S: float array
        S is the resulting multidimensional of the multilinear multiplication (U[0]^T,...,U[L-1]^T)*T.
    """

    L = len(dims)
    # dims_out are the dimensions of the output tensor.
    dims_out = list(dims)
    
    unfolding1 = T1    
    for l in range(L):
        unfolding2 = dot(U[l], unfolding1)
        # Update the current dimension of dims_out.
        dims_out[l] = U[l].shape[0]
        S = empty(dims_out)
        S = cnv.foldback(S, unfolding2, l+1)
        if l < L-1:            
            unfolding1 = cnv.unfold(S, l+2)
        else:
            return S
Beispiel #2
0
 def processing_new(self):
     try:
         self.loader.close()
     except:
         True
     self.setWindowTitle(self.list_filename[0])
     self.save_file.setEnabled(True)
     self.save_roi.setEnabled(True)
     self.popup_()
     self.list_mode_processor=List_Mode()
     s=time.time()
     self.sync_time,sync_channel=Conversion.convert(self.sync_filename[0])
     del sync_channel
     self.list_time,self.list_channel=Conversion.convert(self.list_filename[0])
     
     print('Imported and conveted in {:.2f}s'.format(time.time()-s))
     winsound.MessageBeep()
     delt=(self.sync_time[2]-self.sync_time[1])
     #set the maximum value for the end time and start times
     maxe=int((self.list_time[-1]-self.list_time[0])*1e-6)
     self.end_time.setMaximum(maxe)
     self.end_time.setValue(maxe)
     self.start_time.setMaximum(maxe-1)
     self.start_time.setValue(0)
     self.offset.setMaximum(int(delt-self.duty_cycle.value()/100*delt))
     self.offset.setMinimum(int(-(delt*self.duty_cycle.value()/100)*.5))
     self.view_pop.setEnabled(True)
     self.updater()
Beispiel #3
0
def test_truncation(T, trunc_list, display=True, n_iter=2, power_iteration_normalizer='none'):
    """
    This function test one or several possible truncations for the MLSVD of T, showing the  error of the truncations. It
    is possible to accomplish the same results calling the function mlsvd with display=3 but this is not advisable since
    each call recomputes the same unfolding SVD's.
    The variable trunc_list must be a list of truncations. Even if it is only one truncation, it must be a list with one
    truncation only.
    """

    # Set the main variables about T.
    dims = T.shape
    L = len(dims)
    Tsize = norm(T)

    # Transform list into array and get the maximum for each dimension.
    max_trunc_dims = np.max(array(trunc_list), axis=0)

    # Compute truncated SVD of all unfoldings of T.
    sigmas = []
    U = []
    T1 = empty((dims[0], prod(dims) // dims[0]), dtype=float64)
    for l in range(L):
        Tl = cnv.unfold(T, l+1)
        if l == 0:
            T1 = cnv.unfold_C(T, l+1)
        low_rank = min(dims[l], max_trunc_dims[l])
        Ul, sigma_l, Vlt = rand_svd(Tl, low_rank, n_iter=n_iter, power_iteration_normalizer=power_iteration_normalizer)
        sigmas.append(sigma_l)
        U.append(Ul)

    # Save errors in a list.
    trunc_error = []

    # Truncated MLSVD.
    for trunc in trunc_list:
        # S, U and UT truncated.
        current_dims = trunc
        current_U = []
        current_sigmas = []
        for l in range(L):
            current_U.append(U[l][:, :current_dims[l]])
            current_sigmas.append(sigmas[l][:current_dims[l]])
        current_UT = [current_U[l].T for l in range(L)]
        S = mlinalg.multilin_mult(current_UT, T1, dims)

        # Error of truncation.
        S1 = cnv.unfold(S, 1)
        current_error = mlinalg.compute_error(T, Tsize, S1, current_U, current_dims)
        trunc_error.append(current_error)

        # Display results.
        if display:
            print('Truncation:', current_dims)
            print('Error:', current_error)
            print()

    return trunc_error
Beispiel #4
0
    def test_century(self):

        self.assertEqual(cv.century(1905), 20)

        self.assertEqual(cv.century('1905'), 20)

        self.assertEqual(cv.century(1700), 17)

        self.assertEqual(cv.century(200), 2)

        self.assertEqual(cv.century(-200), 2)
Beispiel #5
0
def compute_dogleg_steps(Tsize, Tl, T1_approx, factors, grad, JT_J_grad, x, y,
                         error, inner_parameters):
    """
    Compute Dogleg step.
    """

    count = 0
    best_x = x.copy()
    best_y = y.copy()
    best_error = error
    best_factors = deepcopy(factors)
    gain_ratio = 1
    delta = 1

    while gain_ratio > 0:
        # Keep the previous value of x and error to compare with the new ones in the next iteration.
        old_x = x
        old_y = y
        old_error = error
        damp, inner_method, cg_maxiter, cg_factor, cg_tol, tol_jump, symm, factors_norm, fix_mode = inner_parameters

        # Apply dog leg method.
        y = dogleg(y, grad, JT_J_grad, delta)

        # Update results.
        x = x + y

        # Balance and transform factors.
        factors = cnv.x2cpd(x, factors, eq=False)
        factors = cnv.transform(factors, symm, factors_norm)

        # Compute error.
        T1_approx = cnv.cpd2unfold1(T1_approx, factors)
        error = crt.fastnorm(Tl[0], T1_approx) / Tsize

        # Update gain ratio.
        gain_ratio = update_gain_ratio(damp, old_error, error, Tsize, old_x, x,
                                       grad)

        if error < old_error:
            best_x = x.copy()
            best_y = y.copy()
            best_error = error
            best_factors = deepcopy(factors)

        # Update delta.
        delta = update_delta(delta, gain_ratio, norm(x - old_x))

        count += 1
        if count > 10:
            break

    return best_factors, best_x, best_y, best_error
Beispiel #6
0
def handle(content, server):
    """
    Handles the content returned from a lp poll
    """
    try:
        content = json.loads(content)
    except:
        logging.debug(traceback.format_exc())
        gevent.sleep(5)
        return
    block = Conversion.extract_block(content)
    
    if block not in blocks:
        blocks[block] = {}
        bitHopper.LongPoll.trigger(content)
    else:
        #Some pools like p2pool need to send a lot of longpolls
        #Always pass through the longpoll
        bitHopper.LongPoll.trigger(content)
        return
        
    if server not in blocks[block]:
        blocks[block][server] = int(time.time())
        logging.debug('%s, %s' % (server, block))
    gevent.spawn(learn_block, blocks, block)
Beispiel #7
0
def rank1_terms_list(factors):
    """
    Compute each rank 1 term, as a multidimensional array, of the CPD. Let T be the corresponding the tensor, in
    coordinates, of thr CPD given by factors, and let rank1_terms = [T_1, T_2, ..., T_R] be the output of this function.
    Then we have that T_1 + T_2 + ... + T_R = T.

    Inputs
    ------
    factors: list of float 2-D ndarrays with shape (dims[i], R) each
        The CPD factors of some tensor.

    Outputs
    -------
    rank1_terms: list of float ndarrays
        Each tensor rank1_terms[r] is the r-th rank-1 term of the given CPD.
    """

    R = factors[0].shape[1]
    L = len(factors)
    rank1_terms = []

    for r in range(R):
        vectors = []
        for l in range(L):
            # vectors[l] = [w_r^(1),w_r^(2),...,w_r^(L)], which represents w_r^(1) ⊗ w_r^(2) ⊗ ... ⊗ w_r^(L).
            v = factors[l][:, r]
            vectors.append(v.reshape(v.size, 1))
        term = cnv.cpd2tens(vectors)
        rank1_terms.append(term)

    return rank1_terms
Beispiel #8
0
def multilin_mult_cpd(U, W, dims):
    """    
    Performs the multilinear multiplication (U[0],...,U[L-1])*(W[0], ..., W[L-1])*I = (U[0]*W[0],...,U[L-1]*W[L-1])*I, 
    where I.shape = dims = (W[0].shape[1],...,W[L-1].shape[1]) are the size of the columns of the W's.

    Inputs
    ------
    U: list of 2-D arrays
    W: list of 2-D arrays
    dims: list of ints

    Outputs
    -------
    S: float array
        S is the resulting multidimensional of the mentioned multiplication.
    """

    L = len(dims)
    # dims_out are the dimensions of the output tensor.
    dims_out = [] 
    W_new = []
    
    for l in range(L):
        W_new.append( dot(U[l], W[l]) )
        dims_out.append(W_new[l].shape[0])
    
    S = cnv.cpd2tens(W_new)
    return S
Beispiel #9
0
def multirank_approx(T, multi_rank, options):
    """
    This function computes an approximation of T with multilinear rank = multi_rank. Truncation the core tensor of the
    MLSVD doesn't gives the best low multirank approximation, but gives very good approximations.
    
    Inputs
    ------
    T: float array
    multi_rank: list of int
        The desired low multilinear rank.
        
    Outputs
    -------
    T_approx: float array
        The approximating tensor with multilinear rank = multi_rank.
    """
    
    # Compute dimensions and norm of T.
    dims = T.shape
    sorted_dims = sort(array(dims))
    L = len(dims)
    Tsize = norm(T)
    
    # Compute truncated MLSVD of T.
    options = aux.make_options(options, L)
    options.display = 0
    options.trunc_dims = multi_rank
    R_gen = int(ceil( prod(sorted_dims)/(np.sum(sorted_dims) - L + 1) ))
    S, U, UT, sigmas = cmpr.mlsvd(T, Tsize, R_gen, options)

    # Construct the corresponding tensor T_approx.
    S1 = cnv.unfold(S, 1)
    T_approx = multilin_mult(U, S1, multi_rank)
    
    return T_approx
Beispiel #10
0
def hessian(factors, P1, P2, sum_dims):
    """
    Approximate Hessian matrix of the error function.
    """

    L = len(factors)
    R = factors[0].shape[1]
    dims = [factors[l].shape[0] for l in range(L)]
    H = zeros((R * sum(dims), R * sum(dims)))
    vec_factors = [zeros(R * dims[l]) for l in range(L)]
    fortran_factors = [np.array(factors[l], order='F') for l in range(L)]
    for l in range(L):
        vec_factors[l] = cnv.vec(factors[l], vec_factors[l], dims[l], R)
        vec_factors[l] = vec_factors[l].reshape(R * dims[l], 1).T

    for l in range(L):
        I = identity(dims[l])
        # Block H_{ll}.
        H[sum_dims[l]:sum_dims[l + 1],
          sum_dims[l]:sum_dims[l + 1]] = mlinalg.kronecker(P1[l, :, :], I)
        for ll in range(l):
            I = ones((dims[l], dims[ll]))
            tmp1 = mlinalg.kronecker(P2[l, ll, :, :], I)
            tmp2 = zeros((R * dims[l], R * dims[ll]))
            tmp2 = compute_blocks(tmp2, fortran_factors[l], vec_factors[ll],
                                  tuple(dims), R, l, ll)
            # Blocks H_{l, ll} and H_{ll, l}.
            H[sum_dims[l]:sum_dims[l+1], sum_dims[ll]:sum_dims[ll+1]] = \
                mlinalg.hadamard(tmp1, tmp2, H[sum_dims[l]:sum_dims[l+1], sum_dims[ll]:sum_dims[ll+1]])
            H[sum_dims[ll]:sum_dims[ll + 1],
              sum_dims[l]:sum_dims[l + 1]] = H[sum_dims[l]:sum_dims[l + 1],
                                               sum_dims[ll]:sum_dims[ll + 1]].T

    return H
Beispiel #11
0
def handle(content, server):
    """
    Handles the content returned from a lp poll
    """
    try:
        content = json.loads(content)
    except:
        logging.debug(traceback.format_exc())
        gevent.sleep(5)
        return
    block = Conversion.extract_block(content)

    if block not in blocks:
        blocks[block] = {}
        bitHopper.LongPoll.trigger(content)
    else:
        #Some pools like p2pool need to send a lot of longpolls
        #Always pass through the longpoll
        bitHopper.LongPoll.trigger(content)
        return

    if server not in blocks[block]:
        blocks[block][server] = int(time.time())
        logging.debug('%s, %s' % (server, block))
    gevent.spawn(learn_block, blocks, block)
Beispiel #12
0
 def testFahrenheitToKelvin(self):
     for val in self.knownvals:
         f = val[1]
         k = val[2]
         expect = conversions.convertFahrenheitToKelvin(f)
         self.assertEqual(expect,
                          k,
                          msg=('{} degrees K '
                               'is not equal to {}'
                               ' degrees K.').format(f, k))
Beispiel #13
0
 def testFahrenheitToCelsius(self):
     for val in self.knownvals:
         f = val[1]
         c = val[0]
         expect = conversions.convertFahrenheitToCelsius(f)
         self.assertEqual(expect,
                          c,
                          msg=('{} degrees C '
                               'is not equal to {}'
                               ' degrees C.').format(f, c))
Beispiel #14
0
 def testCelsiustoKelvin(self):
     for val in self.knownvals:
         c = val[0]
         k = val[2]
         expect = conversions.convertCelsiusToKelvin(c)
         self.assertEqual(expect,
                          k,
                          msg=('{} degrees K '
                               'is not equal to {}'
                               ' degrees K.').format(c, k))
Beispiel #15
0
 def testKelvinToCelsius(self):
     for val in self.knownvals:
         k = val[2]
         c = val[0]
         expect = conversions.convertKelvinToCelsius(k)
         self.assertEqual(expect,
                          c,
                          msg=('{} degrees C '
                               'is not equal to {}'
                               ' degrees C.').format(k, c))
Beispiel #16
0
 def testCelsiustoFahrenheit(self):
     for val in self.knownvals:
         c = val[0]
         f = val[1]
         expect = conversions.convertCelsiusToFahrenheit(c)
         self.assertEqual(expect,
                          f,
                          msg=('{} degrees F '
                               'is not equal to {}'
                               ' degrees F.').format(c, f))
Beispiel #17
0
 def testKelvinToFahrenheit(self):
     for val in self.knownvals:
         k = val[2]
         f = val[1]
         expect = conversions.convertKelvinToFahrenheit(k)
         self.assertEqual(expect,
                          f,
                          msg=('{} degrees F '
                               ' is not equal to {}'
                               ' degrees F.').format(k, f))
Beispiel #18
0
        def Launch(name):
            #S'il n'y a rien, utilisateur a appuyé sans avoir selectionné de programme a lancer
            if (len(name) == 0):
                return

            root.destroy()
            #éxécute une fenêtre choisie dans la combobox
            if (name == "Multi-Compteur"):
                Comptmots.Comptmot()

            if (name == "Mini-jeu"):
                Jeu.jeu()

            if (name == "Feedback"):
                feedback.fb()

            if (name == "Calcul de la moyenne"):
                Moyenne.Moyenne()

            if (name == "Convertisseur d'unité"):
                Conversion.conversion()
Beispiel #19
0
def hill_cipher_algorithm(message_to_encrypt, key_size):
    success = False
    count = 0
    while success == False:
        message_matrix = Conversion.generate_cipher_matrix(
            message_to_encrypt, key_size)
        encryption_key = Generate_Encryption_Key.generate_encryption_key(
            key_size)
        encrypted_message = Conversion.encrypt_message(encryption_key,
                                                       message_matrix)
        decryption_key = Generate_Encryption_Key.generate_sympy_decryption_key(
            encryption_key)
        decrypted_message = Conversion.decrypt_message(decryption_key,
                                                       encrypted_message)
        if np.array_equiv(message_matrix, decrypted_message) == True:
            success = True
        else:
            pass
        count += 1

    return count
Beispiel #20
0
def auditoryFilterbank(x_t, fs, win_size, hop_size, min_freq, max_freq,
                       num_subbands):
    # ============================================================================

    overlap = win_size - hop_size
    windowF = np.hamming(win_size)
    s, f, t, image = plt.specgram(x_t,
                                  Fs=fs,
                                  window=windowF,
                                  NFFT=win_size,
                                  noverlap=overlap,
                                  mode='magnitude')
    #s = abs(s);
    fs_fft = hop_size / fs
    min_mel = convert.hz2mel(min_freq)
    max_mel = convert.hz2mel(max_freq)
    mel_c = np.linspace(min_mel, max_mel, num=num_subbands + 2)
    freq_c = convert.mel2hz(mel_c)

    #Rounding to closest bin
    bin_c = convert.find_nearest(f, freq_c)
    bank = np.zeros((np.size(f), num_subbands))
    bank_norm = np.zeros((np.size(f), num_subbands))

    #Constructing individual filterbank
    for i in range(0, num_subbands):
        pre = np.zeros(((bin_c[i])), dtype=np.int)
        up = np.linspace(0, 1, num=(bin_c[i + 1] - bin_c[i]) + 1)
        down = np.linspace(1, 0, num=(bin_c[i + 2] - bin_c[i + 1]) + 1)
        down = down[1:]
        post = np.zeros(((np.size(f) - bin_c[i + 2]) - 1), dtype=np.int)
        bank[:, i] = np.concatenate([pre, up, down, post])
        bank_norm[:, i] = np.divide(bank[:, i], (np.sum(bank[:, i])))
        #plt(bank_norm)

    #Log magnitude & multiplication with filterbank
    logmag = 20 * np.log10(np.dot(np.transpose(s), (bank_norm)))

    return logmag, fs_fft
Beispiel #21
0
def smart_random(S, dims, R):
    """
    This function generates 1 + int(sqrt(prod(dims))) samples of random possible initializations. The closest to S is
    saved. This method draws R random points in S and generates a tensor with rank <= R from them. The distribution is
    such that it tries to maximize the energy of the sampled tensor, so the error is minimized.
    
    Inputs
    ------
    S: float array
        The core tensor of the MLSVD of T.
    dims: list or tuple
        The dimensions (shape) of S.
    R: int
        The desired rank.
        
    Outputs
    -------
    best_factors: list of float 2D arrays
    """

    # Initialize auxiliary values and arrays.
    dims = array(dims)
    samples = 1 + int(sqrt(prod(dims)))
    best_error = inf
    S1 = cnv.unfold(S, 1)
    Ssize = norm(S)

    # Start search for a good initial point.
    for sample in range(samples):
        init_factors = smart_sample(S, dims, R)
        # Compute error.
        S1_init = empty(S1.shape)
        S1_init = cnv.cpd2unfold1(S1_init, init_factors)
        rel_error = norm(S1 - S1_init) / Ssize
        if rel_error < best_error:
            best_error = rel_error
            best_factors = init_factors.copy()

    return best_factors
Beispiel #22
0
def hill_cipher_algorithm(message_to_encrypt, key_size):
    success = False
    while success == False:
        message_matrix = Conversion.generate_cipher_matrix(
            message_to_encrypt, key_size)
        encryption_key = Generate_Encryption_Key.generate_encryption_key(
            key_size)
        print('Encryption Key:')
        print(encryption_key)
        print()
        encrypted_matrix = Conversion.encrypt_message(encryption_key,
                                                      message_matrix)

        decryption_key = Generate_Encryption_Key.generate_sympy_decryption_key(
            encryption_key)

        print('Decryption Key:')
        print(decryption_key)
        print()
        decrypted_matrix = Conversion.decrypt_message(decryption_key,
                                                      encrypted_matrix)
        if np.array_equiv(message_matrix, decrypted_matrix) == True:
            success = True
        else:
            pass
    folder = os.getcwd() + '//Encryption'
    if not os.path.exists(folder):
        os.mkdir(folder)
    encrypted_text = Conversion.convert_matrix_to_text(encrypted_matrix)
    decrypted_text = Conversion.convert_matrix_to_text(decrypted_matrix)

    print('Encrypted Text:')
    print(encrypted_text)
    print()
    print('Decrypted Text:')
    print(decrypted_text)
    write_encrypted_message(encrypted_text, decryption_key, encryption_key,
                            folder)
def run():
    x = InputNumbers.input_numbers1()
    y = InputNumbers.input_numbers2()
    
    print ("\nFirst decimal number: " +str (x))
    print ("Second decimal number: " +str (y))
    
    binary_num1 = (Conversion.bitToByte(''.join(map(str,Conversion.dectobin(x)[::-1]))))
    print("\nFirst decimal number in binary:  " + str(binary_num1))
    
    binary_num2 = (Conversion.bitToByte(''.join(map(str,Conversion.dectobin(y)[::-1]))))
    print("Second decimal number in binary: " + str(binary_num2))

    final_sum = str(BinaryAddition.addition(binary_num1,binary_num2))

    print("\nThe bitwise addition of above binary numbers is:" + final_sum)
    
    while True:
        restart = input("\nDo you want to start again? (Y/N): ")
        print(end='')
        if restart == "Y":
            run()
        elif restart == "N":
            sys.exit()
Beispiel #24
0
def matvec(factors, P2, P_VT_W, result, result_tmp, z, A, B, dims, sum_dims):
    """
    Makes the matrix-vector computation (Jf^T * Jf)*v.
    """

    L = len(factors)
    R = factors[0].shape[1]

    result_tmp *= 0
    result_tmp = matvec_inner(A, P2, P_VT_W, result_tmp, L)

    for l in range(L):
        dot(factors[l], result_tmp[l], out=result[l])
        result[l] += B[l]
        z[sum_dims[l]:sum_dims[l + 1]] = cnv.vec(
            result[l], z[sum_dims[l]:sum_dims[l + 1]], dims[l], R)

    return z
Beispiel #25
0
def handle(content, server):
    """
    Handles the content returned from a lp poll
    """
    try:
        content = json.loads(content)
    except:
        logging.debug(traceback.format_exc())
        gevent.sleep(5)
        return
    block = Conversion.extract_block(content)

    if block not in blocks:
        blocks[block] = {}
        bitHopper.LongPoll.trigger(content)

    if server not in blocks[block]:
        blocks[block][server] = int(time.time())
        logging.debug("%s, %s" % (server, block))
Beispiel #26
0
# Encryption

# padding with ~ to make it divisible by 8
length = len(plaintext)
if length % 8 != 0:
    remain = 8 - (length % 8)
    for i in range(0, remain):
        plaintext = plaintext + "~"

length = len(plaintext)
chunks = length // 8
cipher_text = ""

# convert key to bit
key_in_64bits = Conversion.tobits(key)

# convert key into 56 bits
key_in_56bits = Conversion.to56bits(key_in_64bits)

SHIFT = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
keys = []
for j in range(0, 16):
    # get first and second half after shifting
    firsthalf_from_key = Conversion.getfirsthalf(key_in_56bits, SHIFT[j])
    secondhalf_from_key = Conversion.getsecondhalf(key_in_56bits, SHIFT[j])
    current_key = []
    for k in range(0, 28):
        current_key.append(firsthalf_from_key[k])
    for k in range(0, 28):
        current_key.append(secondhalf_from_key[k])
Beispiel #27
0
 def doLogic(self) :
     output_ = self.doLogic_(Conversion.getPointValues(self.input_))
     for out in self.output :
         out.pushState(output_)
Beispiel #28
0
def mlsvd(T, Tsize, R, options):
    """
    This function computes a truncated MLSVD of tensors of any order. The output is such that T = (U_1,...,U_L)*S, and
    UT is the list of the transposes of U.
    The parameter n_iter of the randomized SVD is set to 2. It is only good to increase this value when the tensor has
    much noise. Still this issue is addressed by the low rank CPD approximation, so n_iter=2 is enough.

    Inputs
    ------
    T: float array
        Objective tensor in coordinates.
    Tsize: float
        Frobenius norm of T.
    R: int
        An upper bound for the multilinear rank of T. Normally one will use the rank of T.
    options: class with the parameters previously defined.

    Outputs
    -------
    S: float array
        Core tensor of the MLSVD.
    U: list of float 2-D arrays
        List with truncated matrices of the original U.
    T1: float 2-D arrays
        First unfolding of T.
    sigmas: list of float 1-D arrays
        List with truncated arrays of the original sigmas.
    """

    # INITIALIZE RELEVANT VARIABLES.

    sigmas = []
    U = []
    
    # Verify if T is sparse, in which case it will be given as a list with the data.
    if type(T) == list:
        data, idxs, dims = T
    else:
        dims = T.shape
    L = len(dims) 

    # Set options.
    options = aux.make_options(options, L)
    trunc_dims = options.trunc_dims
    display = options.display
    mlsvd_method = options.mlsvd_method
    tol_mlsvd = options.tol_mlsvd
    if type(tol_mlsvd) == list:
        if L > 3:
            tol_mlsvd = tol_mlsvd[0]
        else:
            tol_mlsvd = tol_mlsvd[1]
    gpu = options.gpu
    if gpu:
        import pycuda.gpuarray as gpuarray
        import pycuda.autoinit
        from skcuda import linalg, rlinalg

    # tol_mlsvd = -1 means no truncation and no compression, that is, the original tensor.
    if tol_mlsvd == -1:
        T1 = cnv.unfold(T, 1)
        U = [identity(dims[l]) for l in range(L)]
        sigmas = [ones(dims[l]) for l in range(L)]
        if display > 2 or display < -1:
            return T, U, T1, sigmas, 0.0
        else:
            return T, U, T1, sigmas
    
    # T is sparse.        
    elif type(T) == list:
        for l in range(L):
            Tl = cnv.sparse_unfold(data, idxs, dims, l+1)
            if l == 0:
                T1 = cnv.sparse_unfold(data, idxs, dims, l+1)
            mlsvd_method = 'sparse'
            U, sigmas, Vlt, dim = compute_svd(Tl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, L, l)

        # Compute (U_1^T,...,U_L^T)*T = S.
        new_dims = [U[l].shape[1] for l in range(L)]
        UT = [U[l].T for l in range(L)]
        S = mlinalg.sparse_multilin_mult(UT, data, idxs, new_dims)

    # Compute MLSVD base on sequentially truncated method.
    elif mlsvd_method == 'seq':
        S_dims = copy(dims)
        S = T
        for l in range(L):
            Sl = cnv.unfold(S, l+1)
            if l == 0:
                T1 = cnv.unfold_C(S, l+1)
            U, sigmas, Vlt, dim = compute_svd(Sl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, L, l)

            # Compute l-th unfolding of S truncated at the l-th mode.
            Sl = (Vlt.T * sigmas[-1]).T
            S_dims[l] = dim
            S = empty(S_dims, dtype=float64)
            S = cnv.foldback(S, Sl, l+1)

    # Compute MLSVD based on classic method.
    elif mlsvd_method == 'classic':
        for l in range(L):
            Tl = cnv.unfold(T, l+1)
            if l == 0:
                T1 = cnv.unfold_C(T, l+1)
            U, sigmas, Vlt, dim = compute_svd(Tl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, L, l)

        # Compute (U_1^T,...,U_L^T)*T = S.
        UT = [U[l].T for l in range(L)]
        S = mlinalg.multilin_mult(UT, T1, dims)

    # Specific truncation is given by the user.
    if type(trunc_dims) == list:
        slices = []
        for l in range(L):
            slices.append(slice(0, trunc_dims[l]))
            if trunc_dims[l] > U[l].shape[1]:
                print('trunc_dims[', l, '] =', trunc_dims[l], 'and U[', l, '].shape =', U[l].shape)
                sys.exit('Must have trunc_dims[l] <= min(dims[l], R) for all mode l=1...' + str(L))
            U[l] = U[l][:, :trunc_dims[l]]
        S = S[tuple(slices)]

    # Compute error of compressed tensor.
    if display > 2 or display < -1:
        if type(T) == list:
            best_error = mlinalg.compute_error(T, Tsize, S, U, dims)
        else:
            S1 = cnv.unfold(S, 1)
            best_error = mlinalg.compute_error(T, Tsize, S1, U, S.shape)
        return S, U, T1, sigmas, best_error

    return S, U, T1, sigmas
Beispiel #29
0
'''
Author:Daniel Ramirez
TFG: Steganography in TCP/IP protocols
Main.py
[email protected]
Development by using scapy a Python Framework
'''

#!/usr/bin/env python
from scapy.all import *
from Steganography import *
from Conversion import *

if __name__ == "__main__":
    try:
        print "########## [Steganography] ##########"
        a=steg()
        a.OpenFile()
        a.ConversiontoInteger()
        a.SendingPacket()
        print "########## [Recovering Message] ##########"
        #Comentar codigo
        b=Conversion()
        b.converter()
    except Exception as e:
        print e
Beispiel #30
0
 def __repr__(self) :
     return "(point{}: state:{}; conns:{})".format(self.id, Conversion.getStateAsStr(self), connections[self])
Beispiel #31
0
def starting_point(T, Tsize, S, U, R, ordering, options):
    """
    This function generates a starting point to begin the iterations. There are three options:
        - list: the user may give a list with the factor matrices to be used as starting point.
        - 'random': the entries the factor matrices are generated by the normal distribution with mean 0 and variance 1.
        - 'smart_random': generates a random starting point with a method based on the MLSVD which always guarantee a 
           small relative error. Check the function 'smart_random' for more details about this method.
        - 'smart': works similar as the smart_random method, but this one is deterministic and generates the best rank-R
           approximation based on the MLSVD.
    
    Inputs
    ------
    T: float array
    Tsize: float
    S: float array
        The core tensor of the MLSVD of T.
    U: list of float 2D arrays
        Each element of U is a orthogonal matrix of the MLSVD of T.
    R: int
        the desired rank.
    ordering: list of ints
       Since the cpd may change the index ordering, this list may be necessary if the user gives an initialization,
       which will be based on the original ordering.
    options: class
    
    Outputs
    -------
    init_factors: list of float 2D arrays
    """

    # Extract all variable from the class of options.
    initialization = options.initialization
    c = options.factors_norm
    symm = options.symm
    display = options.display
    dims = S.shape
    L = len(dims)

    if type(initialization) == list:
        init_factors = [dot(U[l].T, initialization[ordering[l]]) for l in range(L)]

    elif initialization == 'random':
        init_factors = [randn(dims[l], R) for l in range(L)]

    elif initialization == 'smart_random':
        init_factors = smart_random(S, dims, R)

    elif initialization == 'smart':
        init_factors = smart(S, dims, R)

    else:
        sys.exit('Error with init parameter.')

    if type(initialization) != list:

        # Depending on the tensor, the factors may have null entries. We want to avoid that. The solution is to 
        # introduce a very small random noise.
        init_factors = clean_zeros(init_factors, dims, R)    
        
        # Make all factors balanced.
        init_factors = cnv.equalize(init_factors, R)

        # Apply additional transformations if requested.
        init_factors = cnv.transform(init_factors, symm, c)

    if display > 2 or display < -1:
        S_init = cnv.cpd2tens(init_factors)
        if type(T) == list:
            rel_error = mlinalg.compute_error(T, Tsize, S_init, U, dims)
        else:
            S1_init = cnv.unfold(S_init, 1)
            rel_error = mlinalg.compute_error(T, Tsize, S1_init, U, dims)
        return init_factors, rel_error

    return init_factors
Beispiel #32
0
#plt.scatter([point[1] for point in points], [point[0] for point in points], s=0.2)
#plt.show()

# Ignoring the distorted (?) regions.
#points_truncated = [point for point in points if 2 <= point[1] <= 4.5 and point[0] > 3105]
#plt.scatter([point[1] for point in points_truncated], [point[0] for point in points_truncated], s=0.2)
#plt.show()

h, bin_edges = Detection.points_histogram(
    [point[0] for point in points_truncated])
#plt.hist([point[0] for point in points_truncated], bin_edges)
#plt.show()

# To do: the inclusion threshold would be a good thing to expose to the user
grooves = Detection.points_to_grooves(h, bin_edges, 1000, points_truncated)

irregular_audio = Conversion.Stylus()

for i, groove in enumerate(grooves):
    #plt.scatter(groove.get_theta_axis(), groove.get_rho_axis(), s=0.2), plt.show()
    irregular_audio.append(Conversion.Stylus(groove, i + 1))

# print("Max Gap: " + str(irregular_audio.get_max_gap()))
plt.scatter(range(len(irregular_audio.data)), irregular_audio.data,
            s=0.2), plt.show()

# audio = DataConversion.Audio(48000, irregular_audio)
# plt.scatter([i for i in range(len(audio.data))], audio.data, s=0.2), plt.show()

Conversion.audio_to_wave(irregular_audio.data)
Beispiel #33
0
import Conversion
import Comparisons
from UserInterface import UserInterface
from APIFunctions import Path
import PySimpleGUI as sg

totalCarbon = 0
tripCarbon = 0
path = Path()
path.GUI.SecondInterface()
kwh = path.GUI.kwh * 12
gallonsPropane = path.GUI.gallonsPropane * 12
cubicFeetNatGas = path.GUI.cubicFeetNatGas * 12
gallonsHeatOil = path.GUI.gallonsHeatOil * 12
distance = path.distance
distance = Conversion.MetersToMiles(distance)
totalDistance = path.GUI.carDistance
time = path.time
fuelEconomy = path.GUI.fuelEconomy
diesel = path.GUI.diesel
airDistance = path.GUI.airDistance
gallonsDriven = Conversion.GallonsUsed(fuelEconomy, distance)
totalGallonsDriven = Conversion.GallonsUsed(fuelEconomy, totalDistance)
totalCarbon += Conversion.ElecToCO2(kwh)
totalCarbon += Conversion.HeatingOilToCO2(gallonsHeatOil)
totalCarbon += Conversion.NatGasToCO2(cubicFeetNatGas)
totalCarbon += Conversion.PropaneToCO2(gallonsPropane)
if diesel:
    tripCarbon += Conversion.DieseltoCO2(gallonsDriven)
    totalCarbon += Conversion.DieseltoCO2(totalGallonsDriven)
else:
Beispiel #34
0
from DFA import DFA
import Conversion

# Get a data from JSON file
dfa = Conversion.collectData();

# Draw an input data as a diagram and
dfa.drawAutomata('Initial DFA', "DFA1")
Beispiel #35
0
def als(T, factors, R, options):
    """
    This function uses the ALS method to compute an approximation of T with rank R. 

    Inputs
    ------
    T: float array
    factors: list of float 2-D array
        The factor matrices to be used as starting point.
    R: int. 
        The desired rank of the approximating tensor.
    options: class
        See the function cpd for more information about the options available.
    
    Outputs
    -------
    factors: list of float 2-D array
        The factor matrices of the CPD of T.
    step_sizes: float 1-D array
        Distance between the computed points at each iteration.
    errors: float 1-D array
        Error of the computed approximating tensor at each iteration. 
    improv: float 1-D array
        Improvement of the error at each iteration. More precisely, the difference between the relative error of the
        current iteration and the previous one.
    gradients: float 1-D array
        Gradient of the error function at each iteration.
    stop: 0, 1, 2, 3, 4, 5, 6 or 7
        This value indicates why the function stopped. See the function dGN for more details.
    """

    # INITIALIZE RELEVANT VARIABLES

    # Extract all relevant variables from the class of options.
    maxiter = options.maxiter
    tol = options.tol
    tol_step = options.tol_step
    tol_improv = options.tol_improv
    tol_grad = options.tol_grad
    symm = options.symm
    display = options.display
    factors_norm = options.factors_norm

    # Verify if some factor should be fixed or not. This only happens when the bicpd function was called.
    L = len(factors)
    fix_mode = -1
    orig_factors = [[] for l in range(L)]
    for l in range(L):
        if type(factors[l]) == list:
            fix_mode = l
            orig_factors[l] = factors[l][0].copy()
            factors[l] = factors[l][0]

    # Set the other variables.
    Tsize = norm(T)
    error = 1
    best_error = inf
    stop = 5
    const = 1 + int(maxiter / 10)

    # INITIALIZE RELEVANT ARRAYS

    x = concatenate([factors[l].flatten('F') for l in range(L)])
    step_sizes = empty(maxiter)
    errors = empty(maxiter)
    improv = empty(maxiter)
    gradients = empty(maxiter)
    best_factors = [copy(factors[l]) for l in range(L)]

    # Compute unfoldings.
    Tl = [cnv.unfold(T, l + 1) for l in range(L)]
    T1_approx = empty(Tl[0].shape, dtype=float64)

    if display > 1:
        if display == 4:
            print('   ', '{:^9}'.format('Iteration'),
                  '| {:^11}'.format('Rel error'),
                  '| {:^11}'.format('Step size'),
                  '| {:^11}'.format('Improvement'),
                  '| {:^11}'.format('norm(grad)'))
        else:
            print('   ', '{:^9}'.format('Iteration'),
                  '| {:^9}'.format('Rel error'),
                  '| {:^11}'.format('Step size'),
                  '| {:^10}'.format('Improvement'),
                  '| {:^10}'.format('norm(grad)'))

    # START ALS ITERATIONS

    for it in range(maxiter):
        # Keep the previous value of x and error to compare with the new ones in the next iteration.
        old_x = x
        old_error = error

        # ALS iteration call.
        factors = als_iteration(Tl, factors, fix_mode)
        x = concatenate([factors[l].flatten('F') for l in range(L)])

        # Transform factors.
        factors = cnv.transform(factors, symm, factors_norm)
        # Some mode may be fixed when the bicpd is called.
        if L == 3:
            for l in range(L):
                if fix_mode == l:
                    factors[l] = copy(orig_factors[l])

        # Compute error.
        T1_approx = cnv.cpd2unfold1(T1_approx, factors)
        error = crt.fastnorm(Tl[0], T1_approx) / Tsize

        # Update best solution.
        if error < best_error:
            best_error = error
            for l in range(L):
                best_factors[l] = copy(factors[l])

        # Save relevant information about the current iteration.
        step_sizes[it] = norm(x - old_x)
        errors[it] = error
        gradients[it] = np.abs(old_error - error) / step_sizes[it]
        if it == 0:
            improv[it] = errors[it]
        else:
            improv[it] = np.abs(errors[it - 1] - errors[it])

        # Show information about current iteration.
        if display > 1:
            if display == 4:
                print('    ', '{:^8}'.format(it + 1),
                      '| {:^10.5e}'.format(errors[it]),
                      '| {:^10.5e}'.format(step_sizes[it]),
                      '| {:^10.5e}'.format(improv[it]),
                      '| {:^11.5e}'.format(gradients[it]))
            else:
                print('   ', '{:^9}'.format(it + 1),
                      '| {:^9.2e}'.format(errors[it]),
                      '| {:^11.2e}'.format(step_sizes[it]),
                      '| {:^11.2e}'.format(improv[it]),
                      '| {:^10.2e}'.format(gradients[it]))

        # Stopping conditions.
        if it > 1:
            if errors[it] < tol:
                stop = 0
                break
            if step_sizes[it] < tol_step:
                stop = 1
                break
            if improv[it] < tol_improv:
                stop = 2
                break
            if gradients[it] < tol_grad:
                stop = 3
                break
            if it > 2 * const and it % const == 0:
                # Let const=1+int(maxiter/10). Comparing the average errors of const consecutive iterations prevents
                # the program to continue iterating when the error starts to oscillate without decreasing.
                mean1 = mean(errors[it - 2 * const:it - const])
                mean2 = mean(errors[it - const:it])
                if mean1 - mean2 <= tol_improv:
                    stop = 4
                    break
                # If the average improvements is too small compared to the average errors, the program stops.
                mean3 = mean(improv[it - const:it])
                if mean3 < 1e-3 * mean2:
                    stop = 7
                    break
            # Prevent blow ups.
            if error > max(1, Tsize**2) / (1e-16 + tol):
                stop = 6
                break

    # SAVE LAST COMPUTED INFORMATION

    errors = errors[0:it + 1]
    step_sizes = step_sizes[0:it + 1]
    improv = improv[0:it + 1]
    gradients = gradients[0:it + 1]

    return factors, step_sizes, errors, improv, gradients, stop
Beispiel #36
0
def decodePacket(line, wayPointCount):
    ret = 1                    #returns the the type of Packet, 0 for bad packet
    package= {}
    wayList = []                #storage for waypoints from truck
    
    line.replace(" ","")        #gets rids of leading spaces in truck output
    tmpByteCount = len(line)
    
    dline = line.split(';')
    
    if (dline[0] == '~'):     #handshake
        ret = 1    
    elif(dline[0] == 'G'):    #ground station packets
        
        if(tmpByteCount == 14 ):    #Packet A
            
            ret = 2
            package['version'] = float(dline[1])                #Version #
            if(str(package['version']).__len__() <= 0):
                ret = 0                #bad packet
            package['alive'] = float(Conversion.timeAliveConvert(line[9:11]))
            if(str(package['alive']).__len__() <= 0):
                ret = 0                #bad packet             #time alive in ms
            package['battery_voltage'] = float(Conversion.voltageConversion(line[12:]))
            if(str(package['battery_voltage']).__len__() <= 0):
                ret = 0                #bad packet  
            
        if(tmpByteCount==22):    #Packet B
            ret = 3
            
            tmp = Conversion.string2gps(dline[1]).split(',')        #seperate direction
            package['latitude'] = tmp[0]                #Latitude
                        
            if(str(package['latitude']).__len__() <= 0):
                ret = 0                #bad packet
            tmp = Conversion.string2gps(dline[2]).split(',')    #seperate direction
            package['longitude'] = tmp[0]                #Longitude
            if(str(package['longitude']).__len__() <= 0):
                ret = 0                #bad packet
            package['altitude'] = Conversion.string2gps(dline[3])                #Altitude
            if(str(package['altitude']).__len__() <= 0):
                ret = 0                #bad packet
            package['heading'] = Conversion.string2gps(dline[4])                #Heading
            if(str(package['heading']).__len__() <= 0):
                ret = 0                #bad packet
                
            package['air_pressure'] =((float(Conversion.string2gps(dline[5]))/1023) + .095)/.009                #Heading
            if(str(package['air_pressure']).__len__() <= 0):
                ret = 0                #bad packet
            
            #package['xpos'] = dline[4]                #X-Position
            #package['ypos'] = dline[5]                #Y-position
            
        if(dline[1]=='c'):  #antenna packet
            ret =6
        
    else:    #vehicle packets

        try:
            if(len(dline)==9):
                
                ret = 4
                package['mode'] = int((dline[0])[1])
                if(str(package['mode']).__len__() <= 0):
                    ret = 0                #bad packet
                package['speed'] = float((dline[1])[1:len(dline[1])])
                if(str(package['speed']).__len__() <= 0):
                    ret = 0                #bad packet
                package['heading'] = float((dline[2])[1:len(dline[2])])
                if(str(package['heading']).__len__() <= 0):
                    ret = 0                #bad packet      
                package['temperature'] = float((dline[3])[1:len(dline[3])])
                if(str(package['temperature']).__len__() <= 0):
                    ret = 0                #bad packet
                package['range'] = float((dline[4])[1:len(dline[4])])
                if(str(package['range']).__len__() <= 0):
                    ret = 0                #bad packet                      
                package['battery_voltage'] = float((dline[5])[1:len(dline[5])])
                if(str(package['battery_voltage']).__len__() <= 0):
                    ret = 0                #bad packet
                package['latitude'] = float((dline[6])[1:len(dline[6])])
                if(str(package['latitude']).__len__() <= 0):
                    ret = 0                #bad packet
                package['longitude'] = float((dline[7])[1:len(dline[7])])
                if(str(package['longitude']).__len__() <= 0):
                    ret = 0                #bad packet
        except Exception:
            ret = 0        #bad packet
        
            if(str(dline[0]).rfind("CURRENT WAYPOINTSIndex:")!=-1):
                ret=5                #waypoints
                tmpWayPointCount = str(((dline[0])[24:25])).rstrip()  #get waypoint count return from vechile  
                if ((tmpWayPointCount+1) !=wayPointCount):            #test for consistency between variables
                    print "Error: Waypoint counts do not match!"
                    wayPointCount= int(tmpWayPointCount+1)            #correct inconsistencies
                (dline[0]).replace ( 'CURRENT WAYPOINTSIndex: 0', '' ) #remove misc. characters
                (dline[0]).lstrip()
                for w in range(0,wayPointCount):                         #create test waylist
                    wayList.append(Waypoint((dline[w])[1:], (dline[w+1])[1:],None))
                
                return(ret,wayList)            
    return (ret,package)