Skip to main content

User-friendly tools for accessing paths, metadata and assets related to AIND sessions.

Project description

aind-session

User-friendly tools for accessing paths, metadata and assets related to AIND sessions.

PyPI Python version

Coverage CI/CD GitHub issues

Under development!

Please check this out and make feature requests, but don't rely on the API to remain stable just yet..

Aim

This package is meant to provide easy access to session information needed for common tasks, in CodeOcean and beyond.

  • when interacting with the CodeOcean API, it uses and returns objects from the official Python library - we will avoid duplicating functionality provided by that package, except to make convenience functions with assumptions baked-in (for example, getting a client with environment variables and a default domain; finding all the assets for a particular session)
  • the core Session class should have a minimal set of methods and attributes that are common to sessions from all platforms - it should be fast to initialize and not do unnecessary work
  • extensions provide additional functionality (e.g. for specific modalities, metadata, databases) - at the moment, this is implemented via registration of namespaces (like Pandas), which allows for extending without subclassing
  • when searching for session data or information, methods should be exhaustive: for example, as naming conventions change, this package should support current and previous versions of names
  • when searching is unsuccessful, as much information as possible should be provided to the user via logging messages and exceptions, so they can understand the reasons for failure

Usage

User secrets

Credentials are required for:

  • AWS
    • in a capsule, use the AWS Assumable Role - aind-codeocean-user secret
    • alternatively, environment variables or a config file will be found automatically (see boto3 docs)
  • CodeOcean API
    • an access token is required with at least Datasets: Read scope (see CodeOcean docs on how to create one)
    • in a capsule, this can be found under the API credentials secret
    • alternatively, CODE_OCEAN_API_TOKEN is the preferred environment variable name
      • if not found, the first environment variable with a value starting with COP_ is used (case-insensitive)
      • the domain name defaults to https://codeocean.allenneuraldynamics.org, but can be overridden with a CODE_OCEAN_DOMAIN environment variable

For development, environment variables can be provided in a .env file in the project root directory or the user's home directory.

Install

pip install aind_session

Python

>>> import aind_session

# Common attributes available for all sessions:
>>> session = aind_session.Session('ecephys_676909_2023-12-13_13-43-40')
>>> session.platform
'ecephys'
>>> session.subject_id
'676909'
>>> session.dt
datetime.datetime(2023, 12, 13, 13, 43, 40)
>>> session.raw_data_asset.id
'16d46411-540a-4122-b47f-8cb2a15d593a'
>>> session.raw_data_dir.as_posix()
's3://aind-ephys-data/ecephys_676909_2023-12-13_13-43-40'
>>> session.modalities
('behavior', 'behavior_videos', 'ecephys')

# Additional functionality in namespace extensions:
>>> session.metadata.subject['genotype']
'Pvalb-IRES-Cre/wt;Ai32(RCL-ChR2(H134R)_EYFP)/wt'
>>> session.ecephys.sorted_data_asset.name
'ecephys_676909_2023-12-13_13-43-40_sorted_2024-03-01_16-02-45'

# Objects refer to the original session, regardless of how they were created:
>>> a = aind_session.Session('ecephys_676909_2023-12-13_13-43-40')
>>> b = aind_session.Session('ecephys_676909_2023-12-13_13-43-40_sorted_2024-03-01_16-02-45')
>>> assert a == b, "Objects are equal if they refer to the same session ID"

# Objects are also hashable and sortable (by their ID)

When working in a capsule, the Session object can be used to find or verify attached data assets:

>>> import os

>>> import aind_session
>>> import codeocean    # codeocean's python sdk for interacting with the api
>>> import upath        # works the same way as pathlib

# find all attached data dirs in the capsule:
>>> capsule_data_dir = upath.UPath('tests/resources/capsule_tree/data') # just '/data' in an actual capsule 
>>> attached_data_names = sorted(d.name for d in capsule_data_dir.iterdir())
>>> attached_data_names
['ecephys_676909_2023-12-11_14-24-35_sorted_2024-03-29_11-29-39', 'ecephys_676909_2023-12-13_13-43-40', 'ecephys_676909_2023-12-13_13-43-40_sorted_2024-03-01_16-02-45']

# get a list of unique sessions that have data attached to the capsule:
>>> attached_sessions = sorted(set(aind_session.Session(d.name) for d in capsule_data_dir.iterdir()))
>>> attached_sessions
[Session('ecephys_676909_2023-12-11_14-24-35'), Session('ecephys_676909_2023-12-13_13-43-40')]

# check that particular sessions have their raw data or latest sorted data assets attached:
>>> attached_sessions[0].ecephys.sorted_data_asset.name in attached_data_names
True
>>> attached_sessions[0].raw_data_asset.name in attached_data_names
False

# a missing asset could then be attached to the current capsule (although this might not be advisable in a "Reproducible run"):
>>> aind_session.get_codeocean_client().capsules.attach_data_assets(            # doctest: +SKIP
...     capsule_id=os.getenv('OS_CAPSULE_ID'),
...     attach_params=[
...         codeocean.data_asset.DataAssetAttachParams(
...             id=attached_sessions[0].raw_data_asset.id,
...         ),
...     ],
... )

Development

See instructions in CONTRIBUTING.md and the original template

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aind_session-0.1.7.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

aind_session-0.1.7-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file aind_session-0.1.7.tar.gz.

File metadata

  • Download URL: aind_session-0.1.7.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for aind_session-0.1.7.tar.gz
Algorithm Hash digest
SHA256 3d52728da3540b7d67ba7d201671b94b81620d0d043ec4d5e927d15045c91aa8
MD5 3b9297b3ed6bff00907292f3ae0e5786
BLAKE2b-256 1ee09d960fe30465ca2825dd81d8f05daf21d1f1bfc20530edfc6b8d64b13955

See more details on using hashes here.

File details

Details for the file aind_session-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: aind_session-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for aind_session-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 cf270e14478f449ad70d91da76bf71cc676744a662cbb1473b4472c18c8b9b6c
MD5 569cf34f1e334262c45d840d3a9b38d9
BLAKE2b-256 4fe559c492502d3b4118f2c496a7673ba3882c4207204d61c5faf51e75ecc533

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page