In [1]:
import os
import warnings
import numpy as np
import pandas
import csv
import re
In [2]:
def get_type(data):
return type(data)
In [3]:
a = np.array([[1,2,3], [3, 5, 6]])
In [5]:
a_string = np.array2string(a)
a_string
Out[5]:
In [7]:
a_string[2:][:-2]
Out[7]:
In [13]:
regex_1 = r"\[|\]"
regex_2 = r"\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\"
repl_3 = r","
for regex_repl in zip((regex_1, regex_2, regex_3), (repl_1, repl_2, repl_3)):
a_string = re.sub(regex_repl[0], regex_repl[1], a_string)
In [12]:
re.sub(regex_3, r"&", a_string[2:][:-2])
Out[12]:
In [173]:
Out[173]:
In [215]:
# Getting the largest number
a = np.array([[1, 3, 505, 42, 434], [2, 43, 43, 43, -4343], [432, 42, 43, 42, 424042]])
string_array = np.array2string(a)
a_string = a_string[2:][:-2]
regex_1 = r"\[|\]"
regex_2 = "\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\\\\n"
repl_3 = " & "
for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
# We need to do second pass. I really wish if there is an easy way of doing that
array_removed_spaces = " ".join(string_array.split())
string_array = re.sub(regex_3, repl_3, array_removed_spaces)
# Adding the extra \n
print("\\\\\n".join(string_array.split(r"\\")))
In [166]:
Out[166]:
In [50]:
5*"s"
Out[50]:
In [331]:
def tolatex(array, header, centring=True, caption="My table", label="table_something"):
"""
This function converts numpy ndarray into a valid latex table.
"""
string_array = np.array2string(array)[2:][:-2] # I use this for tables items
regex_1 = r"\[|\]"
regex_2 = "\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\\\\n"
repl_3 = " & "
a_string = re.sub(regex_1, repl_1, string_array) # I use this to compute the largest number length
string_table_items = a_string.replace("\n", "").split(" ")
length_table_items = max(len(str(i)) for i in string_table_items)
template = """\\begin{table}[]
%s
\\caption{%s}
\\label{%s}
\\begin{tabular}{@{%s}@{}}
\\toprule
""" % (centring, caption, label, len(header) * "l")
length_header_items = max(len(i) for i in header)
padding = " " * (max(length_table_items, length_header_items) // 2) + "&" + " " * (max(length_table_items, length_header_items) // 2)
midrule = " " + padding.join(header) + r"\\" + "\n" + " "* (max(length_table_items, length_header_items) // 2 + 2) + "\midrule" + "\n"
template += midrule
columns, rows = array.shape
if columns != header:
warnings.warn("The length of header doesn't match with the length of \
the column. We will placeholder it with an empty string.")
for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
# We need to do second pass. I really wish if there is an easy way of doing that
array_removed_spaces = " ".join(string_array.strip().split())
string_array = re.sub(regex_3, repl_3, array_removed_spaces)
# Adding the extra \n
string_array = "\t\\\\\n\t".join(string_array.split(r"\\"))
string_array = "\t" + string_array + "\\\\\n"+ " \\bottomrule" + "\n" + " \\end{tabular}" + "\n" + "\\end{table}"
return template + string_array
In [332]:
a = np.random.rand(7, 2)
print(tolatex(a, header=["One", "Two", "Three", "Four", "Five"]))
In [375]:
array = np.random.rand(6, 2)
string_array = np.array2string(array)[2:][:-2] # I use this for tables items
regex_1 = r"\[|\]"
regex_2 = "\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\\\\n"
repl_3 = " & "
a_string = re.sub(regex_1, repl_1, string_array) # I use this to compute the largest number length
string_table_items = a_string.replace("\n", "").split(" ")
length_table_items = max(len(str(i)) for i in string_table_items)
template = """\\begin{table}[]
%s
\\caption{%s}
\\label{%s}
\\begin{tabular}{@{%s}@{}}
\\toprule
""" % (centring, caption, label, len(header) * "l")
length_header_items = max(len(i) for i in header)
padding = " " * (max(length_table_items, length_header_items) // 2) + "&" + " " * (max(length_table_items, length_header_items) // 2)
midrule = " " + padding.join(header) + r"\\" + "\n" + " "* (max(length_table_items, length_header_items) // 2 + 2) + "\midrule" + "\n"
template += midrule
columns, rows = array.shape
if columns != header:
warnings.warn("The length of header doesn't match with the length of \
the column. We will placeholder it with an empty string.")
for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
# We need to do second pass. I really wish if there is an easy way of doing that
array_removed_spaces = " ".join(string_array.split())
string_array = re.sub(regex_3, repl_3, array_removed_spaces)
string_array = string_array.split("\\\\")
In [362]:
a.shape
Out[362]:
In [ ]: