Example #1
0
    def test_to_latex_float_format_no_fixed_width(self):

        # GH 21625
        df = DataFrame({'x': [0.19999]})
        expected = r"""\begin{tabular}{lr}
\toprule
{} &     x \\
\midrule
0 & 0.200 \\
\bottomrule
\end{tabular}
"""
        assert df.to_latex(float_format='%.3f') == expected

        # GH 22270
        df = DataFrame({'x': [100.0]})
        expected = r"""\begin{tabular}{lr}
\toprule
{} &   x \\
\midrule
0 & 100 \\
\bottomrule
\end{tabular}
"""
        assert df.to_latex(float_format='%.0f') == expected
Example #2
0
    def test_to_latex(self, frame):
        # it works!
        frame.to_latex()

        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex()
        withindex_expected = r"""\begin{tabular}{lrl}
\toprule
{} &  a &   b \\
\midrule
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withindex_result == withindex_expected

        withoutindex_result = df.to_latex(index=False)
        withoutindex_expected = r"""\begin{tabular}{rl}
\toprule
 a &   b \\
\midrule
 1 &  b1 \\
 2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withoutindex_result == withoutindex_expected
Example #3
0
    def test_to_latex_longtable(self, frame):
        frame.to_latex(longtable=True)

        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(longtable=True)
        withindex_expected = r"""\begin{longtable}{lrl}
\toprule
{} &  a &   b \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\end{longtable}
"""
        open("expected.txt", "w").write(withindex_result)
        assert withindex_result == withindex_expected

        withoutindex_result = df.to_latex(index=False, longtable=True)
        withoutindex_expected = r"""\begin{longtable}{rl}
\toprule
 a &   b \\
\midrule
\endhead
\midrule
\multicolumn{2}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
 1 &  b1 \\
 2 &  b2 \\
\end{longtable}
"""

        assert withoutindex_result == withoutindex_expected

        df = DataFrame({'a': [1, 2]})
        with1column_result = df.to_latex(index=False, longtable=True)
        assert "\multicolumn{1}" in with1column_result

        df = DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]})
        with3columns_result = df.to_latex(index=False, longtable=True)
        assert "\multicolumn{3}" in with3columns_result
Example #4
0
    def test_to_latex_filename(self, frame):
        with tm.ensure_clean('test.tex') as path:
            frame.to_latex(path)

            with open(path, 'r') as f:
                assert frame.to_latex() == f.read()

        # test with utf-8 and encoding option (GH 7061)
        df = DataFrame([[u'au\xdfgangen']])
        with tm.ensure_clean('test.tex') as path:
            df.to_latex(path, encoding='utf-8')
            with codecs.open(path, 'r', encoding='utf-8') as f:
                assert df.to_latex() == f.read()

        # test with utf-8 without encoding option
        if compat.PY3:  # python3: pandas default encoding is utf-8
            with tm.ensure_clean('test.tex') as path:
                df.to_latex(path)
                with codecs.open(path, 'r', encoding='utf-8') as f:
                    assert df.to_latex() == f.read()
        else:
            # python2 default encoding is ascii, so an error should be raised
            with tm.ensure_clean('test.tex') as path:
                with pytest.raises(UnicodeEncodeError):
                    df.to_latex(path)
Example #5
0
    def test_to_latex_with_formatters(self):
        df = DataFrame({'int': [1, 2, 3],
                        'float': [1.0, 2.0, 3.0],
                        'object': [(1, 2), True, False],
                        'datetime64': [datetime(2016, 1, 1),
                                       datetime(2016, 2, 5),
                                       datetime(2016, 3, 3)]})

        formatters = {'int': lambda x: '0x%x' % x,
                      'float': lambda x: '[% 4.1f]' % x,
                      'object': lambda x: '-%s-' % str(x),
                      'datetime64': lambda x: x.strftime('%Y-%m'),
                      '__index__': lambda x: 'index: %s' % x}
        result = df.to_latex(formatters=dict(formatters))

        expected = r"""\begin{tabular}{llrrl}
\toprule
{} & datetime64 &  float & int &    object \\
\midrule
index: 0 &    2016-01 & [ 1.0] & 0x1 &  -(1, 2)- \\
index: 1 &    2016-02 & [ 2.0] & 0x2 &    -True- \\
index: 2 &    2016-03 & [ 3.0] & 0x3 &   -False- \\
\bottomrule
\end{tabular}
"""
        assert result == expected
Example #6
0
def print_reduction():
    ton = 9
    f = open(DATAP + '/articledict.json', 'r', encoding="UTF8")
    langdict = load(f)
    df = DataFrame(columns=['#seed', '#art', '#rec_art', '#rec_nonseed', 'reduced %'],
                   index=['Formal languages', 'Computer file formats', 'Installation software'])
    for c in ROOTS:
        articles = [a for a in langdict if c + "Depth" in langdict[a]]
        seed_articles = [sa for sa in articles if is_seed(sa, langdict)]
        recovered_articles = [ra for ra in articles if check_sl(ra, langdict)]
        nonseed_articles = [nsa for nsa in articles if not is_seed(nsa, langdict) and check_sl(nsa, langdict)]
        percentage = 100 * (len(recovered_articles) / len(articles))
        FL_overlap = [fl for fl in recovered_articles if "Category:Formal_LanguagesDepth" in langdict[fl]]
        print(len(FL_overlap))
        CFF_overlap = [cff for cff in recovered_articles if "Category:Computer_file_formatsDepth" in langdict[cff]]
        print(len(CFF_overlap))
        IS_overlap = [i for i in recovered_articles if "Category:Installation_softwareDepth" in langdict[i]]
        print(len(IS_overlap))
        df.loc[c.replace("Category:", "").replace("_", " ")] = Series(
            {'#art': len(articles), '#seed': len(seed_articles),
             '#rec_art': len(recovered_articles), '#rec_nonseed': len(nonseed_articles),
             'reduced %': percentage})
        print("----")
    print(df)
    print(df.to_latex())

    nogo = [l for l in langdict if is_seed(l, langdict) and not check_sl(l, langdict)]
    print(nogo)
    print(langdict["Augmented_Backus–Naur_Form"])
Example #7
0
    def test_to_latex_specified_header(self):
        # GH 7124
        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(header=['AA', 'BB'])
        withindex_expected = r"""\begin{tabular}{lrl}
\toprule
{} & AA &  BB \\
\midrule
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withindex_result == withindex_expected

        withoutindex_result = df.to_latex(header=['AA', 'BB'], index=False)
        withoutindex_expected = r"""\begin{tabular}{rl}
\toprule
AA &  BB \\
\midrule
 1 &  b1 \\
 2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withoutindex_result == withoutindex_expected

        withoutescape_result = df.to_latex(header=['$A$', '$B$'], escape=False)
        withoutescape_expected = r"""\begin{tabular}{lrl}
\toprule
{} & $A$ & $B$ \\
\midrule
0 &   1 &  b1 \\
1 &   2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withoutescape_result == withoutescape_expected

        with pytest.raises(ValueError):
            df.to_latex(header=['A'])
Example #8
0
    def test_to_latex_longtable(self, frame):
        frame.to_latex(longtable=True)

        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(longtable=True)
        withindex_expected = r"""\begin{longtable}{lrl}
\toprule
{} &  a &   b \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\end{longtable}
"""

        assert withindex_result == withindex_expected

        withoutindex_result = df.to_latex(index=False, longtable=True)
        withoutindex_expected = r"""\begin{longtable}{rl}
\toprule
 a &   b \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
 1 &  b1 \\
 2 &  b2 \\
\end{longtable}
"""

        assert withoutindex_result == withoutindex_expected
	def print_pca_components(pca, name):
		from plotting import plot_pca_components
		components = DataFrame(pca.components_, columns=utils.get_latex_par_names_from_list(par.columns))
		filename = folder + 'clustering_%s_%s'%(dataset, name)
		plot_pca_components(filename=filename, components=components[::-1])
		components['$\\gamma$'] = pca.explained_variance_ratio_
		
		filename = folder + 'clustering_%s_%s.tex'%(dataset, name)
		tex = utils.prettify_table(components.to_latex(float_format=lambda x: str(round(x,2))), label='table:clustering_%s_%s'%(dataset, name), caption='XXX')
		with open(filename, 'w') as f:
				f.write(tex)
Example #10
0
    def test_to_latex_empty(self):
        df = DataFrame()
        result = df.to_latex()
        expected = r"""\begin{tabular}{l}
\toprule
Empty DataFrame
Columns: Index([], dtype='object')
Index: Index([], dtype='object') \\
\bottomrule
\end{tabular}
"""
        assert result == expected

        result = df.to_latex(longtable=True)
        expected = r"""\begin{longtable}{l}
\toprule
Empty DataFrame
Columns: Index([], dtype='object')
Index: Index([], dtype='object') \\
\end{longtable}
"""
        assert result == expected
Example #11
0
    def test_to_latex_no_header(self):
        # GH 7124
        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(header=False)
        withindex_expected = r"""\begin{tabular}{lrl}
\toprule
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withindex_result == withindex_expected

        withoutindex_result = df.to_latex(index=False, header=False)
        withoutindex_expected = r"""\begin{tabular}{rl}
\toprule
 1 &  b1 \\
 2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withoutindex_result == withoutindex_expected
Example #12
0
    def test_to_latex_special_escape(self):
        df = DataFrame([r"a\b\c", r"^a^b^c", r"~a~b~c"])

        escaped_result = df.to_latex()
        escaped_expected = r"""\begin{tabular}{ll}
\toprule
{} &       0 \\
\midrule
0 &   a\textbackslash b\textbackslash c \\
1 &  \textasciicircum a\textasciicircum b\textasciicircum c \\
2 &  \textasciitilde a\textasciitilde b\textasciitilde c \\
\bottomrule
\end{tabular}
"""
        assert escaped_result == escaped_expected
Example #13
0
    def test_to_latex_format(self, frame):
        # GH Bug #9402
        frame.to_latex(column_format='ccc')

        df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(column_format='ccc')
        withindex_expected = r"""\begin{tabular}{ccc}
\toprule
{} &  a &   b \\
\midrule
0 &  1 &  b1 \\
1 &  2 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withindex_result == withindex_expected
Example #14
0
    def test_to_latex_decimal(self, frame):
        # GH 12031
        frame.to_latex()

        df = DataFrame({'a': [1.0, 2.1], 'b': ['b1', 'b2']})
        withindex_result = df.to_latex(decimal=',')

        withindex_expected = r"""\begin{tabular}{lrl}
\toprule
{} &    a &   b \\
\midrule
0 &  1,0 &  b1 \\
1 &  2,1 &  b2 \\
\bottomrule
\end{tabular}
"""

        assert withindex_result == withindex_expected
Example #15
0
    def test_to_latex_escape_special_chars(self):
        special_characters = ['&', '%', '$', '#', '_', '{', '}', '~', '^',
                              '\\']
        df = DataFrame(data=special_characters)
        observed = df.to_latex()
        expected = r"""\begin{tabular}{ll}
\toprule
{} &  0 \\
\midrule
0 &  \& \\
1 &  \% \\
2 &  \$ \\
3 &  \# \\
4 &  \_ \\
5 &  \{ \\
6 &  \} \\
7 &  \textasciitilde \\
8 &  \textasciicircum \\
9 &  \textbackslash \\
\bottomrule
\end{tabular}
"""

        assert observed == expected
Example #16
0
    def test_to_latex_filename(self, frame):
        with tm.ensure_clean('test.tex') as path:
            frame.to_latex(path)

            with open(path, 'r') as f:
                assert frame.to_latex() == f.read()

        # test with utf-8 and encoding option (GH 7061)
        df = DataFrame([['au\xdfgangen']])
        with tm.ensure_clean('test.tex') as path:
            df.to_latex(path, encoding='utf-8')
            with codecs.open(path, 'r', encoding='utf-8') as f:
                assert df.to_latex() == f.read()

        # test with utf-8 without encoding option
        with tm.ensure_clean('test.tex') as path:
            df.to_latex(path)
            with codecs.open(path, 'r', encoding='utf-8') as f:
                assert df.to_latex() == f.read()
Example #17
0
    def test_to_latex_multiindex(self):
        df = DataFrame({('x', 'y'): ['a']})
        result = df.to_latex()
        expected = r"""\begin{tabular}{ll}
\toprule
{} &  x \\
{} &  y \\
\midrule
0 &  a \\
\bottomrule
\end{tabular}
"""

        assert result == expected

        result = df.T.to_latex()
        expected = r"""\begin{tabular}{lll}
\toprule
  &   &  0 \\
\midrule
x & y &  a \\
\bottomrule
\end{tabular}
"""

        assert result == expected

        df = DataFrame.from_dict({
            ('c1', 0): pd.Series(dict((x, x) for x in range(4))),
            ('c1', 1): pd.Series(dict((x, x + 4) for x in range(4))),
            ('c2', 0): pd.Series(dict((x, x) for x in range(4))),
            ('c2', 1): pd.Series(dict((x, x + 4) for x in range(4))),
            ('c3', 0): pd.Series(dict((x, x) for x in range(4))),
        }).T
        result = df.to_latex()
        expected = r"""\begin{tabular}{llrrrr}
\toprule
   &   &  0 &  1 &  2 &  3 \\
\midrule
c1 & 0 &  0 &  1 &  2 &  3 \\
   & 1 &  4 &  5 &  6 &  7 \\
c2 & 0 &  0 &  1 &  2 &  3 \\
   & 1 &  4 &  5 &  6 &  7 \\
c3 & 0 &  0 &  1 &  2 &  3 \\
\bottomrule
\end{tabular}
"""

        assert result == expected

        # GH 14184
        df = df.T
        df.columns.names = ['a', 'b']
        result = df.to_latex()
        expected = r"""\begin{tabular}{lrrrrr}
\toprule
a & \multicolumn{2}{l}{c1} & \multicolumn{2}{l}{c2} & c3 \\
b &  0 &  1 &  0 &  1 &  0 \\
\midrule
0 &  0 &  4 &  0 &  4 &  0 \\
1 &  1 &  5 &  1 &  5 &  1 \\
2 &  2 &  6 &  2 &  6 &  2 \\
3 &  3 &  7 &  3 &  7 &  3 \\
\bottomrule
\end{tabular}
"""
        assert result == expected

        # GH 10660
        df = pd.DataFrame({'a': [0, 0, 1, 1],
                           'b': list('abab'),
                           'c': [1, 2, 3, 4]})
        result = df.set_index(['a', 'b']).to_latex()
        expected = r"""\begin{tabular}{llr}
\toprule
  &   &  c \\
a & b &    \\
\midrule
0 & a &  1 \\
  & b &  2 \\
1 & a &  3 \\
  & b &  4 \\
\bottomrule
\end{tabular}
"""

        assert result == expected

        result = df.groupby('a').describe().to_latex()
        expected = r"""\begin{tabular}{lrrrrrrrr}
\toprule
{} & \multicolumn{8}{l}{c} \\
{} & count & mean &       std &  min &   25\% &  50\% &   75\% &  max \\
a &       &      &           &      &       &      &       &      \\
\midrule
0 &   2.0 &  1.5 &  0.707107 &  1.0 &  1.25 &  1.5 &  1.75 &  2.0 \\
1 &   2.0 &  3.5 &  0.707107 &  3.0 &  3.25 &  3.5 &  3.75 &  4.0 \\
\bottomrule
\end{tabular}
"""

        assert result == expected
Example #18
0
# Disregard outside this velocity range
vmax = -100 * u.km / u.s
vmin = -300 * u.km / u.s

spec_axis = cube.spectral_axis.to(u.km / u.s)
good_chans = np.logical_and(spec_axis <= vmax, spec_axis >= vmin)

# Same channel width in both
chan_width = \
    np.abs(cube.spectral_axis[1] - cube.spectral_axis[0]).to(u.km / u.s)

total_line_flux = np.nansum(total_spectrum[good_chans]) * chan_width
total_line_flux_arecibo = \
    np.nansum(total_spectrum_arecibo[good_chans]) * chan_width

# Distance is 0.84 Mpc
mass_conversion = 2.36e5 * (gal.distance.to(u.Mpc).value ** 2) * \
    (u.solMass / (u.Jy * u.km / u.s))
# These estimates are low by a factor of ~0.85. Not entirely sure why, but
# the estimate from the moment 0 is correct (comparing to Putman+09)

total_mass = total_line_flux * mass_conversion
total_mass_arecibo = total_line_flux_arecibo * mass_conversion
print("VLA HI Total Mass: {}".format(total_mass))
print("Arecibo HI Total Mass: {}".format(total_mass_arecibo))

df = DataFrame({"VLA Mass": [total_mass.value],
                "Arecibo Mass": [total_mass_arecibo.value]})
df.to_latex(paper1_tables_path("hi_masses.tex"))
df.to_csv(fourteenB_HI_data_path("tables/hi_masses.csv", no_check=True))
Example #19
0
from data import DATAP, INDICATORS
from json import load
from pandas import DataFrame, Series

f = open(DATAP + '/olangdict.json', 'r', encoding="UTF8")
ld = load(f)
inds = INDICATORS
df = DataFrame(columns=['Articles', 'Ex', 'Positive', 'Solo'], index=inds)
for ind in inds:
    sls = [l for l in ld if ld[l][ind] == 1]
    sls_only = [l for l in ld if ld[l][ind] == 1 and not any(ld[l][p] == 1 for p in inds if p is not ind)]
    print(ind + ": " + str(len(sls_only)) + "/" + str(len(sls)))
    df.loc[ind] = Series({'Articles': len(ld.keys()),
                          'Ex': 0,
                          'Positive': len(sls),
                          'Solo': len(sls_only)})
print(df.to_latex())

print(len(ld.keys()))
sls = len([l for l in ld if "DbpediaInfoboxTemplate" in ld[l]])
print(sls)
sls = len([l for l in ld if '(' in l])
print(sls)
Example #20
0
p.xlim([-100, 100])
p.legend()
p.ylim([-0.1, 1.1])
p.grid()
p.draw()

p.savefig(paper1_figures_path("total_profile_corrected_velocity_rotsub_hi_fit.pdf"))
p.savefig(paper1_figures_path("total_profile_corrected_velocity_rotsub_hi_fit.png"))
# raw_input("Next plot?")
p.clf()

# Save parameter table
hi_param_df = DataFrame({"Params": parvals,
                         "Errors": [np.sqrt(cov[i, i]) for i in range(cov.shape[0])]},
                        index=parnames)
hi_param_df.to_latex(paper1_tables_path("hi_gaussian_totalprof_fits.tex"))
hi_param_df.to_csv(fourteenB_HI_data_path("tables/hi_gaussian_totalprof_fits.csv",
                                          no_check=True))

# Lorentzian model

g_HI_init_lor = models.Lorentz1D(amplitude=1., x_0=0., fwhm=5.)

fit_g_lor = fitting.LevMarLSQFitter()

g_HI_lor = fit_g_lor(g_HI_init_lor, vels, norm_intens)

cov = fit_g_lor.fit_info['param_cov']
parnames = [n for n in g_HI_lor.param_names]
parvals = [v for (n, v) in zip(g_HI_lor.param_names, g_HI_lor.parameters)
           if n in parnames]