Example #1
0
async def oneloop(dut):
    """perf oneloop test"""

    t = TicToc()
    tb = settings()

    await reset(dut)

    clkobj = Clock(dut.clk, tb.period, 'us')
    cocotb.fork(clkobj.start())

    t.tic()
    k = 0
    for cycle in range(tb.dinArate * tb.npoints):
        await (RisingEdge(dut.clk))
        if (cycle % tb.dinArate) == 0:
            k = k + 1
            dut.dinA <= 1
            if (k % 100 == 0):
                dut._log.info("Sim progress...{} %".format(
                    int(100 * float(k) / tb.npoints)))
        else:
            dut.dinA <= 0

        if (cycle % tb.dinBrate) == 0:
            dut.dinB <= 1
        else:
            dut.dinB <= 0

    t.toc()
    print(t.elapsed)
def main(individuals_number):

    global individuals
    individuals = individuals_number
    print_variable_info()

    t = TicToc()
    t.tic()

    # Initialize a population
    population = initialize()

    population = [
        (calculate_fitness(i), i) for i in population
    ]  # Calculates the fitness of each individual, and stores it in pairs ordered in the form (5 , [1,2,1,1,1,4,1,8,9,4,1])

    initial_population = sorted(population, key=lambda x: x[0])

    # Evolves the population
    for i in range(generations):
        print("→→", i)
        population = selection_and_reproduction(population)
        print("←←")

    # Print the results
    print_results(initial_population, population)

    t.toc()
Example #3
0
def run_tsp(difficulty, file_name):
    t = TicToc()
    all_cities = my_utils.generate_cities_dict(difficulty)
    cities_map = cities_visualitation.CitiesMap(all_cities)
    tsp = TravelingSalesmanProblem(all_cities, (0, ), ())
    t.tic()
    best_known_solution_value = 0.0

    if difficulty == 'big':
        best_known_solution_value = params.big_best_known_solution
    elif difficulty == 'medium':
        best_known_solution_value = params.medium_best_known_solution
    elif difficulty == 'small':
        best_known_solution_value = params.small_best_known_solution
    tsp_result, msg = search.astar_search(tsp,
                                          best_known_solution_value,
                                          display=True)
    print(msg)
    t.toc()
    cities_list = cities_visualitation.get_normalized_cities_identification(
        tsp_result.state)
    cities_list_evaluation = (-1) * tsp.value(tsp_result.state)
    cities_map.show_map(list(tsp_result.state))
    cities_map.save_plot(file_name)
    my_utils.save_results(file_name, cities_list_evaluation, cities_list, msg)
Example #4
0
def main():

    t = TicToc()

    t.tic()

    with open("/home/ubuntu/sbmd/station", "rb") as f:
        fileobj = pickle.load(f)

    statit = fileobj[2][int(sys.argv[1])]
    statit = np.flip(statit)

    credfile = "/home/ubuntu/sbmd/dwh.cfg"

    config = configparser.ConfigParser()
    config.read(credfile)

    s3k = config['AWS']['KEY']
    s3ks = config['AWS']['SECRET']

    pool = mp.Pool(mp.cpu_count())

    [pool.apply(load_trains_all, args=(co, s3k, s3ks)) for co in statit]

    ind = sys.argv[1]
    logging.info(f"Gathered conn data succesfully with index {ind}")

    t.toc()
def main():
    #Create the Matrix to be tri-diagonalized
    #n = 12                       #Size of input matrix (nxn)
    #A = SymmMat(n)              #Input matrix. (Hermitian)

    #Hamiltonian of tV Model for L = 4, N = 2, ell=2
    A = -1.0 * np.array(
        ((0, 1, 0, 1, 0, 0), (1, 0, 1, 0, 1, 1), (0, 1, 0, 1, 0, 0),
         (1, 0, 1, 0, 1, 1), (0, 1, 0, 1, 0, 0), (0, 1, 0, 1, 0, 0)))

    #A = -1.0*np.array(((0,0,1,0),
    #                   (0,0,1,0),
    #                   (1,1,0,1),
    #                   (0,0,1,0)))

    #Change print format to decimal instead of scientific notation
    np.set_printoptions(formatter={'float_kind': '{:f}'.format})

    #Transform the matrix A to tridiagonal form via Lanczos
    T = LanczosTri(A)

    #Find Eigenvalues for Real, Symmetric, Tridiagonal Matrix via QR Iteration
    t2 = TicToc()
    t2.tic()
    lam = IPI(T, maxiter=50000)
    t2.toc()
    print("Eigs(T): ", lam)

    #Get eigenpairs of untransformed hermitian matrix A and time the process using blackbox function
    t1 = TicToc()
    t1.tic()
    e_gs_A, gs_A = eigsh(A, k=1, which='SA', maxiter=1000)
    #e_gs_A = NSI(A,maxiter=1000)
    t1.toc()
    print("Eigs(A): ", e_gs_A[0])
Example #6
0
def derotate_image_forloop(startNum,stopNum,dateString):
    for f in range(startNum,stopNum+1): # loop over filenames
        print('----------------------------------------')
        print('Derotating image '+str("{:0>5d}".format(f))+'...')

        t = TicToc() # create instance of timer
        t.tic() # start timer

        # retrieve image
        image, header = fits.getdata(calibrated_trapezium_data_stem+
                                     'step02_dewarped/'+
                                     'lm_'+dateString+'_'+
                                     str("{:0>5d}".format(f))+
                                     '.fits',
                                     0,
                                     header=True)
        # find PA from header
        pa = header['LBT_PARA']
    
        # derotate
        image_derot = rot(image, -pa, [1024,1024], order=3, pivot=False) # axis coord here is just a dummy
    
        # save 
        fits.writeto(calibrated_trapezium_data_stem+
                     'step03_derotate/'+
                     'lm_'+dateString+'_'+
                     str("{:0>5d}".format(f))+
                     '.fits',
                     image_derot, header, overwrite=False)

        t.toc()
        print('------------------------------')
Example #7
0
def main(individuals_number, crossover, mutation):

    global individuals
    individuals = individuals_number
    global crossover_type
    crossover_type = crossover
    global mutation_type_random
    mutation_type_random = mutation

    print_variable_info()

    t = TicToc()
    t.tic()

    # Initialize a population
    population = initialize()

    population = [(fitnes(i), i) for i in population] # Calculates the fitness of each individual, and stores it in pairs ordered in the form (5 , [1,2,1,1,1,4,1,8,9,4,1])

    initial_population = sorted(population, reverse=True)

    # Evolves the population
    for i in range(generations):
        population = selection_and_reproduction(population)
        population = mutate(population)

    # Print the results
    print_results(initial_population, population)

    t.toc()
Example #8
0
def accuracyCalculator(dataSetLoader, crrNeuralNet):
    '''
    Calculate and return accuracy on the given data set using current trained Neural Net
    
    Args: 
        dataSetLoader: given data set loader
        crrNeuralNet: given current trained neural net. 
        
    Returns:
        accuracyForCurrentNet: current accuracy on the given data set by trained net at current epoch
        
    '''

    numOfCorrectPrediction = 0
    numOfTotalDataPoints = 0
    timerObj = TicToc()
    timerObj.tic()
    with torch.no_grad():
        for crrData in dataSetLoader:
            crrInputData, crrLabels = crrData
            predictedOutputs = crrNeuralNet.feedForward(crrInputData)
            _, predictedLabels = torch.max(predictedOutputs.data, 1)
            numOfTotalDataPoints += crrLabels.size(0)
            numOfCorrectPrediction += (
                predictedLabels == crrLabels).sum().item()

    accuracyForCurrentNet = numOfCorrectPrediction / numOfTotalDataPoints
    print('Accuracy of the network on the images(train or test): %d %%' %
          (100 * numOfCorrectPrediction / numOfTotalDataPoints))
    timerObj.toc()
    return accuracyForCurrentNet
Example #9
0
def print_time_operation_took(function):
    """
    Print the time it took to complete a given function

    :param function: the function to run
    """
    t = TicToc()
    function()
    t.toc()
Example #10
0
    async def ocr_translate(request):
        tic_toc = TicToc()

        # Read the image from the web request
        tic_toc.tic()
        image_bytes = await request.read()
        image = imread(image_bytes, pilmode="RGB")
        tic_toc.toc("Read image in")

        results = await get_ocr_results(ctx.ocr, image, ctx.max_height)

        return web.json_response({"results": results})
def _find_matches_in_database(db_value_finder, potential_values):
    matches = []
    tic_toc = TicToc()
    tic_toc.tic()
    print(f'Find potential candiates "{potential_values}" in database {db_value_finder.database}')
    try:
        matching_db_values = db_value_finder.find_similar_values_in_database(potential_values)
        matches = list(map(lambda v: v[0], matching_db_values))
    except Exception as e:
        print(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Error executing a query by the database finder. Error: {e}")

    tic_toc.toc()
    return matches
Example #12
0
def derotate_image_forloop(dateString):

    # obtain the list of files which have been dewarped, and need to de-rotated
    # according to the PA in the FITS headers
    asterism_frames_directory_retrieve = str(
        config["data_dirs"]["DIR_ASTERISM_DEWARP"])
    asterism_frames_pre_derot_names = list(
        glob.glob(os.path.join(asterism_frames_directory_retrieve, "*.fits")))

    for f in range(
            0, len(asterism_frames_pre_derot_names)):  # loop over filenames
        print('----------------------------------------')

        t = TicToc()  # create instance of timer
        t.tic()  # start timer

        # retrieve image
        image, header = fits.getdata(asterism_frames_pre_derot_names[f],
                                     0,
                                     header=True)
        #print(header)

        try:
            # find PA from header
            pa = np.float(header['LBT_PARA'])

            # derotate
            image_derot = rot(im=image,
                              angle=-pa,
                              axis=[1024, 1024],
                              order=3,
                              pivot=False)  # axis coord here is just a dummy

            #print(angle)
            print('Derotating image ' +
                  str(os.path.basename(asterism_frames_pre_derot_names[f])) +
                  '...')

            # save
            fits.writeto(str(config["data_dirs"]["DIR_ASTERISM_DEROT"] + \
                         "derotated_" + \
                         os.path.basename(asterism_frames_pre_derot_names[f])),
                         image_derot, header, overwrite=True)

            t.toc()
            print('----------------------------------------')

        except:
            print("Frame " + str(os.path.basename(asterism_frames_pre_derot_names[f])) + \
                  " has no parallactic angle. Maybe header keyword being sought is wrong?")
Example #13
0
    def train_epoch(self, epoch, X_iter, verbose=0):
        t = TicToc()
        start = t.tic()
        epoch_loss = 0.0
        num_batches = 5

        for idx, inputs in enumerate(X_iter):
            inputs = inputs['input']
            batch_size = inputs.shape[0]

            # Convert to tensors and move to device
            inputs = torch.tensor(inputs).to(self.device)

            # Train batch and get batch loss
            batch_loss = self.train_batch(inputs)
            # Update epoch loss given als batch loss
            epoch_loss += batch_loss

            if verbose != 0:
                print(
                    '[{}] Epoch: {} #batches {}/{}, loss: {:.8f}, learning rates: {:.6f}/{:.6f}'
                    .format(datetime.timedelta(seconds=int(t.toc() - start)),
                            epoch + 1, idx + 1, num_batches,
                            (batch_loss / ((idx + 1) * batch_size)),
                            self.encoder_lr, self.decoder_lr),
                    end='\r')

        return epoch_loss
Example #14
0
    def integrateFittedPeakIntensity(self, spaDict=""):
        inTimer = TicToc()
        inTimer.tic()

        if spaDict == "":
            spaDict = self.SPAResultRawDict

        for frameID, frameDict in spaDict.items():
            numberOfSpot = frameDict["numberOfSpot"]
            totalIntensity = 0

            for spotID in range(int(numberOfSpot)):
                spotDict = frameDict[str(spotID)]

                Am = spotDict["Am"]
                xCenter = spotDict["xCenter"]
                yCenter = spotDict["yCenter"]
                sigma_x = spotDict["sigma_x"]
                sigma_y = spotDict["sigma_y"]
                theta = spotDict["theta"]

                xUpperLimit = xCenter + self.halfCropRange
                xLowerLimit = xCenter - self.halfCropRange

                yUpperLimit = yCenter + self.halfCropRange
                yLowerLimit = yCenter - self.halfCropRange

                spotDict["integratedIntensity"], spotDict["integratedIntensityError"] = dblquad(
                    lambda x, y: fitFunc.gauss2D(x, y, Am, xCenter, yCenter, sigma_x, sigma_y, theta), yLowerLimit,
                    yUpperLimit, lambda x: xLowerLimit, lambda x: xUpperLimit)

                totalIntensity += spotDict["integratedIntensity"]
            frameDict["totalIntensity"] = totalIntensity

            if int(frameID) % 50 == 0:
                print("Integrating Frame ID:", frameID, end=', ')
                inTimer.toc()

        for frameID, frameDict in spaDict.items():
            numberOfSpot = frameDict["numberOfSpot"]
            for spotID in range(int(numberOfSpot)):
                spotDict = frameDict[str(spotID)]
                spotDict["integratedIntensityRatio"] = spotDict["integratedIntensity"] / frameDict["totalIntensity"]

        self.SPAResultRawDict = spaDict

        return self.SPAResultRawDict
Example #15
0
def components(
    nodes
):  #randomize graph and calculate connected components given number of nodes
    G = Graph.Erdos_Renyi(nodes, p=0.3)
    t = TicToc()

    #create an adjacency matrix.
    #An = (sparse(1:nodes,assig,np.ones(1,nodes),nodes,nodes))

    tables = G.components(mode=WEAK)
    largest = max(tables)
    t.tic()
    adj = G.get_adjacency()

    t.toc()

    return (tables, largest, adj, t.tocvalue())
Example #16
0
    def value_iteration(self, rewards, error=1):
        # Initialize the value function
        t_complete_func = TicToc()
        t_complete_func.tic()
        values = np.zeros([self.n_states])
        states_range_value = range(0, self.n_states)
        print "states range value is ", states_range_value
        self.rewards = rewards
        # estimate values
        while True:
            # Temporary copy to check find the difference between new value function calculated & current value function
            # to ensure improvement in value
            self.values_tmp = values.copy()
            t_value = TicToc()
            t_value.tic()
            for q, s in self.map(self.calc_value_for_state,
                                 states_range_value):
                values[s] = q
                # print "\nvalues is ", values[s]
            '''
            for s in range(self.n_states):
                values[s] = max(
                    [sum([P_a[s, a, s1] * (rewards[s] + self.gamma * values_tmp[s1])
                          for s1 in range(self.n_states)])
                     for a in range(self.n_actions)])
            '''
            t_value.toc('Value function section took')
            # print "values ", values[s]
            if max([
                    abs(values[s] - self.values_tmp[s])
                    for s in range(self.n_states)
            ]) < error:
                break
        # generate deterministic policy
        policy = np.zeros([self.n_states])
        for s in range(self.n_states):
            policy[s] = np.argmax([
                sum([
                    self.P_a[s, a, s1] *
                    (self.rewards[s] + self.gamma * values[s1])
                    for s1 in range(self.n_states)
                ]) for a in range(self.n_actions)
            ])

        t_complete_func.toc('Complete function section took')
        return values, policy
Example #17
0
def derotate_image_northern_target(fileNum, ignoreExisting=True):
    # this takes single images (intended for multiprocessing)
    print('----------------------------------------')

    t = TicToc()  # create instance of timer
    t.tic()  # start timer

    if ignoreExisting:  # if the file already exists, skip it
        if os.path.exists(dirTreeStem + depositPiece + fileNameStem +
                          str("{:0>5d}".format(fileNum)) + '.fits'):
            print('Not derotating frame ' + str(fileNum) + '!')
            return

        else:

            if os.path.exists(dirTreeStem + retrievalPiece + fileNameStem +
                              str("{:0>5d}".format(fileNum)) + '.fits'):

                print('Derotating image ' + str("{:0>5d}".format(fileNum)) +
                      '...')
                # retrieve image
                image, header = fits.getdata(
                    dirTreeStem + retrievalPiece + fileNameStem +
                    str("{:0>5d}".format(fileNum)) + '.fits',
                    0,
                    header=True)
                # find PA from header
                pa = header['LBT_PARA']

                # derotate
                image_derot = rot(
                    image,
                    180. - pa - pa_corrxn, [1024, 1024],
                    order=3,
                    pivot=False)  # axis coord here is just a dummy

                # save
                hdu = fits.PrimaryHDU(image_derot, header=header)
                hdulist = fits.HDUList([hdu])
                hdulist.writeto(dirTreeStem + depositPiece + fileNameStem +
                                str("{:0>5d}".format(fileNum)) + '.fits',
                                overwrite=True)

                t.toc()
            print('------------------------------')
Example #18
0
def components(
    nodes
):  #randomize graph and calculate connected components given number of nodes
    G = nx.erdos_renyi_graph(nodes, 0.3)
    t = TicToc()

    #t.tic()
    #create an adjacency matrix.
    #An = (sparse(1:nodes,assig,np.ones(1,nodes),nodes,nodes))

    tables = nx.connected_components(G)
    largest = max(tables)
    t.tic()
    adj = G.adjacency()

    t.toc()

    return (tables, largest, adj, t.tocvalue())
Example #19
0
def conventional_training(dataset, learner):
    print("[*] Training with a conventional optimizer...")
    # Make the training / eval splits
    model = learner
    optimizer = torch.optim.Adam(model.parameters())
    criterion = torch.nn.MSELoss(reduction='sum')
    train_dataset, eval_dataset = dataset[0], dataset[0][1]
    print("[*] Evaluating with random initialization...")
    total_loss = 0
    for i, (x, y) in enumerate(eval_dataset):
        y_pred = model(x.to(device))
        loss = criterion(y_pred, y.to(device))
        # print(f"-> Batch {i}: {loss}")
        total_loss += loss
    avg_loss = total_loss.item() / len(eval_dataset)

    print(f"[*] Average evaluation loss: {avg_loss}")

    t = TicToc()
    model.train()
    t.tic()
    for i in range(2000):
        loss = 0
        optimizer.zero_grad()
        for x, y in train_dataset:
            y_pred = model(x)
            loss += criterion(y_pred, y)
        if i % 100 == 99:
            print(i, loss.item() / len(train_dataset))
            t.toc()
            t.tic()
        loss.backward()
        optimizer.step()

    print("[*] Evaluating...")
    model.eval()
    total_loss = 0
    with torch.no_grad():
        for i, (x, y) in enumerate(eval_dataset):
            y_pred = model(x.to(device))
            loss = criterion(y_pred, y.to(device))
            total_loss += loss
    avg_loss = total_loss.item() / len(eval_dataset)
    print(f"[*] Average evaluation loss: {avg_loss}")
Example #20
0
def inference_worker(graph, input_q):
    t = TicToc()
    while True:
        frame = input_q.get()
        t.tic()
        X = cv2.resize(frame / 255. - 0.5,
                       dim,
                       interpolation=cv2.INTER_NEAREST).astype(numpy.float16)
        graph.LoadTensor(X, None)
        out, _ = graph.GetResult()
        # do some post processing
        spatial_grid = out.reshape(output_dim)
        conf = spatial_grid.squeeze()[:, :, -1]
        idx = numpy.unravel_index(numpy.argmax(conf), conf.shape)
        val = spatial_grid[idx[0], idx[1], :]
        v1 = idx[0] * 32 + val[0] * 32
        v2 = idx[1] * 32 + val[1] * 32
        display_q.put_nowait(((X + 0.5) * 255., (v1, v2)))
        t.toc()
Example #21
0
    def startShiftCenter(self):

        t = TicToc()
        t.tic()
        for filePath in self.fileList:
            whileLoopCounter = 0
            fileName = ntpath.basename(filePath)
            print(fileName)
            while True:
                whileLoopCounter += 1
                numberOfSpot, returnList, imageArray = self.applySEPToImg(filePath)

                if whileLoopCounter > 200:
                    break
                if numberOfSpot == 1:
                    break
                if numberOfSpot > 1:
                    self.searchThreshold = self.searchThreshold * 1.05
                    if self.debugMode:
                        print(fileName, self.searchThreshold, "increase")
                if numberOfSpot < 1:
                    self.searchThreshold = self.searchThreshold * 0.9
                    if self.debugMode:
                        print(fileName, self.searchThreshold, "decrease")

            if whileLoopCounter > 250:
                xCenter, yCenter = lastxCenter, lastyCenter
            else:
                xCenter, yCenter = int(returnList[4]), int(returnList[5])
                lastxCenter, lastyCenter = xCenter, yCenter

            if self.setIntCenter != True:
                intXCenter, intYCenter = xCenter, yCenter
                self.setIntCenter = True

            xShift = intXCenter - xCenter
            yShift = intYCenter - yCenter

            imageArray = shift(imageArray, [yShift, xShift])

            self.saveImArrayTo(imageArray, self.dataFolderName + "ShiftCenterResult/" + fileName)

        t.toc()
def main():
    list_size = 214748365 #int max / 10
    list_one = [2 for x in range(list_size)]
    list_two = [3 for x in range(list_size)]
    list_three = [0 for x in range(list_size)]
    threads = []

    t = TicToc() ## TicToc("name")
    t.tic();
    for i in range(list_size):
        thread = Thread(target=threadAdd, args=[list_one, list_two, list_three, i])
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

    t.toc();
    print(t.elapsed)
    print(list_three)
def main():

    pool = mp.Pool(mp.cpu_count())

    t = TicToc()
    t.tic()

    cities = [
        "München", "Puchheim", "Germering", "Fürstenfeldbruck", "Olching",
        "Gröbenzell", "Murnau", "Miesbach", "Gersthofen", "Wolfratshausen",
        "Starnberg", "Gernlinden", "Eichstätt", "Maisach", "Mammendorf",
        "Schöngeising", "Geltendorf", "Buchenau", "Eichenau", "Holzkirchen",
        "Ebersberg", "Grafing", "Zorneding", "Freising", "Haar",
        "Wasserburg am Inn", "Rosenheim", "Augsburg", "Geretsried",
        "Waldkraiburg", "Ingolstadt", "Donauwörth", "Unterhaching",
        "Taufkirchen", "Erding", "Dachau", "Tutzing", "Feldafing",
        "Schrobenhausen", "Mühldorf am Inn", "Deggendorf", "Landsberg",
        "Landshut", "Nürnberg", "Grafrath", "Gräfelfing",
        "Garmisch-Partenkirchen", "Markt Schwaben", "Icking", "Kempten",
        "Schliersee", "Planegg", "Stockdorf", "Gauting", "Gilching",
        "Türkenfeld", "Petershausen", "Röhrmoos", "Hallbergmoos", "Ismaning",
        "Bayrischzell", "Unterföhring", "Daglfing", "Unterschleißheim",
        "Heimstetten", "Tegernsee", "Lenggries", "Mittenwald", "Aying",
        "Vaterstetten", "Baldham", "Steinebach", "Weßling", "Deisenhofen",
        "Sauerlach", "Otterfing", "Kreuzstraße", "Ottobrunn", "Hohenbrunn",
        "Oberschleißheim", "Eching", "Neufahrn", "Altomünster", "Schwabhausen",
        "Karlsfeld", "Kolbermoor", "Bad Aibling"
    ]

    credfile = "/home/ubuntu/sbmd/dwh.cfg"
    config = configparser.ConfigParser()
    config.read(credfile)

    s3k = config['AWS']['KEY']
    s3ks = config['AWS']['SECRET']

    [pool.apply(load_weather, args=(c, s3k, s3ks)) for c in cities]

    pool.close()

    t.toc()
Example #24
0
def main():
    print("Reading data ... ", end="")
    telemetry = pd.read_csv(os.path.join('..', 'data', 'telemetry.csv'))
    print("Done.")

    print("Parsing datetime...", end="")
    telemetry['datetime'] = pd.to_datetime(telemetry['datetime'], format="%m/%d/%Y %I:%M:%S %p")
    print("Done.")

    
    output_folder = os.path.join('..', 'data')
    window_size = 12  # how many measures to include in rolling average
    sensors = telemetry.columns[2:]
    window_sizes = [window_size] * len(sensors)  # this can be changed to have individual window_sizes for each sensor
    machine_ids = telemetry['machineID'].unique()

    t = TicToc()
    for machine_id in machine_ids:
        df = telemetry.loc[telemetry.loc[:, 'machineID'] == machine_id, :]
        t.tic()
        print("Working on sensor: ")
        for s, sensor in enumerate(sensors):
            N = window_sizes[s]
            print("   %s " % sensor)

            df_ra = rolling_average(df, sensor, N)
            anoms_timestamps = do_ad(df_ra)

            df_anoms = pd.DataFrame(data={'datetime': anoms_timestamps, 'machineID': [machine_id] * len(anoms_timestamps), 'errorID': [sensor] * len(anoms_timestamps)})

            # if this is the first machine and sensor, we initialize a new dataframe
            if machine_id == 1 and s == 0:
                df_anoms_all = df_anoms
            else: # otherwise we append the newly detected anomalies to the existing dataframe
                df_anoms_all = df_anoms_all.append(df_anoms, ignore_index=True)

        df_anoms_all.to_csv(os.path.join(output_folder, 'anoms.csv'), index=False)
        t.toc("Processing machine %s took" % machine_id)
Example #25
0
async def forking(dut):
    """perf forking test"""

    t = TicToc()
    tb = settings()

    await reset(dut)

    clkobj = Clock(dut.clk, tb.period, 'us')
    cocotb.fork(clkobj.start())

    t.tic()
    cocotb.fork(genstrobe(dut.dinA, clkobj, dut.clk, tb.dinArate))
    cocotb.fork(genstrobe(dut.dinB, clkobj, dut.clk, tb.dinBrate))
    dinstrobe = RisingEdge(dut.dinA)

    for cycle in range(tb.npoints):
        await dinstrobe
        if (cycle % 100 == 0):
            dut._log.info("Sim progress...{} %".format(
                int(100 * float(cycle) / tb.npoints)))
    t.toc()
    print(t.elapsed)
Example #26
0
    def value_iteration(self, P_a, rewards, gamma, error=1):
        # Initialize the value function
        values = np.zeros([self.n_states])
        t_complete_func = TicToc()
        t_complete_func.tic()
        # estimate values
        while True:
            # Temporary copy to check find the difference between new value function calculated & current value function
            # to ensure improvement in value
            values_tmp = values.copy()
            t_value = TicToc()
            t_value.tic()
            for s in range(self.n_states):
                values[s] = max([
                    sum([
                        P_a[s, a, s1] * (rewards[s] + gamma * values_tmp[s1])
                        for s1 in range(self.n_states)
                    ]) for a in range(self.n_actions)
                ])
                # print "values ", values[s]
            t_value.toc('Value function section took')
            if max(
                [abs(values[s] - values_tmp[s])
                 for s in range(self.n_states)]) < error:
                break
        # generate deterministic policy
        policy = np.zeros([self.n_states])
        for s in range(self.n_states):
            policy[s] = np.argmax([
                sum([
                    P_a[s, a, s1] * (rewards[s] + gamma * values[s1])
                    for s1 in range(self.n_states)
                ]) for a in range(self.n_actions)
            ])
        t_complete_func.toc('Complete function section took')

        return values, policy
Example #27
0
def main():
    #Create the Matrix to be tri-diagonalized
    n = 6  #Size of input matrix (nxn)
    A = SymmMat(n, density=1)  #Input matrix. (Hermitian,Sparse)
    print(A)

    #Check that the matrix is symmetric. The difference should have no non-zero elements
    assert (A - A.T).nnz == 0

    #Hamiltonian of tV Model for L = 4, N = 2, ell=2
    A = -1.0 * np.array(
        ((0, 1, 0, 1, 0, 0), (1, 0, 1, 0, 1, 1), (0, 1, 0, 1, 0, 0),
         (1, 0, 1, 0, 1, 1), (0, 1, 0, 1, 0, 0), (0, 1, 0, 1, 0, 0)))

    #Test Sparse Matrix
    #A = 1.0*np.diag((1,2,3,4,5,6))
    #A[-1,0] = 5
    #A[0,-1] = 5

    #A = -1.0*np.array(((0,0,1,0),
    #                   (0,0,1,0),
    #                   (1,1,0,1),
    #                   (0,0,1,0)))

    #Change print format to decimal instead of scientific notation
    np.set_printoptions(formatter={'float_kind': '{:f}'.format})

    #Transform the matrix A to tridiagonal form via Lanczos
    T = LanczosTri(A)

    #Find Eigenvalues for Real, Symmetric, Tridiagonal Matrix via QR Iteration
    t2 = TicToc()
    t2.tic()
    lam = NSI(T)

    t2.toc()
    print("Eigs(T) (NSI): ", np.sort(lam)[:-1][0])

    #Get eigenpairs of untransformed hermitian matrix A and time the process using blackbox function
    t1 = TicToc()
    t1.tic()
    e_gs_T, gs_T = eigsh(T, k=n - 1, which='SA', maxiter=1000)
    #e_gs_A = NSI(A,maxiter=1000)
    t1.toc()
    print("Eigs(T) (np.eigsh): ", e_gs_T[0])
    #print("Eigs(A): ",np.sort(e_gs_A[:-1]))

    t3 = TicToc()
    t3.tic()
    e_gs_A, gs_A = eigsh(A, k=n - 1, which='SA', maxiter=1000)
    #e_gs_A = NSI(A,maxiter=1000)
    t3.toc()
    print("Eigs(A) (np.eigsh): ", e_gs_A[0])
Example #28
0
async def get_ocr_results(ocr, image, max_height):
    """Runs OCR on a given image and returns the recognized text,
    text as pinyin, position and dictionary translations."""
    tic_toc = TicToc()

    # Determine the ratio from detection coords to image coords.
    # Downscale if the hight exceeds the max height.
    image_to_screen = [1, 1]
    if image.shape[0] > max_height:
        tic_toc.tic()
        orig_shape = image.shape
        image = resize(image, height=max_height)
        image_to_screen = [
            orig_shape[0] / image.shape[0], orig_shape[1] / image.shape[1]
        ]
        tic_toc.toc("Downscaled image in")

    # Detect sentences in image
    tic_toc.tic()
    print("Image shape:", image.shape, "dtype", image.dtype)
    result, _ = await _awaitable(ocr.run, image)
    sentences = [{"text": r[1], "position": r[0][:2]} for r in result.values()]
    tic_toc.toc("OCR in", restart=True)

    # Translate the detected sentences and store results
    results = []
    for sentence in sentences:
        orig_text = sentence["text"]
        if contains_chinese(orig_text):
            pinyin_text = get_pinyin(orig_text)
            translations = get_all_phrase_translations(orig_text)
            translation_text = "\n".join([
                "%s (%s): %s" % (t[0], get_pinyin(t[0]), ", ".join(t[1]))
                for t in translations
            ])

            position = (int(sentence["position"][0] * image_to_screen[0]),
                        int((sentence["position"][1] * image_to_screen[1]) +
                            20))

            results.append({
                "text": orig_text,
                "position": position,
                "pinyin_text": pinyin_text,
                "translation_text": translation_text
            })

    tic_toc.toc("Translate in")

    return results
Example #29
0
 def wireframe_one_study(instance_id, study_id):
     t = TicToc()
     t.tic()
     study_service = StudiesService(get_conn())
     study_info = study_service.query({
         "instance_id": instance_id,
         "study_id": study_id
     })
     t.toc("study_info", restart=True)
     # study_info 0.012129 seconds.
     export_service = ExportService(get_conn())
     _, _, labels = export_service.save_onestudy_label(
         study_id, study_info[0], None, [])
     t.toc("labels gen", restart=True)
     # labels gen 31.750854 seconds.
     crossref_service = CrossRefService()
     label_list = crossref_service.accumulate_contours(labels)
     t.toc("label_list", restart=True)
     # label_list 32.659566 seconds.
     result = crossref_service.merge_dicom_orientation(label_list)
     t.toc("merge dicom", restart=True)
     # merge dicom 8.348602 seconds.
     return jsonify(result), 200
Example #30
0
trainDataSetLoader, testDataSetLoader = dataLoader()

bestLearningRate = 0.02  #from HW5 pr1
bestDecay = 0.01  #from HW5 pr1
bestMomentum = 0.7  #from HW5 pr1

DeeprNeuralNet = DeeperNeuralNet()
numOfEpochFinal = 80

#Takes 3 hours
timerObj = TicToc()  #create timer class
timerObj.tic()
final_accStorage_train, final_accStorage_test = wholeTrainProcessor(
    trainDataSetLoader, bestMomentum, bestLearningRate, bestDecay,
    DeeprNeuralNet, testDataSetLoader, numOfEpochFinal)
timerObj.toc()

#plotting final learning curve
epochArr_final = []
final_accStorage_train_list = []
final_accStorage_test_list = []

for k in range(numOfEpochFinal):
    epochArr_final.append(k + 1)
    #need to convert data type to list for plotting.
    final_accStorage_train_list.append(final_accStorage_train[0, k])
    final_accStorage_test_list.append(final_accStorage_test[0, k])

plt.plot(epochArr_final,
         final_accStorage_train_list,
         label="Accuracy on train set")