diff --git a/lcn/generator.py b/lcn/generator.py index b050a1a..33dd887 100644 --- a/lcn/generator.py +++ b/lcn/generator.py @@ -194,7 +194,7 @@ def make_lcn_random2( count = 0 while count < c: child = ordering[np.random.randint(n - p)] - if cpts[child] is True: + if cpts[child]: continue cpts[child] = True num_higher_vars = n - position[child] - 1 @@ -218,7 +218,7 @@ def make_lcn_random2( # Add the roots for i in range(n): - if cpts[i] is False: + if not cpts[i]: scopes[i] = [i] # Create extra knowledge: l <= P(x) <= u @@ -341,7 +341,7 @@ def make_lcn_dag( count = 0 while count < c: child = ordering[np.random.randint(n - p)] - if cpts[child] is True: + if cpts[child]: continue cpts[child] = True num_higher_vars = n - position[child] - 1 @@ -367,7 +367,7 @@ def make_lcn_dag( # Add the roots for i in range(n): - if cpts[i] is False: + if not cpts[i]: scopes[i] = [i] # Create extra knowledge: l <= P(x) <= u @@ -489,7 +489,7 @@ def make_lcn_polytree( u = ordering[i] v = ordering[j] edge = (u, v)# if np.random.uniform() <= 0.5 else (v, u) - if G.has_edge(edge[0], edge[1]) is False: + if not G.has_edge(edge[0], edge[1]): UG = nx.to_undirected(G) paths = list(nx.all_simple_paths(UG, u, v)) assert(len(paths) == 1) diff --git a/lcn/model.py b/lcn/model.py index 652111a..e9caa61 100644 --- a/lcn/model.py +++ b/lcn/model.py @@ -51,8 +51,13 @@ def __init__( self.label = label # unique identifier of the formula (is the atom if atomic) self.input_formula = formula # store the formula output, atoms = parse_formula(formula) # parse the formula - if output is None or vars is None: + if output is None or atoms is None: raise ValueError(f"Malformed formula: {formula}") + + # Reject empty / whitespace-only formulas that parse as valid but have no content + stripped = formula.strip() + if not stripped: + raise ValueError(f"Empty formula: {formula!r}") self.parsed_formula = output # parse tree of the formula self.atoms = atoms # a dict indexed by 'Vi' where i is the i-th variable @@ -892,7 +897,11 @@ def parse_sentence(line: str): tau = False if ";" in line: pos = line.find(";") - tau = bool(line[pos+1:].strip()) + tau_str = line[pos+1:].strip() + # Parse "tau=true" or "tau=false" correctly — bool() of any + # non-empty string (including "False") returns True, so we + # must split on "=" to get the actual boolean value. + tau = tau_str.split("=")[1].lower() == 'true' line = line[:pos] # Get the lower and upper bounds