Skip to content

Commit

Permalink
Merge pull request #49 from Caltech-IPAC/cleanup-plot.py
Browse files Browse the repository at this point in the history
Cleanup Plot.py
  • Loading branch information
robyww authored Aug 12, 2021
2 parents 551c924 + 0ef5320 commit f20cdcd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 115 deletions.
26 changes: 26 additions & 0 deletions firefly_client/fc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,35 @@ def warn(s): print('WARNING: %s' % s)
def dict_to_str(in_dict): return json.dumps(in_dict, indent=2, default=str)


# id for table, region layer, extension
_item_id = {'Table': 0, 'RegionLayer': 0, 'Extension': 0, 'MaskLayer': 0, 'XYPlot': 0,
'Cell': 0, 'Histogram': 0, 'Plotly': 0, 'Image': 0, 'FootprintLayer': 0}

ALL = 'ALL_EVENTS_ENABLED'


def gen_item_id(item):
"""
Generate an ID for table, region layer, or extension entity.
Parameters
----------
item : {'Table', 'RegionLayer', 'Extension', 'XYPlot', 'Cell', 'FootprintLayer'}
Entity type.
Returns
-------
out : `str`
ID string.
"""

if item in _item_id:
_item_id[item] += 1
return item + '-' + str(_item_id[item])
else:
return None


def create_image_url(image_source):
def is_url(url): return urllib.parse.urlparse(url).scheme != ''

Expand Down
55 changes: 15 additions & 40 deletions firefly_client/firefly_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import weakref
from copy import copy


try:
from .ffws import FFWs
except ImportError:
Expand All @@ -27,10 +28,10 @@
except ImportError:
from range_values import RangeValues
try:
from .fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, \
from .fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, gen_item_id,\
DebugMarker, ALL, ACTION_DICT, LO_VIEW_DICT
except ImportError:
from fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, \
from fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, gen_item_id,\
DebugMarker, ALL, ACTION_DICT, LO_VIEW_DICT

__docformat__ = 'restructuredtext'
Expand Down Expand Up @@ -579,7 +580,7 @@ def add_cell(self, row, col, width, height, element_type, cell_id=None):
cell_id = 'main'
else:
if not cell_id:
cell_id = FireflyClient._gen_item_id('Cell')
cell_id = gen_item_id('Cell')

payload = {'row': row,
'col': col,
Expand Down Expand Up @@ -778,7 +779,7 @@ def show_table(self, file_on_server=None, tbl_id=None, title=None, page_size=100
"""

if not tbl_id:
tbl_id = FireflyClient._gen_item_id('Table')
tbl_id = gen_item_id('Table')
if not title:
title = tbl_id if file_on_server else target_search_info.get('catalog', tbl_id)

Expand Down Expand Up @@ -838,7 +839,7 @@ def fetch_table(self, file_on_server, tbl_id=None, title=None, page_size=1, tabl
"""

if not tbl_id:
tbl_id = FireflyClient._gen_item_id('Table')
tbl_id = gen_item_id('Table')
if not title:
title = tbl_id
tbl_req = {'startIdx': 0, 'pageSize': page_size, 'source': file_on_server,
Expand Down Expand Up @@ -922,7 +923,7 @@ def show_xyplot(self, tbl_id, standalone=False, group_id=None, **chart_params):

chart_data_elements = [{'type': 'xycols', 'options': options, 'tblId': tbl_id}]

cid = FireflyClient._gen_item_id('XYPlot')
cid = gen_item_id('XYPlot')

if not group_id:
group_id = 'default' if standalone else tbl_id
Expand Down Expand Up @@ -991,7 +992,7 @@ def show_histogram(self, tbl_id, group_id='default', **histogram_params):
'binWidth': histogram_params.get('binWidth')})
chart_data_elements.update({'options': options})

cid = FireflyClient._gen_item_id('Histogram')
cid = gen_item_id('Histogram')
payload = {'chartId': cid, 'chartType': 'histogram',
'groupId': group_id,
'viewerId': group_id,
Expand Down Expand Up @@ -1034,7 +1035,7 @@ def show_chart(self, group_id='default', **chart_params):
Status of the request, like {'success': True}.
"""
chart_id = chart_params.get('chartId') if 'chartId' in chart_params else FireflyClient._gen_item_id('Plotly')
chart_id = chart_params.get('chartId') if 'chartId' in chart_params else gen_item_id('Plotly')
payload = {'chartId': chart_id,
'groupId': group_id,
'viewerId': group_id,
Expand Down Expand Up @@ -1125,7 +1126,7 @@ def add_extension(self, ext_type, plot_id=None, title='', tool_tip='',
"""

if not extension_id:
extension_id = FireflyClient._gen_item_id('Extension')
extension_id = gen_item_id('Extension')
payload = {'extension': {
'id': extension_id, 'plotId': plot_id,
'imageUrl': create_image_url(image_src) if image_src else None,
Expand Down Expand Up @@ -1219,7 +1220,7 @@ def show_hips(self, plot_id=None, viewer_id=None, hips_root_url=None, hips_image
payload = {'wpRequest': wp_request}

if not plot_id:
plot_id = FireflyClient._gen_item_id('Image')
plot_id = gen_item_id('Image')

payload.update({'plotId': plot_id})
wp_request.update({'plotId': plot_id})
Expand Down Expand Up @@ -1270,7 +1271,7 @@ def show_image_or_hips(self, plot_id=None, viewer_id=None, image_request=None, h
return

if not plot_id:
plot_id = FireflyClient._gen_item_id('Image')
plot_id = gen_item_id('Image')
if not viewer_id:
viewer_id = 'DEFAULT_FITS_VIEWER_ID'
if self.render_tree_id:
Expand Down Expand Up @@ -1550,7 +1551,7 @@ def overlay_footprints(self, footprint_file, footprint_image=None, title=None,
"""

if not footprint_layer_id:
footprint_layer_id = FireflyClient._gen_item_id('FootprintLayer')
footprint_layer_id = gen_item_id('FootprintLayer')
payload = {'drawLayerId': footprint_layer_id}

title and payload.update({'title': title})
Expand Down Expand Up @@ -1598,7 +1599,7 @@ def overlay_region_layer(self, file_on_server=None, region_data=None, title=None
"""

if not region_layer_id:
region_layer_id = FireflyClient._gen_item_id('RegionLayer')
region_layer_id = gen_item_id('RegionLayer')
payload = {'drawLayerId': region_layer_id}

title and payload.update({'layerTitle': title})
Expand Down Expand Up @@ -1714,7 +1715,7 @@ def add_mask(self, bit_number, image_number, plot_id, mask_id=None, color=None,
"""

if not mask_id:
mask_id = FireflyClient._gen_item_id('MaskLayer')
mask_id = gen_item_id('MaskLayer')
if not title:
title = 'bit %23 ' + str(bit_number)

Expand Down Expand Up @@ -1743,29 +1744,3 @@ def remove_mask(self, plot_id, mask_id):

payload = {'plotId': plot_id, 'imageOverlayId': mask_id}
return self.dispatch(ACTION_DICT['DeleteOverlayMask'], payload)

# -----------------------------------------------------------------
# Range Values
# -----------------------------------------------------------------

@classmethod
def _gen_item_id(cls, item):
"""
Generate an ID for table, region layer, or extension entity.
Parameters
----------
item : {'Table', 'RegionLayer', 'Extension', 'XYPlot', 'Cell', 'FootprintLayer'}
Entity type.
Returns
-------
out : `str`
ID string.
"""

if item in cls._item_id:
cls._item_id[item] += 1
return item + '-' + str(cls._item_id[item])
else:
return None
Loading

0 comments on commit f20cdcd

Please sign in to comment.