Skip to content

Commit 0e44242

Browse files
Merge pull request #43 from BigThinkcode/add_concurrency
Execute concurrently
2 parents dfb8bf9 + 76353b2 commit 0e44242

File tree

10 files changed

+107
-80
lines changed

10 files changed

+107
-80
lines changed

lib/matplotex.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,6 @@ defmodule Matplotex do
773773
Figure.set_rc_params(figure, rc_params)
774774
end
775775

776-
def show({stream, figure}) do
777-
Scatter.materialize(stream, figure)
778-
|> Sketch.call()
779-
end
780-
781776
def show(figure) do
782777
figure
783778
|> Figure.materialize()

lib/matplotex/figure/areal.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ defmodule Matplotex.Figure.Areal do
210210
%__MODULE__{axes | size: frame_size}
211211
end
212212

213+
def process_concurrently(transformed, concurrency, args) do
214+
chunk_size = div(length(transformed), concurrency)
215+
216+
transformed
217+
|> Enum.chunk_every(chunk_size)
218+
|> Task.async_stream(fn part ->
219+
args = [part | args]
220+
apply(__MODULE__, :capture, args)
221+
end)
222+
|> Enum.reduce([], fn {:ok, elements}, acc ->
223+
acc ++ elements
224+
end)
225+
end
226+
213227
def set_region_title(
214228
%Figure{
215229
axes:

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ defmodule Matplotex.Figure.Areal.BarChart do
6565
},
6666
element: elements
6767
} = axes,
68-
rc_params: %RcParams{x_padding: x_padding, white_space: white_space}
68+
rc_params: %RcParams{
69+
x_padding: x_padding,
70+
white_space: white_space,
71+
concurrency: concurrency
72+
}
6973
} = figure
7074
) do
7175
x_padding_value = width_region_content * x_padding + white_space
@@ -82,7 +86,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
8286
height_region_content,
8387
{x_region_content + x_padding_value, y_region_content}
8488
)
85-
|> capture(-y_region_content)
89+
|> capture(-y_region_content, concurrency)
8690
end)
8791
|> List.flatten()
8892

@@ -114,23 +118,27 @@ defmodule Matplotex.Figure.Areal.BarChart do
114118
{min..max |> Enum.into([], fn d -> d * round_to_best(step) end), lim}
115119
end
116120

117-
defp capture(%Dataset{transformed: transformed} = dataset, bly) do
118-
capture(transformed, [], dataset, bly)
121+
def capture(%Dataset{transformed: transformed} = dataset, bly, concurrency) do
122+
if concurrency do
123+
process_concurrently(transformed, concurrency, [[], dataset, bly])
124+
else
125+
capture(transformed, [], dataset, bly)
126+
end
119127
end
120128

121-
defp capture(
122-
[{x, y} | to_capture],
123-
captured,
124-
%Dataset{
125-
color: color,
126-
width: width,
127-
pos: pos_factor,
128-
edge_color: edge_color,
129-
alpha: alpha,
130-
line_width: line_width
131-
} = dataset,
132-
bly
133-
) do
129+
def capture(
130+
[{x, y} | to_capture],
131+
captured,
132+
%Dataset{
133+
color: color,
134+
width: width,
135+
pos: pos_factor,
136+
edge_color: edge_color,
137+
alpha: alpha,
138+
line_width: line_width
139+
} = dataset,
140+
bly
141+
) do
134142
capture(
135143
to_capture,
136144
captured ++
@@ -153,7 +161,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
153161
)
154162
end
155163

156-
defp capture([], captured, _dataset, _bly), do: captured
164+
def capture([], captured, _dataset, _bly), do: captured
157165

158166
defp hypox(y) do
159167
nof_x = length(y)

lib/matplotex/figure/areal/histogram.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ defmodule Matplotex.Figure.Areal.Histogram do
6363
},
6464
element: element
6565
},
66-
rc_params: %RcParams{x_padding: x_padding, white_space: white_space}
66+
rc_params: %RcParams{
67+
x_padding: x_padding,
68+
white_space: white_space,
69+
concurrency: concurrency
70+
}
6771
}) do
6872
x_padding_value = width_region_content * x_padding + white_space
6973
shrinked_width_region_content = width_region_content - x_padding_value * 2
@@ -79,15 +83,19 @@ defmodule Matplotex.Figure.Areal.Histogram do
7983
height_region_content,
8084
{x_region_content + x_padding_value, y_region_content}
8185
)
82-
|> capture(abs(y_region_content), shrinked_width_region_content)
86+
|> capture(abs(y_region_content), shrinked_width_region_content, concurrency)
8387
end)
8488
|> List.flatten()
8589

8690
%Figure{axes: %{element: element ++ hist_elements}}
8791
end
8892

89-
defp capture(%Dataset{transformed: transformed} = dataset, bly, region_width) do
90-
capture(transformed, [], dataset, bly, region_width)
93+
defp capture(%Dataset{transformed: transformed} = dataset, bly, region_width, concurrency) do
94+
if concurrency do
95+
process_concurrently(transformed, concurrency, [[], dataset, bly, region_width])
96+
else
97+
capture(transformed, [], dataset, bly, region_width)
98+
end
9199
end
92100

93101
defp capture(

lib/matplotex/figure/areal/line_plot.ex

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ defmodule Matplotex.Figure.Areal.LinePlot do
6565
},
6666
element: elements
6767
} = axes,
68-
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
68+
rc_params: %RcParams{
69+
x_padding: x_padding,
70+
y_padding: y_padding,
71+
concurrency: concurrency
72+
}
6973
} = figure
7074
) do
7175
# {x_region_content, y_region_content} = Algebra.flip_y_coordinate({x_region_content, y_region_content})
@@ -85,7 +89,7 @@ defmodule Matplotex.Figure.Areal.LinePlot do
8589
shrinked_height_region_content,
8690
{x_region_content + x_padding_value, y_region_content + y_padding_value}
8791
)
88-
|> capture()
92+
|> capture(concurrency)
8993
end)
9094
|> List.flatten()
9195

@@ -129,19 +133,23 @@ defmodule Matplotex.Figure.Areal.LinePlot do
129133
{Ticker.generate_ticks(lim), lim}
130134
end
131135

132-
defp capture(%Dataset{transformed: transformed} = dataset) do
133-
capture(transformed, [], dataset)
136+
def capture(%Dataset{transformed: transformed} = dataset, concurrency) do
137+
if concurrency do
138+
process_concurrently(transformed, concurrency, [[], dataset])
139+
else
140+
capture(transformed, [], dataset)
141+
end
134142
end
135143

136-
defp capture(
137-
[{x1, y1} | [{x2, y2} | _] = to_capture],
138-
captured,
139-
%Dataset{
140-
color: color,
141-
marker: marker,
142-
linestyle: linestyle
143-
} = dataset
144-
) do
144+
def capture(
145+
[{x1, y1} | [{x2, y2} | _] = to_capture],
146+
captured,
147+
%Dataset{
148+
color: color,
149+
marker: marker,
150+
linestyle: linestyle
151+
} = dataset
152+
) do
145153
capture(
146154
to_capture,
147155
captured ++
@@ -162,7 +170,7 @@ defmodule Matplotex.Figure.Areal.LinePlot do
162170
)
163171
end
164172

165-
defp capture([{x, y}], captured, %Dataset{color: color, marker: marker}) do
173+
def capture([{x, y}], captured, %Dataset{color: color, marker: marker}) do
166174
captured ++ [Marker.generate_marker(marker, x, y, color, @marker_size)]
167175
end
168176
end

lib/matplotex/figure/areal/scatter.ex

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ defmodule Matplotex.Figure.Areal.Scatter do
5858
},
5959
element: elements
6060
} = axes,
61-
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
61+
rc_params: %RcParams{
62+
x_padding: x_padding,
63+
y_padding: y_padding,
64+
concurrency: concurrency
65+
}
6266
} = figure
6367
) do
6468
x_padding_value = width_region_content * x_padding
@@ -77,50 +81,31 @@ defmodule Matplotex.Figure.Areal.Scatter do
7781
shrinked_height_region_content,
7882
{x_region_content + x_padding_value, y_region_content + y_padding_value}
7983
)
80-
|> capture()
84+
|> capture(concurrency)
8185
end)
8286
|> List.flatten()
8387

8488
elements = elements ++ line_elements
8589
%Figure{figure | axes: %{axes | element: elements}}
8690
end
8791

88-
def materialize(xystream, figure) do
89-
__MODULE__.materialized_by_region(figure)
90-
|> material_stream(xystream)
92+
def capture(%Dataset{transformed: transformed} = dataset, concurrency) do
93+
if concurrency do
94+
process_concurrently(transformed, concurrency, [[], dataset])
95+
else
96+
capture(transformed, [], dataset)
97+
end
9198
end
9299

93-
def material_stream(
94-
%Figure{
95-
axes: %__MODULE__{
96-
limit: %TwoD{x: xlim, y: ylim},
97-
element: element,
98-
coords: %Coords{bottom_left: {blx, bly}},
99-
size: {width, height}
100-
}
101-
} = figure,
102-
xystream
100+
def capture(
101+
[{x, y} | to_capture],
102+
captured,
103+
%Dataset{
104+
color: color,
105+
marker: marker,
106+
marker_size: marker_size
107+
} = dataset
103108
) do
104-
{Stream.map(xystream, fn {x, y} ->
105-
{matx, maty} = transformation(x, y, xlim, ylim, width, height, {blx, bly})
106-
Marker.generate_marker("o", matx, maty, "blue", 5)
107-
end)
108-
|> Stream.concat(element), figure}
109-
end
110-
111-
defp capture(%Dataset{transformed: transformed} = dataset) do
112-
capture(transformed, [], dataset)
113-
end
114-
115-
defp capture(
116-
[{x, y} | to_capture],
117-
captured,
118-
%Dataset{
119-
color: color,
120-
marker: marker,
121-
marker_size: marker_size
122-
} = dataset
123-
) do
124109
capture(
125110
to_capture,
126111
captured ++
@@ -131,7 +116,7 @@ defmodule Matplotex.Figure.Areal.Scatter do
131116
)
132117
end
133118

134-
defp capture(_, captured, _), do: captured
119+
def capture(_, captured, _), do: captured
135120

136121
@impl Areal
137122
def with_legend_handle(

lib/matplotex/figure/dataset.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Matplotex.Figure.Dataset do
44
@default_marker "o"
55
@default_linestyle "_"
66
@default_width 0.2
7-
@default_marker_size 3.5
7+
@default_marker_size 1
88
@default_alpha 1.0
99
@line_width 2
1010

lib/matplotex/figure/rc_params.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ defmodule Matplotex.Figure.RcParams do
5252
legend_width: @default_legend_width_percentage,
5353
legend_items_orientation: @default_legend_items_orientation,
5454
x_ticks_count: nil,
55-
y_ticks_count: nil
55+
y_ticks_count: nil,
56+
concurrency: nil
5657

5758
def get_rc(%__MODULE__{} = rc_param, get_func) do
5859
apply(__MODULE__, get_func, [rc_param])

test/matplotex/figure/areal/line_plot_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ defmodule Matplotex.Figure.Areal.LinePlotTest do
1414
assert elem1.fill == "blue"
1515
assert elem1.linestyle == "_"
1616
end
17+
18+
test "materialize concurrently if rc_params contains a positive integer", %{figure: figure} do
19+
figure = Matplotex.set_rc_params(figure, concurrency: 2)
20+
assert %Figure{axes: %{element: elements}} = LinePlot.materialize(figure)
21+
[elem1 | _] = Enum.filter(elements, fn elem -> elem.type == "plot.line" end)
22+
assert elem1.fill == "blue"
23+
assert elem1.linestyle == "_"
24+
end
1725
end
1826

1927
describe "create/4" do

test/matplotex/figure/radial/pie_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Matplotex.Figure.Radial.PieTest do
4242
# Colors for the slices
4343
colors = ["lightblue", "lightgreen", "orange", "pink"]
4444
figure = Pie.create(%Figure{axes: %Pie{}}, sizes, labels: labels, colors: colors)
45-
assert %Figure{axes: %{element: element}} = figure|> Figure.materialize()
45+
assert %Figure{axes: %{element: element}} = figure |> Figure.materialize()
4646

4747
assert Enum.filter(element, fn elem -> elem.type == "pie.slice" end) |> length() ==
4848
length(sizes)

0 commit comments

Comments
 (0)