# Path: tensorboardX/summary.py
# _INVALID_TAG_CHARACTERS = _re.compile(r'[^-/\w\.]')
#     PLUGIN_NAME = 'hparams'
#     PLUGIN_DATA_VERSION = 0
#     EXPERIMENT_TAG = '_hparams_/experiment'
#     SESSION_START_INFO_TAG = '_hparams_/session_start_info'
#     SESSION_END_INFO_TAG = '_hparams_/session_end_info'
#     _MINIMUM_COUNT = 1e-7
# def _clean_tag(name):
# def _draw_single_box(image, xmin, ymin, xmax, ymax, display_str, color='black', color_text='black', thickness=2):
# def hparams(hparam_dict=None, metric_dict=None):
# def scalar(name, scalar, display_name="", summary_description=""):
# def histogram_raw(name, min, max, num, sum, sum_squares, bucket_limits, bucket_counts):
# def histogram(name, values, bins, max_bins=None):
# def make_histogram(values, bins, max_bins=None):
# def image(tag, tensor, rescale=1, dataformats='CHW'):
# def image_boxes(tag, tensor_image, tensor_boxes, rescale=1, dataformats='CHW', labels=None):
# def draw_boxes(disp_image, boxes, labels=None):
# def make_image(tensor, rescale=1, rois=None, labels=None):
# def video(tag, tensor, fps=4, dataformats="NTCHW"):
# def make_video(tensor, fps):
# def audio(tag, tensor, sample_rate=44100):
# def custom_scalars(layout):
# def text(tag, text):
# def pr_curve_raw(tag, tp, fp, tn, fn, precision, recall, num_thresholds=127, weights=None):
# def pr_curve(tag, labels, predictions, num_thresholds=127, weights=None):
# def compute_curve(labels, predictions, num_thresholds=None, weights=None):
# def _get_tensor_summary(tag, tensor, content_type, json_config):
# def mesh(tag, vertices, colors, faces, config_dict=None):
# 
# Path: tensorboardX/utils.py
# def make_grid(I, ncols=8):
#     # I: N1HW or N3HW
#     import numpy as np
#     assert isinstance(
#         I, np.ndarray), 'plugin error, should pass numpy array here'
#     if I.shape[1] == 1:
#         I = np.concatenate([I, I, I], 1)
#     assert I.ndim == 4 and I.shape[1] == 3 or I.shape[1] == 4
#     nimg = I.shape[0]
#     H = I.shape[2]
#     W = I.shape[3]
#     ncols = min(nimg, ncols)
#     nrows = int(np.ceil(float(nimg) / ncols))
#     canvas = np.zeros((I.shape[1], H * nrows, W * ncols), dtype=I.dtype)
#     i = 0
#     for y in range(nrows):
#         for x in range(ncols):
#             if i >= nimg:
#                 break
#             canvas[:, y * H:(y + 1) * H, x * W:(x + 1) * W] = I[i]
#             i = i + 1
#     return canvas
# 
# def _prepare_video(V):
#     import numpy as np
#     b, t, c, h, w = V.shape
# 
#     if V.dtype == np.uint8:
#         V = np.float32(V) / 255.
# 
#     def is_power2(num):
#         return num != 0 and ((num & (num - 1)) == 0)
# 
#     # pad to nearest power of 2, all at once
#     if not is_power2(V.shape[0]):
#         len_addition = int(2**V.shape[0].bit_length() - V.shape[0])
#         V = np.concatenate(
#             (V, np.zeros(shape=(len_addition, t, c, h, w))), axis=0)
# 
#     n_rows = 2**((b.bit_length() - 1) // 2)
#     n_cols = V.shape[0] // n_rows
# 
#     V = np.reshape(V, newshape=(n_rows, n_cols, t, c, h, w))
#     V = np.transpose(V, axes=(2, 0, 4, 1, 5, 3))
#     V = np.reshape(V, newshape=(t, n_rows * h, n_cols * w, c))
# 
#     return V
# 
# def convert_to_HWC(tensor, input_format):  # tensor: numpy array
#     import numpy as np
#     assert(len(set(input_format)) == len(input_format)), "You can not use the same dimension shordhand twice. \
#         input_format: {}".format(input_format)
#     assert(len(tensor.shape) == len(input_format)), "size of input tensor and input format are different. \
#         tensor shape: {}, input_format: {}".format(tensor.shape, input_format)
#     input_format = input_format.upper()
# 
#     if len(input_format) == 4:
#         index = [input_format.find(c) for c in 'NCHW']
#         tensor_NCHW = tensor.transpose(index)
#         tensor_CHW = make_grid(tensor_NCHW)
#         return tensor_CHW.transpose(1, 2, 0)
# 
#     if len(input_format) == 3:
#         index = [input_format.find(c) for c in 'HWC']
#         tensor_HWC = tensor.transpose(index)
#         if tensor_HWC.shape[2] == 1:
#             tensor_HWC = np.concatenate([tensor_HWC, tensor_HWC, tensor_HWC], 2)
#         return tensor_HWC
# 
#     if len(input_format) == 2:
#         index = [input_format.find(c) for c in 'HW']
#         tensor = tensor.transpose(index)
#         tensor = np.stack([tensor, tensor, tensor], 2)
#         return tensor


from tensorboardX import summary
from tensorboardX.utils import make_grid, _prepare_video, convert_to_HWC
import numpy as np
import pytest
import unittest


class UtilsTest(unittest.TestCase):
    def test_to_HWC(self):
        np.random.seed(1)
        test_image = np.random.randint(0, 256, size=(3, 32, 32), dtype=np.uint8)

# Path: tensorboardX/summary.py
# _INVALID_TAG_CHARACTERS = _re.compile(r'[^-/\w\.]')
#     PLUGIN_NAME = 'hparams'
#     PLUGIN_DATA_VERSION = 0
#     EXPERIMENT_TAG = '_hparams_/experiment'
#     SESSION_START_INFO_TAG = '_hparams_/session_start_info'
#     SESSION_END_INFO_TAG = '_hparams_/session_end_info'
#     _MINIMUM_COUNT = 1e-7
# def _clean_tag(name):
# def _draw_single_box(image, xmin, ymin, xmax, ymax, display_str, color='black', color_text='black', thickness=2):
# def hparams(hparam_dict=None, metric_dict=None):
# def scalar(name, scalar, display_name="", summary_description=""):
# def histogram_raw(name, min, max, num, sum, sum_squares, bucket_limits, bucket_counts):
# def histogram(name, values, bins, max_bins=None):
# def make_histogram(values, bins, max_bins=None):
# def image(tag, tensor, rescale=1, dataformats='CHW'):
# def image_boxes(tag, tensor_image, tensor_boxes, rescale=1, dataformats='CHW', labels=None):
# def draw_boxes(disp_image, boxes, labels=None):
# def make_image(tensor, rescale=1, rois=None, labels=None):
# def video(tag, tensor, fps=4, dataformats="NTCHW"):
# def make_video(tensor, fps):
# def audio(tag, tensor, sample_rate=44100):
# def custom_scalars(layout):
# def text(tag, text):
# def pr_curve_raw(tag, tp, fp, tn, fn, precision, recall, num_thresholds=127, weights=None):
# def pr_curve(tag, labels, predictions, num_thresholds=127, weights=None):
# def compute_curve(labels, predictions, num_thresholds=None, weights=None):
# def _get_tensor_summary(tag, tensor, content_type, json_config):
# def mesh(tag, vertices, colors, faces, config_dict=None):
# 
# Path: tensorboardX/utils.py
# def make_grid(I, ncols=8):
#     # I: N1HW or N3HW
#     import numpy as np
#     assert isinstance(
#         I, np.ndarray), 'plugin error, should pass numpy array here'
#     if I.shape[1] == 1:
#         I = np.concatenate([I, I, I], 1)
#     assert I.ndim == 4 and I.shape[1] == 3 or I.shape[1] == 4
#     nimg = I.shape[0]
#     H = I.shape[2]
#     W = I.shape[3]
#     ncols = min(nimg, ncols)
#     nrows = int(np.ceil(float(nimg) / ncols))
#     canvas = np.zeros((I.shape[1], H * nrows, W * ncols), dtype=I.dtype)
#     i = 0
#     for y in range(nrows):
#         for x in range(ncols):
#             if i >= nimg:
#                 break
#             canvas[:, y * H:(y + 1) * H, x * W:(x + 1) * W] = I[i]
#             i = i + 1
#     return canvas
# 
# def _prepare_video(V):
#     import numpy as np
#     b, t, c, h, w = V.shape
# 
#     if V.dtype == np.uint8:
#         V = np.float32(V) / 255.
# 
#     def is_power2(num):
#         return num != 0 and ((num & (num - 1)) == 0)
# 
#     # pad to nearest power of 2, all at once
#     if not is_power2(V.shape[0]):
#         len_addition = int(2**V.shape[0].bit_length() - V.shape[0])
#         V = np.concatenate(
#             (V, np.zeros(shape=(len_addition, t, c, h, w))), axis=0)
# 
#     n_rows = 2**((b.bit_length() - 1) // 2)
#     n_cols = V.shape[0] // n_rows
# 
#     V = np.reshape(V, newshape=(n_rows, n_cols, t, c, h, w))
#     V = np.transpose(V, axes=(2, 0, 4, 1, 5, 3))
#     V = np.reshape(V, newshape=(t, n_rows * h, n_cols * w, c))
# 
#     return V
# 
# def convert_to_HWC(tensor, input_format):  # tensor: numpy array
#     import numpy as np
#     assert(len(set(input_format)) == len(input_format)), "You can not use the same dimension shordhand twice. \
#         input_format: {}".format(input_format)
#     assert(len(tensor.shape) == len(input_format)), "size of input tensor and input format are different. \
#         tensor shape: {}, input_format: {}".format(tensor.shape, input_format)
#     input_format = input_format.upper()
# 
#     if len(input_format) == 4:
#         index = [input_format.find(c) for c in 'NCHW']
#         tensor_NCHW = tensor.transpose(index)
#         tensor_CHW = make_grid(tensor_NCHW)
#         return tensor_CHW.transpose(1, 2, 0)
# 
#     if len(input_format) == 3:
#         index = [input_format.find(c) for c in 'HWC']
#         tensor_HWC = tensor.transpose(index)
#         if tensor_HWC.shape[2] == 1:
#             tensor_HWC = np.concatenate([tensor_HWC, tensor_HWC, tensor_HWC], 2)
#         return tensor_HWC
# 
#     if len(input_format) == 2:
#         index = [input_format.find(c) for c in 'HW']
#         tensor = tensor.transpose(index)
#         tensor = np.stack([tensor, tensor, tensor], 2)
#         return tensor


# Path: tests/test_utils.py
from tensorboardX import summary
from tensorboardX.utils import make_grid, _prepare_video, convert_to_HWC
import numpy as np
import pytest
import unittest



class UtilsTest(unittest.TestCase):
    def test_to_HWC(self):
        np.random.seed(1)
        test_image = np.random.randint(0, 256, size=(3, 32, 32), dtype=np.uint8)

        converted = convert_to_HWC(test_image, 'chw')