Ejemplo n.º 1
0
 def _process_placeholder(self, func=None, params=None, name=None):
     """
     Special treatment for placeholders which may be data dependent.
     """
     if name not in self._placeholderdict:
         if self.data is not None and name in self.data:
             params += ', data=self.data[name]'
         else:
             raise MissingDataError('There is no data called %s in the DataSet for this AntGraph.' % name)
         function_call = func + '(' + params + ')'
         try:
             self._placeholderdict[name] = eval(function_call, NODE_GLOBALS, locals())
             if self.develop:
                 heading = 'Node %s: %r' % (name, self.placeholderdict[name])
                 print(termcolor.colored(heading, 'green'))
                 print('\tFunction Call: %s\n\tInput Data: %r' %
                       (function_call, self.data[name]))
             return self._placeholderdict[name]
         except NameError as e:
             traceback.print_exc()
             print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
             raise NameError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                             (function_call, name, self.data[name], name))
         except TypeError as e:
             traceback.print_exc()
             print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
             raise TypeError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                             (function_call, name, self.data[name], name))
         except ValueError as e:
             traceback.print_exc()
             print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
             raise ValueError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                             (function_call, name, self.data[name], name))
     return self._placeholderdict[name]
Ejemplo n.º 2
0
 def showmore(self):
     """
     :method: Pretty print data attributes, and data.
     """
     datasets = [
         s for s in dir(self) if not s.startswith('__') and not s == 'show'
         and not s == 'showmore'
     ]
     for dataset in datasets:
         print tc.colored(dataset + ':', 'yellow')
         getattr(self, dataset).showmore()
Ejemplo n.º 3
0
 def _process_lookup(self, func=None, params=None, name=None):
     """
     Special treatment for lookup function which may be data dependent.
     """
     paramlist = params.split(',')
     dataname = None
     for p in paramlist:
         if p.startswith('dataname='):
             dataname = p.split('=')[1]
     if dataname is not None:
         dataname = dataname.strip("'")
     if dataname in self._placeholderdict:
         params += ', makeplace=False, indices=self._placeholderdict[dataname], ' \
                   'data=self.data[dataname]'
     elif dataname in self.data:
         params += ', data=self.data[dataname]'
     else:
         function_call = func + '(' + params + ')'
         raise ProcessLookupError('"%s" is not a key in the data dictionary for this AntGraph. '
                                  'Need to provide a valid dataname argument for lookup without tensor input.'
                                  ' \nCall: %s' % (dataname, function_call))
     function_call = func + '(' + params + ')'
     try:
         vals = eval(function_call, NODE_GLOBALS, locals())
         self._tensordict[name] = vals[0]
         self._tensordict[name + '_weights'] = vals[1]
         self._placeholderdict[dataname] = vals[2]
         if self.develop:
             heading = 'Node %s: %s' % (name, self.tensordict[name])
             print(termcolor.colored(heading, 'green'))
             print('\tFunction Call: %s\n\tPlaceholder: %s\n\tWeights: %s\n\tInput Data: %r\n\t' %
                   (function_call, ph_rep(vals[1]), vals[2], self.data[dataname]))
         return self._tensordict[name]
     except NameError as e:
         traceback.print_exc()
         print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
         raise NameError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                         (function_call, name, self.data[dataname], dataname))
     except TypeError as e:
         traceback.print_exc()
         print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
         raise TypeError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                         (function_call, name, self.data[dataname], dataname))
     except ValueError as e:
         traceback.print_exc()
         print(termcolor.colored("==========================Original Handled Exception Above============================", 'red'))
         raise ValueError('\nFunction Call: %s\nname=%r\ndata: %r hash=%s)' %
                         (function_call, name, self.data[dataname], dataname))
Ejemplo n.º 4
0
 def _make_tensor(self, spec, intensors=None):
     '''
     Parses a line from config file to make a tensor.
     '''
     spec = spec.strip().split()
     name = spec[0].strip(self.marker[0])
     double_comma = re.compile(',,')  # fix for baffling error
     function_spec = ''.join(spec[1:len(spec)])
     function_params = function_spec.split('(')
     func = function_params[0]
     if name in self._tensordict:
         return self._tensordict[name]
     elif len(spec) > 1:
         params = function_params[1].strip(')')
         if intensors is not None:
             params = double_comma.sub(',', 'intensors,' + params + ',name=name')
             params = params.strip(',')
         else:
             params = double_comma.sub(',', params + ',name=name')
             params = params.strip(',')
         if func == 'placeholder':
             return self._process_placeholder(func, params, name)
         elif func == 'lookup':
             return self._process_lookup(func, params, name)
         else:
             function_call = func + '(' + params + ')'
             try:
                 self._tensordict[name] = eval(function_call, NODE_GLOBALS, locals())
                 if self.develop:
                     heading = 'Node %s: %s' % (name, self.tensordict[name])
                     print(termcolor.colored(heading, 'green'))
                     print('\tFunction Call: %s\n\tTensor Inputs:\n\t\t' %
                           (function_call), end="")
                     if type(intensors) is list:
                         print(*intensors, sep='\n\t\t')
                     else:
                         print(intensors)
             except NameError as e:
                 traceback.print_exc()
                 print(termcolor.colored("==========================Original Handled Exception Above | "
                                         "Input Tensors Below============================", 'red'))
                 print('Input Tensors:\n\t', end="")
                 if type(intensors) is list:
                     print(*intensors, sep='\n\t')
                 else:
                     print(intensors)
                 raise NameError('\nFunction Call: %s\n intensors: %r' % (function_call, intensors))
             except TypeError as e:
                 traceback.print_exc()
                 print(termcolor.colored("==========================Original Handled Exception Above |"
                                         "Input Tensors Below============================", 'red'))
                 print('Input Tensors:\n\t', end="")
                 if type(intensors) is list:
                     print(*intensors, sep='\n\t')
                 else:
                     print(intensors)
                 raise TypeError('\nFunction Call: %s\n intensors: %r' % (function_call, intensors))
             except ValueError as e:
                 traceback.print_exc()
                 print(termcolor.colored("==========================Original Handled Exception Above |"
                                         "Input Tensors Below============================", 'red'))
                 print('Input Tensors:\n\t', end="")
                 if type(intensors) is list:
                     print(*intensors, sep='\n\t')
                 else:
                     print(intensors)
                 raise ValueError('\nFunction Call: %s\n intensors: %r' % (function_call, intensors))
         return self._tensordict[name]
     else:
         raise MissingTensorError('Name %s: from config file is not in the tensor or placeholder '
                                  'dictionary so it must have a function call.' % name)