Skip to content

Fix keyword files with list of lists #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions pyerrors/input/sfcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
String that separates the ensemble identifier from the configuration number (default 'n').
replica: list
list of replica to be read, default is all
files: list
files: list[list[int]]
list of files to be read per replica, default is all.
for non-compact output format, hand the folders to be read here.
check_configs: list[list[int]]
Expand Down Expand Up @@ -236,6 +236,16 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
rep_path = path + '/' + item
if "files" in kwargs:
files = kwargs.get("files")
if isinstance(files, list):
if all(isinstance(f, list) for f in files):
files = files[i]
elif all(isinstance(f, str) for f in files):
files = files
else:
raise TypeError("files has to be of type list[list[str]] or list[str]!")
else:
raise TypeError("files has to be of type list[list[str]] or list[str]!")

Copy link
Owner

@fjosw fjosw Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if files is not of type list[list[int]]? Maybe we should add an explicit exception here or is there a type check somewhere else that I overlooked?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry it took some time, I put explicit type checks in both places where kwargs.get('files') is used and just put two tests.

else:
files = []
sub_ls = _find_files(rep_path, prefix, compact, files)
Expand All @@ -248,7 +258,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
else:
rep_idl.append(int(cfg[3:]))
except Exception:
raise Exception("Couldn't parse idl from directroy, problem with file " + cfg)
raise Exception("Couldn't parse idl from directory, problem with file " + cfg)
rep_idl.sort()
# maybe there is a better way to print the idls
if not silent:
Expand Down Expand Up @@ -309,7 +319,10 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
w = specs[3]
w2 = specs[4]
if "files" in kwargs:
ls = kwargs.get("files")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a bug? Did the code work before when specifying the kwarg files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this worked, but the thing is that the files-keyword didn't do anything in this case... With this, it would give you the option to (at least) say which ids you want, however, this is of course nto the same kind of control you have in the other cases, where you can actually say which idls you want.
For this to work, we would need to implement something similar to #185 for this read method. I coudl also take care of that, but I need some time for that I think.

if isinstance(kwargs.get("files"), list) and all(isinstance(f, str) for f in kwargs.get("files")):
name_ls = kwargs.get("files")
else:
raise TypeError("In append mode, files has to be of type list[str]!")
else:
name_ls = ls
for exc in name_ls:
Expand Down
49 changes: 49 additions & 0 deletions tests/sfcf_in_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def test_o_bi(tmp_path):
assert f_A[2].value == -41.025094911185185


def test_o_bi_files(tmp_path):
build_test_environment(str(tmp_path), "o", 10, 3)
f_A = sfin.read_sfcf(str(tmp_path) + "/data_o", "test", "f_A", quarks="lquark lquark", wf=0, version="2.0",
files=[["cfg" + str(i) for i in range(1, 11, 2)], ["cfg" + str(i) for i in range(2, 11, 2)], ["cfg" + str(i) for i in range(1, 11, 2)]])
print(f_A)
assert len(f_A) == 3
assert list(f_A[0].shape.keys()) == ["test_|r0", "test_|r1", "test_|r2"]
assert f_A[0].value == 65.4711887279723
assert f_A[1].value == 1.0447210336915187
assert f_A[2].value == -41.025094911185185


def test_o_bib(tmp_path):
build_test_environment(str(tmp_path), "o", 5, 3)
f_V0 = sfin.read_sfcf(str(tmp_path) + "/data_o", "test", "F_V0", quarks="lquark lquark", wf=0, wf2=0, version="2.0", corr_type="bib")
Expand Down Expand Up @@ -120,6 +132,25 @@ def test_c_bi(tmp_path):
assert f_A[2].value == -41.025094911185185


def test_c_bi_files(tmp_path):
build_test_environment(str(tmp_path), "c", 10, 3)
f_A = sfin.read_sfcf(str(tmp_path) + "/data_c", "data_c", "f_A", quarks="lquark lquark", wf=0, version="2.0c",
files=[["data_c_r0_n" + str(i) for i in range(1, 11, 2)], ["data_c_r1_n" + str(i) for i in range(2, 11, 2)], ["data_c_r2_n" + str(i) for i in range(1, 11, 2)]])
print(f_A)
assert len(f_A) == 3
assert list(f_A[0].shape.keys()) == ["data_c_|r0", "data_c_|r1", "data_c_|r2"]
assert f_A[0].value == 65.4711887279723
assert f_A[1].value == 1.0447210336915187
assert f_A[2].value == -41.025094911185185


def test_c_bi_files_int_fail(tmp_path):
build_test_environment(str(tmp_path), "c", 10, 3)
with pytest.raises(TypeError):
sfin.read_sfcf(str(tmp_path) + "/data_c", "data_c", "f_A", quarks="lquark lquark", wf=0, version="2.0c",
files=[[range(1, 11, 2)], [range(2, 11, 2)], [range(1, 11, 2)]])


def test_c_bib(tmp_path):
build_test_environment(str(tmp_path), "c", 5, 3)
f_V0 = sfin.read_sfcf(str(tmp_path) + "/data_c", "data_c", "F_V0", quarks="lquark lquark", wf=0, wf2=0, version="2.0c", corr_type="bib")
Expand Down Expand Up @@ -256,6 +287,24 @@ def test_a_bi(tmp_path):
assert f_A[2].value == -41.025094911185185


def test_a_bi_files(tmp_path):
build_test_environment(str(tmp_path), "a", 5, 3)
f_A = sfin.read_sfcf(str(tmp_path) + "/data_a", "data_a", "f_A", quarks="lquark lquark", wf=0, version="2.0a", files=["data_a_r0.f_A", "data_a_r1.f_A", "data_a_r2.f_A"])
print(f_A)
assert len(f_A) == 3
assert list(f_A[0].shape.keys()) == ["data_a_|r0", "data_a_|r1", "data_a_|r2"]
assert f_A[0].value == 65.4711887279723
assert f_A[1].value == 1.0447210336915187
assert f_A[2].value == -41.025094911185185


def test_a_bi_files_int_fail(tmp_path):
build_test_environment(str(tmp_path), "a", 10, 3)
with pytest.raises(TypeError):
sfin.read_sfcf(str(tmp_path) + "/data_a", "data_a", "f_A", quarks="lquark lquark", wf=0, version="2.0a",
files=[[range(1, 11, 2)], [range(2, 11, 2)], [range(1, 11, 2)]])


def test_a_bib(tmp_path):
build_test_environment(str(tmp_path), "a", 5, 3)
f_V0 = sfin.read_sfcf(str(tmp_path) + "/data_a", "data_a", "F_V0", quarks="lquark lquark", wf=0, wf2=0, version="2.0a", corr_type="bib")
Expand Down