Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort dataset variables and add TimeArray example #437

Merged
merged 3 commits into from
Sep 12, 2024
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
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
SkipNan = "aed68c70-c8b0-4309-8cd1-d392a74f991a"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e"
WGLMakie = "276b4fcb-3e11-5398-bf8b-a0c2d153d008"
WeightedOnlineStats = "bbac0a1f-7c9d-5672-960b-c6ca726e5d5d"
YAXArrayBase = "90b8fcef-0c2d-428d-9c56-5f86629e9d14"
Expand Down
48 changes: 47 additions & 1 deletion docs/src/UserGuide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ We can also use more than one criteria for grouping the values. In the next exam
fitcube = cubefittable(t, Mean, :values, by=(:classes, :time))
````

## How do I assing variable names to `YAXArrays` in a `Dataset`
## How do I assign variable names to `YAXArrays` in a `Dataset`

### One variable name

Expand All @@ -288,3 +288,49 @@ nothing # hide
````@ansi howdoi
ds = YAXArrays.Dataset(; (keylist .=> varlist)...)
````

## Ho do I construct a `Dataset` from a TimeArray

In this section we will use `MarketData.jl` and `TimeSeries.jl` to simulate some stocks.

````@example howdoi
using YAXArrays, DimensionalData
using MarketData, TimeSeries

stocks = Dict(:Stock1 => random_ohlcv(), :Stock2 => random_ohlcv(), :Stock3 => random_ohlcv())
d_keys = keys(stocks)
````

currently there is not direct support to obtain `dims` from a `TimeArray`, but we can code a function for it

````@example howdoi
getTArrayAxes(ta::TimeArray) = (Dim{:time}(timestamp(ta)), Dim{:variable}(colnames(ta)), );
nothing # hide
````
then, we create the `YAXArrays` as

````@example howdoi
yax_list = [YAXArray(getTArrayAxes(stocks[k]), values(stocks[k])) for k in d_keys];
nothing # hide
````

and a `Dataset` with all `stocks` names

````@ansi howdoi
ds = Dataset(; (d_keys .=> yax_list)...)
````

and, it looks like there some small differences in the axes, they are being printed independently although they should be the same. Well, they are at least at the `==` level but not at `===`. We could use the axes from one `YAXArray` as reference and `rebuild` all the others

````@example howdoi
yax_list = [rebuild(yax_list[1], values(stocks[k])) for k in d_keys];
nothing # hide
````

and voilà

````@ansi howdoi
ds = Dataset(; (d_keys .=> yax_list)...)
````

now they are printed together, showing that is exactly the same axis structure for all variables.
4 changes: 2 additions & 2 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function Base.show(io::IO, ds::Dataset)
if !isempty(variables_with_shared_axes_only)
printstyled(io, "Variables: ", color=:light_blue)
print(io, "\n")
println(io, join(variables_with_shared_axes_only, ", "))
println(io, join(sort(variables_with_shared_axes_only), ", "))
println(io)
end

Expand All @@ -146,7 +146,7 @@ function Base.show(io::IO, ds::Dataset)
end
printstyled(io, " Variables: ", color=:light_blue)
padding = " " ^ 2 # Adjust this number to match the length of " Variables: "
variables_str = join(variables, ", ")
variables_str = join(sort(variables), ", ")
padded_variables = padding * variables_str
print(io, "\n")
println(io, padded_variables)
Expand Down
Loading