view measure_image_quality.xml @ 0:26945c55ddff draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 6d73056a625002d0275b5a9a90a9fae329ce47f1"
author bgruening
date Thu, 26 Mar 2020 16:47:42 -0400
parents
children 4dcd464c22ff
line wrap: on
line source

<tool id="cp_measure_image_quality" name="MeasureImageQuality" version="@CP_VERSION@">
    <description>measures features that indicate image quality</description>
  
    <macros>
        <import>macros.xml</import>
        <xml name="macro_calc_threshold">
            <conditional name="con_use_all_methods">
                <param name="use_all_methods" type="select" display="radio" label="Use all thresholding methods?">
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </param>
                <when value="Yes" />
                <when value="No">
                    <repeat name="rpt_threshold_method" title="Add another threshold method">
                        <conditional name="con_threshold_methods">
                            <param name="select_threshold_method" type="select" label="Select a thresholding method">
                                <option value="Otsu">Otsu</option>
                                <option value="MoG">MoG</option>
                                <option value="Background">Background</option>
                                <option value="RobustBackground">RobustBackground</option>
                                <option value="RidlerCalvard">RidlerCalvard</option>
                                <option value="Kapur">Kapur</option>
                                <option value="MCT">MCT</option>
                            </param>
                            <when value="Otsu">
                                <expand macro="otsu_method" />
                            </when>
                            <when value="MoG">
                                <expand macro="mog_method" />
                            </when>
                            <when value="Background" />
                            <when value="RobustBackground" />
                            <when value="RidlerCalvard" />
                            <when value="Kapur" />
                            <when value="MCT" />
                        </conditional>
                    </repeat>
                </when>
            </conditional>
        </xml>

        <xml name="otsu_method">
            <param name="otsu_weighted_entropy" label="Minimize the weighted variance or the entropy?" type="select">
                <option value="Weighted variance">Weighted variance</option>
                <option value="Entropy">Entropy</option>
            </param>
            <conditional name="con_threshold_classes">
                <param name="threshold_classes" label="Two-class or three-class thresholding?" type="select">
                    <option value="Two classes">Two classes</option>
                    <option value="Three classes">Three classes</option>
                </param>
                <when value="Three classes">
                    <param name="pixel_foreground_or_background" label="Assign pixels in the middle intensity class to the foreground or the background?" type="select">
                        <option value="Foreground">Foreground</option>
                        <option value="Background">Background</option>
                    </param>
                </when>
                <when value="Two classes" />
            </conditional>
        </xml>

        <xml name="mog_method">
            <param name="mog_fraction_cover" label="Typical fraction of the image covered by objects" type="float" value="0.1" min="0.0" max="1.0"/>
        </xml>
    </macros>
    
    <expand macro="py_requirements"/>
    <expand macro="cmd_modules" />

    <configfiles>
        <inputs name="inputs" />

        <configfile name="script_file">
import json
import sys
import os

FOURSPACES=@SPACES@

input_json_path = sys.argv[1]
input_pipeline= sys.argv[2]

params = json.load(open(input_json_path, "r"))

def writemiq():
    _str = "\nMeasureImageQuality:[module_num:%d|svn_version:\\'Unknown\\'|variable_revision_number:5|show_window:False|notes:\\x5B\\x5D|batch_state:array(\\x5B\\x5D, dtype=uint8)|enabled:True|wants_pause:False]\n" % new_count

    for_images = params['calc_for_images']
    selected_images = ""

    if for_images == "All loaded images":
        _str += FOURSPACES + "Calculate metrics for which images?:%s\n" % for_images
    else:
        _str += FOURSPACES + "Calculate metrics for which images?:Select...\n"
        selected_images = params['calc_for_images']

    image_count = 1
    _str += FOURSPACES + "Image count:%d\n" % image_count

    if 'rpt_blur_measurements' in params['con_calc_blur_metrics']:
        scale_count = len(params['con_calc_blur_metrics']['rpt_blur_measurements'])
    else:
        scale_count = 1

    _str += FOURSPACES + "Scale count:%d\n" % scale_count

    calc_threshold = params['con_calc_threshold']['calc_threshold']
    if calc_threshold == "No":
        threshold_count = 1
        all_method = "No"
    else:
        all_method = params['con_calc_threshold']['con_use_all_methods']['use_all_methods']
        if all_method == "No":
            threshold_count = 1
        else:
            threshold_count = len(params['con_calc_threshold']['con_use_all_methods']['rpt_threshold_method'])

    _str += FOURSPACES + "Threshold count:%d\n" % threshold_count

    _str += FOURSPACES + "Select the images to measure:%s\n" % selected_images

    include_rescaling = params['include_rescaling_value']
    _str += FOURSPACES + "Include the image rescaling value?:%s\n" % include_rescaling

    calc_blur_metrics = params['con_calc_blur_metrics']['calc_blur_metrics']
    _str += FOURSPACES + "Calculate blur metrics?:%s\n" % calc_blur_metrics

    if calc_blur_metrics == "Yes" and 'rpt_blur_measurements' in params['con_calc_blur_metrics']:
        for bm in params['con_calc_blur_metrics']['rpt_blur_measurements']:
            _str += FOURSPACES + "Spatial scale for blur measurements:%d\n" % bm['blur_measurements']

    calc_saturation = params['calc_saturation_metrics']
    _str += FOURSPACES + "Calculate saturation metrics?:%s\n" % calc_saturation

    calc_intensity = params['calc_intensity_metrics']
    _str += FOURSPACES + "Calculate intensity metrics?:%s\n" % calc_intensity

    calc_threshold = params['con_calc_threshold']['calc_threshold']
    _str += FOURSPACES + "Calculate thresholds?:%s\n" % calc_threshold
    _str += FOURSPACES + "Use all thresholding methods?:%s\n" % all_method

    if all_method == "No":
        for method in params['con_calc_threshold']['con_use_all_methods']['rpt_threshold_method']:
            _str += FOURSPACES + "Select a thresholding method:%s\n" % method['con_threshold_methods']['select_threshold_method']

            fraction_cover = method['con_threshold_methods']['mog_fraction_cover'] if 'mog_fraction_cover' in method[
                'con_threshold_methods'] else 0.1

            tt_classes = method['con_threshold_methods']['con_threshold_classes'][
                'threshold_classes'] if 'con_threshold_classes' in method['con_threshold_methods'] else 'Two classes'

            minimize = method['con_threshold_methods']['otsu_weighted_entropy'] if 'otsu_weighted_entropy' in method[
                'con_threshold_methods'] else 'Entropy'

            if tt_classes == "Three classes":
                pixel_intensity = method['con_threshold_methods']['con_threshold_classes'][
                    'pixel_foreground_or_background']
            else:
                pixel_intensity = "Foreground"

            _str += FOURSPACES + "Typical fraction of the image covered by objects:%.1f\n" % fraction_cover

            _str += FOURSPACES + "Two-class or three-class thresholding?:%s\n" % tt_classes

            _str += FOURSPACES + "Minimize the weighted variance or the entropy:%s\n" % minimize

            _str += FOURSPACES + "Assign pixels in the middle intensity class to the foreground or the background?:%s\n" % pixel_intensity

    return _str

with open(input_pipeline) as fin:
    lines = fin.readlines()

    k, v = lines[4].strip().split(':')

    module_count = int(v)
    new_count = module_count + 1
    lines[4] = k + ":%d\n" % new_count

    with open("output", "w") as f:
        f.writelines(lines)
        f.write(writemiq())

f.close()
        </configfile>    
    </configfiles>
  
    <inputs>
        <expand macro="input_pipeline_macro" />
        <param name="calc_for_images" type="text" label="Calculate metrics for which images?" value="All loaded images">
            <expand macro="text_validator" />
        </param>
        <param name="include_rescaling_value" type="select" display="radio" label="Include the image rescaling value?">
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </param>

        <conditional name="con_calc_blur_metrics">
            <param name="calc_blur_metrics" type="select" display="radio" label="Calculate blur metrics?">
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            </param>
            <when value="Yes">
                <repeat name="rpt_blur_measurements" title="Add another scale">
                    <param name="blur_measurements" label="Spatial scale for blur measurements" type="integer" value="20"/>
                </repeat>
            </when>
            <when value="No" />
        </conditional>
        
        <param name="calc_intensity_metrics" type="select" display="radio" label="Calculate intensity metrics?">
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </param>
        <param name="calc_saturation_metrics" type="select" display="radio" label="Calculate saturation metrics">
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </param>

        <conditional name="con_calc_threshold">
            <param name="calc_threshold" type="select" display="radio" label="Calculate thresholds?">
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            </param>
            <when value="Yes">
                <expand macro="macro_calc_threshold" />
            </when>
            <when value="No" />
        </conditional>

    </inputs>

    <outputs>
        <expand macro="output_pipeline_macro" />
    </outputs>

    <tests>
        <test>
            <expand macro="test_input_pipeline_param" />
            
            <param name="calc_for_images" value="All loaded images" />
            <param name="include_rescaling_value" value="Yes" />

            <conditional name="con_calc_blur_metrics">
                <param name="calc_blur_metrics" value="Yes" />
                <repeat name="rpt_blur_measurements">
                    <param name="blur_measurements" value="20" />
                </repeat>
            </conditional>

            <param name="calc_intensity_metrics" value="Yes" />
            <param name="calc_saturation_metrics" value="Yes" />
                <conditional name="con_calc_threshold">
                    <param name="calc_threshold" value="Yes" />
                </conditional>

                <conditional name="con_use_all_methods">
                    <param name="use_all_methods" value="No" />
                    <repeat name="rpt_threshold_method">
                        <conditional name="con_threshold_methods">
                            <param name="select_threshold_method" value="Otsu" />
                            <param name="otsu_weighted_entropy" value="Weighted variance" />
                            <conditional name="con_threshold_classes">
                                <param name="threshold_classes" value="Two classes" />
                            </conditional>
                        </conditional>
                    </repeat>
                </conditional>
            
            <expand macro="test_out_file" file="measure_image_quality.txt" />
        </test>
    </tests>
    
    <expand macro="help" module="MeasureImageQuality" />
    <expand macro="citations" />  
</tool>