Exemplo n.º 1
0
def handle_argument(argument):
    if argument == "-all":
        test_all()
    elif argument == "-example":
        if len(argv) != 3:
            print("-example takes as input the name of the example to run. Please run with 'python main.py -example NAME_OF_EXAMPLE.graph'")
            return
        run_test(argv[2])
    elif argument == "-file":
        if len(argv) != 3:
            print("-file takes as input the name of the example to run. Please run with 'python main.py -file PATH/TO/FILE.graph'")
            return
        run_file(argv[2])
    else:
        print("Unrecognized command.")
Exemplo n.º 2
0
def test_reformatting(out_test_dir):
    out_test_dir = os.path.expanduser(out_test_dir)
    test_dir = out_test_dir + '/tmp'
    if (os.path.isdir(out_test_dir)
        ):  # Warn if output directory already exists
        log.disable(log.NOTSET)  # flip logging back on
        log.warning(
            'Your output directory already exists. Your output directory may already contain output from Fasta-O-Matic in %(out_test_dir)s.'
            % locals())
        log.disable(log.ERROR)  # disable most log output again
    else:
        assert general.mk_out_sub_directory(
            out_test_dir
        ), 'Failed to create output directory. Check that your output directory path.'
    assert general.mk_out_sub_directory(
        test_dir
    ), 'Failed to create output sub-directory for Unit testing. Check that your output directory exists and can be written to.'
    assert (test.test_all(test_dir)
            ), 'Failed to reformat when all three steps were called'
    assert test.test_newline(
        test_dir
    ), 'Failed to reformat when only newline and header reformatting was used'
    assert test.test_wrapping(
        test_dir
    ), 'Failed to reformat when only wrapping and header reformatting was used'
    assert test.test_unique(
        test_dir
    ), 'Failed to reformat or die when only testing uniqueness validation and/or reformatting for headers'
    os.rmdir(test_dir)
def test_reformatting(out_test_dir):
    out_test_dir = os.path.expanduser(out_test_dir)
    test_dir = out_test_dir + '/tmp'
    if (os.path.isdir(out_test_dir)): # Warn if output directory already exists
        log.disable(log.NOTSET) # flip logging back on
        log.warning('Your output directory already exists. Your output directory may already contain output from Fasta-O-Matic in %(out_test_dir)s.' % locals())
        log.disable(log.ERROR) # disable most log output again
    else:
        assert general.mk_out_sub_directory(out_test_dir), 'Failed to create output directory. Check that your output directory path.'
    assert general.mk_out_sub_directory(test_dir), 'Failed to create output sub-directory for Unit testing. Check that your output directory exists and can be written to.'
    assert (test.test_all(test_dir)), 'Failed to reformat when all three steps were called'
    assert test.test_newline(test_dir), 'Failed to reformat when only newline and header reformatting was used'
    assert test.test_wrapping(test_dir), 'Failed to reformat when only wrapping and header reformatting was used'
    assert test.test_unique(test_dir), 'Failed to reformat or die when only testing uniqueness validation and/or reformatting for headers'
    os.rmdir(test_dir)
Exemplo n.º 4
0
def quick_sort(input_list):
    if input_list not in [None, []]:
        lows = []
        equal = []
        highs = []

        if len(input_list) > 1:
            pivot = input_list[0]
            for x in input_list:
                if x < pivot:
                    lows.append(x)
                elif x == pivot:
                    equal.append(x)
                elif x > pivot:
                    highs.append(x)
            return quick_sort(lows) + equal + quick_sort(highs)
    return input_list


sort_methods = {
    'bubble sort': bubble_sort,
    'count sort': count_sort,
    'radix sort (base64)': lambda l: radix_sort(l, base=64),
    'radix sort (base256)': lambda l: radix_sort(l, base=256),
    'merge sort': merge_sort,
    'quick_sort': quick_sort
}

test_all(sort_methods)
Exemplo n.º 5
0
from test import test_all, run_test, run_file
from sys import argv

def handle_argument(argument):
    if argument == "-all":
        test_all()
    elif argument == "-example":
        if len(argv) != 3:
            print("-example takes as input the name of the example to run. Please run with 'python main.py -example NAME_OF_EXAMPLE.graph'")
            return
        run_test(argv[2])
    elif argument == "-file":
        if len(argv) != 3:
            print("-file takes as input the name of the example to run. Please run with 'python main.py -file PATH/TO/FILE.graph'")
            return
        run_file(argv[2])
    else:
        print("Unrecognized command.")

if len(argv) == 1:
    print("Did not receive a commandline argument. Running all tests by default.")
    test_all()
handle_argument(argv[1])
Exemplo n.º 6
0
    x_train = x_train[:args.train_num]
    y_train = y_train[:args.train_num]
    # define model
    if args.dataset != 4:
        model, eval_model, manipulate_model = CapsNet(
            input_shape=x_train.shape[1:],
            n_class=len(np.unique(np.argmax(y_train, 1))),
            routings=args.routings,
            l1=args.l1)
    else:
        model, eval_model, manipulate_model = CapsNet_for_big(
            input_shape=x_train.shape[1:],
            n_class=len(np.unique(np.argmax(y_train, 1))),
            routings=args.routings,
            l1=args.l1)
    model.summary()

    # train or test
    if args.weights is not None:  # init the model weights with provided one
        model.load_weights(args.weights)
    if not args.testing:
        train(model=model, data=(x_train, y_train), args=args)
    else:  # as long as weights are given, will run testing
        if args.weights is None:
            #print('No weights are provided. Will test using random initialized weights.')
            test_all(eval_model, (x_test, y_test), args.dataset)
        #manipulate_latent(manipulate_model, (x_test, y_test), args)
        #save_for_gif(manipulate_model, (x_test, y_test), args)
        #test(model=eval_model, data=(x_test, y_test), args=args)
Exemplo n.º 7
0
def test():
    test_all()
Exemplo n.º 8
0
    def train(self):
        if self.config['is_train']:
            data_dir_ir = 'data/ino_24_17/Train_ir_aug'
            data_dir_vi = 'data/ino_24_17/Train_vi_aug'
        else:
            data_dir_ir = 'data/ino_24_17/Test_ir'
            data_dir_vi = 'data/ino_24_17/Test_vi'

        # image_size = 132, label_size = 120, stride = 14
        # get all patches and labels  (37710, 132, 132, 1) (37710, 120, 120, 1)
        train_data_ir, train_label_ir = preprocessing.get_images2(
            data_dir_ir, self.config['image_size'], self.config['label_size'],
            self.config['stride'])
        train_data_vi, train_label_vi = preprocessing.get_images2(
            data_dir_vi, self.config['image_size'], self.config['label_size'],
            self.config['stride'])
        random_index = torch.randperm(
            len(train_data_ir))  # a list of random indices
        # shuffle randomly
        train_data_vi = train_data_vi[random_index]
        train_data_ir = train_data_ir[random_index]
        train_label_vi = train_label_vi[random_index]
        train_label_ir = train_label_ir[random_index]
        batch_size = self.config['batch_size']
        print('get patch done')

        if self.config['is_train']:
            with SummaryWriter(self.config['summary_dir']) as writer:
                batch_steps = len(train_data_ir) // batch_size
                epochs = self.config['epoch']
                # for each epoch
                for epoch in range(1, 1 + epochs):
                    d_loss_mean = 0
                    g_loss_mean = 0
                    content_loss_mean = 0
                    # for each step
                    for step in range(1, 1 + batch_steps):
                        print('Training. Epoch:%d/%d Step:%d/%d' %
                              (epoch, epochs, step, batch_steps))
                        start_idx = (step - 1) * batch_size
                        # (32, 132, 132, 1) -> (32, 1, 132, 132)
                        inf_x = train_data_ir[start_idx:start_idx +
                                              batch_size].transpose(
                                                  [0, 3, 1, 2])
                        inf_y = train_label_ir[start_idx:start_idx +
                                               batch_size].transpose(
                                                   [0, 3, 1, 2])
                        vis_x = train_data_vi[start_idx:start_idx +
                                              batch_size].transpose(
                                                  [0, 3, 1, 2])
                        vis_y = train_label_vi[start_idx:start_idx +
                                               batch_size].transpose(
                                                   [0, 3, 1, 2])

                        inf_x = torch.tensor(inf_x).float().to(device)
                        inf_y = torch.tensor(inf_y).float().to(device)
                        vis_x = torch.tensor(vis_x).float().to(device)
                        vis_y = torch.tensor(vis_y).float().to(device)

                        d_loss, g_loss, v_gan_loss, content_loss = self.train_step(
                            vis_x, inf_x, vis_y, inf_y, 2)
                        d_loss_mean += d_loss
                        g_loss_mean += g_loss
                        content_loss_mean += content_loss
                        print(
                            'Epoch {}/{}, Step {}/{}, gen loss = {:.4f}, v_gan_loss = {:.4f}, '
                            'content_loss {:.4f}, dis loss = {:.4f}'.format(
                                epoch, epochs, step, batch_steps, g_loss,
                                v_gan_loss, content_loss, d_loss))
                    test_all(
                        self.gen,
                        os.path.join(self.config['output'],
                                     'test{}'.format(epoch)))

                    d_loss_mean /= batch_steps
                    g_loss_mean /= batch_steps
                    content_loss_mean /= batch_steps
                    writer.add_scalar('scalar/gen_loss', g_loss_mean, epoch)
                    writer.add_scalar('scalar/dis_loss', d_loss_mean, epoch)
                    writer.add_scalar('scalar/content_loss', content_loss_mean,
                                      epoch)

                    # for name, param in self.gen.named_parameters():
                    #     if 'bn' not in name:
                    #         writer.add_histogram('gen/'+name, param, epoch)
                    #
                    # for name, param in self.dis.named_parameters():
                    #     if 'bn' not in name:
                    #         writer.add_histogram('dis/'+name, param, epoch)
            print('Saving model......')
            torch.save(self.gen.state_dict(),
                       '%s/final_generator.pth' % (self.config['output']))
            print("Training Finished, Total EPOCH = %d" % self.config['epoch'])