@@ -3,14 +3,20 @@ $TYPEDEF
33
44Data sample data structure.
55Its 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.
6+ It has 3 main (optional) fields: features `x`, cost parameters `θ`, and solution `y`.
7+ Additionally, it has a `context` field (solver kwargs, spread into the maximizer as
8+ `maximizer(θ; sample.context...)`) and an `extra` field (non-solver data, never passed
9+ to the maximizer).
10+
11+ The separation prevents silent breakage from accidentally passing non-solver data
12+ (e.g. a scenario, reward, or step counter) as unexpected kwargs to the maximizer.
813
914# Fields
1015$TYPEDFIELDS
1116"""
1217struct DataSample{
13- I<: NamedTuple ,
18+ CTX<: NamedTuple ,
19+ EX<: NamedTuple ,
1420 F<: Union{AbstractArray,Nothing} ,
1521 S<: Union{AbstractArray,Nothing} ,
1622 C<: Union{AbstractArray,Nothing} ,
@@ -21,53 +27,81 @@ struct DataSample{
2127 θ:: C
2228 " output solution (optional)"
2329 y:: S
24- " additional information, usually the instance (optional)"
25- info:: I
30+ " context information as solver kwargs, e.g. instance, graph, etc."
31+ context:: CTX
32+ " additional data, never passed to the maximizer, e.g. scenario, objective value, reward,
33+ step count, etc. Can be used for any purpose by the user, such as plotting utilities."
34+ extra:: EX
2635end
2736
2837"""
2938$TYPEDSIGNATURES
3039
3140Constructor for `DataSample` with keyword arguments.
3241
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`).
42+ All keyword arguments beyond `x`, `θ`, `y`, and `extra` are collected into the `context`
43+ field (solver kwargs). The `extra` keyword accepts a `NamedTuple` of non-solver data.
44+
45+ Fields in `context` and `extra` must be disjoint. An error is thrown if they overlap.
46+ Both can be accessed directly via property forwarding.
3547
3648# Examples
3749```julia
50+ # Instance goes in context
3851d = DataSample(x=[1,2,3], θ=[4,5,6], y=[7,8,9], instance="my_instance")
39- d.instance # "my_instance"
52+ d.instance # "my_instance" (from context)
53+
54+ # Scenario goes in extra
55+ d = DataSample(x=x, y=y, instance=inst, extra=(; scenario=ξ))
56+ d.scenario # ξ (from extra)
57+
58+ # State goes in context, reward in extra
59+ d = DataSample(x=x, y=y, instance=state, extra=(; reward=-1.5))
60+ d.instance # state (from context)
61+ d.reward # -1.5 (from extra)
4062```
4163"""
42- function DataSample (; x= nothing , θ= nothing , y= nothing , kwargs... )
43- info = (; kwargs... )
44- return DataSample (x, θ, y, info)
64+ function DataSample (; x= nothing , θ= nothing , y= nothing , extra= NamedTuple (), kwargs... )
65+ context = (; kwargs... )
66+ overlap = intersect (keys (context), keys (extra))
67+ if ! isempty (overlap)
68+ error (" Keys $(collect (overlap)) appear in both context and extra of DataSample" )
69+ end
70+ return DataSample (x, θ, y, context, extra)
4571end
4672
4773"""
4874$TYPEDSIGNATURES
4975
5076Extended property access for `DataSample`.
5177
52- Allows accessing `info` fields directly as properties (e.g., `d.instance` instead of `d.info.instance`).
78+ Allows accessing `context` and `extra` fields directly as properties.
79+ `context` is searched first; if the key is not found there, `extra` is searched.
5380"""
5481function Base. getproperty (d:: DataSample , name:: Symbol )
55- if name in (:x , :θ , :y , :info )
82+ if name in (:x , :θ , :y , :context , :extra )
5683 return getfield (d, name)
5784 else
58- return getproperty (getfield (d, :info ), name)
85+ ctx = getfield (d, :context )
86+ if haskey (ctx, name)
87+ return getproperty (ctx, name)
88+ end
89+ return getproperty (getfield (d, :extra ), name)
5990 end
6091end
6192
6293"""
6394$TYPEDSIGNATURES
6495
65- Return all property names of a `DataSample`, including both struct fields and `info` fields.
96+ Return all property names of a `DataSample`, including both struct fields and forwarded
97+ fields from `context` and `extra`.
6698
67- This enables tab completion for all available properties, including those stored in `info` .
99+ This enables tab completion for all available properties.
68100"""
69101function Base. propertynames (d:: DataSample , private:: Bool = false )
70- return (fieldnames (DataSample)... , propertynames (getfield (d, :info ), private)... )
102+ ctx_names = propertynames (getfield (d, :context ), private)
103+ extra_names = propertynames (getfield (d, :extra ), private)
104+ return (fieldnames (DataSample)... , ctx_names... , extra_names... )
71105end
72106
73107"""
@@ -92,7 +126,11 @@ function Base.show(io::IO, d::DataSample)
92126 y_str = sprint (show, d. y; context= io_limited)
93127 push! (fields, " y_true=$y_str " )
94128 end
95- for (key, value) in pairs (d. info)
129+ for (key, value) in pairs (d. context)
130+ value_str = sprint (show, value; context= io_limited)
131+ push! (fields, " $key =$value_str " )
132+ end
133+ for (key, value) in pairs (d. extra)
96134 value_str = sprint (show, value; context= io_limited)
97135 push! (fields, " $key =$value_str " )
98136 end
@@ -116,8 +154,8 @@ Transform the features in the dataset.
116154"""
117155function StatsBase. transform (t, dataset:: AbstractVector{<:DataSample} )
118156 return map (dataset) do d
119- (; info , x, θ, y) = d
120- DataSample (StatsBase. transform (t, x), θ, y, info )
157+ (; context, extra , x, θ, y) = d
158+ DataSample (StatsBase. transform (t, x), θ, y, context, extra )
121159 end
122160end
123161
@@ -139,8 +177,8 @@ Reconstruct the features in the dataset.
139177"""
140178function StatsBase. reconstruct (t, dataset:: AbstractVector{<:DataSample} )
141179 return map (dataset) do d
142- (; info , x, θ, y) = d
143- DataSample (StatsBase. reconstruct (t, x), θ, y, info )
180+ (; context, extra , x, θ, y) = d
181+ DataSample (StatsBase. reconstruct (t, x), θ, y, context, extra )
144182 end
145183end
146184
0 commit comments