Skip to content

Commit

Permalink
Merge pull request #46 from perlinm/fix-stubs
Browse files Browse the repository at this point in the history
Fix broken stubs
  • Loading branch information
quantumgizmos authored Nov 23, 2024
2 parents 00a569e + 115dc98 commit e51d839
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 395 deletions.
118 changes: 38 additions & 80 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,43 @@
## cython stub files

def generate_cython_stub_file(pyx_filepath: str, output_filepath: str) -> None:
with open(pyx_filepath, 'r') as f:
pyx_content = f.read()

# Match function, class, and method definitions, and cdef/cpdef/cclass declarations
pattern = re.compile(r'(cdef class|class|cpdef|def)\s+[\w\[\],\s\*&\<\>\=\:]*')
# cimport_pattern = re.compile(r'from\s+libc\..+\s+cimport\s+')

# Split by lines and filter out lines without definitions
lines = pyx_content.split('\n')
new_lines = []
inside_docstring = False
inside_function = False

for line in lines:

if "__cinit__(" in line:
continue

if "__del__(" in line:
continue

line = line.replace('cdef class', 'class')

stripped_line = line.strip()

# Remove comments
if stripped_line.startswith('#'):
continue

# # Skip cimport statements
# if stripped_line.startswith('cimport'):
# new_lines.append(line)
# continue

# if cimport_pattern.match(stripped_line):
# new_lines.append(line)
# continue

# Include import statements
if stripped_line.startswith('import') or stripped_line.startswith('from') or stripped_line.startswith('cimport'):
new_lines.append(line)
continue

# Handle docstrings
if inside_function:
if stripped_line.startswith('"""') or stripped_line.startswith("'''"):
new_lines.append(line)
# print(line)
inside_docstring = not inside_docstring
if not inside_docstring:
inside_function = False
new_lines.append('\n')
continue

elif inside_docstring:
new_lines.append(line)
# print(line)
continue

# Handle decorators
if stripped_line.startswith('@'):
decorator_found = True
new_lines.append(line)
continue


# Handle function and class definitions
if pattern.match(stripped_line):
print(stripped_line)

new_lines.append(line)
inside_function = True
# new_lines.append('\n')
continue

# Write the stripped content to an output file
with open(output_filepath, 'w') as f:
f.write('\n'.join(new_lines))

pyi_content = ""

# load file contents
pyx_content = open(pyx_filepath, "r").read()

# strip cython syntax, empty lines, and comments
pyx_content = re.sub("cdef ", "", pyx_content)
pyx_content = re.sub(r"^\s*\n", "", pyx_content, flags=re.MULTILINE)
pyx_content = re.sub(r"^\s*#.*\n", "", pyx_content, flags=re.MULTILINE)

# identify top-level import lines
pattern = re.compile(r"^(import|from)\s+.*\n", re.MULTILINE)
for match in pattern.finditer(pyx_content):
pyi_content += pyx_content[match.start():match.end()]

# identify patterns to ignore
ignore_pattern = re.compile(r"__cinit__\(|__del__\(")

# identify class or function declarations
decorator = r"^\s*@.*\n"
declaration = r"^\s*(?:class|def)\s+.*(?:.|\n)\n"
docstring_double = r"\"\"\"(?:.|\n)*?\"\"\""
docstring_single = r"'''(?:.|\n)*?'''"
docstring = rf"\s*(?:{docstring_double}|{docstring_single})\s*\n"
pattern = re.compile(rf"({decorator})?({declaration})({docstring})?", re.MULTILINE)
for match in pattern.finditer(pyx_content):
content = pyx_content[match.start():match.end()]
if not ignore_pattern.match(content, re.MULTILINE):
pyi_content += content.rstrip()
if match.group(3):
# there is a docstring!
pyi_content += "\n"
else:
# there is no docstring
pyi_content += " ...\n"

open(output_filepath, "w").write(pyi_content)

## BUILD

Expand Down Expand Up @@ -140,4 +98,4 @@ def generate_cython_stub_file(pyx_filepath: str, output_filepath: str) -> None:
"embedsignature": True,
},
),
)
)
13 changes: 1 addition & 12 deletions src_python/ldpc/belief_find_decoder/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ from scipy.sparse import spmatrix
class BeliefFindDecoder(BpDecoderBase):
"""
A class representing a decoder that combines Belief Propagation (BP) with the Union Find Decoder (UFD) algorithm.
The BeliefFindDecoder is designed to decode binary linear codes by initially attempting BP decoding, and if that fails,
it falls back to the Union Find Decoder algorithm. The UFD algorithm is based on the principles outlined in
https://arxiv.org/abs/1709.06218, with an option to utilise a more general version as described in
https://arxiv.org/abs/2103.08049 for LDPC codes by setting `uf_method=True`.
Parameters
----------
pcm : Union[np.ndarray, scipy.sparse.spmatrix]
Expand Down Expand Up @@ -39,38 +37,29 @@ class BeliefFindDecoder(BpDecoderBase):
The inversion method can be applied to any parity check matrix.
bits_per_step : int, optional
Specifies the number of bits added to the cluster in each step of the UFD algorithm. If no value is provided, this is set the block length of the code.
Notes
-----
The `BeliefFindDecoder` class leverages soft information outputted by the BP decoder to guide the cluster growth
in the UFD algorithm. The number of bits added to the cluster in each step is controlled by the `bits_per_step` parameter.
The `uf_method` parameter activates a more general version of the UFD algorithm suitable for LDPC codes when set to True.
"""


def decode(self,syndrome):
"""
Decodes the input syndrome using the belief propagation and UFD decoding methods.
Initially, the method attempts to decode the syndrome using belief propagation. If this fails to converge,
it falls back to the UFD algorithm.
Parameters
----------
syndrome : np.ndarray
The input syndrome to decode.
Returns
-------
np.ndarray
The decoded output.
Raises
------
ValueError
If the length of the input syndrome is not equal to the length of the code.
"""


@property
def uf_method(self):
def uf_method(self): ...
Loading

0 comments on commit e51d839

Please sign in to comment.