Ticket #12299: trac_12299-all-in-one.patch

File trac_12299-all-in-one.patch, 10.8 KB (added by jhpalmieri, 9 years ago)

all Sage library patches, folded into one

  • doc/en/reference/interfaces.rst

    # HG changeset patch
    # User Jonathan Gutow <gutow@uwosh.edu>
    # Date 1338133362 18000
    # Node ID b20feb6876f1d0c5a61acf54d85919bb4322b53b
    # Parent  abcd030d6bef0a53e75139d30af69df649227788
    Advance Jmol Interactive Features in Flask Notebook:
    new file sage/interfaces/jmoldata.py, for extracting data and
    generating static images from Jmol.
    
    diff --git a/doc/en/reference/interfaces.rst b/doc/en/reference/interfaces.rst
    a b and testing to make sure nothing funny i 
    7979   sage/interfaces/sage0
    8080   sage/interfaces/singular
    8181   sage/interfaces/tachyon
     82   sage/interfaces/jmoldata
  • new file sage/interfaces/jmoldata.py

    diff --git a/sage/interfaces/jmoldata.py b/sage/interfaces/jmoldata.py
    new file mode 100644
    - +  
     1r"""
     2Interface for extracting data and generating images from Jmol readable files.
     3
     4JmolData is a no GUI version of Jmol useful for extracting data from files Jmol
     5reads and for generating image files.
     6
     7AUTHORS:
     8
     9- Jonathan Gutow (2012-06-14): complete doctest coverage
     10- Jonathan Gutow (2012-03-21): initial version
     11"""
     12
     13#*******************************************************************************
     14#       Copyright (C) 2012 Jonathan Gutow (gutow@uwosh.edu)
     15#
     16#  Distributed under the terms of the GNU General Public License (GPL)
     17#  as published by the Free Software Foundation; either version 2 of
     18#  the License, or (at your option) any later version.
     19#                  http://www.gnu.org/licenses/
     20#*******************************************************************************
     21
     22from sage.structure.sage_object import SageObject
     23
     24from sage.misc.misc import tmp_filename
     25from sage.misc.misc import SAGE_LOCAL, DOT_SAGE, sage_makedirs
     26
     27import subprocess
     28import os
     29
     30class JmolDataError(Exception):
     31
     32    def __init__(self, value):
     33        """
     34        TEST::
     35
     36            sage: from sage.interfaces.jmoldata import JmolDataError
     37            sage: print(JmolDataError("test of init").value)
     38            test of init
     39        """
     40        self.value = value
     41
     42    def __str__(self):
     43        """
     44        TEST::
     45
     46            sage: from sage.interfaces.jmoldata import JmolDataError
     47            sage: print str(JmolDataError("test of error"))
     48            'test of error'
     49        """
     50        return repr(self.value)
     51
     52class JmolData(SageObject):
     53    r"""
     54    .. todo::
     55
     56       Create an animated image file (GIF) if spin is on and put data
     57       extracted from a file into a variable/string/structure to return
     58    """
     59    def __init__(self):
     60        """
     61        EXAMPLES:
     62
     63        Create a JmolData object::
     64
     65            sage: from sage.interfaces.jmoldata import JmolData
     66            sage: JData = JmolData()
     67        """
     68        pass
     69
     70    def is_jvm_available(self):
     71        """
     72        Returns True if the Java Virtual Machine is available and False if not.
     73
     74        EXAMPLES:
     75
     76        Check that it returns a boolean::
     77
     78            sage: from sage.interfaces.jmoldata import JmolData
     79            sage: JData = JmolData()
     80            sage: type(JData.is_jvm_available())
     81            <type 'bool'>
     82        """
     83        #scratch file for  Jmol errors and status
     84        jmolscratch = os.path.join(DOT_SAGE, "sage_notebook.sagenb", "jmol_scratch")
     85        if not os.path.exists(jmolscratch):
     86            sage_makedirs(jmolscratch)
     87        scratchout = os.path.join(jmolscratch,"jmolout.txt")
     88        jout=open(scratchout,'w')
     89        testjavapath = os.path.join(SAGE_LOCAL, "share", "jmol", "testjava.sh")
     90        result = subprocess.call([testjavapath],stdout=jout)
     91        jout.close()
     92        if (result == 0):
     93            return (True)
     94        else:
     95            return (False)
     96
     97    def export_image(self,
     98        targetfile,
     99        datafile, #name (path) of data file Jmol can read or script file telling it what to read or load
     100        datafile_cmd='script', #"script" or "load"
     101        image_type ='PNG', #PNG, JPG, GIF
     102        figsize=5,
     103        **kwds):
     104        r"""
     105        This executes JmolData.jar to make an image file.
     106
     107        INPUT:
     108
     109        - targetfile -- the full path to the file where the image
     110          should be written.
     111
     112        - datafile -- full path to the data file Jmol can read or
     113          text of a script telling Jmol what to read or load.
     114
     115        - datafile_cmd -- (default ``'script'``)  ``'load'`` or ``'script'``
     116          should be ``"load"`` for a data file.
     117
     118        - image_type -- (default ``"PNG"``) ``'PNG'`` ``'JPG'`` or ``'GIF'``
     119
     120        - figsize -- number (default 5) equal to (pixels/side)/100
     121
     122        OUTPUT:
     123
     124        Image file, .png, .gif or .jpg (default .png)
     125
     126        .. note::
     127
     128            Examples will generate an error message if a functional Java Virtual Machine (JVM)
     129            is not installed on the machine the Sage instance is running on.
     130
     131        .. warning::
     132
     133            Programmers using this module should check that the JVM is
     134            available before making calls to avoid the user getting
     135            error messages.  Check for the JVM using the function
     136            :meth:`is_jvm_available`, which returns True if a JVM is available.
     137
     138        EXAMPLES:
     139
     140        Use Jmol to load a pdb file containing some DNA from a web data
     141        base and make an image of the DNA. If you execute this in the
     142        notebook, the image will appear in the output cell::
     143
     144            sage: from sage.interfaces.jmoldata import JmolData
     145            sage: JData = JmolData()
     146            sage: script = "load =1lcd;display DNA;moveto 0.0 { -473 -713 -518 59.94} 100.0 0.0 0.0 {21.17 26.72 27.295} 27.544636 {0.0 0.0 0.0} -25.287832 64.8414 0.0;"
     147            sage: testfile = tmp_filename()+"DNA.png"
     148            sage: JData.export_image(targetfile=testfile,datafile=script,image_type="PNG") # optional -- internet
     149            sage: print os.path.exists(testfile) # optional -- internet
     150            True
     151
     152        Use Jmol to save an image of a 3-D object created in Sage.
     153        This method is used internally by plot3d to generate static images.
     154        This example doesn't have correct scaling::
     155
     156            sage: from sage.interfaces.jmoldata import JmolData
     157            sage: JData = JmolData()
     158            sage: D=dodecahedron()
     159            sage: from sage.misc.misc import SAGE_TMP
     160            sage: archive_name=os.path.join(SAGE_TMP, "archive.jmol.zip")
     161            sage: D.export_jmol(archive_name)  #not scaled properly...need some more steps.
     162            sage: testfile = os.path.join(SAGE_TMP, "testimage.png")
     163            sage: script = 'set defaultdirectory "%s"\n script SCRIPT\n'%archive_name
     164            sage: JData.export_image(targetfile =testfile,datafile = script, image_type="PNG")
     165            sage: print os.path.exists(testfile)
     166            True
     167
     168         """
     169        if (self.is_jvm_available()):
     170            # Set up paths, file names and scripts
     171            jmolpath = os.path.join(SAGE_LOCAL, "share", "jmol", "JmolData.jar")
     172            launchscript = ""
     173            if (datafile_cmd!='script'):
     174                launchscript = "load "
     175            launchscript = launchscript + datafile
     176            #print launchscript
     177            imagescript = "write "+ image_type +" "+targetfile+"\n"
     178            #print imagescript
     179
     180            sizeStr = "%sx%s" %(figsize*100,figsize*100)
     181            #scratch file for  Jmol errors and status
     182            jmolscratch = os.path.join(DOT_SAGE, "sage_notebook.sagenb", "jmol_scratch")
     183            if not os.path.exists(jmolscratch):
     184                sage_makedirs(jmolscratch)
     185            scratchout = os.path.join(jmolscratch,"jmolout.txt")
     186            jout=open(scratchout,'w')
     187            #now call the java application and write the file.
     188            result = subprocess.call(["java","-Xmx512m","-jar",jmolpath,"-iox","-g",sizeStr,"-J",launchscript,"-j",imagescript],stdout=jout)
     189            jout.close()
     190        else:
     191            errStr = "Java Virtual Machine not available.\n"
     192            errStr +="This should be checked before calling JmolData().export_image().\n"
     193            errStr +="Use JmolData().is_jvm_available() to check.\n"
     194            errStr +="Administrator should install JVM."
     195            raise JmolDataError(errStr)
  • sage/plot/plot3d/base.pyx

    diff --git a/sage/plot/plot3d/base.pyx b/sage/plot/plot3d/base.pyx
    a b end_scene""" % (render_params.antialiasi 
    11291129
    11301130            # We need a script to load the file
    11311131            f = open(filename + '.'+ext, 'w')
     1132            import sagenb
    11321133            if EMBEDDED_MODE:
    1133                 import sagenb
    11341134                path = "cells/%s/%s" %(sagenb.notebook.interact.SAGE_CELL_ID, archive_name)
    11351135            else:
    11361136                path = archive_name
    end_scene""" % (render_params.antialiasi 
    11381138            f.write('script SCRIPT\n')
    11391139            f.close()
    11401140
     1141            # If the server has a Java installation we can make better static images with Jmol
     1142            # Test for Java then make image with Jmol or Tachyon if no JavaVM
     1143            if EMBEDDED_MODE:
     1144                #name image file
     1145                head,tail = os.path.split(archive_name)
     1146                png_path = os.path.join(head,'.jmol_images')
     1147                if  not os.path.exists(png_path):
     1148                    os.mkdir(png_path)
     1149                png_name = os.path.join(png_path,filename)
     1150                #test for JavaVM
     1151                from sage.interfaces.jmoldata import JmolData
     1152                jdata = JmolData()
     1153                if (jdata.is_jvm_available()):
     1154                    # make the image with Jmol
     1155                    # hack...need absolute paths since jvm running outside of sage environment
     1156                    cellhead, celltail =os.path.split(os.path.realpath(os.path.join(os.path.curdir,"data")))
     1157                    celldir = os.path.join(cellhead,"cells",str(sagenb.notebook.interact.SAGE_CELL_ID))
     1158                    png_name = png_name+".jmol.png"
     1159                    png_fullpath = os.path.join(celldir,png_name)
     1160                    archive_fullpath = os.path.join(celldir,archive_name)
     1161                    #print png_fullpath
     1162                    script = 'set defaultdirectory \"'+archive_fullpath+'\"\n script SCRIPT\n'
     1163                    #print script
     1164                    jdata.export_image(targetfile = png_fullpath,datafile=script,image_type="PNG", figsize = fg)
     1165                else:
     1166                    #make the image with tachyon
     1167                    T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom)
     1168                    tachyon_rt(T.tachyon(), png_name+".jmol.png", verbosity, True, opts)
     1169
     1170                       
    11411171        if viewer == 'canvas3d':
    11421172            T = self._prepare_for_tachyon(frame, axes, frame_aspect_ratio, aspect_ratio, zoom)
    11431173            data = flatten_list(T.json_repr(T.default_render_params()))