Skip to content

Commit

Permalink
More test import speedups (~4x for CLI commands w/ models)
Browse files Browse the repository at this point in the history
  • Loading branch information
boydgreenfield authored and polyatail committed Jul 19, 2019
1 parent d420aec commit 0496ef5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
7 changes: 6 additions & 1 deletion onecodex/distance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pandas as pd
import skbio.diversity

from onecodex.exceptions import OneCodexException
from onecodex.taxonomy import TaxonomyMixin
Expand All @@ -20,6 +19,8 @@ def alpha_diversity(self, metric="simpson", rank="auto"):
-------
pandas.DataFrame, a distance matrix.
"""
import skbio.diversity

if metric not in ("simpson", "chao1", "shannon"):
raise OneCodexException(
"For alpha diversity, metric must be one of: simpson, chao1, shannon"
Expand Down Expand Up @@ -55,6 +56,8 @@ def beta_diversity(self, metric="braycurtis", rank="auto"):
-------
skbio.stats.distance.DistanceMatrix, a distance matrix.
"""
import skbio.diversity

if metric not in ("jaccard", "braycurtis", "cityblock"):
raise OneCodexException(
"For beta diversity, metric must be one of: jaccard, braycurtis, cityblock"
Expand Down Expand Up @@ -88,6 +91,8 @@ def unifrac(self, weighted=True, rank="auto"):
skbio.stats.distance.DistanceMatrix, a distance matrix.
"""
# needs read counts, not relative abundances
import skbio.diversity

if self._guess_normalized():
raise OneCodexException("UniFrac requires unnormalized read counts.")

Expand Down
4 changes: 3 additions & 1 deletion onecodex/viz/_bargraph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import altair as alt
from onecodex.exceptions import OneCodexException


Expand Down Expand Up @@ -65,6 +64,9 @@ def plot_bargraph(
>>> plot_bargraph(rank='genus', top_n=10)
"""
# Deferred imports
import altair as alt

if rank is None:
raise OneCodexException("Please specify a rank or 'auto' to choose automatically")

Expand Down
32 changes: 22 additions & 10 deletions onecodex/viz/_distance.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
# -*- coding: utf-8 -*-
import altair as alt
from itertools import chain
import numpy as np
import pandas as pd
from scipy.cluster import hierarchy
from scipy.spatial.distance import squareform
from scipy.stats import pearsonr
from skbio.stats import ordination
from sklearn import manifold
from sklearn.metrics.pairwise import euclidean_distances
import warnings

from onecodex.exceptions import OneCodexException
from onecodex.distance import DistanceMixin
from onecodex.viz import dendrogram


class VizDistanceMixin(DistanceMixin):
Expand Down Expand Up @@ -43,6 +33,10 @@ def _compute_distance(self, rank, metric):
return distances

def _cluster_by_sample(self, rank="auto", metric="braycurtis", linkage="average"):
from scipy.cluster import hierarchy
from scipy.spatial.distance import squareform
from sklearn.metrics.pairwise import euclidean_distances

if metric == "euclidean":
dist_matrix = euclidean_distances(self._results).round(6)
else:
Expand All @@ -59,6 +53,10 @@ def _cluster_by_sample(self, rank="auto", metric="braycurtis", linkage="average"
}

def _cluster_by_taxa(self, linkage="average"):
from scipy.cluster import hierarchy
from scipy.spatial.distance import squareform
from sklearn.metrics.pairwise import euclidean_distances

dist_matrix = euclidean_distances(self._results.T).round(6)
clustering = hierarchy.linkage(squareform(dist_matrix), method=linkage)
scipy_tree = hierarchy.dendrogram(clustering, no_plot=True)
Expand Down Expand Up @@ -116,6 +114,11 @@ def plot_distance(
>>> plot_distance(rank='genus', metric='unifrac')
"""
import altair as alt
import numpy as np
import pandas as pd
from onecodex.viz import dendrogram

if len(self._results) < 2:
raise OneCodexException(
"`plot_distance` requires 2 or more valid classification results."
Expand Down Expand Up @@ -263,6 +266,15 @@ def plot_mds(
they truly represent the calculated distances. They do not reflect how well the distance
metric captures similarities between the underlying data (in this case, an OTU table).
"""
import altair as alt
import numpy as np
import pandas as pd
from scipy.spatial.distance import squareform
from scipy.stats import pearsonr
from skbio.stats import ordination
from sklearn import manifold
from sklearn.metrics.pairwise import euclidean_distances

if len(self._results) < 2:
raise OneCodexException("`plot_mds` requires 2 or more valid classification results.")

Expand Down
7 changes: 4 additions & 3 deletions onecodex/viz/_heatmap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import altair as alt
import pandas as pd

from onecodex.exceptions import OneCodexException


Expand Down Expand Up @@ -77,6 +74,10 @@ def plot_heatmap(
>>> plot_heatmap(rank='family', top_n=10)
"""
# Deferred imports
import altair as alt
import pandas as pd

if rank is None:
raise OneCodexException("Please specify a rank or 'auto' to choose automatically")

Expand Down
9 changes: 5 additions & 4 deletions onecodex/viz/_metadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import altair as alt
import pandas as pd

from onecodex.exceptions import OneCodexException
from onecodex.viz import boxplot


class VizMetadataMixin(object):
Expand Down Expand Up @@ -67,6 +63,11 @@ def plot_metadata(
>>> plot_metadata(haxis=('allergy_dogs', 'allergy_cats'), vaxis='Bacteroides')
"""
# Deferred imports
import altair as alt
import pandas as pd
from onecodex.viz import boxplot

if rank is None:
raise OneCodexException("Please specify a rank or 'auto' to choose automatically")

Expand Down
11 changes: 6 additions & 5 deletions onecodex/viz/_pca.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import altair as alt
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA

from onecodex.exceptions import OneCodexException


Expand Down Expand Up @@ -72,6 +67,12 @@ def plot_pca(
>>> plot_pca(tooltip=['Bacteroides', 'Prevotella', 'Bifidobacterium'])
"""
# Deferred imports
import altair as alt
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA

if rank is None:
raise OneCodexException("Please specify a rank or 'auto' to choose automatically")

Expand Down

0 comments on commit 0496ef5

Please sign in to comment.