Skip to content

Commit 671b346

Browse files
committed
add CWLPNGPlot type
1 parent cb64a53 commit 671b346

File tree

6 files changed

+116
-40
lines changed

6 files changed

+116
-40
lines changed

examples/intro.ipynb

Lines changed: 40 additions & 31 deletions
Large diffs are not rendered by default.

ipython2cwl/cwltoolextractor.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from nbformat.notebooknode import NotebookNode # type: ignore
1616

1717
from .iotypes import CWLFilePathInput, CWLBooleanInput, CWLIntInput, CWLStringInput, CWLFilePathOutput, \
18-
CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable
18+
CWLDumpableFile, CWLDumpableBinaryFile, CWLDumpable, CWLPNGPlot
1919
from .requirements_manager import RequirementsManager
2020

2121
with open(os.sep.join([os.path.abspath(os.path.dirname(__file__)), 'templates', 'template.dockerfile'])) as f:
@@ -61,9 +61,16 @@ class AnnotatedVariablesExtractor(ast.NodeTransformer):
6161
}
6262

6363
dumpable_mapper = {
64-
(CWLDumpableFile.__name__,): "with open('{var_name}', 'w') as f:\n\tf.write({var_name})",
65-
(CWLDumpableBinaryFile.__name__,): "with open('{var_name}', 'wb') as f:\n\tf.write({var_name})",
64+
(CWLDumpableFile.__name__,): (
65+
"with open('{var_name}', 'w') as f:\n\tf.write({var_name})", lambda node: node.target.id
66+
),
67+
(CWLDumpableBinaryFile.__name__,): (
68+
"with open('{var_name}', 'wb') as f:\n\tf.write({var_name})", lambda node: node.target.id
69+
),
6670
(CWLDumpable.__name__, CWLDumpable.dump.__name__): None,
71+
(CWLPNGPlot.__name__,): (
72+
'import matplotlib.pyplot as plt\nplt.savefig("{var_name}.png")',
73+
lambda node: str(node.target.id) + '.png'),
6774
}
6875

6976
def __init__(self, *args, **kwargs):
@@ -103,12 +110,11 @@ def _visit_input_ann_assign(self, node, annotation):
103110
return None
104111

105112
def _visit_default_dumper(self, node, dumper):
106-
dump_tree = ast.parse(dumper.format(var_name=node.target.id))
107-
self.to_dump.append(dump_tree.body)
113+
dump_tree = ast.parse(dumper[0].format(var_name=node.target.id))
108114
self.extracted_variables.append(_VariableNameTypePair(
109-
node.target.id, None, None, None, False, True, node.target.id)
115+
node.target.id, None, None, None, False, True, dumper[1](node))
110116
)
111-
return self.conv_AnnAssign_to_Assign(node)
117+
return [self.conv_AnnAssign_to_Assign(node), *dump_tree.body]
112118

113119
def _visit_user_defined_dumper(self, node):
114120
load_ctx = ast.Load()

ipython2cwl/iotypes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,22 @@ class CWLDumpableBinaryFile(CWLDumpable):
158158
and at the CWL, the data, will be mapped as a output.
159159
"""
160160
pass
161+
162+
163+
class CWLPNGPlot(CWLDumpable):
164+
"""Use that annotation to define that after the assigment of that variable the plt.savefig() should
165+
be called
166+
167+
>>> import matplotlib.pyplot as plt
168+
>>> data = [1,2,3]
169+
>>> new_data: 'CWLPNGPlot' = plt.plot(data)
170+
171+
the converter will tranform these lines to
172+
173+
>>> import matplotlib.pyplot as plt
174+
>>> data = [1,2,3]
175+
>>> new_data: 'CWLPNGPlot' = plt.plot(data)
176+
>>> plt.savefig('new_data.png')
177+
178+
"""
179+
pass

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ coveralls>=2.0.0
44
virtualenv>=3.1.0
55
gitpython>=3.1.3
66
docker>=4.2.1
7-
git+https://github.com/giannisdoukas/cwltool.git#egg=cwltool
7+
cwltool==3.0.20200706173533
88
pandas==1.0.5
99
mypy

tests/test_cwltoolextractor.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,45 @@ def test_AnnotatedIPython2CWLToolConverter_custom_dumpables(self):
474474
os.remove(f)
475475
except FileNotFoundError:
476476
pass
477+
478+
def test_AnnotatedIPython2CWLToolConverter_CWLPNGPlot(self):
479+
code = os.linesep.join([
480+
"import matplotlib.pyplot as plt",
481+
"new_data: 'CWLPNGPlot' = plt.plot([1,2,3,4])",
482+
])
483+
converter = AnnotatedIPython2CWLToolConverter(code)
484+
new_script = converter._wrap_script_to_method(
485+
converter._tree,
486+
converter._variables
487+
)
488+
try:
489+
os.remove('new_data.png')
490+
except FileNotFoundError:
491+
pass
492+
exec(new_script)
493+
locals()['main']()
494+
self.assertTrue(os.path.isfile('new_data.png'))
495+
os.remove('new_data.png')
496+
497+
tool = converter.cwl_command_line_tool()
498+
self.assertDictEqual(
499+
{
500+
'cwlVersion': "v1.1",
501+
'class': 'CommandLineTool',
502+
'baseCommand': 'notebookTool',
503+
'hints': {
504+
'DockerRequirement': {'dockerImageId': 'jn2cwl:latest'}
505+
},
506+
'arguments': ['--'],
507+
'inputs': {},
508+
'outputs': {
509+
'new_data': {
510+
'type': 'File',
511+
'outputBinding': {
512+
'glob': 'new_data.png'
513+
}
514+
}
515+
},
516+
},
517+
tool
518+
)

tests/test_system_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def test_repo2cwl(self):
2929
self.assertListEqual(['example1.cwl'], [f for f in os.listdir(output_dir) if not f.startswith('.')])
3030

3131
with open(os.path.join(output_dir, 'example1.cwl')) as f:
32-
print(20 * '=')
3332
print('workflow file')
33+
print(20 * '=')
3434
print(f.read())
3535
print(20 * '=')
3636

0 commit comments

Comments
 (0)