Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add static url and local save #136

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions caveclient/jsonservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
default_global_server_address,
ngl_endpoints_common,
)
import os
import requests
import numpy as np
import numbers
Expand Down Expand Up @@ -224,11 +225,37 @@ def upload_state_json(self, json_state, state_id=None, timestamp=None):
response_re = re.search(".*\/(\d+)", str(response.content))
return int(response_re.groups()[0])

def save_state_json_local(self, json_state, filename, overwrite=False):
"""Save a Neuroglancer JSON state to a JSON file locally.

Parameters
----------
json_state : dict
Dict representation of a neuroglancer state
filename : str
Filename to save the state to
overwrite : bool
Whether to overwrite the file if it exists. Default False.

def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
Returns
-------
None
"""
if os.path.exists(filename) and not overwrite:
raise ValueError("File exists and overwrite is False")
with open(filename, "w") as f:
json.dump(json_state, f, default=neuroglancer_json_encoder)

def build_neuroglancer_url(
self,
state_id,
ngl_url=None,
target_site=None,
static_url=False,
):
"""Build a URL for a Neuroglancer deployment that will automatically retrieve specified state.
If the datastack is specified, this is prepopulated from the info file field "viewer_site".
If no ngl_url is specified in either the function or the client, only the JSON state url is returned.
If no ngl_url is specified in either the function or the client, a fallback neuroglancer deployment is used.

Parameters
----------
Expand All @@ -238,9 +265,11 @@ def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
Base url of a neuroglancer deployment. If None, defaults to the value for the datastack or the client.
As a fallback, a default deployment is used.
target_site : 'seunglab' or 'cave-explorer' or 'mainline' or None
Set this to 'seunglab' for a seunglab deployment, or 'cave-explorer'/'mainline' for a google main branch deployment.
Set this to 'seunglab' for a seunglab deployment, or either 'cave-explorer'/'mainline' for a google main branch deployment.
If None, checks the info field of the neuroglancer endpoint to determine which to use.
Default is None.
static_url : bool
If True, treats "state_id" as a static URL directly to the JSON and does not use the state service.

Returns
-------
Expand All @@ -252,6 +281,7 @@ def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
ngl_url = self.ngl_url
else:
ngl_url = ngl_endpoints_common['fallback_ngl_url']

if target_site is None and ngl_url is not None:
ngl_info = self.get_neuroglancer_info(ngl_url)
if len(ngl_info) > 0:
Expand All @@ -264,16 +294,24 @@ def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
parameter_text = "?json_url="
else:
parameter_text = "/?json_url="
auth_text = ""
elif target_site == "cave-explorer" or target_site == "mainline":
if ngl_url[-1] == "/":
parameter_text = "#!middleauth+"
parameter_text = "#!"
else:
parameter_text = "/#!middleauth+"
parameter_text = "/#!"
auth_text = "middleauth+"
else:
target_site_error = "A specified target_site must be one of 'seunglab', 'cave-explorer' or 'mainline'"
raise ValueError(target_site_error)

url_mapping = self.default_url_mapping
url_mapping["state_id"] = state_id
get_state_url = self._endpoints["get_state"].format_map(url_mapping)
url = ngl_url + parameter_text + get_state_url
if static_url:
url = ngl_url + parameter_text + state_id
else:
url_mapping = self.default_url_mapping
url_mapping["state_id"] = state_id
get_state_url = self._endpoints["get_state"].format_map(url_mapping)
url = ngl_url + parameter_text + auth_text + get_state_url
return url


Expand Down
Loading