Skip to content

Commit

Permalink
example tests pass python3
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodore Lindsay committed Nov 26, 2017
1 parent 89c10a4 commit 45697cd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/example_multi_fig.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pylab import *
from figurefirst import FigureLayout,mpl_functions

print 'running multi figure example'
print('running multi figure example')
#Groups and figures example
layout = FigureLayout('example_multi_fig_layout.svg')
mplfig = layout.make_mplfigures()
Expand Down
14 changes: 13 additions & 1 deletion examples/run_tests.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#! /usr/bin/python
import os
#from subprocess import call
examples = [x for x in os.listdir('.') if ('example' in x) and ('.py' in x)]
print('########################')
print('# Testing Python2 #')
print('########################')
for example in examples:
os.system('python %s'%(example))
print("Running:%s"%example)
os.system('python2 %s'%(example))

print('########################')
print('# Testing Python3 #')
print('########################')
for example in examples:
print("Running:%s"%example)
os.system('python3 %s'%(example))
8 changes: 6 additions & 2 deletions figurefirst/mpl_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import tempfile
import shutil
import os
import sys



# Load some user parameters / defaults
# By default figurefirst will load the figurefirst_user_parameters.py file from the figurefirst/figurefirst directory.
Expand Down Expand Up @@ -44,13 +47,14 @@ def adjust_spines(ax,spines, spine_locations={}, smart_bounds=True, xticks=None,
spine_locations_dict[key] = spine_location_offset

if 'none' in spines:
for loc, spine in ax.spines.iteritems():
#for loc, spine in ax.spines.iteritems():
for loc, spine in ax.spines.items():
spine.set_color('none') # don't draw spine
ax.yaxis.set_ticks([])
ax.xaxis.set_ticks([])
return

for loc, spine in ax.spines.iteritems():
for loc, spine in ax.spines.items():
if loc in spines:
spine.set_position(('outward',spine_locations_dict[loc])) # outward by x points
spine.set_linewidth(linewidth)
Expand Down
55 changes: 41 additions & 14 deletions figurefirst/svg_to_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from warnings import warn
import traceback
import time
import sys
if (sys.version_info > (3, 0)):
PY3 = True
else:
PY3 = False

# Load some user parameters / defaults
# By default figurefirst will load the figurefirst_user_parameters.py file from the figurefirst/figurefirst directory.
Expand Down Expand Up @@ -180,12 +185,17 @@ def parse_transform(transform_str):
[0,0,1.]])
mt_pos = transform_str.find('matrix')
##################
trnsfrms = [mtrx for (pos,mtrx) in sorted(zip([tr_pos,mt_pos,sc_pos],
[tr,mt,sc]))]
tr_pos = {False:tr_pos,True:0}[tr_pos is None]
mt_pos = {False:mt_pos,True:0}[mt_pos is None]
sc_pos = {False:sc_pos,True:0}[sc_pos is None]
s = [tr_pos,mt_pos,sc_pos]
def take0(x):
return x[0]
trnsfrms = [mtrx for pos,mtrx in sorted(zip(s,[tr,mt,sc]),key =take0)]
trnsfrms = [m for m in trnsfrms if not(m is None)]
from numpy import dot
from functools import reduce
if len(trnsfrms) > 1:
if len(trnsfrms) >= 1:
mtrx = reduce(lambda x,y:dot(x,y.T),trnsfrms)
return mtrx

Expand All @@ -209,6 +219,7 @@ def __init__(self,tagnode,**kwargs):
self.id = self.node.getAttribute('id')
self.name = tagnode.getAttribute('figurefirst:name')
self.ismplaxis = False
self.ismplfigure = False
if self.node.hasAttribute('transform'):
self.transform_str = self.node.getAttribute('transform')
else:
Expand Down Expand Up @@ -361,19 +372,28 @@ def __init__(self,tagnode,**kwargs):

def __getattr__(self,attr):
#try:
pnts = np.vstack([np.array([np.array([i.x,i.y]),np.array([i.x+i.w,i.y+i.h])])
vals = [np.array([np.array([i.x,i.y]),np.array([i.x+i.w,i.y+i.h])])
for i in self.values()]
#print(attr)
if len(vals)>0:
pnts = np.vstack([np.array([np.array([i.x,i.y]),np.array([i.x+i.w,i.y+i.h])])
for i in self.values()])
x = np.min(pnts[:,0])
y = np.min(pnts[:,1])
w = np.max(pnts[:,0])-x
h = np.max(pnts[:,1])-y
try:
return {'x':x,'y':y,'w':w,'h':h}[attr]
except KeyError:
#return self.__dict__[attr]
return super(FFGroup, self).__getattribute__(attr)
#return self.__getattribute__(attr)
else:
pnts = None
return self.__getattribute__(attr)
#except ValueError:
# raise NameError('Could not find an axis for this figure. You probably have a figurefirst:figure tag with no axes associated with it.')

x = np.min(pnts[:,0])
y = np.min(pnts[:,1])
w = np.max(pnts[:,0])-x
h = np.max(pnts[:,1])-y
try:
return {'x':x,'y':y,'w':w,'h':h}[attr]
except KeyError:
return self.__getattribute__(attr)

class FFFigure(FFGroup,object):
""" Represents a matplotlib figure """
Expand All @@ -384,6 +404,8 @@ def __init__(self,tagnode,**kwargs):

def __getattr__(self,attr):
try:
if attr is 'ismplfigure':
return False
val = super(FFFigure,self).__getattr__(attr)
return val
except AttributeError:
Expand Down Expand Up @@ -448,6 +470,8 @@ def __getattr__(self,attr):
if attr == 'h':
return (self.p2-self.p1)[1]
else:
if attr is 'ismplaxis':
return self.__getattribute__(attr)
if self.ismplaxis:
return self['axis'].__getattribute__(attr)
else:
Expand Down Expand Up @@ -1057,7 +1081,10 @@ def pass_xml(self, gid, key, value):

def to_svg_buffer(self, fig):
"""writes a matplotlib figure into a stringbuffer and returns the buffer"""
from StringIO import StringIO
if PY3:
from io import StringIO
else:
from StringIO import StringIO
fid = StringIO()
#if fig.ismplfigure:
fig.figure.savefig(fid, format='svg', transparent=True, dpi=self.dpi)
Expand Down Expand Up @@ -1085,7 +1112,7 @@ def save(self,filename,hidelayers = [],targetlayer = None,fix_meterlimt= True):
for l in hidelayers:
self.set_layer_visibility(l,False)
self.write_svg(filename)
import mpl_functions
from . import mpl_functions
if fix_meterlimt:
mpl_functions.fix_mpl_svg(filename)

Expand Down

0 comments on commit 45697cd

Please sign in to comment.