Related
I am trying to read an image from kaggle competition (It is an old competition, but I would like to practice):
https://www.kaggle.com/competitions/dogs-vs-cats-redux-kernels-edition
I am trying to read images from the training file zip using this code:
def get_files_names(zip_file_path):
with ZipFile(zip_file_path) as myzip:
return myzip.namelist()
def get_image(zip_path, image_name):
with ZipFile(zip_path) as myzip:
# print(myzip.namelist()[:10])
with myzip.open(image_name) as myfile:
# img = Image.open(myfile)
img = Image.open(myfile)
return img
names = get_files_names(train_file_path)
img = get_image(train_file_path, names[1])
img.show()
I am getting this error:
Traceback (most recent call last):
File "/cats_vs_dogs/unrelated_file.py", line 46, in <module>
img.show()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 2205, in show
_show(self, title=title, command=command)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3167, in _show
_showxv(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3181, in _showxv
ImageShow.show(image, title, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 56, in show
if viewer.show(image, title=title, **options):
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 81, in show
return self.show_image(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 107, in show_image
return self.show_file(self.save_image(image), **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 103, in save_image
return image._dump(format=self.get_format(image), **self.options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 636, in _dump
self.load()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageFile.py", line 247, in load
s = read(self.decodermaxblock)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 400, in load_read
s = self.fp.read(read_bytes)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 930, in read
data = self._read1(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 998, in _read1
data += self._read2(n - len(data))
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 1030, in _read2
data = self._fileobj.read(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 753, in read
self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'
If I extract the file into finder (using mac), then I see this image:
Also, if I try to convert the RGB image into into a numpy array np.array(img), I get this result:
What am I doing wrong?
I obtain the following error when typing mkdocs serve in terminal:
ERROR - Error reading page 'codereference.md': <class '_ast.ExtSlice'>
(with a very long traceback, see below)
My mkdocs.yaml file is:
site_name: Code Documentation
site_url: https://example.com/
nav:
- Home: index.md
- About: about.md
- Code: codereference.md
theme: readthedocs
plugins:
- mkdocstrings:
handlers:
python:
paths: [.]
The codereference.md file consists of the following:
# Reference
::: path.to.class.from.where.mkdocsyaml.is
Anybody have any possible answers?
The full traceback is:
Traceback (most recent call last):
File "/usr/local/bin/mkdocs", line 8, in <module>
sys.exit(cli())
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/__main__.py", line 181, in serve_command
serve.serve(dev_addr=dev_addr, livereload=livereload, watch=watch, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/serve.py", line 63, in serve
config = builder()
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/serve.py", line 58, in builder
build(config, live_server=live_server, dirty=dirty)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 292, in build
_populate_page(file.page, config, files, dirty)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 174, in _populate_page
page.render(config, files)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/structure/pages.py", line 175, in render
self.content = md.convert(self.markdown)
File "/usr/local/lib/python3.8/dist-packages/markdown/core.py", line 264, in convert
root = self.parser.parseDocument(self.lines).getroot()
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 90, in parseDocument
self.parseChunk(self.root, '\n'.join(lines))
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 105, in parseChunk
self.parseBlocks(parent, text.split('\n\n'))
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 123, in parseBlocks
if processor.run(parent, blocks) is not False:
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings/extension.py", line 121, in run
html, handler, data = self._process_block(identifier, block, heading_level)
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings/extension.py", line 195, in _process_block
data: CollectorItem = handler.collect(identifier, options)
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings_handlers/python/handler.py", line 191, in collect
loader.load_module(module_name)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 148, in load_module
top_module = self._load_module(package.name, package.path, submodules=submodules)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 352, in _load_module
return self._load_module_path(module_name, module_path, submodules, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 380, in _load_module_path
self._load_submodules(module)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 385, in _load_submodules
self._load_submodule(module, subparts, subpath)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 396, in _load_submodule
member_parent[subparts[-1]] = self._load_module(
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 352, in _load_module
return self._load_module_path(module_name, module_path, submodules, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 374, in _load_module_path
module = self._visit_module(code, module_name, module_path, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 413, in _visit_module
module = visit(
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 92, in visit
return Visitor(
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 172, in get_module
self.visit(top_node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 214, in visit_module
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 255, in visit_classdef
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 451, in visit_functiondef
self.handle_function(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 442, in handle_function
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 589, in visit_assign
self.handle_attribute(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 548, in handle_attribute
value = get_value(node.value) # type: ignore[arg-type]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1181, in get_value
return _node_value_map[type(node)](node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1047, in _get_subscript_value
subscript = _get_value(node.slice)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1167, in _get_value
return _node_value_map[type(node)](node)
KeyError: <class '_ast.ExtSlice'>
It's fixed in version 0.21.0 of Griffe: https://mkdocstrings.github.io/griffe/changelog/#bug-fixes
It would previously crash when unparsing values using extended slices such as o[x:y,z].
I was trying to render text with LaTeX in Matplotlib.
There is a demo provided by Matplotlib at https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/tex_demo.html.
However, when I ran this demo, I got this error saying "RuntimeError: Failed to process string with tex because latex could not be found", but I'm sure I have installed Miktex and Ghostscript required by Matlibplot.
Anyone knows how should I solve it?
Thanks.
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)
ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
plt.show()
Here is the error trace back:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
When I tried to change the standard font for matplotlib, I likely incorrectly applied a recommended script and broke my matplotlib and from that moment on always got this error message.
Simply resetting matplotlib settings worked perfectly for me.
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
I managed to solve this issue by installing texlive and latex. In apt based Linux, this command installs texlive:
sudo apt install texlive texlive-latex-extra texlive-fonts-recommended dvipng
latex can be easily installed via pip:
pip install latex
According to the matplotlib documentation:
Matplotlib's LaTeX support requires a working LaTeX installation, dvipng (which may be included with your LaTeX installation), and Ghostscript (GPL Ghostscript 9.0 or later is required). The executables for these external dependencies must all be located on your PATH.
If you're not familiar with PATH, this means that the .exe files of these programs need to be in your system environment variables. In Windows, Edit the System Environment Variables, click on variable Path, and add the filepaths of the aforementioned programs to the list.
Get LaTex here
Get dvipng here
Get Ghostscript > 9.0 here
Matplotlib actually ships with its own way of doing TeX-style text, called mathtex:
Note that you do not need to have TeX installed, since Matplotlib
ships its own TeX expression parser, layout engine, and fonts. The
layout engine is a fairly direct adaptation of the layout algorithms
in Donald Knuth's TeX, so the quality is quite good (matplotlib also
provides a usetex option for those who do want to call out to TeX to
generate their text (see Text rendering With LaTeX).
In most cases, using this is sufficient and requires less stuff installed.
It's used in exactly the same way as LaTeX:
r'$\sum_{i=0}^\infty x_i$'
generates
For some reason, MikTeX doesn't seem to add it's bin folder to PATH during installation. Be sure to add:
Local Install:
%LOCALAPPDATA%\Programs\MiKTeX\miktex\bin\x64\
System (Admin) Install:
C:\Program Files\MiKTeX\miktex\bin\x64\
to your PATH after installation. (Ironically, this gets removed from PATH when MikTeX is uninstalled). This should fix the problem.
NOTE: %LOCALAPPDATA% is C:\Users\your-user-name\AppData\Local.
Also, if you only need or want some text expressions rendered in LaTeX, and not the entire plot, matplotlib uses a subset of LaTeX under the hood so you don't need to install LaTeX or set rcParams in that case.
e.g. On
OS: Windows 10
Python: 3.8.10
matplotlib: 3.6.2
MikTeX: 22.10-x64
Ghostscript: 10.00.0
notice the difference between the two:
Without rcParams
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)
ax.set_xlabel(r'$\mathbf{time, t (s)}$')
ax.set_ylabel(r'$Velocity, v (\phi/sec)$', fontsize=16)
ax.set_title(r'Without rcParams', fontsize=16, color='r')
plt.show()
With rcParams
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams.update({
'text.usetex': True,
'font.family': 'serif',
})
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)
ax.set_xlabel(r'$\mathbf{time, t (s)}$')
ax.set_ylabel(r'$Velocity, v (\phi/sec)$', fontsize=16)
ax.set_title(r'With rcParams', fontsize=16, color='r')
plt.show()
It depends on what you want to do.
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update(plt.rcParamsDefault)
this must be resolve the above issue, by putting the default paramater for the rcParams.
I have created a custom environment for reinforcement learning with tf-agents (not needed to answer this question), which works fine if I instantiate one thread by setting num_parallel_environments to 1, but throws infrequent and seemingly random errors like an IndexError inside random.shuffle(), when I increase num_parallel_environments to 50. Here's the code:
inside train.py
tf_env = tf_py_environment.TFPyEnvironment(
batched_py_environment.BatchedPyEnvironment(
[environment.CardGameEnv()] * num_parallel_environments))
inside my environment, this is run in threads
self.cardStack = getFullDeck()
random.shuffle(self.cardStack)
this is a normal function, imported in every thread class
def getFullDeck():
deck = []
for rank in Ranks:
for suit in Suits:
deck.append(Card(rank, suit))
return deck
And here's one of the possible errors:
Traceback (most recent call last):
File "e:\Users\tmp\.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "e:\Users\tmp\.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\lib\python\ptvsd\__main__.py", line 348, in main
run()
File "e:\Users\tmp\.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\lib\python\ptvsd\__main__.py", line 253, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Python37\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "e:\Users\tmp\Documents\Programming\Neural Nets\Poker_AI\train_v2.py", line 320, in <module>
app.run(main)
File "C:\Python37\lib\site-packages\absl\app.py", line 300, in run
_run_main(main, args)
File "C:\Python37\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "e:\Users\tmp\Documents\Programming\Neural Nets\Poker_AI\train_v2.py", line 315, in main
num_eval_episodes=FLAGS.num_eval_episodes)
File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\gin\config.py", line 1032, in wrapper
utils.augment_exception_message_and_reraise(e, err_str)
File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\gin\utils.py", line 49, in augment_exception_message_and_reraise
six.raise_from(proxy.with_traceback(exception.__traceback__), None)
File "<string>", line 3, in raise_from
File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\gin\config.py", line 1009, in wrapper
return fn(*new_args, **new_kwargs)
File "e:\Users\tmp\Documents\Programming\Neural Nets\Poker_AI\train_v2.py", line 251, in train_eval
collect_driver.run()
File "C:\Python37\lib\site-packages\tf_agents\drivers\dynamic_episode_driver.py", line 149, in run
maximum_iterations=maximum_iterations)
File "C:\Python37\lib\site-packages\tf_agents\utils\common.py", line 111, in with_check_resource_vars
return fn(*fn_args, **fn_kwargs)
File "C:\Python37\lib\site-packages\tf_agents\drivers\dynamic_episode_driver.py", line 180, in _run
name='driver_loop'
File "C:\Python37\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2457, in while_loop_v2
return_same_structure=True)
File "C:\Python37\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2689, in while_loop
loop_vars = body(*loop_vars)
File "C:\Python37\lib\site-packages\tf_agents\drivers\dynamic_episode_driver.py", line 103, in loop_body
next_time_step = self.env.step(action_step.action)
File "C:\Python37\lib\site-packages\tf_agents\environments\tf_environment.py", line 232, in step
return self._step(action)
File "C:\Python37\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 232, in graph_wrapper
return func(*args, **kwargs)
File "C:\Python37\lib\site-packages\tf_agents\environments\tf_py_environment.py", line 218, in _step
_step_py, flat_actions, self._time_step_dtypes, name='step_py_func')
File "C:\Python37\lib\site-packages\tensorflow\python\ops\script_ops.py", line 488, in numpy_function
return py_func_common(func, inp, Tout, stateful=True, name=name)
File "C:\Python37\lib\site-packages\tensorflow\python\ops\script_ops.py", line 452, in py_func_common
result = func(*[x.numpy() for x in inp])
File "C:\Python37\lib\site-packages\tf_agents\environments\tf_py_environment.py", line 203, in _step_py
self._time_step = self._env.step(packed)
File "C:\Python37\lib\site-packages\tf_agents\environments\py_environment.py", line 174, in step
self._current_time_step = self._step(action)
File "C:\Python37\lib\site-packages\tf_agents\environments\batched_py_environment.py", line 140, in _step
zip(self._envs, unstacked_actions))
File "C:\Python37\lib\multiprocessing\pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:\Python37\lib\multiprocessing\pool.py", line 657, in get
raise self._value
File "C:\Python37\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Python37\lib\multiprocessing\pool.py", line 44, in mapstar
return list(map(*args))
File "C:\Python37\lib\site-packages\tf_agents\environments\batched_py_environment.py", line 139, in <lambda>
lambda env_action: env_action[0].step(env_action[1]),
File "C:\Python37\lib\site-packages\tf_agents\environments\py_environment.py", line 174, in step
self._current_time_step = self._step(action)
File "e:\Users\tmp\Documents\Programming\Neural Nets\Poker_AI\environment.py", line 116, in _step
canRoundContinue = self._table.runUntilChoice(action)
File "e:\Users\tmp\Documents\Programming\Neural Nets\Poker_AI\table.py", line 326, in runUntilChoice
random.shuffle(self.cardStack)
File "C:\Python37\lib\random.py", line 278, in shuffle
x[i], x[j] = x[j], x[i]
IndexError: list index out of range
In call to configurable 'train_eval' (<function train_eval at 0x000002722713A158>)
I suspect this error occurs because the threads are changing the array simultaneously, but I do not see why this would be the case:
Everything happens inside a class instance and the array getFullDeck() is returning is recreated every time the function is called, so there should be no way multiple threads have access to the same reference, right?
tf_env = tf_py_environment.TFPyEnvironment(
batched_py_environment.BatchedPyEnvironment(
[environment.CardGameEnv()] * num_parallel_environments))
You are reusing the same environment for each of the parallel instances rather than creating a new environment for each one. You might want to try something like
tf_env = tf_py_environment.TFPyEnvironment(
batched_py_environment.BatchedPyEnvironment(
[environment.CardGameEnv() for _ in range(num_parallel_environments)]))
I tried this using Python 3.6 and the current reportlab and svg2rlg libraries (as of this posting) and I got the error in the title.:
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg
c = Canvas(path+'To Print.pdf')
img27 = svg2rlg(path_to_.svg')
wd27 = img27.width/inch
ht27 = img27.height/inch
renderPDF.draw(img27, c, x=x27, y=y27)
It usually works just fine and I cannot figure out how to troubleshoot it.
I've tried searching for this error but cannot find it anywhere.
Here's the complete traceback:
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Drive/Scripts/PDF.py", line 604, in <module>
renderPDF.draw(img27, c, x=x27, y=y27)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 29, in draw
R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 201, in draw
self.drawNode(drawing)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 282, in drawNodeDispatcher
self.drawGroup(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 311, in drawGroup
self.drawNode(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 54, in drawNode
self.drawNodeDispatcher(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderbase.py", line 278, in drawNodeDispatcher
self.drawPath(node)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/renderPDF.py", line 183, in drawPath
isClosed = _renderPath(path, drawFuncs, forceClose=fill and autoclose=='pdf')
File "/anaconda3/lib/python3.6/site-packages/svglib/svglib.py", line 1075, in patchedRenderPath
return original_renderPath(path, drawFuncs, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/reportlab/graphics/shapes.py", line 996, in _renderPath
drawFuncs[op](*points[i:j])
File "/anaconda3/lib/python3.6/site-packages/reportlab/pdfgen/pathobject.py", line 127, in close
self._code_append('h')
File "/anaconda3/lib/python3.6/site-packages/reportlab/pdfgen/pathobject.py", line 36, in _init_code_append
assert c.endswith(' m') or c.endswith(' re'), 'path must start with a moveto or rect'
AssertionError: path must start with a moveto or rect
It seems the last assert is over-zealous.
File "/anaconda3/lib/python3.6/site-packages/reportlab/pdfgen/pathobject.py", line 36, in _init_code_append
assert c.endswith(' m') or c.endswith(' re'), 'path must start with a moveto or rect'
The actual value of c at this location is "h" (as filled in by the call just above in the stack trace). If you just comment out the assert it works fine.
Please also let the nice people at ReportLab know so they can fix in the future.