Example #1
0
def get_cache_dir():
    cachedir = mpl.get_cachedir()
    if cachedir is None:
        raise RuntimeError('Could not find a suitable configuration directory')
    cache_dir = os.path.join(cachedir, 'test_cache')
    try:
        Path(cache_dir).mkdir(parents=True, exist_ok=True)
    except IOError:
        return None
    if not os.access(cache_dir, os.W_OK):
        return None
    return cache_dir
Example #2
0
# Unable to find font cache of matplotlib on a mac
import matplotlib
matplotlib.get_cachedir()
Example #3
0
import matplotlib as mpl
print mpl.get_configdir()
print mpl.get_cachedir()
print mpl.__version__
Example #4
0
            prop = prop.get_fontconfig_pattern()
        cached = _fc_match_cache.get(prop)
        if cached is not None:
            return cached

        result = fc_match(prop, fontext)
        if result is None:
            result = fc_match(':', fontext)

        _fc_match_cache[prop] = result
        return result

else:
    _fmcache = None

    cachedir = get_cachedir()
    if cachedir is not None:
        _fmcache = os.path.join(cachedir, 'fontList.json')

    fontManager = None

    def _rebuild():
        global fontManager

        fontManager = FontManager()

        if _fmcache:
            with cbook.Locked(cachedir):
                json_dump(fontManager, _fmcache)
        _log.info("generated new fontManager")
Example #5
0
def _get_cache_path():
    cache_dir = Path(mpl.get_cachedir(), 'test_cache')
    cache_dir.mkdir(parents=True, exist_ok=True)
    return cache_dir
Example #6
0
sns.set(font_scale=1)

#>>>>>> 1. Korean Font Setting
import platform
system = platform.system()

# -*- coding: UTF-8 -*-
get_ipython().run_line_magic('matplotlib', 'inline')

import matplotlib as mpl  # 기본 설정 만지는 용도
import matplotlib.pyplot as plt  # 그래프 그리는 용도
import matplotlib.font_manager as fm  # 폰트 관련 용도
print('버전: ', mpl.__version__)
print('설치 위치: ', mpl.__file__)
print('설정 위치: ', mpl.get_configdir())
print('캐시 위치: ', mpl.get_cachedir())
print('설정 파일 위치: ', mpl.matplotlib_fname())
font_list = fm.findSystemFonts(fontpaths=None, fontext='ttf')

if system == 'Windows':
    datapath = os.getcwd() + '\\'
    imagepath = datapath + 'images\\'

    # ttf 폰트 전체개수
    font_list[:10]

    f = [f.name for f in fm.fontManager.ttflist]
    f[:10]

    [(f.name, f.fname) for f in fm.fontManager.ttflist if 'Nanum' in f.name]
Example #7
0
class TexManager:
    """
    Convert strings to dvi files using TeX, caching the results to a directory.

    Repeated calls to this constructor always return the same instance.
    """

    # Caches.
    texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
    grey_arrayd = {}

    font_family = 'serif'
    font_families = ('serif', 'sans-serif', 'cursive', 'monospace')

    font_info = {
        'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
        'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
        'times': ('ptm', r'\usepackage{mathptmx}'),
        'palatino': ('ppl', r'\usepackage{mathpazo}'),
        'zapf chancery': ('pzc', r'\usepackage{chancery}'),
        'cursive': ('pzc', r'\usepackage{chancery}'),
        'charter': ('pch', r'\usepackage{charter}'),
        'serif': ('cmr', ''),
        'sans-serif': ('cmss', ''),
        'helvetica': ('phv', r'\usepackage{helvet}'),
        'avant garde': ('pag', r'\usepackage{avant}'),
        'courier': ('pcr', r'\usepackage{courier}'),
        # Loading the type1ec package ensures that cm-super is installed, which
        # is necessary for unicode computer modern.  (It also allows the use of
        # computer modern at arbitrary sizes, but that's just a side effect.)
        'monospace': ('cmtt', r'\usepackage{type1ec}'),
        'computer modern roman': ('cmr', r'\usepackage{type1ec}'),
        'computer modern sans serif': ('cmss', r'\usepackage{type1ec}'),
        'computer modern typewriter': ('cmtt', r'\usepackage{type1ec}')
    }

    cachedir = cbook.deprecated("3.3",
                                alternative="matplotlib.get_cachedir()")(
                                    property(lambda self: mpl.get_cachedir()))
    rgba_arrayd = cbook.deprecated("3.3")(property(lambda self: {}))
    _fonts = {}  # Only for deprecation period.
    serif = cbook.deprecated("3.3")(
        property(lambda self: self._fonts.get("serif", ('cmr', ''))))
    sans_serif = cbook.deprecated("3.3")(
        property(lambda self: self._fonts.get("sans-serif", ('cmss', ''))))
    cursive = cbook.deprecated("3.3")(property(lambda self: self._fonts.get(
        "cursive", ('pzc', r'\usepackage{chancery}'))))
    monospace = cbook.deprecated("3.3")(
        property(lambda self: self._fonts.get("monospace", ('cmtt', ''))))

    @functools.lru_cache()  # Always return the same instance.
    def __new__(cls):
        Path(cls.texcache).mkdir(parents=True, exist_ok=True)
        return object.__new__(cls)

    def get_font_config(self):
        ff = rcParams['font.family']
        if len(ff) == 1 and ff[0].lower() in self.font_families:
            self.font_family = ff[0].lower()
        else:
            _log.info(
                'font.family must be one of (%s) when text.usetex is '
                'True. serif will be used by default.',
                ', '.join(self.font_families))
            self.font_family = 'serif'

        fontconfig = [self.font_family]
        for font_family in self.font_families:
            for font in rcParams['font.' + font_family]:
                if font.lower() in self.font_info:
                    self._fonts[font_family] = self.font_info[font.lower()]
                    _log.debug('family: %s, font: %s, info: %s', font_family,
                               font, self.font_info[font.lower()])
                    break
                else:
                    _log.debug('%s font is not compatible with usetex.', font)
            else:
                _log.info(
                    'No LaTeX-compatible font found for the %s font '
                    'family in rcParams. Using default.', font_family)
                self._fonts[font_family] = self.font_info[font_family]
            fontconfig.append(self._fonts[font_family][0])
        # Add a hash of the latex preamble to fontconfig so that the
        # correct png is selected for strings rendered with same font and dpi
        # even if the latex preamble changes within the session
        preamble_bytes = self.get_custom_preamble().encode('utf-8')
        fontconfig.append(hashlib.md5(preamble_bytes).hexdigest())

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = [
            self._fonts['serif'][1], self._fonts['sans-serif'][1],
            self._fonts['monospace'][1]
        ]
        if self.font_family == 'cursive':
            cmd.append(self._fonts['cursive'][1])
        self._font_preamble = '\n'.join([r'\usepackage{type1cm}', *cmd])

        return ''.join(fontconfig)

    def get_basefile(self, tex, fontsize, dpi=None):
        """
        Return a filename based on a hash of the string, fontsize, and dpi.
        """
        s = ''.join([
            tex,
            self.get_font_config(),
            '%f' % fontsize,
            self.get_custom_preamble(),
            str(dpi or '')
        ])
        return os.path.join(self.texcache,
                            hashlib.md5(s.encode('utf-8')).hexdigest())

    def get_font_preamble(self):
        """
        Return a string containing font configuration for the tex preamble.
        """
        return self._font_preamble

    def get_custom_preamble(self):
        """Return a string containing user additions to the tex preamble."""
        return rcParams['text.latex.preamble']

    def _get_preamble(self):
        return "\n".join([
            r"\documentclass{article}",
            # Pass-through \mathdefault, which is used in non-usetex mode to
            # use the default text font but was historically suppressed in
            # usetex mode.
            r"\newcommand{\mathdefault}[1]{#1}",
            self._font_preamble,
            r"\usepackage[utf8]{inputenc}",
            r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
            # geometry is loaded before the custom preamble as convert_psfrags
            # relies on a custom preamble to change the geometry.
            r"\usepackage[papersize=72in, margin=1in]{geometry}",
            self.get_custom_preamble(),
            # textcomp is loaded last (if not already loaded by the custom
            # preamble) in order not to clash with custom packages (e.g.
            # newtxtext) which load it with different options.
            r"\makeatletter"
            r"\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}"
            r"\makeatother",
        ])

    def make_tex(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')

        Path(texfile).write_text(r"""
%s
\pagestyle{empty}
\begin{document}
%% The empty hbox ensures that a page is printed even for empty inputs, except
%% when using psfrag which gets confused by it.
\fontsize{%f}{%f}%%
\ifdefined\psfrag\else\hbox{}\fi%%
%s
\end{document}
""" % (self._get_preamble(), fontsize, fontsize * 1.25, fontcmd % tex),
                                 encoding='utf-8')

        return texfile

    _re_vbox = re.compile(
        r"MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)pt")

    @cbook.deprecated("3.3")
    def make_tex_preview(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        It uses the preview.sty to determine the dimension (width, height,
        descent) of the output.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')

        # newbox, setbox, immediate, etc. are used to find the box
        # extent of the rendered text.

        Path(texfile).write_text(r"""
%s
\usepackage[active,showbox,tightpage]{preview}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\def\showbox#1%%
{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}}

\begin{document}
\begin{preview}
{\fontsize{%f}{%f}%s}
\end{preview}
\end{document}
""" % (self._get_preamble(), fontsize, fontsize * 1.25, fontcmd % tex),
                                 encoding='utf-8')

        return texfile

    def _run_checked_subprocess(self, command, tex, *, cwd=None):
        _log.debug(cbook._pformat_subprocess(command))
        try:
            report = subprocess.check_output(
                command,
                cwd=cwd if cwd is not None else self.texcache,
                stderr=subprocess.STDOUT)
        except FileNotFoundError as exc:
            raise RuntimeError(
                'Failed to process string with tex because {} could not be '
                'found'.format(command[0])) from exc
        except subprocess.CalledProcessError as exc:
            raise RuntimeError(
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full report generated by {prog}:\n'
                '{exc}\n\n'.format(prog=command[0],
                                   tex=tex.encode('unicode_escape'),
                                   exc=exc.output.decode('utf-8'))) from exc
        _log.debug(report)
        return report

    def make_dvi(self, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """

        if dict.__getitem__(rcParams, 'text.latex.preview'):
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            # Generate the dvi in a temporary directory to avoid race
            # conditions e.g. if multiple processes try to process the same tex
            # string at the same time.  Having tmpdir be a subdirectory of the
            # final output dir ensures that they are on the same filesystem,
            # and thus replace() works atomically.
            with TemporaryDirectory(dir=Path(dvifile).parent) as tmpdir:
                self._run_checked_subprocess([
                    "latex", "-interaction=nonstopmode", "--halt-on-error",
                    texfile
                ],
                                             tex,
                                             cwd=tmpdir)
                (Path(tmpdir) / Path(dvifile).name).replace(dvifile)
        return dvifile

    @cbook.deprecated("3.3")
    def make_dvi_preview(self, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        It calls make_tex_preview() method and store the size information
        (width, height, descent) in a separate file.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if not os.path.exists(dvifile) or not os.path.exists(baselinefile):
            texfile = self.make_tex_preview(tex, fontsize)
            report = self._run_checked_subprocess([
                "latex", "-interaction=nonstopmode", "--halt-on-error", texfile
            ], tex)

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report.decode("utf-8"))
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if not fname.endswith(('dvi', 'tex', 'baseline')):
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_png(self, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile
        # see get_rgba for a discussion of the background
        if not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            cmd = [
                "dvipng", "-bg", "Transparent", "-D",
                str(dpi), "-T", "tight", "-o", pngfile, dvifile
            ]
            # When testing, disable FreeType rendering for reproducibility; but
            # dvipng 1.16 has a bug (fixed in f3ff241) that breaks --freetype0
            # mode, so for it we keep FreeType enabled; the image will be
            # slightly off.
            if (getattr(mpl, "_called_from_pytest", False)
                    and mpl._get_executable_info("dvipng").version != "1.16"):
                cmd.insert(1, "--freetype0")
            self._run_checked_subprocess(cmd, tex)
        return pngfile

    def get_grey(self, tex, fontsize=None, dpi=None):
        """Return the alpha channel."""
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        key = tex, self.get_font_config(), fontsize, dpi
        alpha = self.grey_arrayd.get(key)
        if alpha is None:
            pngfile = self.make_png(tex, fontsize, dpi)
            rgba = mpl.image.imread(os.path.join(self.texcache, pngfile))
            self.grey_arrayd[key] = alpha = rgba[:, :, -1]
        return alpha

    def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        """Return latex's rendering of the tex string as an rgba array."""
        alpha = self.get_grey(tex, fontsize, dpi)
        rgba = np.empty((*alpha.shape, 4))
        rgba[..., :3] = mpl.colors.to_rgb(rgb)
        rgba[..., -1] = alpha
        return rgba

    def get_text_width_height_descent(self, tex, fontsize, renderer=None):
        """Return width, height and descent of the text."""
        if tex.strip() == '':
            return 0, 0, 0

        dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1

        if dict.__getitem__(rcParams, 'text.latex.preview'):
            # use preview.sty
            basefile = self.get_basefile(tex, fontsize)
            baselinefile = '%s.baseline' % basefile

            if not os.path.exists(baselinefile):
                dvifile = self.make_dvi_preview(tex, fontsize)

            with open(baselinefile) as fh:
                l = fh.read().split()
            height, depth, width = [float(l1) * dpi_fraction for l1 in l]
            return width, height + depth, depth

        else:
            # use dviread.
            dvifile = self.make_dvi(tex, fontsize)
            with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
                page, = dvi
            # A total height (including the descent) needs to be returned.
            return page.width, page.height + page.descent, page.descent
Example #8
0
    def run(self):
        w, w_star, w_all, U_all, d_all, L2, graph = self.make_variables_noise_after_2(
            self.N, self.m, self.r_i, self.sparsity_percentage,
            self.how_weakly_sparse, self.w_noise, self.normal_distribution,
            self.w_zero)
        u_eig, u_vec = np.linalg.eig(U_all.T @ U_all)
        print(min(u_eig))
        # plt.rcParams['text.usetex'] = True
        # plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath']
        # plt.rcParams['font.family'] = 'sans-serif'
        # plt.rcParams['font.sans-serif'] = 'Helvetica'
        # plt.rcParams["font.family"] = "Times New Roman"
        #
        matplotlib.rcParams['ps.useafm'] = True
        matplotlib.rcParams['pdf.use14corefonts'] = True
        matplotlib.rcParams['text.usetex'] = True
        pdf = PdfPages('main performance comparison SSP.pdf')
        plt.rcParams[
            'xtick.direction'] = 'in'  #x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams[
            'ytick.direction'] = 'in'  #y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams['xtick.major.width'] = 1.0  #x軸主目盛り線の線幅
        plt.rcParams['ytick.major.width'] = 1.0  #y軸主目盛り線の線幅
        plt.rcParams['font.size'] = 10  #フォントの大きさ
        plt.rcParams['axes.linewidth'] = 1.0  # 軸の線幅edge linewidth。囲みの太さ
        print(matplotlib.get_cachedir())

        # error_mc = []
        # smallest_error = 0
        # optimal_lamb= 0
        # optimal_rho = 0
        # for i in range(100):
        #     for j in range(100):
        #         lamb = 0.01 + 0.01*i
        #         rho = 0.1 + 0.1*j
        #         error_centralized_mc,wcmc = self.centralized_mc(U_all,d_all,w,w_star,L2,lamb,0.001,rho,100)
        #         if error_centralized_mc[-1] < smallest_error:
        #             optimal_lamb = lamb
        #             optimal_rho = rho
        #             smallest_error = error_centralized_mc[-1]
        #         print(i,".",j,"\% done")
        # print(optimal_lamb,optimal_rho)

        # smallest_error = 0
        # optimal_lamb= 0
        # for i in range(100):
        #     lamb = 0.01 + 0.01*i
        #     error_centralized_L1,wcl1 = self.centralized_L1(U_all,d_all,w,w_star,L2,lamb,0.001,100)
        #     if error_centralized_L1[-1] < smallest_error:
        #         optimal_lamb = lamb
        #         smallest_error = error_centralized_L1[-1]
        #     print(optimal_lamb)
        # print(optimal_lamb)

        # error_mc = []
        # smallest_error = 0
        # optimal_lamb= 0
        # optimal_rho = 0
        # for i in range(100):
        #     for j in range(100):
        #         lamb = 0.01 + 0.01*i
        #         rho = 0.1 + 0.1*j
        #         error_centralized_smc,wcsmc = self.centralized_mc_twin_with_amc(U_all,d_all,w,w_star,L2,lamb,0.001,rho,100,self.m)
        #         if error_centralized_smc[-1] < smallest_error:
        #             optimal_lamb = lamb
        #             optimal_rho = rho
        #             smallest_error = error_centralized_smc[-1]
        #         print(i,j,optimal_lamb,optimal_rho)
        # print(optimal_lamb,optimal_rho)

        # error_mc = []
        # smallest_error = 0
        # optimal_lamb= 0
        # optimal_rho = 0
        # for i in range(100):
        #     for j in range(100):
        #         lamb = 0.001 + 0.001*i
        #         rho = 0.0001 + 0.0001*j
        #         error_approximate_mc,wcmc = self.pg_extra_approximate_mc(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,optimal_lamb/self.m,0.02,optimal_rho/self.m,self.iteration,graph,w_all)
        #         if error_approximate_mc[-1] < smallest_error:
        #             optimal_lamb = lamb
        #             optimal_rho = rho
        #             smallest_error = error_approximate_mc[-1]
        # print(optimal_lamb,optimal_rho)

        error_centralized_mc, wcmc = self.centralized_mc(
            U_all, d_all, w, w_star, L2, 0.29, 0.001, 1.9, 100)
        error_l1, wl1 = self.centralized_L1(U_all, d_all, w, w_star, L2, 0.22,
                                            10, 100)

        # error_centralized_mc_twin,wcmc_twin = self.centralized_mc_twin(U_all,d_all,w,w_star,L2,0.04,0.001,0.09,10000,self.m)
        error_extra, wex = self.extra(U_all, d_all, w_star, L2, self.N, self.m,
                                      self.r_i, self.lamb, 0.02, self.rho,
                                      self.iteration, graph, w_all)
        # self.pg_extra_l1(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,0.1/self.m,0.02,0.09,self.iteration,graph,w_all)
        # self.pg_extra_mc_nonconvex_twin(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,0.04/self.m,0.02,0.09/self.m,self.iteration,graph,w_all)
        # self.prox_dgd(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,0.4/self.m,0.02,17/self.m,self.iteration,graph,w_all)
        error_amc, wamc = self.pg_extra_approximate_mc(
            U_all, d_all, w_star, L2, self.N, self.m, self.r_i, 0.26 / self.m,
            0.01, 1.1 / self.m, self.iteration, graph, w_all)
        I = np.eye(self.N)
        I_approximation = U_all.T @ U_all / self.m
        print("w difference", np.linalg.norm(wcmc - wamc))
        print("approximation validity", np.linalg.norm(I - I_approximation))
        print("amc difference from wstar", error_amc[-1])
        print("mc difference from wstar", error_centralized_mc[-1])
        print("l1 difference", error_l1[-1])
        print("extra error", error_extra[-1])

        plt.xlabel("Iterations", fontsize=12)
        plt.ylabel("System Mismatch (dB)", fontsize=12)
        plt.grid(which="major")
        # plt.axhline(y = error[-1],linestyle="dashed",label = "Centralized L1 penalty",color = "green")
        plt.axhline(y=error_centralized_mc[-1],
                    linestyle="dashed",
                    label="Centralized MC penalty",
                    color="green")
        # plt.axhline(y = error_centralized_mc_twin[-1],linestyle = "dashed",label = "Centralized Approximate MC penalty",color = "purple")
        # pdf.savefig()
        plt.legend(fontsize=12)
        # plt.savefig('SSP_system_mismatch_main_result.pdf')
        plt.show()
        x = range(len(w_star))
        # plt.plot(x,extra,label = "extra")
        # plt.plot(x,wcl1,label = "L1")
        plt.plot(x, wcmc, label="mc")
        plt.plot(x, wamc, label="mc")
        plt.plot(x, w_star, color="black")
        plt.legend()
        plt.show()
Example #9
0
#/space/erebus/1/users/jwang/jon2/bin/python3.6 motion_snr_plots_v3.py
'''
Functions:

'''
import csv
import os
import math
import numpy as np
import nibabel as nib
import os.path
import matplotlib
print(matplotlib.get_cachedir())
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import random
import glob
import fnmatch
from pylab import polyfit
from pylab import polyval
import utils

from numpy import linalg as LA
from pyquaternion import Quaternion
import matplotlib.patches as mpatches
import csv
from nipy.modalities.fmri.glm import GeneralLinearModel

outliers = [
    "BANDA051", "BANDA104", "BANDA131", "BANDA134", "BANDA129", "BANDA137"
]
#!/usr/bin/env python
"""
Add fonts to Matplotlib (for any operating system)

1. add the new font to your operating system.
   In this example, I'll add a Chinese font to Ubuntu::

       apt install fonts-wqy-zenhei
2. Update Matplotlib font cache with this script.

HOWEVER, this didn't work for me on multiple PCs with Matplotlib 2.2.0

See https://github.com/scivision/histfeas/blob/master/Plots/FirstAuroralConjugate.py
"""
import matplotlib
# %% remove old font cache
cachepath = matplotlib.get_cachedir()


matplotlib.font_manager._rebuild()
Example #11
0
    def run(self):
        w,w_star,w_all,U_all,d_all,L2,graph = self.make_variables_noise_after_2(self.N,self.m,self.r_i,self.sparsity_percentage,self.how_weakly_sparse,self.w_noise,self.normal_distribution,self.w_zero)
        
        matplotlib.rcParams['ps.useafm'] = True
        matplotlib.rcParams['pdf.use14corefonts'] = True
        matplotlib.rcParams['text.usetex'] = True 
        pdf = PdfPages('main performance comparison SSP.pdf')
        plt.rcParams['xtick.direction'] = 'in'#x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams['ytick.direction'] = 'in'#y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams['xtick.major.width'] = 1.0#x軸主目盛り線の線幅
        plt.rcParams['ytick.major.width'] = 1.0#y軸主目盛り線の線幅
        plt.rcParams['font.size'] = 10 #フォントの大きさ
        plt.rcParams['axes.linewidth'] = 1.0# 軸の線幅edge linewidth。囲みの太さ
        print(matplotlib.get_cachedir())

        
        w_star_all =np.loadtxt(f"./samples/sample_wstar_100_m={self.m}")
        U_all_all =np.loadtxt(f"./samples/sample_U_all_100_m={self.m}")
        d_all_all =np.loadtxt(f"./samples/sample_d_all_100_m={self.m}")
        graph_all =np.loadtxt(f"./samples/sample_graph_100_m={self.m}")
        
        data_number = 100
        train_number = 90
        test_number = data_number - train_number
        batch_number = 10
        list_prox = list(range(100))
        mc_test_error = []
        amc_test_error = []
        l1_test_error = []
        # for i in range(10):
        #     testing_list = list_prox[i*(self.m):(i+1)*(self.m)]
        #     first_list = list_prox[0:i*(self.m)]
        #     second_list= list_prox[(i+1)*self.m:100]
        #     all_list = first_list + second_list
        #     print(testing_list)
        #     print(all_list)
        # print(len(w_star_all[:,99]))
        for i in range(batch_number):
            train_w_star_1 = w_star_all[:,0:i*(test_number)]
            train_w_star_2 = w_star_all[:,(i+1)*test_number:data_number]
            train_w_star_list = np.append(train_w_star_1,train_w_star_2)
            test_w_star_list = w_star_all[:,i*(test_number):(i+1)*test_number]
            train_d_all_1 = d_all_all[:,0:i*(test_number)]
            train_d_all_2 = d_all_all[:,(i+1)*test_number:data_number]
            train_d_all_list = np.append(train_d_all_1 , train_d_all_2)
            test_d_all_list = d_all_all[:,i*(test_number):(i+1)*test_number]
            train_U_all_1 = U_all_all[:,0:i*(test_number)*self.N]
            train_U_all_2 = U_all_all[:,(i+1)*test_number*self.N:data_number*self.N]
            train_U_all_list = np.append(train_U_all_1 , train_U_all_2)
            test_U_all_list = U_all_all[:,i*(test_number)*self.N:(i+1)*test_number*self.N]
            train_graph_1 = graph_all[:,0:i*(test_number)*self.m]
            train_graph_2 = graph_all[:,(i+1)*test_number*self.m:data_number*self.m]
            train_graph_list = np.append(train_graph_1 , train_graph_2)
            test_graph_list = graph_all[:,i*(test_number)*self.m:(i+1)*test_number*self.m]
            mc_error_best = 0
            amc_error_best = 0
            l1_error_best = 0
            mc_optimal_lambda = 0
            mc_optimal_rho = 0
            amc_optimal_lambda = 0
            amc_optimal_rho = 0
            l1_optimal_lambda = 0
            for delta_lambda in range(100):
                for delta_rho in range(100):
                    mc_lamb = (delta_lambda+1)*0.01
                    mc_rho = (delta_rho+1)*0.01
                    amc_lamb = (delta_lambda+1)*0.01
                    amc_rho = (delta_rho+1)*0.01
                    l1_lamb = (delta_lambda+1)*0.01
                    mc_error_one_train = []
                    amc_error_one_train = []
                    l1_error_one_train = []
                    for train in range(train_number):
                        one_train_w_star = train_w_star_list[train*self.N:(train+1)*self.N]
                        one_train_w_star = one_train_w_star.reshape(self.N,1)
                        one_train_d_all = train_d_all_list[train*self.m:(train+1)*self.m]
                        one_train_d_all = one_train_d_all.reshape(self.m,1)
                        one_train_U_all = train_U_all_list[train*self.m*self.N:(train+1)*self.m*self.N]
                        one_train_U_all = one_train_U_all.reshape(self.m,self.N)
                        one_train_graph = train_graph_list[train*self.m*self.m:(train+1)*self.m*self.m]
                        one_train_graph = one_train_graph.reshape(self.m,self.m)
                        L2 = np.linalg.norm(one_train_w_star)
                        
                        error_centralized_mc,wcmc = self.centralized_mc(one_train_U_all,one_train_d_all,w,one_train_w_star,L2,0.039,0.001,0.0013,self.iteration)
                        error_l1,w_l1=self.pg_extra_l1(one_train_U_all,one_train_d_all,one_train_w_star,L2,self.N,self.m,self.r_i,0.039/self.m,0.02,0.09,self.iteration,one_train_graph,w_all,0,1)
                        error_amc,wamc = self.pg_extra_approximate_mc(one_train_U_all,one_train_d_all,one_train_w_star,L2,self.N,self.m,self.r_i,0.041/self.m,0.04,0.087/self.m,self.iteration,one_train_graph,w_all,1)
                        if error_centralized_mc[-1] < mc_error_best:
                            mc_error_best = error_centralized_mc[-1]
                            mc_optimal_lambda = mc_lamb
                            mc_optimal_rho = mc_rho
                        if error_amc[-1] < amc_error_best:
                            amc_error_best = error_amc[-1]
                            amc_optimal_lambda = amc_lamb
                            amc_optimal_rho = amc_rho
                        if error_l1[-1] < l1_error_best:
                            l1_error_best = error_l1[-1]
                            l1_optimal_lambda = l1_lamb
                        print("batch number : ",i,"\n train number : ",train ,"\n delta lambda : ",delta_lambda,"\n delta rho : ",delta_rho)
            for test in range(test_number):
                print("batch number : ",i,"\n test number : ",test , "\n delta lambda : ",delta_lambda,"\n delta rho : ",delta_rho)

                one_test_w_star = test_w_star_list[test*self.N:(test+1)*self.N]
                one_test_w_star = one_test_w_star.reshape(self.N,1)
                one_test_d_all = test_d_all_list[test*self.N:(test+1)*self.N]
                one_test_d_all = one_test_d_all.reshape(self.N,1)
                one_test_U_all = test_U_all_list[test*self.m*self.N:(test+1)*self.m*self.N]
                one_test_U_all = one_test_U_all.reshape(self.m,self.N)
                one_test_graph = test_graph_list[test*self.m*self.N:(test+1)*self.m*self.N]
                one_test_graph = one_test_graph.reshape(self.m,self.N)
                L2 = np.linalg.norm(one_train_w_star)


                error_centralized_mc,wcmc = self.centralized_mc(one_test_U_all,one_test_d_all,w,one_test_w_star,L2,mc_optimal_lambda,0.001,mc_optimal_rho,self.iteration)
                error_l1,w_l1=self.pg_extra_l1(one_test_U_all,one_test_d_all,one_test_w_star,L2,self.N,self.m,self.r_i,l1_optimal_lambda/self.m,0.02,0.09,self.iteration,one_test_graph,w_all,0,1)
                error_amc,wamc = self.pg_extra_approximate_mc(one_test_U_all,one_test_d_all,one_test_w_star,L2,self.N,self.m,self.r_i,amc_optimal_lambda/self.m,0.04,amc_optimal_rho/self.m,self.iteration,one_test_graph,w_all,1)
                mc_test_error.append(error_centralized_mc[-1])
                amc_test_error.append(error_amc[-1])
                l1_test_error.append(error_l1[-1])

        print(sum(mc_test_error)/len(mc_test_error))                
        print(sum(amc_test_error)/len(amc_test_error))                
        print(sum(l1_test_error)/len(l1_test_error))                
Example #12
0
    def run(self):
        # w,w_star,w_all,U_all,d_all,L2,graph = self.make_variables_noise_after_2(self.N,self.m,self.r_i,self.sparsity_percentage,self.how_weakly_sparse,self.w_noise,self.normal_distribution,self.w_zero)
        # u_eig,u_vec = np.linalg.eig(U_all.T@U_all)
        # print(min(u_eig))
        # plt.rcParams['text.usetex'] = True
        # plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath']
        # plt.rcParams['font.family'] = 'sans-serif'
        # plt.rcParams['font.sans-serif'] = 'Helvetica'
        # plt.rcParams["font.family"] = "Times New Roman"
        #
        matplotlib.rcParams['ps.useafm'] = True
        matplotlib.rcParams['pdf.use14corefonts'] = True
        matplotlib.rcParams['text.usetex'] = True
        pdf = PdfPages('main performance comparison SSP.pdf')
        plt.rcParams[
            'xtick.direction'] = 'in'  #x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams[
            'ytick.direction'] = 'in'  #y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
        plt.rcParams['xtick.major.width'] = 1.0  #x軸主目盛り線の線幅
        plt.rcParams['ytick.major.width'] = 1.0  #y軸主目盛り線の線幅
        plt.rcParams['font.size'] = 10  #フォントの大きさ
        plt.rcParams['axes.linewidth'] = 1.0  # 軸の線幅edge linewidth。囲みの太さ
        print(matplotlib.get_cachedir())

        # error_mc = []
        # smallest_error = 0
        # optimal_lamb= 0
        # optimal_rho = 0
        # for i in range(100):
        #     for j in range(100):
        #         lamb = 0.01 + 0.01*i
        #         rho = 0.01 + 0.01*j
        #         error_centralized_mc,wcmc = self.centralized_mc(U_all,d_all,w,w_star,L2,lamb,0.001,rho,self.iteration)
        #         if error_centralized_mc[-1] < smallest_error:
        #             optimal_lamb = lamb
        #             optimal_rho = rho
        #             smallest_error = error_centralized_mc[-1]
        #         print(optimal_lamb,optimal_rho)
        # print(optimal_lamb,optimal_rho)

        # error_mc = []
        # smallest_error = 0
        # optimal_lamb= 0
        # optimal_rho = 0
        # for i in range(100):
        #     for j in range(100):
        #         lamb = 0.01 + 0.01*i
        #         rho = 0.01 + 0.01*j
        #         error_approximate_mc,wcmc = self.centralized_mc_twin_with_amc(U_all,d_all,w,w_star,L2,lamb,0.02,rho,self.iteration,self.m)
        #         if error_approximate_mc[-1] < smallest_error:
        #             optimal_lamb = lamb
        #             optimal_rho = rho
        #             smallest_error = error_approximate_mc[-1]
        #         print(optimal_lamb,optimal_rho)
        # print(optimal_lamb,optimal_rho)

        # smallest_error = 0
        # optimal_lamb= 0
        # for i in range(100):
        #     lamb = 0.01 + 0.01*i
        #     error_centralized_L1,wcl1 = self.centralized_L1(U_all,d_all,w,w_star,L2,lamb,0.001,self.iteration)
        #     if error_centralized_L1[-1] < smallest_error:
        #         optimal_lamb = lamb
        #         smallest_error = error_centralized_L1[-1]
        #     print(optimal_lamb)
        # print(optimal_lamb)
        amc_list = []
        mc_list = []
        l1_list = []
        trials = 400
        i = 0
        I = np.eye(self.N)
        I_list = []
        while i < trials:
            B = (0.52 / 0.44)**0.5
            w, w_star, w_all, U_all, d_all, L2, graph = self.make_variables_noise_after_2(
                self.N, self.m, self.r_i, self.sparsity_percentage,
                self.how_weakly_sparse, self.w_noise, self.normal_distribution,
                self.w_zero)
            if self.centralized_convexity_checker(B, 0.44, U_all, len(w)):
                w, w_star, w_all, U_all, d_all, L2, graph = self.make_variables_noise_after_2(
                    self.N, self.m, self.r_i, self.sparsity_percentage,
                    self.how_weakly_sparse, self.w_noise,
                    self.normal_distribution, self.w_zero)

                error_centralized_mc, wcmc = self.centralized_mc(
                    U_all, d_all, w, w_star, L2, 0.44, 0.001, 0.52,
                    self.iteration)
                # error_l1,wl1 = self.centralized_L1(U_all,d_all,w,w_star,L2,0.22,10,self.iteration)

                # error_centralized_mc_twin,wcmc_twin = self.centralized_mc_twin(U_all,d_all,w,w_star,L2,0.44,0.001,0.52,10000,self.m)
                # error_extra,w_extra = self.extra(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,self.lamb,0.02,self.rho,self.iteration,graph,w_all)
                error_l1, w_l1 = self.pg_extra_l1(U_all, d_all, w_star, L2,
                                                  self.N, self.m, self.r_i,
                                                  0.22 / self.m, 0.02, 0.09,
                                                  self.iteration, graph, w_all,
                                                  0, 1)
                # self.pg_extra_mc_nonconvex_twin(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,0.4/self.m,0.02,17/self.m,self.iteration,graph,w_all)
                # self.prox_dgd(U_all,d_all,w_star,L2,self.N,self.m,self.r_i,0.4/self.m,0.02,17/self.m,self.iteration,graph,w_all)
                error_amc, wamc = self.pg_extra_approximate_mc(
                    U_all, d_all, w_star, L2, self.N, self.m, self.r_i,
                    0.41 / self.m, 0.04, 0.48 / self.m, self.iteration, graph,
                    w_all, 1)
                amc_list.append(error_amc[-1])
                mc_list.append(error_centralized_mc[-1][0])
                l1_list.append(error_l1[-1])
                I_approximation = U_all.T @ U_all / self.m
                I_list.append(np.linalg.norm(I - I_approximation))
                i += 1
        print("amc difference from wstar", sum(amc_list) / trials)
        print("mc difference from wstar", sum(mc_list) / trials)
        print("l1 difference", sum(l1_list) / trials)
        print("I approximation", sum(I_list) / trials)
Example #13
0
import re

# get locations
mpl_data_dir = Path(matplotlib.matplotlib_fname()).parent
mpl_font_dir = mpl_data_dir / 'fonts/ttf'
mpl_rc = mpl_data_dir / 'matplotlibrc'
# move the ttf file to taregt folder.
for font in Path('fonts').iterdir():
    shutil.move(str(font), str(mpl_font_dir))
# update config
old_rc = mpl_rc.read_text()
new_rc = old_rc.replace(
    '#font.family',
    'font.family',
)
# at least support Noto Sans TC
new_rc = re.sub(
    r'#font.sans-serif ?: ',
    'font.sans-serif : Noto Sans TC, ',
    new_rc,
)
new_rc = re.sub(
    r'#axes.unicode_minus ?: True',
    'axes.unicode_minus : False',
    new_rc,
)
with open(mpl_rc, 'w') as f:
    f.write(new_rc)
# delete the cache folder
shutil.rmtree(matplotlib.get_cachedir(), ignore_errors=True)
Example #14
0
class TexManager:
    """
    Convert strings to dvi files using TeX, caching the results to a directory.

    Repeated calls to this constructor always return the same instance.
    """

    texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
    _grey_arrayd = {}

    _font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
    _font_preambles = {
        'new century schoolbook': r'\renewcommand{\rmdefault}{pnc}',
        'bookman': r'\renewcommand{\rmdefault}{pbk}',
        'times': r'\usepackage{mathptmx}',
        'palatino': r'\usepackage{mathpazo}',
        'zapf chancery': r'\usepackage{chancery}',
        'cursive': r'\usepackage{chancery}',
        'charter': r'\usepackage{charter}',
        'serif': '',
        'sans-serif': '',
        'helvetica': r'\usepackage{helvet}',
        'avant garde': r'\usepackage{avant}',
        'courier': r'\usepackage{courier}',
        # Loading the type1ec package ensures that cm-super is installed, which
        # is necessary for Unicode computer modern.  (It also allows the use of
        # computer modern at arbitrary sizes, but that's just a side effect.)
        'monospace': r'\usepackage{type1ec}',
        'computer modern roman': r'\usepackage{type1ec}',
        'computer modern sans serif': r'\usepackage{type1ec}',
        'computer modern typewriter': r'\usepackage{type1ec}',
    }
    _font_types = {
        'new century schoolbook': 'serif',
        'bookman': 'serif',
        'times': 'serif',
        'palatino': 'serif',
        'zapf chancery': 'cursive',
        'charter': 'serif',
        'helvetica': 'sans-serif',
        'avant garde': 'sans-serif',
        'courier': 'monospace',
        'computer modern roman': 'serif',
        'computer modern sans serif': 'sans-serif',
        'computer modern typewriter': 'monospace',
    }

    grey_arrayd = _api.deprecate_privatize_attribute("3.5")
    font_family = _api.deprecate_privatize_attribute("3.5")
    font_families = _api.deprecate_privatize_attribute("3.5")
    font_info = _api.deprecate_privatize_attribute("3.5")

    @functools.lru_cache()  # Always return the same instance.
    def __new__(cls):
        Path(cls.texcache).mkdir(parents=True, exist_ok=True)
        return object.__new__(cls)

    @_api.deprecated("3.6")
    def get_font_config(self):
        preamble, font_cmd = self._get_font_preamble_and_command()
        # Add a hash of the latex preamble to fontconfig so that the
        # correct png is selected for strings rendered with same font and dpi
        # even if the latex preamble changes within the session
        preambles = preamble + font_cmd + self.get_custom_preamble()
        return hashlib.md5(preambles.encode('utf-8')).hexdigest()

    @classmethod
    def _get_font_family_and_reduced(cls):
        """Return the font family name and whether the font is reduced."""
        ff = rcParams['font.family']
        ff_val = ff[0].lower() if len(ff) == 1 else None
        if len(ff) == 1 and ff_val in cls._font_families:
            return ff_val, False
        elif len(ff) == 1 and ff_val in cls._font_preambles:
            return cls._font_types[ff_val], True
        else:
            _log.info('font.family must be one of (%s) when text.usetex is '
                      'True. serif will be used by default.',
                      ', '.join(cls._font_families))
            return 'serif', False

    @classmethod
    def _get_font_preamble_and_command(cls):
        requested_family, is_reduced_font = cls._get_font_family_and_reduced()

        preambles = {}
        for font_family in cls._font_families:
            if is_reduced_font and font_family == requested_family:
                preambles[font_family] = cls._font_preambles[
                    rcParams['font.family'][0].lower()]
            else:
                for font in rcParams['font.' + font_family]:
                    if font.lower() in cls._font_preambles:
                        preambles[font_family] = \
                            cls._font_preambles[font.lower()]
                        _log.debug(
                            'family: %s, font: %s, info: %s',
                            font_family, font,
                            cls._font_preambles[font.lower()])
                        break
                    else:
                        _log.debug('%s font is not compatible with usetex.',
                                   font)
                else:
                    _log.info('No LaTeX-compatible font found for the %s font'
                              'family in rcParams. Using default.',
                              font_family)
                    preambles[font_family] = cls._font_preambles[font_family]

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = {preambles[family]
               for family in ['serif', 'sans-serif', 'monospace']}
        if requested_family == 'cursive':
            cmd.add(preambles['cursive'])
        cmd.add(r'\usepackage{type1cm}')
        preamble = '\n'.join(sorted(cmd))
        fontcmd = (r'\sffamily' if requested_family == 'sans-serif' else
                   r'\ttfamily' if requested_family == 'monospace' else
                   r'\rmfamily')
        return preamble, fontcmd

    @classmethod
    def get_basefile(cls, tex, fontsize, dpi=None):
        """
        Return a filename based on a hash of the string, fontsize, and dpi.
        """
        src = cls._get_tex_source(tex, fontsize) + str(dpi)
        return os.path.join(
            cls.texcache, hashlib.md5(src.encode('utf-8')).hexdigest())

    @classmethod
    def get_font_preamble(cls):
        """
        Return a string containing font configuration for the tex preamble.
        """
        font_preamble, command = cls._get_font_preamble_and_command()
        return font_preamble

    @classmethod
    def get_custom_preamble(cls):
        """Return a string containing user additions to the tex preamble."""
        return rcParams['text.latex.preamble']

    @classmethod
    def _get_tex_source(cls, tex, fontsize):
        """Return the complete TeX source for processing a TeX string."""
        font_preamble, fontcmd = cls._get_font_preamble_and_command()
        baselineskip = 1.25 * fontsize
        return "\n".join([
            r"\documentclass{article}",
            r"% Pass-through \mathdefault, which is used in non-usetex mode",
            r"% to use the default text font but was historically suppressed",
            r"% in usetex mode.",
            r"\newcommand{\mathdefault}[1]{#1}",
            font_preamble,
            r"\usepackage[utf8]{inputenc}",
            r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
            r"% geometry is loaded before the custom preamble as ",
            r"% convert_psfrags relies on a custom preamble to change the ",
            r"% geometry.",
            r"\usepackage[papersize=72in, margin=1in]{geometry}",
            cls.get_custom_preamble(),
            r"% Use `underscore` package to take care of underscores in text.",
            r"% The [strings] option allows to use underscores in file names.",
            _usepackage_if_not_loaded("underscore", option="strings"),
            r"% Custom packages (e.g. newtxtext) may already have loaded ",
            r"% textcomp with different options.",
            _usepackage_if_not_loaded("textcomp"),
            r"\pagestyle{empty}",
            r"\begin{document}",
            r"% The empty hbox ensures that a page is printed even for empty",
            r"% inputs, except when using psfrag which gets confused by it.",
            r"% matplotlibbaselinemarker is used by dviread to detect the",
            r"% last line's baseline.",
            rf"\fontsize{{{fontsize}}}{{{baselineskip}}}%",
            r"\ifdefined\psfrag\else\hbox{}\fi%",
            rf"{{\obeylines{fontcmd} {tex}}}%",
            r"\special{matplotlibbaselinemarker}%",
            r"\end{document}",
        ])

    @classmethod
    def make_tex(cls, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        """
        texfile = cls.get_basefile(tex, fontsize) + ".tex"
        Path(texfile).write_text(cls._get_tex_source(tex, fontsize))
        return texfile

    @classmethod
    def _run_checked_subprocess(cls, command, tex, *, cwd=None):
        _log.debug(cbook._pformat_subprocess(command))
        try:
            report = subprocess.check_output(
                command, cwd=cwd if cwd is not None else cls.texcache,
                stderr=subprocess.STDOUT)
        except FileNotFoundError as exc:
            raise RuntimeError(
                'Failed to process string with tex because {} could not be '
                'found'.format(command[0])) from exc
        except subprocess.CalledProcessError as exc:
            raise RuntimeError(
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full command invocation and its output:\n\n'
                '{format_command}\n\n'
                '{exc}\n\n'.format(
                    prog=command[0],
                    format_command=cbook._pformat_subprocess(command),
                    tex=tex.encode('unicode_escape'),
                    exc=exc.output.decode('utf-8'))) from None
        _log.debug(report)
        return report

    @classmethod
    def make_dvi(cls, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """
        basefile = cls.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if not os.path.exists(dvifile):
            texfile = Path(cls.make_tex(tex, fontsize))
            # Generate the dvi in a temporary directory to avoid race
            # conditions e.g. if multiple processes try to process the same tex
            # string at the same time.  Having tmpdir be a subdirectory of the
            # final output dir ensures that they are on the same filesystem,
            # and thus replace() works atomically.  It also allows referring to
            # the texfile with a relative path (for pathological MPLCONFIGDIRs,
            # the absolute path may contain characters (e.g. ~) that TeX does
            # not support.)
            with TemporaryDirectory(dir=Path(dvifile).parent) as tmpdir:
                cls._run_checked_subprocess(
                    ["latex", "-interaction=nonstopmode", "--halt-on-error",
                     f"../{texfile.name}"], tex, cwd=tmpdir)
                (Path(tmpdir) / Path(dvifile).name).replace(dvifile)
        return dvifile

    @classmethod
    def make_png(cls, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        """
        basefile = cls.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile
        # see get_rgba for a discussion of the background
        if not os.path.exists(pngfile):
            dvifile = cls.make_dvi(tex, fontsize)
            cmd = ["dvipng", "-bg", "Transparent", "-D", str(dpi),
                   "-T", "tight", "-o", pngfile, dvifile]
            # When testing, disable FreeType rendering for reproducibility; but
            # dvipng 1.16 has a bug (fixed in f3ff241) that breaks --freetype0
            # mode, so for it we keep FreeType enabled; the image will be
            # slightly off.
            if (getattr(mpl, "_called_from_pytest", False) and
                    mpl._get_executable_info("dvipng").raw_version != "1.16"):
                cmd.insert(1, "--freetype0")
            cls._run_checked_subprocess(cmd, tex)
        return pngfile

    @classmethod
    def get_grey(cls, tex, fontsize=None, dpi=None):
        """Return the alpha channel."""
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        key = cls._get_tex_source(tex, fontsize), dpi
        alpha = cls._grey_arrayd.get(key)
        if alpha is None:
            pngfile = cls.make_png(tex, fontsize, dpi)
            rgba = mpl.image.imread(os.path.join(cls.texcache, pngfile))
            cls._grey_arrayd[key] = alpha = rgba[:, :, -1]
        return alpha

    @classmethod
    def get_rgba(cls, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        r"""
        Return latex's rendering of the tex string as an rgba array.

        Examples
        --------
        >>> texmanager = TexManager()
        >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!"
        >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))
        """
        alpha = cls.get_grey(tex, fontsize, dpi)
        rgba = np.empty((*alpha.shape, 4))
        rgba[..., :3] = mpl.colors.to_rgb(rgb)
        rgba[..., -1] = alpha
        return rgba

    @classmethod
    def get_text_width_height_descent(cls, tex, fontsize, renderer=None):
        """Return width, height and descent of the text."""
        if tex.strip() == '':
            return 0, 0, 0
        dvifile = cls.make_dvi(tex, fontsize)
        dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1
        with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
            page, = dvi
        # A total height (including the descent) needs to be returned.
        return page.width, page.height + page.descent, page.descent
Example #15
0
from matplotlib.figure import Figure

import matplotlib.pyplot as plt
import matplotlib.animation as animation

from collections import OrderedDict

import Queue

import random

#from matplotlib.font_manager import FontProperties

# IMPORTANT: If you're getting one or more graphs that fail to initialize properly, clear fontList.cache from the following directory.
# ^ This happened on Windows; hasn't happened on mac. Probably should file a bug.
print "Cachedir: {}".format(matplotlib.get_cachedir())


class ServoDrive(object):
    # simulate values
    #def getVelocity(self): return random.randint(0,50)
    #def getTorque(self): return random.randint(50,100)
    def getVelocity(self):
        return 1

    def getTorque(self):
        return 0


class SimGui(tk.Frame):
    def __init__(self, *args, **kwargs):
Example #16
0
class TexManager(object):
    """
    Convert strings to dvi files using TeX, caching the results to a directory.

    Repeated calls to this constructor always return the same instance.
    """

    cachedir = mpl.get_cachedir()
    if cachedir is not None:
        texcache = os.path.join(cachedir, 'tex.cache')
        Path(texcache).mkdir(parents=True, exist_ok=True)
    else:
        # Should only happen in a restricted environment (such as Google App
        # Engine). Deal with this gracefully by not creating a cache directory.
        texcache = None

    # Caches.
    rgba_arrayd = {}
    grey_arrayd = {}

    serif = ('cmr', '')
    sans_serif = ('cmss', '')
    monospace = ('cmtt', '')
    cursive = ('pzc', r'\usepackage{chancery}')
    font_family = 'serif'
    font_families = ('serif', 'sans-serif', 'cursive', 'monospace')

    font_info = {
        'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
        'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
        'times': ('ptm', r'\usepackage{mathptmx}'),
        'palatino': ('ppl', r'\usepackage{mathpazo}'),
        'zapf chancery': ('pzc', r'\usepackage{chancery}'),
        'cursive': ('pzc', r'\usepackage{chancery}'),
        'charter': ('pch', r'\usepackage{charter}'),
        'serif': ('cmr', ''),
        'sans-serif': ('cmss', ''),
        'helvetica': ('phv', r'\usepackage{helvet}'),
        'avant garde': ('pag', r'\usepackage{avant}'),
        'courier': ('pcr', r'\usepackage{courier}'),
        # Loading the type1ec package ensures that cm-super is installed, which
        # is necessary for unicode computer modern.  (It also allows the use of
        # computer modern at arbitrary sizes, but that's just a side effect.)
        'monospace': ('cmtt', r'\usepackage{type1ec}'),
        'computer modern roman': ('cmr', r'\usepackage{type1ec}'),
        'computer modern sans serif': ('cmss', r'\usepackage{type1ec}'),
        'computer modern typewriter': ('cmtt', r'\usepackage{type1ec}')
    }

    _rc_cache = None
    _rc_cache_keys = (('text.latex.preamble', 'text.latex.unicode',
                       'text.latex.preview', 'font.family') +
                      tuple('font.' + n for n in font_families))

    @functools.lru_cache()  # Always return the same instance.
    def __new__(cls):
        self = object.__new__(cls)
        self._reinit()
        return self

    def _reinit(self):
        if self.texcache is None:
            raise RuntimeError('Cannot create TexManager, as there is no '
                               'cache directory available')

        Path(self.texcache).mkdir(parents=True, exist_ok=True)
        ff = rcParams['font.family']
        if len(ff) == 1 and ff[0].lower() in self.font_families:
            self.font_family = ff[0].lower()
        elif isinstance(ff, str) and ff.lower() in self.font_families:
            self.font_family = ff.lower()
        else:
            _log.info(
                'font.family must be one of (%s) when text.usetex is '
                'True. serif will be used by default.',
                ', '.join(self.font_families))
            self.font_family = 'serif'

        fontconfig = [self.font_family]
        for font_family in self.font_families:
            font_family_attr = font_family.replace('-', '_')
            for font in rcParams['font.' + font_family]:
                if font.lower() in self.font_info:
                    setattr(self, font_family_attr,
                            self.font_info[font.lower()])
                    _log.debug('family: %s, font: %s, info: %s', font_family,
                               font, self.font_info[font.lower()])
                    break
                else:
                    _log.debug('%s font is not compatible with usetex.',
                               font_family)
            else:
                _log.info(
                    'No LaTeX-compatible font found for the %s font '
                    'family in rcParams. Using default.', font_family)
                setattr(self, font_family_attr, self.font_info[font_family])
            fontconfig.append(getattr(self, font_family_attr)[0])
        # Add a hash of the latex preamble to self._fontconfig so that the
        # correct png is selected for strings rendered with same font and dpi
        # even if the latex preamble changes within the session
        preamble_bytes = self.get_custom_preamble().encode('utf-8')
        fontconfig.append(hashlib.md5(preamble_bytes).hexdigest())
        self._fontconfig = ''.join(fontconfig)

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
        if self.font_family == 'cursive':
            cmd.append(self.cursive[1])
        self._font_preamble = '\n'.join([r'\usepackage{type1cm}'] + cmd +
                                        [r'\usepackage{textcomp}'])

    def get_basefile(self, tex, fontsize, dpi=None):
        """
        Return a filename based on a hash of the string, fontsize, and dpi.
        """
        s = ''.join([
            tex,
            self.get_font_config(),
            '%f' % fontsize,
            self.get_custom_preamble(),
            str(dpi or '')
        ])
        return os.path.join(self.texcache,
                            hashlib.md5(s.encode('utf-8')).hexdigest())

    def get_font_config(self):
        """Reinitializes self if relevant rcParams on have changed."""
        if self._rc_cache is None:
            self._rc_cache = dict.fromkeys(self._rc_cache_keys)
        changed = [
            par for par in self._rc_cache_keys
            if rcParams[par] != self._rc_cache[par]
        ]
        if changed:
            _log.debug('following keys changed: %s', changed)
            for k in changed:
                _log.debug('%-20s: %-10s -> %-10s', k, self._rc_cache[k],
                           rcParams[k])
                # deepcopy may not be necessary, but feels more future-proof
                self._rc_cache[k] = copy.deepcopy(rcParams[k])
            _log.debug('RE-INIT\nold fontconfig: %s', self._fontconfig)
            self._reinit()
        _log.debug('fontconfig: %s', self._fontconfig)
        return self._fontconfig

    def get_font_preamble(self):
        """
        Return a string containing font configuration for the tex preamble.
        """
        return self._font_preamble

    def get_custom_preamble(self):
        """Return a string containing user additions to the tex preamble."""
        return rcParams['text.latex.preamble']

    def make_tex(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = r"""
\usepackage[utf8]{inputenc}"""
        else:
            unicode_preamble = ''

        s = r"""
\documentclass{article}
%s
%s
%s
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
\pagestyle{empty}
\begin{document}
\fontsize{%f}{%f}%s
\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble, fontsize,
        fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError:
                    _log.info("You are using unicode and latex, but have not "
                              "enabled the 'text.latex.unicode' rcParam.")
                    raise

        return texfile

    _re_vbox = re.compile(
        r"MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)pt")

    def make_tex_preview(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        It uses the preview.sty to determine the dimension (width, height,
        descent) of the output.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = r"""
\usepackage[utf8]{inputenc}"""
        else:
            unicode_preamble = ''

        # newbox, setbox, immediate, etc. are used to find the box
        # extent of the rendered text.

        s = r"""
\documentclass{article}
%s
%s
%s
\usepackage[active,showbox,tightpage]{preview}
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\def\showbox#1%%
{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}}

\begin{document}
\begin{preview}
{\fontsize{%f}{%f}%s}
\end{preview}
\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble, fontsize,
        fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError:
                    _log.info("You are using unicode and latex, but have not "
                              "enabled the 'text.latex.unicode' rcParam.")
                    raise

        return texfile

    def _run_checked_subprocess(self, command, tex):
        _log.debug(cbook._pformat_subprocess(command))
        try:
            report = subprocess.check_output(command,
                                             cwd=self.texcache,
                                             stderr=subprocess.STDOUT)
        except FileNotFoundError as exc:
            raise RuntimeError(
                'Failed to process string with tex because {} could not be '
                'found'.format(command[0])) from exc
        except subprocess.CalledProcessError as exc:
            raise RuntimeError(
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full report generated by {prog}:\n'
                '{exc}\n\n'.format(prog=command[0],
                                   tex=tex.encode('unicode_escape'),
                                   exc=exc.output.decode('utf-8'))) from exc
        _log.debug(report)
        return report

    def make_dvi(self, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """

        if rcParams['text.latex.preview']:
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            with cbook._lock_path(texfile):
                self._run_checked_subprocess([
                    "latex", "-interaction=nonstopmode", "--halt-on-error",
                    texfile
                ], tex)
            for fname in glob.glob(basefile + '*'):
                if not fname.endswith(('dvi', 'tex')):
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_dvi_preview(self, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        It calls make_tex_preview() method and store the size information
        (width, height, descent) in a separate file.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if not os.path.exists(dvifile) or not os.path.exists(baselinefile):
            texfile = self.make_tex_preview(tex, fontsize)
            report = self._run_checked_subprocess([
                "latex", "-interaction=nonstopmode", "--halt-on-error", texfile
            ], tex)

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report.decode("utf-8"))
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if not fname.endswith(('dvi', 'tex', 'baseline')):
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_png(self, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile
        # see get_rgba for a discussion of the background
        if not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            self._run_checked_subprocess([
                "dvipng", "-bg", "Transparent", "-D",
                str(dpi), "-T", "tight", "-o", pngfile, dvifile
            ], tex)
        return pngfile

    def get_grey(self, tex, fontsize=None, dpi=None):
        """Return the alpha channel."""
        from matplotlib import _png
        key = tex, self.get_font_config(), fontsize, dpi
        alpha = self.grey_arrayd.get(key)
        if alpha is None:
            pngfile = self.make_png(tex, fontsize, dpi)
            X = _png.read_png(os.path.join(self.texcache, pngfile))
            self.grey_arrayd[key] = alpha = X[:, :, -1]
        return alpha

    def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        """Return latex's rendering of the tex string as an rgba array."""
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        r, g, b = rgb
        key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb)
        Z = self.rgba_arrayd.get(key)

        if Z is None:
            alpha = self.get_grey(tex, fontsize, dpi)
            Z = np.dstack([r, g, b, alpha])
            self.rgba_arrayd[key] = Z

        return Z

    def get_text_width_height_descent(self, tex, fontsize, renderer=None):
        """Return width, height and descent of the text."""
        if tex.strip() == '':
            return 0, 0, 0

        dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1

        if rcParams['text.latex.preview']:
            # use preview.sty
            basefile = self.get_basefile(tex, fontsize)
            baselinefile = '%s.baseline' % basefile

            if not os.path.exists(baselinefile):
                dvifile = self.make_dvi_preview(tex, fontsize)

            with open(baselinefile) as fh:
                l = fh.read().split()
            height, depth, width = [float(l1) * dpi_fraction for l1 in l]
            return width, height + depth, depth

        else:
            # use dviread. It sometimes returns a wrong descent.
            dvifile = self.make_dvi(tex, fontsize)
            with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
                page, = dvi
            # A total height (including the descent) needs to be returned.
            return page.width, page.height + page.descent, page.descent
Example #17
0
class TexManager(object):
    """
    Convert strings to dvi files using TeX, caching the results to a
    working dir
    """

    oldpath = mpl.get_home()
    if oldpath is None:
        oldpath = mpl.get_data_path()
    oldcache = os.path.join(oldpath, '.tex.cache')

    cachedir = mpl.get_cachedir()
    if cachedir is not None:
        texcache = os.path.join(cachedir, 'tex.cache')
    else:
        # Should only happen in a restricted environment (such as Google App
        # Engine). Deal with this gracefully by not creating a cache directory.
        texcache = None

    if os.path.exists(oldcache):
        if texcache is not None:
            try:
                shutil.move(oldcache, texcache)
            except IOError as e:
                warnings.warn('File could not be renamed: %s' % e)
            else:
                warnings.warn("""\
Found a TeX cache dir in the deprecated location "%s".
    Moving it to the new default location "%s".""" % (oldcache, texcache))
        else:
            warnings.warn("""\
Could not rename old TeX cache dir "%s": a suitable configuration
    directory could not be found.""" % oldcache)

    if texcache is not None:
        mkdirs(texcache)

    # mappable cache of
    rgba_arrayd = {}
    grey_arrayd = {}
    postscriptd = {}
    pscnt = 0

    serif = ('cmr', '')
    sans_serif = ('cmss', '')
    monospace = ('cmtt', '')
    cursive = ('pzc', '\\usepackage{chancery}')
    font_family = 'serif'
    font_families = ('serif', 'sans-serif', 'cursive', 'monospace')

    font_info = {
        'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
        'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
        'times': ('ptm', '\\usepackage{mathptmx}'),
        'palatino': ('ppl', '\\usepackage{mathpazo}'),
        'zapf chancery': ('pzc', '\\usepackage{chancery}'),
        'cursive': ('pzc', '\\usepackage{chancery}'),
        'charter': ('pch', '\\usepackage{charter}'),
        'serif': ('cmr', ''),
        'sans-serif': ('cmss', ''),
        'helvetica': ('phv', '\\usepackage{helvet}'),
        'avant garde': ('pag', '\\usepackage{avant}'),
        'courier': ('pcr', '\\usepackage{courier}'),
        'monospace': ('cmtt', ''),
        'computer modern roman': ('cmr', ''),
        'computer modern sans serif': ('cmss', ''),
        'computer modern typewriter': ('cmtt', '')
    }

    _rc_cache = None
    _rc_cache_keys = (
        ('text.latex.preamble', ) +
        tuple(['font.' + n for n in ('family', ) + font_families]))

    def __init__(self):

        if self.texcache is None:
            raise RuntimeError(
                ('Cannot create TexManager, as there is no cache directory '
                 'available'))

        mkdirs(self.texcache)
        ff = rcParams['font.family']
        if len(ff) == 1 and ff[0].lower() in self.font_families:
            self.font_family = ff[0].lower()
        elif isinstance(ff,
                        six.string_types) and ff.lower() in self.font_families:
            self.font_family = ff.lower()
        else:
            _log.info(
                'font.family must be one of (%s) when text.usetex is True. '
                'serif will be used by default.',
                ', '.join(self.font_families))
            self.font_family = 'serif'

        fontconfig = [self.font_family]
        for font_family, font_family_attr in [(ff, ff.replace('-', '_'))
                                              for ff in self.font_families]:
            for font in rcParams['font.' + font_family]:
                if font.lower() in self.font_info:
                    setattr(self, font_family_attr,
                            self.font_info[font.lower()])
                    if DEBUG:
                        print(
                            'family: %s, font: %s, info: %s' %
                            (font_family, font, self.font_info[font.lower()]))
                    break
                else:
                    if DEBUG:
                        print('$s font is not compatible with usetex')
            else:
                _log.info(
                    'No LaTeX-compatible font found for the '
                    '%s font family in rcParams. Using '
                    'default.', font_family)
                setattr(self, font_family_attr, self.font_info[font_family])
            fontconfig.append(getattr(self, font_family_attr)[0])
        # Add a hash of the latex preamble to self._fontconfig so that the
        # correct png is selected for strings rendered with same font and dpi
        # even if the latex preamble changes within the session
        preamble_bytes = six.text_type(
            self.get_custom_preamble()).encode('utf-8')
        fontconfig.append(md5(preamble_bytes).hexdigest())
        self._fontconfig = ''.join(fontconfig)

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
        if self.font_family == 'cursive':
            cmd.append(self.cursive[1])
        while '\\usepackage{type1cm}' in cmd:
            cmd.remove('\\usepackage{type1cm}')
        cmd = '\n'.join(cmd)
        self._font_preamble = '\n'.join(
            ['\\usepackage{type1cm}', cmd, '\\usepackage{textcomp}'])

    def get_basefile(self, tex, fontsize, dpi=None):
        """
        returns a filename based on a hash of the string, fontsize, and dpi
        """
        s = ''.join([
            tex,
            self.get_font_config(),
            '%f' % fontsize,
            self.get_custom_preamble(),
            str(dpi or '')
        ])
        # make sure hash is consistent for all strings, regardless of encoding:
        bytes = six.text_type(s).encode('utf-8')
        return os.path.join(self.texcache, md5(bytes).hexdigest())

    def get_font_config(self):
        """Reinitializes self if relevant rcParams on have changed."""
        if self._rc_cache is None:
            self._rc_cache = dict.fromkeys(self._rc_cache_keys)
        changed = [
            par for par in self._rc_cache_keys
            if rcParams[par] != self._rc_cache[par]
        ]
        if changed:
            if DEBUG:
                print('DEBUG following keys changed:', changed)
            for k in changed:
                if DEBUG:
                    print('DEBUG %-20s: %-10s -> %-10s' %
                          (k, self._rc_cache[k], rcParams[k]))
                # deepcopy may not be necessary, but feels more future-proof
                self._rc_cache[k] = copy.deepcopy(rcParams[k])
            if DEBUG:
                print('DEBUG RE-INIT\nold fontconfig:', self._fontconfig)
            self.__init__()
        if DEBUG:
            print('DEBUG fontconfig:', self._fontconfig)
        return self._fontconfig

    def get_font_preamble(self):
        """
        returns a string containing font configuration for the tex preamble
        """
        return self._font_preamble

    def get_custom_preamble(self):
        """returns a string containing user additions to the tex preamble"""
        return '\n'.join(rcParams['text.latex.preamble'])

    def make_tex(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = """\\usepackage{ucs}
\\usepackage[utf8x]{inputenc}"""
        else:
            unicode_preamble = ''

        s = """\\documentclass{article}
%s
%s
%s
\\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
\\pagestyle{empty}
\\begin{document}
\\fontsize{%f}{%f}%s
\\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble, fontsize,
        fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError as err:
                    _log.info("You are using unicode and latex, but "
                              "have not enabled the matplotlib "
                              "'text.latex.unicode' rcParam.")
                    raise

        return texfile

    _re_vbox = re.compile(
        r"MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)pt")

    def make_tex_preview(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific
        font size. It uses the preview.sty to determine the dimension
        (width, height, descent) of the output.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self.font_family, r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = """\\usepackage{ucs}
\\usepackage[utf8x]{inputenc}"""
        else:
            unicode_preamble = ''

        # newbox, setbox, immediate, etc. are used to find the box
        # extent of the rendered text.

        s = """\\documentclass{article}
%s
%s
%s
\\usepackage[active,showbox,tightpage]{preview}
\\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\\def\\showbox#1{\\immediate\\write16{MatplotlibBox:(\\the\\ht#1+\\the\\dp#1)x\\the\\wd#1}}

\\begin{document}
\\begin{preview}
{\\fontsize{%f}{%f}%s}
\\end{preview}
\\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble, fontsize,
        fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError as err:
                    _log.info("You are using unicode and latex, but "
                              "have not enabled the matplotlib "
                              "'text.latex.unicode' rcParam.")
                    raise

        return texfile

    def make_dvi(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex string

        returns the file name
        """

        if rcParams['text.latex.preview']:
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if DEBUG or not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            command = [
                str("latex"), "-interaction=nonstopmode",
                os.path.basename(texfile)
            ]
            _log.debug(command)
            with Locked(self.texcache):
                try:
                    report = subprocess.check_output(command,
                                                     cwd=self.texcache,
                                                     stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError as exc:
                    raise RuntimeError(
                        ('LaTeX was not able to process the following '
                         'string:\n%s\n\n'
                         'Here is the full report generated by LaTeX:\n%s '
                         '\n\n' % (repr(tex.encode('unicode_escape')),
                                   exc.output.decode("utf-8"))))
                _log.debug(report)
            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_dvi_preview(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex
        string. It calls make_tex_preview() method and store the size
        information (width, height, descent) in a separte file.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if (DEBUG or not os.path.exists(dvifile)
                or not os.path.exists(baselinefile)):
            texfile = self.make_tex_preview(tex, fontsize)
            command = [
                str("latex"), "-interaction=nonstopmode",
                os.path.basename(texfile)
            ]
            _log.debug(command)
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('LaTeX was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by LaTeX:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            _log.debug(report)

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report.decode("utf-8"))
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                elif fname.endswith('baseline'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_png(self, tex, fontsize, dpi):
        """
        generates a png file containing latex's rendering of tex string

        returns the filename
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile

        # see get_rgba for a discussion of the background
        if DEBUG or not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [
                str("dvipng"), "-bg", "Transparent", "-D",
                str(dpi), "-T", "tight", "-o",
                os.path.basename(pngfile),
                os.path.basename(dvifile)
            ]
            _log.debug(command)
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvipng was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvipng:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            _log.debug(report)

        return pngfile

    def make_ps(self, tex, fontsize):
        """
        generates a postscript file containing latex's rendering of tex string

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        psfile = '%s.epsf' % basefile

        if DEBUG or not os.path.exists(psfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [
                str("dvips"), "-q", "-E", "-o",
                os.path.basename(psfile),
                os.path.basename(dvifile)
            ]
            _log.debug(command)
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvips was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvips:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            _log.debug(report)

        return psfile

    def get_ps_bbox(self, tex, fontsize):
        """
        returns a list containing the postscript bounding box for latex's
        rendering of the tex string
        """
        psfile = self.make_ps(tex, fontsize)
        with open(psfile) as ps:
            for line in ps:
                if line.startswith('%%BoundingBox:'):
                    return [int(val) for val in line.split()[1:]]
        raise RuntimeError('Could not parse %s' % psfile)

    def get_grey(self, tex, fontsize=None, dpi=None):
        """returns the alpha channel"""
        key = tex, self.get_font_config(), fontsize, dpi
        alpha = self.grey_arrayd.get(key)
        if alpha is None:
            pngfile = self.make_png(tex, fontsize, dpi)
            X = read_png(os.path.join(self.texcache, pngfile))
            self.grey_arrayd[key] = alpha = X[:, :, -1]
        return alpha

    def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        """
        Returns latex's rendering of the tex string as an rgba array
        """
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        r, g, b = rgb
        key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb)
        Z = self.rgba_arrayd.get(key)

        if Z is None:
            alpha = self.get_grey(tex, fontsize, dpi)

            Z = np.zeros((alpha.shape[0], alpha.shape[1], 4), float)

            Z[:, :, 0] = r
            Z[:, :, 1] = g
            Z[:, :, 2] = b
            Z[:, :, 3] = alpha
            self.rgba_arrayd[key] = Z

        return Z

    def get_text_width_height_descent(self, tex, fontsize, renderer=None):
        """
        return width, heigth and descent of the text.
        """
        if tex.strip() == '':
            return 0, 0, 0

        if renderer:
            dpi_fraction = renderer.points_to_pixels(1.)
        else:
            dpi_fraction = 1.

        if rcParams['text.latex.preview']:
            # use preview.sty
            basefile = self.get_basefile(tex, fontsize)
            baselinefile = '%s.baseline' % basefile

            if DEBUG or not os.path.exists(baselinefile):
                dvifile = self.make_dvi_preview(tex, fontsize)

            with open(baselinefile) as fh:
                l = fh.read().split()
            height, depth, width = [float(l1) * dpi_fraction for l1 in l]
            return width, height + depth, depth

        else:
            # use dviread. It sometimes returns a wrong descent.
            dvifile = self.make_dvi(tex, fontsize)
            with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
                page = next(iter(dvi))
            # A total height (including the descent) needs to be returned.
            return page.width, page.height + page.descent, page.descent
Example #18
0
 def cachedir(self):
     return mpl.get_cachedir()
Example #19
0
# https://programmers.co.kr/learn/courses/21/lessons/950
# https://financedata.github.io/posts/matplotlib-hangul-for-ubuntu-linux.html

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

print(f"{'Version.':15}: ", matplotlib.__version__)
print(f"{'Location':15}: ", matplotlib.__file__)
print(f"{'Configs':15}: ", matplotlib.get_configdir())
print(f"{'Cache':15}: ", matplotlib.get_cachedir())
print()

font_list = [f.fname for f in matplotlib.font_manager.fontManager.ttflist]
for font in (font_list):
    print(font)
print()

print(
    f"Current Font size ({plt.rcParams['font.size']}) / Current Font Family {plt.rcParams['font.family']}"
)
print()

plt.rcParams["font.family"] = 'Nanum Gothic'

print(
    f"Current Font size ({plt.rcParams['font.size']}) / Current Font Family {plt.rcParams['font.family']}"
)
print()

font_list = fm.findSystemFonts(fontpaths=None, fontext='ttf')
Example #20
0
from mpl_toolkits import mplot3d

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

matplotlib.get_cachedir()


class Visualizer:
    @staticmethod
    def draw_scatter(input_values, weights, axes):
        x_points = [input_value[0] for input_value in input_values]
        y_points = [input_value[1] for input_value in input_values]
        z_points = [(weights[0] + (weights[1] * input_value[0]) +
                     (weights[2] * input_value[1]))
                    for input_value in input_values]

        axes.scatter3D(x_points, y_points, z_points, c=z_points, cmap='hsv')

    @staticmethod
    def draw_wireframe(input_values, weights, axes):
        def z_function(x_point, y_point):
            return (weights[0] + (weights[1] * x_point) +
                    (weights[2] * y_point))

        x_points = [input_value[0] for input_value in input_values]
        y_points = [input_value[1] for input_value in input_values]

        X, Y = np.meshgrid(x_points, y_points)
Example #21
0
import matplotlib
print(matplotlib.matplotlib_fname())  #設定ファイルを表示(matplotlibrcは後で作ります)
print(matplotlib.rcParams['font.family'])  #現在使用しているフォントを表示
print(matplotlib.get_configdir())  #設定ディレクトリを表示
print(matplotlib.get_cachedir())  #キャッシュディレクトリを表示
Example #22
0
class TexManager:
    """
    Convert strings to dvi files using TeX, caching the results to a
    working dir
    """

    oldpath = mpl.get_home()
    if oldpath is None:
        oldpath = mpl.get_data_path()
    oldcache = os.path.join(oldpath, '.tex.cache')

    cachedir = mpl.get_cachedir()
    if cachedir is not None:
        texcache = os.path.join(cachedir, 'tex.cache')
    else:
        # Should only happen in a restricted environment (such as Google App
        # Engine). Deal with this gracefully by not creating a cache directory.
        texcache = None

    if os.path.exists(oldcache):
        if texcache is not None:
            try:
                shutil.move(oldcache, texcache)
            except IOError as e:
                warnings.warn('File could not be renamed: %s' % e)
            else:
                warnings.warn("""\
Found a TeX cache dir in the deprecated location "%s".
    Moving it to the new default location "%s".""" % (oldcache, texcache))
        else:
            warnings.warn("""\
Could not rename old TeX cache dir "%s": a suitable configuration
    directory could not be found.""" % oldcache)

    if texcache is not None:
        mkdirs(texcache)

    _dvipng_hack_alpha = None
    #_dvipng_hack_alpha = dvipng_hack_alpha()
    # mappable cache of
    rgba_arrayd = {}
    grey_arrayd = {}
    postscriptd = {}
    pscnt = 0

    serif = ('cmr', '')
    sans_serif = ('cmss', '')
    monospace = ('cmtt', '')
    cursive = ('pzc', '\\usepackage{chancery}')
    font_family = 'serif'
    font_families = ('serif', 'sans-serif', 'cursive', 'monospace')

    font_info = {'new century schoolbook': ('pnc',
                                            r'\renewcommand{\rmdefault}{pnc}'),
                 'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
                 'times': ('ptm', '\\usepackage{mathptmx}'),
                 'palatino': ('ppl', '\\usepackage{mathpazo}'),
                 'zapf chancery': ('pzc', '\\usepackage{chancery}'),
                 'cursive': ('pzc', '\\usepackage{chancery}'),
                 'charter': ('pch', '\\usepackage{charter}'),
                 'serif': ('cmr', ''),
                 'sans-serif': ('cmss', ''),
                 'helvetica': ('phv', '\\usepackage{helvet}'),
                 'avant garde': ('pag', '\\usepackage{avant}'),
                 'courier': ('pcr', '\\usepackage{courier}'),
                 'monospace': ('cmtt', ''),
                 'computer modern roman': ('cmr', ''),
                 'computer modern sans serif': ('cmss', ''),
                 'computer modern typewriter': ('cmtt', '')}

    _rc_cache = None
    _rc_cache_keys = (('text.latex.preamble', ) +
                      tuple(['font.' + n for n in ('family', ) +
                             font_families]))

    def __init__(self):

        if self.texcache is None:
            raise RuntimeError(
                ('Cannot create TexManager, as there is no cache directory '
                 'available'))

        mkdirs(self.texcache)
        ff = rcParams['font.family']
        if len(ff) == 1 and ff[0].lower() in self.font_families:
            self.font_family = ff[0].lower()
        elif isinstance(ff, basestring) and ff.lower() in self.font_families:
            self.font_family = ff.lower()
        else:
            mpl.verbose.report(
                'font.family must be one of (%s) when text.usetex is True. '
                'serif will be used by default.' %
                   ', '.join(self.font_families),
                'helpful')
            self.font_family = 'serif'

        fontconfig = [self.font_family]
        for font_family, font_family_attr in [(ff, ff.replace('-', '_'))
                                              for ff in self.font_families]:
            for font in rcParams['font.' + font_family]:
                if font.lower() in self.font_info:
                    setattr(self, font_family_attr,
                            self.font_info[font.lower()])
                    if DEBUG:
                        print('family: %s, font: %s, info: %s' %
                              (font_family, font,
                               self.font_info[font.lower()]))
                    break
                else:
                    if DEBUG:
                        print('$s font is not compatible with usetex')
            else:
                mpl.verbose.report('No LaTeX-compatible font found for the '
                                   '%s font family in rcParams. Using '
                                   'default.' % ff, 'helpful')
                setattr(self, font_family_attr, self.font_info[font_family])
            fontconfig.append(getattr(self, font_family_attr)[0])
        self._fontconfig = ''.join(fontconfig)

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = [self.serif[1], self.sans_serif[1], self.monospace[1]]
        if self.font_family == 'cursive':
            cmd.append(self.cursive[1])
        while '\\usepackage{type1cm}' in cmd:
            cmd.remove('\\usepackage{type1cm}')
        cmd = '\n'.join(cmd)
        self._font_preamble = '\n'.join(['\\usepackage{type1cm}', cmd,
                                         '\\usepackage{textcomp}'])

    def get_basefile(self, tex, fontsize, dpi=None):
        """
        returns a filename based on a hash of the string, fontsize, and dpi
        """
        s = ''.join([tex, self.get_font_config(), '%f' % fontsize,
                     self.get_custom_preamble(), str(dpi or '')])
        # make sure hash is consistent for all strings, regardless of encoding:
        bytes = six.text_type(s).encode('utf-8')
        return os.path.join(self.texcache, md5(bytes).hexdigest())

    def get_font_config(self):
        """Reinitializes self if relevant rcParams on have changed."""
        if self._rc_cache is None:
            self._rc_cache = dict([(k, None) for k in self._rc_cache_keys])
        changed = [par for par in self._rc_cache_keys
                   if rcParams[par] != self._rc_cache[par]]
        if changed:
            if DEBUG:
                print('DEBUG following keys changed:', changed)
            for k in changed:
                if DEBUG:
                    print('DEBUG %-20s: %-10s -> %-10s' %
                          (k, self._rc_cache[k], rcParams[k]))
                # deepcopy may not be necessary, but feels more future-proof
                self._rc_cache[k] = copy.deepcopy(rcParams[k])
            if DEBUG:
                print('DEBUG RE-INIT\nold fontconfig:', self._fontconfig)
            self.__init__()
        if DEBUG:
            print('DEBUG fontconfig:', self._fontconfig)
        return self._fontconfig

    def get_font_preamble(self):
        """
        returns a string containing font configuration for the tex preamble
        """
        return self._font_preamble

    def get_custom_preamble(self):
        """returns a string containing user additions to the tex preamble"""
        return '\n'.join(rcParams['text.latex.preamble'])

    def _get_shell_cmd(self, *args):
        """
        On windows, changing directories can be complicated by the presence of
        multiple drives. get_shell_cmd deals with this issue.
        """
        if sys.platform == 'win32':
            command = ['%s' % os.path.splitdrive(self.texcache)[0]]
        else:
            command = []
        command.extend(args)
        return ' && '.join(command)

    def make_tex(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {'sans-serif': r'{\sffamily %s}',
                   'monospace': r'{\ttfamily %s}'}.get(self.font_family,
                                                       r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = """\\usepackage{ucs}
\\usepackage[utf8x]{inputenc}"""
        else:
            unicode_preamble = ''

        s = """\\documentclass{article}
%s
%s
%s
\\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
\\pagestyle{empty}
\\begin{document}
\\fontsize{%f}{%f}%s
\\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble,
       fontsize, fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError as err:
                    mpl.verbose.report("You are using unicode and latex, but "
                                       "have not enabled the matplotlib "
                                       "'text.latex.unicode' rcParam.",
                                       'helpful')
                    raise

        return texfile

    _re_vbox = re.compile(
        r"MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)pt")

    def make_tex_preview(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific
        font size.  It uses the preview.sty to determin the dimension
        (width, height, descent) of the output.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        custom_preamble = self.get_custom_preamble()
        fontcmd = {'sans-serif': r'{\sffamily %s}',
                   'monospace': r'{\ttfamily %s}'}.get(self.font_family,
                                                       r'{\rmfamily %s}')
        tex = fontcmd % tex

        if rcParams['text.latex.unicode']:
            unicode_preamble = """\\usepackage{ucs}
\\usepackage[utf8x]{inputenc}"""
        else:
            unicode_preamble = ''

        # newbox, setbox, immediate, etc. are used to find the box
        # extent of the rendered text.

        s = """\\documentclass{article}
%s
%s
%s
\\usepackage[active,showbox,tightpage]{preview}
\\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\\def\\showbox#1{\\immediate\\write16{MatplotlibBox:(\\the\\ht#1+\\the\\dp#1)x\\the\\wd#1}}

\\begin{document}
\\begin{preview}
{\\fontsize{%f}{%f}%s}
\\end{preview}
\\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble,
       fontsize, fontsize * 1.25, tex)
        with open(texfile, 'wb') as fh:
            if rcParams['text.latex.unicode']:
                fh.write(s.encode('utf8'))
            else:
                try:
                    fh.write(s.encode('ascii'))
                except UnicodeEncodeError as err:
                    mpl.verbose.report("You are using unicode and latex, but "
                                       "have not enabled the matplotlib "
                                       "'text.latex.unicode' rcParam.",
                                       'helpful')
                    raise

        return texfile

    def make_dvi(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex string

        returns the file name
        """

        if rcParams['text.latex.preview']:
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile

        if DEBUG or not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            outfile = basefile + '.output'
            command = self._get_shell_cmd(
                'cd "%s"' % self.texcache,
                'latex -interaction=nonstopmode %s > "%s"' %
                (os.path.split(texfile)[-1], outfile))
            mpl.verbose.report(command, 'debug')
            exit_status = os.system(command)
            try:
                with open(outfile) as fh:
                    report = fh.read()
            except IOError:
                report = 'No latex error report available.'
            try:
                os.stat(dvifile)
                exists = True
            except OSError:
                exists = False
            if exit_status or not exists:
                raise RuntimeError(
                    ('LaTeX was not able to process the following '
                     'string:\n%s\nHere is the full report generated by '
                     'LaTeX: \n\n' % repr(tex)) + report)
            else:
                mpl.verbose.report(report, 'debug')
            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_dvi_preview(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex
        string. It calls make_tex_preview() method and store the size
        information (width, height, descent) in a separte file.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if (DEBUG or not os.path.exists(dvifile) or
                not os.path.exists(baselinefile)):
            texfile = self.make_tex_preview(tex, fontsize)
            outfile = basefile + '.output'
            command = self._get_shell_cmd(
                'cd "%s"' % self.texcache,
                'latex -interaction=nonstopmode %s > "%s"' %
                (os.path.split(texfile)[-1], outfile))
            mpl.verbose.report(command, 'debug')
            exit_status = os.system(command)
            try:
                with open(outfile) as fh:
                    report = fh.read()

            except IOError:
                report = 'No latex error report available.'
            if exit_status:
                raise RuntimeError(
                    ('LaTeX was not able to process the following '
                     'string:\n%s\nHere is the full report generated by '
                     'LaTeX: \n\n' % repr(tex)) + report)
            else:
                mpl.verbose.report(report, 'debug')

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report)
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                elif fname.endswith('baseline'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile

    def make_png(self, tex, fontsize, dpi):
        """
        generates a png file containing latex's rendering of tex string

        returns the filename
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile

        # see get_rgba for a discussion of the background
        if DEBUG or not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            outfile = basefile + '.output'
            command = self._get_shell_cmd(
                'cd "%s"' % self.texcache,
                'dvipng -bg Transparent -D %s -T tight -o "%s" "%s" > "%s"' %
                (dpi, os.path.split(pngfile)[-1],
                os.path.split(dvifile)[-1], outfile))
            mpl.verbose.report(command, 'debug')
            exit_status = os.system(command)
            try:
                with open(outfile) as fh:
                    report = fh.read()
            except IOError:
                report = 'No dvipng error report available.'
            if exit_status:
                raise RuntimeError(
                    'dvipng was not able to process the following '
                    'file:\n%s\nHere is the full report generated by '
                    'dvipng: \n\n' % dvifile + report)
            else:
                mpl.verbose.report(report, 'debug')
            try:
                os.remove(outfile)
            except OSError:
                pass

        return pngfile

    def make_ps(self, tex, fontsize):
        """
        generates a postscript file containing latex's rendering of tex string

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        psfile = '%s.epsf' % basefile

        if DEBUG or not os.path.exists(psfile):
            dvifile = self.make_dvi(tex, fontsize)
            outfile = basefile + '.output'
            command = self._get_shell_cmd(
                'cd "%s"' % self.texcache,
                'dvips -q -E -o "%s" "%s" > "%s"' %
                (os.path.split(psfile)[-1],
                 os.path.split(dvifile)[-1], outfile))
            mpl.verbose.report(command, 'debug')
            exit_status = os.system(command)
            with open(outfile) as fh:
                if exit_status:
                    raise RuntimeError(
                        'dvipng was not able to process the flowing '
                        'file:\n%s\nHere is the full report generated by '
                        'dvipng: \n\n' % dvifile + fh.read())
                else:
                    mpl.verbose.report(fh.read(), 'debug')
            os.remove(outfile)

        return psfile

    def get_ps_bbox(self, tex, fontsize):
        """
        returns a list containing the postscript bounding box for latex's
        rendering of the tex string
        """
        psfile = self.make_ps(tex, fontsize)
        with open(psfile) as ps:
            for line in ps:
                if line.startswith('%%BoundingBox:'):
                    return [int(val) for val in line.split()[1:]]
        raise RuntimeError('Could not parse %s' % psfile)

    def get_grey(self, tex, fontsize=None, dpi=None):
        """returns the alpha channel"""
        key = tex, self.get_font_config(), fontsize, dpi
        alpha = self.grey_arrayd.get(key)

        if alpha is None:
            pngfile = self.make_png(tex, fontsize, dpi)
            X = read_png(os.path.join(self.texcache, pngfile))

            if rcParams['text.dvipnghack'] is not None:
                hack = rcParams['text.dvipnghack']
            else:
                if TexManager._dvipng_hack_alpha is None:
                    TexManager._dvipng_hack_alpha = dvipng_hack_alpha()
                hack = TexManager._dvipng_hack_alpha

            if hack:
                # hack the alpha channel
                # dvipng assumed a constant background, whereas we want to
                # overlay these rasters with antialiasing over arbitrary
                # backgrounds that may have other figure elements under them.
                # When you set dvipng -bg Transparent, it actually makes the
                # alpha channel 1 and does the background compositing and
                # antialiasing itself and puts the blended data in the rgb
                # channels.  So what we do is extract the alpha information
                # from the red channel, which is a blend of the default dvipng
                # background (white) and foreground (black).  So the amount of
                # red (or green or blue for that matter since white and black
                # blend to a grayscale) is the alpha intensity.  Once we
                # extract the correct alpha information, we assign it to the
                # alpha channel properly and let the users pick their rgb.  In
                # this way, we can overlay tex strings on arbitrary
                # backgrounds with antialiasing
                #
                # red = alpha*red_foreground + (1-alpha)*red_background
                #
                # Since the foreground is black (0) and the background is
                # white (1) this reduces to red = 1-alpha or alpha = 1-red
                #alpha = npy.sqrt(1-X[:,:,0]) # should this be sqrt here?
                alpha = 1 - X[:, :, 0]
            else:
                alpha = X[:, :, -1]

            self.grey_arrayd[key] = alpha
        return alpha

    def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        """
        Returns latex's rendering of the tex string as an rgba array
        """
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        r, g, b = rgb
        key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb)
        Z = self.rgba_arrayd.get(key)

        if Z is None:
            alpha = self.get_grey(tex, fontsize, dpi)

            Z = np.zeros((alpha.shape[0], alpha.shape[1], 4), np.float)

            Z[:, :, 0] = r
            Z[:, :, 1] = g
            Z[:, :, 2] = b
            Z[:, :, 3] = alpha
            self.rgba_arrayd[key] = Z

        return Z

    def get_text_width_height_descent(self, tex, fontsize, renderer=None):
        """
        return width, heigth and descent of the text.
        """
        if tex.strip() == '':
            return 0, 0, 0

        if renderer:
            dpi_fraction = renderer.points_to_pixels(1.)
        else:
            dpi_fraction = 1.

        if rcParams['text.latex.preview']:
            # use preview.sty
            basefile = self.get_basefile(tex, fontsize)
            baselinefile = '%s.baseline' % basefile

            if DEBUG or not os.path.exists(baselinefile):
                dvifile = self.make_dvi_preview(tex, fontsize)

            with open(baselinefile) as fh:
                l = fh.read().split()
            height, depth, width = [float(l1) * dpi_fraction for l1 in l]
            return width, height + depth, depth

        else:
            # use dviread. It sometimes returns a wrong descent.
            dvifile = self.make_dvi(tex, fontsize)
            dvi = dviread.Dvi(dvifile, 72 * dpi_fraction)
            try:
                page = next(iter(dvi))
            finally:
                dvi.close()
            # A total height (including the descent) needs to be returned.
            return page.width, page.height + page.descent, page.descent
Example #23
0
# log
#
# Test Dr. Ross Mitchell's algorithm on GBM data
# to find error on our code
#

import csv
import os

import matplotlib as mpl
import numpy as np

import GLCMTextureSecret
import TextureSecret

print mpl.get_cachedir()

# import SimpleITK as sitk
# from string import maketrans

rootDir = '/Users/yanzhexu/Desktop/Research/GBM/aCGH_whole_tumor_maps_for_Neuro-Onc_dataset/RWh_slices_only/slice17/'

outputDir = '/Users/yanzhexu/Desktop/Research/GBM/TestGLCM'

featuresOutFn = 'GBM_features_GLCM_test_X_Y_ym4xm4_yp4xp4_TestAlgorithm.csv'

featureCSVFn = os.path.join(outputDir, featuresOutFn)

grayScales = 256

ycoord = 126
Example #24
0
@lru_cache()
def is_opentype_cff_font(filename):
    """
    Returns True if the given font is a Postscript Compact Font Format
    Font embedded in an OpenType wrapper.  Used by the PostScript and
    PDF backends that can not subset these fonts.
    """
    if os.path.splitext(filename)[1].lower() == '.otf':
        with open(filename, 'rb') as fd:
            return fd.read(4) == b"OTTO"
    else:
        return False


_get_font = lru_cache(64)(ft2font.FT2Font)
_fmcache = os.path.join(mpl.get_cachedir(),
                        'fontlist-v{}.json'.format(FontManager.__version__))
fontManager = None


def get_font(filename, hinting_factor=None):
    if hinting_factor is None:
        hinting_factor = rcParams['text.hinting_factor']
    return _get_font(filename, hinting_factor)


def _rebuild():
    global fontManager
    fontManager = FontManager()
    with cbook._lock_path(_fmcache):
        json_dump(fontManager, _fmcache)
Example #25
0
import matplotlib as mpl
print(mpl.get_configdir())
'C:\\Documents and Settings\\jdhunter\\.matplotlib'
print(mpl.get_cachedir())
print(mpl.__file__)
C:\Users\matth\AppData\Local\Programs\Python\Python38\Lib\site-packages\ffmpeg
Example #26
0
import numpy
import numpy as np

import matplotlib as mpl
from matplotlib.font_manager import FontProperties

zhfont = FontProperties(
    fname="/usr/share/fonts/cjkuni-ukai/ukai.ttc")  # 图片显示中文字体
mpl.use('Agg')
mpl.get_cachedir()
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab

import scipy.io as sio

import pandas as pd
import seaborn as sns
import GVal
from toolkitJ import cell2dmatlab_jsp
import copy


def featurePlotting(label_all, online_fea_all, processCode):

    nega_index_raw = np.nonzero(label_all[0] == 0)[0]
    posi1_index_raw = np.nonzero(label_all[0] == 1)[0]
    posi2_index_raw = np.nonzero(label_all[0] == 2)[0]
    posi3_index_raw = np.nonzero(label_all[0] == 3)[0]

    # [nega, posi1, posi2, posi3]
    colorlist1 = ['#000000', '#000000', '#0d75f8', '#e50000']
Example #27
0
class TexManager:
    """
    Convert strings to dvi files using TeX, caching the results to a directory.

    Repeated calls to this constructor always return the same instance.
    """

    texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')

    _grey_arrayd = {}
    _font_family = 'serif'
    _font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
    _font_info = {
        'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
        'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
        'times': ('ptm', r'\usepackage{mathptmx}'),
        'palatino': ('ppl', r'\usepackage{mathpazo}'),
        'zapf chancery': ('pzc', r'\usepackage{chancery}'),
        'cursive': ('pzc', r'\usepackage{chancery}'),
        'charter': ('pch', r'\usepackage{charter}'),
        'serif': ('cmr', ''),
        'sans-serif': ('cmss', ''),
        'helvetica': ('phv', r'\usepackage{helvet}'),
        'avant garde': ('pag', r'\usepackage{avant}'),
        'courier': ('pcr', r'\usepackage{courier}'),
        # Loading the type1ec package ensures that cm-super is installed, which
        # is necessary for unicode computer modern.  (It also allows the use of
        # computer modern at arbitrary sizes, but that's just a side effect.)
        'monospace': ('cmtt', r'\usepackage{type1ec}'),
        'computer modern roman': ('cmr', r'\usepackage{type1ec}'),
        'computer modern sans serif': ('cmss', r'\usepackage{type1ec}'),
        'computer modern typewriter': ('cmtt', r'\usepackage{type1ec}')
    }
    _font_types = {
        'new century schoolbook': 'serif',
        'bookman': 'serif',
        'times': 'serif',
        'palatino': 'serif',
        'charter': 'serif',
        'computer modern roman': 'serif',
        'zapf chancery': 'cursive',
        'helvetica': 'sans-serif',
        'avant garde': 'sans-serif',
        'computer modern sans serif': 'sans-serif',
        'courier': 'monospace',
        'computer modern typewriter': 'monospace'
    }

    grey_arrayd = _api.deprecate_privatize_attribute("3.5")
    font_family = _api.deprecate_privatize_attribute("3.5")
    font_families = _api.deprecate_privatize_attribute("3.5")
    font_info = _api.deprecate_privatize_attribute("3.5")

    @functools.lru_cache()  # Always return the same instance.
    def __new__(cls):
        Path(cls.texcache).mkdir(parents=True, exist_ok=True)
        return object.__new__(cls)

    def get_font_config(self):
        ff = rcParams['font.family']
        ff_val = ff[0].lower() if len(ff) == 1 else None
        reduced_notation = False
        if len(ff) == 1 and ff_val in self._font_families:
            self._font_family = ff_val
        elif len(ff) == 1 and ff_val in self._font_info:
            reduced_notation = True
            self._font_family = self._font_types[ff_val]
        else:
            _log.info(
                'font.family must be one of (%s) when text.usetex is '
                'True. serif will be used by default.',
                ', '.join(self._font_families))
            self._font_family = 'serif'

        fontconfig = [self._font_family]
        fonts = {}
        for font_family in self._font_families:
            if reduced_notation and self._font_family == font_family:
                fonts[font_family] = self._font_info[ff_val]
            else:
                for font in rcParams['font.' + font_family]:
                    if font.lower() in self._font_info:
                        fonts[font_family] = self._font_info[font.lower()]
                        _log.debug('family: %s, font: %s, info: %s',
                                   font_family, font,
                                   self._font_info[font.lower()])
                        break
                    else:
                        _log.debug('%s font is not compatible with usetex.',
                                   font)
                else:
                    _log.info(
                        'No LaTeX-compatible font found for the %s font'
                        'family in rcParams. Using default.', font_family)
                    fonts[font_family] = self._font_info[font_family]
            fontconfig.append(fonts[font_family][0])
        # Add a hash of the latex preamble to fontconfig so that the
        # correct png is selected for strings rendered with same font and dpi
        # even if the latex preamble changes within the session
        preamble_bytes = self.get_custom_preamble().encode('utf-8')
        fontconfig.append(hashlib.md5(preamble_bytes).hexdigest())

        # The following packages and commands need to be included in the latex
        # file's preamble:
        cmd = {
            fonts[family][1]
            for family in ['serif', 'sans-serif', 'monospace']
        }
        if self._font_family == 'cursive':
            cmd.add(fonts['cursive'][1])
        cmd.add(r'\usepackage{type1cm}')
        self._font_preamble = '\n'.join(sorted(cmd))

        return ''.join(fontconfig)

    def get_basefile(self, tex, fontsize, dpi=None):
        """
        Return a filename based on a hash of the string, fontsize, and dpi.
        """
        s = ''.join([
            tex,
            self.get_font_config(),
            '%f' % fontsize,
            self.get_custom_preamble(),
            str(dpi or '')
        ])
        return os.path.join(self.texcache,
                            hashlib.md5(s.encode('utf-8')).hexdigest())

    def get_font_preamble(self):
        """
        Return a string containing font configuration for the tex preamble.
        """
        return self._font_preamble

    def get_custom_preamble(self):
        """Return a string containing user additions to the tex preamble."""
        return rcParams['text.latex.preamble']

    def _get_preamble(self):
        return "\n".join([
            r"\documentclass{article}",
            # Pass-through \mathdefault, which is used in non-usetex mode to
            # use the default text font but was historically suppressed in
            # usetex mode.
            r"\newcommand{\mathdefault}[1]{#1}",
            self._font_preamble,
            r"\usepackage[utf8]{inputenc}",
            r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
            # geometry is loaded before the custom preamble as convert_psfrags
            # relies on a custom preamble to change the geometry.
            r"\usepackage[papersize=72in, margin=1in]{geometry}",
            self.get_custom_preamble(),
            # textcomp is loaded last (if not already loaded by the custom
            # preamble) in order not to clash with custom packages (e.g.
            # newtxtext) which load it with different options.
            r"\makeatletter"
            r"\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}"
            r"\makeatother",
        ])

    def make_tex(self, tex, fontsize):
        """
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        texfile = '%s.tex' % basefile
        fontcmd = {
            'sans-serif': r'{\sffamily %s}',
            'monospace': r'{\ttfamily %s}'
        }.get(self._font_family, r'{\rmfamily %s}')

        Path(texfile).write_text(r"""
%s
\pagestyle{empty}
\begin{document}
%% The empty hbox ensures that a page is printed even for empty inputs, except
%% when using psfrag which gets confused by it.
\fontsize{%f}{%f}%%
\ifdefined\psfrag\else\hbox{}\fi%%
%s
\end{document}
""" % (self._get_preamble(), fontsize, fontsize * 1.25, fontcmd % tex),
                                 encoding='utf-8')

        return texfile

    def _run_checked_subprocess(self, command, tex, *, cwd=None):
        _log.debug(cbook._pformat_subprocess(command))
        try:
            report = subprocess.check_output(
                command,
                cwd=cwd if cwd is not None else self.texcache,
                stderr=subprocess.STDOUT)
        except FileNotFoundError as exc:
            raise RuntimeError(
                'Failed to process string with tex because {} could not be '
                'found'.format(command[0])) from exc
        except subprocess.CalledProcessError as exc:
            raise RuntimeError(
                '{prog} was not able to process the following string:\n'
                '{tex!r}\n\n'
                'Here is the full report generated by {prog}:\n'
                '{exc}\n\n'.format(prog=command[0],
                                   tex=tex.encode('unicode_escape'),
                                   exc=exc.output.decode('utf-8'))) from exc
        _log.debug(report)
        return report

    def make_dvi(self, tex, fontsize):
        """
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        if not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            # Generate the dvi in a temporary directory to avoid race
            # conditions e.g. if multiple processes try to process the same tex
            # string at the same time.  Having tmpdir be a subdirectory of the
            # final output dir ensures that they are on the same filesystem,
            # and thus replace() works atomically.
            with TemporaryDirectory(dir=Path(dvifile).parent) as tmpdir:
                self._run_checked_subprocess([
                    "latex", "-interaction=nonstopmode", "--halt-on-error",
                    texfile
                ],
                                             tex,
                                             cwd=tmpdir)
                (Path(tmpdir) / Path(dvifile).name).replace(dvifile)
        return dvifile

    def make_png(self, tex, fontsize, dpi):
        """
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile
        # see get_rgba for a discussion of the background
        if not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            cmd = [
                "dvipng", "-bg", "Transparent", "-D",
                str(dpi), "-T", "tight", "-o", pngfile, dvifile
            ]
            # When testing, disable FreeType rendering for reproducibility; but
            # dvipng 1.16 has a bug (fixed in f3ff241) that breaks --freetype0
            # mode, so for it we keep FreeType enabled; the image will be
            # slightly off.
            bad_ver = parse_version("1.16")
            if (getattr(mpl, "_called_from_pytest", False)
                    and mpl._get_executable_info("dvipng").version != bad_ver):
                cmd.insert(1, "--freetype0")
            self._run_checked_subprocess(cmd, tex)
        return pngfile

    def get_grey(self, tex, fontsize=None, dpi=None):
        """Return the alpha channel."""
        if not fontsize:
            fontsize = rcParams['font.size']
        if not dpi:
            dpi = rcParams['savefig.dpi']
        key = tex, self.get_font_config(), fontsize, dpi
        alpha = self._grey_arrayd.get(key)
        if alpha is None:
            pngfile = self.make_png(tex, fontsize, dpi)
            rgba = mpl.image.imread(os.path.join(self.texcache, pngfile))
            self._grey_arrayd[key] = alpha = rgba[:, :, -1]
        return alpha

    def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)):
        r"""
        Return latex's rendering of the tex string as an rgba array.

        Examples
        --------
        >>> texmanager = TexManager()
        >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!"
        >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))
        """
        alpha = self.get_grey(tex, fontsize, dpi)
        rgba = np.empty((*alpha.shape, 4))
        rgba[..., :3] = mpl.colors.to_rgb(rgb)
        rgba[..., -1] = alpha
        return rgba

    def get_text_width_height_descent(self, tex, fontsize, renderer=None):
        """Return width, height and descent of the text."""
        if tex.strip() == '':
            return 0, 0, 0
        dvifile = self.make_dvi(tex, fontsize)
        dpi_fraction = renderer.points_to_pixels(1.) if renderer else 1
        with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
            page, = dvi
        # A total height (including the descent) needs to be returned.
        return page.width, page.height + page.descent, page.descent
Example #28
0
def is_opentype_cff_font(filename):
    """
    Returns True if the given font is a Postscript Compact Font Format
    Font embedded in an OpenType wrapper.  Used by the PostScript and
    PDF backends that can not subset these fonts.
    """
    if os.path.splitext(filename)[1].lower() == '.otf':
        with open(filename, 'rb') as fd:
            return fd.read(4) == b"OTTO"
    else:
        return False


_get_font = lru_cache(64)(ft2font.FT2Font)
_fmcache = os.path.join(
    mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
fontManager = None


def get_font(filename, hinting_factor=None):
    if hinting_factor is None:
        hinting_factor = rcParams['text.hinting_factor']
    return _get_font(filename, hinting_factor)


def _rebuild():
    global fontManager
    fontManager = FontManager()
    with cbook._lock_path(_fmcache):
        json_dump(fontManager, _fmcache)
    _log.info("generated new fontManager")
Example #29
0
@lru_cache()
def is_opentype_cff_font(filename):
    """
    Return whether the given font is a Postscript Compact Font Format Font
    embedded in an OpenType wrapper.  Used by the PostScript and PDF backends
    that can not subset these fonts.
    """
    if os.path.splitext(filename)[1].lower() == '.otf':
        with open(filename, 'rb') as fd:
            return fd.read(4) == b"OTTO"
    else:
        return False


_fmcache = os.path.join(
    mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
fontManager = None


_get_font = lru_cache(64)(ft2font.FT2Font)
# FT2Font objects cannot be used across fork()s because they reference the same
# FT_Library object.  While invalidating *all* existing FT2Fonts after a fork
# would be too complicated to be worth it, the main way FT2Fonts get reused is
# via the cache of _get_font, which we can empty upon forking (in Py3.7+).
if hasattr(os, "register_at_fork"):
    os.register_at_fork(after_in_child=_get_font.cache_clear)


def get_font(filename, hinting_factor=None):
    if hinting_factor is None:
        hinting_factor = rcParams['text.hinting_factor']
        if not is_string_like(prop):
            prop = prop.get_fontconfig_pattern()
        cached = _fc_match_cache.get(prop)
        if cached is not None:
            return cached

        result = fc_match(prop, fontext)
        if result is None:
            result = fc_match(':', fontext)

        _fc_match_cache[prop] = result
        return result

else:
    if not 'TRAVIS' in os.environ:
        cachedir = get_cachedir()
        if cachedir is not None:
            if sys.version_info[0] >= 3:
                _fmcache = os.path.join(cachedir, 'fontList.py3k.cache')
            else:
                _fmcache = os.path.join(cachedir, 'fontList.cache')
    else:
        _fmcache = None

    fontManager = None

    def _rebuild():
        global fontManager
        fontManager = FontManager()
        if _fmcache:
            pickle_dump(fontManager, _fmcache)
Example #31
0
def _install_mpl():
    """
    Install matplotlib styles and fonts

    """
    try:
        import matplotlib as mpl
        from matplotlib import get_cachedir
    except ImportError:
        warnings.warn(
            'Sorry, but we cannot install mpl plotting styles and fonts '
            'if MatPlotLib is not installed.\n'
            'Please install MatPlotLib using:\n'
            '  pip install matplotlib\n'
            'or\n'
            '  conda install matplotlib\n'
            'and then install again.')
        return

    # install all plotting styles in the matplotlib stylelib library
    stylesheets = Path("scp_data") / "stylesheets"
    if not stylesheets.exists():
        raise IOError(
            f"Can't find the stylesheets from SpectroChemPy {str(stylesheets)}.\n"
            f"Installation incomplete!")

    cfgdir = Path(mpl.get_configdir())
    stylelib = cfgdir / 'stylelib'
    if not stylelib.exists():
        stylelib.mkdir()

    styles = stylesheets.glob('*.mplstyle')
    for src in styles:
        dest = stylelib / src.name
        shutil.copy(src, dest)
    print(f'Stylesheets installed in {dest}')

    # install fonts in mpl-data
    # https://stackoverflow.com/a/47743010

    # Copy files over
    # _dir_data = Path(matplotlib_fname()).parent
    #_dir_data = Path(mpl.rcParams['datapath'])
    _dir_data = Path(mpl.get_data_path())

    dir_source = Path("scp_data") / 'fonts'
    if not dir_source.exists():
        raise IOError(f'directory {dir_source} not found!')

    dir_dest = _dir_data / 'fonts' / 'ttf'
    if not dir_dest.exists():
        dir_dest.mkdir(parents=True, exist_ok=True)

    for file in dir_source.glob('*.[ot]tf'):
        if not (dir_dest / file.name).exists():
            print(f'Adding font "{file.name}".')
            shutil.copy(file, dir_dest)
            if (dir_dest / file.name).exists():
                print('success')

    # Delete cache
    dir_cache = Path(get_cachedir())
    for file in list(dir_cache.glob('*.cache')) + list(
            dir_cache.glob('font*')):
        if not file.is_dir(
        ):  # don't dump the tex.cache folder... because dunno why
            file.unlink()
            print(f'Deleted font cache {file}.')