Beispiel #1
0
def evaluate_reconstruction(sample: LayerSplit,
                            probability_matrix: np.ndarray,
                            verbose: bool = False):
    n = len(sample.node_index)
    assert probability_matrix.shape == (n, n)

    # First, check constraints
    constr_metrics = check_constraints(sample, probability_matrix)

    # Target (hidden) edges. Converting to zero-based index.
    target_edges_src = toolz.get(sample.hidden.edges.node_1.tolist(),
                                 sample.node_index)
    target_edges_dst = toolz.get(sample.hidden.edges.node_2.tolist(),
                                 sample.node_index)
    target_edges = list(zip(target_edges_src, target_edges_dst))

    # Assigning zero probabilities to edges between observed nodes
    observed_entries = matrix_intersetions(sample.observed.nodes,
                                           index=sample.node_index)
    probability_matrix = probability_matrix.copy()
    probability_matrix[observed_entries] = 0
    np.fill_diagonal(probability_matrix, 0)

    # Transforming probabilities into adjacency matrix and edge list
    pred_matrix = probabilies_to_adjacency_exact(probability_matrix)
    pred_edges = adjmatrix_to_edgelist(pred_matrix)

    metrics = binary_classification_metrics(target_edges, pred_edges)
    metrics.update(constr_metrics)
    if verbose:
        display(pd.Series(metrics))
    return metrics
    def vectors(self, p, print_out):
        """Tests different momentum vector shape for correct mixing
        
        Required Inputs
            p           :: np.array :: momentum to refresh
            test_name   :: string   :: name of test
            print_out   :: bool     :: print results to screen
        """

        p_mixed = self.m.fullRefresh(p=p)
        passed = (np.around(self.m.flip(p_mixed),
                            6) == np.around(self.m.noise, 6)).all()

        if print_out:
            utils.display(
                test_name='full refresh: {} dimensions'.format(p.shape),
                outcome=passed,
                details={
                    'original array: {}'.format(np.bmat([[p], [self.m.noise]])):
                    [],
                    # 'rotation matrix: {}'.format(self.m.rot):[],
                    'mix + flip: {}'.format(p_mixed): [],
                })

        return passed
Beispiel #3
0
def drawRegions(img, regions):
    for x1, y1, x2, y2 in regions:
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

    if DEBUG:
        utils.display([('Regions', img)])
    return img
Beispiel #4
0
def getRegions(img):
    grayImg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # grayImg = cv2.equalizeHist(np.copy(grayImg))
    edges = cv2.Canny(grayImg,100,200,apertureSize = 3) 
    if DEBUG:
        utils.display([('Canny Edge Detection', edges)])
    kernel = np.ones((3,3),np.uint8)
    edges = cv2.dilate(edges,kernel,iterations = 14)
    # edges = 255-edges
    # utils.display([('', edges)])
    contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    if DEBUG:
        utils.display([('Contours', edges)])
    # Only take contours of a certain size
    regions = []
    for contour in contours:
        imgH, imgW, _ = img.shape
        [x, y, w, h] = cv2.boundingRect(contour)
        if w < 50 or h < 50:
            pass
        elif w > .95*imgW or h > .95*imgH:
            pass
        else:
            regions.append((x, y, x+w, y+h))
    return regions
Beispiel #5
0
def list(config, app_id, id, type):
    """List available application policies.

    Examples:

    List all group policies that belong to application 50:

        $ jaguar policy list 50 --type group

    List instance policy 101 that belongs to application 50:

        $ jaguar policy list 50 --type instance --id 101
    """
    try:
        url = config.server['url'] + '/v1/applications/' \
              + str(app_id) + '/policies/'
        if type:
            url += type + '/'
            if id > 0:
                url += str(id)

        rest = Rest(url, {'username':config.user})
        display(rest.get())

    except RestError as error:
        quit(error.message)
Beispiel #6
0
def detectRaid(stamp, debug=False):
    resizedStamp = resizeStamp(stamp)

    eggArea = extractEggArea(resizedStamp)
    if debug: utils.display(eggArea)

    raid = determineRaid(eggArea, resizedStamp, debug)
Beispiel #7
0
 def kgDeltaH(self, n_steps = 20, step_size = .1, tol = 1e-1, print_out = True):
     """Tests to see if <exp{-\delta H}> == 1
     
     Optional Inputs
         n_steps         :: int      :: LF trajectory lengths
         step_size       :: int      :: Leap Frog step size
         tol :: float :: tolerance level of deviation from expected value
         print_out :: bool :: prints info if True
     """
     passed = True
     pot = KG()
     delta_hs, av_acc = self._runDeltaH(pot)
     meas_av_exp_dh  = np.asscalar(np.exp(-delta_hs).mean())
     passed *= np.abs(meas_av_exp_dh - 1) <= tol
     
     if print_out:
         utils.display('<exp{-𝛿H}> using ' + pot.name, passed,
             details = {
                 'Inputs':['a: {}'.format(self.spacing),'n steps: {}'.format(n_steps),
                     'step size: {}'.format(step_size),
                     'lattice: {}'.format(self.lattice_shape)],
                 'Outputs':['<exp{{-𝛿H}}>: {}'.format(meas_av_exp_dh),
                     '<Prob. Accept>: {}'.format(av_acc)]
                 })
     return passed
Beispiel #8
0
def detectGymName2( stamp, referenceImages, debug = False):
    roiRect = extractRoiRectFromStamp(stamp, debug)
    maskedRoi = extractMaskedRoi(roiRect, debug)
    gymName, score = findBestMatchViaTemplating( maskedRoi, referenceImages)

    if debug: utils.display(stamp, "{}:{}".format(gymName, score))
    return gymName, score
Beispiel #9
0
 def kgAcceptance(self, n_steps = 20, step_size = .1, tol = 1e-2, print_out = True):
     """calculates the value <P_acc> for the free field
     
     Optional Inputs
         n_steps         :: int      :: LF trajectory lengths
         step_size       :: int      :: Leap Frog step size
         m :: float :: parameter used in potentials
         tol :: float :: tolerance level of deviation from expected value
         print_out :: bool :: prints info if True
     """
     passed = True
     
     pot = KG()
     n_samples = 1000
     delta_hs, measured_acc = self._runDeltaH(pot, n_samples = n_samples, 
         step_size=step_size, n_steps=n_steps)
     av_dh = np.asscalar(delta_hs.mean())
     expected_acc = erfc(.5*np.sqrt(av_dh))
     
     passed *= np.abs(measured_acc - expected_acc) <= tol
     
     meas_av_exp_dh  = np.asscalar(np.exp(-delta_hs).mean())
     if print_out:
         utils.display('<P_acc> using ' + pot.name, passed,
             details = {
                 'Inputs':['a: {}'.format(self.spacing),'m: {}'.format(pot.m),
                     'shape: {}'.format(self.lattice_shape), 'samples: {}'.format(n_samples),
                     'n steps: {}'.format(n_steps), 'step size: {}'.format(step_size)],
                 'Outputs':['expected: {}'.format(expected_acc),
                     'measured: {}'.format(measured_acc),
                     '<exp{{-𝛿H}}>: {}'.format(meas_av_exp_dh),
                     '<𝛿H>: {}'.format(av_dh)]
                 })
     return passed
Beispiel #10
0
def drawRegions(img, regions):
    for x1, y1, x2, y2 in regions:
        cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2)

    if DEBUG:
        utils.display([('Regions', img)])
    return img
Beispiel #11
0
def detectCirclesViaFiltered(colorImage, expectedNrCircles, debug):
    lower = np.array([140, 160, 220], dtype="uint8")  # BGR
    upper = np.array([200, 220, 255], dtype="uint8")

    filtered = cv2.inRange(colorImage, lower, upper)
    print "filtered"
    if debug: utils.display(filtered, "filtered")

    kernel = np.ones((5, 5), np.uint8)
    filtered = cv2.morphologyEx(filtered, cv2.MORPH_CLOSE, kernel, iterations=3)
    if debug: utils.display(filtered, "MORPH_CLOSE")

    ret, thresh = cv2.threshold(filtered, 254, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    getArcLength = lambda x: cv2.arcLength(x, closed=True)
    contours.sort(key=getArcLength, reverse=True)

    result = []
    for contour in contours[0:expectedNrCircles]:
        x, y, w, h = cv2.boundingRect(contour)
        r = int(max(w, h) / 2.0)
        if fallsWithAllowedRadius(colorImage, r):
            cv2.rectangle(colorImage, (x, y), (x + w, y + h), (255, 0, 0), 3)
            xcenter = x + r
            ycenter = y + r
            result.append([xcenter, ycenter, r])
        else:
            print "Warning: found contour with unallowed radius. Ignoring"
            cv2.rectangle(colorImage, (x, y), (x + w, y + h), (0, 0, 255), 3)

    # if debug: utils.display( colorImage )
    if debug: print "circlesViaFiltered: {}".format(result)
    return result
Beispiel #12
0
def run():
    diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
    # naked_twins_grid1 = '84.632.....34798257..518.6...6.97..24.8256..12..84.6...8..65..3.54.2.7.8...784.96'
    # naked_twins_grid2 = '1.4.9..68956.18.34..84.695151.....868..6...1264..8..97781923645495.6.823.6.854179' # from test1
    grid = diag_sudoku_grid

    values = solve(grid)

    logging.debug("Values after solve 2: ", values)

    logging.info("Sudoku values returned after search")

    if values:
        display(solve(grid))

    try:
        from visualize import visualize_assignments

        logging.info("PyGame using visualize being called")

        visualize_assignments(assignments)

    except SystemExit:
        logging.exception('SystemExit occurred')
    except:
        logging.exception(
            'We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.'
        )
Beispiel #13
0
def getText(img, regions, debug=False):
    textChunks = []
    imgChunks = []
    for region in regions:
        x1, y1, x2, y2 = region
        # Clip region and extract text
        chunk = img[y1:y2, x1:x2, ::]
        ret,chunk = cv2.threshold(chunk,140,255,0)
        cv2.imwrite('tmp.jpg', chunk)
        subprocess.call(['tesseract', 'tmp.jpg', 'tmp'])
        f = open('tmp.txt')
        lines = []
        for line in f:
            print line.strip()
            if line.strip() != '':
                lines.append(parseText(line))
        textChunks.append(lines)
        imgChunks.append(chunk)
        f.close()
        subprocess.call(['rm', 'tmp.jpg', 'tmp.txt'])
    if DEBUG:
        display = [(str(text), imgChunks[i]) for i, text in enumerate(textChunks)]
        utils.display(display[:10])
        utils.display(display[10:20])
    return textChunks
def test_road_unwarp(images=True):
    camera = Camera()
    camera.load_or_calibrate_camera()

    if images:
        directory = TEST_IMAGES_DIR
        filenames = glob.glob(directory + '/*.jpg')
        filenames = [
            TEST_IMAGES_DIR + '/test1.jpg',
            TEST_IMAGES_DIR + '/test4.jpg',
            TEST_IMAGES_DIR + '/test5.jpg',
            # 'proj_hard/619.jpg', 'proj_hard/621.jpg', 'proj_hard/623.jpg',
            TEST_IMAGES_DIR + '/signs_vehicles_xygrad.jpg'
        ]

        lanes = Lanes(filenames,
                      undistort=camera.undistort,
                      filtering_pipeline=filtering_pipeline)

        for filename in filenames:
            img = mpimg.imread(filename)
            lane_marked_undistorted = lanes.pipeline(img)
            display(lane_marked_undistorted, filename)
    else:
        # Video Mode
        debug('Processing Video: ', INPUT_VIDEOFILE)
        input_videoclip = VideoFileClip(INPUT_VIDEOFILE)
        output_videofile = OUTPUT_DIR + INPUT_VIDEOFILE[:-4] + '_output.mp4'
        lanes = Lanes(undistort=camera.undistort,
                      filtering_pipeline=filtering_pipeline)
        lane_marked_videoclip = input_videoclip.fl_image(
            lanes.pipeline)  # NOTE: this function expects color images!
        lane_marked_videoclip.write_videofile(output_videofile, audio=False)

    return
Beispiel #15
0
 def make_jpg(self, image, figsize, save_name):
     fig = plt.figure(figsize=figsize)
     ax = fig.add_subplot(111)
     result = self.mrcnn.predict([image])[0]
     utils.display(ax, image, result["boxes"], result["cls_ids"], result["scores"],
         result["masks"], (100, 100), self.class_names, save_name=save_name, show=True)
     plt.close(fig)
    def run(self, p0, x0):
        """Checks the integrator is reversible
        by running and then reversing the integration and 
        verifying the same point in phase space
        
        Required Inputs
            p0          :: lattice :: momentum
            x0          :: lattice :: position
        """
        passed = True

        self.dynamics.newPaths()
        pm, xm = self.dynamics.integrate(p0.copy(), x0.copy())
        p0f, x0f = self.dynamics.integrate(-pm, xm)  # time flip
        p0f = -p0f  # time flip to point in right time again

        phase_change = np.linalg.norm(  # calculate frobenius norm
            np.asarray([[p0f], [x0f]]) - np.asarray([[p0], [x0]]))
        passed = (phase_change < self.tol)

        if self.print_out:
            utils.display(
                test_name="Reversibility of Integrator",
                outcome=passed,
                details={
                    'initial (p, x): ({}, {})'.format(p0, x0): [],
                    'middle  (p, x): ({}, {})'.format(pm, xm): [],
                    'final   (p, x): ({}, {})'.format(p0f, x0f): [],
                    'phase change:    {}'.format(phase_change): [],
                    'number of steps: {}'.format(self.dynamics.n_steps): []
                })

        return passed
    def laplacian(self, print_out=True):
        """tests the laplacian function against expected values"""
        passed = True
        a = self.a1  # shortcut
        passed *= (self.l == self.a1).all()  # check both are equal
        test = [
            [(1, 1), np.asarray([0., 0.]).sum()],  # 11
            [(3, 3), np.asarray([-40., -4.]).sum()],  # 44
            [(4, 4), np.asarray([40., 4.]).sum()],  # 11
            [(3, 4), np.asarray([-40., 4.]).sum()],  # 41
            [(4, 3), np.asarray([40., -4.]).sum()],  # 14
            [(2, 3), np.asarray([0., -4.]).sum()]
        ]  # 34

        store = []
        for pos, act in test:  # iterate test values
            res = laplacian(self.l, position=pos, a_power=0)
            passed *= (res == act).all()
            if print_out: store.append([pos, res, act])

        if print_out:
            utils.display('Laplacian',
                          outcome=passed,
                          details={
                              'checked vs. known values (Mathematica)': [
                                  'pos: {}, res: {}, act: {}'.format(*vals)
                                  for vals in store
                              ]
                          })

        return passed
    def sciPyLaplacian(self, print_out=True):
        """tests the laplacian function against expected values"""
        passed = True
        a = self.a1  # shortcut
        test = [
            [(1, 1), np.asarray([0., 0.]).sum()],  # 11
            [(3, 3), np.asarray([-40., -4.]).sum()],  # 44
            [(4, 4), np.asarray([40., 4.]).sum()],  # 11
            [(3, 4), np.asarray([-40., 4.]).sum()],  # 41
            [(4, 3), np.asarray([40., -4.]).sum()],  # 14
            [(2, 3), np.asarray([0., -4.]).sum()]
        ]  # 34

        store = []
        for pos, act in test:  # iterate test values
            res = laplace(a, mode='wrap').view(Periodic_Lattice)[pos]
            passed *= (res == act).all()
            if print_out: store.append([pos, res, act])

        if print_out:
            utils.display('SciPy Laplacian',
                          outcome=passed,
                          details={
                              'checked vs. known values (Mathematica)': [
                                  'pos: {}, res: {}, act: {}'.format(*vals)
                                  for vals in store
                              ]
                          })

        return passed
    def wrap(self, print_out=True):
        """tests the wrapping function against expected values"""
        passed = True
        a = self.a1  # shortcut
        passed *= (self.l == self.a1).all()  # check both are equal
        test = [
            [(1, 1), 22.],
            [(3, 3), 44.],
            [(4, 4), 11.],  # [index, expected value]
            [(3, 4), 41.],
            [(4, 3), 14.],
            [(10, 10), 33.]
        ]

        for idx, act in test:  # iterate test values
            passed *= (self.l[idx] == act)

        if print_out:
            utils.display('Periodic Boundary',
                          outcome=passed,
                          details={
                              'array storage checked': [],
                              'period indexing vs. known values': []
                          })

        return passed
    def gradSquared(self, print_out=True):
        """tests the gradient squared function against expected values"""
        passed = True
        a = self.a1  # shortcut
        passed *= (self.l == self.a1).all()  # check both are equal

        test = [
            [(1, 1), np.square(np.asarray([10., 1.])).sum()],  # 11
            [(3, 3), np.square(np.asarray([-30., -3.])).sum()],  # 44
            [(4, 4), np.square(np.asarray([10., 1.])).sum()],  # 11
            [(3, 4), np.square(np.asarray([-30., 1.])).sum()],  # 41
            [(4, 3), np.square(np.asarray([10., -3.])).sum()],  # 14
            [(2, 3), np.square(np.asarray([-3., 10.])).sum()]
        ]  # 34

        store = []
        for pos, act in test:  # iterate test values
            res = gradSquared(self.l, position=pos, a_power=0)
            passed *= (res == act).all()
            if print_out: store.append([pos, res, act])

        if print_out:
            utils.display('Gradient Squared : (forward diff)',
                          outcome=passed,
                          details={
                              'checked vs. known values (Mathematica)': [
                                  'pos: {}, res: {}, act: {}'.format(*vals)
                                  for vals in store
                              ]
                          })

        return passed
def farmer(grid, width, num_of_workers=2, steps=1):
    assert len(grid) % width == 0
    height = len(grid) // width

    for step in range(steps):
        updated_grid = [2] * width * height
        strips = split_into_strips(grid, width, height, num_of_workers)
        num_of_workers = len(strips)

        # each step calculated by parralell workers
        for worker_num in range(num_of_workers):  # will be par
            strip = strips[worker_num]
            base_strip_size = (len(strips[0]) // width) - 2
            strip_size = (len(strip) // width) - 2
            updated_strip = worker(strip, strip_size, width)

            for cell_index in range(len(updated_strip)):
                base = worker_num * base_strip_size * width
                if num_of_workers != 1:
                    base += width
                update_index = (base + cell_index) % (width * height)

                updated_grid[update_index] = updated_strip[cell_index]

        grid = updated_grid

    display(grid)

    return grid
Beispiel #22
0
def applyMask(image, mask, debug):
    h, w = mask.shape[:2]
    image = cv2.resize(image, (w, h))
    print "{} and {}".format(mask.shape[:2], image.shape[:2])
    result = cv2.bitwise_and(image, image, mask=mask)
    if debug: utils.display(result, "masked")
    return result
def main(args):
    display(interpreter())
    display('Starting up Analyzer ... ', term='')
    analyzer = Analyzer(args[1])
    serve = server(analyzer)
    analyzer.server = serve
    print(analyzer.server)
Beispiel #24
0
def extractRoiRectFromStamp(stamp, debug):
    circle = detectCirclesViaFiltered(stamp, 1, debug)[0]
    lu = (circle[0] - circle[2], circle[1] - circle[2])
    rd = (circle[0] + circle[2], circle[1] + circle[2])
    roiRect = utils.crop(stamp, lu, rd)
    if debug: utils.display(roiRect, "rect")
    return roiRect
Beispiel #25
0
 def kgCorrelation(self, mu = 1., tol = 1e-2, print_out = True):
     """calculates the value <x(0)x(0)> for the QHO
     
     Optional Inputs
         mu :: float :: parameter used in potentials
         tol :: float :: tolerance level of deviation from expected value
         print_out :: bool :: prints info if True
     """
     passed = True
     
     pot = KG(m=mu)
     measured_xx = self._runCorrelation(pot)
     expected_xx = theory.operators.x2_1df(1, self.n, self.spacing, 0)
     
     passed *= np.abs(measured_xx - expected_xx) <= tol
     
     if print_out:
         utils.display('<x(0)x(t)> using ' + pot.name, passed,
             details = {
                 'Inputs':['a: {}'.format(self.spacing),'µ: {}'.format(mu),
                     'shape: {}'.format(self.lattice_shape), 'samples: {}'.format(self.n)],
                 'Outputs':['expected: {}'.format(expected_xx),
                     'measured: {}'.format(measured_xx)]
                 })
     return passed
Beispiel #26
0
	def track_progress(self, printing, message_optimization, risky):

		portfolio_statistics = self.statistics

		weights = self.weights 
		weights_df =  pd.DataFrame(weights, columns = ['Allocation Weights'])
		weights_df.index = self.ticker_list

		annual_return = portfolio_statistics['portfolio_annual_return']
		annual_std    = portfolio_statistics['portfolio_annual_std']
		annual_sr     = portfolio_statistics['portfolio_annual_sr']

		if not risky : 
			annual_return = annual_return * 0.9 + self.risk_free * 0.1
			annual_std    = 0.9 ** 2 * annual_std
			annual_sr     = (annual_return - self.risk_free) / annual_std

		utils.portfolios['ret'].append(annual_return)
		utils.portfolios['std'].append(annual_std)
		utils.portfolios['sr'].append(annual_sr)

		messages = []
		messages.append(message_optimization)
		messages.append(" Portfolio Annual Return (252 days)  = {} % ".format(round(annual_return * 100, 3)))
		messages.append(" Portfolio Annual Standard Deviation  (252 days)  = {}  ".format( round(annual_std, 3)))
		messages.append(" Portfolio Annual Sharpe Ratio  (252 days)  = {}  ".format( round(annual_sr, 3)))

		if printing : utils.display(weights_df.T)
		if printing : utils.pprint(messages)

		return
Beispiel #27
0
def main(args):
    display(interpreter())
    display('Starting up Analyzer ... ', term='')
    analyzer = Analyzer(args[1])
    serve = server(analyzer)
    analyzer.server = serve
    print(analyzer.server)
Beispiel #28
0
def getContourImage(colorImage, debug):
    img_gray = cv2.cvtColor(colorImage, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(img_gray, 240, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    im2 = cv2.bitwise_not(im2)
    if debug: utils.display(im2, "contours", False)
    return im2
Beispiel #29
0
    def test_solve(self):
        solve = solution.solve(self.diagonal_grid)
        display(solve)
        print()
        display(self.solved_diag_sudoku)

        self.assertEqual(solve, self.solved_diag_sudoku)
Beispiel #30
0
def run_main():
    window, tick_to_display = 4, 1000
    columns_indicators = ['close', 'high', 'low']
    columns_to_predict = ["close", "high", "low"]
    columns_to_predict = ["close", "high", "low"]

    # Create and fit the model
    arc, exchange_data = create_fit_model(window, columns_to_predict,
                                          columns_indicators, True,
                                          "cosmos")  #, "cryptocompare")
    save_data_for_jupyter(arc, "cryp_")

    #display(arc.data_with_indicators, pd.DataFrame(), tick_to_display, arc.x_test.index)

    # Predict future prices
    y_predicted = arc.ml_predict(restore_y=True,
                                 sort_index=True,
                                 original_data=exchange_data,
                                 windows=window,
                                 is_returns=True,
                                 src_columns=columns_to_predict)

    # Get all data
    data_with_indicators = pd.concat([arc.data_with_indicators, y_predicted],
                                     axis=1,
                                     sort=False)

    # Display graphs
    display(data_with_indicators, y_predicted, tick_to_display,
            arc.x_test.index)
Beispiel #31
0
def miss(request,hierarchy,logger,env,links,cacheId):
	request.path.append( [ request.dest[0],request.dest[1] ] )
        utils.display(env,logger,request,"Miss")
        dest=[]
	
	dest.extend([(int(request.dest[0])+1) ,int(hierarchy[cacheId].get_l2_address(request.key))])
	cid= str(dest[0])+"-"+str(dest[1])
	request.source = request.dest
        	
        r = hierarchy[cid].read(request.key,request.size)
	
        if r:
                if ((request.dest[1] == dest[1] ) and (request.dest[0] == 1)):
                        hierarchy[cacheId]._miss_count-=1
                request.dest = dest
		hit(request,hierarchy,logger,env,links,cid)
		
        else:

		utils.add_cache_request_time(hierarchy[cid],env.now,request)
                request.dest = dest
		request.path.append([dest[0],dest[1] ])
                utils.display(env,logger,request,"Miss")
                cacheId = "3-0"
                request.source = dest
                request.dest = [3,0]
		hit(request,hierarchy,logger,env,links,cacheId)
Beispiel #32
0
def main(file_name, pop_size, num_items, max_generation, mutation_rate, crossover_rate, executions, mode, immigrants):
    items, capacity = read_file(file_name)
    if mode == 1:
        for execution in range(0, executions):
            population = generate_population(items, capacity, pop_size, num_items)
            stat = []
            stat_avg = []
            for generation in range(0, max_generation):
                save_fitness(file_name, population, generation, execution)
                stat.append(population[0]['fitness'])
                stat_avg.append(sum(individual['fitness'] for individual in population) / len(population))
                new_population = evolve_population(population, mutation_rate, crossover_rate, capacity)
                population = selection(population, new_population, pop_size)
            display(stat, stat_avg, execution)
    elif mode == 2:
        for execution in range(0, executions):
            population = generate_population(items, capacity, pop_size, num_items)
            stat = []
            stat_avg = []
            for generation in range(0, max_generation):
                save_fitness(file_name, population, generation, execution)
                stat.append(population[0]['fitness'])
                stat_avg.append(sum(individual['fitness'] for individual in population) / len(population))
                new_population = evolve_population(population, mutation_rate, crossover_rate, capacity)
                population = selection(population, new_population, pop_size)
            display(stat, stat_avg, execution)
Beispiel #33
0
def CompletionEvent(request,hierarchy,logger,env,links):
	sLinkId,dLinkId = utils.get_link_id(request.source, request.dest,cephLayer)
	
	if sLinkId:
                yield links[sLinkId].get(links[sLinkId].capacity)
		latency = float(request.size) / links[sLinkId].capacity
        if dLinkId:
		print "Error on Completion"
	yield env.timeout(latency)
	if sLinkId:
                yield links[sLinkId].put(links[sLinkId].capacity)

	request.set_endTime(env.now)
	request.set_compTime( request.endTime - request.startTime)

	request.client.ave_latency += (request.endTime - request.startTime)
	
	global  sim_req_num
	sim_req_num +=1
	global  sim_req_comp
	global  count_w
	count_w+=1
	sim_req_comp.append(request.get_compTime())
	global  sim_end
	sim_end = env.now

	utils.display(env,logger,request)
	cid = request.client
	del request
	request_generator(cid,hierarchy,logger,env,links,1)
Beispiel #34
0
def list(config, app_id, id, type):
    """List available application policies.

    Examples:

    List all group policies that belong to application 50:

        $ jaguar policy list 50 --type group

    List instance policy 101 that belongs to application 50:

        $ jaguar policy list 50 --type instance --id 101
    """
    try:
        url = config.server_url + '/v1/applications/' \
              + str(app_id) + '/policies/'
        if type:
            url += type + '/'
            if id > 0:
                url += str(id)

        rest = Rest(url, {'username': config.user})
        display(rest.get())

    except RestError as error:
        quit(error.message)
Beispiel #35
0
def handlePicture(cardPath):
    # card = BusinessCards.find_one({'_id': Objectid(cardId)})
    # path = '../' + card['filename']
    img = utils.readImage(cardPath)
    good, card = findCard.findCard(img)
    regions, text = process.processCard(card)
    processedCard = process.drawRegions(card, regions)
    cards = [('Original', img), (str(text), processedCard)]
    print text
    utils.display(cards)
def test_local(sentence='Robot1, move to location 1 2!'):
    from feature import as_featurestruct
    display('Starting up Analyzer ... ', term='')
    a = Analyzer('grammar/robots.prefs')
    display('done.\n', 'analyzing', sentence)
    d = a.parse(sentence)
    pprint(d)
#     s = as_featurestruct(d[0])
#     return s
    return d
def optimum_policy2D(grid, init, goal, cost):
    value = [[[999 for _ in range(len(grid[0]))] for _ in range(len(grid))],
             [[999 for _ in range(len(grid[0]))] for _ in range(len(grid))],
             [[999 for _ in range(len(grid[0]))] for _ in range(len(grid))],
             [[999 for _ in range(len(grid[0]))] for _ in range(len(grid))]]
    for i in range(len(value)):
        value[i][goal[0]][goal[1]] = 0

    policy = [[[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))],
              [[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))],
              [[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))],
              [[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))]]
    for i in range(len(policy)):
        policy[i][goal[0]][goal[1]] = '*'

    open = []
    for o in range(len(forward)):
        open.append([goal[0], goal[1], o, 0])

    while len(open) > 0:
        next = open.pop()
        x = next[0]
        y = next[1]
        o = next[2]
        for o2 in range(len(forward)):
            for a in range(len(action)):
                if o == (o2 + action[a]) % len(forward):
                    x2 = x - forward[o][0]
                    y2 = y - forward[o][1]
                    if 0 <= x2 < len(grid) and 0 <= y2 < len(grid[0]):
                        if grid[x2][y2] == 0 and value[o][x][y] + cost[a] < value[o2][x2][y2]:
                            value[o2][x2][y2] = value[o][x][y] + cost[a]
                            policy[o2][x2][y2] = action_name[a]
                            open.append([x2, y2, o2])

    utils.display(policy)
    utils.display(value, 3)

    policy2D = [[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))]

    x = init[0]
    y = init[1]
    o = init[2]
    if policy[o][x][y] != ' ':
        policy2D[x][y] = policy[o][x][y]
        while policy[o][x][y] != '*':
            if policy[o][x][y] == 'R':
                o = (o - 1) % len(forward)
            elif policy[o][x][y] == 'L':
                o = (o + 1) % len(forward)
            x += forward[o][0]
            y += forward[o][1]
            policy2D[x][y] = policy[o][x][y]

    return policy2D
Beispiel #38
0
def delete(config, app_id, type, id):
    """Delete an application policy.
    """
    try:
        url = config.server['url'] + '/v1/applications/' + str(app_id) \
              + '/policies/' + type + '/' + str(id)
        headers = {'username':config.user, 'Content-Type':'application/json'}
        rest = Rest(url, headers)
        display(rest.delete())
    except RestError as error:
        quit(error.message)
def test(args):
    """Just test the analyzer.
    """
    prefs, sent = args

    display('Creating analyzer with grammar %s ... ', prefs, term=' ')
    analyzer = Analyzer(prefs)
    display('done.')

    for p in analyzer.parse(sent):
        pprint(p)
def main(args):
    display(interpreter())
    #display('Starting up Analyzer ... ', term='')
    start = time.time()
    analyzer = Analyzer(args[1])
    end = time.time()
    #usage_time(start, end, analyzer)
    try:
        #server_thread = Thread(target=server, kwargs={'obj': analyzer, 'host': host, 'port': port})
        #serve = server_thread.start()
        serve = server(analyzer)
        analyzer.server = serve
    except Exception, e:
        print(e)
Beispiel #41
0
def update(config, app_id, type, id, file):
    """Update an application policy.
    """
    try:
        data = ''
        while True:
            chunk = file.read(1024)
            if not chunk:
                break
            data += chunk
        if not is_json(data):
            raise click.BadParameter('The policy file ' + file.name \
                                     + ' is not a valid json file.')
        url = config.server['url'] + '/v1/applications/' + str(app_id) \
              + '/policies/' + type + '/' + str(id)
        headers = {'username':config.user, 'Content-Type':'application/json'}
        rest = Rest(url, headers, data)
        display(rest.put())
    except RestError as error:
        quit(error.message)
def dispatch():
    for name, kind, args, kwargs in caches:
        shutil.rmtree('tmp', ignore_errors=True)

        obj = kind(*args, **kwargs)

        for key in range(RANGE):
            key = str(key).encode('utf-8')
            obj.set(key, key)

        try:
            obj.close()
        except:
            pass

        processes = [
            mp.Process(target=worker, args=(value, kind, args, kwargs))
            for value in range(PROCS)
        ]

        for process in processes:
            process.start()

        for process in processes:
            process.join()

        timings = co.defaultdict(list)

        for num in range(PROCS):
            filename = 'output-%d.pkl' % num

            with open(filename, 'rb') as reader:
                output = pickle.load(reader)

            for key in output:
                timings[key].extend(output[key])

            os.remove(filename)

        display(name, timings)
def dispatch():
    setup()

    from django.core.cache import caches

    for name in ['locmem', 'memcached', 'redis', 'diskcache', 'filebased']:
        shutil.rmtree('tmp', ignore_errors=True)

        preparer = mp.Process(target=prepare, args=(name,))
        preparer.start()
        preparer.join()

        processes = [
            mp.Process(target=worker, args=(value, name))
            for value in range(PROCS)
        ]

        for process in processes:
            process.start()

        for process in processes:
            process.join()

        timings = co.defaultdict(list)

        for num in range(PROCS):
            filename = 'output-%d.pkl' % num

            with open(filename, 'rb') as reader:
                output = pickle.load(reader)

            for key in output:
                timings[key].extend(output[key])

            os.remove(filename)

        display(name, timings)
def server(obj, host='localhost', port=8090):
    server = SimpleXMLRPCServer((host, port), allow_none=True, encoding='utf-8')
    server.register_instance(obj)
    display('server ready (listening to http://%s:%d/).', host, port)
    server.serve_forever()
    return server  # Added
Beispiel #45
0
def run(grid):
  if grid.live_cells:
    display(grid)
    time.sleep(0.5)
    return run(grid.next_generation())
Beispiel #46
0
        def onMessage(self, message):

            self.ack(message)

            display(message, config)
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import preprocessing
import utils



if __name__ == '__main__':
	# get X and y
	train_x, train_y = utils.loadDataHelper('train_data.txt')
	test_x, test_id = utils.loadDataHelper('test_data.txt')
	print('train size: %d %d' % (len(train_x), len(train_y)))
	print('test size: %d %d' % (len(test_x), len(test_id)))

	utils.display(train_x)

	#normalize the data attributes
	train_x = preprocessing.scale(train_x)
	test_x = preprocessing.scale(test_x)

	utils.display(train_x)

	model = LinearSVC()
	model.fit(train_x, train_y)
	print(model)

	predicted = model.predict(test_x)
	utils.saveResult('result_linSVM.csv', test_id, predicted)
                    y2 = y - forward[o][1]
                    if 0 <= x2 < len(grid) and 0 <= y2 < len(grid[0]):
                        if grid[x2][y2] == 0 and value[o][x][y] + cost[a] < value[o2][x2][y2]:
                            value[o2][x2][y2] = value[o][x][y] + cost[a]
                            policy[o2][x2][y2] = action_name[a]
                            open.append([x2, y2, o2])

    utils.display(policy)
    utils.display(value, 3)

    policy2D = [[' ' for _ in range(len(grid[0]))] for _ in range(len(grid))]

    x = init[0]
    y = init[1]
    o = init[2]
    if policy[o][x][y] != ' ':
        policy2D[x][y] = policy[o][x][y]
        while policy[o][x][y] != '*':
            if policy[o][x][y] == 'R':
                o = (o - 1) % len(forward)
            elif policy[o][x][y] == 'L':
                o = (o + 1) % len(forward)
            x += forward[o][0]
            y += forward[o][1]
            policy2D[x][y] = policy[o][x][y]

    return policy2D


utils.display(optimum_policy2D(grid, init, goal, cost))
def usage():
    display('Usage: analyzer.py <preference file>')
    sys.exit(-1)
Beispiel #50
0
            else:
                suggestedLinesField.append('')
            lineCount += 1
        lineCount = 0
        suggestedFields.append(suggestedLinesField)
    return suggestedFields

if __name__ == "__main__":
    imgs = utils.getImages('../../stanford_business_cards/photos/', 5)
    # imgs = utils.getImages('../our_cards/', 8)
    DEBUG = True
    # img = utils.readImage('../../stanford_business_cards/photos/004.jpg')
    # imgs = [('',img)]
    # utils.display(imgs)
    good, cardImg = findCard.findCard(imgs[4][1])
    utils.display([('card',cardImg)])
    regions, text = processCard(cardImg)
    processedCard = drawRegions(cardImg, regions)
    suggestedFields = guessFields(regions, text)
    utils.display([('card',processedCard)])

    # scores = []
    # for imgName, img in imgs:
    #     # regions, text = processCard(img)
    #     # guessFields(regions, text)
    #     try:
    #         good, cardImg = findCard.findCard(img)
    #     except:
    #         good = False
    #         pass
    #     if good: