mibitracker package

Submodules

mibitracker.mibitracker_exceptions module

Provides a custom exception for MibiTracker API requests.

Copyright (C) 2021 Ionpath, Inc. All rights reserved.

exception mibitracker.mibitracker_exceptions.MibiTrackerError

Bases: Exception

Raise for exceptions where the response from the MibiTracker API is invalid or unexpected.

mibitracker.request_helpers module

Helper class for making and retrying requests to the MIBItracker.

Copyright (C) 2021 Ionpath, Inc. All rights reserved.

class mibitracker.request_helpers.MibiRequests(url, email=None, password=None, token=None, refresh=300, retries=3, retry_methods=('HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST'), retry_codes=(502, 503), session_timeout=10, data_transfer_timeout=30)

Bases: object

Helper class for making requests to the MIBItracker.

This is an opinionated way of using requests.Session with the following features:

  • In the case of specified HTTP errors, requests are retried.

  • All responses call raise_for_status().

  • An instance requests an authorization token upon initialization, and

    includes it in a default header for all future requests. No token refresh capabilities are built in, so if the token expires a new instance needs to be initialized.

Parameters:
  • url – The string url to the backend of a MIBItracker instance, e.g. 'https://backend-dot-mibitracker-demo.appspot.com'.

  • email – The string email address of your MIBItracker account.

  • password – The string password of your MIBItracker account.

  • token – A JSON Web Token (JWT) to validate a MIBItracker session.

  • refresh – Number of seconds since previous refresh to automatically refresh token. Defaults to 5 minutes. Set to 0 or None in order to not attempt refreshes.

  • retries – The max number of retries for HTTP status errors. Defaults to MAX_RETRIES which is set to 3.

  • retry_methods – The HTTP methods to retry. Defaults to RETRY_METHOD_WHITELIST which is the defaults to urllib3’s whitelist with the addition of POST.

  • retry_codes – The HTTP status codes to retry. Defaults to (502, 503), which are associated with transient errors seen on app engine.

  • session_timeout – Timeout for MIBItracker requests.

  • data_transfer_timeout – Timeout for data transfer requests.

url

The string url to the backend of a MIBItracker instance.

session

A StatusCheckedSession that includes an authorization header and automatically raises for HTTP status errors.

Raises:

HTTPError – Raised by requests.raise_for_status(), i.e. if a response’s status code is >= 400.

refresh()

Refreshes the authorization token stored in the session header.

Raises HTTP 400 if attempting to refresh an expired token.

get(route, *args, **kwargs)

Makes a GET request to the url using the session.

Parameters:
  • route – The route to add to the base url, such as '/images/' or '/tissues/?organ=tonsil'.

  • *args – Passed to requests.Session.get.

  • **kwargs – Passes to requests.Session.get.

Returns:

The response from requests.Session.get.

post(route, *args, **kwargs)

Makes a POST request to the url using the session.

Parameters:
  • route – The route to add to the base url, such as '/slides/1/'.

  • *args – Passed to requests.Session.post.

  • **kwargs – Passes to requests.Session.post.

Returns:

The response from requests.Session.post.

put(route, *args, **kwargs)

Makes a PUT request to the url using the session.

Parameters:
  • route – The route to add to the base url, such as '/images/1/'.

  • *args – Passed to requests.Session.put.

  • **kwargs – Passes to ``requests.Session.put`.

Returns:

The response from requests.Session.put.

delete(route, *args, **kwargs)

Makes a DELETE request to the url using the session.

Parameters:
  • route – The route to add to the base url, such as '/images/1/'.

  • *args – Passed to requests.Session.delete.

  • **kwargs – Passes to requests.Session.delete.

Returns:

The response from requests.Session.delete.

download_file(path)

Downloads a file from MIBItracker storage.

Parameters:

path – The path to the file in storage. This usually can be constructed from the run and image folders.

Returns: An open file object containing the downloaded file’s data,

rewound to the beginning of the file.

search_runs(run_name, run_label=None)

Searches for runs which have the name and optionally label.

Parameters:
  • run_name – The name of the run the image belongs to.

  • run_label – (optional) The label of the run.

Returns: A list of JSON data for each run that matches the search.

If only run_name is specified, this list could be of any length as a run’s name is not necessarily unique. If run_label is also specified, this is guaranteed to be unique and the returned list could either be of length zero or one.

copy_run(old_label, new_label, **kwargs)

Creates a copy of the run corresponding to the label.

Parameters:
  • old_label – The label of the run to copy.

  • new_label – The label to use for the copied run.

  • kwargs – Any key-value pair that can be included in the json when creating the new run to differentiate it from the old run, such as a different project id or FOV size (such as if images were cropped from their originals).

Returns:

A tuple of the response JSON returned by old run and from creating its copy, respectively.

copy_run_image_metadata(old_run_label, new_run_label, **kwargs)

This updates image metadata but not the actual TIFF data.

Each new image will remain unavailable until a TIFF is uploaded.

Parameters:
  • old_run_label – The label of the run whose images’ metadata is to be copied.

  • new_run_label – The label of the run whose images’ metadata will be updated. This run must already exist.

  • kwargs – Any key-value pair that can be included in the json when updating the the images to differentiate them from the old runs’, such as a different project id or frame (such as if images were cropped from their originals).

Returns:

A dictionary with keys of the original image IDs, and values of the response JSON returned when updating the new images.

upload_mibitiff(tiff_file, run_id=None)

Uploads a single TIFF to the MIBItracker.

This uses the ‘run’ and ‘folder’ fields in the MibiTiff’s description to associate this with the correct image.

Parameters:
  • tiff_file – A string path to a TIFF file or an open file object containing the TIFF data.

  • run_id – The ID of the run the image is from. This enables checking of the image metadata against expected values and may become mandatory in the future.

Returns:

The response from the MIBItracker after uploading the file and queuing it to be processed into viewable data.

Raises:
  • TypeError – Raised if tiff_file is not a string path or file object.

  • ValueError – Raised if run_id is None.

upload_channel(image_id, image_file, filename=None)

Uploads a grayscale PNG or TIFF to the MIBItracker.

Parameters:
  • image_id – The integer id of the image to associate the channel with.

  • image_file – A string path to a PNG or TIFF file, or an open file object of a PNG or TIFF file to upload.

  • filename – The name to use for the file being uploaded. If image_file is a path, this can be omitted and that file’s name will be used. It should be the name of the channel/layer with an extension to indicate format, such as “CD45.tiff” or “cell_boundaries.png”.

Returns:

The response from the MIBItracker after uploading the file.

Raises:

TypeError – Raised if image_file is not a string path or file object.

run_images(run_label)

Gets a JSON array of image metadata from a given run label.

Parameters:

run_label – The unique string label of a run.

Returns:

A list of dicts of of image metadata for the specified run.

image_conjugates(image_id)

Gets a JSON array of panel conjugates from a given image id.

Parameters:

image_id – The integer id of an image.

Returns:

A list of dicts of antibody conjugate details from the image’s panel. This will be an empty list of the image does not have a section assigned, or if its section does not have a panel assigned.

image_id(run_label, fov_id)

Gets the primary key of an image given the specified run and FOV.

Parameters:
  • run_label – The label of the run the image belongs to. If no images found using run label (which is the unique identifier of each run), run name is checked instead (run name is not guaranteed to be unique per run).

  • fov_id – The FOV ID, in the format of FOV<n> or Point<n> for data generated with MIBIcontrol and MiniSIMS, respectively.

Returns:

An int id corresponding to the primary key of the image.

Raises:

ValueError – Raised if no images match the specified run and FOV, or if more than one image matches the specified run and FOV.

get_mibi_image(image_id)

Gets image data from MIBItracker and creates a MibiImage instance.

Parameters:

image_id – The integer id of an image.

Returns:

A MibiImage instance of the requested image.

get_channel_data(image_id, channel_name)

Gets a single channel from MIBItracker as a 2D numpy array.

Parameters:
  • image_id – The integer id of an image.

  • channel_name – The name of the channel to download.

Returns:

A MxN numpy array of the channel data.

create_imageset(image_ids, imageset_name, project_id, imageset_description=None)

Creates a new imageset in a project with the specified images.

Note that all images in the set must be either in the project specified or in other projects to which the user has access and sharing is enabled.

Parameters:
  • image_ids – A list of ints of ids of the images in MIBItracker corresponding to the images to be added to the new imageset.

  • imageset_name – A string name for the new imageset.

  • project_id – An integer id specifying the project in which to create the image set.

  • imageset_description – (optional) A string description for the new imageset. Defaults to None.

class mibitracker.request_helpers.StatusCheckedSession(timeout=10)

Bases: Session

Raises for HTTP errors and adds any response JSON to the message.

get(*args, **kwargs)

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

options(*args, **kwargs)

Sends a OPTIONS request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

post(*args, **kwargs)

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

put(*args, **kwargs)

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

delete(*args, **kwargs)

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

Module contents

Provides classes and methods for interacting with the MIBItracker API