I am using PyLaTex to create a pdf document. I'm facing some issues with compilers.
I am running my program on MacOS-High Sierra, and have installed basic version of MacTeX Mactex Download and latexmk is also installed using
sudo tlmgr install latexmk
For the following starter code, I'm getting error in compilers loop. Here's the error log attached after code.
import numpy as np
from pylatex import Document, Section, Subsection, Tabular, Math, TikZ, Axis, \
Plot, Figure, Matrix, Alignat
from pylatex.utils import italic
import os
if __name__ == '__main__':
# image_filename = os.path.join(os.path.dirname(__file__), 'kitten.jpg')
geometry_options = {"tmargin": "1cm", "lmargin": "10cm"}
doc = Document(geometry_options=geometry_options)
with doc.create(Section('The simple stuff')):
doc.append('Some regular text and some')
doc.append(italic('italic text. '))
doc.append('\nAlso some crazy characters: $&#{}')
with doc.create(Subsection('Math that is incorrect')):
doc.append(Math(data=['2*3', '=', 9]))
with doc.create(Subsection('Table of something')):
with doc.create(Tabular('rc|cl')) as table:
table.add_hline()
table.add_row((1, 2, 3, 4))
table.add_hline(1, 2)
table.add_empty_row()
table.add_row((4, 5, 6, 7))
doc.generate_pdf('full', clean_tex=False, compiler_args='--latexmk')
Error code:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-dbe7f407e095> in <module>()
27
28
---> 29 doc.generate_pdf('full', clean_tex=False, compiler_args='--latexmk')
~/anaconda3/lib/python3.6/site-packages/pylatex/document.py in generate_pdf(self, filepath, clean, clean_tex, compiler, compiler_args, silent)
227
228 for compiler, arguments in compilers:
--> 229 command = [compiler] + arguments + compiler_args + main_arguments
230
231 try:
TypeError: can only concatenate list (not "str") to list
Please help me understand the error and fix the same
Regards,
Looks like a confusion between the compiler keyword argument, which accepts a string and compiler_args which accepts a list.
Maybe something like this is what you're after:
doc.generate_pdf('full', clean_tex=False, compiler='latexmk', compiler_args=['-c'])
Related
Good afternoon. Checking the text in the column, I came across characters that I didn't need:
"|,|.|
|2|5|0|1|6|ё|–|8|3|-|c|t|r|l|+|e|n|g|i|w|s|k|z|«|(|)|»|—|9|7|?|o|b|a|/|f|v|:|%|4|!|;|h|y|u|d|&|j|p|x|m|і|№|ұ|…|қ|$|_|[|]|“|”|ғ||||>|−|„|*|¬|ү|ң|#|©|―|q|→|’|∙|·| |ә| |ө|š|é|=||×|″|⇑|⇐|⇒|‑|′|\|<|#|'|˚| |ü|̇|̆|•|½|¾|ń|¤|һ|ý|{|}| |‘|ā|í||ī||ќ|ђ|°|‚|ѓ|џ|ļ|▶|新|千|歳|空|港|全|日|機|が|曲|り|き|れ|ず|に|雪|突|っ|込|む|ニ|ュ|ー|ス|¼|ù|~|ə|ў|ҳ|ό||€|🙂|¸|⠀|ä|¯|ツ|ї|ş|è|`|́|ҹ|®|²||ç| |☑|️|‼|ú|‒||👊|🏽|👁|ó|±|ñ|ł|ش|ا|ه|ن|م|›|
|£||||º
Text encoding - UTF8.
How do I correctly remove all these characters from a specific column (series) of a Pandas data frame?
I try
template = bad_symbols[0].str.cat(sep='|')
print(template)
template = re.compile(template, re.UNICODE)
test = label_data['text'].str.replace(template, '', regex=True)
And I get the following error:
"|,|.|
|2|5|0|1|6|ё|–|8|3|-|c|t|r|l|+|e|n|g|i|w|s|k|z|«|(|)|»|—|9|7|?|o|b|a|/|f|v|:|%|4|!|;|h|y|u|d|&|j|p|x|m|і|№|ұ|…|қ|$|_|[|]|“|”|ғ||||>|−|„|*|¬|ү|ң|#|©|―|q|→|’|∙|·| |ә| |ө|š|é|=||×|″|⇑|⇐|⇒|‑|′|\|<|#|'|˚| |ü|̇|̆|•|½|¾|ń|¤|һ|ý|{|}| |‘|ā|í||ī||ќ|ђ|°|‚|ѓ|џ|ļ|▶|新|千|歳|空|港|全|日|機|が|曲|り|き|れ|ず|に|雪|突|っ|込|む|ニ|ュ|ー|ス|¼|ù|~|ə|ў|ҳ|ό||€|🙂|¸|⠀|ä|¯|ツ|ї|ş|è|`|́|ҹ|®|²||ç| |☑|️|‼|ú|‒||👊|🏽|👁|ó|±|ñ|ł|ش|ا|ه|ن|م|›|
|£||||º
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-105-36817f343a8a> in <module>
5 print(template)
6
----> 7 template = re.compile(template, re.UNICODE)
8
9 test = label_data['text'].str.replace(template, '', regex=True)
5 frames
/usr/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
643 if not item or item[0][0] is AT:
644 raise source.error("nothing to repeat",
--> 645 source.tell() - here + len(this))
646 if item[0][0] in _REPEATCODES:
647 raise source.error("multiple repeat",
error: nothing to repeat at position 36 (line 2, column 30)
You need to escape your characters, use re.escape:
import re
template = '|'.join(map(re.escape, bad_symbols[0]))
Then, not need to compile, pandas will handle it for you:
test = label_data['text'].str.replace(template, '', regex=True, flags=re.UNICODE)
The below code snippet worked fine for python2, but does not work for python3. This code snippet is intended to allow for a luigi workflow to write to a multipage PDF, while still using the LocalTarget context manager that allows for atomicity.
import luigi
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
test = luigi.LocalTarget('test.pdf')
with test.open('wb') as fh, PdfPages(fh) as outf:
plt = plt.plot([1, 2, 3], [4, 5, 6])
This works in python2, but in python3 leads to the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-ba62e5b716d2> in <module>
----> 1 with test.open('wb') as fh, PdfPages(fh) as outf:
2 plt = plt.plot([1, 2, 3], [4, 5, 6])
~/miniconda3/envs/cat3.7/lib/python3.7/site-packages/matplotlib/backends/backend_pdf.py in __init__(self, filename, keep_empty, metadata)
2386
2387 """
-> 2388 self._file = PdfFile(filename, metadata=metadata)
2389 self.keep_empty = keep_empty
2390
~/miniconda3/envs/cat3.7/lib/python3.7/site-packages/matplotlib/backends/backend_pdf.py in __init__(self, filename, metadata)
445 self.fh = fh
446 self.currentstream = None # stream object to write to, if any
--> 447 fh.write(b"%PDF-1.4\n") # 1.4 is the first version to have alpha
448 # Output some eight-bit chars as a comment so various utilities
449 # recognize the file as binary by looking at the first few
TypeError: write() argument must be str, not bytes
How can I retain this atomic functionality in python3?
Sorry, I know this isn't the answer you're looking for, but I found this line in the Luigi repository in the definition of LocalTarget:
def open(self, mode='r'):
rwmode = mode.replace('b', '').replace('t', '')
...
https://github.com/spotify/luigi/blob/master/luigi/local_target.py#L159
It seems that they are not doing byte writing whatsoever (at least in the current version). I would definitely bring this up with them in the Github Issues.
I am no expert in the working of LocalTarget, so i do not know if there is a reason for the removal of the b-flag, or if this is a bug.
A way to work around this is to wrap the code using the temporary_path function:
import luigi
class BinaryFileExample(luigi.Task):
def output(self):
return luigi.LocalTarget("simple_binary_file.extension")
def run(self):
with self.output().temporary_path() as my_binary_file_path:
with open(my_binary_file_path, 'wb') as inner_file:
newFileBytes = [123, 3, 255, 0, 100]
for byte in newFileBytes:
inner_file.write(byte.to_bytes(1, byteorder='big'))
I am trying to run a hidden markov model, however the fit function doesn't work properly.
Code:
import numpy as np
from hmmlearn import hmm
X1 = [[0.5], [1.0], [-1.0], [0.42], [0.24]]
X2 = [[2.4], [4.2], [0.5], [-0.24]]
X = np.concatenate([X1, X2])
lengths = [len(X1), len(X2)]
hmm.GaussianHMM(n_components=3).fit(X, lengths)
I get this error message:
TypeError Traceback (most recent call last)
<ipython-input-16-cdfada1be202> in <module>()
8 lengths = [len(X1), len(X2)]
9
---> 10 hmm.GaussianHMM(n_components=3).fit(X, lengths)
TypeError: fit() takes 2 positional arguments but 3 were given
Please check the version of hmmlearn you have and update that. The lengths param is available in newer versions as seen here
http://hmmlearn.readthedocs.io/en/latest/api.html#hmmlearn.hmm.GaussianHMM.fit
Then try doing (as #Harpal suggested):
hmm.GaussianHMM(n_components=3).fit(X, lengths=lengths)
This error can be reproduced for hmmlearn 0.1.1,
however if you do a pip install hmmlearn==0.2.0 in your virtual env and follow up with hmm.GaussianHMM(n_components=3).fit(X, lengths=lengths).
Things should work out just fine!
Using sklearn.cluster.KMeans. Nearly this exact code worked earlier, all I changed was the way I built my dataset. I have just no idea where even to start... Here's the code:
from sklearn.cluster import KMeans
km = KMeans(n_clusters=20)
for item in dfX:
if type(item) != type(dfX[0]):
print(item)
print(len(dfX))
print(dfX[:10])
km.fit(dfX)
print(km.cluster_centers_)
Which outputs the following:
12147
[1.201, 1.237, 1.092, 1.074, 0.979, 0.885, 1.018, 1.083, 1.067, 1.071]
/home/sbendl/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
Traceback (most recent call last):
File "/home/sbendl/PycharmProjects/MLFP/K-means.py", line 20, in <module>
km.fit(dfX)
File "/home/sbendl/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py", line 812, in fit
X = self._check_fit_data(X)
File "/home/sbendl/anaconda3/lib/python3.5/site-packages/sklearn/cluster/k_means_.py", line 789, in _check_fit_data
X.shape[0], self.n_clusters))
ValueError: n_samples=1 should be >= n_clusters=20
Process finished with exit code 1
As you can see from the output, there are definitely 12147 samples, which is greater than 20 in most counting systems ;). Additionally they're all floats, so it couldn't be having a problem with that. Anyone have any ideas?
Using anaconda distribution, Python 3.61 and using Jupyter notebook for Scipy/Numpy. I can use the print(' blah {} '.format(x)) to format numbers but if I iterate over a nparray I get an error.
# test of formatting
'{:+f}; {:+f}'.format(3.14, -3.14) # show it always
example stolen from the Python 3.6 manual section 6.1.3.2 Here and I get the expected response. So I know that it isn't that I've forgotten to import something i.e. it is built in.
if I do this:
C_sense = C_pixel + C_stray
print('Capacitance of node')
for x, y in np.nditer([Names,C_sense]):
print('The {} has C ={} [F]'.format(x,y))
I get output
Capacitance of node
The 551/751 has C =8.339999999999999e-14 [F]
The 554 has C =3.036e-13 [F]
The 511 has C =1.0376e-12 [F]
But if I do this:
# now with formatting
C_sense = C_pixel + C_stray
print('Capacitance of node')
for x, y in np.nditer([Names,C_sense]):
print('The {} has C ={:.3f} [F]'.format(x,y))
I get the following error:
TypeError Traceback (most recent call last)
<ipython-input-9-321e0b5edb03> in <module>()
3 print('Capacitance of node')
4 for x, y in np.nditer([Names,C_sense]):
----> 5 print('The {} has C ={:.3f} [F]'.format(x,y))
TypeError: unsupported format string passed to numpy.ndarray.__format__
I've attached a screen shot of my Jupyter notebook to show context of this code.
The error is clearly coming from the formatter, not knowing what to do with the numpy iterable you get from np.nditer.
Does the following work?
for x,y in zip(Names,C_sense):
print('The {} has C ={:.3f} [F]'.format(x,y))