OlĂĄ Habr! Vou falar sobre a estrutura arquitetĂŽnica que estou desenvolvendo.
A arquitetura determina a estrutura mais geral do programa e a interação de seus componentes. O Lena como estrutura implementa uma arquitetura especĂfica para anĂĄlise de dados (mais sobre isso abaixo) e fornece ao usuĂĄrio classes e funçÔes que podem ser Ășteis (levando em consideração essa arquitetura).
Lena Ă© escrita na popular linguagem Python e trabalha com versĂ”es do Python 2, 3 e PyPy. Ă publicado sob a licença Apache gratuita (versĂŁo 2) aqui . No momento, ele ainda estĂĄ sendo desenvolvido, mas os recursos descritos neste manual jĂĄ estĂŁo em uso, testados (a cobertura total de toda a estrutura Ă© de cerca de 90%) e dificilmente serĂĄ alterada. Lena surgiu na anĂĄlise de dados experimentais em fĂsica de neutrinos e recebeu o nome do grande rio siberiano.

Em geral, os problemas de arquitetura surgem em projetos de grande e mĂ©dio porte. Se vocĂȘ estĂĄ pensando em usar essa estrutura, aqui estĂĄ uma breve visĂŁo geral de suas tarefas e vantagens.
Do ponto de vista da programação:
- modularidade, envolvimento fraco. Os algoritmos podem ser facilmente adicionados, substituĂdos ou reutilizados.
- ( ). . PyPy " ".
- . . .
- . , . . .
- , .
, Python, , .
:
(tutorial) â Lena. , , , , . . .
Lena
. , , . .
, . Lena , , . , , .
Lena
Lena â . .
Lena . , :
>>> from __future__ import print_function
>>> from lena.core import Sequence
>>> s = Sequence(
... lambda i: pow(-1, i) * (2 * i + 1),
... )
>>> results = s.run([0, 1, 2, 3])
>>> for res in results:
... print(res)
1 -3 5 -7
Lena Python 2 3, print. .
Sequence . run. ( ).
, for.
. - , - . Source:
from lena.core import Sequence, Source
from lena.flow import CountFrom, ISlice
s = Sequence(
lambda i: pow(-1, i) * (2 * i + 1),
)
spi = Source(
CountFrom(0),
s,
ISlice(10**6),
lambda x: 4./x,
Sum(),
)
results = list(spi())
Source __call__, . : , .
CountFrom â , . , Âč. CountFrom ( ). CountFrom â start ( ) step ( 1).
Source ( ) (callable) run. Sequence.
. s Source. , s s.
Sequence , Sequence. Sequence Source, (flow).
: Sequence Source , LenaTypeError ( TypeError Python).
Lena â LenaException. ( , ).
, - . ISlice. ISlice CountFrom islice count itertools Python. ISlice start, stop[, step], ( ) step ( step , ).
, .
.
. run, flow:
class Sum():
def run(self, flow):
s = 0
for val in flow:
s += val
yield s
, return, yield. Yield â Python, .
â Python.
>>> results = s.run([0, 1, 2, 3])
Sequence run . , , , . , . ( ) :
>>> for res in results:
... print(res)
:
- . . , , , . , .
- . -. , , .
Python yield. Lena. run, . , , , , - .
(yield) . (flow) . , (value).
Lena . â , .
Lena , . Jinja . Lena , . LaTeX:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot [
const plot,
]
table [col sep=comma, header=false] {\VAR{ output.filepath }};
\end{axis}
\end{tikzpicture}
\end{document}
TikZ , : \VAR{ output.filepath }. \VAR{ var } var . , . output.filepath .
:
\BLOCK{ set var = variable if variable else '' }
\begin{tikzpicture}
\begin{axis}[
\BLOCK{ if var.latex_name }
xlabel = { $\VAR{ var.latex_name }$
\BLOCK{ if var.unit }
[$\mathrm{\VAR{ var.unit }}$]
\BLOCK{ endif }
},
\BLOCK{ endif }
]
...
variable, var . latex_name unit (), x. , x [m] E [keV] . , , .
Jinja . , . JinjaÂČ .
Jinja LaTeX, Lena Âł: \BLOCK \VAR .
â Python . Flow Lena (data, context). dataflow, . , Lena. . , :
class ReadData():
"""Read data from CSV files."""
def run(self, flow):
"""Read filenames from flow and yield vectors.
If vector component could not be cast to float,
*ValueError* is raised.
"""
for filename in flow:
with open(filename, "r") as fil:
for line in fil:
vec = [float(coord)
for coord in line.split(',')]
yield (vec, {"data": {"filename": filename}})
flow . data ( ). filename data["filename"] data.filename.
-, HTML LaTeX , , . , . â , - ( ).
Lena. , .
, , . , , .
. x.
docs/examples/tutorial .
main.py
from __future__ import print_function
import os
from lena.core import Sequence, Source
from lena.math import mesh
from lena.output import HistToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX
from lena.structures import Histogram
from read_data import ReadData
def main():
data_file = os.path.join("..", "data", "normal_3d.csv")
s = Sequence(
ReadData(),
lambda dt: (dt[0][0], dt[1]),
Histogram(mesh((-10, 10), 10)),
HistToCSV(),
MakeFilename("x"),
Writer("output"),
RenderLaTeX("histogram_1d.tex"),
Writer("output"),
LaTeXToPDF(),
PDFToPNG(),
)
results = s.run([data_file])
print(list(results))
if __name__ == "__main__":
main()
, output/, :
$ python main.py
pdflatex -halt-on-error -interaction batchmode -output-directory output output/x.tex
pdftoppm output/x.pdf output/x -png -singlefile
[(âoutput/x.pngâ, {âoutputâ: {âfiletypeâ: âpngâ}, âdataâ: {âfilenameâ: â../data/normal_3d.csvâ}, âhistogramâ: {ârangesâ: [(-10, 10)], âdimâ: 1, ânbinsâ: [10]}})]
LaTeXToPDF pdflatex, PDFToPNG pdftoppm. , LaTeX , output/x.tex ( ).
â , (run) . , , ( , ). , ( ) output/x.png.
. s ( ). ReadData (data, context), lambda , ( (data, context)).
lambda , . , .
x Histogram, (edges), (mesh) -10 10 .
, , CSV (, ). ( pdflatex) , .
MakeFilename context["output"]. Context.output.filename â ( : csv, pdf ..). , x.
Writer . . , "output".
csv, LaTeX histogram_1d.tex , pdf png. , RenderLaTeX , .
: , . Lena, .
:
from lena.context import Context
from lena.flow import Cache, End, Print
s = Sequence(
Print(),
ReadData(),
ISlice(1000),
lambda val: val[0][0],
Histogram(mesh((-10, 10), 10)),
Context(),
Cache("x_hist.pkl"),
HistToCSV(),
)
Print , . , , Print . print .
ISlice, , , . , , , .
Context â , , , . Context , , ( , ). .
Cache . â , . , Cache , , . , . Cache pickle, Python ( ). (, , ), Cache. Cache, , .
End . , Cache ( End), HistToCSV . End , .
Lena , . , , . , .
(callable) . , , . , .
. â . , .
. Sequence , . Source Sequence, .
, , , . .
End. :
class End(object):
"""Stop sequence here."""
def run(self, flow):
"""Exhaust all preceding flow and stop iteration."""
for val in flow:
pass
raise StopIteration()
main.py . ,
Traceback (most recent call last):
File âmain.pyâ, line 46, in <module>
main()
File âmain.pyâ, line 42, in main
results = s.run([data_file])
File âlena/core/sequence.pyâ, line 70, in run
flow = elem.run(flow)
File âmain.pyâ, line 24, in run
raise StopIteration()
StopIteration
, , , . , StopIteration . ?
, . , .
Count , . , . ? , .
, .
" - ",- . " CSV, , , ,⊠, , code bloat ( )."
? ?
** Sum . , , .
Sum , ? ? .
As respostas para os exercĂcios sĂŁo dadas no final do manual .
Notas de rodapé
1. Esse recurso pode ser adicionado no futuro.
2. Documentação do Jinja
3. Usando o layout do Jinja para LaTeX, foi proposta aqui e aqui , a sintaxe dos modelos foi retirada do artigo original.
Alternativas
Ruffus Ă© um pipeline computacional para Python usado em ciĂȘncia e bioinformĂĄtica. Ele conecta os componentes do programa atravĂ©s da gravação e leitura de arquivos.