|
15 | 15 |
|
16 | 16 |
|
17 | 17 | # load library |
18 | | -import anndata as ad |
19 | | -import bioio_base |
20 | | -from bioio.writers import OmeTiffWriter |
21 | | -import geopandas as gpd |
22 | 18 | import matplotlib.pyplot as plt |
23 | 19 | from matplotlib import cm |
24 | 20 | from matplotlib import colors |
25 | 21 | try: |
26 | 22 | import muspan as ms |
27 | 23 | except ModuleNotFoundError: |
28 | 24 | ms = None |
29 | | -import networkx as nx |
30 | | -import neuroglancer |
31 | 25 | import numpy as np |
32 | 26 | import os |
33 | 27 | import pandas as pd |
34 | 28 | from pcdl import imagine |
35 | 29 | from pcdl import pdplt |
36 | | -from pcdl import neuromancer |
| 30 | +from pcdl.dependency import optional_import |
37 | 31 | from scipy import io |
38 | 32 | from scipy import sparse |
39 | | -import shapely |
40 | | -import spatialdata as sd |
41 | 33 | import sys |
42 | | -import vtk |
43 | 34 | import warnings |
44 | 35 | import xml.etree.ElementTree as etree |
45 | 36 | from pcdl.VERSION import __version__ |
@@ -223,6 +214,10 @@ def render_neuroglancer(tiffpathfile, timestep=0, intensity_cmap='gray'): |
223 | 214 | function to load a time step from an ome tiff files, generated |
224 | 215 | with make_ome_tiff, into neuroglancer. |
225 | 216 | """ |
| 217 | + # load optional dependencies (neuromancer itself lazy loads bioio and scikit-image) |
| 218 | + neuroglancer = optional_import('neuroglancer', s_caller='pcdl.render_neuroglancer') |
| 219 | + from pcdl import neuromancer |
| 220 | + |
226 | 221 | # start neuroglancer |
227 | 222 | viewer = neuroglancer.Viewer() |
228 | 223 | with viewer.txn() as state: |
@@ -1404,6 +1399,9 @@ def make_conc_vtk(self, ext='_conc.vtr'): |
1404 | 1399 |
|
1405 | 1400 | https://www.paraview.org/ |
1406 | 1401 | """ |
| 1402 | + # load optional dependency |
| 1403 | + vtk = optional_import('vtk', s_caller='TimeStep.make_conc_vtk') |
| 1404 | + |
1407 | 1405 | # off we go. |
1408 | 1406 | s_vtkfile = self.xmlfile.replace('.xml', ext) |
1409 | 1407 | if self.verbose: |
@@ -1896,6 +1894,9 @@ def make_cell_vtk(self, attribute=['cell_type'], ext='_cell.vtp'): |
1896 | 1894 |
|
1897 | 1895 | https://www.paraview.org/ |
1898 | 1896 | """ |
| 1897 | + # load optional dependency |
| 1898 | + vtk = optional_import('vtk', s_caller='TimeStep.make_cell_vtk') |
| 1899 | + |
1899 | 1900 | # off we go. |
1900 | 1901 | s_vtkfile = self.xmlfile.replace('.xml', ext) |
1901 | 1902 | if self.verbose: |
@@ -2031,6 +2032,11 @@ def make_ome_tiff(self, cell_attribute='ID', conc_cutoff={}, focus=None, file=Tr |
2031 | 2032 | https://napari.org/stable/ |
2032 | 2033 | https://fiji.sc/ |
2033 | 2034 | """ |
| 2035 | + # load optional dependencies |
| 2036 | + if file: |
| 2037 | + OmeTiffWriter = optional_import('bioio.writers', s_attr='OmeTiffWriter', s_pip='bioio', s_caller='TimeStep.make_ome_tiff') |
| 2038 | + bioio_base = optional_import('bioio_base', s_pip='bioio', s_caller='TimeStep.make_ome_tiff') |
| 2039 | + |
2034 | 2040 | # handle channels |
2035 | 2041 | ls_substrate = self.get_substrate_list() |
2036 | 2042 | ls_celltype = self.get_celltype_list() |
@@ -2404,6 +2410,9 @@ def get_anndata(self, values=1, drop=set(), keep=set(), scale='maxabs'): |
2404 | 2410 | function to transform a mcds time step into an anndata object |
2405 | 2411 | for downstream analysis. |
2406 | 2412 | """ |
| 2413 | + # load optional dependency |
| 2414 | + ad = optional_import('anndata', s_caller='TimeStep.get_anndata') |
| 2415 | + |
2407 | 2416 | # processing |
2408 | 2417 | if self.verbose: |
2409 | 2418 | print(f'processing: 1/1 {round(self.get_time(),9)}[min] mcds into anndata obj.') |
@@ -2479,6 +2488,12 @@ def get_spatialdata(self, images={'subs'}, labels={}, points={'subs'}, shapes={' |
2479 | 2488 | function to transform a mcds time step into |
2480 | 2489 | a spatialdata object for downstream analysis. |
2481 | 2490 | """ |
| 2491 | + # load optional dependencies |
| 2492 | + ad = optional_import('anndata', s_caller='TimeStep.get_spatialdata') |
| 2493 | + sd = optional_import('spatialdata', s_caller='TimeStep.get_spatialdata') |
| 2494 | + shapely = optional_import('shapely', s_caller='TimeStep.get_spatialdata') |
| 2495 | + gpd = optional_import('geopandas', s_caller='TimeStep.get_spatialdata') |
| 2496 | + |
2482 | 2497 | # set table spatial element links |
2483 | 2498 | s_region_subs = None |
2484 | 2499 | s_region_cell = None |
@@ -2720,9 +2735,12 @@ def get_muspan(self, z_slice=None, values=1, drop=set(), keep=set()): |
2720 | 2735 | + https://docs.muspan.co.uk/latest/Documentation.html |
2721 | 2736 | """ |
2722 | 2737 | # check if muspan library is installed |
2723 | | - if ms is None: |
| 2738 | + if (ms is None) or (ms.__file__ is None): |
2724 | 2739 | sys.exit(f'Error @ TimeStep.get_muspa : the muspan Multi Spatial Analysis python3 library is not installed!\nfor instructions check out : https://www.muspan.co.uk/') |
2725 | 2740 |
|
| 2741 | + # load optional dependency |
| 2742 | + nx = optional_import('networkx', s_caller='TimeStep.get_muspan') |
| 2743 | + |
2726 | 2744 | # get conc and cell dataframe |
2727 | 2745 | df_conc = self.get_conc_df(values=values, drop=drop, keep=keep) |
2728 | 2746 | df_cell = self.get_cell_df(values=values, drop=drop, keep=keep) |
|
0 commit comments