Skip to content

Commit

Permalink
Merge pull request #346 from NeuroML/feat/warn-optional-deps
Browse files Browse the repository at this point in the history
feat(optional-deps): add warnings to inform users of optional packages
  • Loading branch information
sanjayankur31 authored Apr 23, 2024
2 parents b61cbe4 + 21918cb commit 3a42361
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 73 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ repos:
rev: v0.4.1
hooks:
- id: ruff
args: [ --exit-zero ]
- id: ruff-format
15 changes: 10 additions & 5 deletions pyneuroml/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
from pyneuroml.utils.plot import get_next_hex_color
from pyneuroml.plot import generate_plot
import neuroml as nml
from pyelectro.analysis import max_min
from pyelectro.analysis import mean_spike_frequency

from typing import Optional


logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from pyelectro.analysis import max_min
from pyelectro.analysis import mean_spike_frequency
except ImportError:
logger.warning("Please install optional dependencies to use analysis features:")
logger.warning("pip install pyneuroml[analysis]")


def generate_current_vs_frequency_curve(
Expand Down Expand Up @@ -202,9 +209,7 @@ def generate_current_vs_frequency_curve(
stims = []
if len(custom_amps_nA) > 0:
stims = [float(a) for a in custom_amps_nA]
stim_info = [
"%snA" % float(a) for a in custom_amps_nA
] # type: typing.Union[str, typing.List[str]]
stim_info = ["%snA" % float(a) for a in custom_amps_nA] # type: typing.Union[str, typing.List[str]]
else:
# else generate a list using the provided arguments
amp = start_amp_nA
Expand Down
11 changes: 7 additions & 4 deletions pyneuroml/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
Copyright 2024 NeuroML contributors
"""


import logging
import pprint
import typing

from lxml import etree
from rdflib import RDF, BNode, Graph, Literal, Namespace, URIRef
from rdflib.namespace import DC, DCTERMS, FOAF, RDF, RDFS

from pyneuroml.utils.xml import _find_elements, _get_attr_in_element

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from rdflib import BNode, Graph, Literal, Namespace, URIRef
from rdflib.namespace import DC, DCTERMS, FOAF, RDFS
except ImportError:
logger.warning("Please install optional dependencies to use annotation features:")
logger.warning("pip install pyneuroml[annotations]")


# From https://docs.biosimulations.org/concepts/conventions/simulation-project-metadata/
PREDICATES_MAP = {
Expand Down
15 changes: 11 additions & 4 deletions pyneuroml/biosimulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
Copyright 2024 NeuroML contributors
"""


import logging
import typing
from datetime import datetime

import requests
from pydantic import BaseModel
from requests_toolbelt.multipart.encoder import MultipartEncoder

from pyneuroml import __version__
from pyneuroml.annotations import create_annotation
Expand All @@ -24,6 +21,16 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

try:
from pydantic import BaseModel
from requests_toolbelt.multipart.encoder import MultipartEncoder
except ImportError:
logger.warning(
"Please install optional dependencies to use Biosimulation.org features:"
)
logger.warning("pip install pyneuroml[combine]")


biosimulators_api_url = "https://api.biosimulators.org"
biosimulations_api_url = "https://api.biosimulations.org"

Expand Down Expand Up @@ -59,7 +66,7 @@ def get_simulator_versions(
"xpp",
"brian2",
"copasi",
]
],
) -> typing.Dict[str, typing.List[str]]:
"""Get simulator list from biosimulators.
Expand Down
11 changes: 7 additions & 4 deletions pyneuroml/neuron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
from pyneuroml.pynml import validate_neuroml1
from pyneuroml.pynml import validate_neuroml2

from pyneuroml.neuron.nrn_export_utils import set_erev_for_mechanism
from neuron import h


pp = pprint.PrettyPrinter(depth=4)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from neuron import h
from pyneuroml.neuron.nrn_export_utils import set_erev_for_mechanism
except ImportError:
logger.warning("Please install optional dependencies to use NEURON features:")
logger.warning("pip install pyneuroml[neuron]")


def get_utils_hoc() -> pathlib.Path:
"""Get full path of utils.hoc file
Expand Down
12 changes: 10 additions & 2 deletions pyneuroml/neuron/nrn_export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
"""

import logging

from neuron import h
from nrn import *
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from neuron import h
from nrn import *
except ImportError:
logger.warning("Please install optional dependencies to use NEURON features:")
logger.warning("pip install pyneuroml[neuron]")


def clear_neuron() -> None:
Expand Down
8 changes: 6 additions & 2 deletions pyneuroml/nsgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
Copyright 2024 NeuroML contributors
"""


import logging
import os
import pathlib
import typing
from zipfile import ZipFile

from pyneuroml.runners import generate_sim_scripts_in_folder
from pynsgr.commands.nsgr_submit import nsgr_submit

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

try:
from pynsgr.commands.nsgr_submit import nsgr_submit
except ImportError:
logger.warning("Please install optional dependencies to use NSG features:")
logger.warning("pip install pyneuroml[nsgr]")


def run_on_nsg(
engine: str,
Expand Down
7 changes: 6 additions & 1 deletion pyneuroml/plot/PlotMorphologyPlotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
import os
import typing
import logging
import plotly.graph_objects as go
from neuroml import Cell, NeuroMLDocument
from pyneuroml.pynml import read_neuroml2_file

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
import plotly.graph_objects as go
except ImportError:
logger.warning("Please install optional dependencies to use plotly features:")
logger.warning("pip install pyneuroml[plotly]")


def plot_3D_cell_morphology_plotly(
nml_file: typing.Union[str, Cell, NeuroMLDocument],
Expand Down
17 changes: 11 additions & 6 deletions pyneuroml/plot/PlotMorphologyVispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Copyright 2023 NeuroML contributors
"""


import logging
import math
import random
Expand All @@ -28,16 +27,22 @@
load_minimal_morphplottable__model,
)
from scipy.spatial.transform import Rotation
from vispy import app, scene, use
from vispy.geometry.generation import create_sphere
from vispy.geometry.meshdata import MeshData
from vispy.scene.visuals import InstancedMesh
from vispy.util.transforms import rotate
from typing import Optional

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
from vispy import app, scene, use
from vispy.geometry.generation import create_sphere
from vispy.geometry.meshdata import MeshData
from vispy.scene.visuals import InstancedMesh
from vispy.util.transforms import rotate
except ImportError:
logger.warning("Please install optional dependencies to use vispy features:")
logger.warning("pip install pyneuroml[vispy]")
logger.warning("or (for Qt5):")
logger.warning("pip install pyneuroml[vispy-qt5]")

VISPY_THEME = {
"light": {"bg": "white", "fg": "black"},
Expand Down
10 changes: 8 additions & 2 deletions pyneuroml/plot/PlotSpikes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Copyright 2023 NeuroML contributors
"""

import argparse
import logging
import os
Expand All @@ -18,6 +19,13 @@
from pyneuroml.utils.cli import build_namespace

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
import tables # pytables for HDF5 support
except ImportError:
logger.warning("Please install optional dependencies to use hdf5 features:")
logger.warning("pip install pyneuroml[hdf5]")

FORMAT_ID_T = "id_t"
FORMAT_ID_TIME_NEST_DAT = "id_t_nest_dat"
Expand Down Expand Up @@ -121,8 +129,6 @@ def read_sonata_spikes_hdf5_file(file_name: str):
full_path = os.path.abspath(file_name)
logger.info("Loading SONATA spike times from: %s (%s)" % (file_name, full_path))

import tables # pytables for HDF5 support

h5file = tables.open_file(file_name, mode="r")

sorting = (
Expand Down
11 changes: 9 additions & 2 deletions pyneuroml/povray/MakeMovie.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import cv2
import colorsys
import argparse
import sys
import os.path
import logging


"""
STILL IN DEVELOPMENT
Expand All @@ -18,6 +16,15 @@


logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
import cv2
except ImportError:
logger.warning("Please install optional dependencies to use povray features:")
logger.warning("pip install pyneuroml[povray]")


scale_font = 1

font = cv2.FONT_HERSHEY_COMPLEX_SMALL
Expand Down
15 changes: 13 additions & 2 deletions pyneuroml/sbml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@

import os
import errno
import libsbml
from libsbml import SBMLReader
from typing import List


import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
import libsbml
from libsbml import SBMLReader
except ImportError:
logger.warning("Please install optional dependencies to use SBML features:")
logger.warning("pip install pyneuroml[combine]")


def validate_sbml_files(input_files: List[str], strict_units: bool = False) -> bool:
"""Validate input files using libsbml.SBMLDocument.checkConsistency
Expand Down
23 changes: 19 additions & 4 deletions pyneuroml/tellurium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
run a model using the tellurium engine
"""

import os
import logging
from pyneuroml.sedml import validate_sedml_files
import tellurium as te

te.setDefaultPlottingEngine("matplotlib")

import os
import libsedml
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

try:
import tellurium as te

te.setDefaultPlottingEngine("matplotlib")
except ImportError:
logger.warning("Please install optional dependencies to use Tellurium features:")
logger.warning("pip install pyneuroml[tellurium]")

try:
import libsedml
except ImportError:
logger.warning("Please install optional dependencies to use SED-ML features:")
logger.warning("pip install pyneuroml[combine]")


# # For technical reasons, any software which uses libSEDML
# # must provide a custom build - Tellurium uses tesedml
Expand Down
Loading

0 comments on commit 3a42361

Please sign in to comment.