Beispiel #1
0
def run_MC_2(gridsize: int,
             steps: int,
             MC_iterations: int,
             error=0.001) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    Sensitivity and specificity, confusion 4-matrix,
    
    return 2*2*2 np.ndarray, in which [0,:,:] is mean values and [1,:,:] is error
    """

    # all confusion matrices (like ...0100,0010...)
    results_mat = np.zeros((MC_iterations, 2, 2), dtype=int)

    height = gridsize
    width = gridsize

    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()

        num_found_exact, x, y = locator.locate(
            v.map(), v.history_error(iteration_for_seed=run_idx,
                                     error=error))[0:3]
        num_found_inextact, x, y = locator.locate(v.map(), v.history())[0:3]

        true_pos = 0
        true_neg = 0
        false_pos = 0
        false_neg = 0

        if (num_found_exact == 1) and (num_found_inextact == 1):
            true_pos += 1
        elif (num_found_exact != 1) and (num_found_inextact != 1):
            true_neg += 1
        elif (num_found_exact == 1) and (num_found_inextact != 1):
            false_neg += 1
        elif (num_found_exact != 1) and (num_found_inextact == 1):
            false_pos += 1

        confusion_mat = np.array(
            ((true_pos, false_neg), (false_pos, true_neg)))
        results_mat[run_idx, :, :] = confusion_mat

    #confusion mat
    mean_mat = np.mean(results_mat, axis=0)
    err_mat = np.std(results_mat, axis=0) / np.sqrt(MC_iterations)

    return np.array((mean_mat, err_mat))
Beispiel #2
0
def run_MC(gridsize: int,
           steps: int,
           MC_iterations: int,
           error=False) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    """

    numFound_correct = 0
    numFound_incorrect = 0
    numNotFound = 0

    height = gridsize
    width = gridsize

    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()

        if error:
            num_found, x, y = locator.locate(
                v.map(), v.history_error(iteration_for_seed=run_idx))[0:3]
        else:
            num_found, x, y = locator.locate(v.map(), v.history())[0:3]

        if num_found == 1:
            loc_x, loc_y, dir = v.location()
            if ((x == loc_x) and (y == loc_y)):
                numFound_correct += 1
            else:
                numFound_incorrect += 1
        elif num_found == 0:
            numNotFound += 1
            if not error:
                log.warning("Not found!, gridsize:", gridsize, "steps", steps,
                            "error", error)


    log.debug("    correct match:{:7.3f}".format(numFound_correct/(MC_iterations)) + \
              ",  incorrect match:{:7.3f}".format(numFound_incorrect/(MC_iterations)) )

    return np.array(
        (numFound_correct / MC_iterations, numFound_incorrect / MC_iterations,
         numNotFound / MC_iterations))
Beispiel #3
0
def plot_single_run():
    height = 10
    width = 10

    m = mapgrid.generate_map(width=width, height=height)
    v = vehicle.Vehicle(m, 10)

    plot = plotter.LocatorPlot(v)
    plot.plot_map()

    # The placement of the vehicle need not be shown
    # plot.plot_vehicle()

    log.info("Start location:", str(v.location()))
    log.info("Start color:", v.get_color())

    for i in range(10):
        v.move_unbound()
        plot.plot_vehicle()

    # found, x, y = locator.locate(v.map(), v.history(), v)
    num_matches, x, y, possible_loc = locator.locate(v.map(), v.history())

    log.info("End location:", v.location())
    log.info("End color:", v.get_color())

    if num_matches == 1:
        loc_x, loc_y, dir = v.location()
        log.info(
            "Found on location", x, y, " and its correctness is " + str(
                (num_matches == 1) and (x == loc_x) and (y == loc_y)))
    else:
        log.warning("Location not found!")

    # Graph testing
    """
    history_graphs = []
    for i in range(4):
        history_graph = graph_utils.history_to_digraph(v.history(), i)
        history_graphs.append(history_graph)

        graph_utils.digraph_history_analyzer(history_graph)

    map_graph = graph_utils.map_to_digraph(m)

    for i in range(4):
        match = graph_utils.graph_matcher(map_graph, history_graphs[i])
        print("Start direction", direction.Direction(i), "graph match:", match)
    """

    # History error handling test
    locator.locate_with_one_possible_error(v.map(), v.history())
    locator.locate_with_one_possible_error(v.map(), v.history_error_one())
    locator.locate_with_error_fallback(v.map(), v.history())
    locator.locate_with_error_fallback(v.map(), v.history_error_one())
    # locator.locate_with_movement_and_error_fallback(v.map(), v.history(), v, retries=10)
    # locator.locate_with_movement_and_error_fallback(v.map(), v.history_error_one(), v, retries=10)

    plot.show()
Beispiel #4
0
 def parse(self):
     tests = []
     for filename in locator.locate(self.pattern, self.tests_dir):
         filedir = dirname(filename)
         filename = splitext(split(filename)[1])[0]
         if re.match("^[a-zA-Z]\w+$", filename):
             sys.path.append(filedir)
             try:
                 mod = __import__('%s' % filename)
             except ImportError, err:
                 raise SyntaxError("Error importing '%s'. Error: %s" % (filename, str(err)))
             sys.path.pop()
             tests.extend(self.load_fixture_from_module(mod))
Beispiel #5
0
def register():

    addr = locate()

    if request.method == "POST":
        username = request.form['username']
        phone = request.form['phone']
        password = request.form['password']
        register = users(username=username, phone=phone, password=password)
        db.session.add(register)
        db.session.commit()

        return redirect(url_for("login"))

    return render_template("register.html", addr=addr)
Beispiel #6
0
def register():
    if 'user' in session:
        flash('Please Logout First')
        return redirect(url_for('total'))
    if 'admin' in session:
        flash('Please Logout First')
        return redirect(url_for('admin'))

    addr = locate()

    if request.method == "POST":
        username = request.form['username']
        phone = request.form['phone']
        password1 = request.form['password1']
        register = users(username=username, phone=phone, password1=password1)
        db.session.add(register)
        db.session.commit()

        return redirect(url_for("login"))

    return render_template("register.html", addr=addr)
Beispiel #7
0
def single_run_demo():
    height = 10
    width = 10

    # m = mapgrid.generate_map(width=width, height=height)
    m = np.load("calulated_results/single_run_demo/map.npy")
    v = vehicle.Vehicle(m, 10)

    plot = plotter.LocatorPlot(v)
    plot.plot_map()

    log.info("Start location:", str(v.location()))
    log.info("Start color:", v.get_color())

    for i in range(10):
        v.move_unbound()
        plot.plot_vehicle()

    # found, x, y = locator.locate(v.map(), v.history(), v)
    num_matches, x, y, possible_loc = locator.locate(v.map(), v.history())

    log.info("End location:", v.location())
    log.info("End color:", v.get_color())

    if num_matches == 1:
        loc_x, loc_y, dir = v.location()
        log.info(
            "Found on location", x, y, " and its correctness is " + str(
                (num_matches == 1) and (x == loc_x) and (y == loc_y)))
    else:
        log.warning("Location not found!")

    print(v.history())

    # mpz.save("single_run.tikz")
    plot.show()
Beispiel #8
0
from tempfile import mkdtemp

from locator import locate


source = sys.argv[1]
tempdir = mkdtemp()

print('creating temporary directory ' + tempdir)

print('copying files', end='')
count = 0

match_pattern = '*.[jn][pe][gf]'

for photo in locate(match_pattern, root=source):
    print('.', end='')
    count += 1

    copy(photo, tempdir)

print(' done.  Copied {0} files.'.format(count))

print('auto rotate and date stamp photos...')

call('jhead -ft -autorot {0}/*.jpg'.format(tempdir))

print('copying photo files...')

call('exiftool "-Filename<CreateDate" -d /photos/%Y_%m_%d/%Y%m%d-%H%M%S%%-c.%%e {0}/'.format(tempdir))
Beispiel #9
0
import sys
from tempfile import mkdtemp

from locator import locate

source = sys.argv[1]
tempdir = mkdtemp()

print('creating temporary directory ' + tempdir)

print('copying files', end='')
count = 0

match_pattern = '*.[jn][pe][gf]'

for photo in locate(match_pattern, root=source):
    print('.', end='')
    count += 1

    copy(photo, tempdir)

print(' done.  Copied {0} files.'.format(count))

print('auto rotate and date stamp photos...')

call('jhead -ft -autorot {0}/*.jpg'.format(tempdir))

print('copying photo files...')

call(
    'exiftool "-Filename<CreateDate" -d /photos/%Y_%m_%d/%Y%m%d-%H%M%S%%-c.%%e {0}/'
Beispiel #10
0
def run_MC_3(gridsize: int, steps: int, MC_iterations: int, error=0.001) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    Meant fo error correction and error analysis
    
    This time this function returns full array of results, so that can be used again
    
    return 
        results_mat (N*2*2 np.ndarray)   in which [n,:,:] one confusion 4-matrix of single MC_simulation n
        error_info   
            #   0: number of found locations without error, 
            #   1: number of found locations with error, 
            #   2: if two both of those are equal to 1, 
            #           1 if the answer in error is correct, 0 else
        results_mat_corr with error correction
    """
    
    # all confusion matrices (like ...0100,0010...)
    results_mat = np.zeros((MC_iterations, 2, 2), dtype=int)
    
    error_info = np.zeros((MC_iterations, 3), dtype=int) 
    
    results_mat_corr = np.zeros((MC_iterations, 2, 2), dtype=int)
            
    
    height = gridsize
    width = gridsize
    
    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()
        
        
        err_hist = v.history_error(iteration_for_seed=run_idx, error=error)
        
        num_found_exact, x_ext, y_ext = locator.locate(v.map(), v.history())[0:3]
        num_found_inexact, x_err, y_err = locator.locate(v.map(), err_hist)[0:3]
        num_found_corrected, x_corr, y_corr = locator.locate_with_error_fallback(v.map(), err_hist)[0:3]
        
        true_pos = 0
        true_neg = 0
        false_pos = 0
        false_neg = 0
        
        true_pos_corr = 0
        true_neg_corr = 0
        false_pos_corr = 0
        false_neg_corr = 0
        
        error_info[run_idx, 0] = num_found_exact
        error_info[run_idx, 1] = num_found_inexact
        
        if (num_found_exact == 1) and (num_found_inexact == 1):
            true_pos += 1
            if not (x_err == x_ext) and (y_err == y_ext):
                error_info[run_idx, 2] = 1
                log.debug("Incorrect TP! args:", gridsize,steps,error)
                
        elif (num_found_exact != 1) and (num_found_inexact != 1):
            true_neg += 1
        elif (num_found_exact == 1) and (num_found_inexact != 1):
            false_neg += 1
        elif (num_found_exact != 1) and (num_found_inexact == 1):
            false_pos += 1
        
        # corrected
        if (num_found_exact == 1) and (num_found_corrected == 1):
            true_pos_corr += 1
        elif (num_found_exact != 1) and (num_found_corrected != 1):
            true_neg_corr += 1
        elif (num_found_exact == 1) and (num_found_corrected != 1):
            false_neg_corr += 1
        elif (num_found_exact != 1) and (num_found_corrected == 1):
            false_pos_corr += 1
        
        confusion_mat = np.array(((true_pos, false_neg), (false_pos, true_neg)))
        results_mat[run_idx,:,:] = confusion_mat
        
        conf_mat_corr = np.array(((true_pos_corr, false_neg_corr), (false_pos_corr, true_neg_corr)))
        results_mat_corr[run_idx,:,:] = confusion_mat

    return results_mat, error_info, results_mat_corr
Beispiel #11
0
def run_MC_4(gridsize: int, steps: int, MC_iterations: int, error=0.001) -> np.ndarray:
    """
    Yet another way to run calculations. This time thi just returns all values
    """
    # num found vector for every simulation in MC
    num_found_exact    = np.zeros((MC_iterations), dtype=int)
    num_found_inexact   = np.zeros((MC_iterations), dtype=int)
    num_found_corrected = np.zeros((MC_iterations), dtype=int)
    num_found_corrected_move = np.zeros((MC_iterations), dtype=int)
    
    # if TP then if location correct
    found_correct_exact    = np.zeros((MC_iterations), dtype=int)
    found_correct_inexact   = np.zeros((MC_iterations), dtype=int)
    found_correct_corrected = np.zeros((MC_iterations), dtype=int)
    found_correct_corrected_move = np.zeros((MC_iterations), dtype=int)
    
    
    
    
    min_moves = 9
    max_moves = 30
    required_moves = np.zeros(max_moves+1, dtype=int)

                
    
    height = gridsize
    width = gridsize
    
    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()
        
        
        err_hist = v.history_error(iteration_for_seed=run_idx, error=error)
        
        num_found_exact[run_idx], x_ext, y_ext = locator.locate(v.map(), v.history())[0:3]
        num_found_inexact[run_idx], x_err, y_err = locator.locate(v.map(), err_hist)[0:3]
        num_found_corrected[run_idx], x_corr, y_corr = locator.locate_with_error_fallback(v.map(), err_hist)[0:3]
        
        if num_found_exact[run_idx] == 1:
            found_correct_exact[run_idx] = 1
        
        if num_found_inexact[run_idx] == 1:
            if x_err == x_ext and y_err == y_ext:
                found_correct_inexact[run_idx] = 1
        
        if num_found_corrected[run_idx] == 1:
            if x_corr == x_ext and y_corr == y_ext:
                found_correct_corrected[run_idx] = 1
                
    
        # now the moving error correction from 'movement.py'
        
        v2 = vehicle.Vehicle(m, 10, error=error)
        num_found_corrected_move[run_idx], x_mov, y_mov, possible_loc, movements = \
            locator.locate_with_movement_and_error_fallback(m, v2, max_moves, min_moves=min_moves)

        if num_found_corrected_move[run_idx] == 1:
            correct_x, correct_y, direction = v2.location()
            if x_mov == correct_x and y_mov == correct_y:
                found_correct_corrected_move[run_idx] = 1
                required_moves[movements] += 1
        
    return num_found_exact, num_found_inexact, num_found_corrected, num_found_corrected_move, \
            found_correct_exact, found_correct_inexact, found_correct_corrected, found_correct_corrected_move, required_moves
 def test_east_half_street(self):
     test_address = "3000 E 14th 1/2 st"
     results = locate(test_address)
     self.assertTrue(num_results(results) > 0)
     self.failUnlessEqual(top_match(results),"3006 E 14TH HALF ST")
 def test_full_address(self):
     test_address = "1124 S IH 35"
     results = locate(test_address)
     self.assertTrue(num_results(results) > 0)
     self.failUnlessEqual(top_match(results),"1124 S IH 35 SVRD SB")
def main(fdt, ldt, lateness_threshold_mins, send_notification, no_lights):
    r = requests.get(BASE_URL % (conf.ROUTES,))
    j = r.json()

    logging.debug("Retrieved at: %s",
                  datetime.fromtimestamp(j["timestamp"]).ctime())
    # Save the realtime data for troubleshooting and verification
    logging.debug("JSON data follows:")
    logging.debug(r.text)

    logging.debug("Looking for arrivals between %s and %s", fdt, ldt)
    # Trains only appear in the vehicles list once they have actually departed.
    # Trains that are past their departure time ("start") but have not left
    #  their origin are not listed in vehicles until they've actually left
    #  the station.
    trips = []
    for tripId in [v["tripId"] for v in j["vehicles"]
                   if v["route"] == conf.ROUTES]:
        trips.append(extract_trip(j, tripId, fdt, ldt))

    notification_lines = []
    short_summary_lines = []
    full_summary_lines = []
    trains_are_running_late = False
    for t in trips:
        if t.is_current() and t.arrives_in_departure_window():
            if t.is_running_late(lateness_threshold_mins):
                trains_are_running_late = True
                notification_lines.append(t.short_summary())
            short_summary_lines.append(t.short_summary())
            full_summary_lines.append(t.full_summary())
        else:
            full_summary_lines.append(t.full_summary())

    if len(notification_lines) == 0:
        notification_subject = "All trains on time"
    elif len(notification_lines) == 1:
        notification_subject = "1 train running late"
    else:
        notification_subject = "%s trains running late" % \
                               (len(notification_lines),)

    logging.info(notification_subject)
    notification_lines.append("Retrieved at: %s" %
                              (datetime.fromtimestamp(j["timestamp"]).ctime(),))
    notification_message = "\n".join(notification_lines)

    notification_device_location = locator.locate(
        conf.ADDRESS_NAME_PAIR_LISTS,
        conf.LOCATION_PING_PERIOD_SECS)
    if send_notification == SEND_NOTIFICATION_ALWAYS or \
            (send_notification != SEND_NOTIFICATION_NEVER and
             trains_are_running_late and
             notification_device_location in conf.NOTIFICATION_LOCATIONS):
        logging.info("Sending pushover notification. Subject: %s",
                     notification_subject)
        logging.info("Notification message: %s", notification_message)
        request = notifier.send_pushover_notification(notification_message,
                                                      notification_subject)
        logging.debug("Request is %s", request)
    else:
        logging.info("Not sending pushover notification")

    if not no_lights:
        light_set_status = notifier.set_lamp_state(trains_are_running_late)
        if light_set_status == notifier.LIGHT_SET_OK:
            logging.debug("Light operations successful")
        elif light_set_status == notifier.LIGHT_SET_FAILED_BRIDGE_COMMS:
            logging.error("Unable to perform light operations - timeout to bridge")
    else:
        logging.debug("Not turning on lights because --no_lights cmdline param")

    logging.debug("--- Short summary start ---")
    logging.debug(short_summary_lines)
    logging.debug("--- Full summary start ---")
    logging.debug(full_summary_lines)