Best Python code snippet using lisa_python
common.py
Source:common.py
1#!/usr/bin/env python2#=============================================================================3#4# Copyright (c) 2015-2016 Qualcomm Technologies, Inc.5# All Rights Reserved.6# Confidential and Proprietary - Qualcomm Technologies, Inc.7#8#=============================================================================9from abc import ABCMeta, abstractmethod10from util import ConverterError11from converters import code_to_message12class LayerDescriptor(object):13 def __init__(self, layer_type, layer_name, operations, output_names=None):14 """15 Defines a base class to hold information regarding a layer's parameters extracted from a set of operations16 within a tensorflow.Graph.17 :type layer_type: str18 :type layer_name: str19 :type operations: list[tensorflow.Operation]20 """21 super(LayerDescriptor, self).__init__()22 self.layer_type = layer_type23 self.layer_name = str(layer_name)24 self.child_ops = operations25 self._ignored = False26 self._output_names = output_names27 @property28 def output_names(self):29 """30 :rtype: [str]31 """32 if self._output_names is None:33 return [str(self.child_ops[-1].outputs[0].name)]34 else:35 return self._output_names36 @output_names.setter37 def output_names(self, value):38 self._output_names = value39 def get_output_names_for(self, input_tensors):40 """41 :type input_tensors: [tensorflow.Tensor]42 :rtype: [str]43 """44 output_tensors = [t for o in self.child_ops for t in o.outputs]45 return [str(t.name) for t in input_tensors if t in output_tensors]46 @classmethod47 def none(cls):48 """49 Returns a LayerDescriptor to represent an invalid layer. This is used50 by implementations of LayerBuilder.build_layer_descriptor(..) to convey51 not being able to extract a layer from a set of graph operations.52 :rtype: LayerDescriptor.53 """54 return LayerDescriptor("none", "none", [])55 def set_ignored(self, ignored):56 """57 Sets the descriptor as ignored which will cause58 the conversion process to not build a layer from59 this descriptor.60 :type ignored: bool61 :return:62 """63 self._ignored = ignored64 @property65 def is_ignored(self):66 return self._ignored67 def is_output_op(self, op):68 if self._output_names is not None:69 return op.outputs[0].name in self._output_names70 else:71 return self.child_ops[-1] == op72 def is_input_op(self, op):73 return op in self.child_ops74 def __eq__(self, other):75 result = False76 if isinstance(other, self.__class__):77 result = (self.layer_name == other.layer_name)78 result = result and (self.child_ops == other.child_ops)79 return result80 def __hash__(self):81 return hash(tuple(self.layer_name, ) + tuple(self.child_ops))82class InputLayerDescriptor(LayerDescriptor):83 def __init__(self, name, nodes):84 super(InputLayerDescriptor, self).__init__('Input', name, nodes)85class LayerResolver(object):86 """87 Defines an API for each type of layer to extend and implement it's layer parameter resolution.88 """89 __metaclass__ = ABCMeta90 @abstractmethod91 def resolve_layer(self, graph_matcher, graph_helper):92 """93 :type graph_matcher: GraphMatcher94 :type graph_helper: GraphHelper95 :rtype: list(LayerDescriptor)96 """97 pass98 def is_final_resolution(self):99 return False100class LayerBuilder(object):101 """102 Defines an API for each type of layer to extend and implement how to build the layer in the target format.103 """104 __metaclass__ = ABCMeta105 @abstractmethod106 def build_layer(self, converter_context, descriptor, input_descriptors, output_descriptors):107 """108 Creates a layer from the specified LayerDescriptor and returns an int representing the layer unique id.109 :type converter_context: converters.tensorflow.converter.ConverterContext110 :type descriptor: converters.tensorflow.common.LayerDescriptor111 :type input_descriptors: [converters.tensorflow.common.LayerDescriptor]112 :type output_descriptors: [converters.tensorflow.common.LayerDescriptor]113 :rtype: int114 """115 pass116 def transform_layer(self, converter_context, descriptor, input_descriptors, output_descriptors):117 """118 Allows builders to fuse or skip layers.119 :type converter_context: converters.tensorflow.converter.ConverterContext120 :type descriptor: converters.tensorflow.common.LayerDescriptor121 :type input_descriptors: [converters.tensorflow.common.LayerDescriptor]122 :type output_descriptors: [converters.tensorflow.common.LayerDescriptor]123 """124 pass125 @classmethod126 def get_input_name(cls, converter_context, descriptor, input_descriptors):127 """128 :type converter_context: converters.tensorflow.converter.ConverterContext129 :type input_descriptors: [LayerDescriptor]130 :type descriptor: LayerDescriptor131 :rtype: str132 """133 if len(input_descriptors) > 1:134 raise ConverterError(code_to_message.get_message('ERROR_TF_LAYER_INPUT_COUNT_ERROR')(135 input_descriptors[0].layer_type, 1, len(input_descriptors)136 ))137 input_names = cls.get_input_names(converter_context, descriptor, input_descriptors)138 if len(input_names) == 0:139 raise ConverterError(code_to_message.get_message('ERROR_TF_LAYER_NO_INPUT_FOUND')(140 descriptor.layer_type, descriptor.layer_name))141 if len(input_names) > 1:142 raise ConverterError(code_to_message.get_message('ERROR_TF_LAYER_INPUT_COUNT_ERROR')(143 input_descriptors[0].layer_type, 1, len(input_descriptors)144 ))145 return input_names[0]146 @classmethod147 def get_input_names(cls, converter_context, descriptor, input_descriptors):148 """149 :type converter_context: converters.tensorflow.converter.ConverterContext150 :type input_descriptors: [LayerDescriptor]151 :type descriptor: LayerDescriptor152 :rtype: str153 """154 input_names = []155 for d in input_descriptors:156 input_tensors = converter_context.get_output_tensors_between(d, descriptor)157 input_names.extend(d.get_output_names_for(input_tensors))158 if len(input_names) == 0:159 raise ConverterError(code_to_message.get_message('ERROR_TF_LAYER_NO_INPUT_FOUND')(160 descriptor.layer_type, descriptor.layer_name))...
wrappers.py
Source:wrappers.py
1# -*- coding: utf-8 -*-2# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-3# vi: set ft=python sts=4 ts=4 sw=4 et:4"""5# changing to temporary directories6 >>> tmp = getfixture('tmpdir')7 >>> old = tmp.chdir()8"""9from ... import logging10from ..base import (11 traits,12 DynamicTraitedSpec,13 Undefined,14 isdefined,15 BaseInterfaceInputSpec,16)17from ..io import IOBase, add_traits18from ...utils.filemanip import ensure_list19from ...utils.functions import getsource, create_function_from_source20iflogger = logging.getLogger("nipype.interface")21class FunctionInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):22 function_str = traits.Str(mandatory=True, desc="code for function")23class Function(IOBase):24 """Runs arbitrary function as an interface25 Examples26 --------27 >>> func = 'def func(arg1, arg2=5): return arg1 + arg2'28 >>> fi = Function(input_names=['arg1', 'arg2'], output_names=['out'])29 >>> fi.inputs.function_str = func30 >>> res = fi.run(arg1=1)31 >>> res.outputs.out32 633 """34 input_spec = FunctionInputSpec35 output_spec = DynamicTraitedSpec36 def __init__(37 self,38 input_names=None,39 output_names="out",40 function=None,41 imports=None,42 **inputs43 ):44 """45 Parameters46 ----------47 input_names: single str or list or None48 names corresponding to function inputs49 if ``None``, derive input names from function argument names50 output_names: single str or list51 names corresponding to function outputs (default: 'out').52 if list of length > 1, has to match the number of outputs53 function : callable54 callable python object. must be able to execute in an55 isolated namespace (possibly in concert with the ``imports``56 parameter)57 imports : list of strings58 list of import statements that allow the function to execute59 in an otherwise empty namespace60 """61 super(Function, self).__init__(**inputs)62 if function:63 if hasattr(function, "__call__"):64 try:65 self.inputs.function_str = getsource(function)66 except IOError:67 raise Exception(68 "Interface Function does not accept "69 "function objects defined interactively "70 "in a python session"71 )72 else:73 if input_names is None:74 fninfo = function.__code__75 elif isinstance(function, (str, bytes)):76 self.inputs.function_str = function77 if input_names is None:78 fninfo = create_function_from_source(function, imports).__code__79 else:80 raise Exception("Unknown type of function")81 if input_names is None:82 input_names = fninfo.co_varnames[: fninfo.co_argcount]83 self.inputs.on_trait_change(self._set_function_string, "function_str")84 self._input_names = ensure_list(input_names)85 self._output_names = ensure_list(output_names)86 add_traits(self.inputs, [name for name in self._input_names])87 self.imports = imports88 self._out = {}89 for name in self._output_names:90 self._out[name] = None91 def _set_function_string(self, obj, name, old, new):92 if name == "function_str":93 if hasattr(new, "__call__"):94 function_source = getsource(new)95 fninfo = new.__code__96 elif isinstance(new, (str, bytes)):97 function_source = new98 fninfo = create_function_from_source(new, self.imports).__code__99 self.inputs.trait_set(100 trait_change_notify=False, **{"%s" % name: function_source}101 )102 # Update input traits103 input_names = fninfo.co_varnames[: fninfo.co_argcount]104 new_names = set(input_names) - set(self._input_names)105 add_traits(self.inputs, list(new_names))106 self._input_names.extend(new_names)107 def _add_output_traits(self, base):108 undefined_traits = {}109 for key in self._output_names:110 base.add_trait(key, traits.Any)111 undefined_traits[key] = Undefined112 base.trait_set(trait_change_notify=False, **undefined_traits)113 return base114 def _run_interface(self, runtime):115 # Create function handle116 function_handle = create_function_from_source(117 self.inputs.function_str, self.imports118 )119 # Get function args120 args = {}121 for name in self._input_names:122 value = getattr(self.inputs, name)123 if isdefined(value):124 args[name] = value125 out = function_handle(**args)126 if len(self._output_names) == 1:127 self._out[self._output_names[0]] = out128 else:129 if isinstance(out, tuple) and (len(out) != len(self._output_names)):130 raise RuntimeError("Mismatch in number of expected outputs")131 else:132 for idx, name in enumerate(self._output_names):133 self._out[name] = out[idx]134 return runtime135 def _list_outputs(self):136 outputs = self._outputs().get()137 for key in self._output_names:138 outputs[key] = self._out[key]...
the_pipe.py
Source:the_pipe.py
...11 self.Y_names = Y_names12 self.output_spec = output_spec13 self.the_pipe = []14 self.verbose = verbose15 def _output_names(self, j):16 return list(self.output_spec[j].keys())17 def _determine_names(self, j, blade_runner):18 if isinstance(self.X_names[j], int):19 if self.X_names[j] == 0:20 self.X_names[j] = [x for x in blade_runner.train.columns.values if x not in self._output_names(j)]21 else:22 self.X_names[j] = list(self.output_spec[(j + self.X_names[j])].keys())23 if self.Y_names[j] is None:24 raise Exception("Y_names anyway should be specified as non-None value, set something pls")25 def _fit_predict(self, j, blade_runner):26 self.the_pipe[j].fit(blade_runner.train[self.X_names[j]].values,27 blade_runner.train[self.Y_names[j]].values)28 if self.output_spec[j] is None:29 cols = numpy.array(self.X_names[j])[self.the_pipe[j].support].tolist()30 self.output_spec[j] = {x: blade_runner.train[x].dtype.name for x in cols}31 blade_runner.train[self._output_names(j)] = self.the_pipe[j].predict(blade_runner.train[self.X_names[j]].values)32 return blade_runner33 def _predict(self, j, blade_runner, on='train'):34 if on == 'train':35 blade_runner.train[self._output_names(j)] = self.the_pipe[j].predict(blade_runner.train[self.X_names[j]].values)36 else:37 blade_runner.test[self._output_names(j)] = self.the_pipe[j].predict(blade_runner.test[self.X_names[j]].values)38 return blade_runner39 def _inverse(self, j, blade_runner, on='train'):40 if on == 'train':41 blade_runner.train[self._output_names(j)] = self.the_pipe[j].inverse(blade_runner.train[self.X_names[j]].values)42 else:43 blade_runner.test[self._output_names(j)] = self.the_pipe[j].inverse(blade_runner.test[self.X_names[j]].values)44 return blade_runner45 def fit(self):46 blade_runner = self.data.copy()47 for j in range(len(self.items)):48 # determine names49 self._determine_names(j, blade_runner)50 # append item51 self.the_pipe.append(self.items[j](**self.items_args[j]))52 # fit and predict53 blade_runner = self._fit_predict(j, blade_runner)54 # print current state55 if self.verbose:56 print(self.the_pipe[j])57 def infer(self, on='train'):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!