def make_web_thumbnails(animal):
    """
    This was originally getting the thumbnails from the preps/thumbnail dir but they aren't usuable.
    The ones in the preps/CH1/thumbnail_aligned are much better
    But we need to test if there ane aligned files, if not use the cleaned ones.
    Thumbnails are always created from CH1
    Args:
        animal: the prep id of the animal
        njobs: number of jobs for parallel computing

    Returns:
        nothing
    """
    fileLocationManager = FileLocationManager(animal)
    sqlController = SqlController(animal)
    INPUT = '/home/eodonnell/DK39/cshl/jpg'

    OUTPUT = os.path.join(fileLocationManager.root, animal, 'cshl/jpg')
    tifs = sqlController.get_sections(animal, 1)

    for i, tif in enumerate(tqdm(tifs)):
        input_path = os.path.join(INPUT,
                                  os.path.splitext(tif.file_name)[0] + '.jpg')

        output_path = os.path.join(OUTPUT, str(i).zfill(3) + '.jpg')

        if not os.path.exists(input_path):
            continue

        if os.path.exists(output_path):
            continue

        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        cmd = "convert {} {}".format(input_path, output_path)
        subprocess.run(cmd, shell=True)
def make_pages(animal, fetch, layer):
    templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
    templateEnv = jinja2.Environment(loader=templateLoader)
    if 'sql' in fetch:
        sqlController = SqlController(animal)
        valid_sections = sqlController.get_sections(animal, 1)
        container = "container.html"
    else:
        fileLocationManager = FileLocationManager(animal)
        valid_sections = sorted(os.listdir(fileLocationManager.thumbnail_web))
        container = "list_dir.html"

    if layer is not None:
        fileLocationManager = FileLocationManager(animal)
        INPUT = os.path.join(fileLocationManager.thumbnail_web, 'points',
                             layer)
        valid_sections = sorted(os.listdir(INPUT))
        container = "list_points.html"
        template = templateEnv.get_template(container)
        lfiles = len(valid_sections)
        outputText = template.render(animal=animal,
                                     valid_sections=valid_sections,
                                     lfiles=lfiles,
                                     layer=layer)
    else:
        template = templateEnv.get_template(container)
        lfiles = len(valid_sections)
        outputText = template.render(
            animal=animal, valid_sections=valid_sections, lfiles=lfiles
        )  # this is where to put args to the template renderer

    # to save the results
    filename = '{}.thumbnails.html'.format(animal)
    with open(filename, "w") as fh:
        fh.write(outputText)
def make_web_thumbnails(animal):
    """
    This was originally getting the thumbnails from the preps/thumbnail dir but they aren't usuable.
    The ones in the preps/CH1/thumbnail_aligned are much better
    But we need to test if there ane aligned files, if not use the cleaned ones.
    Thumbnails are always created from CH1
    Args:
        animal: the prep id of the animal
        njobs: number of jobs for parallel computing

    Returns:
        nothing
    """
    channel_dir = 'CH1'
    fileLocationManager = FileLocationManager(animal)
    sqlController = SqlController(animal)
    INPUT = os.path.join(fileLocationManager.prep, channel_dir,
                         'thumbnail_aligned')
    len_files = len(os.listdir(INPUT))
    if len_files < 10:
        INPUT = os.path.join(fileLocationManager.prep, channel_dir,
                             'thumbnail_cleaned')
    ##### Check if files in dir are valid
    error = test_dir(animal, INPUT, downsample=True, same_size=True)
    if len(error) > 0:
        print(error)
        sys.exit()

    OUTPUT = fileLocationManager.thumbnail_web
    os.makedirs(OUTPUT, exist_ok=True)
    tifs = sqlController.get_sections(animal, 1)

    for i, tif in enumerate(tqdm(tifs)):
        input_path = os.path.join(INPUT, str(i).zfill(3) + '.tif')
        output_path = os.path.join(OUTPUT,
                                   os.path.splitext(tif.file_name)[0] + '.png')

        if not os.path.exists(input_path):
            continue

        if os.path.exists(output_path):
            continue

        original = Image.open(input_path)
        original.save(output_path, format="png")
def fix_tifs(animal, channel):
    sqlController = SqlController(animal)
    fileLocationManager = FileLocationManager(animal)
    dir = fileLocationManager.tif
    db_files = sqlController.get_sections(animal, channel)

    source_files = []
    source_keys = []
    for tif in db_files:
        source_files.append(tif.file_name)
        source_keys.append(tif.id)
    files = os.listdir(dir)
    files = [file for file in files if 'C{}.tif'.format(channel) in file]
    missing_files = list(set(source_files) - set(files))

    for i, missing in enumerate(missing_files):
        #pass
        file_id = source_keys[source_files.index(missing)]
        section = sqlController.get_section(file_id)
        print(i, missing, file_id, section.id, section.file_name)
        make_tif(animal, section.tif_id, file_id, testing=False)
Exemple #5
0
def make_full_resolution(animal, channel):
    """
    Args:
        animal: the prep id of the animal
        channel: the channel of the stack to process
        compress: Use the default LZW compression, otherwise just copy the file with the correct name
    Returns:
        list of commands
    """

    fileLocationManager = FileLocationManager(animal)
    sqlController = SqlController(animal)

    if 'thion' in sqlController.histology.counterstain:
        sqlController.set_task(animal, CREATE_CHANNEL_2_FULL_RES)
        sqlController.set_task(animal, CREATE_CHANNEL_3_FULL_RES)

    INPUT = os.path.join(fileLocationManager.tif)
    ##### Check if files in dir are valid
    OUTPUT = os.path.join(fileLocationManager.prep, f'CH{channel}', 'full')
    os.makedirs(OUTPUT, exist_ok=True)

    sections = sqlController.get_sections(animal, channel)
    for section_number, section in enumerate(tqdm(sections)):
        input_path = os.path.join(INPUT, section.file_name)
        output_path = os.path.join(OUTPUT,
                                   str(section_number).zfill(3) + '.tif')

        if not os.path.exists(input_path):
            #print('Input tif does not exist', input_path)
            continue

        if os.path.exists(output_path):
            continue

        copyfile(input_path, output_path)
        width, height = get_image_size(input_path)
        sqlController.update_tif(section.id, width, height)
Exemple #6
0
def make_histogram(animal, channel):
    """
    This method creates an individual histogram for each tif file by channel.
    Args:
        animal: the prep id of the animal
        channel: the channel of the stack to process  {1,2,3}
    Returns:
        nothing
    """

    logger = get_logger(animal)
    fileLocationManager = FileLocationManager(animal)
    sqlController = SqlController(animal)
    INPUT = os.path.join(fileLocationManager.prep, f'CH{channel}', 'thumbnail')
    MASK_INPUT = fileLocationManager.thumbnail_masked
    tifs = sqlController.get_sections(animal, channel)
    error = test_dir(animal, INPUT, downsample=True, same_size=False)
    if len(tifs) == 0:
        error += " No sections in the database"
    if len(error) > 0:
        print(error)
        sys.exit()
    ch_dir = f'CH{channel}'
    OUTPUT = os.path.join(fileLocationManager.histogram, ch_dir)
    os.makedirs(OUTPUT, exist_ok=True)
    progress_id = sqlController.get_progress_id(True, channel, 'HISTOGRAM')
    sqlController.set_task(animal, progress_id)

    for i, tif in enumerate(tqdm(tifs)):
        filename = str(i).zfill(3) + '.tif'
        input_path = os.path.join(INPUT, filename)
        mask_path = os.path.join(MASK_INPUT, filename)
        output_path = os.path.join(OUTPUT,
                                   os.path.splitext(tif.file_name)[0] + '.png')
        if not os.path.exists(input_path):
            print('Input tif does not exist', input_path)
            continue

        if os.path.exists(output_path):
            continue

        try:
            img = io.imread(input_path)
        except:
            logger.warning(f'Could not open {input_path}')
            continue
        try:
            mask = io.imread(mask_path)
        except:
            logger.warning(f'Could not open {mask_path}')
            continue

        img = cv2.bitwise_and(img, img, mask=mask)

        if img.shape[0] * img.shape[1] > 1000000000:
            scale = 1 / float(2)
            img = img[::int(1. / scale), ::int(1. / scale)]

        try:
            flat = img.flatten()
        except:
            logger.warning(f'Could not flat {input_path}')
            continue

        fig = plt.figure()
        plt.rcParams['figure.figsize'] = [10, 6]
        plt.hist(flat, flat.max(), [0, 10000], color=COLORS[channel])
        plt.style.use('ggplot')
        plt.yscale('log')
        plt.grid(axis='y', alpha=0.75)
        plt.xlabel('Value')
        plt.ylabel('Frequency')
        plt.title(f'{tif.file_name} @16bit')
        plt.close()
        fig.savefig(output_path, bbox_inches='tight')