Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sample_data/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
trendify
trendify
good_result
2 changes: 1 addition & 1 deletion sample_data/models/0/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/1/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/2/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/3/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/4/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/5/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/6/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/7/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/8/data_product.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample_data/models/9/data_product.json

Large diffs are not rendered by default.

109 changes: 82 additions & 27 deletions src/trendify/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ class HistogramStyle(HashableBase):
color: str = "k"
label: str | None = None
histtype: str = "stepfilled"
alpha_edge: float = 1
alpha_edge: float = 0
alpha_face: float = 0.3
linewidth: float = 2
bins: int | list[int] | Tuple[int] | NDArray[Shape["*"], int] | None = None
Expand Down Expand Up @@ -1253,6 +1253,8 @@ def process_collection(
# [tag] = collection.get_tags()
# except:
# breakpoint()
saf: SingleAxisFigure | None = None
format_2ds: list[Format2D] = []

if not no_tables:

Expand Down Expand Up @@ -1281,14 +1283,31 @@ def process_collection(

if points or traces or axlines: # Update condition
print(f"\n\nMaking xy plot for {tag = }\n")
XYDataPlotter.handle_points_and_traces(
saf = XYDataPlotter.handle_points_and_traces(
tag=tag,
points=points,
traces=traces,
axlines=axlines, # Add this parameter
dir_out=dir_out,
dpi=dpi,
saf=saf,
)

format_2ds += [
p.format2d
for p in points
if isinstance(p.format2d, Format2D)
]
format_2ds += [
t.format2d
for t in traces
if isinstance(t.format2d, Format2D)
]
format_2ds += [
a.format2d
for a in axlines
if isinstance(a.format2d, Format2D)
]
print(f"\nFinished xy plot for {tag = }\n")

# traces: List[Trace2D] = collection.get_products(tag=tag, object_type=Trace2D).elements
Expand All @@ -1311,14 +1330,34 @@ def process_collection(

if histogram_entries:
print(f"\n\nMaking histogram for {tag = }\n")
Histogrammer.handle_histogram_entries(
saf = Histogrammer.handle_histogram_entries(
tag=tag,
histogram_entries=histogram_entries,
dir_out=dir_out,
dpi=dpi,
saf=saf,
)

format_2ds += [
h.format2d
for h in histogram_entries
if isinstance(h.format2d, Format2D)
]
print(f"\nFinished histogram for {tag = }\n")

if isinstance(saf, SingleAxisFigure):
formats = list(set(format_2ds))
format2d = Format2D.union_from_iterable(formats)
saf.apply_format(format2d)

save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(
".jpg"
)
save_path.parent.mkdir(exist_ok=True, parents=True)
print(f"Saving to {save_path}")
saf.savefig(save_path, dpi=dpi)
del saf

@classmethod
def make_grafana_panels(
cls,
Expand Down Expand Up @@ -1570,6 +1609,7 @@ def handle_points_and_traces(
axlines: List[AxLine], # Add this parameter
dir_out: Path,
dpi: int,
saf: SingleAxisFigure | None = None,
):
"""
Plots points, traces, and axlines, formats figure, saves figure, and closes matplotlinb figure.
Expand All @@ -1583,7 +1623,8 @@ def handle_points_and_traces(
dpi (int): resolution of plot
"""

saf = SingleAxisFigure.new(tag=tag)
if saf is None:
saf = SingleAxisFigure.new(tag=tag)

if points:
markers = set([p.marker for p in points])
Expand All @@ -1601,16 +1642,25 @@ def handle_points_and_traces(
for axline in axlines:
axline.plot_to_ax(saf.ax)

formats = list(set([p.format2d for p in points] + [t.format2d for t in traces]))
format2d = Format2D.union_from_iterable(formats)
saf.apply_format(format2d)
# formats = list(
# set(
# [p.format2d for p in points]
# + [t.format2d for t in traces]
# + [a.format2d for a in axlines]
# )
# )

# format2d = Format2D.union_from_iterable(formats)
# saf.apply_format(format2d)
# saf.ax.autoscale(enable=True, axis='both', tight=True)

save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
save_path.parent.mkdir(exist_ok=True, parents=True)
print(f"Saving to {save_path = }")
saf.savefig(path=save_path, dpi=dpi)
del saf
# save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
# save_path.parent.mkdir(exist_ok=True, parents=True)
# print(f"Saving to {save_path = }")
# saf.savefig(path=save_path, dpi=dpi)
# del saf

return saf


class TableBuilder:
Expand Down Expand Up @@ -1801,7 +1851,8 @@ def handle_histogram_entries(
histogram_entries: List[HistogramEntry],
dir_out: Path,
dpi: int,
):
saf: SingleAxisFigure | None = None,
) -> SingleAxisFigure:
"""
Histograms the provided entries. Formats and saves the figure. Closes the figure.

Expand All @@ -1811,7 +1862,8 @@ def handle_histogram_entries(
dir_out (Path): Directory to which the generated histogram will be stored
dpi (int): resolution of plot
"""
saf = SingleAxisFigure.new(tag=tag)
if saf is None:
saf = SingleAxisFigure.new(tag=tag)

histogram_styles = set([h.style for h in histogram_entries])
for s in histogram_styles:
Expand All @@ -1822,19 +1874,22 @@ def handle_histogram_entries(
else:
saf.ax.hist(values)

try:
format2d_set = set([h.format2d for h in histogram_entries]) - {None}
[format2d] = format2d_set
saf.apply_format(format2d=format2d)
except:
print(
f"Format not applied to {save_path = } multiple entries conflict for given tag:\n\t{format2d_set = }"
)
save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
save_path.parent.mkdir(exist_ok=True, parents=True)
print(f"Saving to {save_path}")
saf.savefig(save_path, dpi=dpi)
del saf
# save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
# try:
# format2d_set = set([h.format2d for h in histogram_entries]) - {None}
# [format2d] = format2d_set
# saf.apply_format(format2d=format2d)
# except:
# print(
# f"Format not applied to {save_path = } multiple entries conflict for given tag:\n\t{format2d_set = }"
# )
# save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
# save_path.parent.mkdir(exist_ok=True, parents=True)
# print(f"Saving to {save_path}")
# saf.savefig(save_path, dpi=dpi)
# del saf

return saf


### Runners
Expand Down
6 changes: 3 additions & 3 deletions src/trendify/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def example_data_product_generator(workdir: Path) -> trendify.ProductList:

trendify.AxLine(
tags=["histogram"],
value=series.min(),
value=series.mean(),
orientation=trendify.LineOrientation.VERTICAL,
pen=trendify.Pen(color="b"),
)
pen=trendify.Pen(color="b", label="mean"),
).append_to_list(products)

return products

Expand Down
Loading