def __init__(self, path=None, string=None): """ Initialisation of BifReader object Parameters ---------------- path : file or str File of bif data string : str String of bif data Examples ----------------- # dog-problem.bif file is present at # http://www.cs.cmu.edu/~javabayes/Examples/DogProblem/dog-problem.bif >>> from pgmpy.readwrite import BIFReader >>> reader = BIFReader("bif_test.bif") >>> reader = BIFReader("bif_test.bif") <pgmpy.readwrite.BIF.BIFReader object at 0x7f2375621cf8> """ if path: with open(path, 'r') as network: self.network = network.read() elif string: self.network = string else: raise ValueError("Must specify either path or string") if '"' in self.network: # Replacing quotes by spaces to remove case sensitivity like: # "Dog-Problem" and Dog-problem # or "true""false" and "true" "false" and true false self.network = self.network.replace('"', ' ') if '/*' in self.network or '//' in self.network: self.network = cppStyleComment.suppress().transformString( self.network) # removing comments from the file self.name_expr, self.state_expr, self.property_expr = self.get_variable_grammar( ) self.probability_expr, self.cpd_expr = self.get_probability_grammar() self.network_name = self.get_network_name() self.variable_names = self.get_variables() self.variable_states = self.get_states() self.variable_properties = self.get_property() self.variable_parents = self.get_parents() self.variable_cpds = self.get_values() self.variable_edges = self.get_edges()
def __init__(self, path=None, string=None): """ Initialisation of BifReader object Parameters ---------------- path : file or str File of bif data string : str String of bif data Examples ----------------- # dog-problem.bif file is present at # http://www.cs.cmu.edu/~javabayes/Examples/DogProblem/dog-problem.bif >>> from pgmpy.readwrite import BIFReader >>> reader = BIFReader("bif_test.bif") >>> reader = BIFReader("bif_test.bif") <pgmpy.readwrite.BIF.BIFReader object at 0x7f2375621cf8> """ if path: with open(path, 'r') as network: self.network = network.read() elif string: self.network = string else: raise ValueError("Must specify either path or string") if '"' in self.network: # Replacing quotes by spaces to remove case sensitivity like: # "Dog-Problem" and Dog-problem # or "true""false" and "true" "false" and true false self.network = self.network.replace('"', ' ') if '/*' in self.network or '//' in self.network: self.network = cppStyleComment.suppress().transformString(self.network) # removing comments from the file self.name_expr, self.state_expr, self.property_expr = self.get_variable_grammar() self.probability_expr, self.cpd_expr = self.get_probability_grammar() self.network_name = self.get_network_name() self.variable_names = self.get_variables() self.variable_states = self.get_states() self.variable_properties = self.get_property() self.variable_parents = self.get_parents() self.variable_cpds = self.get_cpd() self.variable_edges = self.get_edges()
def removeCommentsGo(fStr, fName='', removePrint=False): returnStr = fStr raw_lookup_table = '' lookup_table = '' fixStr = '' # First remove the large blobs of data so we don't hit memory faults :-/ raw_lookup_table_query = re.compile(r' raw_lookup_table.*?\n', re.I) lookup_table_query = re.compile(r' lookup_table.*?\n', re.I) try: raw_lookup_table = re.search(raw_lookup_table_query, returnStr).group() except: raw_lookup_table = '' try: lookup_table = re.search(lookup_table_query, returnStr).group() except: lookup_table = '' if raw_lookup_table: returnStr = re.sub(raw_lookup_table_query, ' raw_lookup_table := ""\n', returnStr) if lookup_table: returnStr = re.sub(lookup_table_query, ' lookup_table := ""\n', returnStr) ##### cppStyleComment.ignore(dblQuotedString) v1 = cppStyleComment.suppress().transformString(returnStr) if removePrint: comp2 = re.compile(r'^\s*fmt.P.*?\n', re.MULTILINE) v2 = re.sub(comp2, "", v1) returnStr = v2 #Check for the case of fmt.Sprintf or other fmt modifications that are not printing if not "fmt." in v2: comp3 = re.compile(r'^\s*\"fmt\".*?\n', re.MULTILINE) v3 = re.sub(comp3, "", v2) returnStr = v3 #Ugggggh deal with the C import types :( if "import \"C\"" in returnStr: #print "In Import C Section" comp4 = re.compile(r'^import \"C\".*?\n', re.MULTILINE) repVal = '''/* #cgo CFLAGS: -IMemoryModule #cgo LDFLAGS: MemoryModule/build/MemoryModule.a #include "MemoryModule/MemoryModule.h" */ import "C" ''' v4 = re.sub(comp4, repVal, returnStr) returnStr = v4 else: returnStr = v1 #### Inject back in the blobs returnStr = re.sub(raw_lookup_table_query, raw_lookup_table, returnStr) returnStr = re.sub(lookup_table_query, lookup_table, returnStr) ####### if fName: try: myOut = open(fileN + '_clean', 'w') myOut.write(returnStr) myOut.close() except: print("[!] Can not write output file") else: return returnStr
def __init__(self, path=None, string=None, include_properties=False, n_jobs=-1): """ Initializes a BIFReader object. Parameters ---------- path : file or str File of bif data string : str String of bif data include_properties: boolean If True, gets the properties tag from the file and stores in graph properties. n_jobs: int (default: -1) Number of jobs to run in parallel. `-1` means use all processors. Examples -------- # dog-problem.bif file is present at # http://www.cs.cmu.edu/~javabayes/Examples/DogProblem/dog-problem.bif >>> from ProbabilityModel.readwrite import BIFReader >>> reader = BIFReader("bif_test.bif") >>> reader = BIFReader("bif_test.bif") <ProbabilityModel.readwrite.BIF.BIFReader object at 0x7f2375621cf8> """ if path: with open(path, "r") as network: self.network = network.read() elif string: self.network = string else: raise ValueError("Must specify either path or string") self.n_jobs = n_jobs self.include_properties = include_properties if '"' in self.network: # Replacing quotes by spaces to remove case sensitivity like: # "Dog-Problem" and Dog-problem # or "true""false" and "true" "false" and true false self.network = self.network.replace('"', " ") if "/*" in self.network or "//" in self.network: self.network = cppStyleComment.suppress().transformString( self.network) # removing comments from the file ( self.name_expr, self.state_expr, self.property_expr, ) = self.get_variable_grammar() self.probability_expr, self.cpd_expr = self.get_probability_grammar() self.network_name = self.get_network_name() self.variable_names = self.get_variables() self.variable_states = self.get_states() if self.include_properties: self.variable_properties = self.get_property() self.variable_parents = self.get_parents() self.variable_cpds = self.get_values() self.variable_edges = self.get_edges()
def removeCommentsGo(fStr,fName='',removePrint=False): returnStr = fStr raw_lookup_table = '' lookup_table='' fixStr = '' # First remove the large blobs of data so we don't hit memory faults :-/ raw_lookup_table_query = re.compile(r' raw_lookup_table.*?\n',re.I) lookup_table_query = re.compile(r' lookup_table.*?\n',re.I) try: raw_lookup_table = re.search(raw_lookup_table_query,returnStr).group() except: raw_lookup_table = '' try: lookup_table = re.search(lookup_table_query,returnStr).group() except: lookup_table = '' if raw_lookup_table: returnStr = re.sub(raw_lookup_table_query,' raw_lookup_table := ""\n',returnStr) if lookup_table: returnStr = re.sub(lookup_table_query,' lookup_table := ""\n',returnStr) ##### cppStyleComment.ignore(dblQuotedString) v1 = cppStyleComment.suppress().transformString(returnStr) if removePrint: comp2 = re.compile(r'^\s*fmt.P.*?\n',re.MULTILINE) v2 = re.sub(comp2,"",v1) returnStr = v2 #Check for the case of fmt.Sprintf or other fmt modifications that are not printing if not "fmt." in v2: comp3 = re.compile(r'^\s*\"fmt\".*?\n',re.MULTILINE) v3 = re.sub(comp3,"",v2) returnStr = v3 #Ugggggh deal with the C import types :( if "import \"C\"" in returnStr: #print "In Import C Section" comp4 = re.compile(r'^import \"C\".*?\n',re.MULTILINE) repVal = '''/* #cgo CFLAGS: -IMemoryModule #cgo LDFLAGS: MemoryModule/build/MemoryModule.a #include "MemoryModule/MemoryModule.h" */ import "C" ''' v4 = re.sub(comp4,repVal,returnStr) returnStr = v4 else: returnStr = v1 #### Inject back in the blobs returnStr = re.sub(raw_lookup_table_query,raw_lookup_table,returnStr) returnStr = re.sub(lookup_table_query,lookup_table,returnStr) ####### if fName: try: myOut = open(fileN+'_clean','w') myOut.write(returnStr) myOut.close() except: print "[!] Can not write output file" else: return returnStr
def header_parser(): identifier = Regex("[a-zA-Z_][a-zA-Z0-9_\$]*") comment = cppStyleComment.suppress() size = Group( Optional( Suppress('[') + SkipTo(']') + Suppress(']') ) ) # Params end_param = Literal(',') + 'parameter' | Literal(')') + '(' ptype = Optional( oneOf('integer real realtime time') ) # NOTE: this isn't completely right, good enough for parsing valid Verilog param = Group( 'parameter' + ptype + size + identifier + Suppress('=') + SkipTo(end_param) ) list_of_params = Group( Suppress('#(') + delimitedList( param ) + Suppress(')') ) # Ports dir_ = Optional( oneOf('input output inout') ) type_ = Optional( oneOf('wire reg') ) port = Group( dir_ + type_ + size + identifier ) list_of_ports = Group( Suppress('(') + delimitedList( port ) + Suppress(')') ) # Module module_identifier = identifier module = Group( Suppress('module') + module_identifier('module_name') + Optional( list_of_params('params') ) + Optional( list_of_ports ('ports' ) ) + Suppress(';') + SkipTo('endmodule') + Suppress('endmodule') ) # Debug #print #module_identifier.setParseAction( dbg('modname') )#.setDebug() #param .setParseAction( dbg('param') )#.setDebug() #port .setParseAction( dbg('port' ) )#.setDebug() #module .setParseAction( dbg('module', 1) )#.setDebug() file_ = SkipTo('module', ignore=comment ).suppress() + \ OneOrMore( module ).ignore( comment ) + \ SkipTo( StringEnd() ).suppress() return file_