JuliaAI / MLJModelInterface.jl

Lightweight package to interface with MLJ
MIT License
37 stars 8 forks source link

`Machine` abstract type? #188

Closed MilesCranmer closed 8 months ago

MilesCranmer commented 8 months ago

Hey all,

I want to create a default plot interface for a fitted regressor in SymbolicRegression.jl. However I'm not sure what type to write it for. Is there a good abstract type I can specialize to for creating plots?

e.g.,

julia> typeof(mach)
Machine{SRRegressor{DynamicQuantities.SymbolicDimensions{DynamicQuantities.FixedRational{Int32, 25200}}, DataType, false}, true}

I would like to create a method for

julia> plot(mach)

One way is by creating an extension to MLJBase but I'm wondering if there's just an abstract type for, say, AbstractMachine{SRRegressor}.

Cheers, Miles

ablaom commented 8 months ago

You raise a good question here. Currently we have a plot recipe for machines bound to TunedModels; see here, but this happens in MLJTuning which already has MLJBase as a dep. This was probably an unfortunate design.

I hadn't thought of adding an AbstractMachine type to MLJModelInterface. I tend to think of machines as a completely separate, higher level interface. Another idea is to implement your recipe for thefitresult returned by your implementation of MLJModelInterface.fit (in SymbolicRegression.jl) and we add a generic overloading of plot recipes for machines in MLJBase; something like:

PlotRecipes.plot(mach::Machine) = plot(machine.fitresult)

What do you think?

MilesCranmer commented 8 months ago

I hadn't thought of adding an AbstractMachine type to MLJModelInterface. I tend to think of machines as a completely separate, higher level interface. Another idea is to implement your recipe for thefitresult returned by your implementation of MLJModelInterface.fit (in SymbolicRegression.jl) and we add a generic overloading of plot recipes for machines in MLJBase; something like:

PlotRecipes.plot(mach::Machine) = plot(machine.fitresult)

What do you think?

That’s a good way to do it! I would be quite content with this.

MilesCranmer commented 8 months ago

Quick addendum: I think it should instead be:

@recipe function default_machine_plot(mach::Machine)
    mach.fitresult
end

which is the idiomatic way to do it https://docs.juliaplots.org/latest/RecipesBase/syntax/ and will automatically forward all plotting information.

And it should be RecipesBase which is the interface package (rather than PlotRecipes)