Ejemplo n.º 1
0
def _format_simple_list(list_to_format):
    """Gets a list, and returns a formated string."""
    if len(list_to_format) == 0:
        return 'Empty result'

    max_item_length = 1
    for item in list_to_format:
        if not isinstance(item, basestring):
            item = str(item)
        max_item_length = max(max_item_length, len(item))

    max_item_length += len(ANSICOLORS.FBLUE)
    terminal_width, terminal_height = utils.terminal_size()

    number_of_columns_to_draw = min(terminal_width / (max_item_length + 2),
                                    len(list_to_format)) or 1

    columns_width = ((terminal_width - 4) / number_of_columns_to_draw) - 2

    table_border = ANSICOLORS.FBLUE % ('  ' + \
                                      ('+'  + \
                                      ('-'*(columns_width))) * \
                                      number_of_columns_to_draw + '+\n')

    result = table_border
    line = '  %s ' % ANSICOLORS.FBLUE % '|'

    current_column = 1
    for item in list_to_format:
        #Ensure that our item is string, and we can apply formatting on it.
        if not isinstance(item, basestring):
            item = str(item)

        #If the length of the item is greater that the column's width, cut it.
        if len(item) >= columns_width - 2:
            item = item[:columns_width - 5] + '...'

        line += '%-{0}s %s '.format(columns_width - 2) %\
                             (item, ANSICOLORS.FBLUE%'|')
        if current_column >= number_of_columns_to_draw:
            result += line + '\n'
            line = '  %s ' % ANSICOLORS.FBLUE % '|'
            current_column = 0

        current_column += 1

    if len(line) > (4 + len(ANSICOLORS.FBLUE)):
        #There's some another items in the current line,
        #don't forget to add them to the result, too!
        result += line + '\n'

    result += table_border
    result += ANSICOLORS.BGREEN % \
              ('   count: %s' % len(list_to_format)) + '\n'

    return result
Ejemplo n.º 2
0
def _format_object_as_tree(object_to_format):
    """Format the given object as a tree.
    
    @return: A string represents a tree.
    """
    raw_tree = _format_single_tree_node(object_to_format, 1)

    terminal_width, terminal_height = utils.terminal_size()
    #Cut the lines greater that terminal width
    formated_tree = ''
    tree_lines = raw_tree.splitlines()
    for line in tree_lines:
        if len(line) > terminal_width - 1:
            line = line[:len(line) - 4] + '...'
        formated_tree += line + '\n'

    return formated_tree
Ejemplo n.º 3
0
def test_namespace_types():
    """ Test instances of various SimpleNamespace subclasses """

    from pprint import pformat
    if __package__ is None or __package__ == '':
        from utils import terminal_size
    else:
        from .utils import terminal_size

    nsdata: tx.Dict[str, str] = {
        'yo': 'dogg',
        'i': 'heard',
        'you': 'liked',
        'namespaced': 'dictionary data'
    }

    simpleNamespace: types.SimpleNamespace = types.SimpleNamespace(**nsdata)
    namespace: Namespace[str, str] = Namespace(**nsdata)
    ocdNamespace: OCDNamespace[str, str] = OCDNamespace(**nsdata)
    sortedNamespace: SortedNamespace[str] = SortedNamespace(**nsdata)
    width: int = terminal_size().width

    print("=" * width)
    print("")

    print("• SIMPLE NAMESPACE: ")
    print(pformat(simpleNamespace, indent=4, depth=10, width=width))
    print("")

    print("• REGULAR NAMESPACE: ")
    print(pformat(namespace, indent=4, depth=10, width=width))
    print("")

    print("• OCD NAMESPACE: ")
    print(pformat(ocdNamespace, indent=4, depth=10, width=width))
    print("")

    print("• SORTED NAMESPACE: ")
    print(pformat(sortedNamespace, indent=4, depth=10, width=width))
    print("")
Ejemplo n.º 4
0
def main(args):
    # Device setting
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Data transformation & augmentation
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.Resize((args.resize_pixel, args.resize_pixel)),
            transforms.RandomAffine(args.random_affine),
            transforms.ColorJitter(brightness=(0.5, 2)),
            transforms.RandomResizedCrop(
                (args.resize_pixel, args.resize_pixel), scale=(0.85, 1)),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            transforms.RandomErasing(p=0.3, scale=(0.01, 0.05))
        ]),
        'valid':
        transforms.Compose([
            transforms.Resize((args.resize_pixel, args.resize_pixel)),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
        ])
    }

    # Train, valid data split
    train_img_list, valid_img_list = train_valid_split(
        random_seed=args.random_seed,
        data_path=args.data_path,
        valid_ratio=args.valid_ratio)

    # Custom dataset & dataloader setting
    image_datasets = {
        'train':
        CustomDataset(train_img_list,
                      isTrain=True,
                      transform=data_transforms['train']),
        'valid':
        CustomDataset(valid_img_list,
                      isTrain=True,
                      transform=data_transforms['valid'])
    }
    dataloaders = {
        'train':
        DataLoader(image_datasets['train'],
                   batch_size=args.batch_size,
                   shuffle=True,
                   num_workers=args.num_workers),
        'valid':
        DataLoader(image_datasets['valid'],
                   batch_size=args.batch_size,
                   shuffle=True,
                   num_workers=args.num_workers),
    }

    # Model Setting
    model = conv_model(efficientnet_not_use=False,
                       efficient_model_number=6,
                       letter_model_path=args.letter_model_path)
    criterion = nn.CrossEntropyLoss()
    # optimizer = optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr)
    optimizer = RAdam(params=filter(lambda p: p.requires_grad,
                                    model.parameters()),
                      lr=args.lr)
    # lr_step_scheduler = lr_scheduler.StepLR(optimizer,
    #                                         step_size=args.lr_step_size, gamma=args.lr_decay_gamma)
    lr_step_scheduler = lr_scheduler.ReduceLROnPlateau(
        optimizer, factor=args.lr_decay_gamma, patience=10)
    # lr_step_scheduler = WarmupLinearSchedule(optimizer,
    #                                          warmup_steps=round(len(dataloaders['train'])/args.num_epochs*0.1),
    #                                          t_total=round(len(dataloaders['train'])/args.num_epochs))
    model.to(device)

    ## Training
    # Initialize
    best_model_wts = copy.deepcopy(model.state_dict())
    best_loss = 9999999999
    early_stop = False

    # Train
    for epoch in range(args.num_epochs):
        print('#' * terminal_size())
        print('Epoch {}/{}'.format(epoch + 1, args.num_epochs))

        if early_stop:
            print('Early Stopping!!!')
            break

        for phase in ['train', 'valid']:
            if phase == 'train':
                model.train()
            else:
                model.eval()
            running_loss = 0.0
            running_corrects = 0
            start_time = time.time()

            # Iterate over data
            for inputs, letters, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = torch.tensor([int(x) for x in labels]).to(device)

                # Zero the parameter gradients
                optimizer.zero_grad()

                # Track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(inputs)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, labels)

                    # Backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        torch_utils.clip_grad_norm_(model.parameters(),
                                                    args.max_grad_norm)
                        optimizer.step()

                # Statistics
                running_loss += loss.item() * inputs.size(0)
                if phase == 'valid':
                    val_loss = running_loss
                running_corrects += torch.sum(preds == labels.data)

            # Epoch loss calculate
            epoch_loss = running_loss / len(image_datasets[phase])
            epoch_acc = running_corrects.double() / len(image_datasets[phase])

            if phase == 'valid' and epoch_loss < best_loss:
                best_epoch = epoch
                best_loss = epoch_loss
                best_model_wts = copy.deepcopy(model.state_dict())

            if phase == 'train' and epoch_loss < 0.001:
                early_stop = True

            spend_time = (time.time() - start_time) / 60
            print('{} Loss: {:.4f} Acc: {:.4f} Time: {:.3f}min'.format(
                phase, epoch_loss, epoch_acc, spend_time))
        # Learning rate scheduler
        lr_step_scheduler.step(val_loss)

    # Model Saving
    model.load_state_dict(best_model_wts)
    if not os.path.exists(args.save_path):
        os.mkdir(args.save_path)
    if not os.path.exists(os.path.join(args.save_path, 'digit')):
        os.mkdir(os.path.join(args.save_path, 'digit'))
    best_loss = round(best_loss, 4)
    save_path_ = os.path.join(
        os.path.join(args.save_path, 'digit'),
        str(datetime.datetime.now())[:10] + f'_{best_loss}')
    os.mkdir(save_path_)
    print('Best validation loss: {:.4f}'.format(best_loss))
    with open(os.path.join(save_path_, 'hyperparameter.json'), 'w') as f:
        json.dump(
            {
                'efficientnet_not_use': args.efficientnet_not_use,
                'efficientnet_model_number': args.efficientnet_model_number,
                'num_epochs': args.num_epochs,
                'resize_pixel': args.resize_pixel,
                'random_affine': args.random_affine,
                'lr': args.lr,
                'random_seed': args.random_seed,
                'best_loss': best_loss
            }, f)
    torch.save(model.state_dict(), os.path.join(save_path_, 'model.pt'))
Ejemplo n.º 5
0
def _format_list_of_dictionaries(list_to_format):
    """Format the given list of dictionaries, to a human readable
    table. If keys of the dictionaries were different, it will
    return a Tree instead of table.
    """
    if len(list_to_format) == 0:
        return 'Empty result'

    first_element_keys = list_to_format[0].keys()
    #Estimate how many columns should be draw on screen
    terminal_width, terminal_height = utils.terminal_size()
    number_of_keys_to_draw = len(first_element_keys)
    if (number_of_keys_to_draw * 11) > (terminal_width - 5):
        #Terminal width is too small for showing all of the columns.
        print "Some of the columns can't be draw on the screen because of the",\
              "small width of the terminal."
        user_answer = raw_input("Do you want to see a detailed view? [Y/n]")
        if not user_answer.upper() in ('N', 'NO'):
            return _format_list_of_dict_as_detailed_view(list_to_format)

        number_of_keys_to_draw = (terminal_width - 5) / 11

    #Finding the max len of data in each column.
    max_length = {}
    #First, column headers.
    dict_keys = list_to_format[0].keys()
    for column_index in range(number_of_keys_to_draw):
        max_length[column_index] = len(dict_keys[column_index])

    for dict in list_to_format:
        for column_index in range(number_of_keys_to_draw):
            dict_value = dict[dict_keys[column_index]]
            if not isinstance(dict_value, basestring):
                dict_value = str(dict_value)

            max_length[column_index] = max(max_length[column_index],
                                           len(dict_value))

    #Calculating column size's factors
    sum_of_lengths = 0
    for column_index in max_length:
        sum_of_lengths += max_length[column_index]
    size_factors = {}
    for column_index in max_length:
        size_factors[column_index] = float(max_length[column_index]) / \
                                     sum_of_lengths

    columns_width = {}
    for column_index in range(number_of_keys_to_draw):
        columns_width[column_index] = \
            int((terminal_width - 9) * size_factors[column_index])

    #Normalizing columns width
    for column_index in columns_width:
        if columns_width[column_index] < 8:
            #Set it to min size
            leakage = 11 - columns_width[column_index]
            columns_width[column_index] = 8
            #Normalize other columns
            for i in range(leakage):
                max_width_index = 0
                for index in columns_width:
                    if columns_width[index] > columns_width[max_width_index]:
                        max_width_index = index
                columns_width[max_width_index] -= 1

    #Creating table's border
    table_border = ANSICOLORS.FBLUE % '   +'
    for column_index in columns_width:
        table_border += ANSICOLORS.FBLUE % \
                        ('-' * columns_width[column_index] + '+')

    result = table_border + '\n'

    line = ANSICOLORS.FBLUE % '   | '

    #Table header
    column_index = 0
    for key_index in range(number_of_keys_to_draw):
        column_header = first_element_keys[key_index]
        if len(column_header) > columns_width[column_index] - 2:
            column_header = column_header[:columns_width[column_index]-5] \
                            + '...'

        line += '%-{0}s'.format(columns_width[column_index]-2) \
                % column_header
        line += ANSICOLORS.FBLUE % ' | '
        column_index += 1

    result += line + '\n'
    result += table_border + '\n'

    #Table content
    line = ANSICOLORS.FBLUE % '   | '

    for item in list_to_format:
        #Make sure that our item is a string.
        column_index = 0
        for key_index in range(number_of_keys_to_draw):
            strItem = item[first_element_keys[key_index]]
            if not isinstance(strItem, basestring):
                strItem = str(strItem)

            if len(strItem) > columns_width[column_index] - 4:
                strItem = strItem[:columns_width[column_index] - 7] + '...'

            line += '%-{0}s'.format(columns_width[column_index] - 2) % strItem
            line += ANSICOLORS.FBLUE % ' | '

            column_index += 1

        result += line + '\n'
        line = ANSICOLORS.FBLUE % '   | '

    result += table_border + '\n'
    result += ANSICOLORS.BGREEN % '      count: %s\n' % len(list_to_format)
    if number_of_keys_to_draw != len(first_element_keys):
        #Warn the user that some columns didn't draw
        result += ANSICOLORS.FYELLOW % ("   Some of the columns are not "+\
                                      "appears because of "+\
                                      "the small width of the screen.\n")
    return result
Ejemplo n.º 6
0
def print_horizonal_line():
    """Prints an horizonal line on the terminal.
    """
    terminal_width, terminal_height = utils.terminal_size()
    print ANSICOLORS.FGREY % ('-' * (terminal_width - 3))
Ejemplo n.º 7
0
def _format_dictionary(dict_to_format):
    """Gets a dictionary, and returns a formatted table."""
    if len(dict_to_format) == 0:
        return 'Empty result'

    for item in dict_to_format.values():
        if isinstance(item, (list, dict, tuple)):
            #Some of the values of the dictionary are list or dict,
            #So we can't show this as a simple table.
            return _format_object_as_tree(dict_to_format)

    result = ''

    terminal_width, terminal_height = utils.terminal_size()
    columns_width = (terminal_width - 9) / 2
    keys_column_width = columns_width
    values_column_width = columns_width

    if columns_width > 26:  #Max width for keys column
        keys_column_width = 26
        values_column_width = terminal_width - 9 - keys_column_width

    table_border = ANSICOLORS.FBLUE % ('   +' + \
                                      '='*(keys_column_width) + '+' + \
                                      '='*(values_column_width) + '+' + '\n')
    if not keys_column_width % 2 == 0:
        #Ensure that the widths are even.
        keys_column_width = keys_column_width - 1
        values_column_width = values_column_width + 1

    table_header = table_border
    table_header += ANSICOLORS.FBLUE % ('   |' + (' '*(keys_column_width/2-2)) + \
                                        'KEYS' + \
                                        (' '*(keys_column_width/2-2)) +\
                                        '|' +  (' '*(values_column_width/2-3)) + \
                                        'VALUES' + \
                                        (' '*(values_column_width/2-3)) +\
                                        '|' + '\n' )
    table_header += table_border
    result += table_header

    line = ANSICOLORS.FBLUE % '   | '
    for key in dict_to_format:
        value = dict_to_format[key]
        #Ensure that our keys and values are strings.
        key = str(key)
        if not isinstance(value, basestring):
            value = str(value)

        #Printing in KEYS column.
        #-2 is for those extra spaces between and after text in the column.
        if len(key) > keys_column_width - 2:
            key = key[:keys_column_width - 5] + '...'
        line += ('%-{0}s'.format(keys_column_width-2) % key) + \
                ANSICOLORS.FBLUE % ' | '

        #Printing in VALUES column.
        if len(value) > values_column_width - 2:
            value = value[:values_column_width - 5] + '...'
        line += ('%-{0}s'.format(values_column_width-2) % value) + \
                ANSICOLORS.FBLUE % ' |'

        result += line + '\n'
        line = ANSICOLORS.FBLUE % '   | '

    result += table_border
    return result
    def main(self, loop, event, canvas):
        self.rows, self.columns = utils.terminal_size()
        self.song, self.artist, self.album, self.art_url = self.spotify.metadata(
        )

        self.update_directories()

        self.update_lyrics()

        album_cover = canvas.create_placement(
            'album_cover',
            x=self.columns // 2,
            y=4,
            scaler=ueberzug.ScalerOption.COVER.value)
        album_cover.path = self.image_file
        if self.album_hidden:
            album_cover.visibility = ueberzug.Visibility.INVISIBLE
        else:
            album_cover.visibility = ueberzug.Visibility.VISIBLE

        utils.hide_cursor()

        self.print_metadata()

        start_row = 5

        with utils.KeyPoller() as key_poller:
            while event.is_set():
                song, artist, album, art_url = self.spotify.metadata()
                if self.song != song or self.artist != artist:
                    self.song = song
                    self.artist = artist
                    self.album = album
                    self.art_url = art_url
                    self.update_directories()
                    self.update_lyrics()
                    album_cover.path = self.image_file
                    self.print_metadata()

                rows, columns = utils.terminal_size()
                if self.rows != rows or self.columns != columns:
                    difference = rows - self.rows
                    self.rows, self.columns = rows, columns
                    if difference > 0:
                        self.current_line -= difference
                        self.current_line = max(0, self.current_line)
                        self.current_line = min(
                            self.current_line,
                            self.total_lines - self.n_entries)
                    album_cover.x = self.columns // 2
                    self.print_metadata()

                if self.changed:
                    lines = self.lyrics.split('\n')
                    wrapped_lines = []
                    for line in lines:
                        wrapped_lines.extend(
                            textwrap.fill(line,
                                          (columns if self.album_hidden else
                                           columns // 2 - 2)).split('\n'))
                    self.total_lines = len(wrapped_lines)

                    utils.move_cursor(0, start_row)
                    self.n_entries = min(rows + self.current_line - start_row,
                                         self.total_lines) - self.current_line
                    for i in range(self.current_line,
                                   self.current_line + self.n_entries):
                        utils.delete_line()
                        print(utils.boldify(wrapped_lines[i]))
                    utils.move_cursor(0, self.n_entries + start_row)
                    utils.delete_line()
                    self.changed = False

                key = key_poller.poll(timeout=0.1)
                if key is not None:
                    if key == 'q':
                        os.system('clear')
                        loop.quit()
                        event.clear()
                        break
                    elif key == 'j' or ord(key) == 5:
                        self.current_line += 1
                    elif key == 'k' or ord(key) == 25:
                        self.current_line += -1
                    elif key == 'e':
                        try:
                            EDITOR = os.environ.get('EDITOR')
                            album_cover.visibility = ueberzug.Visibility.INVISIBLE
                            call([EDITOR, self.lyrics_file])
                            self.update_lyrics()
                            self.print_metadata()
                            utils.hide_cursor()
                            if self.album_hidden:
                                album_cover.visibility = ueberzug.Visibility.INVISIBLE
                            else:
                                album_cover.visibility = ueberzug.Visibility.VISIBLE
                        except TypeError:
                            os.system('clear')
                            print('$EDITOR is not set')
                            time.sleep(1)
                    elif key == 'r':
                        self.print_metadata()
                    elif key == 'd':
                        os.remove(self.lyrics_file)
                        self.update_lyrics()
                    elif key == 'n':
                        self.spotify.next()
                    elif key == 'p':
                        self.spotify.prev()
                    elif key == 't':
                        self.spotify.toggle()
                    elif key == 'i':
                        self.album_hidden = not self.album_hidden
                        self.changed = True
                        if self.album_hidden:
                            album_cover.visibility = ueberzug.Visibility.INVISIBLE
                        else:
                            album_cover.visibility = ueberzug.Visibility.VISIBLE
                    elif key == 'h':
                        os.system('clear')
                        album_cover.visibility = ueberzug.Visibility.INVISIBLE
                        utils.move_cursor(0, 0)
                        utils.print_help()
                        time.sleep(5)
                        self.print_metadata()
                        if self.album_hidden:
                            album_cover.visibility = ueberzug.Visibility.INVISIBLE
                        else:
                            album_cover.visibility = ueberzug.Visibility.VISIBLE
                        key_poller.flush()
                    elif key == 'g':
                        modified_key = key_poller.poll(timeout=1.0)
                        if modified_key == 'g':
                            self.current_line = 0
                    elif key == 'G':
                        self.current_line = self.total_lines - self.n_entries