Skip to content

Conversation

@shnizzedy
Copy link
Member

@shnizzedy shnizzedy commented Sep 18, 2024

Fixes

Fixes #2152 by Shinwon Park

Description

Replaces

# header wouldn't be longer than 5, right? I don't want to
# loop over the whole file
for i in range(5):
line = f.readline()
if line.startswith("#") or isinstance(line[0], str):
header.append(line)
with
for line in _f.readlines():
try:
float(line.split()[0])
break
except ValueError:
header.append(line)

Technical details

This try / except ValueError strategy resolves the check on the first line that starts with a number:

In [1]: isinstance("0.3", str)
Out[1]: True

In [2]: try:
   ...:     float("0.3")
   ...:     print(False)
   ...: except ValueError:
   ...:     print(True)
   ...: 
False

and maintains the functionality for each line that starts with a non-number string:

In [3]: isinstance("#", str)
Out[3]: True

In [4]: try:
   ...:     float("#")
   ...:     print(False)
   ...: except ValueError:
   ...:     print(True)
   ...: 
True

Tests

@pytest.mark.parametrize("start_line", list(range(6)))
def test_read_1D(start_line: int, tmp_path: Path) -> None:
"""Test the correct number of rows are read when reading a 1D file."""
regressor: Path = tmp_path / f"regressor_startAtL{start_line}.1D"
# create a regressor.1D file with (5 - ``start_line``) lines of header
with (
RAW_ONE_D.open("r", encoding="utf-8") as _raw,
regressor.open("w", encoding="utf-8") as _test_file,
):
for line in _raw.readlines()[start_line:]:
_test_file.write(line)
header: list[str]
data: NDArray
header, data = read_1D(regressor)
# should get the same array no matter how many lines of header
assert data.shape == (10, 29)
# all header lines should be captured
assert len(header) == 5 - start_line

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I updated the changelog.
  • I added or updated documentation (📝 Document FCP-INDI/C-PAC#2152 + FCP-INDI/C-PAC#2153 fcp-indi.github.io#327).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@shnizzedy
Copy link
Member Author

Let's wait to merge this until we get some documentation up

@shnizzedy shnizzedy merged commit 1fec66e into develop Oct 2, 2024
1 check passed
@shnizzedy shnizzedy deleted the bandpass/header branch October 2, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants