Skip to content

Commit

Permalink
Revert "Remove numpy dependency"
Browse files Browse the repository at this point in the history
This reverts commit 72e0b74.
  • Loading branch information
martinghunt committed Sep 27, 2023
1 parent 72e0b74 commit 701d599
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:
with:
update: true
install: >-
autotools gcc git libcurl-devel bzip2 liblzma zip libbz2-devel xz zlib zlib-devel libtool wget liblzma-devel ncurses-devel base-devel openssl-devel mingw64/mingw-w64-x86_64-python3 mingw64/mingw-w64-x86_64-python3-pip mingw-w64-x86_64-python3-setuptools mingw-w64-x86_64-python3-wheel mingw-w64-x86_64-python-biopython
autotools gcc git libcurl-devel bzip2 liblzma zip libbz2-devel xz zlib zlib-devel libtool wget liblzma-devel ncurses-devel base-devel openssl-devel mingw64/mingw-w64-x86_64-python3 mingw64/mingw-w64-x86_64-python3-pip mingw-w64-x86_64-python3-numpy mingw-w64-x86_64-python3-setuptools mingw-w64-x86_64-python3-wheel mingw-w64-x86_64-python-biopython
- name: Check out code for the build
uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PyVCF3>=1.0.3
requests>=2.27.1
mongoengine>=0.24.1
cython>=0.29.28
numpy>=1.22.0
## Testing
pytest
pytest-cov
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def read(*names, **kwargs):
"PyVCF3",
"mongoengine",
"requests",
"numpy",
],
entry_points={
"console_scripts": [
Expand Down
13 changes: 6 additions & 7 deletions src/mykrobe/cmds/amr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import copy
import json
import logging
import math
import random
import sys
import tempfile
import time

import numpy as np

from mykrobe import ONT_E_RATE, ONT_PLOIDY
from mykrobe.metagenomics import AMRSpeciesPredictor
from mykrobe.mformat import json_to_csv
Expand All @@ -22,8 +23,6 @@
from mykrobe.typing.typer.variant import VariantTyper
from mykrobe.utils import fix_amino_acid_X_variants_keys
from mykrobe.utils import load_json
from mykrobe.utils import binomial_random_sample
from mykrobe.utils import poisson_random_sample
from mykrobe.version import __version__ as atlas_version
from mykrobe.version import __version__ as predictor_version

Expand Down Expand Up @@ -76,9 +75,9 @@ def _get_incorrect_kmer_percent_cov(cls, k_count, incorrect_kmer_to_pc_cov):
return 0

def _simulate_snps(self):
correct_covg = poisson_random_sample(self.mean_depth, self.iterations)
incorrect_covg = binomial_random_sample(
self.mean_depth, self.error_rate, self.iterations
correct_covg = np.random.poisson(lam=self.mean_depth, size=self.iterations)
incorrect_covg = np.random.binomial(
self.mean_depth, self.error_rate, size=self.iterations
)
probe_coverage_list = []
vtyper = VariantTyper(
Expand Down Expand Up @@ -123,7 +122,7 @@ def _simulate_snps(self):
)
call = vtyper.type(vpc)

cov = math.log10(correct_covg[i] + incorrect_covg[i])
cov = np.log10(correct_covg[i] + incorrect_covg[i])
conf = call["info"]["conf"]
self.log_conf_and_covg.append((conf, cov))

Expand Down
36 changes: 0 additions & 36 deletions src/mykrobe/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import gzip
import hashlib
import json
import math
import os
import random
import re
from pathlib import Path
from typing import TextIO, Union
Expand Down Expand Up @@ -192,37 +190,3 @@ def get_first_chrom_name(fp: TextIO) -> str:
if not header.startswith(">"):
raise ValueError(f"Expected fasta file, but it did not start with '>'")
return header[1:].split()[0]


def poisson_random(lam):
# see https://en.wikipedia.org/wiki/Poisson_distribution#Random_variate_generation
L = math.exp(-lam)
k = 0
p = 1
while p > L:
k += 1
p *= random.uniform(0, 1)
return k - 1


def poisson_random_sample(lam, size):
return [poisson_random(lam) for _ in range(size)]


def binomial_random(n, p):
if not 0 <= p <= 1:
raise ValueError(f"Must have 0<=p<=1. p={p}")
if n <= 0:
raise ValueError(f"Must have n>0. n={n}")

successes = 0

for _ in range(n):
if random.random() < p:
successes += 1

return successes


def binomial_random_sample(n, p, size):
return [binomial_random(n, p) for _ in range(size)]

0 comments on commit 701d599

Please sign in to comment.