Skip to content

Commit

Permalink
fix: precision of phenotypes written to pheno file (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryarm authored Mar 22, 2023
1 parent aade751 commit a397c96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
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

0 comments on commit a397c96

Please sign in to comment.