diff keras_deep_learning.py @ 1:cc49634df38f draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ba6a47bdf76bbf4cb276206ac1a8cbf61332fd16"
author bgruening
date Fri, 13 Sep 2019 12:08:44 -0400
parents 68aaa903052a
children 2b8406e74f9e
line wrap: on
line diff
--- a/keras_deep_learning.py	Fri Aug 09 07:09:06 2019 -0400
+++ b/keras_deep_learning.py	Fri Sep 13 12:08:44 2019 -0400
@@ -8,7 +8,10 @@
 
 from ast import literal_eval
 from keras.models import Sequential, Model
-from galaxy_ml.utils import try_get_attr, get_search_params
+from galaxy_ml.utils import try_get_attr, get_search_params, SafeEval
+
+
+safe_eval = SafeEval()
 
 
 def _handle_shape(literal):
@@ -100,13 +103,14 @@
         if key in ['input_shape', 'noise_shape', 'shape', 'batch_shape',
                    'target_shape', 'dims', 'kernel_size', 'strides',
                    'dilation_rate', 'output_padding', 'cropping', 'size',
-                   'padding', 'pool_size', 'axis', 'shared_axes']:
+                   'padding', 'pool_size', 'axis', 'shared_axes'] \
+                and isinstance(value, str):
             params[key] = _handle_shape(value)
 
-        elif key.endswith('_regularizer'):
+        elif key.endswith('_regularizer') and isinstance(value, dict):
             params[key] = _handle_regularizer(value)
 
-        elif key.endswith('_constraint'):
+        elif key.endswith('_constraint') and isinstance(value, dict):
             params[key] = _handle_constraint(value)
 
         elif key == 'function':  # No support for lambda/function eval
@@ -129,12 +133,15 @@
         options = layer['layer_selection']
         layer_type = options.pop('layer_type')
         klass = getattr(keras.layers, layer_type)
-        other_options = options.pop('layer_options', {})
-        options.update(other_options)
+        kwargs = options.pop('kwargs', '')
 
         # parameters needs special care
         options = _handle_layer_parameters(options)
 
+        if kwargs:
+            kwargs = safe_eval('dict(' + kwargs + ')')
+            options.update(kwargs)
+
         # add input_shape to the first layer only
         if not getattr(model, '_layers') and input_shape is not None:
             options['input_shape'] = input_shape
@@ -158,11 +165,15 @@
         layer_type = options.pop('layer_type')
         klass = getattr(keras.layers, layer_type)
         inbound_nodes = options.pop('inbound_nodes', None)
-        other_options = options.pop('layer_options', {})
-        options.update(other_options)
+        kwargs = options.pop('kwargs', '')
 
         # parameters needs special care
         options = _handle_layer_parameters(options)
+
+        if kwargs:
+            kwargs = safe_eval('dict(' + kwargs + ')')
+            options.update(kwargs)
+
         # merge layers
         if 'merging_layers' in options:
             idxs = literal_eval(options.pop('merging_layers'))