Skip to content

Commit

Permalink
structures.graph gets a public attribute dim. Improve its documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed Feb 16, 2022
1 parent 5b91cfb commit 7f338e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
49 changes: 32 additions & 17 deletions lena/structures/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ def __init__(self, points, field_names=("x", "y"), scale=None):
For example, a 3-dimensional graph could be distinguished
from a 2-dimensional graph with errors by its fields
("x", "y", "z") instead of ("x", "y", "y_err"),
or ("x", "y", "y_err_low", "y_err_high").
Field names are used to transform Lena graphs to graphs
from other libraries.
or ("E", "time", "E_err_low", "time_err_high").
Field names don't affect drawing graphs:
for that :class:`~Variable`-s should be used.
Default field names,
provided for the most used 2-dimensional graphs,
are "x" and "y".
*field_names* can be a string separated by whitespace
and/or commas
or a sequence of strings, such as ["x", "y", "y_err"].
*field_names* must be a tuple,
have as many elements as *points*,
and each field name must be unique.
Default field names are "x" and "y",
provided for the most used 2-dimensional graphs.
Error fields must go after all other coordinates.
Names of coordinate errors are those of coordinates plus "_err",
further error details are appended after '_'
Expand All @@ -66,8 +65,17 @@ def __init__(self, points, field_names=("x", "y"), scale=None):
so use them only if you understand what you are doing.
A graph can be iterated yielding tuples of numbers
for each point. Graph field names can be accessed
as its *field_names* attribute.
for each point.
**Attributes**
:attr:`field_names`
:attr:`dim` is the dimension of the graph,
that is of all its coordinates without errors.
If the initialization arguments are incorrect,
:exc:`~.LenaTypeError` or :exc:`~.LenaValueError` are raised.
"""
if not points:
raise lena.core.LenaValueError(
Expand Down Expand Up @@ -113,6 +121,20 @@ def __init__(self, points, field_names=("x", "y"), scale=None):
self._points = points
self._scale = scale

# x_error looks better than x_err,
# but x_err_low, y_err_low are much better
# than x_error_low and the same for y.
# So, for consistency and ease, we use x_err.

def get_last_coord_ind(field_names):
for ind, fn in enumerate(field_names):
if fn.endswith("_err") or "_err_" in fn:
ind -= 1
break
return ind

self.dim = get_last_coord_ind(field_names) + 1

# todo: add subsequences of points as attributes
# with field names.
# In case if someone wants to create a graph of another function
Expand Down Expand Up @@ -184,15 +206,8 @@ def scale(self, other=None):
"can't rescale a graph with zero or unknown scale"
)

def get_last_coord_ind_name(field_names):
for ind, fn in enumerate(field_names):
if fn.endswith("_err") or "_err_" in fn:
ind -= 1
break
return (ind, field_names[ind])

last_coord_ind, last_coord_name = \
get_last_coord_ind_name(self.field_names)
last_coord_ind = self.dim - 1
last_coord_name = self.field_names[last_coord_ind]

def get_err_indices(coord_name, field_names):
err_indices = []
Expand Down
5 changes: 5 additions & 0 deletions tests/structures/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_graph_structure():
# simplest 2d initialization works
gr0 = graph([xs, ys])
assert gr0.field_names == ("x", "y")
# dim works
assert gr0.dim == 2

# iteration works
assert list(gr0) == [(0, 2), (1, 3)]
Expand Down Expand Up @@ -55,6 +57,7 @@ def test_graph_structure():
# 2d graph works
gr1 = graph(copy.deepcopy([xs, ys]), scale=2)
assert gr1.scale() == 2
assert gr1.dim == 2
gr1.scale(1)
assert gr1._points == [xs, [1, 1.5]]
assert gr1.scale() == 1
Expand All @@ -64,6 +67,7 @@ def test_graph_structure():
gr2.scale(3)
assert gr2._points == [xs, ys, [1.5, 3.]]
assert gr2.scale() == 3
assert gr2.dim == 3

# graph with errors works
# x errors are unchanged, y coords change
Expand All @@ -76,6 +80,7 @@ def test_graph_structure():
# y errors and coords change
gr4 = graph(copy.deepcopy([xs, ys, [1, 2]]), field_names="x,y,y_err", scale=2)
gr4.scale(1)
assert gr4.dim == 2
assert gr4._points == [xs, [1, 1.5], [0.5, 1]]


Expand Down

0 comments on commit 7f338e8

Please sign in to comment.