Closed cisaacstern closed 3 months ago
A little more detail on import time of the various components:
from ecoscope import analyis
➡️ 4sfrom ecoscope import base
➡️ circular import w analysis, can't be imported on it's own (side note: this seems like its own issue, i would think things named base
don't have other dependencies within the package)from ecoscope import contrib
➡️ 0.2sfrom ecoscope import io
➡️ 0.98sfrom ecoscope import mapping
➡️ 0.58sfrom ecoscope import plotting
➡️ 1.92s4 + 0.2 + 0.98 + 0.58 + 1.92 = 7.68 seconds (so these numbers check out)
I am going to see if I can reduce import time and open a PR for review 😃
Note, for now to unblock development of #141, I've just commented out this line:
I can revisit a more general fix soon!
Deferring import of mapclassify
saves about 1.5s
diff --git a/ecoscope/analysis/speed.py b/ecoscope/analysis/speed.py
index 5873b72..825da98 100644
--- a/ecoscope/analysis/speed.py
+++ b/ecoscope/analysis/speed.py
@@ -1,7 +1,6 @@
import typing
import geopandas as gpd
-import mapclassify
import pandas as pd
import shapely
@@ -64,7 +63,16 @@ def apply_classification(x, k, cls_method="natural_breaks", multiples=[-2, -1, 1
multiples : Listlike
The multiples of the standard deviation to add/subtract from the sample mean to define the bins. defaults=
"""
-
+ import mapclassify
+
+ classification_methods = {
+ "equal_interval": mapclassify.EqualInterval,
+ "natural_breaks": mapclassify.NaturalBreaks,
+ "quantile": mapclassify.Quantiles,
+ "std_mean": mapclassify.StdMean,
+ "max_breaks": mapclassify.MaximumBreaks,
+ "fisher_jenks": mapclassify.FisherJenks,
+ }
classifier = classification_methods.get(cls_method)
if not classifier:
return
@@ -82,12 +90,3 @@ default_speed_colors = [
"#fc8d59",
"#d73027",
]
-
-classification_methods = {
- "equal_interval": mapclassify.EqualInterval,
- "natural_breaks": mapclassify.NaturalBreaks,
- "quantile": mapclassify.Quantiles,
- "std_mean": mapclassify.StdMean,
- "max_breaks": mapclassify.MaximumBreaks,
- "fisher_jenks": mapclassify.FisherJenks,
-}
~
~
~
I've tested the following:
analysis/astronomy.py
into base/utils.py
to solve the circular import issuemapclassify
and astroplan
ecoscope/__init__.py
so that analysis
is loaded lastThis reduces the import time to around ~2.7 seconds.
Deferring the load of ecoscope.analysis.UD
and ecoscope.mapping
(removing their imports from the relevant __init__.py
s) gets this down to ~1.3 seconds. This means one would have to explicitly import these modules in addition to import ecoscope
.
Deferring the expensive imports isn't exactly PEP8 ApprovedTM, so I think playing around with lazy imports is worthwhile.
Also learned that tuna for visualizing import profiler outputs. (See the pretty screenshot)
amazing progress!
Currently importing ecoscope takes ~6 seconds.
If I comment out this line
https://github.com/wildlife-dynamics/ecoscope/blob/b3b9d1543eee5357ef9b2df8a4135d47d40f39d4/ecoscope/__init__.py#L1
that is reduced to 0.3 seconds:
IMHO 6 seconds is too long to wait for ecoscope to import. An example of why this matters is in test development: if test session takes a minimum of 6 seconds to initialize, that adds up to a lot of developer time over the course of hundreds or thousands of test invocations.
I am going to see if I can reduce import time and open a PR for review 😃