Skip to content
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

fix: precision of phenotypes written to pheno file #199

Merged
merged 2 commits into from
Mar 22, 2023
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
8 changes: 5 additions & 3 deletions haptools/data/phenotypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,17 @@ def write(self):
# now we can finally write the file
with self.hook_compressed(self.fname, mode="w") as phens:
phens.write("#IID\t" + "\t".join(names) + "\n")
formatter = {"float_kind": lambda x: "%.2f" % x}
for samp, phen in zip(self.samples, self.data):
line = np.array2string(
phen,
sign=" ",
legacy=False,
separator="\t",
formatter=formatter,
max_line_width=np.inf,
threshold=np.inf,
edgeitems=np.inf,
floatmode="unique",
suppress_small=False,
max_line_width=np.inf,
)[1:-1]
phens.write(f"{samp}\t" + line + "\n")

Expand Down
16 changes: 14 additions & 2 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,24 @@ def test_write_phenotypes(self):
# now, let's load the data and check that it's what we wrote
result = Phenotypes(expected_phen.fname)
result.read()
np.testing.assert_allclose(expected_phen.data, result.data)
np.testing.assert_allclose(expected_phen.data, result.data, rtol=0, atol=0)
assert expected_phen.names == result.names
assert expected_phen.samples == result.samples

# try to standardize the data and see if it's still close
# to validate that our code can handle phenotypes with arbitrary precision
expected_phen.standardize()
expected_phen.write()

# now, let's load the data and check that it's what we wrote
result = Phenotypes(expected_phen.fname)
result.read()
np.testing.assert_allclose(expected_phen.data, result.data, rtol=0, atol=0)
assert expected_phen.names == result.names
assert expected_phen.samples == result.samples

# let's clean up after ourselves and delete the file
os.remove(str(expected_phen.fname))
expected_phen.fname.unlink()

def test_append_phenotype(self):
expected1 = self._get_fake_phenotypes()
Expand Down