def _assert_sim_parameter( sim: __Simulation__, parameter: str, expected: Any ) -> None: """Compare a value from start.in/run.in with a reference value.""" value = sim.get_value(parameter) assert_equal( value, expected, "sim.get_value({}) = {} ≠ {}".format( parameter, _pretty_print(value), _pretty_print(expected) ), )
def test_example_cwl_simple_redirect(): command_io = _example("seqtk convert '1.bed' > '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"]) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["seqtk", "convert"]) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["outputs"][0].glob, "out") assert_equal(cwl_properties["stdout"], "out")
def test_example_cwl_simplest(): command_io = _example("convert '1.bed' '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"]) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["convert"]) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["outputs"][0].position, 2)
def neat_short_tricks() -> None: """Code from python/tutorials/basics/neat_short-tricks.py . Adapted to use existing data directories """ import pencil as pc # Obtain sim object try: import dill # noqa except ModuleNotFoundError as e: print( "\nTo use get_sim(), you need to have 'dill' installed" "\ne.g." "\n sudo apt install python3-dill" "\nor" "\n pip3 install dill", file=sys.stderr, ) raise Exception(e) sim = pc.get_sim(get_run_dir()) # Access a value from start.in / run.in _assert_sim_parameter(sim, "inituu", "gaussian-noise") assert_equal(sim.get_value("ldensity_nolog"), True) assert_equal(sim.get_value("iforce"), "helical") assert_equal(sim.get_value("nu"), 1.0) assert_equal(sim.get_varlist(), []) assert_equal(sim.get_varlist(particle=True), []) assert_equal(sim.get_varlist(pos="last10"), [])
def test_read_dim() -> None: """Read dim.dat file.""" global_dim = dim(DATA_DIR) assert_equal(global_dim.mx, 10) assert_equal(global_dim.my, 12) assert_equal(global_dim.mz, 11) assert_equal(global_dim.mvar, 5) assert_equal(global_dim.precision, "S") assert_equal(global_dim.nghostx, 3) assert_equal(global_dim.nghosty, 3) assert_equal(global_dim.nghostz, 3) assert_equal(global_dim.nprocx, 1) assert_equal(global_dim.nprocy, 1) assert_equal(global_dim.nprocz, 1) proc_dim = dim(DATA_DIR, 0) # As we don't have a Dim.__eq__() method: attributes = [ "mx", "my", "mz", "mvar", "precision", "nghostx", "nghosty", "nghostz", ] for attr in attributes: assert_equal( getattr(global_dim, attr), getattr(proc_dim, attr), "global_dim.{0} = {1} ≠ proc_dim.{0} = {2}".format( attr, getattr(global_dim, attr), getattr(proc_dim, attr) ), )
def test_read_param() -> None: """Read param.nml file.""" params = param(DATA_DIR) assert_equal(params.coord_system, "cartesian") assert_equal(params.lcollective_io, False) assert_equal(params.gamma, 1.666_666_6) assert_equal(params.kx_uu, 1.0) assert_equal(params.cs2top, 1.0) assert_equal(params.lhydro, True) assert_equal(params.ldensity, True) assert_equal(params.lentropy, True) assert_equal(params.ltemperature, False)
def test_prefixes_joined(): command_io = _example( "seqtk convert INPUT=1.bed OUTPUT=1.bam", example_outputs=["1.bam"], example_inputs=["1.bed"] ) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["seqtk", "convert"]) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["inputs"][0].prefix.prefix, "INPUT=") assert_equal(cwl_properties["inputs"][0].prefix.separated, False) assert_equal(cwl_properties["outputs"][0].glob, "$(inputs.output1)") assert_equal(cwl_properties["outputs"][0].prefix.prefix, "OUTPUT=") assert_equal(cwl_properties["outputs"][0].prefix.separated, False) assert_equal(cwl_properties["stdout"], None)
def test_prefixes_separated(): command_io = _example( "seqtk convert -i '1.bed' --output '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"] ) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["seqtk", "convert"]) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["inputs"][0].prefix.prefix, "-i") assert_equal(cwl_properties["inputs"][0].prefix.separated, True) assert_equal(cwl_properties["outputs"][0].glob, "$(inputs.output1)") assert_equal(cwl_properties["outputs"][0].prefix.prefix, "--output") assert_equal(cwl_properties["outputs"][0].prefix.separated, True) assert_equal(cwl_properties["stdout"], None)
def test_integer_parameters(): command_io = _example( "seqtk convert --size 100 -i '1.bed' --threshold 2.0 --output_type bam > '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"] ) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["seqtk", "convert"]) assert_equal(len(cwl_properties["inputs"]), 4) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["inputs"][0].type, "int") assert_equal(cwl_properties["inputs"][0].prefix.prefix, "--size") assert_equal(cwl_properties["inputs"][1].position, 2) assert_equal(cwl_properties["inputs"][1].type, "File") assert_equal(cwl_properties["inputs"][1].prefix.prefix, "-i") assert_equal(cwl_properties["inputs"][2].position, 3) assert_equal(cwl_properties["inputs"][2].type, "float") assert_equal(cwl_properties["inputs"][2].prefix.prefix, "--threshold") assert_equal(cwl_properties["inputs"][3].position, 4) assert_equal(cwl_properties["inputs"][3].type, "string") assert_equal(cwl_properties["inputs"][3].prefix.prefix, "--output_type") assert_equal(cwl_properties["outputs"][0].glob, "out") assert_equal(cwl_properties["stdout"], "out")
def test_example_cwl_argument(): command_io = _example("seqtk convert '1.bed' moo '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"]) cwl_properties = command_io.cwl_properties() assert_equal(cwl_properties["base_command"], ["seqtk", "convert"]) assert_equal(cwl_properties["inputs"][0].position, 1) assert_equal(cwl_properties["inputs"][0].prefix, None) assert_equal(cwl_properties["outputs"][0].position, 3) assert_equal(cwl_properties["outputs"][0].prefix, None) assert_equal(cwl_properties["outputs"][0].glob, "$(inputs.output1)") assert_equal(len(cwl_properties["arguments"]), 1) assert_equal(cwl_properties["arguments"][0].position, 2) assert_equal(cwl_properties["arguments"][0].value, "moo")
def test_example_already_quoted_single(): """Test example input/output transition.""" command_io = _example("convert '1.bed' '1.bam'", example_outputs=["1.bam"], example_inputs=["1.bed"]) assert_equal(command_io.cheetah_template, "convert '$input1' '$output1'")
def test_example_already_quoted(): """Test example input/output transition.""" command_io = _example('convert "1.bed" "1.bam"', example_outputs=["1.bam"], example_inputs=["1.bed"]) assert_equal(command_io.cheetah_template, 'convert "$input1" "$output1"')
def test_example_and_quotes(): """Test example input/output transition.""" command_io = _example("convert 1.bed 1.bam", example_outputs=["1.bam"], example_inputs=["1.bed"]) assert_equal(command_io.cheetah_template, 'convert "$input1" "$output1"') assert_equal(len(command_io.outputs), 1) assert_equal(len(command_io.inputs), 1)
from test_utils import assert_equal # WARM-UP # TODO: Write a function that takes a list of ints and returns only the odd ones # Example: odd_filter([1,2,3,4,5]) == [1,3,5] # Remember: a number n is odd if n % 2 == 1 def only_odds(xs): return [x for x in xs if x % 2 == 1] assert_equal([1, 3, 5], only_odds(range(6))) # TODO: Write a function that can read roman numerals # https://en.wikipedia.org/wiki/Roman_numerals # Example: roman_numerals_to_int('XIV') == 14 # Important string methods: count, replace NUMERALS = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} SPECIAL_NUMERALS = { 'IV': 4,
def test_simplest_command(): """Test very simple command.""" command_io = _example("random_fastq") assert_equal(command_io.cheetah_template, "random_fastq")
from test_utils import assert_equal # TODO: write a function that returns the intersection of two lists # (all the elements that are common in both groups) def intersect(l1, l2): xs = [] for x in l1: if x in l2: xs.append(x) return xs # TODO: Write some tests for the intersect function assert_equal([1, 2, 3], intersect([1, 2, 3, 4, 5, 6], [3, 2, 1])) # TODO: Rewrite the intersection function using list comprehension def intersect2(l1, l2): return [x for x in l1 if x in l2] # TODO: Write a function that takes an integer n and uses List Comprehension to # return a list of all the divisors of n def divisors(n): return [x for x in range(1, n + 1) if n % x == 0] assert_equal([1, 2, 3, 4, 6, 12], divisors(12))
import string from test_utils import assert_equal def map_count(xs): """ :param xs: a list of items :return: a map of item to number of times it occurs in the list """ count = {} for x in xs: count[x] = count.get(x, 0) + 1 return count assert_equal({'a': 2, 'b': 1}, map_count('aba')) def count_occurrences(items, target): """ Return how many instances of target are in the list of items. items may be any iterable a list of numbers like [1,2,3] or a string like 'abc' :param target: The item that we would like to count :param items: A collection of items """ return map_count(items)[target] # Tests for count_occurrences assert_equal(2, count_occurrences('noodle', 'o'))