How to use skip_function method in Test_junkie

Best Python code snippet using test_junkie

code_object.py

Source:code_object.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2###############################################################################3# This file is part of metalibm (https://github.com/kalray/metalibm)4###############################################################################5# MIT License6#7# Copyright (c) 2018 Kalray8#9# Permission is hereby granted, free of charge, to any person obtaining a copy10# of this software and associated documentation files (the "Software"), to deal11# in the Software without restriction, including without limitation the rights12# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell13# copies of the Software, and to permit persons to whom the Software is14# furnished to do so, subject to the following conditions:15#16# The above copyright notice and this permission notice shall be included in all17# copies or substantial portions of the Software.18#19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR20# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,21# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE22# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER23# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,24# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE25# SOFTWARE.26###############################################################################27# created: Dec 24th, 201328# last-modified: Mar 7th, 201829#30# author(s): Nicolas Brunie (nicolas.brunie@kalray.eu)31###############################################################################32import re33import subprocess34import sys35import pdb36import sollya37from ..core.ml_operations import Variable38from ..core.ml_hdl_operations import Signal39from .code_constant import (40 C_Code, Gappa_Code, LLVM_IR_Code41)42from ..core.ml_formats import ML_GlobalRoundMode, ML_Fixed_Format, ML_FP_Format, FunctionFormat43from .code_configuration import CodeConfiguration44class DataLayout(object):45 def __init__(self):46 pass47from collections import OrderedDict48class SymbolTable(object):49 def __init__(self, uniquifier=""):50 # using an ordered ditcionnary to ensure processing order51 # during code generation52 self.table = OrderedDict()53 self.reverse_map = {}54 self.prefix_index = {}55 self.uniquifier = uniquifier56 def is_free_name(self, name):57 return not name in self.table58 def uniquify(self, name):59 return self.uniquifier + name60 def is_empty(self):61 return len(self.table) == 062 def get_free_name(self, var_type, prefix = "sttmp", update_index = True):63 _prefix = self.uniquify(prefix)64 if self.is_free_name(_prefix):65 self.prefix_index[_prefix] = 066 return _prefix67 else:68 new_index = 069 if _prefix in self.prefix_index:70 new_index = self.prefix_index[_prefix] + 171 while not self.is_free_name("%s%d" % (_prefix, new_index)):72 new_index += 173 if update_index:74 self.prefix_index[_prefix] = new_index75 return "%s%d" % (_prefix, new_index)76 def get_values(self):77 return self.table.values()78 def has_definition(self, symbol_object):79 if symbol_object in self.reverse_map:80 return self.reverse_map[symbol_object]81 else:82 return None83 #for key in self.table:84 # if symbol_object is self.table[key]: return key85 #return None86 def declare_symbol(self, name, symbol_object):87 self.table[name] = symbol_object88 self.reverse_map[symbol_object] = name89 def generate_declaration(self, code_generator):90 code_object = ""91 for symbol in self.table:92 symbol_object = self.table[symbol]93 code_object += code_generator.generate_declaration(symbol, symbol_object)94 return code_object95 def generate_initialization(self, code_generator):96 """ generate symbol initialization, only necessary97 if symbols require a specific initialization procedure98 after declaration (e.g. mpfr_t variable) """99 code_object = ""100 for symbol in self.table:101 symbol_object = self.table[symbol]102 code_object += code_generator.generate_initialization(symbol, symbol_object)103 return code_object104class MultiSymbolTable(object):105 """ symbol table object """106 class ConstantSymbol: pass107 class FunctionSymbol: pass108 class ComponentSymbol: pass109 class EntitySymbol: pass110 class VariableSymbol: pass111 class SignalSymbol: pass112 class ProtectedSymbol: pass113 class TableSymbol: pass114 class LabelSymbol: pass115 def get_shared_table(self, symbol_tag, shared_tables):116 if symbol_tag in shared_tables: return shared_tables[symbol_tag]117 else: return SymbolTable(uniquifier = self.uniquifier)118 def __init__(self, shared_tables = None, parent_tables = None, uniquifier = ""):119 """ symbol table initialization120 shared_tables is a map of pre-defined tables shared with other parent block121 (and not created within this block)122 parent_tables is a list of pre-existing tables, which are used when123 checking whether a name is free or not124 """125 self.uniquifier = uniquifier126 self.default_exclusion_list = [MultiSymbolTable.ProtectedSymbol]127 shared_tables = shared_tables if shared_tables else {}128 parent_tables = parent_tables if parent_tables else []129 self.constant_table = self.get_shared_table(MultiSymbolTable.ConstantSymbol, shared_tables)130 self.function_table = self.get_shared_table(MultiSymbolTable.FunctionSymbol, shared_tables)131 self.variable_table = self.get_shared_table(MultiSymbolTable.VariableSymbol, shared_tables)132 self.signal_table = self.get_shared_table(MultiSymbolTable.SignalSymbol, shared_tables)133 self.protected_table = self.get_shared_table(MultiSymbolTable.ProtectedSymbol, shared_tables)134 self.table_table = self.get_shared_table(MultiSymbolTable.TableSymbol, shared_tables)135 self.component_table = self.get_shared_table(MultiSymbolTable.ComponentSymbol, shared_tables)136 self.entity_table = self.get_shared_table(MultiSymbolTable.EntitySymbol, shared_tables)137 self.label_table = self.get_shared_table(MultiSymbolTable.LabelSymbol, shared_tables)138 self.parent_tables = parent_tables139 # initializing through a list of paire (key, value) to ensure140 # known ordering141 self.table_list = OrderedDict([142 (MultiSymbolTable.ConstantSymbol, self.constant_table),143 (MultiSymbolTable.FunctionSymbol, self.function_table),144 (MultiSymbolTable.VariableSymbol, self.variable_table),145 (MultiSymbolTable.SignalSymbol, self.signal_table),146 (MultiSymbolTable.ProtectedSymbol, self.protected_table),147 (MultiSymbolTable.TableSymbol, self.table_table),148 (MultiSymbolTable.ComponentSymbol, self.component_table),149 (MultiSymbolTable.EntitySymbol, self.entity_table),150 (MultiSymbolTable.LabelSymbol, self.label_table),151 ])152 self.prefix_index = {}153 def is_empty(self):154 for table_tag in self.table_list:155 if not self.table_list[table_tag].is_empty(): return False156 return True157 def table_has_definition(self, table_object):158 """ search for a previous definition of ML_Table <table_object>159 returns the table index if found, else None """160 table_key = self.table_table.has_definition(table_object)161 if table_key != None:162 return table_key163 for table in self.parent_tables:164 table_name = table.table_has_definition(table_object)165 if table_name != None: return table_name166 return None167 def get_table(self, symbol):168 return self.table_list[symbol]169 def get_parent_tables(self):170 return self.parent_tables171 def get_extended_dependency_table(self):172 return self.parent_tables + [self]173 def is_free_name(self, name):174 for table_tag in self.table_list:175 if not self.table_list[table_tag].is_free_name(name): return False176 for table in self.parent_tables:177 if not table.is_free_name(name): return False178 return True179 def get_free_name(self, var_type, prefix = "mstmp"):180 if self.is_free_name(prefix):181 self.prefix_index[prefix] = 0182 return prefix183 else:184 new_index = 0185 if prefix in self.prefix_index:186 new_index = self.prefix_index[prefix] + 1187 while not self.is_free_name("%s%d" % (prefix, new_index)):188 new_index += 1189 self.prefix_index[prefix] = new_index190 return "%s%d" % (prefix, new_index)191 ## return a free name for a constant192 def get_free_cst_name(self, var_type, prefix = "cst"):193 cst_free_name = self.constant_table.get_free_name(var_type, prefix, update_index = True)194 while not self.is_free_name(cst_free_name):195 cst_free_name = self.constant_table.get_free_name(var_type, prefix, update_index = True)196 return cst_free_name197 def declare_symbol_name(self, symbol_name, symbol_object, symbol_type):198 """ Generic function to declare a symbol of a specified type """199 self.table_list[symbol_type].declare_symbol(symbol_name, symbol_object)200 def declare_function_name(self, function_name, function_object):201 self.function_table.declare_symbol(function_name, function_object)202 def declare_component_name(self, component_name, component_object):203 self.component_table.declare_symbol(component_name, component_object)204 self.entity_table.declare_symbol(component_name, component_object)205 def get_entity_list(self):206 """ return a list of components registered in this table """207 return self.entity_table.get_values()208 def declare_protected_symbol(self, name, node):209 self.protected_table.declare_symbol(name, node)210 def declare_var_name(self, var_name, var_object):211 self.variable_table.declare_symbol(var_name, var_object)212 def declare_signal_name(self, signal_name, signal_object):213 self.signal_table.declare_symbol(signal_name, signal_object)214 def has_signal_definition(self, signal_object):215 return self.signal_table.has_definition(signal_object)216 def declare_cst_name(self, cst_name, cst_object):217 self.constant_table.declare_symbol(cst_name, cst_object)218 def declare_table_name(self, table_name, table_object):219 self.table_table.declare_symbol(table_name, table_object)220 def generate_declarations(self, code_generator, exclusion_list = []):221 code_object = ""222 for table_tag in self.table_list:223 if table_tag in exclusion_list + self.default_exclusion_list:224 continue225 code_object += self.table_list[table_tag].generate_declaration(code_generator)226 return code_object227 def generate_initializations(self, code_generator, init_required_list = []):228 code_object = ""229 for table_tag in init_required_list:230 code_object += self.table_list[table_tag].generate_initialization(code_generator)231 return code_object232def get_git_tag():233 """ extract git commit tag """234 git_tag = subprocess.call("git log -n 1")235 return git_tag236class CodeObject(CodeConfiguration):237 level_header = "{\n"238 level_footer = "}"239 # Prefix added to every free variable name240 GENERAL_PREFIX = ""241 def __init__(self, language, shared_tables = None, parent_tables = None, rounding_mode = ML_GlobalRoundMode, uniquifier = "", main_code_level = None, var_ctor = None):242 """ code object initialization """243 self.expanded_code = ""244 self.uniquifier = uniquifier245 self.tablevel = 0246 self.header_list = []247 self.library_list = []248 self.symbol_table = MultiSymbolTable(shared_tables if shared_tables else {}, parent_tables = (parent_tables if parent_tables else []), uniquifier = self.uniquifier)249 self.language = language250 self.header_comment = []251 self.default_var_ctor = var_ctor252 def add_header_comment(self, comment):253 self.header_comment.append(comment)254 def is_empty(self):255 return len(self.header_list) == 0 and len(self.library_list) == 0 and self.symbol_table.is_empty() and len(self.header_comment) == 0 and len(self.expanded_code) == 0256 def get_symbol_table(self):257 return self.symbol_table258 def reindent(self, line):259 """ re indent code line <line> with proper current indentation level """260 # inserting proper indentation level261 codeline = re.sub("\n", lambda _: ("\n" + self.tablevel * CodeObject.tab), line)262 # removing trailing whitespaces263 codeline = re.sub(" +\n", "\n", codeline)264 return codeline265 def append_code(self, code):266 self.expanded_code += code267 return self268 def __lshift__(self, added_code):269 """ implicit code insertion through << operator """270 indented_code = self.reindent(added_code)271 return self.append_code(indented_code)272 def inc_level(self):273 """ increase indentation level """274 self.tablevel += 1275 self.expanded_code += CodeObject.tab276 def dec_level(self):277 """ decrease indentation level """278 self.tablevel -= 1279 # deleting last inserted tab280 if self.expanded_code[-len(CodeObject.tab):] == CodeObject.tab:281 self.expanded_code = self.expanded_code[:-len(CodeObject.tab)]282 def open_level(self, inc=True, header=None):283 """ open nested block """284 header = self.level_header if header is None else header285 self << header # "{\n"286 if inc: self.inc_level()287 def close_level(self, cr="\n", inc=True, footer=None):288 """ close nested block """289 footer = self.level_footer if footer is None else footer290 if inc: self.dec_level()291 self << "%s%s" % (footer, cr)292 def link_level(self, transition = ""):293 """ close nested block """294 self.dec_level()295 self << "} %s {" % transition296 self.inc_level()297 def add_header(self, header_file):298 """ add a new header file """299 if not header_file in self.header_list:300 self.header_list.append(header_file)301 def get_multiline_comment(self, comment_list):302 result = "/**\n"303 for comment in comment_list:304 result += " * " + comment.replace("\n", "\n *") + "\n"305 result += "**/\n"306 return result307 def add_library(self, library_file):308 """ add a new library file """309 if not library_file in self.library_list:310 self.library_list.append(library_file)311 def generate_header_code(self, git_tag = True):312 """ generate code for header file inclusion """313 result = ""314 # generating git comment315 if git_tag:316 git_comment = CodeConfiguration.get_git_comment()317 self.header_comment.insert(0, git_comment)318 # generating header comments319 result += self.get_multiline_comment(self.header_comment)320 for header_file in self.header_list:321 result += """#include <%s>\n""" % (header_file)322 return result323 def get_free_symbol_name(self, symbol_type, symbol_format, prefix="cotmp", declare=True, symbol_ctor=Variable):324 free_symbol_name = self.symbol_table.get_free_name(symbol_format, prefix)325 if declare:326 self.symbol_table.declare_symbol_name(free_symbol_name, symbol_ctor(free_symbol_name), symbol_type=symbol_type)327 return free_symbol_name328 def get_free_var_name(self, var_type, prefix = "cotmp", declare = True, var_ctor = Variable):329 assert not var_type is None330 free_var_name = self.symbol_table.get_free_name(var_type, self.GENERAL_PREFIX + prefix)331 # declare free var if required332 if declare:333 self.symbol_table.declare_var_name(free_var_name, var_ctor(free_var_name, precision = var_type))334 return free_var_name335 def declare_var_name(self, var_name, var_object):336 self.symbol_table.declare_var_name(var_name, var_object)337 return var_name338 def declare_protected_symbol(self, name, node):339 self.symbol_table.declare_protected_symbol(name, node)340 return node341 def get_free_name(self, var_type, prefix = "cotmp"):342 assert not var_type is None343 return self.symbol_table.get_free_name(var_type, prefix)344 def table_has_definition(self, table_object):345 return self.symbol_table.table_has_definition(table_object)346 ## Declare a new constant object whose name is build347 # from @p prefix348 # @param cst_objet Constant constant object to be declared349 # @para, prefix str constant name prefix350 def declare_cst(self, cst_object, prefix = "cst"):351 """ declare a new constant object and return the registered name """352 free_var_name = self.symbol_table.get_free_cst_name(cst_object.get_precision(), prefix)353 self.symbol_table.declare_cst_name(free_var_name, cst_object)354 return free_var_name355 def declare_table(self, table_object, prefix):356 table_name = self.table_has_definition(table_object)357 if table_name != None:358 return table_name359 else:360 free_var_name = self.symbol_table.get_free_name(table_object.get_storage_precision(), prefix)361 self.symbol_table.declare_table_name(free_var_name, table_object)362 return free_var_name363 def declare_function(self, function_name, function_object):364 self.symbol_table.declare_function_name(function_name, function_object)365 return function_name366 def declare_component(self, component_name, component_object):367 self.symbol_table.declare_component_name(component_name, component_object)368 return component_name369 def get(self, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):370 """ generate unrolled code content """371 result = ""372 if headers:373 result += self.generate_header_code()374 result += "\n\n"375 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []376 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []377 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []378 result += self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)379 result += self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])380 result += "\n" if result != "" else ""381 result += self.expanded_code382 return result383 def push_into_parent_code(self, parent_code, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):384 if headers:385 parent_code << self.generate_header_code()386 parent_code << "\n\n"387 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []388 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []389 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []390 parent_code << self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)391 parent_code << self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])392 parent_code << "\n"393 parent_code << self.expanded_code394 def add_comment(self, comment):395 """ add a full line comment """396 self << ("/* %s */\n" % comment)397 def add_multiline_comment(self, comment):398 self.add_comment(comment)399class Gappa_Unknown(object):400 def __str__(self):401 return "?"402class GappaCodeObject(CodeObject):403 def __init__(self):404 CodeObject.__init__(self, Gappa_Code)405 self.hint_table = []406 self.hypothesis_table = []407 self.goal_table = []408 def add_hint(self, hypoth_code, goal_code, annotation_code, isApprox = False):409 self.hint_table.append((hypoth_code, goal_code, annotation_code, isApprox))410 def add_hypothesis(self, hypoth_code, hypoth_value):411 self.hypothesis_table.append((hypoth_code, hypoth_value))412 def add_goal(self, goal_code, goal_value = Gappa_Unknown):413 self.goal_table.append((goal_code, goal_value))414 def gen_hint(self):415 result = "#hints\n"416 for hypoth_code, goal_code, annotation_code, isApprox in self.hint_table:417 annotation_code = "{%s}" % annotation_code.get() if annotation_code is not None else ""418 symbol = "~" if isApprox else "->"419 result += "%s %s %s %s;\n\n" % (hypoth_code.get(), symbol, goal_code.get(), annotation_code)420 return result421 def gen_complete_goal(self):422 result = "# goalee\n"423 hypothesis = []424 for hc, hv in self.hypothesis_table:425 hypothesis.append("%s in %s" % (hc.get(), self.get_value_str(hv)))426 if isinstance(hc.precision, ML_Fixed_Format):427 hypothesis.append("@FIX(%s,%s)" % (hc.get(), str(- hc.precision.get_frac_size())))428 if isinstance(hc.precision, ML_FP_Format):429 hypothesis.append("@FLT(%s,%s)" % (hc.get(), str(hc.precision.get_field_size()+1)))430 goal = ["%s in %s" % (hc.get(), self.get_value_str(hv)) for hc, hv in self.goal_table]431 result += "{ %s -> %s }\n\n" % (" /\ ".join(hypothesis), " /\ ".join(goal))432 return result433 def get_value_str(self, value):434 if value is Gappa_Unknown:435 return "?"436 elif isinstance(value, sollya.SollyaObject) and value.is_range():437 return "[%s, %s]" % (sollya.inf(value), sollya.sup(value))438 else:439 return str(value)440 def get(self, code_generator, static_cst = False, static_table = False, headers = False, skip_function = True):441 result = ""442 # symbol exclusion list443 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []444 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []445 declaration_exclusion_list += [MultiSymbolTable.VariableSymbol]446 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []447 # declaration generation448 result += self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)449 result += self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])450 result += "\n" if result != "" else ""451 result += self.expanded_code452 result += "\n\n"453 result += self.gen_complete_goal()454 result += self.gen_hint()455 return result456 def push_into_parent_code(self, parent_code, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):457 # symbol exclusion list458 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []459 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []460 declaration_exclusion_list += [MultiSymbolTable.VariableSymbol]461 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []462 # declaration generation463 parent_code << self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)464 parent_code << self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])465 parent_code << "\n"466 parent_code << self.expanded_code467 parent_code << "\n\n"468 parent_code << self.gen_complete_goal()469 parent_code << self.gen_hint()470class LLVMCodeObject(CodeObject):471 GENERAL_PREFIX = "%"472 def __init__(self, language, shared_tables=None, parent_tables=None,473 rounding_mode=ML_GlobalRoundMode, uniquifier="",474 main_code_level=None, var_ctor=None):475 CodeObject.__init__(476 self, LLVM_IR_Code, shared_tables, parent_tables, rounding_mode,477 uniquifier, main_code_level, var_ctor478 )479 def get_multiline_comment(self, comment_list):480 result = ""481 for comment in comment_list:482 result += "; " + comment.replace("\n", "\n;") + "\n"483 return result484 def get(self, code_generator, static_cst=False, static_table=False, headers=False, skip_function = True):485 result = ""486 if headers:487 result += self.generate_header_code()488 result += "\n\n"489 # symbol exclusion list490 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []491 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []492 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []493 # always exclude the following from llvm-ir code generation494 declaration_exclusion_list += [495 MultiSymbolTable.VariableSymbol,496 MultiSymbolTable.LabelSymbol497 ]498 # declaration generation499 result += self.symbol_table.generate_declarations(code_generator, exclusion_list=declaration_exclusion_list)500 result += self.symbol_table.generate_initializations(501 code_generator,502 init_required_list=[503 MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol504 ]505 )506 result += "\n" if result != "" else ""507 result += self.expanded_code508 result += "\n"509 return result510 def push_into_parent_code(self, parent_code, code_generator, static_cst = False, static_table = False, headers = False, skip_function=True):511 # symbol exclusion list512 declaration_exclusion_list = [MultiSymbolTable.ConstantSymbol] if static_cst else []513 declaration_exclusion_list += [MultiSymbolTable.TableSymbol] if static_table else []514 declaration_exclusion_list += [MultiSymbolTable.VariableSymbol]515 declaration_exclusion_list += [MultiSymbolTable.LabelSymbol]516 declaration_exclusion_list += [MultiSymbolTable.FunctionSymbol] if skip_function else []517 # declaration generation518 parent_code << self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)519 parent_code << self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])520 parent_code << "\n"521 parent_code << self.expanded_code522 parent_code << "\n"523class VHDLCodeObject(CodeConfiguration):524 def __init__(self, language, shared_tables = None, parent_tables = None, rounding_mode = ML_GlobalRoundMode, uniquifier = "", main_code_level = False, var_ctor = None):525 """ code object initialization """526 self.expanded_code = ""527 self.uniquifier = uniquifier528 self.tablevel = 0529 self.header_list = []530 self.library_list = []531 self.symbol_table = MultiSymbolTable(shared_tables if shared_tables else {}, parent_tables = (parent_tables if parent_tables else []), uniquifier = self.uniquifier)532 self.language = language533 self.header_comment = []534 self.shared_symbol_table_f = MultiSymbolTable.SignalSymbol in shared_tables535 self.main_code_level = main_code_level536 self.default_var_ctor = var_ctor537 def add_header_comment(self, comment):538 self.header_comment.append(comment)539 def is_empty(self):540 return len(self.header_list) == 0 and len(self.library_list) == 0 and self.symbol_table.is_empty() and len(self.header_comment) == 0 and len(self.expanded_code) == 0541 def get_symbol_table(self):542 return self.symbol_table543 def __lshift__(self, added_code):544 """ implicit code insertion through << operator """545 self.expanded_code += re.sub("\n", lambda _: ("\n" + self.tablevel * CodeObject.tab), added_code)546 def inc_level(self):547 """ increase indentation level """548 self.tablevel += 1549 self.expanded_code += CodeObject.tab550 def dec_level(self):551 """ decrease indentation level """552 self.tablevel -= 1553 # deleting last inserted tab554 if self.expanded_code[-len(CodeObject.tab):] == CodeObject.tab:555 self.expanded_code = self.expanded_code[:-len(CodeObject.tab)]556 def open_level(self, inc = True, header=None):557 """ open nested block """558 if inc: self.inc_level()559 def close_level(self, cr = "\n", inc = True, footer=None):560 """ close nested block """561 if inc: self.dec_level()562 def link_level(self, transition = ""):563 """ close nested block """564 raise NotImplementedError565 def add_header(self, header_file):566 """ add a new header file """567 if not header_file in self.header_list:568 self.header_list.append(header_file)569 def add_library(self, library_file):570 """ add a new library file """571 if not library_file in self.library_list:572 self.library_list.append(library_file)573 def generate_header_code(self, git_tag = True):574 """ generate code for header file inclusion """575 result = ""576 # generating git comment577 if git_tag:578 git_comment = CodeConfiguration.get_git_comment()579 self.header_comment.insert(0, git_comment)580 # generating header comments581 result += "--\n"582 for comment in self.header_comment:583 result += "-- " + comment.replace("\n", "\n--") + "\n"584 # TODO/FIXME: erase trailing white spaces properly585 result.replace("--\n", "--\n")586 result += "--\n"587 for library_file in self.library_list:588 result += "library {lib};\n".format(lib = library_file)589 for header_file in self.header_list:590 result += """use %s;\n""" % (header_file)591 return result592 def get_free_var_name(self, var_type, prefix = "tmps", declare = True, var_ctor = None):593 assert not var_type is None594 var_ctor = var_ctor or self.default_var_ctor or Signal595 free_var_name = self.symbol_table.get_free_name(var_type, prefix)596 # declare free var if required597 if declare:598 if var_ctor is Variable:599 self.symbol_table.declare_var_name(free_var_name, var_ctor(free_var_name, precision = var_type))600 elif var_ctor is Signal:601 self.symbol_table.declare_signal_name(free_var_name, var_ctor(free_var_name, precision = var_type))602 else:603 Log.report(Log.Error, "unsupported var constructor in get_free_var_name")604 return free_var_name605 def get_free_signal_name(self, signal_type, prefix = "stmp", declare = True):606 assert not signal_type is None607 free_signal_name = self.symbol_table.get_free_name(signal_type, prefix)608 # declare free var if required609 if declare:610 self.symbol_table.declare_signal_name(free_signal_name, Signal(free_signal_name, precision = signal_type))611 return free_signal_name612 def declare_var_name(self, var_name, var_object):613 self.symbol_table.declare_var_name(var_name, var_object)614 return var_name615 def declare_protected_symbol(self, name, node):616 self.symbol_table.declare_protected_symbol(name, node)617 return node618 def declare_signal(self, signal_object, signal_type, prefix = "stmp"):619 signal_key = self.symbol_table.has_signal_definition(signal_object)620 if not signal_key is None:621 return signal_key622 else:623 free_signal_name = self.symbol_table.get_free_name(signal_type, prefix)624 self.symbol_table.declare_signal_name(free_signal_name, signal_object)625 return free_signal_name626 def get_free_name(self, var_type, prefix = "svtmp"):627 return self.symbol_table.get_free_name(var_type, prefix)628 def table_has_definition(self, table_object):629 return self.symbol_table.table_has_definition(table_object)630 ## Declare a new constant object whose name is build631 # from @p prefix632 # @param cst_objet Constant constant object to be declared633 # @para, prefix str constant name prefix634 def declare_cst(self, cst_object, prefix = "cst"):635 """ declare a new constant object and return the registered name """636 free_var_name = self.symbol_table.get_free_cst_name(cst_object.get_precision(), prefix)637 self.symbol_table.declare_cst_name(free_var_name, cst_object)638 return free_var_name639 def declare_table(self, table_object, prefix):640 table_name = self.table_has_definition(table_object)641 if table_name != None:642 return table_name643 else:644 free_var_name = self.symbol_table.get_free_name(table_object.get_storage_precision(), prefix)645 self.symbol_table.declare_table_name(free_var_name, table_object)646 return free_var_name647 def declare_function(self, function_name, function_object):648 self.symbol_table.declare_function_name(function_name, function_object)649 return function_name650 def declare_component(self, component_name, component_object):651 self.symbol_table.declare_component_name(component_name, component_object)652 return component_name653 def get_entity_list(self):654 """ Return the list of component instanciated in this code object """655 return self.symbol_table.get_entity_list()656 def get(self, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):657 """ generate unrolled code content """658 result = ""659 if headers:660 result += self.generate_header_code()661 result += "\n\n"662 # Entities are always excluded from generation663 # They will be processed in a separate manner (see ML_EntityBasis)664 declaration_exclusion_list = [MultiSymbolTable.EntitySymbol]665 if static_cst:666 declaration_exclusion_list.append(MultiSymbolTable.ConstantSymbol)667 if static_table:668 declaration_exclusion_list.append(MultiSymbolTable.TableSymbol)669 if skip_function:670 declaration_exclusion_list.append(MultiSymbolTable.FunctionSymbol)671 if self.shared_symbol_table_f:672 declaration_exclusion_list.append(MultiSymbolTable.SignalSymbol)673 result += self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)674 result += self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])675 result += "begin\n" if not self.main_code_level else ""676 result += "\n" if result != "" else ""677 result += self.expanded_code678 return result679 def push_into_parent_code(self, parent_code, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):680 """ generate unrolled code content """681 if headers:682 parent_code << self.generate_header_code()683 parent_code << "\n\n"684 declaration_exclusion_list = [MultiSymbolTable.EntitySymbol]685 if static_cst:686 declaration_exclusion_list.append(MultiSymbolTable.ConstantSymbol)687 if static_table:688 declaration_exclusion_list.append(MultiSymbolTable.TableSymbol)689 if skip_function:690 declaration_exclusion_list.append(MultiSymbolTable.FunctionSymbol)691 if self.shared_symbol_table_f:692 declaration_exclusion_list.append(MultiSymbolTable.SignalSymbol)693 parent_code << self.symbol_table.generate_declarations(code_generator, exclusion_list = declaration_exclusion_list)694 parent_code << self.symbol_table.generate_initializations(code_generator, init_required_list = [MultiSymbolTable.ConstantSymbol, MultiSymbolTable.VariableSymbol])695 parent_code.dec_level()696 parent_code << "\n"697 parent_code << ("begin\n" if not self.main_code_level else "")698 parent_code.inc_level()699 parent_code << self.expanded_code700 def add_comment(self, comment):701 """ add a full line comment """702 self << ("-- %s\n" % comment)703 def add_multiline_comment(self, comment):704 for line in comment.split("\n"):705 if line != "": self.add_comment(line) 706## Nested code object707# language is derived from code_generator's language708class NestedCode(object):709 """ object to support multiple levels of nested code with local and710 global variable management """711 ##712 # @param uniquifier <str> unifiquation prefix for name generation713 def __init__(self, code_generator,714 static_cst=False,715 static_table=True,716 uniquifier="",717 code_ctor=CodeObject,718 main_code_level=None,719 shared_symbol_list=None,720 static_var=False):721 self.language = code_generator.language722 self.code_generator = code_generator723 # name uniquifier724 self.uniquifier = uniquifier725 # constructor function for code levels726 self.code_ctor = code_ctor727 # top-level global tables728 self.global_tables = {}729 # defaulting list of shared symbol table to build730 # if none is defined731 self.global_symbol_list = [732 MultiSymbolTable.ConstantSymbol,733 MultiSymbolTable.TableSymbol,734 MultiSymbolTable.FunctionSymbol,735 MultiSymbolTable.EntitySymbol,736 MultiSymbolTable.ProtectedSymbol,737 ] if shared_symbol_list is None else shared_symbol_list738 for symbol in self.global_symbol_list:739 self.global_tables[symbol] = SymbolTable(uniquifier=uniquifier)740 shared_tables = self.global_tables741 self.main_code = self.code_ctor(self.language, shared_tables, uniquifier=self.uniquifier, main_code_level=True)742 self.code_list = [self.main_code]743 @property744 def default_var_ctor(self):745 return self.code_list[0].default_var_ctor746 def add_header_comment(self, comment):747 self.main_code.add_header_comment(comment)748 def add_header(self, header_file):749 self.main_code.add_header(header_file)750 def add_library(self, library_file):751 self.main_code.add_library(library_file)752 def add_local_header(self, header_file):753 self.code_list[0].add_header(header_file)754 def append_code(self, code):755 self.code_list[0].append_code(code)756 return self757 def __lshift__(self, added_code):758 self.code_list[0] << added_code759 return self760 def add_comment(self, comment):761 self.code_list[0].add_comment(comment)762 def add_multiline_comment(self, comment):763 self.code_list[0].add_multiline_comment(comment)764 def open_level(self, extra_shared_tables=None, inc=True, var_ctor=None, header=None):765 """ Open a new level of code """766 self.code_list[0].open_level(inc=inc, header=header)767 parent_tables = self.code_list[0].get_symbol_table().get_extended_dependency_table()768 shared_tables = {}769 # preparing sub-global shared tables770 for global_symbol in self.global_tables:771 shared_tables[global_symbol] = self.global_tables[global_symbol]772 if extra_shared_tables:773 for table_key in extra_shared_tables:774 shared_tables[table_key] = self.code_list[0].get_symbol_table().get_table(table_key)775 # building a new code object for the sub-level776 sublevel_code = self.code_ctor(777 self.language, shared_tables,778 parent_tables=parent_tables, var_ctor=var_ctor)779 self.code_list.insert(0, sublevel_code)780 @property781 def static_cst(self):782 return MultiSymbolTable.ConstantSymbol in self.global_tables783 @property784 def static_table(self):785 return MultiSymbolTable.TableSymbol in self.global_tables786 def close_level(self, cr = "\n", inc = True, footer=None):787 level_code = self.code_list.pop(0)788 level_code.push_into_parent_code(self, self.code_generator, static_cst = self.static_cst, static_table = self.static_table, skip_function = True)789 self.code_list[0].close_level(cr=cr, inc=inc, footer=footer)790 def inc_level(self):791 """ increase indentation level """792 self.code_list[0].inc_level()793 def dec_level(self):794 """ decrease indentation level """795 self.code_list[0].dec_level()796 # @param function_object possible dummy FunctionObject associated with new function_name797 def declare_free_function_name(self, prefix = "foo", function_object = None):798 function_name = self.code_list[0].get_free_name(FunctionFormat(), prefix = prefix)799 self.code_list[0].declare_function(function_name, function_object)800 return function_name801 def get_free_symbol_name(self, symbol_type, symbol_format, symbol_ctor, prefix="tmp", declare=True):802 return self.code_list[0].get_free_symbol_name(symbol_type, symbol_format, prefix, declare, symbol_ctor=symbol_ctor)803 def get_free_var_name(self, var_type, prefix = "tmpv", declare = True, var_ctor = None):804 # trying not to override code_list[0] default var_ctor805 if var_ctor is None:806 return self.code_list[0].get_free_var_name(var_type, prefix, declare)807 else:808 return self.code_list[0].get_free_var_name(var_type, prefix, declare, var_ctor)809 def get_free_name(self, var_type, prefix="tmps"):810 return self.code_list[0].get_free_name(var_type, prefix)811 def get_free_signal_name(self, signal_type, prefix = "stmp", declare = True):812 return self.code_list[0].get_free_signal_name(signal_type, prefix, declare)813 def declare_signal(self, signal_object, signal_type, prefix = "stmp"):814 return self.code_list[0].declare_signal(signal_object, signal_type, prefix)815 def declare_var_name(self, var_name, var_object):816 return self.code_list[0].declare_var_name(var_name, var_object)817 def declare_protected_symbol(self, name, node):818 return self.code_list[0].declare_protected_symbol(name, node)819 def declare_cst(self, cst_object, prefix = "cst"):820 return self.code_list[0].declare_cst(cst_object, prefix)821 def declare_table(self, table_object, prefix = "table"):822 return self.code_list[0].declare_table(table_object, prefix)823 def declare_function(self, function_name, function_object):824 return self.code_list[0].declare_function(function_name, function_object)825 def declare_component(self, component_name, component_object):826 component_name = self.code_list[0].declare_component(component_name, component_object)827 return component_name828 def get_entity_list(self):829 """ Return the component list """830 return self.code_list[0].get_entity_list()831 def get(self, code_generator, static_cst = False, static_table = False, headers = True, skip_function = False):832 return self.code_list[0].get(code_generator, static_cst = static_cst, static_table = static_table, headers = headers, skip_function = skip_function)833 def push_into_parent_code(self, parent_code, code_generator, static_cst = False, static_table = False, headers = False, skip_function = False):...

Full Screen

Full Screen

mailboximap.py

Source:mailboximap.py Github

copy

Full Screen

...153 data = self.M.fetch(num, '(BODY[HEADER])')[1]154 emailBody = data[0][1]155 mail = email.message_from_bytes(156 emailBody, _class=EmailMessage)157 if skip_function(mail):158 continue159 if body:160 data = self.M.fetch(num, '(RFC822)')[1]161 emailBody = data[0][1]162 mail = email.message_from_bytes(163 emailBody, _class=EmailMessage)164 elif skip_function is None:165 data = self.M.fetch(num, '(BODY[HEADER])')[1]166 emailBody = data[0][1]167 mail = email.message_from_bytes(168 emailBody, _class=EmailMessage)169 yield mail170 self.M.close()171 def enumerate_search_person(self, person, folder, skip_function=None,...

Full Screen

Full Screen

model_selection.py

Source:model_selection.py Github

copy

Full Screen

1###############################################################################2# #3# Copyright (C) 2003-2013 Edward d'Auvergne #4# #5# This file is part of the program relax (http://www.nmr-relax.com). #6# #7# This program is free software: you can redistribute it and/or modify #8# it under the terms of the GNU General Public License as published by #9# the Free Software Foundation, either version 3 of the License, or #10# (at your option) any later version. #11# #12# This program is distributed in the hope that it will be useful, #13# but WITHOUT ANY WARRANTY; without even the implied warranty of #14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #15# GNU General Public License for more details. #16# #17# You should have received a copy of the GNU General Public License #18# along with this program. If not, see <http://www.gnu.org/licenses/>. #19# #20###############################################################################21# Module docstring.22"""Module for selecting the best model."""23# Python module imports.24import sys25# relax module imports.26from lib.errors import RelaxError, RelaxPipeError27from lib.io import write_data28from lib.model_selection import aic, aicc, bic29import pipe_control.pipes30from pipe_control.pipes import get_type, has_pipe, pipe_names, switch31from specific_analyses.setup import get_specific_fn32def select(method=None, modsel_pipe=None, bundle=None, pipes=None):33 """Model selection function.34 @keyword method: The model selection method. This can currently be one of:35 - 'AIC', Akaike's Information Criteria.36 - 'AICc', Small sample size corrected AIC.37 - 'BIC', Bayesian or Schwarz Information Criteria.38 - 'CV', Single-item-out cross-validation.39 None of the other model selection techniques are currently supported.40 @type method: str41 @keyword modsel_pipe: The name of the new data pipe to be created by copying of the selected data pipe.42 @type modsel_pipe: str43 @keyword bundle: The optional data pipe bundle to associate the newly created pipe with.44 @type bundle: str or None45 @keyword pipes: A list of the data pipes to use in the model selection.46 @type pipes: list of str47 """48 # Test if the pipe already exists.49 if has_pipe(modsel_pipe):50 raise RelaxPipeError(modsel_pipe)51 # Use all pipes.52 if pipes == None:53 # Get all data pipe names from the relax data store.54 pipes = pipe_names()55 # Select the model selection technique.56 if method == 'AIC':57 print("AIC model selection.")58 formula = aic59 elif method == 'AICc':60 print("AICc model selection.")61 formula = aicc62 elif method == 'BIC':63 print("BIC model selection.")64 formula = bic65 elif method == 'CV':66 print("CV model selection.")67 raise RelaxError("The model selection technique " + repr(method) + " is not currently supported.")68 else:69 raise RelaxError("The model selection technique " + repr(method) + " is not currently supported.")70 # No pipes.71 if len(pipes) == 0:72 raise RelaxError("No data pipes are available for use in model selection.")73 # Initialise.74 function_type = {}75 model_loop = {}76 model_type = {}77 duplicate_data = {}78 model_statistics = {}79 skip_function = {}80 modsel_pipe_exists = False81 # Cross validation setup.82 if isinstance(pipes[0], list):83 # No pipes.84 if len(pipes[0]) == 0:85 raise RelaxError("No pipes are available for use in model selection in the array " + repr(pipes[0]) + ".")86 # Loop over the data pipes.87 for i in range(len(pipes)):88 for j in range(len(pipes[i])):89 # Specific functions.90 model_loop[pipes[i][j]] = get_specific_fn('model_loop', get_type(pipes[i][j]))91 model_type[pipes[i][j]] = get_specific_fn('model_type', get_type(pipes[i][j]))92 duplicate_data[pipes[i][j]] = get_specific_fn('duplicate_data', get_type(pipes[i][j]))93 model_statistics[pipes[i][j]] = get_specific_fn('model_stats', get_type(pipes[i][j]))94 skip_function[pipes[i][j]] = get_specific_fn('skip_function', get_type(pipes[i][j]))95 # The model loop should be the same for all data pipes!96 for i in range(len(pipes)):97 for j in range(len(pipes[i])):98 if model_loop[pipes[0][j]] != model_loop[pipes[i][j]]:99 raise RelaxError("The models for each data pipes should be the same.")100 model_loop = model_loop[pipes[0][0]]101 # The model description.102 model_desc = get_specific_fn('model_desc', get_type(pipes[0]))103 # Global vs. local models.104 global_flag = False105 for i in range(len(pipes)):106 for j in range(len(pipes[i])):107 if model_type[pipes[i][j]]() == 'global':108 global_flag = True109 # All other model selection setup.110 else:111 # Loop over the data pipes.112 for i in range(len(pipes)):113 # Specific functions.114 model_loop[pipes[i]] = get_specific_fn('model_loop', get_type(pipes[i]))115 model_type[pipes[i]] = get_specific_fn('model_type', get_type(pipes[i]))116 duplicate_data[pipes[i]] = get_specific_fn('duplicate_data', get_type(pipes[i]))117 model_statistics[pipes[i]] = get_specific_fn('model_stats', get_type(pipes[i]))118 skip_function[pipes[i]] = get_specific_fn('skip_function', get_type(pipes[i]))119 model_loop = model_loop[pipes[0]]120 # The model description.121 model_desc = get_specific_fn('model_desc', get_type(pipes[0]))122 # Global vs. local models.123 global_flag = False124 for j in range(len(pipes)):125 if model_type[pipes[j]]() == 'global':126 global_flag = True127 # Loop over the base models.128 for model_info in model_loop():129 # Print out.130 print("\n")131 desc = model_desc(model_info)132 if desc:133 print(desc)134 # Initial model.135 best_model = None136 best_crit = 1e300137 data = []138 # Loop over the pipes.139 for j in range(len(pipes)):140 # Single-item-out cross validation.141 if method == 'CV':142 # Sum of chi-squared values.143 sum_crit = 0.0144 # Loop over the validation samples and sum the chi-squared values.145 for k in range(len(pipes[j])):146 # Alias the data pipe name.147 pipe = pipes[j][k]148 # Switch to this pipe.149 switch(pipe)150 # Skip function.151 if skip_function[pipe](model_info):152 continue153 # Get the model statistics.154 k, n, chi2 = model_statistics[pipe](model_info)155 # Missing data sets.156 if k == None or n == None or chi2 == None:157 continue158 # Chi2 sum.159 sum_crit = sum_crit + chi2160 # Cross-validation criterion (average chi-squared value).161 crit = sum_crit / float(len(pipes[j]))162 # Other model selection methods.163 else:164 # Reassign the pipe.165 pipe = pipes[j]166 # Switch to this pipe.167 switch(pipe)168 # Skip function.169 if skip_function[pipe](model_info):170 continue171 # Get the model statistics.172 k, n, chi2 = model_statistics[pipe](model_info, global_stats=global_flag)173 # Missing data sets.174 if k == None or n == None or chi2 == None:175 continue176 # Calculate the criterion value.177 crit = formula(chi2, float(k), float(n))178 # Store the values for a later printout.179 data.append([pipe, repr(k), repr(n), "%.5f" % chi2, "%.5f" % crit])180 # Select model.181 if crit < best_crit:182 best_model = pipe183 best_crit = crit184 # Write out the table.185 write_data(out=sys.stdout, headings=["Data pipe", "Num_params_(k)", "Num_data_sets_(n)", "Chi2", "Criterion"], data=data)186 # Duplicate the data from the 'best_model' to the model selection data pipe.187 if best_model != None:188 # Print out of selected model.189 print("The model from the data pipe " + repr(best_model) + " has been selected.")190 # Switch to the selected data pipe.191 switch(best_model)192 # Duplicate.193 duplicate_data[best_model](best_model, modsel_pipe, model_info, global_stats=global_flag, verbose=False)194 # Model selection pipe now exists.195 modsel_pipe_exists = True196 # No model selected.197 else:198 # Print out of selected model.199 print("No model has been selected.")200 # Switch to the model selection pipe.201 if modsel_pipe_exists:202 switch(modsel_pipe)203 # Bundle the data pipe.204 if bundle:...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

...42 throw_away_prob = data_cuts['throw_away_prob']43 throw_away = np.random.choice([False, True], p=[1 - throw_away_prob, throw_away_prob])44 if throw_away: continue_bool = True45 if data_cuts['custom_skip_function'] is not None:46 continue_bool = use_custom_skip_function(data_cuts['custom_skip_function'], event_track)47 return continue_bool48def use_custom_skip_function(skip_function, event_track):49 """50 User defined, custom skip functions.51 Parameters52 ----------53 skip_function : str54 String that specifies, which custom skip function should be used.55 event_track : ndarray(ndim=1)56 Structured numpy array containing the McTracks info of this event.57 Returns58 -------59 continue_bool : bool60 Bool which specifies, if this event should be skipped or not.61 """62 if skip_function == 'ts_e_flat':...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Test_junkie automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful