Skip to content

Commit

Permalink
Rename Writer to Write. This looks better inside a Sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed May 2, 2021
1 parent 4709cea commit d94bdec
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 105 deletions.
6 changes: 3 additions & 3 deletions docs/examples/tutorial/1_intro/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from lena.core import Sequence, Source
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX
from lena.structures import Histogram

Expand All @@ -19,9 +19,9 @@ def main():
Histogram(mesh((-10, 10), 10)),
ToCSV(),
MakeFilename("x"),
Writer("output"),
Write("output"),
RenderLaTeX("histogram_1d.tex"),
Writer("output"),
Write("output"),
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down
10 changes: 5 additions & 5 deletions docs/examples/tutorial/2_split/main1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lena.core import Sequence, Split, Source
from lena.flow import Print
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import RenderLaTeX
from lena.structures import Histogram

Expand All @@ -22,23 +22,23 @@ def main():
Histogram(mesh((-10, 10), 10)),
ToCSV(),
Print(),
Writer("output", "x"),
Write("output", "x"),
),
(
lambda vec: vec[1],
Histogram(mesh((-10, 10), 10)),
ToCSV(),
Writer("output", "y"),
Write("output", "y"),
),
# (
# lambda vec: vec[2],
# Histogram(mesh((-10, 10), 10)),
# ToCSV(),
# Writer("output", ("z", "csv")),
# Write("output", ("z", "csv")),
# ),
]),
RenderLaTeX("histogram_1d.tex", "templates"),
Writer("output"),
Write("output"),
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/tutorial/2_split/main2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lena.core import Sequence, Split, Source
from lena.structures import Histogram
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX

from read_data import ReadData
Expand Down Expand Up @@ -33,9 +33,9 @@ def main():
),
]),
ToCSV(),
Writer("output"),
Write("output"),
RenderLaTeX("histogram_1d.tex", "templates"),
Writer("output"),
Write("output"),
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/tutorial/2_split/main3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lena.core import Sequence, Split, Source
from lena.structures import Histogram
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX
from lena.variables import Variable

Expand All @@ -14,7 +14,7 @@

def main():
data_file = os.path.join("..", "data", "normal_3d.csv")
writer = Writer("output")
write = Write("output")
s = Sequence(
ReadData(),
Split([
Expand All @@ -33,9 +33,9 @@ def main():
]),
MakeFilename("{{variable.name}}"),
ToCSV(),
writer,
write,
RenderLaTeX("histogram_1d.tex", "templates"),
writer,
write,
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/tutorial/2_split/main4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from lena.core import Sequence, Split, Source
from lena.structures import Histogram
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX
from lena.variables import Variable, Compose, Combine

Expand Down Expand Up @@ -49,7 +49,7 @@ def select_template(val):

def main():
data_file = os.path.join("..", "data", "double_ev.csv")
writer = Writer("output")
write = Write("output")
s = Sequence(
ReadDoubleEvents(),
Split(
Expand All @@ -66,9 +66,9 @@ def main():
),
MakeFilename("{{variable.particle}}/{{variable.coordinate}}"),
ToCSV(),
writer,
write,
RenderLaTeX(select_template, template_path="templates"),
writer,
write,
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/tutorial/2_split/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from lena.core import Sequence, Split, Source
from lena.structures import Histogram
from lena.math import mesh
from lena.output import ToCSV, Writer, LaTeXToPDF, PDFToPNG
from lena.output import ToCSV, Write, LaTeXToPDF, PDFToPNG
from lena.output import MakeFilename, RenderLaTeX
from lena.variables import Variable

from read_data import ReadData


def main_copybuf(data_file):
writer = Writer("output")
write = Write("output")
s = Sequence(
ReadData(),
Split([
Expand All @@ -35,9 +35,9 @@ def main_copybuf(data_file):
]),
MakeFilename("{{variable.name}}"),
ToCSV(),
# writer,
# write,
# RenderLaTeX("histogram_1d.tex", "templates"),
# writer,
# write,
# LaTeXToPDF(),
# PDFToPNG(),
)
Expand Down
10 changes: 0 additions & 10 deletions docs/source/flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Flow

Chain
CountFrom
ISlice
Reverse
Slice

Expand Down Expand Up @@ -112,12 +111,3 @@ Split into bins
---------------

.. automodule:: lena.flow.split_into_bins



.. the end
.. autoclass:: ISlice
:members:
.. autoclass:: CountFrom
:special-members: __call__
3 changes: 2 additions & 1 deletion docs/source/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Output
MakeFilename
PDFToPNG
ToCSV
Writer
Write

.. not covered yet: JSONEncoder, ExtendedHistToHist
Expand All @@ -31,6 +31,7 @@ Output
.. autoclass:: ToCSV
.. autofunction:: hist1d_to_csv
.. autofunction:: hist2d_to_csv
.. autoclass:: Write
.. autoclass:: Writer

LaTeX
Expand Down
2 changes: 1 addition & 1 deletion docs/source/tutorial/1-introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ it must be written to a file.
first it is a *csv* table, then it may become a *pdf* plot, etc.)
Since there is only one file expected, we can simply call it *x*.

*Writer* element writes text data to the file system.
*Write* element writes text data to the file system.
It is initialized with the name of the output directory.
To be written, the context of a value must have an “output” subdictionary.

Expand Down
38 changes: 19 additions & 19 deletions docs/source/tutorial/2-split.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ Here is a working example:
lambda vec: vec[0],
Histogram(mesh((-10, 10), 10)),
ToCSV(),
Writer("output", "x"),
Write("output", "x"),
),
(
lambda vec: vec[1],
Histogram(mesh((-10, 10), 10)),
ToCSV(),
Writer("output", "y"),
Write("output", "y"),
),
]),
RenderLaTeX("histogram_1d.tex", "templates"),
Writer("output"),
Write("output"),
LaTeXToPDF(),
PDFToPNG(),
)
Expand All @@ -122,14 +122,14 @@ Lena Histogram is a FillCompute element.
The elements of the list in *Split* (tuples in this example)
during the initialization of *Split* are transformed into FillCompute sequences.
The *lambdas* select parts of vectors, which will fill the corresponding histogram.
After the histogram is filled, it is given appropriate name by *Writer*
After the histogram is filled, it is given appropriate name by *Write*
(so that they could be distinguished in the following flow).

..
.. admonition:: Writer
.. admonition:: Write
*Writer* has two initialization parameters: the default directory and the default file name.
Writer only writes strings (and *unicode* in Python 2).
*Write* has two initialization parameters: the default directory and the default file name.
Write only writes strings (and *unicode* in Python 2).
Its corresponding context is called *output* (as its module).
If *output* is missing in the context, values pass unchanged.
Otherwise, file name and extension are searched in *context.output*.
Expand All @@ -138,14 +138,14 @@ then the default file name or "txt" are used.
The default file name should be used only when you are sure
that only one file is going to be written, otherwise it will be rewritten
every time.
The defaults *Writer*'s parameters are empty string (current directory)
The defaults *Write*'s parameters are empty string (current directory)
and "output" (resulting in *output.txt*).

*ToCSV* yields a string and sets *context.output.fileext* to *"csv"*.
In the example above Writer objects write CSV data to *output/x.csv*
In the example above Write objects write CSV data to *output/x.csv*
and *output/y.csv*.

For each file written, *Writer* yields a tuple *(file path, context)*,
For each file written, *Write* yields a tuple *(file path, context)*,
where *context.output.filepath* is updated with the path to file.

After the histograms are filled and written,
Expand All @@ -160,8 +160,8 @@ One of the basic principles in programming is

In the example above, we wanted to give distinct names to histograms
in different analysis branches,
and used two *writers* to do that.
However, we can move *ToCSV* and *Writer*
and used two *writes* to do that.
However, we can move *ToCSV* and *Write*
outside the *Split* (and make our code one line shorter):

.. _main2_py:
Expand All @@ -185,12 +185,12 @@ outside the *Split* (and make our code one line shorter):
),
]),
ToCSV(),
Writer("output"),
Write("output"),
# ... as earlier ...
)
Element *MakeFilename* adds file name to *context.output*.
*Writer* doesn't need a default file name anymore.
*Write* doesn't need a default file name anymore.
Now it writes two different files,
because *context.output.filename* is different.

Expand All @@ -212,7 +212,7 @@ but let us unite them in one element (and improve the *cohesion* of our code):
def main():
data_file = os.path.join("..", "data", "normal_3d.csv")
writer = Writer("output")
write = Write("output")
s = Sequence(
ReadData(),
Split([
Expand All @@ -231,9 +231,9 @@ but let us unite them in one element (and improve the *cohesion* of our code):
]),
MakeFilename("{{variable.name}}"),
ToCSV(),
writer,
write,
RenderLaTeX("histogram_1d.tex", "templates"),
writer,
write,
LaTeXToPDF(),
PDFToPNG(),
)
Expand Down Expand Up @@ -267,7 +267,7 @@ and is available as a method *getter*.
which take arguments from context.
In our example, *MakeFilename("{{variable.name}}")* creates file name from *context.variable.name*.

Note also that since two *Writers* do the same thing, we rewrote them as one object.
Note also that since two *Writes* do the same thing, we rewrote them as one object.

Combine
^^^^^^^
Expand Down Expand Up @@ -651,7 +651,7 @@ that your analysis will remain correct.

There are several things in Lena that help against context interference:

- elements change their own context (*Writer* changes *context.output* and not *context.variable*),
- elements change their own context (*Write* changes *context.output* and not *context.variable*),
- if *Split* has several sequences, it makes a deep copy of the flow before feeding that to them,
- *FillCompute* and *FillRequest* elements make a deep copy of context before yielding [#f3]_.

Expand Down
6 changes: 3 additions & 3 deletions docs/source/tutorial/answers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ A simple output function could be the following:
.. code-block:: python
def output(output_dir="output"):
writer = lena.output.Writer(output_dir)
write = lena.output.Write(output_dir)
s = lena.core.Sequence(
lena.output.ToCSV(),
writer,
write,
lena.context.Context(),
lena.output.RenderLaTeX(), # initialize properly here
writer,
write,
lena.output.LaTeXToPDF(),
lena.output.PDFToPNG(),
)
Expand Down
8 changes: 4 additions & 4 deletions lena/flow/group_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
),
UpdateContext("output.plot.name", "{{variable.name}}",
raise_on_missing=True),
writer,
write,
# Several prints were used during this code creation
# Print(transform=lambda val: val[1]["plot"]["name"]),
),
Expand All @@ -51,13 +51,13 @@
# non-combined plots will still need file names
MakeFilename("{{variable.name}}"),
lena.output.ToCSV(),
writer,
write,
lena.context.Context(),
# here our jinja template renders a group as a list of items
lena.output.RenderLaTeX(template_path=TEMPLATE_PATH,
select_template=select_template),
# we have a single template, no more groups are present
writer,
write,
lena.output.LaTeXToPDF(),
)
"""
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self, group_by, select, transform=(), scale=None,
*transform* is a sequence, which processes individual plots
before yielding.
For example, set ``transform=(ToCSV(), writer)``.
For example, set ``transform=(ToCSV(), write)``.
*transform* is called after *scale*.
*scale* is a number or a string.
Expand Down

0 comments on commit d94bdec

Please sign in to comment.