<commit_before>def _diff_integration_goldens_impl(ctx):
    # Extract the Java source files from the generated 3 srcjars from API bazel target,
    # and put them in the temporary folder `codegen_tmp`.
    # Compare the `codegen_tmp` with the goldens folder e.g `test/integration/goldens/redis`
    # and save the differences in output file `diff_output.txt`.

    diff_output = ctx.outputs.diff_output
    check_diff_script = ctx.outputs.check_diff_script
    gapic_library = ctx.attr.gapic_library
    resource_name_library = ctx.attr.resource_name_library
    test_library = ctx.attr.test_library
    srcs = ctx.files.srcs
    api_name = ctx.attr.name

    script = """
    mkdir codegen_tmp
    unzip -j {input} -d codegen_tmp
    unzip -j {input_resource_name} -d  codegen_tmp
    unzip -j {input_test} -d codegen_tmp
    cd codegen_tmp
    # Remove unneeded non-Java files, like MANIFEST
    rm -rf $(find . -type f ! -name "*.java")
    cd ..
    diff codegen_tmp test/integration/goldens/{api_name}/ > {diff_output}
    # Bash `diff` command will return exit code 1 when there are differences between the two
    # folders. So we explicitly `exit 0` after the diff command to avoid build failure.
    exit 0
    """.format(
        diff_output = diff_output.path,
        input = gapic_library[JavaInfo].source_jars[0].path,
        input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
        input_test = test_library[JavaInfo].source_jars[0].path,
        api_name = api_name
    )
    ctx.actions.run_shell(
        inputs = srcs + [
            gapic_library[JavaInfo].source_jars[0],
            resource_name_library[JavaInfo].source_jars[0],
            test_library[JavaInfo].source_jars[0],
        ],
        outputs = [diff_output],
        command = script,
    )

    # Check the generated diff_output file, if it is empty, that means there is no difference
    # between generated source code and goldens files, test should pass. If it is not empty, then
    # test will fail by exiting 1.

    check_diff_script_content = """
    # This will not print diff_output to the console unless `--test_output=all` option
    # is enabled, it only emits the comparison results to the test.log.
    # We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that
    # file is not existing at the moment. It is generated once test is finished.
    cat $PWD/test/integration/{api_name}_diff_output.txt
    if [ -s $PWD/test/integration/{api_name}_diff_output.txt ]
    then
        exit 1
    fi
    """.format(
        api_name = api_name,
    )

    ctx.actions.write(
        output = check_diff_script,
        content = check_diff_script_content,
    )
    runfiles = ctx.runfiles(files = [ctx.outputs.diff_output])
    return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)]

diff_integration_goldens_test = rule(
    attrs = {
        "gapic_library": attr.label(),
        "resource_name_library": attr.label(),
        "test_library": attr.label(),
        "srcs": attr.label_list(
            allow_files = True,
            mandatory = True,
        ),
    },
    outputs = {
        "diff_output": "%{name}_diff_output.txt",
        "check_diff_script": "%{name}_check_diff_script.sh",
    },
    implementation = _diff_integration_goldens_impl,
    test = True,
)

def integration_test(name, target, data):
    # Bazel target `java_gapic_library` will generate 3 source jars including the
    # the source Java code of the gapic_library, resource_name_library and test_library.
    diff_integration_goldens_test(
        name = name,
        gapic_library = target,
        resource_name_library = "%s_resource_name" % target,
        test_library = "%s_test" % target,
        srcs = data,
    )

def _overwrite_golden_impl(ctx):
    # Extract the Java source files from the generated 3 srcjars from API bazel target,
    # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.
    # Overwrite the goldens folder e.g `test/integration/goldens/redis` with the
    # code generation in `goldens_output_zip`.

    gapic_library = ctx.attr.gapic_library
    resource_name_library = ctx.attr.resource_name_library
    test_library = ctx.attr.test_library
    srcs = ctx.files.srcs
    # Convert the name of bazel rules e.g. `redis_update` to `redis`
    # because we will need to overwrite the goldens files in `redis` folder.
    api_name = "_".join(ctx.attr.name.split("_")[:-1])
    goldens_output_zip = ctx.outputs.goldens_output_zip

    script = """
    mkdir codegen_tmp
    unzip -j {input} -d codegen_tmp
    unzip -j {input_resource_name} -d  codegen_tmp
    unzip -j {input_test} -d codegen_tmp
    cd codegen_tmp
    # Remove unneeded non-Java files, like MANIFEST
    rm -rf $(find . -type f ! -name "*.java")
    zip -r ../{goldens_output_zip} .
    """.format(
        goldens_output_zip = goldens_output_zip.path,
        input = gapic_library[JavaInfo].source_jars[0].path,
        input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
        input_test = test_library[JavaInfo].source_jars[0].path,
    )

    ctx.actions.run_shell(
        inputs = srcs + [
            gapic_library[JavaInfo].source_jars[0],
            resource_name_library[JavaInfo].source_jars[0],
            test_library[JavaInfo].source_jars[0],
        ],
        outputs = [goldens_output_zip],
        command = script,
    )

    # Overwrite the goldens.
    golden_update_script_content = """
    cd ${{BUILD_WORKSPACE_DIRECTORY}}
    unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name}
    """.format(
        goldens_output_zip = goldens_output_zip.path,
        api_name = api_name,
    )
    ctx.actions.write(
        output = ctx.outputs.golden_update_script,
        content = golden_update_script_content,
        is_executable = True,
    )
    return [DefaultInfo(executable = ctx.outputs.golden_update_script)]

overwrite_golden = rule(
    attrs = {
        "gapic_library": attr.label(),
        "resource_name_library": attr.label(),
        "test_library": attr.label(),
        "srcs": attr.label_list(
            allow_files = True,
            mandatory = True,
        ),
    },
    outputs = {
        "goldens_output_zip": "%{name}.zip",
        "golden_update_script": "%{name}.sh",
    },
    executable = True,
    implementation = _overwrite_golden_impl,
)

def golden_update(name, target, data):
    overwrite_golden(
        name = name,
        gapic_library = target,
        resource_name_library = "%s_resource_name" % target,
        test_library = "%s_test" % target,
        srcs = data,
    )
<commit_msg>[ggj][engx] build: support file deletion in integration test update Bazel rules (#558)<commit_after>def _diff_integration_goldens_impl(ctx):
    # Extract the Java source files from the generated 3 srcjars from API bazel target,
    # and put them in the temporary folder `codegen_tmp`.
    # Compare the `codegen_tmp` with the goldens folder e.g `test/integration/goldens/redis`
    # and save the differences in output file `diff_output.txt`.

    diff_output = ctx.outputs.diff_output
    check_diff_script = ctx.outputs.check_diff_script
    gapic_library = ctx.attr.gapic_library
    resource_name_library = ctx.attr.resource_name_library
    test_library = ctx.attr.test_library
    srcs = ctx.files.srcs
    api_name = ctx.attr.name

    script = """
    mkdir codegen_tmp
    unzip -j {input} -d codegen_tmp
    unzip -j {input_resource_name} -d  codegen_tmp
    unzip -j {input_test} -d codegen_tmp
    cd codegen_tmp
    # Remove unneeded non-Java files, like MANIFEST
    rm -rf $(find . -type f ! -name "*.java")
    cd ..
    diff codegen_tmp test/integration/goldens/{api_name}/ > {diff_output}
    # Bash `diff` command will return exit code 1 when there are differences between the two
    # folders. So we explicitly `exit 0` after the diff command to avoid build failure.
    exit 0
    """.format(
        diff_output = diff_output.path,
        input = gapic_library[JavaInfo].source_jars[0].path,
        input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
        input_test = test_library[JavaInfo].source_jars[0].path,
        api_name = api_name,
    )
    ctx.actions.run_shell(
        inputs = srcs + [
            gapic_library[JavaInfo].source_jars[0],
            resource_name_library[JavaInfo].source_jars[0],
            test_library[JavaInfo].source_jars[0],
        ],
        outputs = [diff_output],
        command = script,
    )

    # Check the generated diff_output file, if it is empty, that means there is no difference
    # between generated source code and goldens files, test should pass. If it is not empty, then
    # test will fail by exiting 1.

    check_diff_script_content = """
    # This will not print diff_output to the console unless `--test_output=all` option
    # is enabled, it only emits the comparison results to the test.log.
    # We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that
    # file is not existing at the moment. It is generated once test is finished.
    cat $PWD/test/integration/{api_name}_diff_output.txt
    if [ -s $PWD/test/integration/{api_name}_diff_output.txt ]
    then
        exit 1
    fi
    """.format(
        api_name = api_name,
    )

    ctx.actions.write(
        output = check_diff_script,
        content = check_diff_script_content,
    )
    runfiles = ctx.runfiles(files = [ctx.outputs.diff_output])
    return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)]

diff_integration_goldens_test = rule(
    attrs = {
        "gapic_library": attr.label(),
        "resource_name_library": attr.label(),
        "test_library": attr.label(),
        "srcs": attr.label_list(
            allow_files = True,
            mandatory = True,
        ),
    },
    outputs = {
        "diff_output": "%{name}_diff_output.txt",
        "check_diff_script": "%{name}_check_diff_script.sh",
    },
    implementation = _diff_integration_goldens_impl,
    test = True,
)

def integration_test(name, target, data):
    # Bazel target `java_gapic_library` will generate 3 source jars including the
    # the source Java code of the gapic_library, resource_name_library and test_library.
    diff_integration_goldens_test(
        name = name,
        gapic_library = target,
        resource_name_library = "%s_resource_name" % target,
        test_library = "%s_test" % target,
        srcs = data,
    )

def _overwrite_golden_impl(ctx):
    # Extract the Java source files from the generated 3 srcjars from API bazel target,
    # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.
    # Overwrite the goldens folder e.g `test/integration/goldens/redis` with the
    # code generation in `goldens_output_zip`.

    gapic_library = ctx.attr.gapic_library
    resource_name_library = ctx.attr.resource_name_library
    test_library = ctx.attr.test_library
    srcs = ctx.files.srcs

    # Convert the name of bazel rules e.g. `redis_update` to `redis`
    # because we will need to overwrite the goldens files in `redis` folder.
    api_name = "_".join(ctx.attr.name.split("_")[:-1])
    goldens_output_zip = ctx.outputs.goldens_output_zip

    script = """
    mkdir codegen_tmp
    unzip -j {input} -d codegen_tmp
    unzip -j {input_resource_name} -d  codegen_tmp
    unzip -j {input_test} -d codegen_tmp
    cd codegen_tmp
    # Remove unneeded non-Java files, like MANIFEST
    rm -rf $(find . -type f ! -name "*.java")
    zip -r ../{goldens_output_zip} .
    """.format(
        goldens_output_zip = goldens_output_zip.path,
        input = gapic_library[JavaInfo].source_jars[0].path,
        input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
        input_test = test_library[JavaInfo].source_jars[0].path,
    )

    ctx.actions.run_shell(
        inputs = srcs + [
            gapic_library[JavaInfo].source_jars[0],
            resource_name_library[JavaInfo].source_jars[0],
            test_library[JavaInfo].source_jars[0],
        ],
        outputs = [goldens_output_zip],
        command = script,
    )

    # Overwrite the goldens.
    golden_update_script_content = """
    cd ${{BUILD_WORKSPACE_DIRECTORY}}
    rm -r test/integration/goldens/{api_name}/*.java
    unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name}
    """.format(
        goldens_output_zip = goldens_output_zip.path,
        api_name = api_name,
    )
    ctx.actions.write(
        output = ctx.outputs.golden_update_script,
        content = golden_update_script_content,
        is_executable = True,
    )
    return [DefaultInfo(executable = ctx.outputs.golden_update_script)]

overwrite_golden = rule(
    attrs = {
        "gapic_library": attr.label(),
        "resource_name_library": attr.label(),
        "test_library": attr.label(),
        "srcs": attr.label_list(
            allow_files = True,
            mandatory = True,
        ),
    },
    outputs = {
        "goldens_output_zip": "%{name}.zip",
        "golden_update_script": "%{name}.sh",
    },
    executable = True,
    implementation = _overwrite_golden_impl,
)

def golden_update(name, target, data):
    overwrite_golden(
        name = name,
        gapic_library = target,
        resource_name_library = "%s_resource_name" % target,
        test_library = "%s_test" % target,
        srcs = data,
    )
<|endoftext|>


<commit_before>"""
Handle the command line parsing and select the correct sub process.
"""

import argparse
import sys
import pkg_resources  # part of setuptools

from .map import map_seq_files
from .mapping_stats import generate_stats
from .htseq_count import count
from .bigwig import generate_bigwig

version = pkg_resources.require("run_cgprna")[0].version

def main():
    """
    Sets up the parser and handles triggereing of correct sub-command
    """
    common_parser = argparse.ArgumentParser('parent', add_help=False)
    common_parser.add_argument('-v', '--version',
                               action='version',
                               version='%(prog)s ' + version)

    parser = argparse.ArgumentParser(prog='run-cgprna', parents=[common_parser])

    subparsers = parser.add_subparsers(help='sub-command help')

    # mapping arguments
    parser_a = subparsers.add_parser(
        'map',
        parents=[common_parser],
        description='Map RNA-Seq reads to a reference genome',
        epilog='Input can be either bam or \'f(ast)?q(\.gz)?\'.')
    parser_a.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        nargs='+',
        help='An input raw bam file, or one of a pair of FastQ file. (optionally gzip compressed).',
        required=True)
    parser_a.add_argument(
        '-r', '--reference', dest='ref',
        metavar='TAR|PATH',
        help='A reference bundle tar file or the path to reference root directory.',
        required=True)
    parser_a.add_argument(
        '-s', '--sample-name', dest='sample_name',
        metavar='STR',
        help='Sample name, which will used to prefix output file names and SM tag in the BAM file header.',
        required=True)
    parser_a.add_argument(
        '-sp', '--species', dest='species',
        metavar='STR',
        help='Species name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate reference files.',
        required=False)
    parser_a.add_argument(
        '-rb', '--reference-build', dest='ref_build',
        metavar='STR',
        help='Reference build name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate reference files.',
        required=False)
    parser_a.add_argument(
        '-gb', '--gene-build', dest='gene_build',
        metavar='STR',
        help='Gene build name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate for GTF file.',
        required=False)
    parser_a.add_argument(
        '-gtf', '--gene-build-gtf-name', dest='gene_build_gtf_name',
        metavar='STR',
        help='File name of the gene build file. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate the GTF file.',
        required=False)
    parser_a.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_a.add_argument(
        '-t', '--threads', dest='threads',
        metavar='INT', type=int, default=1,
        help='Number of threads to use.',
        required=False)
    parser_a.add_argument(
        '--rg-id-tag', dest='rg_id_tag',
        metavar='STR',
        help='Readgroup ID tag value in the output BAM. Default: 1 or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--lb-tag', dest='lb_tag',
        metavar='STR',
        help='Sequencing library tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--ds-tag', dest='ds_tag',
        metavar='STR',
        help='Description tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--pl-tag', dest='pl_tag',
        metavar='STR',
        help='Platform tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--pu-tag', dest='pu_tag',
        metavar='STR',
        help='Platform unit tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.set_defaults(func=map_seq_files)

    # create the parser for the "stats" command
    parser_b = subparsers.add_parser(
        'stats',
        parents=[common_parser],
        description='Generate mapping stats from a BAM file, with/without a BAM file in which reads were mapped to the transcriptome instead of genome.')
    parser_b.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_b.add_argument(
        '-r', '--reference', dest='ref',
        metavar='TAR|PATH',
        help='A reference bundle tar file or the path to reference root directory.',
        required=True)
    parser_b.add_argument(
        '-tb', '--transcriptome-bam', dest='trans_bam',
        metavar='FILE',
        help='BAM file, in which reads are mapped to a reference transciptome (NOT genome).',
        required=False)
    parser_b.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_b.set_defaults(func=generate_stats)

    # create the parser for the "bigwig" command
    parser_c = subparsers.add_parser(
        'bigwig',
        parents=[common_parser],
        description='Generate bigwig file from a BAM file.')
    parser_c.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_c.add_argument(
        '-r', '--reference', dest='ref',
        metavar='FASTA_FILE',
        help='FASTA file of a reference file, which the input BAM file was mapped to.',
        required=True)
    parser_c.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_c.add_argument(
        '-t', '--threads', dest='threads',
        metavar='INT', type=int, default=1,
        help='Number of threads to use.',
        required=False)
    parser_c.set_defaults(func=generate_bigwig)

    # create the parser for the "count" command
    parser_d = subparsers.add_parser(
        'count',
        parents=[common_parser],
        description='Generate gene counts from a BAM file.')
    parser_d.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_d.add_argument(
        '-r', '--reference', dest='ref',
        metavar='GTF_FILE',
        help='A reference GTF file.',
        required=True)
    parser_d.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_d.set_defaults(func=count)

    args = parser.parse_args()
    if len(sys.argv) > 1:
        args.func(args)
    else:
        sys.exit('\nError: missed required arguments.\n\tPlease run: run-cgprna --help\n')
<commit_msg>updated some help messages<commit_after>"""
Handle the command line parsing and select the correct sub process.
"""

import argparse
import sys
import pkg_resources  # part of setuptools

from .map import map_seq_files
from .mapping_stats import generate_stats
from .htseq_count import count
from .bigwig import generate_bigwig

version = pkg_resources.require("run_cgprna")[0].version

def main():
    """
    Sets up the parser and handles triggereing of correct sub-command
    """
    common_parser = argparse.ArgumentParser('parent', add_help=False)
    common_parser.add_argument('-v', '--version',
                               action='version',
                               version='%(prog)s ' + version)

    parser = argparse.ArgumentParser(prog='run-cgprna', parents=[common_parser])

    subparsers = parser.add_subparsers(help='sub-command help')

    # mapping arguments
    parser_a = subparsers.add_parser(
        'map',
        parents=[common_parser],
        description='Use STAR to map RNA-Seq reads to a reference genome',
        epilog='Input can be either bam or \'f(ast)?q(\.gz)?\'.')
    parser_a.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        nargs='+',
        help='An input raw bam file, or a pair of FastQ files split with spaces. (optionally gzip compressed).',
        required=True)
    parser_a.add_argument(
        '-r', '--reference', dest='ref',
        metavar='TAR|PATH',
        help='A reference bundle tar file or the path to reference root directory.',
        required=True)
    parser_a.add_argument(
        '-s', '--sample-name', dest='sample_name',
        metavar='STR',
        help='Sample name, which will used to prefix output file names and SM tag in the BAM file header.',
        required=True)
    parser_a.add_argument(
        '-sp', '--species', dest='species',
        metavar='STR',
        help='Species name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate reference files.',
        required=False)
    parser_a.add_argument(
        '-rb', '--reference-build', dest='ref_build',
        metavar='STR',
        help='Reference build name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate reference files.',
        required=False)
    parser_a.add_argument(
        '-gb', '--gene-build', dest='gene_build',
        metavar='STR',
        help='Gene build name. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate for GTF file.',
        required=False)
    parser_a.add_argument(
        '-gtf', '--gene-build-gtf-name', dest='gene_build_gtf_name',
        metavar='STR',
        help='File name of the gene build file. No need to set if using a pre-built reference bundle. If using a folder as the reference, it\' be used to locate the GTF file.',
        required=False)
    parser_a.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_a.add_argument(
        '-t', '--threads', dest='threads',
        metavar='INT', type=int, default=1,
        help='Number of threads to use.',
        required=False)
    parser_a.add_argument(
        '--rg-id-tag', dest='rg_id_tag',
        metavar='STR',
        help='Readgroup ID tag value in the output BAM. Default: 1 or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--lb-tag', dest='lb_tag',
        metavar='STR',
        help='Sequencing library tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--ds-tag', dest='ds_tag',
        metavar='STR',
        help='Description tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--pl-tag', dest='pl_tag',
        metavar='STR',
        help='Platform tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.add_argument(
        '--pu-tag', dest='pu_tag',
        metavar='STR',
        help='Platform unit tag value in the output BAM header. Default: None or taken from the input raw BAM file.',
        required=False)
    parser_a.set_defaults(func=map_seq_files)

    # create the parser for the "stats" command
    parser_b = subparsers.add_parser(
        'stats',
        parents=[common_parser],
        description='Generate mapping stats from a BAM file, with/without a BAM file in which reads were mapped to the transcriptome instead of genome.')
    parser_b.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_b.add_argument(
        '-r', '--reference', dest='ref',
        metavar='TAR|PATH',
        help='A reference bundle tar file or the path to reference root directory.',
        required=True)
    parser_b.add_argument(
        '-tb', '--transcriptome-bam', dest='trans_bam',
        metavar='FILE',
        help='BAM file, in which reads are mapped to a reference transciptome (NOT genome).',
        required=False)
    parser_b.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_b.set_defaults(func=generate_stats)

    # create the parser for the "bigwig" command
    parser_c = subparsers.add_parser(
        'bigwig',
        parents=[common_parser],
        description='Generate bigwig file from a BAM file.')
    parser_c.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_c.add_argument(
        '-r', '--reference', dest='ref',
        metavar='FASTA_FILE',
        help='FASTA file of a reference file, which the input BAM file was mapped to.',
        required=True)
    parser_c.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_c.add_argument(
        '-t', '--threads', dest='threads',
        metavar='INT', type=int, default=1,
        help='Number of threads to use.',
        required=False)
    parser_c.set_defaults(func=generate_bigwig)

    # create the parser for the "count" command
    parser_d = subparsers.add_parser(
        'count',
        parents=[common_parser],
        description='Generate gene counts from a BAM file.')
    parser_d.add_argument(
        '-i', '--input', dest='input',
        metavar='FILE',
        help='Input BAM file, in which reads are mapped to a reference genome (NOT transcriptome).',
        required=True)
    parser_d.add_argument(
        '-r', '--reference', dest='ref',
        metavar='GTF_FILE',
        help='A reference GTF file.',
        required=True)
    parser_d.add_argument(
        '-od', '--output-directory', dest='out_dir',
        metavar='DIR', default='.',
        help='Output directory. Default: current directory.',
        required=False)
    parser_d.set_defaults(func=count)

    args = parser.parse_args()
    if len(sys.argv) > 1:
        args.func(args)
    else:
        sys.exit('\nError: missed required arguments.\n\tPlease run: run-cgprna --help\n')
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from appium.webdriver.common.touch_action import TouchAction

from AppiumLibrary.locators import ElementFinder
from .keywordgroup import KeywordGroup

class _TouchKeywords(KeywordGroup):

    def __init__(self):
        self._element_finder = ElementFinder()

    # Public, element lookups
    def zoom(self, locator, percent="200%", steps=1):
        """
        Zooms in on an element a certain amount.
        """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.zoom(element=element, percent=percent, steps=steps)

    def pinch(self, locator, percent="200%", steps=1):
        """
        Pinch in on an element a certain amount.
        """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.pinch(element=element, percent=percent, steps=steps)

    def swipe(self, start_x, start_y, offset_x, offset_y, duration=1000):
        """
        Swipe from one point to another point, for an optional duration.

        Args:
         - start_x - x-coordinate at which to start
         - start_y - y-coordinate at which to start
         - offset_x - x-coordinate distance from start_x at which to stop
         - offset_y - y-coordinate distance from start_y at which to stop
         - duration - (optional) time to take the swipe, in ms.

        Usage:
        | Swipe | 500 | 100 | 100 | 0 | 1000 |

        _*NOTE: *_
        Android 'Swipe' is not working properly, use ``offset_x`` and ``offset_y`` as if these are destination points.
        """
        driver = self._current_application()
        driver.swipe(start_x, start_y, offset_x, offset_y, duration)

    def swipe_by_percent(self, start_x, start_y, end_x, end_y, duration=1000):
        """
        Swipe from one percent of the screen to another percent, for an optional duration.
        Normal swipe fails to scale for different screen resolutions, this can be avoided using percent.

        Args:
         - start_x - x-percent at which to start
         - start_y - y-percent at which to start
         - end_x - x-percent distance from start_x at which to stop
         - end_y - y-percent distance from start_y at which to stop
         - duration - (optional) time to take the swipe, in ms.

        Usage:
        | Swipe By Percent | 90 | 50 | 10 | 50 | # Swipes screen from right to left. |

        _*NOTE: *_
        This also considers swipe acts different between iOS and Android.

        New in AppiumLibrary 1.4.5
        """
        width = self.get_window_width()
        height = self.get_window_height()
        x_start = float(start_x) / 100 * width
        x_end = float(end_x) / 100 * width
        y_start = float(start_y) / 100 * height
        y_end = float(end_y) / 100 * height
        x_offset = x_end - x_start
        y_offset = y_end - y_start
        platform = self._get_platform()
        if platform == 'android':
            self.swipe(x_start, y_start, x_end, y_end, duration)
        else:
            self.swipe(x_start, y_start, x_offset, y_offset, duration)

    def scroll(self, start_locator, end_locator):
        """
        Scrolls from one element to another
        Key attributes for arbitrary elements are `id` and `name`. See
        `introduction` for details about locating elements.
        """
        el1 = self._element_find(start_locator, True, True)
        el2 = self._element_find(end_locator, True, True)
        driver = self._current_application()
        driver.scroll(el1, el2)

    def scroll_down(self, locator):
        """Scrolls down to element"""
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.execute_script("mobile: scroll", {"direction": 'down', 'element': element.id})

    def scroll_up(self, locator):
        """Scrolls up to element"""
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.execute_script("mobile: scroll", {"direction": 'up', 'element': element.id})

    def long_press(self, locator, duration=1000):
        """ Long press the element with optional duration """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        long_press = TouchAction(driver).long_press(element, duration)
        long_press.perform()

    def tap(self, locator, x_offset=None, y_offset=None, count=1):
        """ Tap element identified by ``locator``.

        Args:
        - ``x_offset`` - (optional) x coordinate to tap, relative to the top left corner of the element.
        - ``y_offset`` - (optional) y coordinate. If y is used, x must also be set, and vice versa
        - ``count`` - can be used for multiple times of tap on that element
        """
        driver = self._current_application()
        el = self._element_find(locator, True, True)
        action = TouchAction(driver)
        action.tap(el,x_offset,y_offset, count).perform()

    def click_a_point(self, x=0, y=0, duration=100):
        """ Click on a point"""
        self._info("Clicking on a point (%s,%s)." % (x,y))
        driver = self._current_application()
        action = TouchAction(driver)
        try:
            action.press(x=float(x), y=float(y)).wait(float(duration)).release().perform()
        except:
            assert False, "Can't click on a point at (%s,%s)" % (x,y)

    def click_element_at_coordinates(self, coordinate_X, coordinate_Y):
        """ click element at a certain coordinate """
        self._info("Pressing at (%s, %s)." % (coordinate_X, coordinate_Y))
        driver = self._current_application()
        action = TouchAction(driver)
        action.press(x=coordinate_X, y=coordinate_Y).release().perform()
<commit_msg>Fix long press for iPhone<commit_after># -*- coding: utf-8 -*-

from appium.webdriver.common.touch_action import TouchAction

from AppiumLibrary.locators import ElementFinder
from .keywordgroup import KeywordGroup

class _TouchKeywords(KeywordGroup):

    def __init__(self):
        self._element_finder = ElementFinder()

    # Public, element lookups
    def zoom(self, locator, percent="200%", steps=1):
        """
        Zooms in on an element a certain amount.
        """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.zoom(element=element, percent=percent, steps=steps)

    def pinch(self, locator, percent="200%", steps=1):
        """
        Pinch in on an element a certain amount.
        """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.pinch(element=element, percent=percent, steps=steps)

    def swipe(self, start_x, start_y, offset_x, offset_y, duration=1000):
        """
        Swipe from one point to another point, for an optional duration.

        Args:
         - start_x - x-coordinate at which to start
         - start_y - y-coordinate at which to start
         - offset_x - x-coordinate distance from start_x at which to stop
         - offset_y - y-coordinate distance from start_y at which to stop
         - duration - (optional) time to take the swipe, in ms.

        Usage:
        | Swipe | 500 | 100 | 100 | 0 | 1000 |

        _*NOTE: *_
        Android 'Swipe' is not working properly, use ``offset_x`` and ``offset_y`` as if these are destination points.
        """
        driver = self._current_application()
        driver.swipe(start_x, start_y, offset_x, offset_y, duration)

    def swipe_by_percent(self, start_x, start_y, end_x, end_y, duration=1000):
        """
        Swipe from one percent of the screen to another percent, for an optional duration.
        Normal swipe fails to scale for different screen resolutions, this can be avoided using percent.

        Args:
         - start_x - x-percent at which to start
         - start_y - y-percent at which to start
         - end_x - x-percent distance from start_x at which to stop
         - end_y - y-percent distance from start_y at which to stop
         - duration - (optional) time to take the swipe, in ms.

        Usage:
        | Swipe By Percent | 90 | 50 | 10 | 50 | # Swipes screen from right to left. |

        _*NOTE: *_
        This also considers swipe acts different between iOS and Android.

        New in AppiumLibrary 1.4.5
        """
        width = self.get_window_width()
        height = self.get_window_height()
        x_start = float(start_x) / 100 * width
        x_end = float(end_x) / 100 * width
        y_start = float(start_y) / 100 * height
        y_end = float(end_y) / 100 * height
        x_offset = x_end - x_start
        y_offset = y_end - y_start
        platform = self._get_platform()
        if platform == 'android':
            self.swipe(x_start, y_start, x_end, y_end, duration)
        else:
            self.swipe(x_start, y_start, x_offset, y_offset, duration)

    def scroll(self, start_locator, end_locator):
        """
        Scrolls from one element to another
        Key attributes for arbitrary elements are `id` and `name`. See
        `introduction` for details about locating elements.
        """
        el1 = self._element_find(start_locator, True, True)
        el2 = self._element_find(end_locator, True, True)
        driver = self._current_application()
        driver.scroll(el1, el2)

    def scroll_down(self, locator):
        """Scrolls down to element"""
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.execute_script("mobile: scroll", {"direction": 'down', 'element': element.id})

    def scroll_up(self, locator):
        """Scrolls up to element"""
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        driver.execute_script("mobile: scroll", {"direction": 'up', 'element': element.id})

    def long_press(self, locator, duration=1000):
        """ Long press the element with optional duration """
        driver = self._current_application()
        element = self._element_find(locator, True, True)
        action = TouchAction(driver)
        action.press(element).wait(duration).release().perform()

    def tap(self, locator, x_offset=None, y_offset=None, count=1):
        """ Tap element identified by ``locator``.

        Args:
        - ``x_offset`` - (optional) x coordinate to tap, relative to the top left corner of the element.
        - ``y_offset`` - (optional) y coordinate. If y is used, x must also be set, and vice versa
        - ``count`` - can be used for multiple times of tap on that element
        """
        driver = self._current_application()
        el = self._element_find(locator, True, True)
        action = TouchAction(driver)
        action.tap(el,x_offset,y_offset, count).perform()

    def click_a_point(self, x=0, y=0, duration=100):
        """ Click on a point"""
        self._info("Clicking on a point (%s,%s)." % (x,y))
        driver = self._current_application()
        action = TouchAction(driver)
        try:
            action.press(x=float(x), y=float(y)).wait(float(duration)).release().perform()
        except:
            assert False, "Can't click on a point at (%s,%s)" % (x,y)

    def click_element_at_coordinates(self, coordinate_X, coordinate_Y):
        """ click element at a certain coordinate """
        self._info("Pressing at (%s, %s)." % (coordinate_X, coordinate_Y))
        driver = self._current_application()
        action = TouchAction(driver)
        action.press(x=coordinate_X, y=coordinate_Y).release().perform()
<|endoftext|>


<commit_before>"""
Configure Flask app instance
"""

import logging
import os

from flask import Flask

app = Flask(__name__)

# Running on heroku
if 'HEROKU' in os.environ:
    import example_config

    # example_config.py provides a blueprint for which variables to look for in
    # the environment and set in our app config.
    for var in example_config.HEROKU_ENV_REQUIREMENTS:
        try:
            value = os.environ[var]
        except KeyError:
            print 'Unable to find environment variable %s, defaulting to None' % (var)
            value = None

        app.config.setdefault(var, value)

        if var == 'SECRET_KEY':
            app.secret_key = os.environ[var]

    if 'DEBUG' in os.environ:
        app.debug = True

        print 'Config values'
        for key, value in app.config.iteritems():
            print key, value

else:
    try:
        app.config.from_object(os.environ['APP_SETTINGS'])
    except KeyError:
        import example_config
        print 'Unable to find configuration, using example'
        app.config.from_object(example_config.DevelopmentConfig)

    app.secret_key = app.config['SECRET_KEY']

# Force flask to log to a file otherwise if we're not in DEBUG mode the logs
# won't show up in the console (on heroku)
if not app.debug:
    app.logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s [in %(pathname)s:%(lineno)d]: %(message)s '))

    app.logger.addHandler(handler)

import pskb_website.views
import pskb_website.filters

app.jinja_env.filters['date_string'] = filters.date_string
app.jinja_env.filters['url_for_article'] = filters.url_for_article
app.jinja_env.filters['url_for_user'] = filters.url_for_user
app.jinja_env.filters['author_name'] = filters.author_name
<commit_msg>Fixed Heroku environment verification<commit_after>"""
Configure Flask app instance
"""

import logging
import os

from flask import Flask

app = Flask(__name__)

# Running on heroku
if 'HEROKU' in os.environ and os.environ['HEROKU'].lower() in ('true', 'yes', '1'):
    import example_config

    # example_config.py provides a blueprint for which variables to look for in
    # the environment and set in our app config.
    for var in example_config.HEROKU_ENV_REQUIREMENTS:
        try:
            value = os.environ[var]
        except KeyError:
            print 'Unable to find environment variable %s, defaulting to None' % (var)
            value = None

        app.config.setdefault(var, value)

        if var == 'SECRET_KEY':
            app.secret_key = os.environ[var]

    if 'DEBUG' in os.environ:
        app.debug = True

        print 'Config values'
        for key, value in app.config.iteritems():
            print key, value

else:
    try:
        app.config.from_object(os.environ['APP_SETTINGS'])
    except KeyError:
        import example_config
        print 'Unable to find configuration, using example'
        app.config.from_object(example_config.DevelopmentConfig)

    app.secret_key = app.config['SECRET_KEY']

# Force flask to log to a file otherwise if we're not in DEBUG mode the logs
# won't show up in the console (on heroku)
if not app.debug:
    app.logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s [in %(pathname)s:%(lineno)d]: %(message)s '))

    app.logger.addHandler(handler)

import pskb_website.views
import pskb_website.filters

app.jinja_env.filters['date_string'] = filters.date_string
app.jinja_env.filters['url_for_article'] = filters.url_for_article
app.jinja_env.filters['url_for_user'] = filters.url_for_user
app.jinja_env.filters['author_name'] = filters.author_name
<|endoftext|>


<commit_before>import torch
import torch.nn as nn
import torch.nn.functional as F

class conv2DBatchNorm(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(conv2DBatchNorm, self).__init__()

        self.cb_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                               padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),)

    def forward(self, inputs):
        outputs = self.cb_unit(inputs)
        return outputs

class deconv2DBatchNorm(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(deconv2DBatchNorm, self).__init__()

        self.dcb_unit = nn.Sequential(nn.ConvTranspose2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                               padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),)

    def forward(self, inputs):
        outputs = self.dcb_unit(inputs)
        return outputs

class conv2DBatchNormRelu(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(conv2DBatchNormRelu, self).__init__()

        self.cbr_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                                padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),
                                 nn.ReLU(inplace=True),)

    def forward(self, inputs):
        outputs = self.cbr_unit(inputs)
        return outputs

class deconv2DBatchNormRelu(nn.Module):
    def __init__(self, in_channels, n_filters, k_size, stride, padding, bias=True):
        super(deconv2DBatchNormRelu, self).__init__()

        self.dcbr_unit = nn.Sequential(nn.ConvTranspose2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                                padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),
                                 nn.ReLU(inplace=True),)

    def forward(self, inputs):
        outputs = self.dcbr_unit(inputs)
        return outputs

class unetConv2(nn.Module):
    def __init__(self, in_size, out_size, is_batchnorm):
        super(unetConv2, self).__init__()

        if is_batchnorm:
            self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0),
                                       nn.BatchNorm2d(out_size),
                                       nn.ReLU(),)
            self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0),
                                       nn.BatchNorm2d(out_size),
                                       nn.ReLU(),)
        else:
            self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0),
                                       nn.ReLU(),)
            self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0),
                                       nn.ReLU(),)
    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        return outputs

class unetUp(nn.Module):
    def __init__(self, in_size, out_size, is_deconv):
        super(unetUp, self).__init__()
        self.conv = unetConv2(in_size, out_size, False)
        if is_deconv:
            self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
        else:
            self.up = nn.UpsamplingBilinear2d(scale_factor=2)

    def forward(self, inputs1, inputs2):
        outputs2 = self.up(inputs2)
        offset = outputs2.size()[2] - inputs1.size()[2]
        padding = 2 * [offset // 2, offset // 2]
        outputs1 = F.pad(inputs1, padding)
        return self.conv(torch.cat([outputs1, outputs2], 1))

class segnetDown2(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetDown2, self).__init__()
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.maxpool_with_argmax = nn.MaxPool2d(2, 2, return_indices=True)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        unpooled_shape = outputs.size()
        outputs, indices = self.maxpool_with_argmax(outputs)
        return outputs, indices, unpooled_shape

class segnetDown3(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetDown3, self).__init__()
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.conv3 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.maxpool_with_argmax = nn.MaxPool2d(2, 2, return_indices=True)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        outputs = self.conv3(outputs)
        unpooled_shape = outputs.size()
        outputs, indices = self.maxpool_with_argmax(outputs)
        return outputs, indices, unpooled_shape

class segnetUp2(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetUp2, self).__init__()
        self.unpool = nn.MaxUnpool2d(2, 2)
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)

    def forward(self, inputs, indices, output_shape):
        outputs = self.unpool(input=inputs, indices=indices, output_size=output_shape)
        outputs = self.conv1(outputs)
        outputs = self.conv2(outputs)
        return outputs

class segnetUp3(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetUp3, self).__init__()
        self.unpool = nn.MaxUnpool2d(2, 2)
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.conv3 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)

    def forward(self, inputs, indices, output_shape):
        outputs = self.unpool(input=inputs, indices=indices, output_size=output_shape)
        outputs = self.conv1(outputs)
        outputs = self.conv2(outputs)
        outputs = self.conv3(outputs)
        return outputs

class residualBlock(nn.Module):
    expansion = 1

    def __init__(self, in_channels, n_filters, stride=1, downsample=None):
        super(residualBlock, self).__init__()

        self.convbnrelu1 = conv2DBatchNormRelu(in_channels, n_filters, 3,  stride, 1, bias=False)
        self.convbn2 = conv2DBatchNorm(n_filters, n_filters, 3, 1, 1, bias=False)
        self.downsample = downsample
        self.stride = stride
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        residual = x

        out = self.convbnrelu1(x)
        out = self.convbn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)
        return out

class residualBottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, n_filters, stride=1, downsample=None):
        super(residualBottleneck, self).__init__()
        self.convbn1 = nn.Conv2DBatchNorm(in_channels,  n_filters, k_size=1, bias=False)
        self.convbn2 = nn.Conv2DBatchNorm(n_filters,  n_filters, k_size=3, padding=1, stride=stride, bias=False)
        self.convbn3 = nn.Conv2DBatchNorm(n_filters,  n_filters * 4, k_size=1, bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.convbn1(x)
        out = self.convbn2(out)
        out = self.convbn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class linknetUp(nn.Module):
    def __init__(self, in_channels, n_filters):
        super(linknetUp, self).__init__()

        # B, 2C, H, W -> B, C/2, H, W
        self.convbnrelu1 = conv2DBatchNormRelu(in_channels, n_filters/2, k_size=1, stride=1, padding=1)

        # B, C/2, H, W -> B, C/2, H, W
        self.deconvbnrelu2 = nn.deconv2DBatchNormRelu(n_filters/2, n_filters/2, k_size=3,  stride=2, padding=0,)

        # B, C/2, H, W -> B, C, H, W
        self.convbnrelu3 = conv2DBatchNormRelu(n_filters/2, n_filters, k_size=1, stride=1, padding=1)

    def forward(self, x):
        x = self.convbnrelu1(x)
        x = self.deconvbnrelu2(x)
        x = self.convbnrelu3(x)
        return x

class FRRU(nn.Module):
    def __init__(self, prev_channels, out_channels, scale):
        super(FRRU, self).__init__()
        self.scale = scale
        self.prev_channels = prev_channels
        self.out_channels = out_channels

        self.conv1 = conv2DBatchNormRelu(prev_channels + 32, out_channels, k_size=3, stride=1, padding=1)
        self.conv2 = conv2DBatchNormRelu(out_channels, out_channels, k_size=3, stride=1, padding=1)
        self.conv_res = nn.Conv2d(out_channels, 32, kernel_size=1, stride=1, padding=0)

    def forward(self, y, z):
        x = torch.cat([y, nn.MaxPool2d(self.scale, self.scale)(z)], dim=1)
        y_prime = self.conv1(x)
        y_prime = self.conv2(y_prime)

        x = self.conv_res(y_prime)
        upsample_size = torch.Size([_s*self.scale for _s in y_prime.shape[-2:]])
        x = F.upsample(x, size=upsample_size, mode='nearest')
        z_prime = z + x

        return y_prime, z_prime

class RU(nn.Module):
    def __init__(self, channels, kernel_size=3, strides=1):
        super(RU, self).__init__()

        self.conv1 = conv2DBatchNormRelu(channels, channels, k_size=kernel_size, stride=strides, padding=1)
        self.conv2 = conv2DBatchNorm(channels, channels, k_size=kernel_size, stride=strides, padding=1)

    def forward(self, x):
        incoming = x
        x = self.conv1(x)
        x = self.conv2(x)
        return x + incoming<commit_msg>Fix bugs in upstream modules of segnet<commit_after>import torch
import torch.nn as nn
import torch.nn.functional as F

class conv2DBatchNorm(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(conv2DBatchNorm, self).__init__()

        self.cb_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                               padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),)

    def forward(self, inputs):
        outputs = self.cb_unit(inputs)
        return outputs

class deconv2DBatchNorm(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(deconv2DBatchNorm, self).__init__()

        self.dcb_unit = nn.Sequential(nn.ConvTranspose2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                               padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),)

    def forward(self, inputs):
        outputs = self.dcb_unit(inputs)
        return outputs

class conv2DBatchNormRelu(nn.Module):
    def __init__(self, in_channels, n_filters, k_size,  stride, padding, bias=True):
        super(conv2DBatchNormRelu, self).__init__()

        self.cbr_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                                padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),
                                 nn.ReLU(inplace=True),)

    def forward(self, inputs):
        outputs = self.cbr_unit(inputs)
        return outputs

class deconv2DBatchNormRelu(nn.Module):
    def __init__(self, in_channels, n_filters, k_size, stride, padding, bias=True):
        super(deconv2DBatchNormRelu, self).__init__()

        self.dcbr_unit = nn.Sequential(nn.ConvTranspose2d(int(in_channels), int(n_filters), kernel_size=k_size,
                                                padding=padding, stride=stride, bias=bias),
                                 nn.BatchNorm2d(int(n_filters)),
                                 nn.ReLU(inplace=True),)

    def forward(self, inputs):
        outputs = self.dcbr_unit(inputs)
        return outputs

class unetConv2(nn.Module):
    def __init__(self, in_size, out_size, is_batchnorm):
        super(unetConv2, self).__init__()

        if is_batchnorm:
            self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0),
                                       nn.BatchNorm2d(out_size),
                                       nn.ReLU(),)
            self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0),
                                       nn.BatchNorm2d(out_size),
                                       nn.ReLU(),)
        else:
            self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0),
                                       nn.ReLU(),)
            self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0),
                                       nn.ReLU(),)
    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        return outputs

class unetUp(nn.Module):
    def __init__(self, in_size, out_size, is_deconv):
        super(unetUp, self).__init__()
        self.conv = unetConv2(in_size, out_size, False)
        if is_deconv:
            self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
        else:
            self.up = nn.UpsamplingBilinear2d(scale_factor=2)

    def forward(self, inputs1, inputs2):
        outputs2 = self.up(inputs2)
        offset = outputs2.size()[2] - inputs1.size()[2]
        padding = 2 * [offset // 2, offset // 2]
        outputs1 = F.pad(inputs1, padding)
        return self.conv(torch.cat([outputs1, outputs2], 1))

class segnetDown2(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetDown2, self).__init__()
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.maxpool_with_argmax = nn.MaxPool2d(2, 2, return_indices=True)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        unpooled_shape = outputs.size()
        outputs, indices = self.maxpool_with_argmax(outputs)
        return outputs, indices, unpooled_shape

class segnetDown3(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetDown3, self).__init__()
        self.conv1 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.conv3 = conv2DBatchNormRelu(out_size, out_size, 3, 1, 1)
        self.maxpool_with_argmax = nn.MaxPool2d(2, 2, return_indices=True)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        outputs = self.conv3(outputs)
        unpooled_shape = outputs.size()
        outputs, indices = self.maxpool_with_argmax(outputs)
        return outputs, indices, unpooled_shape

class segnetUp2(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetUp2, self).__init__()
        self.unpool = nn.MaxUnpool2d(2, 2)
        self.conv1 = conv2DBatchNormRelu(in_size, in_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)

    def forward(self, inputs, indices, output_shape):
        outputs = self.unpool(input=inputs, indices=indices, output_size=output_shape)
        outputs = self.conv1(outputs)
        outputs = self.conv2(outputs)
        return outputs

class segnetUp3(nn.Module):
    def __init__(self, in_size, out_size):
        super(segnetUp3, self).__init__()
        self.unpool = nn.MaxUnpool2d(2, 2)
        self.conv1 = conv2DBatchNormRelu(in_size, in_size, 3, 1, 1)
        self.conv2 = conv2DBatchNormRelu(in_size, in_size, 3, 1, 1)
        self.conv3 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)

    def forward(self, inputs, indices, output_shape):
        outputs = self.unpool(input=inputs, indices=indices, output_size=output_shape)
        outputs = self.conv1(outputs)
        outputs = self.conv2(outputs)
        outputs = self.conv3(outputs)
        return outputs

class residualBlock(nn.Module):
    expansion = 1

    def __init__(self, in_channels, n_filters, stride=1, downsample=None):
        super(residualBlock, self).__init__()

        self.convbnrelu1 = conv2DBatchNormRelu(in_channels, n_filters, 3,  stride, 1, bias=False)
        self.convbn2 = conv2DBatchNorm(n_filters, n_filters, 3, 1, 1, bias=False)
        self.downsample = downsample
        self.stride = stride
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        residual = x

        out = self.convbnrelu1(x)
        out = self.convbn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)
        return out

class residualBottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, n_filters, stride=1, downsample=None):
        super(residualBottleneck, self).__init__()
        self.convbn1 = nn.Conv2DBatchNorm(in_channels,  n_filters, k_size=1, bias=False)
        self.convbn2 = nn.Conv2DBatchNorm(n_filters,  n_filters, k_size=3, padding=1, stride=stride, bias=False)
        self.convbn3 = nn.Conv2DBatchNorm(n_filters,  n_filters * 4, k_size=1, bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.convbn1(x)
        out = self.convbn2(out)
        out = self.convbn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class linknetUp(nn.Module):
    def __init__(self, in_channels, n_filters):
        super(linknetUp, self).__init__()

        # B, 2C, H, W -> B, C/2, H, W
        self.convbnrelu1 = conv2DBatchNormRelu(in_channels, n_filters/2, k_size=1, stride=1, padding=1)

        # B, C/2, H, W -> B, C/2, H, W
        self.deconvbnrelu2 = nn.deconv2DBatchNormRelu(n_filters/2, n_filters/2, k_size=3,  stride=2, padding=0,)

        # B, C/2, H, W -> B, C, H, W
        self.convbnrelu3 = conv2DBatchNormRelu(n_filters/2, n_filters, k_size=1, stride=1, padding=1)

    def forward(self, x):
        x = self.convbnrelu1(x)
        x = self.deconvbnrelu2(x)
        x = self.convbnrelu3(x)
        return x

class FRRU(nn.Module):
    def __init__(self, prev_channels, out_channels, scale):
        super(FRRU, self).__init__()
        self.scale = scale
        self.prev_channels = prev_channels
        self.out_channels = out_channels

        self.conv1 = conv2DBatchNormRelu(prev_channels + 32, out_channels, k_size=3, stride=1, padding=1)
        self.conv2 = conv2DBatchNormRelu(out_channels, out_channels, k_size=3, stride=1, padding=1)
        self.conv_res = nn.Conv2d(out_channels, 32, kernel_size=1, stride=1, padding=0)

    def forward(self, y, z):
        x = torch.cat([y, nn.MaxPool2d(self.scale, self.scale)(z)], dim=1)
        y_prime = self.conv1(x)
        y_prime = self.conv2(y_prime)

        x = self.conv_res(y_prime)
        upsample_size = torch.Size([_s*self.scale for _s in y_prime.shape[-2:]])
        x = F.upsample(x, size=upsample_size, mode='nearest')
        z_prime = z + x

        return y_prime, z_prime

class RU(nn.Module):
    def __init__(self, channels, kernel_size=3, strides=1):
        super(RU, self).__init__()

        self.conv1 = conv2DBatchNormRelu(channels, channels, k_size=kernel_size, stride=strides, padding=1)
        self.conv2 = conv2DBatchNorm(channels, channels, k_size=kernel_size, stride=strides, padding=1)

    def forward(self, x):
        incoming = x
        x = self.conv1(x)
        x = self.conv2(x)
        return x + incoming
<|endoftext|>


<commit_before>from .utils import Command, Notification, WIPObject
from .Runtime import RemoteObject
import json
import re

### Console.clearMessages
def canSetScriptSource():
    command = Command('Debugger.canSetScriptSource', {})
    return command

def enable():
    command = Command('Debugger.enable', {})
    return command

def setPauseOnExceptions(state):
    command = Command('Debugger.setPauseOnExceptions', {"state": state})
    return command

def setOverlayMessage(message=None):
    if message:
        command = Command('Page.setOverlayMessage', {"message":"Paused in Sublime Web Inspector"})
    else:
        command = Command('Page.setOverlayMessage', {})
    return command

def evaluateOnCallFrame(callFrameId, expression):
    params = {}
    params['callFrameId'] = callFrameId()
    params['expression'] = expression
    command = Command('Debugger.evaluateOnCallFrame', params)
    return command

def evaluateOnCallFrame_parser(result):
    data = RemoteObject(result['result'])
    return data

def disable():
    command = Command('Debugger.disable', {})
    return command

def pause():
    command = Command('Debugger.pause', {})
    return command

def resume():
    command = Command('Debugger.resume', {})
    return command

def stepInto():
    command = Command('Debugger.stepInto', {})
    return command

def stepOut():
    command = Command('Debugger.stepOut', {})
    return command

def stepOver():
    command = Command('Debugger.stepOver', {})
    return command

def removeBreakpoint(breakpointId):
    params = {}
    params['breakpointId'] = breakpointId
    command = Command('Debugger.removeBreakpoint', params)
    return command

def setBreakpoint(location, condition=None):
    params = {}

    location.lineNumber = location.lineNumber-1
    params['location'] = location()

    if condition:
        params['condition'] = condition

    command = Command('Debugger.setBreakpoint', params)
    return command

def setBreakpoint_parser(result):
    data = {}
    data['breakpointId'] = BreakpointId(result['breakpointId'])
    data['actualLocation'] = Location(result['actualLocation'])
    data['actualLocation'].lineNumber = data['actualLocation'].lineNumber+1
    return data

def setScriptSource(scriptId, scriptSource):
    params = {}
    params['scriptId'] = scriptId
    params['scriptSource'] = scriptSource

    command = Command('Debugger.setScriptSource', params)
    return command

def setScriptSource_parser(result):
    data = {}
    data['callFrames'] = []
    for callFrame in result['callFrames']:
        data['callFrames'].append(CallFrame(callFrame))
    return data

def setBreakpointByUrl(lineNumber, url, urlRegex=None, columnNumber=None, condition=None):
    params = {}
    params['lineNumber'] = lineNumber-1
    params['url'] = restoreQueryString(url)

    if urlRegex:
        params['urlRegex'] = urlRegex

    if columnNumber:
        params['columnNumber'] = columnNumber
    else:
        params['columnNumber'] = 0

    if condition:
        params['condition'] = condition
    else:
        params['condition'] = ''

    command = Command('Debugger.setBreakpointByUrl', params)
    return command

def setBreakpointByUrl_parser(result):
    data = {}
    data['breakpointId'] = BreakpointId(result['breakpointId'])
    data['locations'] = []
    for location in result['locations']:
        location_found = Location(location)
        location_found.lineNumber = location_found.lineNumber + 1
        data['locations'].append(location_found)
    return data

def scriptParsed():
    notification = Notification('Debugger.scriptParsed')
    return notification

def scriptParsed_parser(params):
    url = stripQueryString(params['url'])
    return {'scriptId': ScriptId(params['scriptId']), 'url': url}

def paused():
    notification = Notification('Debugger.paused')
    return notification

def paused_parser(params):
    data = {}
    data['callFrames'] = []
    for callFrame in params['callFrames']:
        data['callFrames'].append(CallFrame(callFrame))
    data['reason'] = params['reason']
    return data

url_to_originalUrl = {}

def stripQueryString(url):
    # Some users use query strings as cache breakers
    # We don't want these in our mapping process or window titles
    # Strip on url from debuggee, restore on url to debuggee
    url_parts = url.split("/")
    url_parts[-1] = re.sub(r"\?.*$", "", url_parts[-1])
    cleanUrl = "/".join(url_parts)
    if url != cleanUrl:
        url_to_originalUrl[cleanUrl] = url
    return cleanUrl

def restoreQueryString(url):
    if url in url_to_originalUrl:
        url = url_to_originalUrl[url]
    return url

def resumed():
    notification = Notification('Debugger.resumed')
    return notification

class BreakpointId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class CallFrameId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class ScriptId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class Scope(WIPObject):
    def __init__(self, value):
        self.set_class(value, 'object', RemoteObject)
        self.set(value, 'type')

class Location(WIPObject):
    def __init__(self, value):
        self.set(value, 'columnNumber')
        self.set(value, 'lineNumber')
        self.set_class(value, 'scriptId', ScriptId)

    def __call__(self):
        obj = {}
        if self.columnNumber:
            obj['columnNumber'] = self.columnNumber
        obj['lineNumber'] = self.lineNumber
        obj['scriptId'] = self.scriptId()
        return obj

class CallFrame(WIPObject):
    def __init__(self, value):
        self.set_class(value, 'callFrameId', CallFrameId)
        self.set(value, 'functionName')
        self.set_class(value, 'location', Location)
        self.location.lineNumber = self.location.lineNumber+1
        self.scopeChain = []
        if 'scopeChain' in value:
            for scope in value['scopeChain']:
                self.scopeChain.append(Scope(scope))

    def __str__(self):
        return "%s:%d %s" % (self.location.scriptId, self.location.lineNumber, self.functionName)
<commit_msg>dead comment<commit_after>from .utils import Command, Notification, WIPObject
from .Runtime import RemoteObject
import json
import re

def canSetScriptSource():
    command = Command('Debugger.canSetScriptSource', {})
    return command

def enable():
    command = Command('Debugger.enable', {})
    return command

def setPauseOnExceptions(state):
    command = Command('Debugger.setPauseOnExceptions', {"state": state})
    return command

def setOverlayMessage(message=None):
    if message:
        command = Command('Page.setOverlayMessage', {"message":"Paused in Sublime Web Inspector"})
    else:
        command = Command('Page.setOverlayMessage', {})
    return command

def evaluateOnCallFrame(callFrameId, expression):
    params = {}
    params['callFrameId'] = callFrameId()
    params['expression'] = expression
    command = Command('Debugger.evaluateOnCallFrame', params)
    return command

def evaluateOnCallFrame_parser(result):
    data = RemoteObject(result['result'])
    return data

def disable():
    command = Command('Debugger.disable', {})
    return command

def pause():
    command = Command('Debugger.pause', {})
    return command

def resume():
    command = Command('Debugger.resume', {})
    return command

def stepInto():
    command = Command('Debugger.stepInto', {})
    return command

def stepOut():
    command = Command('Debugger.stepOut', {})
    return command

def stepOver():
    command = Command('Debugger.stepOver', {})
    return command

def removeBreakpoint(breakpointId):
    params = {}
    params['breakpointId'] = breakpointId
    command = Command('Debugger.removeBreakpoint', params)
    return command

def setBreakpoint(location, condition=None):
    params = {}

    location.lineNumber = location.lineNumber-1
    params['location'] = location()

    if condition:
        params['condition'] = condition

    command = Command('Debugger.setBreakpoint', params)
    return command

def setBreakpoint_parser(result):
    data = {}
    data['breakpointId'] = BreakpointId(result['breakpointId'])
    data['actualLocation'] = Location(result['actualLocation'])
    data['actualLocation'].lineNumber = data['actualLocation'].lineNumber+1
    return data

def setScriptSource(scriptId, scriptSource):
    params = {}
    params['scriptId'] = scriptId
    params['scriptSource'] = scriptSource

    command = Command('Debugger.setScriptSource', params)
    return command

def setScriptSource_parser(result):
    data = {}
    data['callFrames'] = []
    for callFrame in result['callFrames']:
        data['callFrames'].append(CallFrame(callFrame))
    return data

def setBreakpointByUrl(lineNumber, url, urlRegex=None, columnNumber=None, condition=None):
    params = {}
    params['lineNumber'] = lineNumber-1
    params['url'] = restoreQueryString(url)

    if urlRegex:
        params['urlRegex'] = urlRegex

    if columnNumber:
        params['columnNumber'] = columnNumber
    else:
        params['columnNumber'] = 0

    if condition:
        params['condition'] = condition
    else:
        params['condition'] = ''

    command = Command('Debugger.setBreakpointByUrl', params)
    return command

def setBreakpointByUrl_parser(result):
    data = {}
    data['breakpointId'] = BreakpointId(result['breakpointId'])
    data['locations'] = []
    for location in result['locations']:
        location_found = Location(location)
        location_found.lineNumber = location_found.lineNumber + 1
        data['locations'].append(location_found)
    return data

def scriptParsed():
    notification = Notification('Debugger.scriptParsed')
    return notification

def scriptParsed_parser(params):
    url = stripQueryString(params['url'])
    return {'scriptId': ScriptId(params['scriptId']), 'url': url}

def paused():
    notification = Notification('Debugger.paused')
    return notification

def paused_parser(params):
    data = {}
    data['callFrames'] = []
    for callFrame in params['callFrames']:
        data['callFrames'].append(CallFrame(callFrame))
    data['reason'] = params['reason']
    return data

url_to_originalUrl = {}

def stripQueryString(url):
    # Some users use query strings as cache breakers
    # We don't want these in our mapping process or window titles
    # Strip on url from debuggee, restore on url to debuggee
    url_parts = url.split("/")
    url_parts[-1] = re.sub(r"\?.*$", "", url_parts[-1])
    cleanUrl = "/".join(url_parts)
    if url != cleanUrl:
        url_to_originalUrl[cleanUrl] = url
    return cleanUrl

def restoreQueryString(url):
    if url in url_to_originalUrl:
        url = url_to_originalUrl[url]
    return url

def resumed():
    notification = Notification('Debugger.resumed')
    return notification

class BreakpointId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class CallFrameId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class ScriptId(WIPObject):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __call__(self):
        return self.value

class Scope(WIPObject):
    def __init__(self, value):
        self.set_class(value, 'object', RemoteObject)
        self.set(value, 'type')

class Location(WIPObject):
    def __init__(self, value):
        self.set(value, 'columnNumber')
        self.set(value, 'lineNumber')
        self.set_class(value, 'scriptId', ScriptId)

    def __call__(self):
        obj = {}
        if self.columnNumber:
            obj['columnNumber'] = self.columnNumber
        obj['lineNumber'] = self.lineNumber
        obj['scriptId'] = self.scriptId()
        return obj

class CallFrame(WIPObject):
    def __init__(self, value):
        self.set_class(value, 'callFrameId', CallFrameId)
        self.set(value, 'functionName')
        self.set_class(value, 'location', Location)
        self.location.lineNumber = self.location.lineNumber+1
        self.scopeChain = []
        if 'scopeChain' in value:
            for scope in value['scopeChain']:
                self.scopeChain.append(Scope(scope))

    def __str__(self):
        return "%s:%d %s" % (self.location.scriptId, self.location.lineNumber, self.functionName)
<|endoftext|>