<jupyter_start><jupyter_text>Montage Generator<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from codex import config as codex_config
from codex import io as codex_io
from codex.ops import tile_crop, tile_generator
import os.path as osp

exp_dir = 'F:\\20180424-d16-tcells-DAPI_CD3_CD8'
config_dir = osp.join(exp_dir, 'config')
output_dir = osp.join(exp_dir, 'output')
res_image = osp.join(exp_dir, 'results', 'montage.tif')

# exp_dir = 'F:\\2018-01-09-Run0-Clean'
# config_dir = exp_dir
# output_dir = 'F:\\2018-01-09-Run0-Clean-out-none\\1-Processor'
# res_image = 'F:\\ComparisonResults\\hammerlab_lung_sample\\montage.tif'
config = codex_config.load(config_dir)
config.channel_names
config.n_tiles_per_region, config.tile_width, config.tile_height
def load_montage(icyc=0, iz=0, ich=0, region=0, bw=1):
    tiles = []
    tile_indexes = list(range(config.n_tiles_per_region))
    tw, th = config.tile_width, config.tile_height
    rw, rh = config.region_width, config.region_height
    img = np.zeros((rh * th, rw * tw))
    for tile_index in tile_indexes:
        tx, ty = config.get_tile_coordinates(tile_index)
        path = codex_io.get_processor_img_path(region, tx, ty)
        
        tile = codex_io.read_tile(osp.join(output_dir, path), config)
        tile = tile[icyc, iz, ich, :, :]
        
        # Highlight borders
        if bw > 0:
            tile[0:bw, :] = 0
            tile[-bw:, :] = 0
            tile[:, 0:bw] = 0
            tile[:, -bw:] = 0
        
        # Add to montage
        img[(ty * th):((ty + 1) * th), (tx * tw):((tx + 1) * tw)] = tile
    return img
channel = 0
img = load_montage(ich=channel, bw=20)
img.shape
codex_io.save_image(res_image, img.astype(np.float32))
res_image
# fig, ax = plt.subplots()
# fig.set_size_inches(12, 12)
# ax.imshow(img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13665594855305466, 'fraction_numerical': 0.028938906752411574, 'mean_word_length': 3.4346793349168645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '426168', 'license': 'Apache-2.0', 'n_tokens_mistral': 751, 'n_tokens_neox': 718, 'n_words': 143, 'path': 'python/notebooks/unit_test_dev/montage_generator.ipynb', 'repo_name': 'eric-czech/codex', 'size': '1866'}