CDAT / vcdat

vCDAT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import and export vcs colormaps

James-Crean opened this issue · comments

Importing and exporting a colormap with vCDAT should be compatible with vcs colormaps.

Currently, exporting a colormap is done by taking the name and the colormap data from the frontend and this is converted to json for downloading. This needs to be changed such that the files exported by vCDAT are able to be imported by vcs. The same goes for the reverse. Colormaps exported by vcs should be importable by vCDAT.

Several issues I've observed:
The json format of the frontend and backend are mismatched.
Frontend json format: {"name":<some_name: str>, "colormap":[colormap:array]"
Backend json format: {"Cp":{<some_name:str>: {"index": {data: {<index_val:int>: <colormap_values: array}}}}

This mismatch causes a KeyError to be raised when vcs.setcolormap is invoked in saveColormap().

@William-Hill do you have a branch somewhere I can play with? Thanks.

@doutriaux1 Yes, it's here: https://github.com/CDAT/vcdat/commits/import_export_colormaps

I've made the change to make the exported json match the format of the backend. I'm working on the import now.

thanks hopefully I'm getting to this very soon.

Full stack trace of importing a colormap that was exported in the Frontend json format:

"Traceback (most recent call last):
File "/anaconda2/envs/vcdat/lib/python2.7/site-packages/wslink/websocket.py", line 274, in onMessage
results = func(obj, *args, **kwargs)
File "build/bdist.macosx-10.9-x86_64/egg/vcs_server/Visualizer.py", line 128, in setcolormap
cm = vcs.getcolormap(name)
File "/anaconda2/envs/vcdat/lib/python2.7/site-packages/vcs/manageElements.py", line 1750, in getcolormap
return vcs.elements["colormap"][Cp_name_src]
KeyError: 'test_colormap.json'
"

you probably do not want the ".json" at the end, Cp_name_src is the name of the colormap, not the file name.

@doutriaux1 I stripped away the ".json" and still got the error. It appears that the getcolormap methods is looking for a colormap with the given name with no error handling. Since a colormap is being imported and the name isn't found in vcs, the KeyError is bubbling up. Or so I think for looking at this code:

`def getcolormap(Cp_name_src='default'):
"""%s

:param Cp_name_src: String name of an existing colormap VCS object
:type Cp_name_src: `str`_

:returns: A pre-existing VCS colormap object
:rtype: vcs.colormap.Cp
"""
# Check to make sure the argument passed in is a STRING
if not isinstance(Cp_name_src, basestring):
    raise ValueError('Error -  The argument must be a string.')

return vcs.elements["colormap"][Cp_name_src]

getcolormap.doc = getcolormap.doc % xmldocs.get_docs['colormap'] # noqa`

I'm almost there, will take a look in 15mn or so.

@William-Hill I think I know what is wrong. the colormap only exists on the vcs-js server. Not on the front-end. You will need to ask vcsjs to send you the json and then save it locally. Similar to what they do for png export.
@scottwittenburg @downiec can you help @William-Hill with this.
The other way is to construct an actual mapper from @James-Crean json format to vcs actual format.

or have the local vcs also create the colormap not just the server, like that your call will work.

@William-Hill ok this converts whatever is in james files into what vcs would export:

def convert(cmap):
    out = {"Cp":{}}
    cmap_out= {}
    for i, rgbo in enumerate(cmap["colormap"]):
        cmap_out[str(i)] = rgbo
    out["Cp"][cmap["name"]] = {"index": {"data": cmap_out}}
    return out

Full file to test it obn viridis colormpa exported from VCDAT (current format) is attached.
convert_colormap_json.py.txt

so just take whatever james was writing and preprocess it through this, then do a json.dump of the converted dictionary.

you will likely need to reverse the function for the import

@doutriaux1 Thanks Charles. I have the exported part working. Not it's just getting the import working. I'll try reversing it as you mentioned

great! thanks to you!

Resolved by #329