def test_c_reader_npz(self): distreader = DistNpz(self.currentFolder + "/../examples/toydata10.dist.npz") distdata = distreader.read(order='F',force_python_only=False) snp_c = distdata.val self.assertEqual(np.float64, snp_c.dtype) self.assertTrue(np.allclose(self.dist_values[:,:10], snp_c, rtol=1e-05, atol=1e-05)) distreader1 = DistNpz(self.currentFolder + "/../examples/toydata10.dist.npz") distreader2 = DistNpz(self.currentFolder + "/../examples/toydata.dist.npz")[:,:10] self.assertTrue(np.allclose(distreader1.read().val, distreader2.read().val, rtol=1e-05, atol=1e-05)) distdata.val[1,2] = np.NaN # Inject a missing value to test writing and reading missing values output = "tempdir/distreader/toydata10.dist.npz" create_directory_if_necessary(output) DistNpz.write(output,distdata) snpdata2 = DistNpz(output).read() np.testing.assert_array_almost_equal(distdata.val, snpdata2.val, decimal=10) snpdata3 = distdata[:,0:0].read() #create distdata with no sids output = "tempdir/distreader/toydata0.dist.npz" DistNpz.write(output,snpdata3) snpdata4 = DistNpz(output).read() assert snpdata3 == snpdata4
def test_write_x_x_cpp(self): distreader = DistNpz(self.currentFolder + "/../examples/toydata.dist.npz") for order in ['C','F']: for dtype in [np.float32,np.float64]: distdata = distreader.read(order=order,dtype=dtype) distdata.val[-1,0] = float("NAN") output = "tempdir/toydata.{0}{1}.cpp.dist.npz".format(order,"32" if dtype==np.float32 else "64") create_directory_if_necessary(output) DistNpz.write(output, distdata) snpdata2 = DistNpz(output).read() np.testing.assert_array_almost_equal(distdata.val, snpdata2.val, decimal=10)
def test_write_distnpz_f64cpp_0(self): distreader = DistNpz(self.currentFolder + "/../examples/toydata.dist.npz") iid_index = 0 logging.info("iid={0}".format(iid_index)) #if distreader.iid_count % 4 == 0: # divisible by 4 isn't a good test # distreader = distreader[0:-1,:] #assert distreader.iid_count % 4 != 0 distdata = distreader[0:iid_index,:].read(order='F',dtype=np.float64) if distdata.iid_count > 0: distdata.val[-1,0] = float("NAN") output = "tempdir/toydata.F64cpp.{0}.dist.npz".format(iid_index) create_directory_if_necessary(output) DistNpz.write(output, distdata ) snpdata2 = DistNpz(output).read() np.testing.assert_array_almost_equal(distdata.val, snpdata2.val, decimal=10)
if __name__ == '__main__': logging.basicConfig(level=logging.WARN) if False: from pysnptools.snpreader import Bed from pysnptools.distreader import DistData, DistNpz # Create toydata.dist.npz currentFolder = os.path.dirname(os.path.realpath(__file__)) if True: snpreader = Bed(currentFolder + "/../examples/toydata.5chrom.bed",count_A1=True)[:25,:] np.random.seed(392) val = np.random.random((snpreader.iid_count,snpreader.sid_count,3)) val /= val.sum(axis=2,keepdims=True) #make probabilities sum to 1 distdata = DistData(iid=snpreader.iid,sid=snpreader.sid,pos=snpreader.pos,val=val) DistNpz.write(currentFolder + "/../examples/toydata.dist.npz",distdata) if True: distdata = DistNpz(currentFolder + "/../examples/toydata.dist.npz").read() for sid_major,name_bit in [(False,'iidmajor'),(True,'snpmajor')]: DistHdf5.write(currentFolder + "/../examples/toydata.{0}.dist.hdf5".format(name_bit),distdata,sid_major=sid_major) if True: distdata = DistNpz(currentFolder + "/../examples/toydata.dist.npz")[:,:10].read() DistNpz.write(currentFolder + "/../examples/toydata10.dist.npz",distdata) if True: distdata = DistNpz(currentFolder + "/../examples/toydata.dist.npz")[:,:10].read() DistMemMap.write(currentFolder + "/../examples/tiny.dist.memmap",distdata) print('done') suites = getTestSuite() r = unittest.TextTestRunner(failfast=False) ret = r.run(suites)