def test_graphics(): f = Figure(data=None, position=None) f.add_image(filename='', width=r'0.8\textwidth', placement=r'\centering') f.add_caption(caption='') # Subfigure s = SubFigure(data=None, position=None, width=r'0.45\linewidth', seperate_paragraph=False) s.add_image(filename='', width='r\linewidth', placement=None) s.add_caption(caption='') # Matplotlib plot = MatplotlibFigure(data=None, position=None) x = [0, 1, 2, 3, 4, 5, 6] y = [15, 2, 7, 1, 5, 6, 9] pyplot.plot(x, y) plot.add_plot(width=r'0.8\textwidth', placement=r'\centering') plot.add_caption(caption='I am a caption.') # Quantities Quantity(quantity=1 * pq.kg) Quantity(quantity=1 * pq.kg, format_cb=lambda x: str(int(x)))
def test_graphics(): f = Figure(data=None, position=None) f.add_image(filename='', width=r'0.8\textwidth', placement=r'\centering') f.add_caption(caption='') repr(f) # Subfigure s = SubFigure(data=None, position=None, width=r'0.45\linewidth') s.add_image(filename='', width='r\linewidth', placement=None) s.add_caption(caption='') repr(s) # Matplotlib plot = Figure(data=None, position=None) x = [0, 1, 2, 3, 4, 5, 6] y = [15, 2, 7, 1, 5, 6, 9] pyplot.plot(x, y) plot.add_plot(width=r'0.8\textwidth', placement=r'\centering') plot.add_caption(caption='I am a caption.') repr(plot)
def de_image(s: lines.Image): img_path = ImageTool.verify(s.link, config.cacheimg_dir) if img_path is None: c = Center() c.append(NoEscape('Link or path not found: {}'.format(s.link))) return c if config.give_rele_path: img_path = os.path.relpath(img_path, config.output_dir) img_path = norm_path(img_path) c = Center() if isinstance(s.parent, env.Quote): c.append( NoEscape( r"\includegraphics[width=0.8\textwidth]{{{img_path}}}".format( img_path=img_path))) else: fig = Figure(position=config.fig_position) fig.add_image(img_path, placement='') if len(s.desc.strip()) > 0: fig.add_caption(s.desc) c.append(fig) return c
def test_graphics(): f = Figure(data=None, position=None) f.add_image(filename='', width=r'0.8\textwidth', placement=r'\centering') f.add_caption(caption='') repr(f) # Subfigure s = SubFigure(data=None, position=None, width=r'0.45\linewidth') s.add_image(filename='', width='r\linewidth', placement=None) s.add_caption(caption='') repr(s) # Matplotlib plot = Figure(data=None, position=None) x = [0, 1, 2, 3, 4, 5, 6] y = [15, 2, 7, 1, 5, 6, 9] pyplot.plot(x, y) plot.add_plot(width=r'0.8\textwidth', placement=r'\centering') plot.add_caption(caption='I am a caption.') repr(plot) # StandAloneGraphic stand_alone_graphic = StandAloneGraphic( filename='', image_options=r"width=0.8\textwidth") repr(stand_alone_graphic)
def _append2latexdoc(doc, content): if isinstance(content, list): for item in content: if item.get('title'): doc.append(_get_section(**item)) _append2latexdoc(doc, item['content']) else: _append2latexdoc(doc, item) else: section = _get_last_section(doc) if content.get('text'): if isinstance(content['text'], dict): section.append(open(content['text']['filename']).read()) else: temp = '' if isinstance(content['text'],list): for subtext in content['text']: temp += subtext elif isinstance(content['text'],str): temp = content['text'] else: raise Exception(f'Did not understand format of text string: \n {content["text"]}') section.append(temp) if content.get('latex_code'): if isinstance(content['latex_code'], dict): section.append(NoEscape(open(content['latex_code']['filename']).read())) else: section.append(NoEscape(content['latex_code'])) if content.get('table'): for table in content['table']: section.append(NoEscape('\\begin{table}[H]')) # note require float latex package for H command if table.get('filename'): df = pd.read_excel(table['filename'], **table['kwargs']) elif isinstance(table.get('dataframe'),pd.DataFrame): df = table.get('dataframe') section.append(NoEscape(df.to_latex(longtable=True,multicolumn_format='c'))) section.append(NoEscape('\\end{table}')) if content.get('image'): for image in content.get('image'): section.append(NoEscape('\\begin{figure}[H]')) # note require float latex package for H command Figure.add_image(section, image['filename']) section.append(NoEscape('\\end{figure}')) if content.get('subimage'): figure = Figure(position='H') for i, subimage in enumerate(content['subimage']): subfigure = SubFigure(width=NoEscape( r'{}\linewidth'.format(np.round(1. / subimage.get('nr_horizontal_subimages', 2), 2) - 0.01))) subfigure.add_image(subimage['filename']) if subimage.get('caption', False): subfigure.add_caption(subimage['caption']) if subimage.get('figure_caption', False) and i == 0: figure.add_caption(subimage['figure_caption']) figure.append(subfigure) if (i + 1) % subimage.get('nr_horizontal_subimages', 2) == 0 and i != 0 or subimage.get( 'nr_horizontal_subimages', 2) == 1: section.append(figure) figure = Figure(arguments=NoEscape('\ContinuedFloat'), position='H') section.append(figure) if content.get('packages'): [doc.packages.append(Package(package)) for package in content['packages']]
def test_graphics(): f = Figure(data=None, position=None) f.add_image(filename="", width=r"0.8\textwidth", placement=r"\centering") f.add_caption(caption="") # Subfigure s = SubFigure(data=None, position=None, width=r"0.45\linewidth", seperate_paragraph=False) s.add_image(filename="", width="r\linewidth", placement=None) s.add_caption(caption="") # Matplotlib plot = MatplotlibFigure(data=None, position=None) x = [0, 1, 2, 3, 4, 5, 6] y = [15, 2, 7, 1, 5, 6, 9] pyplot.plot(x, y) plot.add_plot(width=r"0.8\textwidth", placement=r"\centering") plot.add_caption(caption="I am a caption.") # Quantities Quantity(quantity=1 * pq.kg) Quantity(quantity=1 * pq.kg, format_cb=lambda x: str(int(x)))
def latex(self): width = "auto" height = "auto" self._parse_attr_latex() if 'width' in self.attrs: width = self.attrs['width'] if 'height' in self.attrs: width = self.attrs['height'] _figure = Figure(position = 'h') _figure.add_image(self.uri, width = NoEscape(width), placement= NoEscape('\centering')) _figure.add_caption(self.caption) return _figure
def add_figure(self, file=None, caption=None, width='240px'): """ 添加图形 :param str file: 图形文件路径 :param str caption: 图形标题 :param str width: 图形宽度 :return: 无返回值 """ graph = Figure(position='h!') graph.add_image(file, width=width) if caption is not None: graph.add_caption(caption) self.doc.append(graph)
def add_figure(self,file=None,caption=None,width='240px'): """ 添加图形 :param str file: 图形文件路径 :param str caption: 图形标题 :param str width: 图形宽度 :return: 无返回值 """ graph = Figure(position='h!') graph.add_image(file, width=width) if caption is not None: graph.add_caption(caption) self.doc.append(graph)
def get_canvas_as_figure_and_filename(self): image_names = self.canvas.save_canvas_as_images() # Create figure with canvas image pictures = [] for filename in image_names: picture = Figure(position="h") img = Image.open(filename) # If the canvas is small, it does not need to be the full width of the page fq = 1 magic_scale_factor_number_thing = 900 if (tot := img.size[0]) < magic_scale_factor_number_thing: fq = tot / magic_scale_factor_number_thing # Add the image to the figure with the calculated width picture.add_image(filename, width=NoEscape(str(fq) + r"\textwidth")) pictures.append(picture)
def test_graphics(): f = Figure(data=None, position=None) f.add_image(filename="", width=r"0.8\textwidth", placement=r"\centering") f.add_caption(caption="") # Subfigure s = SubFigure(data=None, position=None, width=r"0.45\linewidth", separate_paragraph=False) s.add_image(filename="", width="r\linewidth", placement=None) s.add_caption(caption="") # Matplotlib plot = Figure(data=None, position=None) x = [0, 1, 2, 3, 4, 5, 6] y = [15, 2, 7, 1, 5, 6, 9] pyplot.plot(x, y) plot.add_plot(width=r"0.8\textwidth", placement=r"\centering") plot.add_caption(caption="I am a caption.")
def visualize_zeta(self, doc, zeta_edge_pair_results, path, top_n_results=10): # def visualize_zeta(self, edge_pair_result, top_n_results=10): section = Section("Zeta Scores for Pairs with highest edge weights") section.append( "Zeta score is a stylometry measure that measures preferred and avoided terms in the context of character pairs." ) for index, edge_pair_result in enumerate(zeta_edge_pair_results): #features().visualize_zeta(zeta_results[index], name_for_figure="zeta_pair_%s" %index, path=dirpath) #subsection = Subsection("Zeta Scores for Pairs with highest edge weights", numbering=False) subsection = Subsection("Edge Pair: %s -- %s" % (edge_pair_result["name_target"], edge_pair_result["name_comparison"]), numbering=False) #Target (Character A) # the following index [::-1] inverts the list for the figure objects = [ el[0] for el in edge_pair_result["zeta_scores_target_sorted"] [0:top_n_results] ][::-1] y_pos = np.arange(len(objects)) performance = [ el[1] for el in edge_pair_result["zeta_scores_target_sorted"] [0:top_n_results] ][::-1] plt.barh(y_pos, performance, align='center', alpha=0.5) plt.yticks(y_pos, objects) plt.xlabel('Zeta Score') plt.title('%s-context' % edge_pair_result["name_target"]) # plt.show() # plt.savefig("zeta.pdf", bbox_inches='tight') #print(path) plt.savefig("%s/zeta_pair_%s_a_target.pdf" % (path, index), bbox_inches='tight') target_pic = Figure(position="H") target_pic.add_image(os.path.join( path, "zeta_pair_%s_a_target.pdf" % index), width='240px') target_pic.add_caption( "Prefered terms in context of %s (compared to %s)" % (edge_pair_result["name_target"], edge_pair_result["name_comparison"])) #wordcloud_pic.add_caption('word cloud of "%s -- %s"' % ( subsection.append(target_pic) ################ Comparison (Character B) objects = [ el[0] for el in edge_pair_result["zeta_scores_comparison_sorted"] [0:top_n_results] ][::-1] y_pos = np.arange(len(objects)) performance = [ el[1] for el in edge_pair_result["zeta_scores_comparison_sorted"] [0:top_n_results] ][::-1] plt.barh(y_pos, performance, align='center', alpha=0.5) plt.yticks(y_pos, objects) plt.xlabel('Zeta Score') plt.title('%s-context' % edge_pair_result["name_comparison"]) # plt.show() # plt.savefig("zeta.pdf", bbox_inches='tight') plt.savefig("%s/zeta_pair_%s_b_comparison.pdf" % (path, index), bbox_inches='tight') comparison_pic = Figure(position="H") comparison_pic.add_image(os.path.join( path, "zeta_pair_%s_b_comparison.pdf" % index), width='240px') comparison_pic.add_caption( "Prefered terms in context of %s (compared to %s)" % (edge_pair_result["name_comparison"], edge_pair_result["name_target"])) subsection.append(comparison_pic) #subsection.append(subsubsection) section.append(subsection) doc.append(section)
def write_word_cloud_single_character( self, doc, dta_holder, tpath, number_of_wc, head_of_file_name="wordcloud_for_single_character", wc_context_selection="MFW", words_in_word_cloud=12): doc.packages.append(Package('float')) if wc_context_selection == "PMI": section = Section( "Word Cloud for single characters (method: pointwise mutual information", numbering=True) section.append( "These word clouds were constructed based on pointwise mutual information (PMI). PMI is a measure of how strongly each term is associated with each character mention. " ) if wc_context_selection == "MFW": section = Section( "Word Cloud for single characters (method: most frequent contexts words", numbering=True) section.append( "These word clouds were constructed based on most frequent words. They show the most frequent words that appear around character mention. " ) network_parameters = dta_holder["network_parameters"] weighted_degrees = network_parameters[5] weighted_degrees_sorted = sorted(weighted_degrees, key=operator.itemgetter(1), reverse=True) if wc_context_selection == "PMI": character_names_with_highest_degree = list() for weighted_degree in weighted_degrees_sorted[0:number_of_wc]: character_names_with_highest_degree += [weighted_degree[0]] for index, context_dic in enumerate( dta_holder["single_character_context"]): if dta_holder["single_character_context"][index][ "character_names"] in character_names_with_highest_degree: if len(context_dic["tf_sorted_list"] [0:words_in_word_cloud]) == 0: text_string = "<<empty_word_cloud>>" if len(context_dic["tf_sorted_list"] [0:words_in_word_cloud]) > 0: #print(context_dic) text_string = str() # for word_freq_tuple in relation["tf_sorted_list"][0:10]: for word_freq_tuple in context_dic["tf_sorted_list"][ 0:words_in_word_cloud]: if word_freq_tuple[1] < 10: for i in range(word_freq_tuple[1]): text_string += "%s " % word_freq_tuple[0] if word_freq_tuple[1] >= 10: for i in range(9): # print(i) text_string += "%s " % word_freq_tuple[0] # print(text_string) word_cloud.generate_wordcloud_simple( text=text_string, ending_number=index, temppath=tpath, file_name_head=head_of_file_name) #wc = word_cloud.generate_wordcloud_simple(text=str(text_string)) wordcloud_pic = Figure(position="H") wordcloud_pic.add_image( os.path.join( tpath, "wordcloud_for_single_character%s.png" % index), width='240px') # , placement="center") wordcloud_pic.add_caption( 'word cloud of "%s"' % (dta_holder["single_character_context"][index] ["character_names"])) section.append(wordcloud_pic) doc.append(section) if wc_context_selection == "MFW": character_names_with_highest_degree = list() for weighted_degree in weighted_degrees_sorted[0:number_of_wc]: character_names_with_highest_degree += [weighted_degree[0]] for index in range(0, len(dta_holder["single_character_context"])): if dta_holder["single_character_context"][index][ "character_names"] in character_names_with_highest_degree: if len(dta_holder["single_character_context"][index] ["tf_sorted_list"][0:words_in_word_cloud]) == 0: text_string = "<<empty_word_cloud>>" if len(dta_holder["single_character_context"][index] ["tf_sorted_list"][0:words_in_word_cloud]) > 0: #print(dta_holder["single_character_context"][index]["tf_sorted_list"][0:12]) text_string = str() # for word_freq_tuple in relation["tf_sorted_list"][0:10]: for word_freq_tuple in dta_holder[ "single_character_context"][index][ "tf_sorted_list"][0:words_in_word_cloud]: #print(word_freq_tuple) # for i in range(word_freq_tuple[1]): # text_string += "%s " % word_freq_tuple[0] if word_freq_tuple[1] < 10: for i in range(word_freq_tuple[1]): text_string += "%s " % word_freq_tuple[0] if word_freq_tuple[1] >= 10: for i in range(9): #print(i) text_string += "%s " % word_freq_tuple[0] #print(text_string) word_cloud.generate_wordcloud_simple( text=text_string, ending_number=index, temppath=tpath, file_name_head=head_of_file_name) # wc = word_cloud.generate_wordcloud_simple(text=str(text_string)) ####### wordcloud_pic = Figure(position="H") wordcloud_pic.add_image( os.path.join( tpath, "wordcloud_for_single_character%s.png" % index), width='240px') # , placement="center") wordcloud_pic.add_caption( 'word cloud of "%s"' % (dta_holder["single_character_context"][index] ["character_names"])) # subs.append(wordcloud_pic) # section.append(subs) section.append(wordcloud_pic) doc.append(section)
def write_word_cloud(self, doc, dta_holder, tpath, number_of_wc, head_of_file_name="wordcloud", wc_context_selection="MFW", words_in_word_cloud=12): # import float package for -> Figure(position="H") doc.packages.append(Package('float')) #section = Section("Word Cloud for character pairs", numbering=True) if wc_context_selection == "PMI": section = Section( "Word Cloud for character pairs (method: pointwise mutual information", numbering=True) section.append( "These word clouds were constructed based on pointwise mutual information (PMI). PMI is a measure of how strongly each term is associated with the character pair. " ) if wc_context_selection == "MFW": section = Section( "Word Cloud for character pairs (method: most frequent contexts words", numbering=True) section.append( "These word clouds were constructed based on most frequent words. They show the most frequent words that appear in the context of the character pair. " ) network_parameters = dta_holder["network_parameters"] edge_weights = network_parameters[6] edge_weights_sorted = sorted(edge_weights, key=operator.itemgetter(2), reverse=True) ### here we iterate over all context_terms in "character_relations_context" ### it is done with the index since a numbering for the word cloud file is necessary # for relation in dta_holder["character_relations_context"]: if wc_context_selection == "PMI": #print(dta_holder["character_relations_context"]) PMI_all_pairs = features().PMI( context_words=dta_holder["character_relations_context"]) #print(PMI_all_pairs[0:3]) # for i in PMI_all_pairs[0:3]: # print(i) for edge_pair_list in edge_weights_sorted[0:number_of_wc]: for index, character_context_dic in enumerate(PMI_all_pairs): #print(index) if character_context_dic["character_names"][ 0] == edge_pair_list[0] and character_context_dic[ "character_names"][1] == edge_pair_list[1]: #print(index, character_context_dic["character_names"], character_context_dic["PMI"]) if len(character_context_dic["PMI"] [0:words_in_word_cloud]) == 0: text_string = "<<empty_word_cloud>>" if len(character_context_dic["PMI"] [0:words_in_word_cloud]) > 0: text_string = str() # for word_freq_tuple in relation["tf_sorted_list"][0:10]: #print(index,character_context_dic["character_names"],character_context_dic["PMI"][0:12]) for word_freq_list in character_context_dic["PMI"][ 0:words_in_word_cloud]: if round(word_freq_list[1]) < 10: if round(word_freq_list[1]) > 0: for i in range(round( word_freq_list[1])): text_string += "%s " % word_freq_list[ 0] if round(word_freq_list[1]) <= 0: text_string += "%s " % word_freq_list[0] if round(word_freq_list[1]) >= 10: for i in range(9): text_string += "%s " % word_freq_list[0] #print(text_string) word_cloud.generate_wordcloud_simple( text=text_string, ending_number=index, temppath=tpath, file_name_head=head_of_file_name) # wc = word_cloud.generate_wordcloud_simple(text=str(text_string)) ####### wordcloud_pic = Figure(position="H") wordcloud_pic.add_image( os.path.join(tpath, "wordcloud%s.png" % index), width='240px') # , placement="center") # wordcloud_pic.add_image(wc, width='240px')#, placement="center") # wordcloud_pic.add_caption('word cloud of "%s -- %s"' % (relation["character_names"][0], relation["character_names"][1])) wordcloud_pic.add_caption( 'word cloud of "%s -- %s"' % (dta_holder["character_relations_context"][index] ["character_names"][0], dta_holder["character_relations_context"][index] ["character_names"][1])) # subs.append(wordcloud_pic) # section.append(subs) section.append(wordcloud_pic) doc.append(section) if wc_context_selection == "MFW": #BETTER WITH ENUMERATE (CHANGE, IF HAVE SOME TIME) # for edge_pair_list in edge_weights_sorted[0:number_of_wc]: # for index, character_context_dic in enumerate(dta_holder["character_relations_context"]): # #print(index) # if character_context_dic["character_names"][0] == edge_pair_list[0] and character_context_dic["character_names"][1] == edge_pair_list[1]: # print(index, character_context_dic["character_names"], character_context_dic["tf_sorted_list"]) # for edge_pair_list in edge_weights_sorted[0:number_of_wc]: for index in range( len(dta_holder["character_relations_context"])): if dta_holder["character_relations_context"][index][ "character_names"][0] == edge_pair_list[ 0] and dta_holder[ "character_relations_context"][index][ "character_names"][ 1] == edge_pair_list[1]: #print(dta_holder["character_relations_context"][index]["tf_sorted_list"][0:12]) if len(dta_holder["character_relations_context"][index] ["tf_sorted_list"][0:words_in_word_cloud]) == 0: text_string = "<<empty_word_cloud>>" if len(dta_holder["character_relations_context"][index] ["tf_sorted_list"][0:words_in_word_cloud]) > 0: text_string = str() # for word_freq_tuple in relation["tf_sorted_list"][0:10]: for word_freq_tuple in dta_holder[ "character_relations_context"][index][ "tf_sorted_list"][ 0:words_in_word_cloud]: if word_freq_tuple[1] < 10: for i in range(word_freq_tuple[1]): text_string += "%s " % word_freq_tuple[ 0] if word_freq_tuple[1] >= 10: for i in range(9): # print(i) text_string += "%s " % word_freq_tuple[ 0] #print(text_string) word_cloud.generate_wordcloud_simple( text=text_string, ending_number=index, temppath=tpath, file_name_head=head_of_file_name) # wc = word_cloud.generate_wordcloud_simple(text=str(text_string)) ####### wordcloud_pic = Figure(position="H") wordcloud_pic.add_image( os.path.join(tpath, "wordcloud%s.png" % index), width='240px') # , placement="center") # wordcloud_pic.add_image(wc, width='240px')#, placement="center") # wordcloud_pic.add_caption('word cloud of "%s -- %s"' % (relation["character_names"][0], relation["character_names"][1])) wordcloud_pic.add_caption( 'word cloud of "%s -- %s"' % (dta_holder["character_relations_context"][index] ["character_names"][0], dta_holder["character_relations_context"][index] ["character_names"][1])) # subs.append(wordcloud_pic) # section.append(subs) section.append(wordcloud_pic) doc.append(section)
import os from pylatex import Command, NewPage, Figure from pylatex.position import Center from pylatex.utils import NoEscape from pytex import ExamCore from pytex.exam import Question core = ExamCore(packages=[["geometry", "a4paper,centering,scale=0.8"], "amsmath", "graphicx", "amssymb"]) core.define([r"\dif"], [r"\text{d}"]) fig = Figure(position='h!') fig.add_image(os.path.join(os.path.dirname(__file__), "resources/timg.jpg"), width=NoEscape(r'0.8\linewidth')) core.pre_append(title=Command('heiti', '试卷示例'), author=Command('kaishu', '六个骨头'), date=Command('today')) core.body_append(Command('maketitle')) core.body_append( NoEscape(r'本试卷分为100分的必答题和10分的选做题,选做题做对加分,' r'做错不扣分,最多累计10分,整卷最高分110分。')) core.body_append(NoEscape(r"\vspace{15cm}")) with core.create(Center()) as centered: core.body_append(NoEscape(r'\bf{姓名}\ \underline{\hbox to 20mm{}}'), NoEscape(r'\bf{学号}\ \underline{\hbox to 20mm{}}'), NoEscape(r'\bf{编号}\ \underline{\hbox to 20mm{}}')) core.body_append(NewPage())
t.add_row(cells=(1, 2), escape=False) t.add_multicolumn(size=2, align='|c|', content='Multicol', cells=None, escape=False) t.add_multirow(size=3, align='*', content='Multirow', hlines=True, cells=None, escape=False) # Command c = Command('documentclass', arguments=None, options=None, packages=None) # Figure f = Figure(data=None, position=None) f.add_image(filename='', width=r'0.8\textwidth', placement=r'\centering') f.add_caption('') # Numpy v = VectorName(name='') M = np.matrix([[2, 3, 4], [0, 0, 1], [0, 0, 2]]) m = Matrix(matrix=M, name='', mtype='p', alignment=None) # Package p = Package(name='', base='usepackage', options=None) # PGFPlots
escape=False) # MultiColumn/MultiRow. t.add_row((MultiColumn(size=2, align='|c|', data='MultiColumn'), )) t.add_row((MultiRow(size=2, width='*', data='MultiRow'), )) # Command c = Command(command='documentclass', arguments=None, options=None, packages=None) # Figure f = Figure(data=None, position=None) f.add_image(filename='', width=r'0.8\textwidth', placement=r'\centering') f.add_caption(caption='') # Subfigure s = SubFigure(data=None, position=None, width=r'0.45\linewidth', seperate_paragraph=False) s.add_image(filename='', width='r\linewidth', placement=None) s.add_caption(caption='') # Plt plot = Plt(data=None, position=None)