22$TYPEDEF
33
44Data sample data structure.
5+ Its main purpose is to store datasets generated by the benchmarks.
6+ It has 3 main fields: features `x`, cost parameters `θ` and solution `y`.
7+ Additionally, it has an `info` field to store any additional information as a `NamedTuple`, usually the instance, but can be used for anything else.
58
69# Fields
710$TYPEDFIELDS
811"""
9- @kwdef struct DataSample{
10- I,
12+ struct DataSample{
13+ I<: NamedTuple ,
1114 F<: Union{AbstractArray,Nothing} ,
1215 S<: Union{AbstractArray,Nothing} ,
1316 C<: Union{AbstractArray,Nothing} ,
1417}
1518 " input features (optional)"
16- x:: F = nothing
19+ x:: F
1720 " intermediate cost parameters (optional)"
18- θ:: C = nothing
21+ θ:: C
1922 " output solution (optional)"
20- y:: S = nothing
23+ y:: S
2124 " additional information, usually the instance (optional)"
22- info:: I = nothing
25+ info:: I
2326end
2427
28+ """
29+ $TYPEDSIGNATURES
30+
31+ Constructor for `DataSample` with keyword arguments.
32+
33+ Additional keyword arguments beyond `x`, `θ`, and `y` are stored in the `info` field
34+ and can be accessed directly (e.g., `data.instance` instead of `data.info.instance`).
35+
36+ # Examples
37+ ```julia
38+ d = DataSample(x=[1,2,3], θ=[4,5,6], y=[7,8,9], instance="my_instance")
39+ d.instance # "my_instance"
40+ ```
41+ """
42+ function DataSample (; x= nothing , θ= nothing , y= nothing , kwargs... )
43+ info = (; kwargs... )
44+ return DataSample (x, θ, y, info)
45+ end
46+
47+ """
48+ $TYPEDSIGNATURES
49+
50+ Extended property access for `DataSample`.
51+
52+ Allows accessing `info` fields directly as properties (e.g., `d.instance` instead of `d.info.instance`).
53+ """
54+ function Base. getproperty (d:: DataSample , name:: Symbol )
55+ if name in (:x , :θ , :y , :info )
56+ return getfield (d, name)
57+ else
58+ return getproperty (getfield (d, :info ), name)
59+ end
60+ end
61+
62+ """
63+ $TYPEDSIGNATURES
64+
65+ Return all property names of a `DataSample`, including both struct fields and `info` fields.
66+
67+ This enables tab completion for all available properties, including those stored in `info`.
68+ """
69+ function Base. propertynames (d:: DataSample , private:: Bool = false )
70+ return (fieldnames (DataSample)... , propertynames (getfield (d, :info ), private)... )
71+ end
72+
73+ """
74+ $TYPEDSIGNATURES
75+
76+ Display a `DataSample` with truncated array representations for readability.
77+
78+ Large arrays are automatically truncated with ellipsis (`...`), similar to standard Julia array printing.
79+ """
2580function Base. show (io:: IO , d:: DataSample )
2681 fields = String[]
82+ io_limited = IOContext (io, :limit => true , :compact => true )
2783 if ! isnothing (d. x)
28- push! (fields, " x=$(d. x) " )
84+ x_str = sprint (show, d. x; context= io_limited)
85+ push! (fields, " x=$x_str " )
2986 end
3087 if ! isnothing (d. θ)
31- push! (fields, " θ_true=$(d. θ) " )
88+ θ_str = sprint (show, d. θ; context= io_limited)
89+ push! (fields, " θ_true=$θ_str " )
3290 end
3391 if ! isnothing (d. y)
34- push! (fields, " y_true=$(d. y) " )
92+ y_str = sprint (show, d. y; context= io_limited)
93+ push! (fields, " y_true=$y_str " )
3594 end
36- if ! isnothing (d. info)
37- push! (fields, " instance=$(d. info) " )
95+ for (key, value) in pairs (d. info)
96+ value_str = sprint (show, value; context= io_limited)
97+ push! (fields, " $key =$value_str " )
3898 end
3999 return print (io, " DataSample(" , join (fields, " , " ), " )" )
40100end
@@ -57,7 +117,7 @@ Transform the features in the dataset.
57117function StatsBase. transform (t, dataset:: AbstractVector{<:DataSample} )
58118 return map (dataset) do d
59119 (; info, x, θ, y) = d
60- DataSample (; info, x = StatsBase. transform (t, x), θ, y)
120+ DataSample (StatsBase. transform (t, x), θ, y, info )
61121 end
62122end
63123
@@ -80,7 +140,7 @@ Reconstruct the features in the dataset.
80140function StatsBase. reconstruct (t, dataset:: AbstractVector{<:DataSample} )
81141 return map (dataset) do d
82142 (; info, x, θ, y) = d
83- DataSample (; info, x = StatsBase. reconstruct (t, x), θ, y)
143+ DataSample (StatsBase. reconstruct (t, x), θ, y, info )
84144 end
85145end
86146
0 commit comments