Skip to content

Commit

Permalink
transpose()
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Jun 28, 2019
1 parent 82f5f8d commit a1e18a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
18 changes: 16 additions & 2 deletions WDL/StdLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,22 @@ def infer_type(self, expr: E.Apply) -> T.Base:
return expr.arguments[0].type

def _call_eager(self, expr: E.Apply, arguments: List[V.Base]) -> V.Base:
raise NotImplementedError()

ty = self.infer_type(expr)
assert isinstance(ty, T.Array) and isinstance(ty.item_type, T.Array)
mat = arguments[0].coerce(ty)
assert isinstance(mat, V.Array)
n = None
ans = []
for row in mat.value:
assert isinstance(row, V.Array)
if n is None:
n = len(row.value)
ans = [V.Array(ty.item_type, []) for _ in row.value]
if len(row.value) != n:
raise Error.EvalError(expr, "transpose(): ragged input matrix")
for i in range(len(row.value)):
ans[i].value.append(row.value[i])
return V.Array(ty, ans)

class _Range(EagerFunction):
# int -> int array
Expand Down
22 changes: 22 additions & 0 deletions tests/test_5stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,25 @@ def test_write(self):
with open(outputs["o_json"]) as infile:
self.assertEqual(json.load(infile), {"key1": "value1", "key2": "value2"})
self.assertEqual(outputs["o_tsv"], [["one", "two", "three"], ["un", "deux", "trois"]])

def test_transpose(self):
outputs = self._test_task(R"""
version 1.0
task hello {
command {}
output {
Array[Array[Int]] mat = transpose([[0, 1, 2], [3, 4, 5]])
}
}
""")
self.assertEqual(outputs["mat"], [[0, 3], [1, 4], [2, 5]])

outputs = self._test_task(R"""
version 1.0
task hello {
command {}
output {
Array[Array[Int]] mat = transpose([[0, 1, 2], [3, 4, 5], []])
}
}
""", expected_exception=WDL.Error.EvalError)

0 comments on commit a1e18a1

Please sign in to comment.