Skip to content

Commit 86e12b9

Browse files
committed
Update livebook and Mix env
1 parent 3321105 commit 86e12b9

File tree

2 files changed

+47
-61
lines changed

2 files changed

+47
-61
lines changed

lib/exgboost/plotting/styles.ex

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
defmodule EXGBoost.Plotting.Styles do
2-
@bst File.cwd!() |> Path.join("test/data/model.json") |> EXGBoost.read_model()
1+
Mix.env() == :docs &&
2+
defmodule EXGBoost.Plotting.Styles do
3+
@bst File.cwd!() |> Path.join("test/data/model.json") |> EXGBoost.read_model()
34

4-
@moduledoc """
5-
<div class="vega-container">
6-
#{Enum.map(EXGBoost.Plotting.get_styles(), fn {name, _style} -> """
7-
<div class="vega-item">
8-
<h2>#{name}</h2>
9-
<pre>
10-
<code class="vega-lite">
11-
#{EXGBoost.plot_tree(@bst, style: name, height: 200, width: 300).spec |> Jason.encode!()}
12-
</code>
13-
</pre>
5+
@moduledoc """
6+
<div class="vega-container">
7+
#{Enum.map(EXGBoost.Plotting.get_styles(), fn {name, _style} -> """
8+
<div class="vega-item">
9+
<h2>#{name}</h2>
10+
<pre>
11+
<code class="vega-lite">
12+
#{EXGBoost.plot_tree(@bst, style: name, height: 200, width: 300).spec |> Jason.encode!()}
13+
</code>
14+
</pre>
15+
</div>
16+
""" end) |> Enum.join("\n\n")}
1417
</div>
15-
""" end) |> Enum.join("\n\n")}
16-
</div>
17-
"""
18-
19-
@functions [{:a, "a"}, {:b, "b"}, {:c, "c"}]
20-
for {name, ret} <- @functions do
21-
def unquote(name)() do
22-
unquote(ret)
23-
end
18+
"""
2419
end
25-
end

notebooks/plotting.livemd

+31-39
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
# Plotting in EXGBoost
22

33
```elixir
4-
# Mix.install(
5-
# [
6-
# {:exgboost, "~> 0.5", env: :dev}
7-
# ]
8-
# )
9-
104
Mix.install([
11-
{:exgboost, path: "/Users/andres/Documents/exgboost"},
12-
{:nx, "~> 0.5"},
5+
{:exgboost, "~> 0.5"},
136
{:scidata, "~> 0.1"},
14-
{:scholar, "~> 0.1"},
157
{:kino_vega_lite, "~> 0.1"}
168
])
179

@@ -32,24 +24,24 @@ This notebook will go over some of the details of the `EXGBoost.Plotting` module
3224

3325
There are 2 main APIs exposed to control plotting in `EXGBoost`:
3426

35-
* Top-level API (`EXGBoost.plot_tree/2`)
27+
- Top-level API (`EXGBoost.plot_tree/2`)
3628

37-
* Using predefined styles
38-
* Defining custom styles
39-
* Mix of the first 2
29+
- Using predefined styles
30+
- Defining custom styles
31+
- Mix of the first 2
4032

41-
* `EXBoost.Plotting` module API
33+
- `EXBoost.Plotting` module API
4234

43-
* Use the Vega `data` spec defined in `EXGBoost.get_data_spec/2`
44-
* Define your own Vega spec using the data from either `EXGBoost.Plotting.to_tabular/1` or some other means
35+
- Use the Vega `data` spec defined in `EXGBoost.Plotting.get_data_spec/2`
36+
- Define your own Vega spec using the data from either `EXGBoost.Plotting.to_tabular/1` or some other means
4537

4638
We will walk through each of these in detail.
4739

4840
Regardless of which API you choose to use, it is helpful to understand how the plotting module works (althought the higher-level API you choose to work with the less important it becomes).
4941

5042
## Implementation Details
5143

52-
The plotting functionality provided in `EXGBoost` is powered by the [Vega](https://vega.github.io/vega/) JavaScript library and the Elixir [`VegaLite`](https://hexdocs.pm/vega_lite/VegaLite.html) library which provides the piping to interop with the JavaScript libraries. **We do not actually much use the Elixir API provided by the Elixir VegaLite library. It is mainly used for the purposes of rendering.**
44+
The plotting functionality provided in `EXGBoost` is powered by the [Vega](https://vega.github.io/vega/) JavaScript library and the Elixir [`VegaLite`](https://hexdocs.pm/vega_lite/VegaLite.html) library which provides the piping to interop with the JavaScript libraries. **We do not actually much use the Elixir API provided by the Elixir VegaLite library. It is mainly used for the purposes of rendering.**
5345

5446
Vega is a plotting library built on top of the very powerful [D3](https://d3js.org/) JavaScript library. Vega visualizations are defined according to the respective JSON Schema specification. Vega-Lite offers a [reduced schema](https://vega.github.io/schema/vega-lite/v5.json) compared to the [full Vega spec](https://vega.github.io/schema/vega/v5.json). `EXGBoost.Plotting` leverages several transforms which are not available in the reduced Vega-Lite schema, which is the reason for targeting the lower-level API.
5547

@@ -77,7 +69,7 @@ y_test = Nx.tensor(y_test)
7769

7870
## Train Your Booster
7971

80-
Now go ahead and train your booster. We will use `early_stopping_rounds: 1` because we're not interested in the accuracy of the booster for this demonstration (*Note that we need to set `evals` to use early stopping*).
72+
Now go ahead and train your booster. We will use `early_stopping_rounds: 1` because we're not interested in the accuracy of the booster for this demonstration (_Note that we need to set `evals` to use early stopping_).
8173

8274
You will notice that `EXGBoost` also provides an implementation for `Kino.Render` so that `EXGBoost.Booster`s are rendered as a plot by default.
8375

@@ -120,20 +112,20 @@ This API uses [Vega `Mark`s](https://vega.github.io/vega/docs/marks/) to describ
120112

121113
The plot is composed of the following parts:
122114

123-
* Top-level keys: Options controlling parts of the plot outside of direct control of a `Mark`, such as `:padding`, `:autosize`, etc. Accepts any Vega top-level [top-level key](https://vega.github.io/vega/docs/specification/) in addition to several specific to this API (scuh as `:style` and `:depth`).
124-
* `:leaves`: `Mark` specifying the leaf nodes of the tree
125-
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
126-
* `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
127-
* `:splits` `Mark` specifying the split (or inner / decision) nodes of the tree
128-
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
129-
* `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
130-
* `:children`: [Text Mark](https://vega.github.io/vega/docs/marks/text/) for the child count
131-
* `:yes`
132-
* `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
133-
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
134-
* `:no`
135-
* `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
136-
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
115+
- Top-level keys: Options controlling parts of the plot outside of direct control of a `Mark`, such as `:padding`, `:autosize`, etc. Accepts any Vega top-level [top-level key](https://vega.github.io/vega/docs/specification/) in addition to several specific to this API (scuh as `:style` and `:depth`).
116+
- `:leaves`: `Mark` specifying the leaf nodes of the tree
117+
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
118+
- `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
119+
- `:splits` `Mark` specifying the split (or inner / decision) nodes of the tree
120+
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
121+
- `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
122+
- `:children`: [Text Mark](https://vega.github.io/vega/docs/marks/text/) for the child count
123+
- `:yes`
124+
- `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
125+
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
126+
- `:no`
127+
- `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
128+
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
137129

138130
`EXGBoost.plot_tree/2` defaults to outputting a `VegaLite` struct. If you pass the `:path` option it will save to a file instead.
139131

@@ -348,7 +340,7 @@ For example, if you just want to change the default pre-configured style you can
348340
Mix.install([
349341
{:exgboost, path: Path.join(__DIR__, ".."), env: :dev},
350342
],
351-
config:
343+
config:
352344
[
353345
exgboost: [
354346
plotting: [
@@ -363,7 +355,7 @@ You can also make one-off changes to any of the settings with this method. In ef
363355
<!-- livebook:{"force_markdown":true} -->
364356

365357
```elixir
366-
default_style =
358+
default_style =
367359
[
368360
style: nil,
369361
background: "#3f3f3f",
@@ -398,7 +390,7 @@ You can also make one-off changes to any of the settings with this method. In ef
398390
Mix.install([
399391
{:exgboost, path: Path.join(__DIR__, ".."), env: :dev},
400392
],
401-
config:
393+
config:
402394
[
403395
exgboost: [
404396
plotting: default_style,
@@ -407,7 +399,7 @@ config:
407399
)
408400
```
409401

410-
**NOTE: When you specify a parameter in the configuration, it is merged with the defaults which is different from runtime behavior.**
402+
**NOTE: When you specify a parameter in the configuration, it is merged with the defaults which is different from runtime behavior.**
411403

412404
At any point, you can check what your default settings are by using `EXGBoost.Plotting.get_defaults/0`
413405

@@ -445,8 +437,8 @@ EXGBoost.Plotting.get_data_spec(booster, rankdir: :bt)
445437

446438
The Vega fields which are not included with `get_data_spec/2` and are included in `plot/2` are:
447439

448-
* [Marks](https://vega.github.io/vega/docs/marks/)
449-
* [Scales](https://vega.github.io/vega/docs/scales/)
450-
* [Signals](https://vega.github.io/vega/docs/signals/)
440+
- [Marks](https://vega.github.io/vega/docs/marks/)
441+
- [Scales](https://vega.github.io/vega/docs/scales/)
442+
- [Signals](https://vega.github.io/vega/docs/signals/)
451443

452444
You can make a completely valid plot using only the Data from `get_data_specs/2` and adding the marks you need.

0 commit comments

Comments
 (0)