Skip to content

Commit

Permalink
Replace float64 and int64 with float32 and int32, respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
szhan committed Sep 18, 2023
1 parent 45064fb commit 9c90522
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
24 changes: 12 additions & 12 deletions python/tests/beagle.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def get_weights(genotyped_pos, ungenotyped_pos):
genotyped_cm = convert_to_genetic_map_position(genotyped_pos)
ungenotyped_cm = convert_to_genetic_map_position(ungenotyped_pos)
# Calculate weights for ungenotyped markers.
weights = np.zeros(x, dtype=np.float64)
weights = np.zeros(x, dtype=np.float32)
# Identify genotype markers k and k + 1 sandwiching marker i.
marker_interval_start = np.zeros(x, dtype=np.int64)
marker_interval_start = np.zeros(x, dtype=np.int32)
for i in np.arange(x):
if ungenotyped_pos[i] < genotyped_pos[0]:
# Ungenotyped marker is before the first genotyped marker.
Expand Down Expand Up @@ -126,7 +126,7 @@ def get_mismatch_prob(pos, miscall_rate):
assert isinstance(miscall_rate, float)
if miscall_rate >= 0.5:
miscall_rate = 0.5
mu = np.zeros(len(pos), dtype=np.float64) + miscall_rate
mu = np.zeros(len(pos), dtype=np.float32) + miscall_rate
return mu


Expand All @@ -150,7 +150,7 @@ def get_switch_prob(pos, h, ne):
assert isinstance(ne, float), "Effective population size is not a float."
# Get genetic distance between adjacent markers.
cm = convert_to_genetic_map_position(pos)
d = np.zeros(len(pos), dtype=np.float64)
d = np.zeros(len(pos), dtype=np.float32)
d[0] = cm[0]
d[1:] = cm[1:] - cm[:-1]
assert len(d) == len(pos)
Expand Down Expand Up @@ -185,7 +185,7 @@ def compute_forward_probability_matrix(ref_h, query_h, rho, mu):
assert m == len(mu)
assert 0 <= np.min(rho) and np.max(rho) <= 1
assert 0 <= np.min(mu) and np.max(mu) <= 0.5
fm = np.zeros((m, h), dtype=np.float64)
fm = np.zeros((m, h), dtype=np.float32)
last_sum = 1.0 # Part of normalization factor
for i in np.arange(m):
# Get site-specific parameters.
Expand Down Expand Up @@ -236,7 +236,7 @@ def compute_backward_probability_matrix(ref_h, query_h, rho, mu):
assert m == len(mu)
assert 0 <= np.min(rho) and np.max(rho) <= 1
assert 0 <= np.min(mu) and np.max(mu) <= 0.5
bm = np.zeros((m, h), dtype=np.float64)
bm = np.zeros((m, h), dtype=np.float32)
bm[-1, :] = 1.0 / h # Initialise the last column
for i in np.arange(m - 2, -1, -1):
query_a = query_h[i + 1]
Expand Down Expand Up @@ -330,9 +330,9 @@ def _compute_state_probability_matrix(fm, bm, ref_h, query_h, rho, mu):
assert 0 <= np.min(rho) and np.max(rho) <= 1
assert 0 <= np.min(mu) and np.max(mu) <= 0.5
# m + 1 columns to allow for imputed markers right of the last genotyped marker.
sm = np.zeros((m + 1, h), dtype=np.float64)
fwd_hap_probs = np.zeros((m, 4), dtype=np.float64)
bwd_hap_probs = np.zeros((m, 4), dtype=np.float64)
sm = np.zeros((m + 1, h), dtype=np.float32)
fwd_hap_probs = np.zeros((m, 4), dtype=np.float32)
bwd_hap_probs = np.zeros((m, 4), dtype=np.float32)
for i in np.arange(m - 1, -1, -1):
for j in np.arange(h):
sm[i, j] = fm[i, j] * bm[i, j]
Expand Down Expand Up @@ -384,7 +384,7 @@ def _interpolate_allele_probabilities(
assert x == len(imputed_cm)
weights, _ = get_weights(genotyped_cm, imputed_cm)
assert x == len(weights)
i_hap_probs = np.zeros((x, 2), dtype=np.float64)
i_hap_probs = np.zeros((x, 2), dtype=np.float32)
for i in np.arange(x):
# Identify the genotyped markers k and k + 1 sandwiching ungenotyped marker i.
if ungenotyped_pos[i] < genotyped_pos[0]:
Expand Down Expand Up @@ -426,7 +426,7 @@ def compute_state_probability_matrix(fm, bm):
assert not np.any(bm < 0), "Backward probability matrix has negative values."
assert not np.any(np.isnan(bm)), "Backward probability matrix has NaN values."
# m + 1 columns to allow for imputed markers right of the last genotyped marker.
sm = np.zeros((m + 1, h), dtype=np.float64)
sm = np.zeros((m + 1, h), dtype=np.float32)
sm[:-1, :] = np.multiply(fm, bm)
sm[m, :] = sm[m - 1, :] # Not in BEAGLE
return sm
Expand Down Expand Up @@ -464,7 +464,7 @@ def interpolate_allele_probabilities(sm, ref_h, genotyped_pos, ungenotyped_pos):
weights, marker_interval_start = get_weights(genotyped_pos, ungenotyped_pos)
assert x == len(weights)
assert x == len(marker_interval_start)
p = np.zeros((x, len(alleles)), dtype=np.float64)
p = np.zeros((x, len(alleles)), dtype=np.float32)
# Compute allele probabilities as per Equation 1 in BB2016.
for a in alleles:
# Going from left to right.
Expand Down
16 changes: 8 additions & 8 deletions python/tests/beagle_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def get_weights(genotyped_pos, ungenotyped_pos):
genotyped_cm = convert_to_genetic_map_position(genotyped_pos)
ungenotyped_cm = convert_to_genetic_map_position(ungenotyped_pos)
# Calculate weights for ungenotyped markers.
weights = np.zeros(x, dtype=np.float64)
weights = np.zeros(x, dtype=np.float32)
# Identify genotype markers k and k + 1 sandwiching ungenotyped marker i.
marker_interval_start = np.zeros(x, dtype=np.int64)
marker_interval_start = np.zeros(x, dtype=np.int32)
idx_m = m - 1
# Going from right to left.
for idx_x in np.arange(x - 1, -1, -1):
Expand Down Expand Up @@ -86,7 +86,7 @@ def get_mismatch_prob(pos, miscall_rate):
"""
if miscall_rate >= 0.5:
miscall_rate = 0.5
mu = np.zeros(len(pos), dtype=np.float64) + miscall_rate
mu = np.zeros(len(pos), dtype=np.float32) + miscall_rate
return mu


Expand All @@ -107,7 +107,7 @@ def get_switch_prob(pos, h, ne):
"""
# Get genetic distances between adjacent markers.
cm = convert_to_genetic_map_position(pos)
d = np.zeros(len(pos), dtype=np.float64)
d = np.zeros(len(pos), dtype=np.float32)
d[0] = cm[0]
d[1:] = cm[1:] - cm[:-1]
rho = -np.expm1(-(4 * ne * d / h))
Expand All @@ -131,7 +131,7 @@ def compute_forward_probability_matrix(ref_h, query_h, rho, mu):
"""
m = ref_h.shape[0]
h = ref_h.shape[1]
fm = np.zeros((m, h), dtype=np.float64)
fm = np.zeros((m, h), dtype=np.float32)
last_sum = 1.0 # Part of normalization factor
for i in np.arange(m):
# Get site-specific parameters
Expand Down Expand Up @@ -173,7 +173,7 @@ def compute_backward_probability_matrix(ref_h, query_h, rho, mu):
"""
m = ref_h.shape[0]
h = ref_h.shape[1]
bm = np.zeros((m, h), dtype=np.float64)
bm = np.zeros((m, h), dtype=np.float32)
bm[-1, :] = 1.0 / h # Initialise the last column
for i in np.arange(m - 2, -1, -1):
query_a = query_h[i + 1]
Expand Down Expand Up @@ -211,7 +211,7 @@ def compute_state_probability_matrix(fm, bm):
m = fm.shape[0]
h = bm.shape[1]
# m + 1 columns to allow for imputed markers right of the last genotyped marker.
sm = np.zeros((m + 1, h), dtype=np.float64)
sm = np.zeros((m + 1, h), dtype=np.float32)
sm[:-1, :] = np.multiply(fm, bm)
sm[m, :] = sm[m - 1, :] # Not in BEAGLE
return sm
Expand Down Expand Up @@ -242,7 +242,7 @@ def interpolate_allele_probabilities(sm, ref_h, genotyped_pos, ungenotyped_pos):
alleles = np.arange(4) # ACGT encoding
x = len(ungenotyped_pos)
weights, marker_interval_start = get_weights(genotyped_pos, ungenotyped_pos)
p = np.zeros((x, len(alleles)), dtype=np.float64)
p = np.zeros((x, len(alleles)), dtype=np.float32)
for a in alleles:
for i in np.arange(x):
# Identify genotyped markers k and k + 1 sandwiching ungenotyped marker i.
Expand Down

0 comments on commit 9c90522

Please sign in to comment.