示例#1
0
文件: app.py 项目: varnika2212/third
def result():
    if request.method == 'POST':
        start_time = request.form.get('st')
        end_time = request.form.get('et')
        counter = 0
        id_dict = {}
        answer = {}
        if (validate(start_time) and validate(end_time)):
            start = datetime.strptime(start_time, '%Y-%m-%dT%H:%M:%S%z')
            end = datetime.strptime(end_time, '%Y-%m-%dT%H:%M:%S%z')
            with open('json_3.json') as f:
                J = json.load(f)
                L = []

                for item in J:
                    i = item['id']
                    s = 0
                    if (item['state']):
                        s = 1
                    b1 = item['belt1']
                    b2 = item['belt2']
                    t = datetime.strptime(item['time'], '%Y-%m-%d %H:%M:%S')
                    t = t.replace(tzinfo=timezone.utc)
                    L.append(
                        (t, int(remove_alphabets(i)), (1 - s) * b1, s * b2))
                    # print((t.time(), int(remove_alphabets(i)), (1 - s) * b1, s * b2))

                for item in L:
                    t = item[0]
                    i = item[1]
                    b1 = item[2]
                    b2 = item[3]
                    if t >= start and t <= end:
                        tmp = (0, 0, 0)
                        if i in id_dict:
                            tmp = id_dict[i]
                        id_dict[i] = (b1 + tmp[0], b2 + tmp[1], 1 + tmp[2])
                for key in id_dict:
                    tmp = id_dict[key]
                    answer[key] = (tmp[0] / tmp[2], tmp[1] / tmp[2])
        answer = sorted(answer.items())
        print(answer)
        l = []
        for key in answer:
            dic = {}
            dic["id"] = key[0]
            dic["avg_belt1"] = key[1][0]
            dic["avg_belt2"] = key[1][1]
            l.append(dic)

    return jsonify(l)
示例#2
0
    def get_path_filename(self, obj):
        ''' Get the path and filename and build it into protocol://path
        '''
        obj['Filename'] = obj['Path'].rsplit(
            '\\', 1)[1] if '\\' in obj['Path'] else obj['Path'].rsplit('/',
                                                                       1)[1]

        if self.direct_path:

            if not validate(obj['Path']):
                raise PathValidationException(
                    "Failed to validate path. User stopped.")

            obj['Path'] = obj['Path'].replace(obj['Filename'], "")

        else:
            obj['Path'] = "plugin://plugin.video.jellyfin/%s/" % obj[
                'LibraryId']
            params = {
                'filename': py2_encode(obj['Filename'], 'utf-8'),
                'id': obj['Id'],
                'dbid': obj['MvideoId'],
                'mode': "play"
            }
            obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params))
示例#3
0
    def get_path_filename(self, obj):
        ''' Get the path and filename and build it into protocol://path
        '''
        obj['Filename'] = obj['Path'].rsplit(
            '\\', 1)[1] if '\\' in obj['Path'] else obj['Path'].rsplit('/',
                                                                       1)[1]

        if self.direct_path:

            if not validate(obj['Path']):
                raise Exception("Failed to validate path. User stopped.")

            obj['Path'] = obj['Path'].replace(obj['Filename'], "")
            '''check bluray directries and point it to ./BDMV/index.bdmv'''
            if validate_bluray_dir(obj['Path'] + obj['Filename']):
                obj['Path'] = obj['Path'] + obj['Filename'] + '/BDMV/'
                obj['Filename'] = 'index.bdmv'
                LOG.debug("Bluray directry %s", obj['Path'])

        else:
            obj['Path'] = "plugin://plugin.video.jellyfin/%s/" % obj[
                'LibraryId']
            params = {
                'filename': py2_encode(obj['Filename'], 'utf-8'),
                'id': obj['Id'],
                'dbid': obj['MovieId'],
                'mode': "play"
            }
            obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params))
示例#4
0
    def testValidate1(self):
        yamlStr = '\n'.join([
            "{",
            "  example: {",
            "    test: /^src//,",
            "    script: stack test,",
            "  }",
            "}"
        ])
        configRaw = yaml.load(yamlStr)

        expected = True
        actual = validate(configRaw)
        self.assertEqual(expected, actual)
示例#5
0
    def get_path_filename(self, obj):
        ''' Get the path and build it into protocol://path
        '''
        if self.direct_path:

            if '\\' in obj['Path']:
                obj['Path'] = "%s\\" % obj['Path']
                obj['TopLevel'] = "%s\\" % dirname(dirname(obj['Path']))
            else:
                obj['Path'] = "%s/" % obj['Path']
                obj['TopLevel'] = "%s/" % dirname(dirname(obj['Path']))

            if not validate(obj['Path']):
                raise Exception("Failed to validate path. User stopped.")
        else:
            obj['TopLevel'] = "plugin://plugin.video.emby.tvshows/"
            obj['Path'] = "%s%s/" % (obj['TopLevel'], obj['Id'])
示例#6
0
    def get_song_path_filename(self, obj, api):

        ''' Get the path and filename and build it into protocol://path
        '''
        obj['Path'] = api.get_file_path(obj['Path'])
        obj['Filename'] = obj['Path'].rsplit('\\', 1)[1] if '\\' in obj['Path'] else obj['Path'].rsplit('/', 1)[1]

        if self.direct_path:

            if not validate(obj['Path']):
                raise PathValidationException("Failed to validate path. User stopped.")

            obj['Path'] = obj['Path'].replace(obj['Filename'], "")

        else:
            server_address = self.server.auth.get_server_info(self.server.auth.server_id)['address']
            obj['Path'] = "%s/Audio/%s/" % (server_address, obj['Id'])
            obj['Filename'] = "stream.%s?static=true" % obj['Container']
示例#7
0
    def testValidate9(self):
        yamlStr = '\n'.join([
            "{",
            "  example1: {",
            "    test: /^src//,",
            "    script: stack test,",
            "    other: additional,",
            "  },",
            "  example2: {",
            "    test: /^src//,",
            "    script: stack test,",
            "  }",
            "}"
        ])
        configRaw = yaml.load(yamlStr)

        expected = False
        actual = validate(configRaw)
        self.assertEqual(expected, actual)
示例#8
0
    def get_path_filename(self, obj):

        ''' Get the path and filename and build it into protocol://path
        '''
        obj['Filename'] = obj['Path'].rsplit('\\', 1)[1] if '\\' in obj['Path'] else obj['Path'].rsplit('/', 1)[1]

        if self.direct_path:

            if not validate(obj['Path']):
                raise Exception("Failed to validate path. User stopped.")

            obj['Path'] = obj['Path'].replace(obj['Filename'], "")

        else:
            obj['Path'] = "plugin://plugin.video.emby.movies/"
            params = {
                'filename': obj['Filename'].encode('utf-8'),
                'id': obj['Id'],
                'dbid': obj['MovieId'],
                'mode': "play"
            }
            obj['Filename'] = "%s?%s" % (obj['Path'], urllib.urlencode(params))
示例#9
0
                                          shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=64)
validloader = torch.utils.data.DataLoader(validate_data, batch_size=64)

if args.arch == 'vgg':
    input_size = 25088
    model = models.vgg16(pretrained=True)
elif args.arch == 'resnet':
    input_size = 2048
    model = models.alexnet(pretrained=True)

for param in model.parameters():
    param.requires_grad = False
model.classifier = nn.Sequential(nn.Linear(input_size, args.hidden_layers),
                                 nn.ReLU(), nn.Dropout(p=0.5),
                                 nn.Linear(args.hidden_layers, 102),
                                 nn.LogSoftmax(dim=1))
print(model)

criterion = nn.NLLLoss()
device = args.gpu
optimizer = optim.Adam(model.classifier.parameters(), args.lr)
loss, accuracy = helper.validate(model, criterion, testloader, device)
print(f"loss: {loss} \n Accuracy: {accuracy}")
epochs = args.epochs
model = helper.train(model, optimizer, criterion, epochs, trainloader,
                     validloader, device)
helper.accuracy(model, testloader, device)
helper.save(model, train_data, args.arch, input_size, args.hidden_layers,
            epochs, args.lr)
示例#10
0
def myTradingSystem(DATE, CLOSE, settings):
    ### 0.SET UP
    # num and build: counter used to retrain the model by interval
    # settings['train_period']: record individual market's lookback period found in the last retrain: at the start
    # settings['accuracies']: record the accuracy achieved in the last retain using the recorded lookback period and test period for each market
    # filtered: counter of how many markets are removed due to inaccurate model or non-convergent problem
    num = settings['counter'] % interval
    build = num == 0
    nMarkets = CLOSE.shape[1]
    weights = np.zeros(nMarkets)
    filtered = 0
    date = DATE[-1]
    settings['counter'] += 1
    if settings[
            'counter'] == 1:  ###first day set up the lookback by search from 50~500
        periods, accuracies = set_train_period(CLOSE, settings['train_period'],
                                               settings['accuracies'])
        settings['train_period'] = periods
        settings['accuracies'] = accuracies

    ### 1. Data preprocessing: from raw data to input
    # CLOSE: log the prices to 1)linearize the trend 2)reduce the impact of different price scale of different markets
    # CLOSE: remove consecutive duplicates as that will cause the model to produce nan values when it detects constant pattern
    print('\n{}'.format(date))
    for i in range(1, nMarkets):
        cur = np.log(np.array([k for k, g in groupby(CLOSE[:, i])]))
        pred, model = None, None

        ### 2. Model building: process input to produce predictions
        # prev_period: the last lookback period used for this market in the last retrain; might be ajusted slightly if another number can achieve better performance
        # train_period, model, accuracy: the adjusted lookback period, model trained using the new period and the accuracy for the retrained model in the validation data
        if build:
            prev_period = settings['train_period'][i]
            train_period, model, accuracy = adjust_period(cur, prev_period)

            ### 3. Model filtering
            # threshold: if the accuracy is less than the threshold, exclude the market in the final output to avoid loss
            # settings['models']: if the model achieves acceptable accuracy, save it for next day prediction until next retrain
            if accuracy < threshold:
                model = None
                print(' {}%==={}: inaccurate'.format(int(100 * accuracy),
                                                     settings['markets'][i]))
            settings['models'][i] = model
            settings['train_period'][i] = train_period
            settings['accuracies'][i] = accuracy
        else:
            model = settings['models'][i]
            accuracy = settings['models'][i]
            accuracy = settings['accuracies'][i]

        if model:
            ### 4. Prediction and equity allocation
            # pred: the HW model will predict the next log(price)
            # weights: take the predicted log(price) and minus the latest observed log(price) as the final output weights
            pred = validate(cur[-1], model.forecast(num + 1)[-1])
            if not np.isnan(pred):
                print(' {}%==={}'.format(int(accuracy * 100),
                                         settings['markets'][i]))
                weights[i] = pred - cur[-1]
            else:
                print(' {}%==={}: non-convergent'.format(
                    int(accuracy * 100), settings['markets'][i]))

    acc = settings['accuracies']
    included = np.nonzero(weights)[0]
    settings['num_trades'].append(included.size)
    print("(average daily trades/ total markets): ({}/{})".format(
        round(np.mean(settings['num_trades']), 2), nMarkets - 1))

    if cluster_on and num_cluster > 1:
        clusters, weightage = cluster(CLOSE[-cluster_period:,
                                            included], num_cluster,
                                      np.array(acc)[included], dist_func,
                                      smooth_alpha)
        weights = group_norm(num_cluster, included, clusters,
                             np.array(weights), build, weightage)

    return weights, settings
示例#11
0
#!/bin/python3

from runTest import ConfigFile
import helper

if __name__ == "__main__":
    config = helper.loadFile(ConfigFile)
    if helper.validate(config):
        print("no problem is found")
    else:
        print("config file is invalid")
示例#12
0
def result():
    if request.method == 'POST':
        start_time = request.form.get('st')
        end_time = request.form.get('et')
        counter = 0
        counterAA = 0
        counterAB = 0
        counterBA = 0
        counterBB = 0
        counterCA = 0
        counterCB = 0
        counterDA = 0
        counterDB = 0

        ## J's Space

        ## V's Space

        if (validate(start_time) and validate(end_time)):
            start = datetime.strptime(start_time, '%Y-%m-%dT%H:%M:%S%z')
            end = datetime.strptime(end_time, '%Y-%m-%dT%H:%M:%S%z')
            with open('data2.json') as f:
                data_list = json.load(f)
                shifta_start = datetime.strptime('00:30:01', '%H:%M:%S')
                shifta_end = datetime.strptime('08:30:00', '%H:%M:%S')
                shiftb_start = datetime.strptime('08:30:01', '%H:%M:%S')
                shiftb_end = datetime.strptime('14:30:00', '%H:%M:%S')
                shiftc_1_start = datetime.strptime('14:30:01', '%H:%M:%S')
                shiftc_1_end = datetime.strptime('23:59:59', '%H:%M:%S')
                shiftc_2_start = datetime.strptime('00:00:00', '%H:%M:%S')
                shiftc_2_end = datetime.strptime('00:30:00', '%H:%M:%S')
                shifta_start = shifta_start.time()
                shifta_end = shifta_end.time()
                shiftb_start = shiftb_start.time()
                shiftb_end = shiftb_end.time()
                shiftc_1_start = shiftc_1_start.time()
                shiftc_1_end = shiftc_1_end.time()
                shiftc_2_start = shiftc_2_start.time()
                shiftc_2_end = shiftc_2_end.time()
                dbg = True
                if (dbg):
                    print("DEBUG_START")
                    print("Time entered : ", start, " to ", end)
                    print("Shift A : ", shifta_start, " to ", shifta_end)
                    print("Shift B : ", shiftb_start, " to ", shiftb_end)
                    print("Shift C : ", shiftc_1_start, " to ", shiftc_1_end)
                    print("Shift C : ", shiftc_2_start, " to ", shiftc_2_end)
                for item in data_list:
                    t = datetime.strptime(item['time'], '%Y-%m-%d %H:%M:%S')
                    t = t.replace(tzinfo=timezone.utc)
                    if t >= start and t <= end:
                        if (dbg):
                            print("Counter is ", counter, " Item time is : ",
                                  item['time'], " t is ", t)
                        nt = t.time()
                        if (item['production_A'] == True):
                            if (dbg):
                                print("Found A at counter index : ", counter,
                                      ", item is ", item)
                            if (nt >= shifta_start and nt <= shifta_end):
                                counterAA += 1
                            if (nt >= shiftb_start and nt <= shiftb_end):
                                counterBA += 1
                            if (nt >= shiftc_1_start and nt <= shiftc_1_end):
                                counterCA += 1
                            if (nt >= shiftc_2_start and nt <= shiftc_2_end):
                                counterDA += 1

                        if (item['production_B'] == True):
                            if (dbg):
                                print("Found B at counter index : ", counter,
                                      ", item is ", item)
                            if (nt >= shifta_start and nt <= shifta_end):
                                counterAB += 1
                            if (nt >= shiftb_start and nt <= shiftb_end):
                                counterBB += 1
                            if (nt >= shiftc_1_start and nt <= shiftc_1_end):
                                counterCB += 1
                            if (nt >= shiftc_2_start and nt <= shiftc_2_end):
                                counterDB += 1
                        counter += 1

        dic = {
            'shiftA': {
                'production_A_count': counterAA,
                'production_B_count': counterAB
            },
            'shiftB': {
                'production_A_count': counterBA,
                'production_B_count': counterBB
            },
            'shiftC': {
                'production_A_count': counterCA + counterDA,
                'production_B_count': counterCB + counterDB
            }
        }
        # jo = json.dumps(dic)

    return jsonify(dic)