Skip to content

Commit

Permalink
Add missing additional documents
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-jezegou committed Jun 16, 2024
1 parent dc69deb commit 150f394
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
57 changes: 57 additions & 0 deletions additional_algorithms/class_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
''' Aim of this file :
- Use json export from word_challenge to plot performances charts/plots
'''
from jinja2 import Template

EXPORT_PATH = "documentation/images/visualizations/"
TEMPLATE_PATH = "additional_algorithms/class_description_template.tex.jinja"

printable_methods = [
"__init__",
"__repr__",
"__eq__",
"__lt__",
"__hash__",
]

class ObjectClassToTikz():
'''Draw TiKz images from class to show graphically the class construction'''
def __init__(self, object, template_path: str = TEMPLATE_PATH, hidden_methods: bool = False) -> None:
self.object = object
self.classname = self.object.__class__.__name__
self.attributes = self.object.__dict__.keys()
self.methods = [method for method in dir(self.object)
if callable(getattr(self.object, method))
and (not method.startswith('__') if hidden_methods==False else (method in printable_methods or not method.startswith('__')))]
self.template_path = template_path
self.hidden_methods = hidden_methods


def build_elements(self, elements) -> str:
'''Parse attributes/methods from a class'''
attributes_code = ""
for i, key in enumerate(list(elements)):
# Check if hidden method
attributes_code += r"\small{%d}: \verb|%s|" % (i, key)
if i != len(list(elements)) - 1:
attributes_code += "\\\\\n"
return attributes_code

def save_drawing(self) -> None:
'''Use classes attributes/methods and write in a TiKz template'''
filename = f"class_%s.tex" % self.classname

context = {
'title': self.classname,
'attributes': self.build_elements(self.attributes),
'methods': self.build_elements(self.methods)
}

with open(self.template_path, 'r') as f:
template_content = f.read()

template = Template(template_content)

with open(EXPORT_PATH + filename, 'w') as f:
f.write(template.render(context))

27 changes: 27 additions & 0 deletions additional_algorithms/class_description_template.tex.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
\begin{figure}[h]
\centering
\begin{tikzpicture}
\node[draw, rounded corners=2mm, inner sep = 0.2cm, fill=orange!0] {
\begin{tikzpicture}
\node[inner sep = 1mm] (title) { \Large{\bfseries {{ title }} }};
% \draw (title.south west) -- (title.south east);
\node[at=(title.south), anchor=north, inner sep=3mm, align=left, fill=green!30!black!10, yshift=-0.2cm] (attributes) {
\begin{minipage}{60mm}
\textbf{Attributes}\\
{{ attributes }}
\end{minipage}
};
% \draw (attributes.north west) -- (attributes.north east);
\node[at=(attributes.south), anchor=north, inner sep=3mm, align=left, fill=blue!10, yshift=-0.2cm] (methods) {
\begin{minipage}{60mm}
\textbf{Methods}\\
{{ methods }}
\end{minipage}
};
% \draw (methods.north west) -- (methods.north east);
\end{tikzpicture}
};
\end{tikzpicture}
\caption{Class description - {{ title }} }
\label{class:{{ title }}}
\end{figure}
4 changes: 2 additions & 2 deletions additional_algorithms/template_2D_intersections.tex.jinja
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
\begin{tikzpicture}
\begin{axis}
\begin{axis}[title={{ title }}]
% Segments
{% for segment in segments %}
{{ segment }}
{% endfor %}
% Intersections
{% for intersection in intersections %}
\node[cross out, text=blue, fill=blue] at {{ intersection }} {$\times$};
\node[cross out, text=blue, fill=blue] at {{ intersection }} {$\mathbf{\times}$};
{% endfor %}
\end{axis}
\end{tikzpicture}
6 changes: 5 additions & 1 deletion additional_algorithms/template_performances.tex.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
\begin{tikzpicture}
\begin{axis}[]
\begin{axis}[
title={ {{ title }}},
xlabel={ {{ xlabel }} },
ylabel={ {{ ylabel }} }
]
{% for metric in metrics %}
{{ metric }};
{% endfor %}
Expand Down

0 comments on commit 150f394

Please sign in to comment.