示例#1
0
def test():
    doc = Document("utils_escape_latex")
    section = Section('Escape LaTeX characters test')

    text = escape_latex('''\
    & (ampersand)
    % (percent)
    $ (dollar)
    # (number)
    _ (underscore)
    { (left curly brace)
    } (right curly brace)
    ~ (tilde)
    ^ (caret)
    \\ (backslash)
    --- (three minuses)
    a\xA0a (non breaking space)
    [ (left bracket)
    ] (right bracket)
    ''')

    section.append(text)
    doc.append(section)

    doc.generate_pdf()
示例#2
0
def circuit_to_pdf_using_qcircuit_via_tex(circuit: circuits.Circuit,
                                          filepath: str,
                                          pdf_kwargs=None,
                                          qcircuit_kwargs=None,
                                          clean_ext=('dvi', 'ps'),
                                          documentclass='article'):
    """Compiles the QCircuit-based latex diagram of the given circuit.

    Args:
        circuit: The circuit to produce a pdf of.
        filepath: Where to output the pdf.
        pdf_kwargs: The arguments to pass to generate_pdf.
        qcircuit_kwargs: The arguments to pass to
            circuit_to_latex_using_qcircuit.
        clean_ext: The file extensions to clean up after compilation. By
            default, latexmk is used with the '-pdfps' flag, which produces
            intermediary dvi and ps files.
        documentclass: The documentclass of the latex file.
    """
    pdf_kwargs = {'compiler': 'latexmk', 'compiler_args': ['-pdfps'],
                  **({} if pdf_kwargs is None else pdf_kwargs)}
    qcircuit_kwargs = {} if qcircuit_kwargs is None else qcircuit_kwargs
    tex = circuit_to_latex_using_qcircuit(circuit, **qcircuit_kwargs)
    doc = Document(documentclass=documentclass, document_options='dvips')
    doc.packages.append(Package('amsmath'))
    doc.packages.append(Package('qcircuit'))
    doc.append(NoEscape(tex))
    doc.generate_pdf(filepath, **pdf_kwargs)
    for ext in clean_ext:
        try:
            os.remove(filepath + '.' + ext)
        except (OSError, IOError) as e:
            if e.errno != errno.ENOENT:
                raise
示例#3
0
def test():
    doc = Document('jobname_test', data=['Jobname test'])
    doc.generate_pdf()

    assert os.path.isfile('jobname_test.pdf')

    os.remove('jobname_test.pdf')

    folder = 'tmp_jobname'
    os.makedirs(folder)
    path = os.path.join(folder, 'jobname_test_dir')

    doc = Document(path, data=['Jobname test dir'])
    doc.generate_pdf()

    assert os.path.isfile(path + '.pdf')
    shutil.rmtree(folder)

    folder = 'tmp_jobname2'
    os.makedirs(folder)
    path = os.path.join(folder, 'jobname_test_dir2')

    doc = Document(path, data=['Jobname test dir'])
    doc.generate_pdf(os.path.join(folder, ''))

    assert os.path.isfile(path + '.pdf')

    shutil.rmtree(folder)
示例#4
0
def get_cv_doc(filename):
    """Returns a pylatex.Document instance pre-loaded with everything needed for my cv style."""
    doc = Document(filename,
                   documentclass='article')

    # Set Packages
    doc.packages.append(Package('marginnote'))
    doc.packages.append(UnsafeCommand('reversemarginpar'))
    doc.packages.append(Package('graphicx'))
    doc.packages.append(Package('classicthesis', options='nochapters'))
    doc.packages.append(Package('currvita', options='LabelsAligned'))
    doc.packages.append(Package('hyperref'))
    doc.packages.append(UnsafeCommand('hypersetup', extra_arguments=r'colorlinks, breaklinks, urlcolor=Maroon, linkcolor=Maroon'))

    doc.packages.append(UnsafeCommand('newlength', r'\datebox', ))
    doc.packages.append(UnsafeCommand('settowidth', r'\datebox', extra_arguments='Tuebingen, Germany'))

    doc.packages.append(UnsafeCommand('renewcommand', r'\cvheadingfont', extra_arguments=r'\LARGE\color{Maroon}'))

    # Unchanged-ish (Extra line break at the end)
    doc.packages.append(UnsafeCommand('newcommand', r'\SubHeading', options=1,
                             extra_arguments=r'\vspace{1em}\noindent\spacedlowsmallcaps{#1}\vspace{0.7em}\\'))

    doc.packages.append(UnsafeCommand('newcommand', r'\Email', options=1,
                             extra_arguments=r'\href{mailto:#1}{#1}'))

    # Unchanged
    doc.packages.append(UnsafeCommand('newcommand', r'\MarginText', options=1, extra_arguments=r'\marginpar{\raggedleft\small#1}'))

    # Unchanged
    doc.packages.append(UnsafeCommand('newcommand', r'\Description', options=1,
                             extra_arguments=r'\hangindent=2em\hangafter=0\footnotesize{#1}\par\normalsize\vspace{1em}'))

    doc.packages.append(UnsafeCommand('newcommand', r'\DescMarg', options=2,
                             extra_arguments=r'\noindent\hangindent=2em\hangafter=0 \parbox{\datebox}{\small} \MarginText{#1} #2 \vspace{0.3em}\\'))

    ##################
    doc.packages.append(UnsafeCommand('newcommand', r'\HeaderOnly', options=2,
                                      extra_arguments= r'\noindent\hangindent=2em\hangafter=0 \parbox{\datebox}{\small \textit{#1}}\hspace{1.5em} #2 \vspace{0.5em}\\'))

    doc.packages.append(UnsafeCommand('newcommand', r'\EntryHeader', options=3,
                             extra_arguments=r'\noindent\hangindent=2em\hangafter=0 \parbox{\datebox}{\small \textit{#2}}\hspace{1.5em} \MarginText{#1} #3 \vspace{0.5em}'))

    doc.packages.append(UnsafeCommand('newcommand', r'\NewEntry', options=4,
            extra_arguments=r'\EntryHeader{#1}{#2}{#3}\\\Description{#4}'))




    # Fill Document
    doc.append(UnsafeCommand('thispagestyle', 'empty'))
    doc.append(NoEscape(r'\raggedright'))

    return doc
示例#5
0
文件: pictures.py 项目: votti/PyLaTeX
def test():
    doc = Document()
    section = Section('Multirow Test')
    figure = Figure()
    image_filename = os.path.join(os.path.dirname(__file__),
                                  '../examples/kitten.jpg')
    figure.add_image(image_filename)
    figure.add_caption('Whoooo an imagage of a pdf')
    section.append(figure)
    doc.append(section)

    doc.generate_pdf()
def test():
    doc = Document()
    Subsection('Only a single string', data='Some words')

    sec1 = Section('Only contains one subsection', data='Subsection')

    sec2 = Section('Only a single italic command', data=Command('textit',
                                                                'Hey'))
    sec2.append('something else that is not italic')
    doc.append(sec1)
    doc.append(sec2)

    doc.generate_pdf()
示例#7
0
def arxiv_search(search_query):

    url = 'http://export.arxiv.org/api/query?search_query=all:' + \
        search_query + '&start=0&max_results=5'
    data = urllib.request.urlopen(url).read()

    soup = bs(data, features='xml')
    title_array = []
    summary_array = []

    for i in soup.findAll('title'):
        title_array.append(i)

    for i in soup.findAll('summary'):
        summary_array.append(i)

    doc = Document()
    doc.packages.append(
        Package(
            'geometry',
            options=[
                'tmargin=1cm',
                'lmargin=1cm',
                'rmargin=1cm']))

    with doc.create(Section('Search results for \"' + search_query + '\"')):

        for i in range(1, 5):
            doc.append(Subsection(italic(title_array[i].string)))
            doc.append(summary_array[i].string)

    doc.generate_pdf()
示例#8
0
def test():
    doc = Document("multirow_cm")

    with doc.create(Section('Multirow Test')):
        with doc.create(Subsection('Multicol')):
            # we need to keep track of the object here
            with doc.create(Tabular('|c|c|')) as table1:
                table1.add_hline()
                table1.add_multicolumn(2, '|c|', 'Multicol')
                table1.add_hline()
                table1.add_row((1, 2))
                table1.add_hline()
                table1.add_row((3, 4))
                table1.add_hline()

        with doc.create(Subsection('Multirow')):
            with doc.create(Tabular('|c|c|c|')) as table2:
                table2.add_hline()
                table2.add_multirow(3, '*', 'Multirow', cells=((1, 2), (3, 4),
                                                               (5, 6)))
                table2.add_hline()
                table2.add_multirow(3, '*', 'Multirow2')
                table2.add_hline()

    doc.generate_pdf()
示例#9
0
def createSimpleTable(tableColumns,d,filename):
    doc = Document()

    with doc.create(Table('|c|c|c|c|c|c|c|c|c|')) as table:

        for row in d:

            row[0]=row[0].replace('_','\_')
            row[0]="\\textbf{"+row[0]+"}"
            table.add_hline()
            table.add_row(tuple(row))
        table.add_hline()

    a = open(filename, 'wr')
    table.dump(a)
示例#10
0
    def __init__(self, lda_based_context):
        self.lda_based_context = lda_based_context
        self.doc =\
            Document(Constants.ITEM_TYPE + '-topic-models-nouns-complete-3')
        self.num_cols = Constants.TOPIC_MODEL_STABILITY_NUM_TERMS
        self.num_topics = Constants.TOPIC_MODEL_NUM_TOPICS
        self.rgb_tuples = None
        self.automatic_context_topic_colors = None
        self.keyword_context_topic_colors = None
        self.manual_context_topic_colors = None
        self.automatic_context_topic_ids = None
        self.keyword_context_topic_ids = None
        self.manual_context_topic_ids = None
        self.automatic_context_topic_words = None
        self.keyword_context_topic_words = None
        self.manual_context_topic_words = None
        self.headers = None
        self.topic_words_map = None
        self.table_format = '|c|' + 'c|' * (self.num_cols + 1)
        self.tagger = nltk.PerceptronTagger()
        self.tag_count_map = {'NN': 0, 'JJ': 0, 'VB': 0}

        self.init_colors()
        self.init_headers()
        self.init_topic_words()
        self.init_topic_ids()
        self.doc.packages.append(Package('color'))
        new_comm = UnsafeCommand(
            'newcommand', '\exampleCommand', options=4,
            extra_arguments=r'\colorbox[rgb]{#1,#2,#3}{#4} \color{black}')
        self.doc.append(new_comm)
        new_comm2 = UnsafeCommand('tiny')
        self.doc.append(new_comm2)
示例#11
0
 def setuplatex(self):
     self.filename = self.id + '-' + time.strftime('%Y')
     self.documentclass = Command('documentclass', arguments='scrlttr2', options=self.docoptions)
     self.doc = Document(self.filename, documentclass=self.documentclass, fontenc='T1', inputenc='utf8')
     self.doc.preamble.append(Command('input', latex_preamble))
     self.doc.preamble.append(Command('LoadLetterOption', 'template'))
     self.doc.preamble.append(Command('setkomavar', arguments='subject', extra_arguments=self.subject))
     self.doc.preamble.append(Command('setkomavar', arguments='yourmail', options=self.categroy[1], extra_arguments=self.filename))
示例#12
0
def generate_info_report():
			"""
			Generate a report with cover, status und last 2 lines in log for every test
			"""

			## Define the geometry for LaTeX files
			geometry_options = {
					"head": "40pt",
					"margin": "0.5in",
					"bottom": "0.6in",
					"includeheadfoot": True
				}


			## Create the LaTeX object, a instance of Document Class
			doc = Document(documentclass='article', geometry_options=geometry_options)

			## Add cover
			generate_cover2(doc)
			doc.append(NewPage())

			## Add status table
			generate_status_tabel(doc)
			doc.append(NewPage())

			## Add last 2 lines in log.txt
			for i in range(1, 4):
					generate_info_list(doc, i)


			doc.generate_pdf("RiMEA-Projekt Analyse", clean_tex=False)
示例#13
0
    def __init__(self, title):
        self.title = title
        self.doc = Document(title)
        self.doc.packages.append(Package('geometry', options=['a4paper', 'top=2cm', 'bottom=2.5cm', 'left=2cm', 'right=2cm']))
        self.doc.packages.append(Package('titlesec', options=['sf']))
        self.doc.packages.append(Package('lmodern'))
        self.doc.append(Command('sffamily'))

        return
示例#14
0
    def make_doc(self, foirequest):
        doc = Document(
            lmodern=True,
            geometry_options={
                "a4paper": True,
                "margin": "1in",
            },
        )
        # Change font
        doc.packages.append(Package('fontspec,xunicode,array'))
        doc.packages.append(Package('sectsty'))
        doc.preamble.append(NoEscape("\\usepackage{helvet}"))
        doc.preamble.append(NoEscape("\\sectionfont{\\fontsize{12}{15}\\selectfont}"))

        # Adjust description list
        doc.packages.append(Package('enumitem'))
        doc.preamble.append(NoEscape("\\setlist[description]{labelindent=0cm,style=multiline,leftmargin=1.5cm}"))

        # Hyphenation
        doc.append(NoEscape("\\lefthyphenmin=5"))
        doc.append(NoEscape("\\sloppy"))

        # doc.preamble.append(Command('title', foirequest.title))
        # doc.preamble.append(Command('author', foirequest.get_absolute_domain_short_url()))
        # doc.preamble.append(Command('date', NoEscape('\\today')))
        # doc.append(NoEscape('\\maketitle'))

        # Set up header and footer
        header = PageStyle("header")
        with header.create(Foot("L")):
            header.append(italic(settings.SITE_NAME))
        with header.create(Head("C")):
            header.append(foirequest.title)
        with header.create(Foot("R")):
            header.append(str(
                _('Request #{request_no}').format(request_no=foirequest.pk)))
        with header.create(Foot("C")):
            header.append(italic(NoEscape(r'Seite \thepage\ von \pageref{LastPage}')))
        doc.preamble.append(header)
        doc.change_document_style("header")

        for i, message in enumerate(foirequest.messages):
            last = i == len(foirequest.messages) - 1
            add_message_to_doc(doc, message)
            if not last:
                doc.append(NewPage())

        return doc
示例#15
0
def start_doc(title, date=datetime.today().date()):
	docclass = Command('documentclass', arguments=Arguments('article'), options=Options('titlepage'))
	# Start LaTex Doc
	doc = Document(title=title, date=date, author='Ken Payne [kenpay@]', maketitle=True, documentclass=docclass)
	#doc.packages.append(Command('usepackage', options=Options('a4paper'), arguments=Arguments('geometry')))
	doc.packages.append(Command('renewcommand', arguments=Arguments('\\familydefault','\sfdefault')))
	doc.packages.append(Command('usepackage', arguments=Arguments('caption, setspace')))
	doc.packages.append(Command('captionsetup', arguments=Arguments('font={large, stretch=1.3}')))
	#doc.packages.append(Command('usepackage', arguments=Arguments('showframe')))
	doc.packages.append(Command('usepackage', arguments=Arguments('geometry'), options=Options('top=1in', 'bottom=1in', 'left=1in', 'right=1in')))
	doc.packages.append(Command('usepackage', arguments=Arguments('tocloft')))
	doc.packages.append(Command('renewcommand', arguments=Arguments('\cftsecleader}{\cftdotfill{\cftdotsep}')))
	doc.packages.append(Command('usepackage', arguments=Arguments('booktabs')))
	doc.packages.append(Command('usepackage', arguments=Arguments('graphicx')))
	doc.packages.append(Command('usepackage', arguments=Arguments('subcaption')))
	doc.packages.append(Command('usepackage', arguments=Arguments('subcaption')))
	doc.packages.append(Command('usepackage', arguments=Arguments('xcolor'), options=Options('usenames,dvipsnames')))
	doc.packages.append(Command('usepackage', arguments=Arguments('fancyheadings')))
	doc.packages.append("\\pagestyle{fancy}")
	doc.packages.append("\\chead{\\bfseries \\textcolor{Gray}{CONFIDENTIAL}}")
	doc.packages.append("\\setcounter{totalnumber}{6}")
	doc.packages.append("\\usepackage{pdflscape}")
	doc.packages.append("\\usepackage[colorlinks = true, linkcolor = blue, urlcolor  = blue, citecolor = blue, anchorcolor = blue]{hyperref}")
	#doc.packages.append(Command('extrafloats', arguments=Arguments(100)))
	doc.packages.append("\\hypersetup{linktocpage}")
	doc.packages.append("\\usepackage{longtable}")
	doc.packages.append("\\usepackage[cc]{titlepic}")
	doc.packages.append("\\titlepic{\centering\includegraphics[width=0.3\\textwidth]{aws.png}}")
	doc.packages.append("\\usepackage[none]{hyphenat}")
	doc.packages.append("\\usepackage[flushleft]{threeparttable}")

	# ----- Doc Begins Here -----
	doc.append(Command('begin', arguments=Arguments('abstract')))
	doc.append("Please add campaign code \href{https://na3.salesforce.com/701500000016PTG}{EMEA-UKIR-FY15-SMART-Report Tool-Tracking} to any  opportunities\
				created. This helps me to asses the usefulness of the tool and to justify any time spent on it!\\\\\
				\\\\\
				Report generated from SMART data for given territory/territories, with the following aims:\\\\\
				\\begin{enumerate}\
				\\item To reduce the 'undiferentiated heavy lifting' associated with performance analysis of\
				a territory. It is my experiance that similar analytics are currently run using SMART/excel, but\
				depend heavily on detailed knowledge of these tools. This report aims remove this skill barrier between AM's\
				and valuable territory insight, allowing more time to be spent on direct customer engagements.\
				\\item To provide \\textbf{actionable} insights into customer spend behaviour by identifying lists of customers\
				exhibiting behaviours of interest (e.g. significant increases, customers moving across tier thresholds etc.).\
				\\end{enumerate}")
	doc.append(Command('end', arguments=Arguments('abstract')))
	doc.append(Command('tableofcontents'))
	return doc
示例#16
0
class TrainingDocumentWriter:

    def __init__(self, title):
        self.title = title
        self.doc = Document(title)
        self.doc.packages.append(Package('geometry', options=['a4paper', 'top=2cm', 'bottom=2.5cm', 'left=2cm', 'right=2cm']))
        self.doc.packages.append(Package('titlesec', options=['sf']))
        self.doc.packages.append(Package('lmodern'))
        self.doc.append(Command('sffamily'))

        return


    def append_plot(self, pyplot, caption=''):
        with self.doc.create(Plt(position='htbp')) as plot:
            plot.add_plot(pyplot, width=ur'\textwidth')
            if caption!='':
                plot.add_caption(caption)

        return

    def append_section(self, section_title='', content=''):
        with self.doc.create(Section(section_title)):
            if content!='':
                self.doc.append(content)
        return

    def generate_pdf(self,filename=u'', clean=True, compiler='pdflatex'):

        #This is a workaround to save the generated pdf in a different directory (not in working dir)
        cur_path = os.getcwd()
        dirlist = filename.split('/')
        os.chdir(os.path.dirname(filename)) #cd to specified directory

        #Remove possible existing file extension (.pdf)
        if '.' in filename:
            self.doc.generate_pdf(dirlist[len(dirlist)-1].split('.')[0])
        else:
            self.doc.generate_pdf(dirlist[len(dirlist)-1])

        os.chdir(cur_path) #cd to original working directory

        return
示例#17
0
文件: utils.py 项目: pylipp/mscthesis
def show_pdf(filename):
    """
    Compiles a simple tex document that the matplotlib2tikz-generated figure 'filename' is embedded
    in. Displays pdf.

    :param filename | Name of input tex snippet (without extension)
    """
    from pylatex import Document, Figure, Command, Package
    document = Document()
    document.packages.append(Package('pgfplots'))
    with document.create(Figure(placement='ht')) as fig:
        fig.append(Command("centering"))
        fig.append(Command("input", "../figures/{}.tex".format(filename)))
        fig.add_caption("Test printing.")

    document.generate_pdf(clean=True, clean_tex=True)

    from subprocess import call
    call(['evince', "../figures/default_filepath.pdf"])
示例#18
0
def generate_hr_tex(G, matchings, output_dir, stats_filename):
    """
    print statistics for the resident proposing stable,
    max-cardinality popular, and popular amongst max-cardinality
    matchings as a tex file
    :param G: graph
    :param matchings: information about the matchings
    """
    # create a tex file with the statistics
    doc = Document('table')
    # M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G))

    # add details about the graph, |A|, |B|, and # of edges
    n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A)
    with doc.create(Subsection('graph details')):
        with doc.create(Tabular('|c|c|')) as table:
            table.add_hline()
            table.add_row('n1', n1)
            table.add_hline()
            table.add_row('n2', n1)
            table.add_hline()
            table.add_row('m', m)
        table.add_hline()

    with doc.create(Subsection('general statistics')):
        with doc.create(Tabular('|c|c|c|c|')) as table:
            table.add_hline()
            table.add_row(('description', 'size', 'bp', 'bp ratio'))
            for desc in matchings:
                M = matchings[desc]
                sig = signature(G, M)
                msize = matching_utils.matching_size(G, M)
                bp = matching_utils.unstable_pairs(G, M)
                table.add_hline()
                table.add_row((desc, msize, len(bp), len(bp)/(m - msize)))
            table.add_hline()

    # statistics w.r.t. set A
    stats_for_partition(G, matchings, doc)

    # statistics w.r.t. set B
    # stats_for_partition(G, matchings, doc, False)

    stats_abs_path = os.path.join(output_dir, stats_filename)
    doc.generate_pdf(filepath=stats_abs_path, clean_tex='False')
    doc.generate_tex(filepath=stats_abs_path)
def build_document(transcript):
    """
    Processes a Transcript object to build a LaTeX document.
    """
    # Open temporary file
    doc = Document(documentclass='scrartcl', title=transcript.title,
                   subtitle=transcript.school,
                   author=transcript.student,
                   date=transcript.date.strftime('%d %B %Y'), temporary=True)

    doc.packages.append(Package('geometry', option='margin=1.0in'))
    doc.preamble.append(Command('renewcommand', argument=['\\familydefault', '\\sfdefault']))

    doc.append(Command('maketitle'))

    # Iterate through each transcript section
    for t_section in transcript.sections:
        # Create new section
        s = Section(escape_latex(t_section.title))
        # Add content to section
        for s_line in t_section.content:
            s_line = '\t'.join(s_line)
            s.append(escape_latex(s_line) + ' \\\n')

        # Add subsections to section
        for t_subsection in t_section.subsections:
            ss = Subsection(escape_latex(t_subsection.title))
            num_cols = max(len(l) for l in t_subsection.content)
            ss_table = Table(' l ' * num_cols)
            # Add content to subsection
            for ss_line in t_subsection.content:

                ss_line = '\t'.join(ss_line)
                if ss_line.startswith('Course Topic'):
                    ss_table.append('&')
                    ss_table.add_multicolumn(num_cols-1, 'l',
                                             escape_latex(ss_line))
                    ss_table.append(r'\\')
                elif not ss_line[:3].isupper() and not ss_line.startswith('Test'):
                    ss_table.add_multicolumn(num_cols, 'l', escape_latex(ss_line))
                    ss_table.append(r'\\')
                else:
                    if ss_line.startswith('TERM'):
                        ss_table.add_hline()
                    filled = escape_latex(ss_line).split('\t')
                    filled += (num_cols - len(filled)) * ['']
                    ss_table.add_row(filled)

            ss.append(ss_table)
            s.append(ss)

        doc.append(s)
    doc.generate_pdf(clean=True)

    return doc
示例#20
0
def main(argv):
	doclink = ''
	outputfile = ''
	try:
		opts, args = getopt.getopt(argv,"hl:o:n:",["doclink=","output=","toolname="])
	except getopt.GetoptError:
		sys.exit(2)
	global defaultname
	for opt, arg in opts:
		if opt == '-h':
			print("paperpp.py -l <document class link> -o <outputfile> -n <name>")
			sys.exit()
		elif opt in ("-l", "--doclink"):
			doclink = arg
		elif opt in ("-n", "--toolname"):
			defaultname = arg
		elif opt in ("-o", "--output"):
			outputfile = arg 
	documentcls=download_doc_class(doclink)
	print("Initialize docclass:",documentcls)
	paperfile = currdir+ os.sep +"paper"
	macrofile = currdir+ os.sep +"macro.tex" 
	with open(macrofile,"w+") as f:
		f.write("\\newcommand{\\toolname}{"+defaultname+"}")
	
	name = current_user()
	print("Current_user:"******".tex",paperfile+".tex.copy")	
	#doc.generate_pdf(paperfile)
	shutil.copy2(paperfile+".tex.copy",paperfile+".tex")	
	os.remove(paperfile+".tex.copy")
示例#21
0
def generate_heuristic_tex(G, matchings, output_dir, stats_filename):
    """
    print statistics for the hospital proposing heuristic as a tex file
    :param G: graph
    :param matchings: information about the matchings
    """
    # create a tex file with the statistics
    doc = Document('table')

    # add details about the graph, |A|, |B|, and # of edges
    n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A)
    with doc.create(Subsection('graph details')):
        with doc.create(Tabular('|c|c|')) as table:
            table.add_hline()
            table.add_row('n1', n1)
            table.add_hline()
            table.add_row('n2', n1)
            table.add_hline()
            table.add_row('m', m)
        table.add_hline()

    M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G))
    with doc.create(Subsection('Size statistics')):
        with doc.create(Tabular('|c|c|c|c|c|c|c|')) as table:
            table.add_hline()
            table.add_row(('description', 'size', 'bp', 'bp ratio', 'block-R',
                           'rank-1', 'deficiency'))
            for desc in matchings:
                M = matchings[desc]
                sig = signature(G, M)
                bp = matching_utils.unstable_pairs(G, M)
                msize = matching_utils.matching_size(G, M)
                table.add_hline()
                table.add_row((desc, msize, len(bp), len(bp)/(m - msize),
                               len(blocking_residents(bp)),
                               sum_ranks(sig, (1,)), #sum_ranks(sig, (1, 2, 3)),
                               total_deficiency(G, M_s)))
            table.add_hline()

    stats_abs_path = os.path.join(output_dir, stats_filename)
    doc.generate_pdf(filepath=stats_abs_path, clean_tex='False')
    doc.generate_tex(filepath=stats_abs_path)
示例#22
0
文件: args.py 项目: markdoerr/PyLaTeX
def test_document():
    doc = Document(
        default_filepath="default_filepath",
        documentclass="article",
        fontenc="T1",
        inputenc="utf8",
        lmodern=True,
        data=None,
    )

    doc.append("Some text.")

    doc.generate_tex(filepath="")
    doc.generate_pdf(filepath="", clean=True)
示例#23
0
    def _upload_problem(self, problem):
        """Uploads a specified problem to imgur.

        Returns:
            A tuple (str, (int, int)), where str is the url, on imgur and
            the tuples are the dimensions of the image (width, height).
        Raises:
           LatexParsingException : there was an issue parsing the document
        """
        default_doc = []
        # populate doc with the appropriate problem
        for line in problem[0]:
            # these first two statements are for wrapping the title around in a minipage which allows
            # the problem to be generated on one page and doesn't invoke \newpage
            if line == '\\begin{document}':
                default_doc.append(NoEscape(line))
                default_doc.append(NoEscape('\\begin{minipage}{\\textwidth}'))
            elif line == '\\maketitle':
                default_doc.append(NoEscape(line))
                default_doc.append(NoEscape('\\end{minipage}'))
            elif line == '\\end{itemize}':
                for line2 in problem[1]:
                    default_doc.append(NoEscape(line2))
                default_doc.append(NoEscape(line))
            else:
                default_doc.append(NoEscape(line))

        doc_class_line = NoEscape(default_doc[0])
        use_pkg_line = NoEscape(default_doc[1])
        # skip twocolumn since it makes the problem look spread awfully
        opts = filter(lambda pkg: pkg != 'twocolumn', doc_class_line[doc_class_line.find('[') + 1: doc_class_line.find(']')].split(','))
        args = NoEscape(doc_class_line[doc_class_line.find('{') + 1: doc_class_line.find('}')])
        doc = Document(documentclass=Command('documentclass', options=opts, arguments=args))
        # load packages
        doc.packages = [Package(i) for i in use_pkg_line[use_pkg_line.find('{') + 1: use_pkg_line.find('}')].split(',')]
        # position right after \begin{document}
        it = 4
        while default_doc[it].strip() != '\end{document}':
            doc.append(NoEscape(default_doc[it]))
            it += 1
        # fail safe for future problems which may not parse correctly
        try:
            doc.generate_pdf('default', compiler="pdflatex")
        except:
            raise LatexParsingException

        # These are normal Linux commands that are used to convert the pdf
        # file created by pylatex into a snippet
        os.system("pdfcrop default.pdf")
        os.system("pdftoppm default-crop.pdf|pnmtopng > default.png")
        path = os.path.abspath('default.png')
        uploaded_image = self._client.upload_image(path, title="LaTeX")
        return uploaded_image.link, OnlineImage.get_local_image_info(path)
示例#24
0
def test_document():
    doc = Document(
        default_filepath='default_filepath',
        documentclass='article',
        fontenc='T1',
        inputenc='utf8',
        lmodern=True,
        data=None,
    )

    repr(doc)

    doc.append('Some text.')

    doc.generate_tex(filepath='')
    doc.generate_pdf(filepath='', clean=True)
示例#25
0
def generate_eva_report():
		"""
    Generate a report which contains the evacution time for every test
    """
		geometry_options = {
		"head": "40pt",
		"margin": "0.5in",
		"bottom": "0.6in",
		"includeheadfoot": True
		}
		doc = Document(geometry_options=geometry_options)

		## Define style of report
		reportStyle = PageStyle("reportStyle")

		with reportStyle.create(Head("R")) as left_header:
				with left_header.create(MiniPage(width=NoEscape(r"0.49\textwidth"),
				                                  pos='c', align='l')) as title_wrapper:
					title_wrapper.append(LargeText(bold("RiMEA-Projekt")))
					title_wrapper.append(LineBreak())
					title_wrapper.append(MediumText(bold("Anlyse")))

		doc.preamble.append(reportStyle)

		## Apply Pagestyle
		doc.change_document_style("reportStyle")

		## Create table
		with doc.create(LongTabu("X[c] X[c]",
		                       row_height=1.5)) as report_table:
				report_table.add_row(["Test",
				                    "evacuation time(s)"],
				                   mapper=bold)
				report_table.add_hline()

		## Write the results of 3 tests in table
		for i in range(1,4):
			report_table.add_row(i, get_evac_time(i)[0])

		doc.append(NewPage())

		doc.generate_pdf("RiMEA-Projekt Evacution Analyse", clean_tex=False)
示例#26
0
文件: args.py 项目: rfilmyer/PyLaTeX
def test_document():
    doc = Document(
        default_filepath='default_filepath',
        documentclass='article',
        fontenc='T1',
        inputenc='utf8',
        author='',
        title='',
        date='',
        data=None,
        maketitle=False
    )

    doc.append('Some text.')

    doc.generate_tex(filepath='')
    doc.generate_pdf(filepath='', clean=True)
示例#27
0
    def __init__(self,title=None,author=None,date=None):
        # 初始化
        self.doc = Document()
        # 载入中文ctex包
        self.doc.packages.append(Package('ctex',options=['UTF8','noindent']))
        self.doc.packages.append(Package('geometry', options=['tmargin=1cm','lmargin=2cm']))
        # 设置标题,作者和日期
        if title is not None:
            self.doc.preamble.append(Command('title', title))
        if author is not None:
            self.doc.preamble.append(Command('author', author))
        if date is not None:
            self.doc.preamble.append(Command('date', date))
        self.doc.append(r'\maketitle')

        # 设置现在的章节
        self.presentSection = deque()

        # 初始化章节内容
        self.content = []
示例#28
0
文件: jobname.py 项目: votti/PyLaTeX
def test():
    doc = Document('jobname_test', title='Jobname test', maketitle=True)
    doc.generate_pdf()

    assert os.path.isfile('jobname_test.pdf')

    os.remove('jobname_test.pdf')

    folder = 'tmp_jobname'
    os.makedirs(folder)
    path = os.path.join(folder, 'jobname_test_dir')

    doc = Document(path, title='Jobname test dir', maketitle=True)
    doc.generate_pdf()

    assert os.path.isfile(path + '.pdf')

    filename = doc.select_filepath(os.path.join(folder, ''))
    assert '' != os.path.basename(filename)

    shutil.rmtree(folder)
示例#29
0
文件: tabus.py 项目: JelteF/PyLaTeX
def genenerate_tabus():
    geometry_options = {
        "landscape": True,
        "margin": "1.5in",
        "headheight": "20pt",
        "headsep": "10pt",
        "includeheadfoot": True
    }
    doc = Document(page_numbers=True, geometry_options=geometry_options)

    # Generate data table with 'tight' columns
    fmt = "X[r] X[r] X[r] X[r] X[r] X[r]"
    with doc.create(LongTabu(fmt, spread="0pt")) as data_table:
        header_row1 = ["Prov", "Num", "CurBal", "IntPay", "Total", "IntR"]
        data_table.add_row(header_row1, mapper=[bold])
        data_table.add_hline()
        data_table.add_empty_row()
        data_table.end_table_header()
        data_table.add_row(["Prov", "Num", "CurBal", "IntPay", "Total",
                            "IntR"])
        row = ["PA", "9", "$100", "%10", "$1000", "Test"]
        for i in range(40):
            data_table.add_row(row)

    with doc.create(Center()) as centered:
        with centered.create(Tabu("X[r] X[r]", spread="1in")) as data_table:
            header_row1 = ["X", "Y"]
            data_table.add_row(header_row1, mapper=[bold])
            data_table.add_hline()
            row = [randint(0, 1000), randint(0, 1000)]
            for i in range(4):
                data_table.add_row(row)

    with doc.create(Center()) as centered:
        with centered.create(Tabu("X[r] X[r]", to="4in")) as data_table:
            header_row1 = ["X", "Y"]
            data_table.add_row(header_row1, mapper=[bold])
            data_table.add_hline()
            row = [randint(0, 1000), randint(0, 1000)]
            for i in range(4):
                data_table.add_row(row)

    doc.generate_pdf("tabus", clean_tex=False)
示例#30
0
def description_classification(full_data_files, description_folder):
    data_list = list()
    for d in full_data_files:
        name = d.split('/')[2].split('.')[0]
        df = pd.read_csv(d,
                         sep='\s+',
                         header=None)
        lines, columns = df.shape
        attribute = columns - 1  # Minus one because of target
        last_pos = attribute
        classes = np.unique(df[last_pos])
        n_classes = len(classes)
        distribution_list = [len(df[df[last_pos] == c]) for c in classes]
        distribution_list.sort(reverse=True)
        distribution = tuple(distribution_list)
        data_list.append({'Dataset': name,
                          'Size': lines,
                          'Attributes': attribute,
                          'Classes': n_classes,
                          'Class distribution': distribution})

    df = pd.DataFrame(data_list)
    df = df.sort_values('Size', ascending=False)
    cols = ['Dataset',
            'Size',
            'Attributes',
            'Classes',
            'Class distribution']
    df = df[cols]
    df_copy = deepcopy(df)
    df_copy['Class distribution'] = [str(value).replace(', ', ';').replace(')', '').replace('(', '')
                                     for value in df_copy['Class distribution']]
    df_copy.to_csv(os.path.join(description_folder,
                                'data_description.csv'),
                   sep=',',
                   header=True,
                   columns=['Dataset',
                            'Size',
                            'Attributes',
                            'Classes',
                            'Class distribution'],
                   index=False)

    # # LaTeX
    df = df.set_index(['Dataset'])

    # Max classes per row
    max_classes = np.inf
    geometry_options = {
        "margin": "1.00cm",
        "includeheadfoot": True
    }
    doc = Document(page_numbers=True, geometry_options=geometry_options)

    # Generate data table
    with doc.create(LongTable("l l l l l")) as data_table:
            data_table.add_hline()
            header = ["Dataset",
                      "Size",
                      "#Attr.",
                      "#Classes",
                      "Class distribution"]
            data_table.add_row(header)
            data_table.add_hline()
            data_table.add_hline()
            for index in df.index.values:
                row = [index] + df.loc[index].values.tolist()
                if len(row[-1]) > max_classes:
                    max = max_classes
                    finished = False
                    subrow = row.copy()
                    # Select subtuple and removing last parenthesis
                    subrow[-1] = str(subrow[-1][:max]).replace(')', ',')
                    data_table.add_row(subrow)
                    while finished is False:
                        last_element = row[-1][max:max + max_classes]
                        if len(last_element) == 1:
                            # To string
                            last_element = str(last_element)
                            # Remove first and last parenthesis and comma
                            last_element = last_element[1:-2]
                        else:
                            # To string
                            last_element = str(last_element)
                            # Remove first and last parenthesis
                            last_element = last_element[1:-1]
                        max = max + max_classes
                        if max >= len(row[-1]):
                            finished = True
                            last_element += ')'
                        else:
                            # Remove last parenthesis or comma if len is 1
                            last_element = last_element[:-1]
                        subrow = ['', '', '', '', last_element]
                        data_table.add_row(subrow)

                else:
                    data_table.add_row(row)

    doc.generate_pdf(os.path.join(description_folder,
                                  'data_description'), clean_tex=False)
示例#31
0
class Traceability(Trace):
    """Automatically generate summary reports of the training.

    Args:
        save_path: Where to save the output files. Note that this will generate a new folder with the given name, into
            which the report and corresponding graphics assets will be written.
        extra_objects: Any extra objects which are not part of the Estimator, but which you want to capture in the
            summary report. One example could be an extra pipeline which performs pre-processing.

    Raises:
        OSError: If graphviz is not installed.
    """
    def __init__(self, save_path: str, extra_objects: Any = None):
        # Verify that graphviz is available on this machine
        try:
            pydot.Dot.create(pydot.Dot())
        except OSError:
            raise OSError(
                "Traceability requires that graphviz be installed. See www.graphviz.org/download for more information.")
        # Verify that the system locale is functioning correctly
        try:
            locale.getlocale()
        except ValueError:
            raise OSError("Your system locale is not configured correctly. On mac this can be resolved by adding \
                'export LC_ALL=en_US.UTF-8' and 'export LANG=en_US.UTF-8' to your ~/.bash_profile")
        super().__init__(inputs="*", mode="!infer")  # Claim wildcard inputs to get this trace sorted last
        # Report assets will get saved into a folder for portability
        path = os.path.normpath(save_path)
        path = os.path.abspath(path)
        root_dir = os.path.dirname(path)
        report = os.path.basename(path) or 'report'
        report = report.split('.')[0]
        self.save_dir = os.path.join(root_dir, report)
        self.figure_dir = os.path.join(self.save_dir, 'resources')
        self.report_name = None  # This will be set later by the experiment name
        os.makedirs(self.save_dir, exist_ok=True)
        os.makedirs(self.figure_dir, exist_ok=True)
        # Other member variables
        self.config_tables = []
        # Extra objects will automatically get included in the report since this Trace is @traceable, so we don't need
        # to do anything with them. Referencing here to stop IDEs from flagging the argument as unused and removing it.
        to_list(extra_objects)
        self.doc = Document()
        self.log_splicer = None

    def on_begin(self, data: Data) -> None:
        exp_name = self.system.summary.name
        if not exp_name:
            raise RuntimeError("Traceability reports require an experiment name to be provided in estimator.fit()")
        # Convert the experiment name to a report name (useful for saving multiple experiments into same directory)
        report_name = "".join('_' if c == ' ' else c for c in exp_name
                              if c.isalnum() or c in (' ', '_')).rstrip().lower()
        report_name = re.sub('_{2,}', '_', report_name)
        self.report_name = report_name or 'report'
        # Send experiment logs into a file
        log_path = os.path.join(self.figure_dir, f"{report_name}.txt")
        if self.system.mode != 'test':
            # If not running in test mode, we need to remove any old log file since it would get appended to
            with contextlib.suppress(FileNotFoundError):
                os.remove(log_path)
        self.log_splicer = LogSplicer(log_path)
        self.log_splicer.__enter__()
        # Get the initialization summary information for the experiment
        self.config_tables = self.system.summary.system_config
        models = self.system.network.models
        n_floats = len(self.config_tables) + len(models)

        self.doc = Document(geometry_options=['lmargin=2cm', 'rmargin=2cm', 'tmargin=2cm', 'bmargin=2cm'])
        # Keep tables/figures in their sections
        self.doc.packages.append(Package(name='placeins', options=['section']))
        self.doc.preamble.append(NoEscape(r'\usetikzlibrary{positioning}'))

        # Fix an issue with too many tables for LaTeX to render
        self.doc.preamble.append(NoEscape(r'\maxdeadcycles=' + str(2 * n_floats + 10) + ''))
        self.doc.preamble.append(NoEscape(r'\extrafloats{' + str(n_floats + 10) + '}'))

        # Manipulate booktab tables so that their horizontal lines don't break
        self.doc.preamble.append(NoEscape(r'\aboverulesep=0ex'))
        self.doc.preamble.append(NoEscape(r'\belowrulesep=0ex'))
        self.doc.preamble.append(NoEscape(r'\renewcommand{\arraystretch}{1.2}'))

        self.doc.preamble.append(Command('title', exp_name))
        self.doc.preamble.append(Command('author', f"FastEstimator {fe.__version__}"))
        self.doc.preamble.append(Command('date', NoEscape(r'\today')))
        self.doc.append(NoEscape(r'\maketitle'))

        # TOC
        self.doc.append(NoEscape(r'\tableofcontents'))
        self.doc.append(NoEscape(r'\newpage'))

    def on_end(self, data: Data) -> None:
        self._document_training_graphs()
        self.doc.append(NoEscape(r'\newpage'))
        self._document_fe_graph()
        self.doc.append(NoEscape(r'\newpage'))
        self._document_init_params()
        self._document_models()
        self._document_sys_config()

        # Need to move the tikz dependency after the xcolor package
        self.doc.dumps_packages()
        packages = self.doc.packages
        tikz = Package(name='tikz')
        packages.discard(tikz)
        packages.add(tikz)

        if shutil.which("latexmk") is None and shutil.which("pdflatex") is None:
            # No LaTeX Compiler is available
            self.doc.generate_tex(os.path.join(self.save_dir, self.report_name))
            suffix = '.tex'
        else:
            # Force a double-compile since some compilers will struggle with TOC generation
            self.doc.generate_pdf(os.path.join(self.save_dir, self.report_name), clean_tex=False, clean=False)
            self.doc.generate_pdf(os.path.join(self.save_dir, self.report_name), clean_tex=False)
            suffix = '.pdf'
        print("FastEstimator-Traceability: Report written to {}{}".format(os.path.join(self.save_dir, self.report_name),
                                                                          suffix))
        self.log_splicer.__exit__()

    def _document_training_graphs(self) -> None:
        """Add training graphs to the traceability document.
        """
        with self.doc.create(Section("Training Graphs")):
            log_path = os.path.join(self.figure_dir, f'{self.report_name}_logs.png')
            visualize_logs(experiments=[self.system.summary],
                           save_path=log_path,
                           verbose=False,
                           ignore_metrics={'num_device', 'logging_interval'})
            with self.doc.create(Figure(position='h!')) as plot:
                plot.add_image(os.path.relpath(log_path, start=self.save_dir),
                               width=NoEscape(r'1.0\textwidth,height=0.95\textheight,keepaspectratio'))

    def _document_fe_graph(self) -> None:
        """Add FE execution graphs into the traceability document.
        """
        with self.doc.create(Section("FastEstimator Architecture")):
            for mode in self.system.pipeline.data.keys():
                scheduled_items = self.system.pipeline.get_scheduled_items(
                    mode) + self.system.network.get_scheduled_items(mode) + self.system.traces
                signature_epochs = get_signature_epochs(scheduled_items, total_epochs=self.system.epoch_idx, mode=mode)
                epochs_with_data = self.system.pipeline.get_epochs_with_data(total_epochs=self.system.epoch_idx,
                                                                             mode=mode)
                if set(signature_epochs) & epochs_with_data:
                    self.doc.append(NoEscape(r'\FloatBarrier'))
                    with self.doc.create(Subsection(mode.capitalize())):
                        for epoch in signature_epochs:
                            if epoch not in epochs_with_data:
                                continue
                            self.doc.append(NoEscape(r'\FloatBarrier'))
                            with self.doc.create(
                                    Subsubsection(f"Epoch {epoch}",
                                                  label=Label(Marker(name=f"{mode}{epoch}", prefix="ssubsec")))):
                                diagram = self._draw_diagram(mode, epoch)
                                ltx = d2t.dot2tex(diagram.to_string(), figonly=True)
                                args = Arguments(**{'max width': r'\textwidth, max height=0.9\textheight'})
                                args.escape = False
                                with self.doc.create(Center()):
                                    with self.doc.create(AdjustBox(arguments=args)) as box:
                                        box.append(NoEscape(ltx))

    def _document_init_params(self) -> None:
        """Add initialization parameters to the traceability document.
        """
        from fastestimator.estimator import Estimator  # Avoid circular import
        with self.doc.create(Section("Parameters")):
            model_ids = {
                FEID(id(model))
                for model in self.system.network.models if isinstance(model, (tf.keras.Model, torch.nn.Module))
            }
            # Locate the datasets in order to provide extra details about them later in the summary
            datasets = {}
            for mode in ['train', 'eval', 'test']:
                objs = to_list(self.system.pipeline.data.get(mode, None))
                idx = 0
                while idx < len(objs):
                    obj = objs[idx]
                    if obj:
                        feid = FEID(id(obj))
                        if feid not in datasets:
                            datasets[feid] = ({mode}, obj)
                        else:
                            datasets[feid][0].add(mode)
                    if isinstance(obj, Scheduler):
                        objs.extend(obj.get_all_values())
                    idx += 1
            # Parse the config tables
            start = 0
            start = self._loop_tables(start,
                                      classes=(Estimator, BaseNetwork, Pipeline),
                                      name="Base Classes",
                                      model_ids=model_ids,
                                      datasets=datasets)
            start = self._loop_tables(start,
                                      classes=Scheduler,
                                      name="Schedulers",
                                      model_ids=model_ids,
                                      datasets=datasets)
            start = self._loop_tables(start, classes=Trace, name="Traces", model_ids=model_ids, datasets=datasets)
            start = self._loop_tables(start, classes=Op, name="Ops", model_ids=model_ids, datasets=datasets)
            start = self._loop_tables(start,
                                      classes=(Dataset, tf.data.Dataset),
                                      name="Datasets",
                                      model_ids=model_ids,
                                      datasets=datasets)
            start = self._loop_tables(start,
                                      classes=(tf.keras.Model, torch.nn.Module),
                                      name="Models",
                                      model_ids=model_ids,
                                      datasets=datasets)
            start = self._loop_tables(start,
                                      classes=types.FunctionType,
                                      name="Functions",
                                      model_ids=model_ids,
                                      datasets=datasets)
            start = self._loop_tables(start,
                                      classes=(np.ndarray, tf.Tensor, tf.Variable, torch.Tensor),
                                      name="Tensors",
                                      model_ids=model_ids,
                                      datasets=datasets)
            self._loop_tables(start, classes=Any, name="Miscellaneous", model_ids=model_ids, datasets=datasets)

    def _loop_tables(self,
                     start: int,
                     classes: Union[type, Tuple[type, ...]],
                     name: str,
                     model_ids: Set[FEID],
                     datasets: Dict[FEID, Tuple[Set[str], Any]]) -> int:
        """Iterate through tables grouping them into subsections.

        Args:
            start: What index to start searching from.
            classes: What classes are acceptable for this subsection.
            name: What to call this subsection.
            model_ids: The ids of any known models.
            datasets: A mapping like {ID: ({modes}, dataset)}. Useful for augmenting the displayed information.

        Returns:
            The new start index after traversing as many spaces as possible along the list of tables.
        """
        stop = start
        while stop < len(self.config_tables):
            if classes == Any or issubclass(self.config_tables[stop].type, classes):
                stop += 1
            else:
                break
        if stop > start:
            self.doc.append(NoEscape(r'\FloatBarrier'))
            with self.doc.create(Subsection(name)):
                self._write_tables(self.config_tables[start:stop], model_ids, datasets)
        return stop

    def _write_tables(self,
                      tables: List[FeSummaryTable],
                      model_ids: Set[FEID],
                      datasets: Dict[FEID, Tuple[Set[str], Any]]) -> None:
        """Insert a LaTeX representation of a list of tables into the current doc.

        Args:
            tables: The tables to write into the doc.
            model_ids: The ids of any known models.
            datasets: A mapping like {ID: ({modes}, dataset)}. Useful for augmenting the displayed information.
        """
        for tbl in tables:
            name_override = None
            toc_ref = None
            extra_rows = None
            if tbl.fe_id in model_ids:
                # Link to a later detailed model description
                name_override = Hyperref(Marker(name=str(tbl.name), prefix="subsec"),
                                         text=NoEscape(r'\textcolor{blue}{') + bold(tbl.name) + NoEscape('}'))
            if tbl.fe_id in datasets:
                modes, dataset = datasets[tbl.fe_id]
                title = ", ".join([s.capitalize() for s in modes])
                name_override = bold(f'{tbl.name} ({title})')
                # Enhance the dataset summary
                if isinstance(dataset, FEDataset):
                    extra_rows = list(dataset.summary().__getstate__().items())
                    for idx, (key, val) in enumerate(extra_rows):
                        key = f"{prettify_metric_name(key)}:"
                        if isinstance(val, dict) and val:
                            if isinstance(list(val.values())[0], (int, float, str, bool, type(None))):
                                val = jsonpickle.dumps(val, unpicklable=False)
                            else:
                                subtable = Tabular('l|l')
                                for k, v in val.items():
                                    if hasattr(v, '__getstate__'):
                                        v = jsonpickle.dumps(v, unpicklable=False)
                                    subtable.add_row((k, v))
                                val = subtable
                        extra_rows[idx] = (key, val)
            tbl.render_table(self.doc, name_override=name_override, toc_ref=toc_ref, extra_rows=extra_rows)

    def _document_models(self) -> None:
        """Add model summaries to the traceability document.
        """
        with self.doc.create(Section("Models")):
            for model in humansorted(self.system.network.models, key=lambda m: m.model_name):
                if not isinstance(model, (tf.keras.Model, torch.nn.Module)):
                    continue
                self.doc.append(NoEscape(r'\FloatBarrier'))
                with self.doc.create(Subsection(f"{model.model_name}")):
                    if isinstance(model, tf.keras.Model):
                        # Text Summary
                        summary = []
                        model.summary(line_length=92, print_fn=lambda x: summary.append(x))
                        summary = "\n".join(summary)
                        self.doc.append(Verbatim(summary))
                        with self.doc.create(Center()):
                            self.doc.append(HrefFEID(FEID(id(model)), model.model_name))

                        # Visual Summary
                        # noinspection PyBroadException
                        try:
                            file_path = os.path.join(self.figure_dir,
                                                     "{}_{}.pdf".format(self.report_name, model.model_name))
                            dot = tf.keras.utils.model_to_dot(model, show_shapes=True, expand_nested=True)
                            # LaTeX \maxdim is around 575cm (226 inches), so the image must have max dimension less than
                            # 226 inches. However, the 'size' parameter doesn't account for the whole node height, so
                            # set the limit lower (100 inches) to leave some wiggle room.
                            dot.set('size', '100')
                            dot.write(file_path, format='pdf')
                        except Exception:
                            file_path = None
                            print(
                                f"FastEstimator-Warn: Model {model.model_name} could not be visualized by Traceability")
                    elif isinstance(model, torch.nn.Module):
                        if hasattr(model, 'fe_input_spec'):
                            # Text Summary
                            # noinspection PyUnresolvedReferences
                            inputs = model.fe_input_spec.get_dummy_input()
                            self.doc.append(
                                Verbatim(
                                    pms.summary(model.module if self.system.num_devices > 1 else model,
                                                inputs,
                                                print_summary=False)))
                            with self.doc.create(Center()):
                                self.doc.append(HrefFEID(FEID(id(model)), model.model_name))
                            # Visual Summary
                            # Import has to be done while matplotlib is using the Agg backend
                            old_backend = matplotlib.get_backend() or 'Agg'
                            matplotlib.use('Agg')
                            # noinspection PyBroadException
                            try:
                                # Fake the IPython import when user isn't running from Jupyter
                                sys.modules.setdefault('IPython', MagicMock())
                                sys.modules.setdefault('IPython.display', MagicMock())
                                import hiddenlayer as hl
                                with Suppressor():
                                    graph = hl.build_graph(model.module if self.system.num_devices > 1 else model,
                                                           inputs)
                                graph = graph.build_dot()
                                graph.attr(rankdir='TB')  # Switch it to Top-to-Bottom instead of Left-to-Right
                                # LaTeX \maxdim is around 575cm (226 inches), so the image must have max dimension less
                                # than 226 inches. However, the 'size' parameter doesn't account for the whole node
                                # height, so set the limit lower (100 inches) to leave some wiggle room.
                                graph.attr(size="100,100")
                                graph.attr(margin='0')
                                file_path = graph.render(filename="{}_{}".format(self.report_name, model.model_name),
                                                         directory=self.figure_dir,
                                                         format='pdf',
                                                         cleanup=True)
                            except Exception:
                                file_path = None
                                print("FastEstimator-Warn: Model {} could not be visualized by Traceability".format(
                                    model.model_name))
                            finally:
                                matplotlib.use(old_backend)
                        else:
                            file_path = None
                            self.doc.append("This model was not used by the Network during training.")
                    if file_path:
                        with self.doc.create(Figure(position='ht!')) as fig:
                            fig.append(Label(Marker(name=str(FEID(id(model))), prefix="model")))
                            fig.add_image(os.path.relpath(file_path, start=self.save_dir),
                                          width=NoEscape(r'1.0\textwidth,height=0.95\textheight,keepaspectratio'))
                            fig.add_caption(NoEscape(HrefFEID(FEID(id(model)), model.model_name).dumps()))

    def _document_sys_config(self) -> None:
        """Add a system config summary to the traceability document.
        """
        with self.doc.create(Section("System Config")):
            with self.doc.create(Itemize()) as itemize:
                itemize.add_item(escape_latex(f"FastEstimator {fe.__version__}"))
                itemize.add_item(escape_latex(f"Python {platform.python_version()}"))
                itemize.add_item(escape_latex(f"OS: {sys.platform}"))
                itemize.add_item(f"Number of GPUs: {torch.cuda.device_count()}")
                if fe.fe_deterministic_seed is not None:
                    itemize.add_item(escape_latex(f"Deterministic Seed: {fe.fe_deterministic_seed}"))
            with self.doc.create(LongTable('|lr|', pos=['h!'], booktabs=True)) as tabular:
                tabular.add_row((bold("Module"), bold("Version")))
                tabular.add_hline()
                tabular.end_table_header()
                tabular.add_hline()
                tabular.add_row((MultiColumn(2, align='r', data='Continued on Next Page'), ))
                tabular.add_hline()
                tabular.end_table_footer()
                tabular.end_table_last_footer()
                color = True
                for name, module in humansorted(sys.modules.items(), key=lambda x: x[0]):
                    if "." in name:
                        continue  # Skip sub-packages
                    if name.startswith("_"):
                        continue  # Skip private packages
                    if isinstance(module, Base):
                        continue  # Skip fake packages we mocked
                    if hasattr(module, '__version__'):
                        tabular.add_row((escape_latex(name), escape_latex(str(module.__version__))),
                                        color='black!5' if color else 'white')
                        color = not color
                    elif hasattr(module, 'VERSION'):
                        tabular.add_row((escape_latex(name), escape_latex(str(module.VERSION))),
                                        color='black!5' if color else 'white')
                        color = not color

    def _draw_diagram(self, mode: str, epoch: int) -> pydot.Dot:
        """Draw a summary diagram of the FastEstimator Ops / Traces.

        Args:
            mode: The execution mode to summarize ('train', 'eval', 'test', or 'infer').
            epoch: The epoch to summarize.

        Returns:
            A pydot digraph representing the execution flow.
        """
        ds = self.system.pipeline.data[mode]
        if isinstance(ds, Scheduler):
            ds = ds.get_current_value(epoch)
        pipe_ops = get_current_items(self.system.pipeline.ops, run_modes=mode, epoch=epoch) if isinstance(
            ds, Dataset) else []
        net_ops = get_current_items(self.system.network.ops, run_modes=mode, epoch=epoch)
        traces = sort_traces(get_current_items(self.system.traces, run_modes=mode, epoch=epoch))
        diagram = pydot.Dot()
        diagram.set('rankdir', 'TB')
        diagram.set('dpi', 300)
        diagram.set_node_defaults(shape='record')

        # Make the dataset the first of the pipeline ops
        pipe_ops.insert(0, ds)
        label_last_seen = defaultdict(lambda: str(id(ds)))  # Where was this key last generated

        batch_size = ""
        if isinstance(ds, Dataset) and not isinstance(ds, BatchDataset):
            batch_size = self.system.pipeline.batch_size
            if isinstance(batch_size, Scheduler):
                batch_size = batch_size.get_current_value(epoch)
            if batch_size is not None:
                batch_size = f" (Batch Size: {batch_size})"
        self._draw_subgraph(diagram, label_last_seen, f'Pipeline{batch_size}', pipe_ops)
        self._draw_subgraph(diagram, label_last_seen, 'Network', net_ops)
        self._draw_subgraph(diagram, label_last_seen, 'Traces', traces)
        return diagram

    @staticmethod
    def _draw_subgraph(diagram: pydot.Dot,
                       label_last_seen: DefaultDict[str, str],
                       subgraph_name: str,
                       subgraph_ops: List[Union[Op, Trace, Any]]) -> None:
        """Draw a subgraph of ops into an existing `diagram`.

        Args:
            diagram: The diagram to be appended to.
            label_last_seen: A mapping of {data_dict_key: node_id} indicating the last node which generated the key.
            subgraph_name: The name to be associated with this subgraph.
            subgraph_ops: The ops to be wrapped in this subgraph.
        """
        subgraph = pydot.Cluster(style='dashed', graph_name=subgraph_name)
        subgraph.set('label', subgraph_name)
        subgraph.set('labeljust', 'l')
        for idx, op in enumerate(subgraph_ops):
            node_id = str(id(op))
            Traceability._add_node(subgraph, op, node_id)
            if isinstance(op, (Op, Trace)):
                # Need the instance check since subgraph_ops might contain a tf dataset or torch dataloader
                edge_srcs = defaultdict(lambda: [])
                for inp in op.inputs:
                    if inp == '*':
                        continue
                    edge_srcs[label_last_seen[inp]].append(inp)
                for src, labels in edge_srcs.items():
                    diagram.add_edge(pydot.Edge(src=src, dst=node_id, label=f" {', '.join(labels)} "))
                for out in op.outputs:
                    label_last_seen[out] = node_id
            if isinstance(op, Trace) and idx > 0:
                # Invisibly connect traces in order so that they aren't all just squashed horizontally into the image
                diagram.add_edge(pydot.Edge(src=str(id(subgraph_ops[idx - 1])), dst=node_id, style='invis'))
        diagram.add_subgraph(subgraph)

    @staticmethod
    def _add_node(diagram: Union[pydot.Dot, pydot.Cluster], op: Union[Op, Trace], node_id: str) -> None:
        """Draw a node onto a diagram based on a given op.

        Args:
            diagram: The diagram to be appended to.
            op: The op (or trace) to be visualized.
            node_id: The id to use as the node label.
        """
        if isinstance(op, Sometimes) and op.numpy_op:
            wrapper = pydot.Cluster(style='loosely dotted', graph_name=str(id(op)))
            wrapper.set('label', f'Sometimes ({op.prob}):')
            wrapper.set('labeljust', 'r')
            Traceability._add_node(wrapper, op.numpy_op, node_id)
            diagram.add_subgraph(wrapper)
        elif isinstance(op, OneOf) and op.numpy_ops:
            wrapper = pydot.Cluster(style='loosely dotted', graph_name=str(id(op)))
            wrapper.set('label', 'One Of:')
            wrapper.set('labeljust', 'r')
            Traceability._add_node(wrapper, op.numpy_ops[0], node_id)
            for sub_op in op.numpy_ops[1:]:
                Traceability._add_node(wrapper, sub_op, str(id(sub_op)))
            diagram.add_subgraph(wrapper)
        else:
            if isinstance(op, ModelOp):
                label = f"{op.__class__.__name__} ({FEID(id(op))}): {op.model.model_name}"
                model_ref = Hyperref(Marker(name=str(op.model.model_name), prefix='subsec'),
                                     text=NoEscape(r'\textcolor{blue}{') + bold(op.model.model_name) +
                                     NoEscape('}')).dumps()
                texlbl = f"{HrefFEID(FEID(id(op)), name=op.__class__.__name__).dumps()}: {model_ref}"
            else:
                label = f"{op.__class__.__name__} ({FEID(id(op))})"
                texlbl = HrefFEID(FEID(id(op)), name=op.__class__.__name__).dumps()
            diagram.add_node(pydot.Node(node_id, label=label, texlbl=texlbl))
示例#32
0
plt.xlabel('runs')
plt.savefig(throughput_image_dir)

# Bar chart for Average latency vs Siddhi version
plt.figure(2)
rects2 = plt.bar(ind, latency, width, color='blue')
plt.xticks(ind + width / 2, (no_of_file))
plt.ylabel('Average Latency(microseconds)')
plt.xlabel('runs')
plt.savefig(latency_image_dir)


# Report Generation
if __name__ == '__main__':
    # basic document
    doc = Document()

    doc.preamble.append(Command('title', 'Report'))
    doc.preamble.append(Command('author', 'Stream Processor Performance Testing-' + path))

    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))
    doc.append(NoEscape(r'\newpage'))

# Generating summary results chart for each run
# throughput summary table and chart

with doc.create(
        Section('Average Throughput During 60-180 seconds time period since the start of the benchmarking experiment')):
    doc.append(NoEscape(r'\hfill \break'))
    fmt = "X[r] X[r]"
示例#33
0
def main(fname, width, *args, **kwargs):
    doc = Document(fname)
    doc.packages.append(Package('geometry', options=['left=2cm', 'right=2cm']))

    doc.append('Introduction.')

    with doc.create(Section('I am a section')):
        doc.append('Take a look at this beautiful plot:')

        with doc.create(Figure(position='htbp')) as plot:
            plot.add_plot(width=NoEscape(width), *args, **kwargs)
            plot.add_caption('I am a caption.')

        doc.append('Created using matplotlib.')

    doc.append('Conclusion.')

    doc.generate_pdf()
示例#34
0
		"link_for_arised_issue_on_github": "https://github.com/JelteF/PyLaTeX/issues/209",
	}
"""
from pylatex import Document, LargeText
from pylatex.section import Paragraph;
from pylatex.utils import bold

geometry_options = {
	"head": "0.5pt",
	"margin": "0.5in",
	"bottom": "0.5in",
	"includeheadfoot": True
}

# documnet_options = ["a4paper"] can also be used to set the paper size
doc = Document("paragraph", indent=True, geometry_options=geometry_options, document_options="a4paper");

text1 = """At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id.""";

text2 = """quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis olor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessit atibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.""";

text3 = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duisaute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum""";

with doc.create(Paragraph("")):
	doc.append(LargeText( bold("Dear Malinikesh,")));

with doc.create(Paragraph("Paragraph1")):
	doc.append(text1);

with doc.create(Paragraph("Paragraph2")):
	doc.append(text2);
示例#35
0
def _add_string_notes(document: pylatex.Document, texts: dict,
                      images: dict) -> None:
    document.append(
        pylatex.Subsection(
            title=texts["strings"]["title"],
            label=False,
            numbering=False,
        ))

    document.append(
        pylatex.Subsubsection(
            title=texts["strings"]["subtitle0"],
            label=False,
            numbering=False,
        ))

    document.append(texts["strings"]["text0"])

    document.append(
        pylatex.Subsubsection(
            title=texts["microtonal_notation"]["title"],
            label=False,
            numbering=False,
        ))
    document.append(texts["microtonal_notation"]["text0"])
    document.append(_make_img(images["twelfth_tone_explanation"]))

    document.append(texts["microtonal_notation"]["text1"])

    for instrument in ("violin", "viola", "cello"):
        document.append(_make_img(images["scale_{}".format(instrument)]))
        document.append(
            _make_img(
                images["scale_{}_artificial_harmonics".format(instrument)]))
        document.append(
            pylatex.Command("hspace", arguments=[pylatex.NoEscape("5mm")]))

    document.append(texts["microtonal_notation"]["text2"])

    document.append(
        pylatex.Subsubsection(
            title=texts["strings"]["subtitle1"],
            label=False,
            numbering=False,
        ))
    document.append(_make_img(images["ornamentation"], width=0.25))
    document.append(texts["strings"]["text1"])

    document.append(_make_img(images["glissando"], width=0.28))

    document.append(texts["strings"]["text2"])
示例#36
0
    def create(self):
        try:
            self.status['text'] = 'Загрузка, подождите'
            self.root.update()
            # Настройки отступов, языка
            geometry_options = {
                "tmargin": "2cm",
                "lmargin": "3cm",
                "rmargin": "1.5cm",
                "bmargin": "2cm"
            }
            doc = Document(geometry_options=geometry_options)
            doc.packages.add(
                Package('grffile',
                        options=['encoding', 'filenameencoding=utf8']))
            doc.packages.add(Package('babel', options=['russian']))

            # Вставляем логотип МИРЭА
            image_filename = os.path.join(os.path.dirname(__file__),
                                          'логотип.png')
            with doc.create(Figure(position='h!')) as logo:
                logo.add_image(image_filename, width='80px')

            # Обложка
            doc.change_length(r"\TPHorizModule", "1mm")
            doc.change_length(r"\TPVertModule", "1mm")
            with doc.create(MiniPage(width=r"\textwidth")) as page:
                # Центральная часть обложки
                with page.create(TextBlock(180, 0, 0)):
                    page.append(Command('centering'))
                    page.append('МИНОБРНАУКИ РОССИИ\n')
                    page.append(
                        'Федеральное государственное бюджетное образовательное учреждение высшего образования\n'
                    )
                    page.append(
                        MediumText(
                            bold(
                                '«МИРЭА - Российский Технологический Университет»\n'
                            )))
                    page.append(LargeText(bold('РТУ МИРЭА\n')))
                    page.append(LineBreak())
                    page.append(
                        MediumText(
                            bold(
                                'Институт комплексной безопасности и специального приборостроения\n'
                            )))
                    page.append(LineBreak())
                    page.append(
                        'Кафедра КБ-8 «Информационное Противоборство»\n')
                    page.append(LineBreak())
                    page.append(LineBreak())
                    page.append(LineBreak())
                    page.append(LargeText('Отчёт\n'))
                    page.append(f'{str(self.entry1.get())}\n')

                # Правая часть обложки
                with page.create(TextBlock(80, 88, 120)):
                    if self.your_var.get() == 0:
                        page.append('Выполнил:\n')
                        page.append(
                            f'Студент {str(self.entry2.get())} курса\n')
                    else:
                        page.append('Выполнила:\n')
                        page.append(
                            f'Студентка {str(self.entry2.get())} курса\n')
                    page.append(f'Группа {str(self.entry3.get())}\n')
                    page.append(f'Шифр {str(self.entry4.get())}\n')
                    page.append(f'{str(self.entry5.get())}\n')
                    page.append(LineBreak())
                    page.append(LineBreak())
                    if self.var.get() == 0:
                        page.append('Проверил:\n')
                    else:
                        page.append('Проверила:\n')
                    page.append(f'{str(self.entry6.get())}\n')

            # Нижний колонтитул
            header = PageStyle("header")
            with header.create(Foot("C")):
                header.append(f'Москва, {time.now().timetuple().tm_year}\n')
            doc.preamble.append(header)
            doc.change_document_style("header")

            doc.generate_pdf("title", clean_tex=False)
            self.status['text'] = 'Готово!'

        except FileNotFoundError as e:
            self.status['text'] = str(e)
示例#37
0
class Resume1:
    def __init__(self, name, email, number):
        borders = {
            "tmargin": "0.6in",
            "rmargin": "0.75in",
            "lmargin": "0.75in",
            "bmargin": "0.6in"
        }
        self.doc = Document(documentclass="resume", geometry_options=borders)
        self.doc.append(Command("begin", "flushleft"))
        self.addCustomCommands()
        self.addTitle(name, email, number)

    def addCustomCommands(self):
        EduEntry = UnsafeCommand('newcommand',
                                 r'\EducationEntry',
                                 options=1,
                                 extra_arguments=r"""
                            {\bf California Polytechnic State University}
                            \hfill
                            {\em September {#1} - Present}
                        """)

        BoldHeading = UnsafeCommand('newcommand',
                                    r'\BoldHeading',
                                    options=1,
                                    extra_arguments=r"""
                            {\bf{#1}}
                        """)

        FirstDatedEntry = UnsafeCommand('newcommand',
                                        r'\FirstDatedEntry',
                                        options=2,
                                        extra_arguments=r"""
                            \text{#1} 
                            \hfill
                            {\em{#2}}
                        """)

        DatedEntry = UnsafeCommand('newcommand',
                                   r'\DatedEntry',
                                   options=2,
                                   extra_arguments=r"""
                            \item{#1} 
                            \hfill
                            {\em{#2}}
                        """)

        self.doc.append(EduEntry)
        self.doc.append(BoldHeading)
        self.doc.append(DatedEntry)
        self.doc.append(FirstDatedEntry)

    def addTitle(self, student_name, email, number):
        self.doc.preamble.append(Command("name", student_name))
        self.doc.preamble.append(Command("address", f"{number} | {email}"))

    def addEducation(self, year, grad, major):
        self.doc.append(Command("begin", ["rSection", "Education"]))
        self.doc.append(Command("EducationEntry", year))
        self.doc.append(LineBreak())
        self.doc.append("Bachelor of Science in " + major)
        self.doc.append(LineBreak())
        self.doc.append("Projected graduation date: June " + grad)
        self.doc.append(Command("end", "rSection"))

    """ Projects must be given as a list of tuples in the format (name, description) """

    def addProjects(self, projects):
        self.doc.append(Command("begin", ["rSection", "Projects"]))
        for i in range(len(projects)):
            self.doc.append(Command("BoldHeading", projects[i][0]))
            self.doc.append(LineBreak())
            self.doc.append(projects[i][1])

            if i < len(projects) - 1:
                self.doc.append(LineBreak())
                self.doc.append(LineBreak())

        self.doc.append(Command("end", "rSection"))

    """ Projects must be given as a list of tuples in the format (name, date, position, description) """

    def addExperience(self, jobs):
        self.doc.append(Command("begin", ["rSection", "Work Experience"]))
        for job in jobs:
            self.doc.append(
                Command("begin",
                        ["rSubsection", job[0],
                         italic(job[1]), job[2], '']))
            self.doc.append(Command("item", job[3]))

        self.doc.append(Command("end", "rSubsection"))
        self.doc.append(Command("end", "rSection"))

    """ Type should be either 'Techincal' or 'Soft' """

    def addSkills(self, type, skills):
        self.doc.append(Command("begin", ["rSection", type + " Skills"]))
        self.doc.append(", ".join(skills))
        self.doc.append(Command("end", "rSection"))

    """ Extracurriculars must be given as a list of tuples in the format (name, date) """

    def addExtracurriculars(self, activities):
        self.doc.append(
            Command("begin", ["rSection", "Extracurricular Activities"]))
        self.doc.append(Command("FirstDatedEntry", activities[0]))
        for act in activities[1:]:
            self.doc.append(Command("DatedEntry", act))
        self.doc.append(Command("end", "rSection"))

    def addRelevantCoursework(self, classes):
        self.doc.append(Command("begin", ["rSection", "Relevant Coursework"]))
        self.doc.append(Command("text", classes[0]))
        for c in classes[1:]:
            self.doc.append(Command("item", c))
        self.doc.append(Command("end", "rSection"))

    def finish(self):
        self.doc.append(Command("end", "flushleft"))
示例#38
0
                                                         cv=folds,
                                                         n_jobs=-1)
            scores.append(accsespf1g(testpredict, testtarget))
            print(str(clf))
            print_scores(testpredict, testtarget)
        # usrednanie wynikow
        avgscores = avgaccsespf1g(scores)
        to_decimal = print_to_latex_two_decimal(avgscores)
        for i, score in enumerate(to_decimal):
            rows[i].append(score)
    for table, row in zip(tables, rows):
        print(row)
        max_v = max(row[1:])
        new_row = []

        for item in row:
            if item == max_v:
                new_row.append(bold(max_v))
            else:
                new_row.append(item)
        table.add_row(new_row)

# zapis do pliku
doc = Document("adaboost_NB")
for i, tab, in enumerate(tables):
    section = Section(sections[i])
    section.append(tab)
    doc.append(section)
doc.generate_tex(os.path.join(path, 'wyniki/pliki_tex/'))
doc.generate_pdf(os.path.join(path, 'wyniki/pliki_pdf/'))
示例#39
0
def details():
    if __name__ == '__main__':


        image_filename = os.path.join(os.path.dirname(__file__), 'kitten.jpg')
        logo_file = os.path.join(os.path.dirname(__file__),'sample-logo.png')

        geometry_options = {"tmargin": "1cm", "lmargin": "3cm"}
        doc = Document(geometry_options=geometry_options)
        header = PageStyle("header")
        with header.create(Head("R")):
            header.append(simple_page_number())
        with header.create(Foot("C")):
            header.append("Center Footer")
        first_page = PageStyle("firstpage")
        with first_page.create(Head("L")) as header_left:
        with header_left.create(MiniPage(width=NoEscape(r"0.49\textwidth"),pos='c')) as logo_wrapper

        with doc.create(MiniPage(align='l')):
            doc.append(LargeText(bold("INVESTMENT PROPERTY - BUY & HOLD")))
            doc.append(LineBreak())
            doc.append(MediumText(bold(" ")))
        logo_wrapper.append(StandAloneGraphic(image_options="width=120px",
                                              filename=logo_file))
        with doc.create(Section('Home Adress')):
            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))

        a = np.array([[100, 10, 20]]).T
        M = np.matrix([[2, 3, 4],
                       [0, 0, 1],
                       [0, 0, 2]])

        with doc.create(Section('The fancy stuff')):
            with doc.create(Subsection('Correct matrix equations')):
                doc.append(Math(data=[Matrix(M), Matrix(a), '=', Matrix(M * a)]))

            with doc.create(Subsection('Alignat math environment')):
                with doc.create(Alignat(numbering=False, escape=False)) as agn:
                    agn.append(r'\frac{a}{b} &= 0 \\')
                    agn.extend([Matrix(M), Matrix(a), '&=', Matrix(M * a)])

            with doc.create(Subsection('Beautiful graphs')):
                with doc.create(TikZ()):
                    plot_options = 'height=4cm, width=6cm, grid=major'
                    with doc.create(Axis(options=plot_options)) as plot:
                        plot.append(Plot(name='model', func='-x^5 - 242'))

                        coordinates = [
                            (-4.77778, 2027.60977),
                            (-3.55556, 347.84069),
                            (-2.33333, 22.58953),
                            (-1.11111, -493.50066),
                            (0.11111, 46.66082),
                            (1.33333, -205.56286),
                            (2.55556, -341.40638),
                            (3.77778, -1169.24780),
                            (5.00000, -3269.56775),
                        ]

                        plot.append(Plot(name='estimate', coordinates=coordinates))

            with doc.create(Subsection('Cute kitten pictures')):
                with doc.create(Figure(position='h!')) as kitten_pic:
                    kitten_pic.add_image(image_filename, width='120px')
                    kitten_pic.add_caption('Look it\'s on its back')

        doc.generate_pdf('full', clean_tex=False)
示例#40
0
def generate_header():
    geometry_options = {"margin": "0.7in"}
    doc = Document(geometry_options=geometry_options)
    # Add document header
    header = PageStyle("header")
    # Create left header
    with header.create(Head("L")):
        header.append("__")
        header.append(LineBreak())
        header.append("_")
    # Create center header
    with header.create(Head("C")):
        header.append("____")
    # Create right header
    with header.create(Head("R")):
        header.append(simple_page_number())
    # Create left footer
    with header.create(Foot("L")):
        header.append("Left Footer")
    # Create center footer
    with header.create(Foot("C")):
        header.append("Center Footer")
    # Create right footer
    with header.create(Foot("R")):
        header.append("Right Footer")

    doc.preamble.append(header)
    doc.change_document_style("header")

    # Add Heading
    with doc.create(MiniPage(align='l')):
        doc.append(LargeText(bold("INVESTMENT PROPERTY - BUY & HOLD")))
        doc.append(LineBreak())
        doc.append(MediumText(bold(" ")))
    doc.generate_pdf("header", clean_tex=False)

    return print('nice')
示例#41
0
def get_document(document_title='Report',
                 author='Entail AS',
                 fig_ext=u'.pdf',
                 header_logofilename='entail.pdf',
                 logo_image_option_header="width=250px",
                 workflow_ID=0):
    global _fig_ext
    _fig_ext = fig_ext

    geometry_options = {
        "head": "70pt",
        "margin": "1.5cm",
        "bottom": "1.5cm",
        "includeheadfoot": True
    }
    document_options = ['a4paper']
    doc = Document(geometry_options=geometry_options,
                   document_options=document_options)

    # packages
    doc.packages.append(Package('booktabs'))
    doc.packages.append(Package('chngcntr'))
    doc.packages.append(Package('longtable'))
    doc.packages.append(Package('titlepic'))
    doc.packages.append(Package('float'))
    # page style
    first_page = PageStyle("firstpage")

    # Header image
    with first_page.create(Head("L")) as header_left:
        with header_left.create(
                MiniPage(width=NoEscape(r"0.25\textwidth"),
                         pos='c')) as logo_wrapper:
            logo_file = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                header_logofilename).replace('\\', '/')
            logo_wrapper.append(
                StandAloneGraphic(image_options="width=80px",
                                  filename=logo_file))

    # Add document title
    with first_page.create(Head("R")) as right_header:
        with right_header.create(
                MiniPage(width=NoEscape(r"0.75\textwidth"), pos='c',
                         align='r')) as title_wrapper:
            title_wrapper.append(LargeText(bold(document_title)))
            title_wrapper.append(LineBreak())
            title_wrapper.append(MediumText(bold(NoEscape(r'\today'))))

    # Add footer
    with first_page.create(Foot("C")) as center_footer:
        center_footer.append(simple_page_number())
    with first_page.create(Foot("R")) as right_footer:
        with right_footer.create(
                MiniPage(width=NoEscape(r"0.15\textwidth"),
                         pos='r')) as logo_wrapper:
            logo_file = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'tailor.png').replace('\\', '/')
            logo_wrapper.append(
                StandAloneGraphic(image_options="width=50px",
                                  filename=logo_file))

    doc.preamble.append(first_page)
    doc.change_document_style("firstpage")
    doc.preamble.append(Command('title', document_title))
    logo_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             header_logofilename).replace('\\', '/')
    doc.preamble.append(
        Command(
            'titlepic',
            StandAloneGraphic(image_options=logo_image_option_header,
                              filename=logo_file)))
    # doc.preamble.append(Command('author', author))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.preamble.append(
        Command(
            'hypersetup',
            arguments=
            'colorlinks, citecolor=black, filecolor=black, linkcolor=black, urlcolor=black',
            packages=[Package('hyperref')]))
    doc.preamble.append(NoEscape(r'\counterwithin{figure}{section}'))
    doc.append(NoEscape(r'\maketitle'))

    # Add disclaimer
    doc.append(NewPage())
    doc.append(NoEscape(r'\textbf{Disclaimer}'))
    doc.append(NoEscape(r'\newline'))
    doc.append(NoEscape(r'\vspace{0.2in}'))

    temptext = f'This is in automatically generated report. '
    if workflow_ID == 0:
        temptext += f'The report was generated '
    else:
        temptext += f'The results are extracted from Workflow ID {workflow_ID} '
    temptext += f'on the {datetime.datetime.now().strftime("%m/%d/%Y at %H:%M:%S")}. '
    temptext += f'Errors may occur and it is the user’s responsibility to interpret the reported data '
    temptext += f'with sound engineering judgement.'
    doc.append(temptext)

    doc.append(NewPage())
    doc.append(Command('tableofcontents'))
    doc.append(NewPage())

    return doc
示例#42
0
def generate_docs(config):
    """
        Function used to generate two pdf:

        -results.pdf: which shows all statistics about each formula with each
         benchmarked method.

        -summary.pdf: which count the number of times that each method is
         better than another.
    """
    # Let's create the documents (result & summary)
    doc = Document(documentclass='standalone')
    doc.packages.append(Package('amsmath'))
    doc.packages.append(Package('color'))
    doc.packages.append(Package('colortbl'))
    doc2 = Document(documentclass='standalone')

    # Declare colors in result document
    doc.append(
        DefineColor(arguments=Arguments('Gray', 'rgb', '0.7, 0.7, 0.7')))
    doc.append(DefineColor(arguments=Arguments('Green', 'rgb', '0.4, 1, 0.4')))
    doc.append(DefineColor(arguments=Arguments('Red', 'rgb', '0.8, 0, 0')))
    doc.append(DefineColor(arguments=Arguments('Yelw', 'rgb', '1, 0.98, 0.4')))
    doc.append(DefineColor(arguments=Arguments('Purpl', 'rgb', '1, 0.6, 1')))

    # Create Table with format : True is result format, False is summary format
    table = Tabular(add_fmt(len(config.l), True))
    table2 = Tabular(add_fmt(len(config.l), False))

    # Write everything
    write_results(table, config)
    write_summary(table2, config)

    # Output PDF
    doc.append(table)
    doc2.append(table2)
    doc.generate_pdf('results')
    doc2.generate_pdf('summary')
示例#43
0
    def generate_latex(self):
        doc = Document('ImageLatexResult')
        doc.content_separator = '\n'

        for i in range(len(self.lines)):

            if self.lines[i].type == "large":
                doc.append("\Huge")
            elif self.lines[i].type == "medium":
                doc.append("\LARGE")
            elif self.lines[i].type == "small":
                doc.append("\large")

            if self.lines[i].is_centered:
                doc.append(NoEscape(r'\centerline'))
            elif self.lines[i].is_indented:
                doc.append("\t")

            doc.append(NoEscape(r'{'))
            doc.content_separator = ''
            for k in xrange(0, len(self.lines[i].words)):
                doc.append(self.lines[i].words[k].word)
            doc.content_separator = '\n'
            doc.append(NoEscape(r'}'))
            doc.append(NoEscape("\n"))

            if self.lines[i].is_end_paragraph:
                doc.append(NoEscape(r'\n'))
                doc.append(NoEscape(r'\n'))
                doc.append(NoEscape(r'\n'))

            #if self.lines[i].is_bold:
            #	for k in xrange(0, len(self.lines[i].words)):
            #		doc.append(bold(self.lines[i].words[k].word))
            #	doc.append("\n")

        return doc.dumps()
示例#44
0
def generate_report(tests, program, is_loopback, calib_program, dut_host):
    geometry_options = {"right": "2cm", "left": "2cm"}
    fname = program.split('/')[-1] + '-tests'
    try:
        os.remove(fname + '.pdf')
    except OSError:
        pass
    
    doc = Document(fname, geometry_options=geometry_options)

    doc.append('Test results for %s\n' % program)
    doc.append('Calibration method: %s\n' % "loopback" if is_loopback else calib_program)
    doc.append('DUT host: %s\n' % dut_host)

    sorted_tests = sorted(tests, key = lambda test: len(test.expected_path))

    for i in range(len(sorted_tests)):
        times = sorted(tests[i].packets[0].times)
        runs = range(len(times))

	plt.figure(figsize=(16,5))
        plt.bar(runs, times)
	plt.xlim(runs[0], runs[-1] + 1)
        plt.xlabel('Run #')
        plt.ylabel('Time - calibration (microseconds) ')
        plt.title('Test times')
        with doc.create(Section('Test %d' % (i+1))):
	    with doc.create(Subsection('Expected path')):
	        for point in tests[i].expected_path:
	    	    if 'tbl_act' in point:
	    	        continue
	    	    doc.append(str(point) + '\n')

	    with doc.create(Subsection('Stats (time - calibration time in microseconds)')):
	        doc.append('Mean: %f\n' % np.mean(times))
	        doc.append('Std dev: %f\n' % np.std(times))
	        doc.append('Median: %f\n' % np.median(times))

            with doc.create(Figure(position='htbp')) as plot:
                plot.add_plot(width=NoEscape(r'1\textwidth'))

        plt.clf()
	plt.close()
	gc.collect()

    doc.generate_pdf(clean_tex=True)
示例#45
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Test for list structures in PyLaTeX.
# More info @ http://en.wikibooks.org/wiki/LaTeX/List_Structures

from pylatex import Document, Section, Itemize, Enumerate, Description

doc = Document()

# create a bulleted "itemize" list like the below:
# \begin{itemize}
#   \item The first item
#   \item The second item
#   \item The third etc \ldots
# \end{itemize}

with doc.create(Section('"Itemize" list')):
    with doc.create(Itemize()) as itemize:
        itemize.add_item("the first item")
        itemize.add_item("the second item")
        itemize.add_item("the third etc")
        itemize.append("\\ldots")  # you can append to existing items

# create a numbered "enumerate" list like the below:
# \begin{enumerate}
#   \item The first item
#   \item The second item
#   \item The third etc \ldots
# \end{enumerate}
示例#46
0
parser = argparse.ArgumentParser()
parser.add_argument("datafile", help="the file used as a data source")
parser.add_argument("-l",
                    action="store_true",
                    help="display LaTeX source generated from data")
parser.add_argument("--outdir", default="./", help="output directory")
parser.add_argument("--noinc",
                    action="store_true",
                    help="do not increment revision number")
prog_args = parser.parse_args()

with open(prog_args.datafile) as f:
    portfolio = yaml.safe_load(f)

doc = Document(documentclass='Resume')

with doc.create(Tabular("l")) as ntab:
    ntab.add_row([Command("Huge", Command("textbf", portfolio["name"]))])
    ntab.add_row([Command("Large", portfolio["title"])])

doc.append(Command("hfill"))
with doc.create(Tabular("rl")) as ctab:
    contact_handles = portfolio["contact"]
    for handle in contact_handles:
        ctab.add_row([
            handle["name"] + ": ",
            TexList([
                Command("hspace", "5pt"),
                Command("href", [handle["url"], handle["handle"]])
            ])
示例#47
0
def generate_header():
    geometry_options = {"margin": "0.7in"}
    doc = Document(geometry_options=geometry_options)
    # Add document header
    header = PageStyle("header")
    # Create left header
    with header.create(Head("L")):
        header.append("Page date: ")
        header.append(LineBreak())
        header.append("R3")
    # Create center header
    with header.create(Head("C")):
        header.append("Company")
    # Create right header
    with header.create(Head("R")):
        header.append(simple_page_number())
    # Create left footer
    with header.create(Foot("L")):
        header.append("Left Footer")
    # Create center footer
    with header.create(Foot("C")):
        header.append("Center Footer")
    # Create right footer
    with header.create(Foot("R")):
        header.append("Right Footer")

    doc.preamble.append(header)
    doc.change_document_style("header")

    # Add Heading
    with doc.create(MiniPage(align='c')):
        doc.append(LargeText(bold("Title")))
        doc.append(LineBreak())
        doc.append(MediumText(bold("As at:")))

    doc.generate_pdf("header", clean_tex=False)
示例#48
0
def _add_keyboard_notes(document: pylatex.Document, texts: dict,
                        images: dict) -> None:
    document.append(
        pylatex.Subsection(
            title=texts["keyboard"]["title"],
            label=False,
            numbering=False,
        ))

    document.append(texts["keyboard"]["text0"])
    document.append(pylatex.NoEscape(r"\vspace{5mm}"))

    table = pylatex.Tabular(r" l | l | p{9cm} ")

    # table.add_hline()
    table.add_row(*tuple(
        pylatex.MediumText(column_title) for column_title in (
            "Bereich",
            "Beschreibung der Klänge",
            "verwendete Lautsprecher",
        )))
    for zone_idx in range(3):
        table.add_hline()
        table.add_row(
            _make_img(images["zone_{}".format(zone_idx)],
                      width=0.22,
                      add_figure=False),
            texts["keyboard"]["zone{}sound".format(zone_idx)],
            texts["keyboard"]["zone{}speaker".format(zone_idx)],
        )

    # table.add_hline()

    document.append(pylatex.Table(data=table, position="h!"))
    document.append(texts["keyboard"]["text1"])
    # document.append(pylatex.NoEscape(r"\vspace{3mm}"))
    document.append(texts["keyboard"]["text2"])
示例#49
0
def generate_unique():
    geometry_options = {
        "head": "40pt",
        "margin": "0.5in",
        "bottom": "0.6in",
        "includeheadfoot": True
    }
    doc = Document(geometry_options=geometry_options)

    # Generating first page style
    first_page = PageStyle("firstpage")

    # Header image
    with first_page.create(Head("L")) as header_left:
        with header_left.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"),
                         pos='c')) as logo_wrapper:
            logo_file = os.path.join(os.path.dirname(__file__),
                                     'sample-logo.jpg')
            logo_wrapper.append(
                StandAloneGraphic(image_options="width=120px",
                                  filename=logo_file))

    # Add document title
    with first_page.create(Head("R")) as right_header:
        with right_header.create(
                MiniPage(width=NoEscape(r"0.49\textwidth"), pos='c',
                         align='r')) as title_wrapper:
            title_wrapper.append(LargeText(bold("Bank Account Statement")))
            title_wrapper.append(LineBreak())
            title_wrapper.append(MediumText(bold("Date")))

    # Add footer
    with first_page.create(Foot("C")) as footer:
        message = "Important message please read"
        with footer.create(
                Tabularx(
                    "X X X X",
                    width_argument=NoEscape(r"\textwidth"))) as footer_table:

            footer_table.add_row(
                [MultiColumn(4, align='l', data=TextColor("blue", message))])
            footer_table.add_hline(color="blue")
            footer_table.add_empty_row()

            branch_address = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                      pos='t')
            branch_address.append("960 - 22nd street east")
            branch_address.append("\n")
            branch_address.append("Saskatoon, SK")

            document_details = MiniPage(width=NoEscape(r"0.25\textwidth"),
                                        pos='t',
                                        align='r')
            document_details.append("1000")
            document_details.append(LineBreak())
            document_details.append(simple_page_number())

            footer_table.add_row([
                branch_address, branch_address, branch_address,
                document_details
            ])

    doc.preamble.append(first_page)
    # End first page style

    # Add customer information
    with doc.create(Tabu("X[l] X[r]")) as first_page_table:
        customer = MiniPage(width=NoEscape(r"0.49\textwidth"), pos='h')
        customer.append("Verna Volcano")
        customer.append("\n")
        customer.append("For some Person")
        customer.append("\n")
        customer.append("Address1")
        customer.append("\n")
        customer.append("Address2")
        customer.append("\n")
        customer.append("Address3")

        # Add branch information
        branch = MiniPage(width=NoEscape(r"0.49\textwidth"),
                          pos='t!',
                          align='r')
        branch.append("Branch no.")
        branch.append(LineBreak())
        branch.append(bold("1181..."))
        branch.append(LineBreak())
        branch.append(bold("TIB Cheque"))

        first_page_table.add_row([customer, branch])
        first_page_table.add_empty_row()

    doc.change_document_style("firstpage")
    doc.add_color(name="lightgray", model="gray", description="0.80")

    # Add statement table
    with doc.create(LongTabu("X[l] X[2l] X[r] X[r] X[r]",
                             row_height=1.5)) as data_table:
        data_table.add_row(
            ["date", "description", "debits($)", "credits($)", "balance($)"],
            mapper=bold,
            color="lightgray")
        data_table.add_empty_row()
        data_table.add_hline()
        row = ["2016-JUN-01", "Test", "$100", "$1000", "-$900"]
        for i in range(30):
            if (i % 2) == 0:
                data_table.add_row(row, color="lightgray")
            else:
                data_table.add_row(row)

    doc.append(NewPage())

    # Add cheque images
    with doc.create(LongTabu("X[c] X[c]")) as cheque_table:
        cheque_file = os.path.join(os.path.dirname(__file__), 'programmer.jpg')
        cheque = StandAloneGraphic(cheque_file, image_options="width=200px")
        for i in range(0, 20):
            cheque_table.add_row([cheque, cheque])

    doc.generate_pdf("complex_report", clean_tex=False)
import os
import sys
from read_results import read_results, chunks
from pylatex import Document, LongTable, NoEscape, Command, MultiColumn, Section, Subsection
from pylatex.base_classes.command import Options

geometry_options = {"margin": "0.5cm", "includeheadfoot": True}
doc = Document(page_numbers=True, geometry_options=geometry_options)
doc.preamble.append(NoEscape(r'\usepackage[czech]{babel}'))
doc.preamble.append(NoEscape(r'\usepackage{threeparttablex}'))
doc.preamble.append(NoEscape(r'\usepackage{pdfpages}'))
doc.preamble.append(NoEscape(r'\definecolor{Hex}{RGB}{239,239,239}'))

doc.documentclass.options = Options('10pt')


def d_generator(data_dir, vystup_dir):
    os.chdir(data_dir)
    data_PU = read_results('results.csv')
    info_PU = read_results('raw_data_info.csv')

    # Arrange data to desired shape
    data_check = []
    data_used = []
    data_replace_p = []
    data_check_p = []
    data_replace_a = []
    l_names = []
    p_stale = []
    for i in range(0, len(data_PU)):
        data_used.append(data_PU[i][0])
示例#51
0
def fill_document(doc):
    """Add a section, a subsection and some text to the document.
    :param doc: the document
    :type doc: :class:`pylatex.document.Document` instance
    """
    with doc.create(Section('A section')):
        doc.append('Some regular text and some ')
        doc.append(italic('italic text. '))

        with doc.create(Subsection('A subsection')):
            doc.append('Also some crazy characters: $&#{}')


if __name__ == '__main__':
    # Basic document
    doc = Document('basic')
    fill_document(doc)

    doc.generate_pdf(clean_tex=False)
    doc.generate_tex()

    # Document with `\maketitle` command activated
    doc = Document()

    doc.preamble.append(Command('title', 'Awesome Title'))
    doc.preamble.append(Command('author', 'Anonymous author'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

    fill_document(doc)
示例#52
0

class ExampleCommand(CommandBase):
    """
    A class representing a custom LaTeX command.

    This class represents a custom LaTeX command named
    ``exampleCommand``.
    """

    _latex_name = 'exampleCommand'
    packages = [Package('color')]


# Create a new document
doc = Document()
with doc.create(Section('Custom commands')):
    doc.append(
        NoEscape(r"""
        The following is a demonstration of a custom \LaTeX{}
        command with a couple of parameters.
        """))

    # Define the new command
    new_comm = UnsafeCommand('newcommand',
                             '\exampleCommand',
                             options=3,
                             extra_arguments=r'\color{#1} #2 #3 \color{black}')
    doc.append(new_comm)

    # Use our newly created command with different arguments
    def save_latex(self,
                   uiObj,
                   Design_Check,
                   reportsummary,
                   filename,
                   rel_path,
                   Disp_2d_image,
                   Disp_3d_image,
                   module=''):
        companyname = str(reportsummary["ProfileSummary"]['CompanyName'])
        companylogo = str(reportsummary["ProfileSummary"]['CompanyLogo'])
        groupteamname = str(reportsummary["ProfileSummary"]['Group/TeamName'])
        designer = str(reportsummary["ProfileSummary"]['Designer'])
        projecttitle = str(reportsummary['ProjectTitle'])
        subtitle = str(reportsummary['Subtitle'])
        jobnumber = str(reportsummary['JobNumber'])
        client = str(reportsummary['Client'])

        does_design_exist = reportsummary['does_design_exist']
        osdagheader = '/ResourceFiles/images/Osdag_header_report.png'
        # Add document header
        geometry_options = {
            "top": "5cm",
            "hmargin": "2cm",
            "headheight": "100pt",
            "footskip": "100pt",
            "bottom": "5cm"
        }
        doc = Document(geometry_options=geometry_options, indent=False)
        doc.packages.append(Package('amsmath'))
        doc.packages.append(Package('graphicx'))
        doc.packages.append(Package('needspace'))
        doc.append(pyl.Command('fontsize', arguments=[8, 12]))
        doc.append(pyl.Command('selectfont'))

        doc.add_color('OsdagGreen', 'RGB', '153,169,36')
        doc.add_color('PassColor', 'RGB', '153,169,36')
        doc.add_color('Red', 'RGB', '255,0,0')
        doc.add_color('Green', 'RGB', '0,200,0')
        doc.add_color('FailColor', 'HTML', '933A16')
        header = PageStyle("header")
        # Create center header
        with header.create(Head("C")):
            with header.create(Tabularx('|l|p{4cm}|l|X|')) as table:
                table.add_hline()
                # MultiColumn(4)
                table.add_row((
                    MultiColumn(
                        2,
                        align='|c|',
                        data=('' if companylogo is '' else StandAloneGraphic(
                            image_options="height=0.95cm",
                            filename=companylogo))),
                    MultiColumn(2,
                                align='|c|',
                                data=[
                                    'Created with',
                                    StandAloneGraphic(
                                        image_options="width=4.0cm,height=1cm",
                                        filename=rel_path + osdagheader)
                                ]),
                ))
                table.add_hline()
                table.add_row(('Company Name', companyname, 'Project Title',
                               projecttitle),
                              color='OsdagGreen')
                table.add_hline()
                table.add_row(
                    ('Group/Team Name', groupteamname, 'Subtitle', subtitle),
                    color='OsdagGreen')
                table.add_hline()
                table.add_row(('Designer', designer, 'Job Number', jobnumber),
                              color='OsdagGreen')
                table.add_hline()
                table.add_row(
                    ('Date', time.strftime("%d /%m /%Y"), 'Client', client),
                    color='OsdagGreen')
                table.add_hline()

        # Create right footer
        with header.create(Foot("R")):
            header.append(NoEscape(r'Page \thepage'))
        #
        # doc.preamble.append(header)
        # doc.change_document_style("header")

        # Add Heading
        # with doc.create(MiniPage(align='c')):

        doc.preamble.append(header)
        doc.change_document_style("header")
        with doc.create(Section('Input Parameters')):
            with doc.create(
                    LongTable('|p{5cm}|p{2.5cm}|p{1.5cm}|p{3cm}|p{3.5cm}|',
                              row_height=1.2)) as table:
                table.add_hline()
                for i in uiObj:
                    # row_cells = ('9', MultiColumn(3, align='|c|', data='Multicolumn not on left'))
                    if i == "Selected Section Details" or i == KEY_DISP_ANGLE_LIST or i == KEY_DISP_TOPANGLE_LIST or i == KEY_DISP_CLEAT_ANGLE_LIST:
                        # if type(uiObj[i]) == list:
                        continue
                    if type(uiObj[i]) == dict:
                        table.add_hline()
                        sectiondetails = uiObj[i]
                        image_name = sectiondetails[KEY_DISP_SEC_PROFILE]

                        Img_path = '/ResourceFiles/images/' + image_name + '.png'
                        if (len(sectiondetails)) % 2 == 0:
                            # merge_rows = int(round_up(len(sectiondetails),2)/2 + 2)
                            merge_rows = int((len(sectiondetails) / 2)) + 2
                        else:
                            merge_rows = round_up((len(sectiondetails) / 2), 2)
                        if (len(sectiondetails)) % 2 == 0:
                            sectiondetails[''] = ''

                        a = list(sectiondetails.keys())
                        # index=0
                        for x in range(1, (merge_rows + 1)):
                            # table.add_row("Col.Det.",i,columndetails[i])
                            if x == 1:
                                table.add_row((
                                    MultiRow(
                                        merge_rows,
                                        data=StandAloneGraphic(
                                            image_options=
                                            "width=5cm,height=5cm",
                                            filename=rel_path + Img_path)),
                                    MultiColumn(2, align='|c|', data=a[x]),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=sectiondetails[a[x]]),
                                ))
                            elif x <= 4:
                                table.add_row((
                                    '',
                                    MultiColumn(2,
                                                align='|c|',
                                                data=NoEscape(a[x])),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=NoEscape(
                                                    sectiondetails[a[x]])),
                                ))
                            else:
                                table.add_row((
                                    '',
                                    NoEscape(a[x]),
                                    sectiondetails[a[x]],
                                    NoEscape(a[merge_rows + x - 4]),
                                    sectiondetails[a[merge_rows + x - 4]],
                                ))
                            table.add_hline(2, 5)
                    elif uiObj[i] == "TITLE":
                        table.add_hline()
                        table.add_row((MultiColumn(
                            5,
                            align='|c|',
                            data=bold(i),
                        ), ))
                        table.add_hline()
                    elif i == 'Section Size*':
                        table.add_hline()
                        table.add_row((
                            MultiColumn(
                                3,
                                align='|c|',
                                data=i,
                            ),
                            MultiColumn(2,
                                        align='|c|',
                                        data="Ref List of Input Section"),
                        ))
                        table.add_hline()
                    elif len(str(uiObj[i])) > 55 and type(
                            uiObj[i]) != pyl.math.Math:
                        str_len = len(str(uiObj[i]))
                        loop_len = round_up((str_len / 55), 1, 1)
                        for j in range(1, loop_len + 1):
                            b = 55 * j + 1
                            if j == 1:
                                table.add_row((
                                    MultiColumn(3,
                                                align='|c|',
                                                data=MultiRow(loop_len,
                                                              data=i)),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=uiObj[i][0:b]),
                                ))
                            else:
                                table.add_row((
                                    MultiColumn(3,
                                                align='|c|',
                                                data=MultiRow(loop_len,
                                                              data="")),
                                    MultiColumn(2,
                                                align='|c|',
                                                data=uiObj[i][b - 55:b]),
                                ))
                        table.add_hline()
                    else:
                        table.add_hline()
                        table.add_row((
                            MultiColumn(3, align='|c|', data=NoEscape(i)),
                            MultiColumn(2, align='|c|', data=uiObj[i]),
                        ))
                        table.add_hline()
            for i in uiObj:
                if i == 'Section Size*' or i == KEY_DISP_ANGLE_LIST or i == KEY_DISP_TOPANGLE_LIST or i == KEY_DISP_CLEAT_ANGLE_LIST:
                    with doc.create(Subsection("List of Input Section")):
                        # with doc.create(LongTable('|p{8cm}|p{8cm}|', row_height=1.2)) as table:
                        with doc.create(Tabularx('|p{4cm}|X|',
                                                 row_height=1.2)) as table:
                            table.add_hline()
                            table.add_row((
                                MultiColumn(
                                    1,
                                    align='|c|',
                                    data=i,
                                ),
                                MultiColumn(1,
                                            align='|X|',
                                            data=uiObj[i].strip("[]")),
                            ))
                            # str_len = len(uiObj[i])
                            # loop_len = round_up((str_len/100),1,1)
                            # table.add_hline()
                            # for j in range(1,loop_len+1):
                            #     b= 100*j+1
                            #     if j ==1:
                            #         table.add_row((MultiColumn(1, align='|c|', data=i, ),
                            #                        MultiColumn(1, align='|X|', data=uiObj[i][0:b]),))
                            #     else:
                            #         table.add_row((MultiColumn(1, align='|c|', data=" ", ),
                            #                        MultiColumn(1, align='|X|', data=uiObj[i][b-100:b]),))
                            table.add_hline()

        doc.append(
            pyl.Command('Needspace', arguments=NoEscape(r'10\baselineskip')))
        doc.append(NewPage())
        count = 0
        with doc.create(Section('Design Checks')):
            with doc.create(
                    Tabularx(
                        r'|>{\centering}p{12.5cm}|>{\centering\arraybackslash}X|',
                        row_height=1.2)) as table:
                table.add_hline()
                # Fail = TextColor("FailColor", bold("Fail"))
                # Pass = TextColor("PassColor", bold("Pass"))

                if does_design_exist != True:
                    table.add_row(bold('Design Status'),
                                  color_cell("Red", bold("Fail")))
                else:
                    table.add_row(bold('Design Status'),
                                  color_cell("OsdagGreen", bold("Pass")))
                table.add_hline()

            for check in Design_Check:

                if check[0] == 'SubSection':
                    if count >= 1:
                        # doc.append(NewPage())
                        doc.append(
                            pyl.Command(
                                'Needspace',
                                arguments=NoEscape(r'10\baselineskip')))
                    with doc.create(Subsection(check[1])):
                        #########################
                        # if uiObj== "WELDImage":
                        #     table.add_hline()
                        #     table.add_row((MultiColumn(5, align='|c|', data=bold(i), ),))
                        #     table.add_hline()
                        # else:
                        #########################
                        with doc.create(LongTable(check[2], row_height=1.2)
                                        ) as table:  # todo anjali remove
                            table.add_hline()
                            table.add_row(
                                ('Check', 'Required', 'Provided', 'Remarks'),
                                color='OsdagGreen')
                            table.add_hline()
                            table.end_table_header()
                            table.add_hline()
                            count = count + 1
                elif check[0] == "Selected":
                    if count >= 1:
                        # doc.append(NewPage())
                        doc.append(
                            pyl.Command(
                                'Needspace',
                                arguments=NoEscape(r'10\baselineskip')))
                    with doc.create(Subsection(check[1])):
                        with doc.create(LongTable(check[2],
                                                  row_height=1.2)) as table:
                            table.add_hline()
                            for i in uiObj:
                                # row_cells = ('9', MultiColumn(3, align='|c|', data='Multicolumn not on left'))

                                print(i)
                                if type(
                                        uiObj[i]
                                ) == dict and i == 'Selected Section Details':
                                    table.add_hline()
                                    sectiondetails = uiObj[i]
                                    image_name = sectiondetails[
                                        KEY_DISP_SEC_PROFILE]
                                    Img_path = '/ResourceFiles/images/' + image_name + '.png'
                                    if (len(sectiondetails)) % 2 == 0:
                                        # merge_rows = int(round_up(len(sectiondetails),2)/2 + 2)
                                        merge_rows = int(
                                            round_up((len(sectiondetails) /
                                                      2), 1, 0) + 2)
                                    else:
                                        merge_rows = int(
                                            round_up((len(sectiondetails) /
                                                      2), 1, 0) + 1)
                                    print('Hi',
                                          len(sectiondetails) / 2,
                                          round_up(len(sectiondetails), 2) / 2,
                                          merge_rows)
                                    if (len(sectiondetails)) % 2 == 0:
                                        sectiondetails[''] = ''
                                    a = list(sectiondetails.keys())
                                    # index=0
                                    for x in range(1, (merge_rows + 1)):
                                        # table.add_row("Col.Det.",i,columndetails[i])
                                        if x == 1:
                                            table.add_row((
                                                MultiRow(
                                                    merge_rows,
                                                    data=StandAloneGraphic(
                                                        image_options=
                                                        "width=5cm,height=5cm",
                                                        filename=rel_path +
                                                        Img_path)),
                                                MultiColumn(2,
                                                            align='|c|',
                                                            data=NoEscape(
                                                                a[x])),
                                                MultiColumn(
                                                    2,
                                                    align='|c|',
                                                    data=NoEscape(
                                                        sectiondetails[a[x]])),
                                            ))
                                        elif x <= 4:
                                            table.add_row((
                                                '',
                                                MultiColumn(2,
                                                            align='|c|',
                                                            data=NoEscape(
                                                                a[x])),
                                                MultiColumn(
                                                    2,
                                                    align='|c|',
                                                    data=sectiondetails[a[x]]),
                                            ))
                                        else:
                                            table.add_row((
                                                '',
                                                NoEscape(a[x]),
                                                sectiondetails[a[x]],
                                                NoEscape(a[merge_rows + x -
                                                           4]),
                                                sectiondetails[a[merge_rows +
                                                                 x - 4]],
                                            ))

                                        table.add_hline(2, 5)
                            table.add_hline()
                        count = count + 1
                else:

                    if check[3] == 'Fail':
                        table.add_row((NoEscape(check[0])), check[1], check[2],
                                      TextColor("Red", bold(check[3])))
                    else:
                        table.add_row((NoEscape(check[0])), check[1], check[2],
                                      TextColor("OsdagGreen", bold(check[3])))
                    table.add_hline()

        # 2D images
        if len(Disp_2d_image) != 0:

            if module == KEY_DISP_BCENDPLATE or module == KEY_DISP_BB_EP_SPLICE:
                if does_design_exist and sys.platform != 'darwin':
                    doc.append(NewPage())
                    weld_details = rel_path + Disp_2d_image[0]
                    detailing_details = rel_path + Disp_2d_image[1]
                    stiffener_details = rel_path + Disp_2d_image[2]

                    with doc.create(Section('2D Drawings (Typical)')):

                        with doc.create(Figure()) as image:
                            image.add_image(weld_details,
                                            width=NoEscape(r'0.7\textwidth'),
                                            placement=NoEscape(r'\centering'))
                            image.add_caption(
                                'Typical Weld Details -- Beam to End Plate Connection'
                            )
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_2:
                            image_2.add_image(
                                detailing_details,
                                width=NoEscape(r'0.7\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_2.add_caption('Typical Detailing')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_3:
                            image_3.add_image(
                                stiffener_details,
                                width=NoEscape(r'0.9\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_3.add_caption('Typical Stiffener Details')
                            # doc.append(NewPage())

            elif module == KEY_DISP_BASE_PLATE:
                if does_design_exist and sys.platform != 'darwin':
                    doc.append(NewPage())
                    bp_sketch = rel_path + Disp_2d_image[0]
                    bp_detailing = rel_path + Disp_2d_image[1]
                    bp_weld = rel_path + Disp_2d_image[2]
                    bp_anchor = rel_path + Disp_2d_image[3]
                    bp_key = rel_path + Disp_2d_image[4]

                    with doc.create(Section('2D Drawings (Typical)')):
                        with doc.create(Figure()) as image_1:
                            image_1.add_image(
                                bp_sketch,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_1.add_caption('Typical Base Plate Details')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_2:
                            image_2.add_image(
                                bp_detailing,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_2.add_caption('Typical Base Plate Detailing')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_3:
                            image_3.add_image(
                                bp_weld,
                                width=NoEscape(r'1.0\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_3.add_caption('Typical Weld Details')
                            # doc.append(NewPage())

                        with doc.create(Figure()) as image_4:
                            image_4.add_image(
                                bp_anchor,
                                width=NoEscape(r'0.5\textwidth'),
                                placement=NoEscape(r'\centering'))
                            image_4.add_caption('Typical Anchor Bolt Details')
                            # doc.append(NewPage())

                        if len(Disp_2d_image[-1]) > 0:
                            with doc.create(Figure()) as image_5:
                                image_5.add_image(
                                    bp_key,
                                    width=NoEscape(r'0.9\textwidth'),
                                    placement=NoEscape(r'\centering'))
                                image_5.add_caption(
                                    'Typical Shear Key Details')
                                # doc.append(NewPage())

        if does_design_exist and sys.platform != 'darwin':
            doc.append(NewPage())
            Disp_top_image = "/ResourceFiles/images/top.png"
            Disp_side_image = "/ResourceFiles/images/side.png"
            Disp_front_image = "/ResourceFiles/images/front.png"
            view_3dimg_path = rel_path + Disp_3d_image
            view_topimg_path = rel_path + Disp_top_image
            view_sideimg_path = rel_path + Disp_side_image
            view_frontimg_path = rel_path + Disp_front_image
            with doc.create(Section('3D Views')):
                with doc.create(
                        Tabularx(
                            r'|>{\centering}X|>{\centering\arraybackslash}X|',
                            row_height=1.2)) as table:
                    view_3dimg_path = rel_path + Disp_3d_image
                    view_topimg_path = rel_path + Disp_top_image
                    view_sideimg_path = rel_path + Disp_side_image
                    view_frontimg_path = rel_path + Disp_front_image
                    table.add_hline()
                    table.add_row([
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_3dimg_path),
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_topimg_path)
                    ])
                    table.add_row('(a) 3D View', '(b) Top View')
                    table.add_hline()
                    table.add_row([
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_sideimg_path),
                        StandAloneGraphic(image_options="height=4cm",
                                          filename=view_frontimg_path)
                    ])
                    table.add_row('(c) Side View', '(d) Front View')
                    table.add_hline()
                # with doc.create(Figure(position='h!')) as view_3D:
                #     view_3dimg_path = rel_path + Disp_3d_image
                #     # view_3D.add_image(filename=view_3dimg_path, width=NoEscape(r'\linewidth'))
                #
                #     view_3D.add_image(filename=view_3dimg_path,width=NoEscape(r'\linewidth,height=6.5cm'))
                #
                #     view_3D.add_caption('3D View')

        with doc.create(Section('Design Log')):
            doc.append(
                pyl.Command('Needspace',
                            arguments=NoEscape(r'10\baselineskip')))
            logger_msgs = reportsummary['logger_messages'].split('\n')
            for msg in logger_msgs:
                if ('WARNING' in msg):
                    colour = 'blue'
                elif ('INFO' in msg):
                    colour = 'OsdagGreen'
                elif ('ERROR' in msg):
                    colour = 'red'
                else:
                    continue
                doc.append(TextColor(colour, '\n' + msg))
        try:
            doc.generate_pdf(filename, compiler='pdflatex', clean_tex=False)
        except:
            pass
示例#54
0
    def make_pdf(self, jobid, filename, dirname):
        """
        Makes a PDF for a given job id. It retrieves data from Prometheus HTTP API and calls make_plot() as well as make_pie() in order to make the graphs
        and display resource usage to users.
        Parameters
        ----------
        jobid : integer,
            Slurm job's ID
        filename : string
            File name to save the figure
        dirname : string
            directory name to save the file
        """
        geometry_options = {"right": "2cm", "left": "2cm"}
        fname = CWD + "pdf/" + str(jobid) + "_summary"
        doc = Document(fname, geometry_options=geometry_options)

        metrics = ("jobs_cpu_percent", "jobs_rss", "jobs_read_mb",
                   "jobs_write_mb")

        doc.preamble.append(
            Command("title", "Plots for job " + str(self.__jobid)))
        doc.preamble.append(Command("date", NoEscape(r"\today")))
        doc.append(NoEscape(r"\maketitle"))

        doc.append(
            "This pdf contains plots/pie charts which are representative of your job's usage of HPC resources on CC clusters"
        )
        doc.append(NewPage())

        for item in metrics:
            self.make_plot(item, filename, dirname, True)

            with doc.create(Section("Plot for " + Y_LABELS[item])):

                with doc.create(Figure(position="htbp")) as plot:
                    plot.add_plot(width=NoEscape(r"1\textwidth"))
                    plot.add_caption(Y_LABELS[item] + " variation with time")
                if item == "jobs_cpu_percent":
                    doc.append(
                        "The dashed line on this plot shows the lowest acceptable bound for CPU usage for a job with as many cores as yours"
                    )
            doc.append(NewPage())
            plt.close()

        metrics = ("jobs_user_time", "jobs_system_time")
        title = [Y_LABELS[metric] for metric in metrics]

        self.make_pie(metrics, filename, dirname, True)
        with doc.create(Section("Pie chart for " + " vs  ".join(title))):

            with doc.create(Figure(position="htbp")) as plot:
                plot.add_plot(width=NoEscape(r"1\textwidth"))
                plot.add_caption(", ".join(title) + " proportions")
        doc.append(NewPage())

        plt.close()

        metrics = ("jobs_cpu_time_core", )

        self.make_pie(metrics, filename, dirname, True)

        with doc.create(
                Section("Pie chart for " + Y_LABELS[metrics[0]] +
                        " per core")):
            with doc.create(Figure(position="htbp")) as plot:
                plot.add_plot(width=NoEscape(r"1\textwidth"))
                plot.add_caption(Y_LABELS[metrics[0]] + " proportions")
        doc.append(NewPage())

        plt.close()

        doc.generate_pdf(clean_tex=False)
示例#55
0
class ReportGenerator():
    '''
    - a class to generate the report (pdf only).
    - uses the methods from visualization class to draw graphs.
    - takes as input: path, options user selected, final model
    '''
    def __init__(self, X_train, y_train, target_names, path, experiments,
                 maximize, num_of_iter, regression, elapsed_time,
                 final_model_time):
        # assume model is an object contains function to predict #
        self.doc = Document(path)
        self.experiments = experiments
        # sort experiements on score
        self.experiments = self.experiments.sort_values('Score Mean',
                                                        ascending=not maximize)
        print '----------- Experiments -------------\n'
        print[p for p in self.experiments['Learner']]
        self.num_of_iter = num_of_iter
        self.num_of_features = X_train.shape[1]
        self.regression = regression
        self.elapsed_time = elapsed_time
        self.final_model_time = final_model_time
        self.X_train = X_train
        self.y_train = y_train
        self.target_names = target_names
        self.path = path
        self.probabilities = np.load(
            self.experiments['Path'].iloc[0])['PREDICTIONS']
        if not regression:
            self.predictions = np.argmax(self.probabilities, axis=1)

    # function checks if the given model is ensemble #
    def is_ensemble(self, s):
        return (s[:2] == "es")

    # function prints single model's parameters #
    def print_param(self, row):
        self.doc.append(self.format_dict(row[3]) + '\n')

    # functions extracts the actual model name to be shown #
    # in the doc from the encodded one #
    def get_model_name(self, s):
        ns = ''
        if self.is_ensemble(s):
            ns = re.sub("es", "Ensemble Selection", s)
            ns = re.sub('[_]', ' ', ns)
        else:
            for c in s:
                if c == c.upper():
                    ns += ' '
                ns += c
        return ns

    #edit function print ensemble
    def print_ensemble(self, row):
        d = row[3]  # dictionary of dictionaries(of ensembles)
        cnt = 1
        with self.doc.create(
                Subsubsection('Ensemble Selection', numbering=False)):
            self.doc.append(NoEscape(r'\leftskip=40pt'))  # indentation
            self.doc.append('Score: ' + str(row[1]) + '\n\n')
            for sub_d in d:
                self.doc.append('Bag: ' + str(cnt) + '\n\n')
                cnt += 1
                # for every ensemble print it
                table = Tabular('|c|c|c|l|')
                table.add_hline()
                # add header of table #
                table.add_row(('Learner', 'Score', 'Parameters', 'weight'))
                table.add_hline()
                for k in sub_d:
                    cur_model = self.experiments.loc[k]
                    data = [
                        cur_model[0],
                        round(cur_model[1], 4),
                        self.format_dict(cur_model[3]), sub_d[k]
                    ]
                    table.add_row(data)
                    table.add_hline()
                self.doc.append(table)
                self.doc.append('\n\n\n\n')

    # function prints the layer zero models of the given ensemble model
    def print_ensemble_models(self, row):
        # - create a sub_sub_section to easily indent #
        # - subsubsection title is the ensmble name #
        # - table of the ensemble models (layer 0) #
        ensemble_method = row[0]  # name of the ensmble method
        with self.doc.create(
                Subsubsection(self.get_model_name(ensemble_method),
                              numbering=False)):
            # create  table for the ensmeble models #
            self.doc.append(NoEscape(r'\leftskip=40pt'))  # indentation
            self.doc.append('Score: ' + str(row[1]) + '\n\n')
            table = Tabular('|c|c|c|l|')
            table.add_hline()
            # add header of table #
            table.add_row(('Learner', 'Score', 'Parameters', 'weight'))
            table.add_hline()
            # foreach model in the ensemble add row in the table #
            for k in row[3]:
                cur_model = self.experiments.loc[k]
                data = [
                    cur_model[0], cur_model[1],
                    self.format_dict(cur_model[3]), row[3][k]
                ]
                table.add_row(data)
                table.add_hline()
            self.doc.append(table)

    # function converts dictionary of parameters into a string #
    def format_dict(self, d):
        if type(d) == type(''):
            d = eval(d)
        s = ""
        f = False
        for k in d:
            if f:
                s += ", "
            f = True
            s += (str(k) + ":" + str(d[k]))
        return s

    def gen_summary(self, score):
        '''
        - function generates the first part of the doc the summary of the final model
        - inputs are booleans to decide whether to show then in the report or not.
        '''
        with self.doc.create(Section('Summary', numbering=False)):
            # -------- Final Model Description --------#
            '''
            final model:- single: learner name, parameters
                        - ensemble: type, models(parameters, scores)
            '''
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(
                    Subsection('Final Model Description', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                # check if ensemble or single model from its name #
                #edit
                #if self.is_ensemble(self.experiments.iloc[0][0]):
                #    self.print_ensemble_models(self.experiments.iloc[0])
                if self.experiments.iloc[0][0] == "ensembleSelection":
                    self.print_ensemble(self.experiments.iloc[0])
                else:
                    model_name = self.get_model_name(
                        self.experiments.iloc[0][0])
                    self.doc.append(model_name + ": ")
                    self.print_param(self.experiments.iloc[0])

            # ----------- Number OF iterations -----------#
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(
                    Subsection('Number of iterations', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                self.doc.append(str(self.num_of_iter))

            # ----------- Number Of Features -------------#
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(
                    Subsection('Number of features', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                self.doc.append(str(self.num_of_features))

            # ---------- Classification / Regression ------#
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(Subsection('Task type', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                if self.regression:
                    self.doc.append('Regression')
                else:
                    self.doc.append('Classification')
            '''
            # ----------- Elapsed Time ------------------#
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(Subsection('Elapsed Time', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                self.doc.append(self.elapsed_time)

            # --------------- final model time -------- #
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(Subsection('Final Model Time', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                self.doc.append(self.final_model_time)
            '''

    # function generates a table of the best models #
    def draw_top_models(self, n):
        '''
        - functions draw a table of the top models, with theri details: name, score, parameters.
        - takes the data frame of the models as input.
        - print the best ensemble models, then the best single models in a table
        '''
        self.doc.append(NoEscape(r'\leftskip=0pt'))
        with self.doc.create(
                Section('Top' + ' ' + str(n) + ' ' + 'Models',
                        numbering=False)):
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            single_models_table = Tabular("|c|c|c|")
            single_models_table.add_hline()
            single_models_table.add_row(["learner", "Score", "Parameters"])
            single_models_table.add_hline()
            # if ensemble print it, else append to the table
            k = 0
            single = 0
            ens = 0
            for model in self.experiments.values:
                if k >= n:
                    break
                print 'Model---\n', model[0]
                #edit
                if model[0] != "ensembleSelection":
                    #self.doc.append(NoEscape(r'\leftskip=20pt'))
                    #self.print_ensemble(model)
                    #else:
                    data = [model[0], model[1], self.format_dict(model[3])]
                    single_models_table.add_row(data)
                    single_models_table.add_hline()
                    single += 1
                    k += 1
            if single > 0:
                self.doc.append(NoEscape(r'\leftskip=20pt'))
                with self.doc.create(
                        Subsubsection('Single Models', numbering=False)):
                    self.doc.append(NoEscape(r'\leftskip=40pt'))
                    self.doc.append(single_models_table)

    # function generates the graphs according to user preferences #
    # uses visualization class #
    def gen_graphs(self, visu):
        '''
        function calls the graphs methods to draw from the visualization
        module according to use preferences
        takes as input a visualizer object
        '''
        # start Graphs Section in the report #
        self.doc.append(NoEscape(r'\leftskip=0pt'))
        with self.doc.create(Section('Graphs', numbering=False)):
            # uncomment after generating predictions

            if not self.regression:
                # Confusion Matrix #
                self.doc.append(NoEscape(r'\leftskip=20pt'))
                with self.doc.create(
                        Subsection('Confusion Matrix', numbering=False)):
                    self.doc.append(NoEscape(r'\leftskip=40pt'))
                    with self.doc.create(
                            Figure(position='htbp')
                    ) as conf_mat_plot:  # Create new Figure in tex
                        # get image path from the visualizer
                        file_name = visu.gen_conf_mat(
                            target_names=self.target_names,
                            predictions=self.predictions,
                            y=self.y_train,
                            dpi=300)
                        conf_mat_plot.add_image(file_name,
                                                width=NoEscape(r'1\textwidth'))

                self.doc.append(NoEscape(r'\pagebreak'))  #start new page
                # ROC Curve #
                self.doc.append(NoEscape(r'\leftskip=20pt'))
                with self.doc.create(Subsection('ROC Curve', numbering=False)):
                    self.doc.append(NoEscape(r'\leftskip=40pt'))
                    with self.doc.create(
                            Figure(position='htbp'
                                   )) as ROC_plot:  # Create new Figure in tex
                        # y_tr = self.y_train
                        y_ts = self.y_train
                        file_name = visu.gen_roc_curve(
                            probabilities=self.probabilities,
                            y=y_ts,
                            target_names=self.target_names)
                        ROC_plot.add_image(file_name,
                                           width=NoEscape(r'1.2\textwidth'))
                self.doc.append(NoEscape(r'\pagebreak'))  #start new page

            # Feature importance #
            self.doc.append(NoEscape(r'\leftskip=20pt'))
            with self.doc.create(
                    Subsection('Feature Importance', numbering=False)):
                self.doc.append(NoEscape(r'\leftskip=40pt'))
                with self.doc.create(Figure(position='htbp')) as imp_plot:
                    file_name = visu.gen_feature_imp(
                        regression=self.regression,
                        X=self.X_train,
                        y=self.y_train)
                    imp_plot.add_image(file_name,
                                       width=NoEscape(r'1.2\textwidth'))

            self.doc.append(NoEscape(r'\pagebreak'))  #start new page
            if self.experiments.iloc[0][0] == 'ensembleSelection':
                # Word Cloud #
                self.doc.append(NoEscape(r'\leftskip=20pt'))
                with self.doc.create(
                        Subsection('Models Cloud', numbering=False)):
                    self.doc.append(NoEscape(r'\leftskip=40pt'))
                    with self.doc.create(
                            Figure(position='htbp')) as cloud_plot:
                        file_name = visu.gen_word_cloud(self.experiments)
                        cloud_plot.add_image(file_name,
                                             width=NoEscape(r'1.2\textwidth'))

    # main function to be called to generate the report #
    def generate(self):
        '''
        main function that generate the report and call the
        functions of the report sections
        '''
        self.doc.packages.append(
            Package('geometry', options=['tmargin=1cm', 'lmargin=0.5cm']))
        # cover page #
        self.doc.preamble.append(Command(
            'title',
            'Experiemts Report',
        ))
        self.doc.preamble.append(Command('author', 'ATOM'))
        self.doc.append(NoEscape(r'\maketitle'))
        self.doc.append(NoEscape(r'\pagebreak'))
        # summary #
        self.gen_summary(self.experiments.iloc[0][1])
        self.doc.append(NoEscape(r'\pagebreak'))  #start new page
        # Top N Models #
        self.draw_top_models(4)
        self.doc.append(NoEscape(r'\pagebreak'))  #start new page
        # Graphs #
        visu = visualizer(save_dir=self.path)
        self.gen_graphs(visu)
        # generate pdf file #
        self.doc.generate_pdf('example')
        print 'Finished Report Generation'
示例#56
0
 def __init__(self, tex_filename):
     self.doc = Document(tex_filename)
     self.problems = []
示例#57
0
class Filewriter:

    unsupported_commands = {
        r'\tfrac': r'\frac',
        '•': r'$\cdot$',
        r'$$': r'$\$'
    }

    @classmethod
    def prep_file(cls, filename: str, header: str = ''):
        with open(filename, 'w') as output:
            output.write(header)

    @classmethod
    def write_file(cls, filename: str, content: str, mode: str = 'w'):
        with open(filename, mode) as output:
            output.write(content)

    @classmethod
    def write_pdf_from_tex(cls, tex_filename: str, pdf_filename: str = None):

        assert tex_filename.endswith(
            '.tex'), '"{}" is not a valid .tex file name'.format(tex_filename)

        if pdf_filename is None:
            pdf_filename = tex_filename[:-4]
        if pdf_filename[0] == '/':
            pdf_filename = pdf_filename[1:]
        if pdf_filename.endswith('.pdf'):
            pdf_filename = pdf_filename[:-4]

        pdfl = PDFLaTeX.from_texfile(tex_filename)
        pdfl.set_jobname(pdf_filename)
        pdfl.create_pdf(keep_pdf_file=True)

    def __init__(self, tex_filename):
        self.doc = Document(tex_filename)
        self.problems = []

    def add_problem_to_section(self, problem: str):
        for unsupported, supported in Filewriter.unsupported_commands.items():
            problem = problem.replace(unsupported, supported)
        self.problems.append(problem)

    def initialize_doc(self, title: str, author: str, with_date: bool = True):

        self.doc.packages.append(Package('amsmath'))  # To enable align
        self.doc.packages.append(Package('hyperref'))  # To enable hyperlink

        self.doc.preamble.append(Command('title', title))
        self.doc.preamble.append(Command('author', author))
        if with_date:
            self.doc.preamble.append(Command('date', NoEscape(r'\today')))
        self.doc.append(NoEscape(r'\maketitle'))

        with self.doc.create(Section('Acknowledgement')):
            self.doc.append(
                NoEscape(
                    r"All the following problems are copyrighted by the \href{https://www.maa.org/}{Mathematical Association of America}'s \href{https://www.maa.org/math-competitions}{American Mathematics Competitions}."
                ))

    def add_section(self, section_name: str):
        with self.doc.create(Section(section_name)):
            with self.doc.create(Enumerate()) as enum:
                for problem in self.problems:
                    enum.add_item(NoEscape(problem))
        self.problems = []

    def write_tex(self):
        self.doc.generate_tex()
示例#58
0
from service.genera_seccion_sumas_aritmeticas import GeneraSumas as gs
from service.genera_seccion_restas_aritmeticas import GeneraRestas as gr
from pylatex import Document, Section, Subsection, Tabular, Math, TikZ, Axis, \
    Plot, Figure, Matrix, Alignat, Enumerate
from pylatex.utils import italic

ejercicios_suma = gs(cantidad_sumas=10,
                     sumandos=10,
                     limite_superior=100,
                     limite_inferior=-100)
ejercicios_resta = gr(cantidad_restas=10,
                      sumandos=10,
                      limite_superior=100,
                      limite_inferior=-100)
geometry_options = {"tmargin": "1cm", "lmargin": "1cm"}
doc = Document(geometry_options=geometry_options)
with doc.create(Section('Aritmética ')):
    doc.append('Esta es la sección destinada a aritmética.')
    with doc.create(Subsection('Sumas')):
        doc.append('Resuelva correctamente las siguientes sumas:')
        with doc.create(Enumerate()) as enum:
            for ejercicio in ejercicios_suma.ejercicios:
                enum.add_item(ejercicio.__str__())
    with doc.create(Subsection('Restas')):
        doc.append('Resuelva correctamente las siguientes sumas:')
        with doc.create(Enumerate()) as enum:
            for ejercicio in ejercicios_resta.ejercicios:
                enum.add_item(ejercicio.__str__())

doc.generate_pdf('/tmp/ejercicios', clean_tex=True)
示例#59
0
dfResCOPAug = dfResCOP.loc[dfResCOP['date'] == augDate]
#Chapter 3
dfForHolStCOPOct = dfForHolStCOP.loc[dfForHolStCOP['date'] == octDate]
dfForHolFlCOPOct = dfForHolFlCOP.loc[dfForHolFlCOP['date'] == octDate]

dfForHolStUSDOct = dfForHolStUSD.loc[dfForHolStUSD['date'] == octDate]
dfForHolFlUSDOct = dfForHolFlUSD.loc[dfForHolFlUSD['date'] == octDate]

#Chapter 4
dfPortCOPJun = dfPortCOP.loc[dfPortCOP['date'] == junDate]
dfPortUSDJun = dfPortUSD.loc[dfPortUSD['date'] == junDate]

#__________________________________________________

#geometry_options = {"tmargin": "5cm"}
doc = Document(documentclass='report', document_options=['11pt, notitlepage'])

doc.preamble.append(NoEscape(r'\usepackage{appendix}'))
doc.preamble.append(NoEscape(r'\usepackage{graphicx}'))
doc.preamble.append(NoEscape(r'\usepackage{pdflscape}'))
doc.preamble.append(NoEscape(r'\usepackage[margin=1in]{geometry}'))
doc.preamble.append(NoEscape(r'\usepackage{hyperref}'))
doc.preamble.append(
    NoEscape(
        r'\hypersetup{colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan}'
    ))
doc.preamble.append(NoEscape(r'\urlstyle{same}'))
doc.preamble.append(NoEscape(r'\linespread{1.6}'))

doc.preamble.append(Command('title', 'Trounceflow Countries: Colombia'))
doc.preamble.append(Command('author', 'Michael Trounce'))
def create_pdf(recipes):
    doc = Document()
    doc.packages.append(Package("hyperref", options="hidelinks"))

    doc.append("\\begin{titlepage}\n"
               "\\centering\n"
               "{\\scshape\\Huge\\textbf The Greater Book of Transmutation \\par}\n"
               "\\vspace{1.5cm}\n"
               "{\\scshape\\large How to make almost anything in a few easy steps \\par}\n"
               "\\vspace{5.5cm}\n"
               "{\\large A Procedurally Generated DIY Book \\par}\n"
               "{\\large for the \href{https://github.com/dariusk/NaNoGenMo-2015}{NaNoGenMo 2015} \\par}\n"
               "\\vspace{0.5cm}\n"
               "{\\large by \\href{http://dragonlab.de}{Tobias Wehrum} \\par}\n"
               "\\vfill\n"
               "{\\scriptsize using data by: \\par}\n"
               "\\vspace{0.3cm}\n"
               "{\\scriptsize\\href{http://web.usf.edu/FreeAssociation}{Nelson, D. L., McEvoy, C. L., \\& Schreiber, T. A. (1998). The University of South Florida word association, rhyme, and word fragment norms}\\par}\n"
               "\\vspace{0.3cm}\n"
               "{\\scriptsize\\href{https://www.englishclub.com/vocabulary/nouns-uncountable-list.htm}{English Club: Uncountable Nouns List}\\par}\n"
               "\\vspace{0.3cm}\n"
               "{\\scriptsize\\href{http://archives.nd.edu/whitaker/dictpage.htm}{LATIN-ENGLISH DICTIONARY WORDLIST Version 1.97FC by William Whitaker}\\par}\n"
               "\\vspace{0.5cm}\n"
               "\\end{titlepage}\n"
               "\n"
               "\\setcounter{tocdepth}{1}\n"
               "\\renewcommand*\\contentsname{How to make...}\n"
               "\\tableofcontents\n"
               "\\newpage");

    for r in recipes:
        r.print_to_doc(doc)

    try:
        os.remove("TheGreaterBookOfTransmutation.toc")
    except Exception as err:
        traceback.print_tb(err.__traceback__)

    try:
        doc.generate_pdf("TheGreaterBookOfTransmutation")
    except Exception as err:
        traceback.print_tb(err.__traceback__)

    try:
        doc.generate_tex("TheGreaterBookOfTransmutation")
    except Exception as err:
        traceback.print_tb(err.__traceback__)