Example #1
0
def _vector_str(self, indent, summarize, formatter1):
    # length includes spaces and comma between elements
    element_length = formatter1.width() + 2
    elements_per_line = max(
        1, int(math.floor((PRINT_OPTS.linewidth - indent) / (element_length)))
    )

    def _val_formatter(val, formatter1=formatter1):
        return formatter1.format(val)

    if summarize and self.size(0) > 2 * PRINT_OPTS.edgeitems:
        left_values = _try_convert_to_local_tensor(
            slice_wrapper(self, [0, PRINT_OPTS.edgeitems, 1])
        ).tolist()
        right_values = _try_convert_to_local_tensor(
            slice_wrapper(self, [self.size(0) - PRINT_OPTS.edgeitems, self.size(0), 1])
        ).tolist()
        data = (
            [_val_formatter(val) for val in left_values]
            + [" ..."]
            + [_val_formatter(val) for val in right_values]
        )
    else:
        values = _try_convert_to_local_tensor(self).tolist()
        data = [_val_formatter(val) for val in values]

    data_lines = [
        data[i : i + elements_per_line] for i in range(0, len(data), elements_per_line)
    ]
    lines = [", ".join(line) for line in data_lines]
    return "[" + ("," + "\n" + " " * (indent + 1)).join(lines) + "]"
Example #2
0
def get_summarized_data(self):
    dim = self.dim()
    if dim == 0:
        return self
    if dim == 1:
        if self.size(0) > 2 * PRINT_OPTS.edgeitems:
            return flow.cat(
                (
                    slice_wrapper(self, [0, PRINT_OPTS.edgeitems, 1]),
                    slice_wrapper(
                        self, [self.size(0) - PRINT_OPTS.edgeitems, self.size(0), 1]
                    ),
                )
            )
        else:
            return self
    if self.size(0) > 2 * PRINT_OPTS.edgeitems:
        start = [
            slice_wrapper(self, [i, i + 1, 1]) for i in range(0, PRINT_OPTS.edgeitems)
        ]
        end = [
            slice_wrapper(self, [i, i + 1, 1])
            for i in range(self.shape[0] - PRINT_OPTS.edgeitems, self.shape[0])
        ]
        return flow.stack([get_summarized_data(x) for x in (start + end)])
    else:
        return flow.stack(
            [
                get_summarized_data(slice_wrapper(self, [i, i + 1, 1]))
                for i in range(len(self))
            ]
        )
Example #3
0
def _tensor_str_with_formatter(self, indent, summarize, formatter1):
    dim = self.dim()

    if dim == 0:
        return _scalar_str(self, formatter1)

    if dim == 1:
        return _vector_str(self, indent, summarize, formatter1)

    if summarize and self.size(0) > 2 * PRINT_OPTS.edgeitems:
        slices = ([
            _tensor_str_with_formatter(
                slice_wrapper(self, [i, i + 1, 1]),
                indent + 1,
                summarize,
                formatter1,
            ) for i in range(0, PRINT_OPTS.edgeitems)
        ] + ["..."] + [
            _tensor_str_with_formatter(
                slice_wrapper(self, [i, i + 1, 1]),
                indent + 1,
                summarize,
                formatter1,
            )
            for i in range(self.shape[0] - PRINT_OPTS.edgeitems, self.shape[0])
        ])
    else:
        slices = [
            _tensor_str_with_formatter(slice_wrapper(self, [i, i + 1, 1]),
                                       indent + 1, summarize, formatter1)
            for i in range(0, self.size(0))
        ]

    tensor_str = ("," + "\n" * (dim - 1) + " " * (indent + 1)).join(slices)
    return "[" + tensor_str + "]"