more testing

This commit is contained in:
Martin Diehl 2020-06-27 21:26:25 +02:00
parent b78c809375
commit 1c03bd157f
2 changed files with 26 additions and 14 deletions

View File

@ -234,23 +234,30 @@ class Colormap(mpl.colors.ListedColormap):
@staticmethod @staticmethod
def _export_GOM(colormap,fhandle=None): def _export_GOM(colormap,fhandle=None):
pass # ToDo: test in GOM
s =(f'1 1 {colormap.name.replace(" ","_")} 9 {colormap.name.replace(" ","_")} ' GOM_str = f'1 1 {colormap.name.replace(" ","_")} 9 {colormap.name.replace(" ","_")} ' \
f' 0 1 0 3 0 0 -1 9 \\ 0 0 0 255 255 255 0 0 255 ' + '0 1 0 3 0 0 -1 9 \\ 0 0 0 255 255 255 0 0 255 ' \
f'30 NO_UNIT 1 1 64 64 64 255 1 0 0 0 0 0 0 3 0 {str(len(colormap.colors))}' + f'30 NO_UNIT 1 1 64 64 64 255 1 0 0 0 0 0 0 3 0 {len(colormap.colors)}' \
' '.join([' 0 %s 255 1'%(' '.join([str(int(x*255.0)) for x in color])) for color in reversed(colormap.colors)])) + ' '.join([f' 0 {c[0]} {c[1]} {c[2]} 255 1' for c in reversed((colormap.colors*255).astype(int))]) \
print(s) + '\n'
if fhandle is None:
with open(colormap.name.replace(' ','_')+'.legend', 'w') as f:
f.write(GOM_str)
else:
fhandle.write(GOM_str)
@staticmethod @staticmethod
def _export_gmsh(colormap,fhandle=None): def _export_gmsh(colormap,fhandle=None):
colormap = 'View.ColorTable = {\n'\ # ToDo: test in gmsh
+'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in reversed(colormap.colors)])\ gmsh_str = 'View.ColorTable = {\n' \
+'}' +'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in colormap.colors[:,:3]*255]) \
+'\n}\n'
if fhandle is None: if fhandle is None:
with open(colormap.name.replace(' ','_')+'.txt', 'w') as f: with open(colormap.name.replace(' ','_')+'.msh', 'w') as f:
t.to_ASCII(f) f.write(gmsh_str)
else: else:
t.to_ASCII(fhandle) fhandle.write(gmsh_str)
@staticmethod @staticmethod

View File

@ -86,10 +86,15 @@ class TestColormap:
]) ])
def test_write_filehandle(self,format,name,tmpdir): def test_write_filehandle(self,format,name,tmpdir):
c = Colormap.from_predefined('Dark2') c = Colormap.from_predefined('Dark2')
with open(tmpdir/name,'w') as f: fname = tmpdir/name
with open(fname,'w') as f:
c.to_file(f,format=format) c.to_file(f,format=format)
for i in range(10):
if fname.exists(): return
time.sleep(.5)
assert False
def test_invalid_format(self): def test_write_invalid_format(self):
c = Colormap.from_predefined('Dark2') c = Colormap.from_predefined('Dark2')
with pytest.raises(ValueError): with pytest.raises(ValueError):
c.to_file(format='invalid') c.to_file(format='invalid')