def handle_data():
    req_data = json.loads(request.data)
    result = []
    routeDatas = req_data['routeDatas']
    isSaved = req_data['isSaved']
    req_count = len(routeDatas)
    print 'req_count =', req_count
    i = 0
    j = 0
    for gps_point in routeDatas:
        if i % math.ceil(req_count * 0.05) == 0:
            noise_point = DataGenerator.addGaussNoise(gps_point, 0.0002)
            poi_point = POI_tagger.get_poi_by_point(noise_point)
            feature_point = DataGenerator.generate_feature(poi_point, crf_event_prob_map['value'])
            result.append(feature_point)
            j += 1
            # print j
        i += 1
    # print 'done'
    objid = 'null'
    if isSaved:
        objid = LeancloudUtils.save_to_leancloud(result)
    resp = make_response(json.dumps({'trace_id': objid, 'result': result}), 200)
    # resp.headers['Access-Control-Allow-Origin'] = '*'
    resp.headers["Access-Control-Allow-Headers"] = "content-type"
    return resp
Esempio n. 2
0
def main():
    global args, best_loss
    args = parser.parse_args()



    model = Model.ColorNet()
    model.cuda()
    criterion = nn.MSELoss().cuda()
    optimizer=torch.optim.Adam(model.parameters(),lr=0.1)

    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            best_prec1 = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
           print("=> no checkpoint found at '{}'".format(args.resume))


    if args.action = 'train':
        train_loader = dGenerator.makeDataLoader(args.data, 'train')

        val_loader = dGenerator.makeDataLoader(args.val, 'validate')

        for epoch in range(args.epoch):
            print('='*10+'epoch '+str(epoch)+'='*10)

            adjust_learning_rate(optimizer, epoch)

            Run.train(train_loader, model, criterion, optimizer, epoch)

            loss = Run.validate(val_loader, model, criterion)

            print('loss: '+str(loss)+'\n')


            is_best = loss < best_loss

            best_loss = min(loss, best_loss)

            save_checkpoint({
                'epoch': epoch + 1,
                'arch': 'inception_v3',
                'state_dict': model.state_dict(),
                'best_loss': best_loss,
                'optimizer': optimizer.state_dict(),
                }, is_best)
Esempio n. 3
0
def categorical_test():
    path = "../Data/Experiments/"
    train_rates = [0, 5, 10, 25]
    test_rates = [1, 5, 10, 25, 50, 100, 250, 500]
    anoms_rates = []
    for train_rate in train_rates:
        for test_rate in test_rates:
            anoms_rates.append((train_rate, test_rate))

    for i in range(len(anoms_rates)):
        print(anoms_rates[i])
        scores = []
        for run in range(RUNS):
            print("Run %i" % run)
            train_file = path + "%i_train_%i.csv" % (i, anoms_rates[i][0])
            test_file = path + "%i_test_%i.csv" % (i, anoms_rates[i][1])
            generator.create_shipment_data(10000, 10000, anoms_rates[i][0],
                                           anoms_rates[i][1], train_file,
                                           test_file)

            train_data = LogFile(train_file, ",", 0, 1000000, None, "Case")
            train_data.remove_attributes(["Anomaly"])
            test_data = LogFile(test_file,
                                ",",
                                0,
                                1000000,
                                None,
                                "Case",
                                values=train_data.values)

            model = edbn.train(train_data)
            edbn.test(test_data, path + "Output_%i_%i.csv" % anoms_rates[i],
                      model, "Anomaly", "0")

            output_file = path + "Output_%i_%i.csv" % anoms_rates[i]
            output_roc = path + "roc_%i_%i.png" % anoms_rates[i]
            output_prec = path + "prec_recall_%i_%i.png" % anoms_rates[i]

            score = plt.get_roc_auc(output_file)
            scores.append(plt.get_roc_auc(output_file))
            print("Score = %f" % score)

        with open(path + "results.txt", "a") as fout:
            fout.write("Testing:\ntrain rate: %i\ntest rate: %i\n" %
                       (anoms_rates[i][0], anoms_rates[i][1]))
            fout.write("Result: " + str(scores) + "\n")
            fout.write("Mean: %f Median: %f\n" %
                       (np.mean(scores), np.median(scores)))
            fout.write("Variance: %f\n\n" % np.var(scores))
Esempio n. 4
0
def InitDicoAircrafts():
    global dico_aircrafts
    # lbs
    f = open(PATH_RESOURCES + "\\..\\Data\\aircrafts", "r")
    dico_aircrafts = eval(f.read())
    f.close()

    ac = list(dico_aircrafts.keys())[0]
    if 'drag_coef' not in dico_aircrafts[ac].keys():
        print("generating dico values...")
        for ac in dico_aircrafts:
            dico_aircrafts[ac]["drag_coef"] = dg.InitDragCoef()
            dico_aircrafts[ac]["lift_coef"] = dg.InitLiftCoef()
            dico_aircrafts[ac]["surface"] = dg.InitSurface()
        f = open(PATH_RESOURCES + "\\..\\Data\\aircrafts", "w+")
        f.write(str(dico_aircrafts))
        f.close()

    else:
        print("getting dico values...")

    print(dico_aircrafts)
Esempio n. 5
0
def GetUSPressure(pa):
    return dg.PatoPSI(pa)
Esempio n. 6
0
def GetUSPower(w):
    return dg.WToHP(w)
Esempio n. 7
0
def GetUSSpeed(ms):
    return dg.MSToMPH(ms)
Esempio n. 8
0
def GetUSSurface(m2):
    return dg.M2ToFt2(m2)
Esempio n. 9
0
def GetUSTemp(deg):
    return dg.DegToK(deg)
Esempio n. 10
0
def GetUSDistance(m):
    return dg.MToFt(m)
Esempio n. 11
0
def GetRUMass(lbs):
    return dg.LbsToKg(lbs)
Esempio n. 12
0
def GenerateFile(f_id, f_name, records, start_tstp):

    f_type = "US"
    if f_name in c.US_AIRCRAFTS:
        f_type = "US"
        GetDate = GetUSDate
        GetMass = GetUSMass
        GetDistance = GetUSDistance
        GetTemp = GetUSTemp
        GetSurface = GetUSSurface
        GetPercentage = GetUSPercentage
        GetSpeed = GetUSSpeed
        GetPower = GetUSPower
        GetPressure = GetUSPressure
    else:
        f_type = "RU"
        GetDate = GetRUDate
        GetMass = GetRUMass
        GetDistance = GetRUDistance
        GetTemp = GetRUTemp
        GetSurface = GetRUSurface
        GetPercentage = GetRUPercentage
        GetSpeed = GetRUSpeed
        GetPower = GetRUPower
        GetPressure = GetRUPressure

    if not os.path.exists(c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
                          c.CURRENT_CONDITION[1] + "\\" + f_type):
        os.mkdir(c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
                 c.CURRENT_CONDITION[1] + "\\" + f_type)

    ## Header
    meta = {}
    meta['origin'] = f_type
    meta['nb_motor'] = dg.GetMotor(f_name)
    meta["drag_coef"] = dg.GetDragCoef(f_name)
    meta["lift_coef"] = dg.GetLiftCoef(f_name)
    meta["mass"] = GetMass(dg.GetMass(f_name))  # from lbs
    meta["mass fuel"] = GetMass(dg.GetMassFuel(f_name))  # from lbs
    meta['surface'] = GetSurface(dg.GetSurface(f_name))  # from m2
    # for computations only
    meta["mass_kg"] = dg.LbsToKg(dg.GetMass(f_name))
    meta["mass_fuel_kg"] = dg.LbsToKg(dg.GetMassFuel(f_name))
    meta["surface_m2"] = dg.GetSurface(f_name)

    ## Values
    #        [degree,     degree,     m,          degree,    degree, deg, m,    m,  degree]
    names = [
        "longitude", "latitude", "altitude", "roll", "pitch", "yaw", "u", "v",
        "heading"
    ]
    dico = GetDico(records, names, f_id, start_tstp)

    # get speed
    # m / s
    #names.append("speed_uv")
    dico['vx'], dico['vy'], dico['vz'], dico['speed_uv'] = dg.GetSpeedUV(dico)

    # generate air speed
    # m / s
    names.append("air_speed")
    dico['air_x'], dico['air_y'], dico['air_z'], dico[
        'air_speed'] = dg.GetAirSpeed(dico)

    # then compute engine(s) power
    # W
    engines, names, dico = dg.GetEnginePower2(names, dico, meta)

    # COCKPIT

    # celsius
    col = "temperature_in"
    names.append(col)
    dico = dg.GetTemperatureCockpit(dico, col)

    # % (e.g., 70)
    col = "humidity_in"
    names.append(col)
    dico = dg.GetHumidityCockpit(dico, col)

    # Pa
    col = "pressure_in"
    names.append(col)
    dico = dg.GetPressureCockpit(dico, col)

    # BPM
    col = "heart_rate"
    names.append(col)
    dico = dg.GetHeartRate(dico, col)

    # % (e.g., 70)
    col = "oxygen_mask"
    names.append(col)
    dico = dg.GetOxygenMask(dico, col)

    ## convert values if necessary
    ## use agnostic function (so check them again!)
    dico['altitude'] = GetDistance(dico['altitude'])
    dico['u'] = GetDistance(dico['u'])
    dico['v'] = GetDistance(dico['v'])

    dico['air_speed'] = GetSpeed(dico['air_speed'])
    for e in engines:
        dico[e] = GetPower(dico[e])

    dico['temperature_in'] = GetTemp(dico['temperature_in'])
    dico['humidity_in'] = GetPercentage(dico['humidity_in'])
    dico['pressure_in'] = GetPressure(dico['pressure_in'])
    dico['oxygen_mask'] = GetPercentage(dico['oxygen_mask'])

    names.insert(0, "timestamp")
    names.remove("u")
    names.remove("v")

    f = None
    head = GetHeader(f_id, f_name, start_tstp, meta)

    if c.CURRENT_CONDITION[0] == c.NORMAL[0]:
        f = open(
            c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
            c.CURRENT_CONDITION[1] + "\\" + f_type + "\\" +
            str(c.num_condition) + "_" + f_id + "_" + f_name + ".csv", "w+")
        f.write(head)
        LogDico(f, names, dico)
        f.close()
        return

    elif c.CURRENT_CONDITION[0] == c.MISSING_HEADER[0]:
        f = open(
            c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
            c.CURRENT_CONDITION[1] + "\\" + f_type + "\\" +
            str(c.num_condition) + "_" + f_id + "_" + f_name + ".csv", "w+")
        #f.write(head)
        LogDico(f, names, dico)
        f.close()
        return

    elif c.CURRENT_CONDITION[0] == c.INCOMPLETE_HEADER[0]:
        tab = head.split('\n')
        for cur in range(0, len(tab)):
            t2 = [x for i, x in enumerate(tab) if i != cur]
            s = "\n".join(t2)
            f = open(
                c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
                c.CURRENT_CONDITION[1] + "\\" + f_type + "\\" +
                str(c.num_condition) + "_" + str(cur) + f_id + "_" + f_name +
                ".csv", "w+")
            f.write(head)
            LogDico(f, names, dico)
            f.close()
        return

    elif c.CURRENT_CONDITION[0] == c.MISSING_COLUMN[0]:
        for removed in range(0, len(names)):
            print(str(removed) + " / " + str(len(names)))
            names2 = list(names)
            names2.pop(removed)
            dico2 = {}
            for cur in names2:
                dico2[cur] = dico[cur]
            f = open(
                c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
                c.CURRENT_CONDITION[1] + "\\" + f_type + "\\" +
                str(c.num_condition) + "_" + str(removed) + f_id + "_" +
                f_name + ".csv", "w+")
            f.write(head)
            LogDico(f, names2, dico2)
            f.close()
        return

    else:
        f = open(c.PATH_RESOURCES + "\\" + c.OUT_FOLDER + "\\" +
                 c.CURRENT_CONDITION[1] + "\\" + f_type + "\\" +
                 str(c.num_condition) + "_" + f_id + "_" + f_name + ".csv",
                 "w+",
                 encoding='utf-8')
        f.write(head)
        LogDico(f, names, dico)
        f.close()
        return
Esempio n. 13
0
    train_rates = [0, 5, 10, 25]
    test_rates = [1, 5, 10, 25, 50, 100, 250, 500]
    anoms_rates = []
    for train_rate in train_rates:
        for test_rate in test_rates:
            anoms_rates.append((train_rate, test_rate))

    for i in range(len(anoms_rates)):
        print(anoms_rates[i])
        scores = []
        for run in range(1):
            print("Run %i" % run)
            train_file = path + "%i_train_%i.csv" % (i, anoms_rates[i][0])
            test_file = path + "%i_test_%i.csv" % (i, anoms_rates[i][1])
            generator.create_shipment_data(10000, 10000, anoms_rates[i][0],
                                           anoms_rates[i][1], train_file,
                                           test_file)

            train_date = LogFile(train_file, ",", 0, 1000000, None, "Case")
            train_date.remove_attributes(["Anomaly"])
            test_date = LogFile(test_file,
                                ",",
                                0,
                                1000000,
                                None,
                                "Case",
                                string_2_int=train_date.string_2_int,
                                int_2_string=train_date.int_2_string)
            model = edbn.train(train_date)
            edbn.test(test_date, path + "Output_%i_%i.csv" % anoms_rates[i],
                      model, "Anomaly", "0")
Esempio n. 14
0
# https://aviation.stackexchange.com/questions/46518/what-is-the-typical-value-of-maximum-lift-coefficient-for-aerobatic-aircraft
LIFTS = [x * 0.1 for x in range(10, 20, 1)]
LIFT_FACTOR = 0.4

# kg
MASS = [x for x in range(10000, 20000, 500)]
MASS_FACTOR = 1000

# https://en.wikipedia.org/wiki/Wing_loading
# same for drag (asked Philippe)
# m^2
WING_AREA = [x for x in range(20, 25, 1)]
WING_FACTOR = 1

# m
AIR_LEVELS = [dg.FtToM(x) for x in [-1000, 1600, 6600, 26000]]

# m
EARTH_RADIUS = 6371 * 1000

# C
T_COCKPIT = 25
# https://www.researchgate.net/publication/276074552_Simulation_for_temperature_control_of_a_military_aircraft_cockpit_to_avoid_pilot%27s_thermal_stress
# > +10 deg (sometimes > +45 deg) compared to ambient temp.
T_NOISE = 15
T_EVENT = +20

# https://www.google.com/search?client=firefox-b-d&q=heart+rate+data+bpm+fighter+jet+pilot
BPM = 90
BPM_NOISE = 20
BPM_EVENT = +40