Code - Quasi Newton Problem
This is the code for Quasi Newton Problem. For this, I am getting an error
MethodError: no method matching isless(::Matrix{Float64}, ::Matrix{Float64})
Closest candidates are:
isless(::Any, ::Missing) at missing.jl:88
isless(::Missing, ::Any) at missing.jl:87
Stacktrace:
[1] <(x::Matrix{Float64}, y::Matrix{Float64})
# Base .\operators.jl:279
[2] >(x::Matrix{Float64}, y::Matrix{Float64})
# Base .\operators.jl:305
[3] bracket_minimum(f::var"#45#46"{typeof(k), Matrix{Float64}, Matrix{Float64}}, x::Int64; s::Float64, k::Float64)
# Main .\In[122]:12
[4] bracket_minimum(f::Function, x::Int64) (repeats 2 times)
# Main .\In[122]:10
[5] line_search(f::typeof(k), x::Matrix{Float64}, d::Matrix{Float64})
# Main .\In[122]:35
[6] step!(M::BFGS, f::Function, βf::typeof(l), x::Matrix{Float64})
# Main .\In[122]:48
[7] quasi_method(f::typeof(k), g::Function, x0::Matrix{Float64})
# Main .\In[122]:67
[8] top-level scope
# In[128]:3
[9] eval
# .\boot.jl:360 [inlined]
[10] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
# Base .\loading.jl:1116
I still couldn't figure what's the issue with line 10 and 12. So help me out here? Maybe it's due to Matrix we are using or some other issues, I couldn't debug.
You forgot to vectorize your code.
See the example below for reference:
julia> a=[1. 2.; 3. 4.]; b=[1. 3.; 5. 7.];
julia> a < b
ERROR: MethodError: no method matching isless(::Matrix{Float64}, ::Matrix{Float64})
Now let us add dot to vectorize:
julia> a .< b
2Γ2 BitMatrix:
0 1
1 1
If you want to check if all elements are lower use all:
julia> all(a .< b)
false
In your code bracket_minimum does the comparison yc > yb and those values are matrices while your code expects scalars.
Related
I'm trying to call this macro from within a method where the parameters to the macro are passed in to the method. It works fine when I call it directly but there is something about the macro expansion which is preventing the variables lat and lon from being correctly used in the macro.
The macro I'm calling is #select here: https://github.com/Alexander-Barth/NCDatasets.jl/blob/4e35e843a53cdcff7f7ef66ebc3ceab1ee1e860b/src/select.jl#L54-L168
and here is the function where the lat and lon variables are not being expanded correctly
function data_for_lat_lon(ds, region, lat_lon_pair)
println("have latlon piar ", lat_lon_pair)
lat = lat_lon_pair[1]
lon = lat_lon_pair[2]
data = []
if(ArchGDAL.contains(region[1], ArchGDAL.createpoint(lon, lat)))
println(lat, " ", lon)
#the below call fails when called in this way
single_lat_lon = NCDatasets.#select(ds, latitude==$lat && longitude==$lon)
for (varname, var) in single_lat_lon
if varname in ["latitude", "longitude", "time"]
continue
end
push!(var_names, varname)
push!(data, Array[single_lat_lon[varname]][1][:])
end
return reduce(hcat, data)'
end
end
This is the error & stack trace I get when calling it:
MethodError: no method matching (::NCDatasets.var"#154#155")(::Float64)
The applicable method may be too new: running in world age 32645, while current world is 32646.
Closest candidates are:
(::NCDatasets.var"#154#155")(::Any) at none:0 (method too new to be called from this world context.)
Stacktrace:
[1] _broadcast_getindex_evalf
# .\broadcast.jl:670 [inlined]
[2] _broadcast_getindex
# .\broadcast.jl:643 [inlined]
[3] getindex
# .\broadcast.jl:597 [inlined]
[4] copy
# .\broadcast.jl:899 [inlined]
[5] materialize
# .\broadcast.jl:860 [inlined]
[6] findall(testf::NCDatasets.var"#154#155", A::Vector{Float64})
# Base .\array.jl:2311
[7] macro expansion
# C:\Users\scott\.julia\packages\NCDatasets\sLdiM\src\select.jl:242 [inlined]
[8] data_for_lat_lon(ds::NCDatasets.MFDataset{DeferDataset, 1, String, NCDatasets.DeferAttributes, NCDatasets.DeferDimensions, NCDatasets.DeferGroups}, region::DataFrameRow{DataFrame, DataFrames.Index}, lat_lon_pair::Tuple{Float64, Float64})
# Main .\In[46]:8
[9] top-level scope
# .\In[50]:3
[10] eval
# .\boot.jl:368 [inlined]
[11] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
# Base .\loading.jl:1428
I'm fairly confident this has something to do with the concept of "hygiene" and variable expansion with a macro but I'm new enough to julia to not understand what needs to be done in my calling function to resolve this. I have reveiwed this question but am not sure it applies to this case: How to pass variable value to a macro in julia?
Thanks!
Turns out the issue was unrelated and was a known issue in julia: https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012
I'm trying to learn Recurrent Neural Networks (RNN) with Flux.jl in Julia by following along some tutorials, like Char RNN from the FluxML/model-zoo.
I managed to build and train a model containing some RNN cells, but am failing to evaluate the model after training.
Can someone point out what I'm missing for this code to evaluate a simple (untrained) RNN?
julia> using Flux
julia> simple_rnn = Flux.RNN(1, 1, (x -> x))
julia> simple_rnn.([1, 2, 3])
ERROR: MethodError: no method matching (::Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}})(::Matrix{Float32}, ::Int64)
Closest candidates are:
(::Flux.RNNCell{F, A, V, var"#s263"} where var"#s263"<:AbstractMatrix{T})(::Any, ::Union{AbstractMatrix{T}, AbstractVector{T}, Flux.OneHotArray}) where {F, A, V, T} at C:\Users\UserName\.julia\packages\Flux\6o4DQ\src\layers\recurrent.jl:83
Stacktrace:
[1] (::Flux.Recur{Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}}, Matrix{Float32}})(x::Int64)
# Flux C:\Users\UserName\.julia\packages\Flux\6o4DQ\src\layers\recurrent.jl:34
[2] _broadcast_getindex_evalf
# .\broadcast.jl:648 [inlined]
[3] _broadcast_getindex
# .\broadcast.jl:621 [inlined]
[4] getindex
# .\broadcast.jl:575 [inlined]
[5] copy
# .\broadcast.jl:922 [inlined]
[6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, Flux.Recur{Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}}, Matrix{Float32}}, Tuple{Vector{Int64}}})
# Base.Broadcast .\broadcast.jl:883
[7] top-level scope
# REPL[3]:1
[8] top-level scope
# C:\Users\UserName\.julia\packages\CUDA\LTbUr\src\initialization.jl:81
I'm using Julia 1.6.1 on Windows 10.
Turns out it's just a problem with the input type.
Doing something like this will work:
julia> v = Vector{Vector{Float32}}([[1], [2], [3]])
julia> simple_rnn.(v)
3-element Vector{Vector{Float32}}:
[9.731078]
[16.657223]
[28.398548]
I tried a lot of combinations until I found the working one. There is probably a way to automatically convert the input with some evaluation function.
I am trying to figure out how to bin/histogram an array of data in Julia. I have an array of units from the Unitful.jl package and want to use Histogram from StatsBase to bin the data. The first error I got was an error saying there were no methods for log10 to use Unitful.FreeUnits so I wrote one. Now I get a dimensional error. I thought Unitful was just going to work with other stuff.. guess not. The following is where I am at.
using Unitful
using StatsBase
data = [rand()*100*1u"MHz" for x in 1:10000]
function Base.log10(x::Quantity{})
u = unit(x)
return log10(x.val)u
end
# eventually I want to define my bin width manually.. but this is a start.
fit(Histogram, data)
ERROR
ERROR: DimensionError: 0.0 and 0.8237981449864736 MHz are not dimensionally compatible.
Stacktrace:
[1] _lt at /home/mcamp/.julia/packages/Unitful/1t88N/src/quantities.jl:274 [inlined]
[2] <(::Quantity{Float64,NoDims,Unitful.FreeUnits{(),NoDims,nothing}}, ::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}) at /home/mcamp/.julia/packages/Unitful/1t88N/src/quantities.jl:264
[3] <(::Int64, ::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}) at /home/mcamp/.julia/packages/Unitful/1t88N/src/quantities.jl:266
[4] <=(::Int64, ::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}) at ./operators.jl:326
[5] >=(::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}, ::Int64) at ./operators.jl:350
[6] histrange(::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}, ::Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}}, ::Int64, ::Symbol) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:51
[7] histrange(::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}, ::Int64, ::Symbol) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:39
[8] (::StatsBase.var"#127#128"{Symbol})(::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}, ::Int64) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:103
[9] map(::StatsBase.var"#127#128"{Symbol}, ::Tuple{Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}}, ::Tuple{Int64}) at ./tuple.jl:176
[10] histrange(::Tuple{Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}}, ::Tuple{Int64}, ::Symbol) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:102
[11] fit(::Type{Histogram{Int64,N,E} where E where N}, ::Tuple{Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}}; closed::Symbol, nbins::Int64) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:332
[12] fit(::Type{Histogram{Int64,N,E} where E where N}, ::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}; closed::Symbol, nbins::Int64) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:276
[13] fit(::Type{Histogram{Int64,N,E} where E where N}, ::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:276
[14] fit(::Type{Histogram}, ::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:383
[15] fit(::Type{Histogram}, ::Array{Quantity{Float64,π^-1,Unitful.FreeUnits{(MHz,),π^-1,nothing}},1}) at /home/mcamp/.julia/packages/StatsBase/EA8Mh/src/hist.jl:383
[16] top-level scope at REPL[183]:1
[17] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
[EDITED because I misunderstood the question]
Are you looking to plot a histogram of the data? If yes, instead of StatsBase.jl, I would use the histogram function of a plotting package like Plots.jl. For instance, to combine plots from Plots.jl with units from Unitful.jl, you can use the UnitfulRecipes.jl package. See this MWE that may be what you are after:
using Unitful: MHz
using Plots
using UnitfulRecipes
data = 100 * exp.(randn(10000)) * MHz
histogram(data)
will output
[BEFORE EDIT]
I don't think it makes sense mathematically or physically to take the logarithm of a non-nondimensional variable (i.e., a variable with a unit). That is, your redefinition of log10 is not a good idea IMHO. Instead, I would non-dimensionalize the data before taking the log, with something similar to
using Unitful: MHz
using StatsBase
data = 100 * rand(10000) * MHz
data_nodim = log10.(data / MHz) # <- this is valid
fit(Histogram, data_nodim) # <- this is valid too
I'm learning julia from https://docs.juliaplots.org/latest/basics/
Here is the julia code
using Plots,UnicodePlots,RDatasets
unicodeplots()
v=dataset("Ecdat","Airline")
typeof(v)
plot(v, :Cost) # this line will produce the error
plot(v) # this line will produce the error, too
I got
ERROR: Cannot convert DataFrame to series data for plotting
Stacktrace:
[1] prepareSeriesData(::DataFrame) at /home/dlin/.julia/packages/Plots/qZHsp/src/series.jl:14
[2] convertToAnyVector(::DataFrame, ::Dict{Symbol,Any}) at /home/dlin/.julia/packages/Plots/qZHsp/src/series.jl:26
[3] macro expansion at /home/dlin/.julia/packages/Plots/qZHsp/src/series.jl:130 [inlined]
[4] apply_recipe(::Dict{Symbol,Any}, ::Type{Plots.SliceIt}, ::Nothing, ::DataFrame, ::Nothing) at /home/dlin/.julia/packages/RecipesBase/zBoFG/src/RecipesBase.jl:275
[5] _process_userrecipes(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{DataFrame}) at /home/dlin/.julia/packages/Plots/qZHsp/src/pipeline.jl:83
[6] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{DataFrame}) at /home/dlin/.julia/packages/Plots/qZHsp/src/plot.jl:178
[7] #plot#138(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(plot), ::DataFrame) at /home/dlin/.julia/packages/Plots/qZHsp/src/plot.jl:57
[8] plot(::DataFrame) at /home/dlin/.julia/packages/Plots/qZHsp/src/plot.jl:51
[9] top-level scope at REPL[4]:1
In the code of series.jl, it is
prepareSeriesData(x) = error("Cannot convert $(typeof(x)) to series data for plotting")
How to correct this?
What about simply using plot(v.Cost):
julia> plot(v.Cost)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4.88870026e6 ββ β‘β β β β β β β β β£ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β y1
ββ β‘β β β β β β β β β£Ώβ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β’°β’Ήβ β β β β β β β β’β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β‘β’Έβ β β β β β β β β‘Έβ‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β’β β β’Έβ β β β β β β β’ β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β’Έβ β β’Έβ β β β β β β β‘β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β‘β β β’Έβ β β β β β β’Έβ β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β‘β β β’Έβ β β β β β β‘β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β’°β β β β’Έβ β β β β β β‘β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β‘Έβ β β β’Έβ β β β β β’°β β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β’β β β β β’Έβ β β β β β’Έβ β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β‘Όβ β β β β β‘β β β β β‘β β β β’£β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β‘°β β β β β β β‘β β β β’°β β β β β’Έβ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β‘°β β β β β β β β‘β β β’ β β β β β β’Έβ β β β β β β β β β β β β β β β β β β‘β β β β β β β β β β β β β β β β β β β β β
ββ β‘β‘°β β β β β β β β β‘β β’β β β β β β β’Έβ β β β β β β β β£β β β β β β β β’β β‘β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β β β‘β β‘β β β β β β β’Έβ β β β β β’β β β’Έβ β β β β β β’β β β‘β β β β β β β β β β β β β β β β β β β‘β β
ββ β‘β β β β β β β β β β‘β‘Όβ β β β β β β β’Έβ β β β β’ β β β β’Έβ β β β β β β‘β β β’±β β β β β β β β β β β β β β β β β β‘β β β
ββ β‘β β β β β β β β β β β β β β β β β β β’Έβ β β‘ β β β β β β β‘β β β β’β β β β β’Έβ β β β β β β‘ β β’²β β β β β β β β‘β β β β
ββ β‘β β β β β β β β β β β β β β β β β β β Έβ β β β β β β β β β‘β‘ β β β β β β β β’Έβ β β β’β‘ β β β β β‘β β β β β‘ β β β β β β
-71402.26000000001 β⠀⑧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀β β €β €β €β €β €β €β €β €β Όβ Άβ Ύβ β ₯β €β €β €β €β €β ·β Άβ Ύβ β β €β €β €β €β €β €β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-1.67 92.67
That part of the docs might be outdated. You have to use StatsPlots and the #df macro to get the desired behavior.
julia> using StatsPlots
julia> #df v plot(:Cost)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4.88870026e6 ββ β‘β β β β β β β β β£ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β y1
ββ β‘β β β β β β β β β£Ώβ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β’°β’Ήβ β β β β β β β β’β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β‘β’Έβ β β β β β β β β‘Έβ‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β’β β β’Έβ β β β β β β β’ β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β’Έβ β β’Έβ β β β β β β β‘β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β‘β β β’Έβ β β β β β β’Έβ β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β‘β β β’Έβ β β β β β β‘β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β’°β β β β’Έβ β β β β β β‘β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β‘Έβ β β β’Έβ β β β β β’°β β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β’β β β β β’Έβ β β β β β’Έβ β β β‘β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β‘Όβ β β β β β‘β β β β β‘β β β β’£β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β‘°β β β β β β β‘β β β β’°β β β β β’Έβ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β‘°β β β β β β β β‘β β β’ β β β β β β’Έβ β β β β β β β β β β β β β β β β β β‘β β β β β β β β β β β β β β β β β β β β β
ββ β‘β‘°β β β β β β β β β‘β β’β β β β β β β’Έβ β β β β β β β β£β β β β β β β β’β β‘β β β β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β β β‘β β‘β β β β β β β’Έβ β β β β β’β β β’Έβ β β β β β β’β β β‘β β β β β β β β β β β β β β β β β β β‘β β
ββ β‘β β β β β β β β β β‘β‘Όβ β β β β β β β’Έβ β β β β’ β β β β’Έβ β β β β β β‘β β β’±β β β β β β β β β β β β β β β β β β‘β β β
ββ β‘β β β β β β β β β β β β β β β β β β β’Έβ β β‘ β β β β β β β‘β β β β’β β β β β’Έβ β β β β β β‘ β β’²β β β β β β β β‘β β β β
ββ β‘β β β β β β β β β β β β β β β β β β β Έβ β β β β β β β β β‘β‘ β β β β β β β β’Έβ β β β’β‘ β β β β β‘β β β β β‘ β β β β β β
-71402.26000000001 β⠀⑧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀β β €β €β €β €β €β €β €β €β Όβ Άβ Ύβ β ₯β €β €β €β €β €β ·β Άβ Ύβ β β €β €β €β €β €β €β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-1.67 92.67
This is explained in later sections of the manual: https://docs.juliaplots.org/latest/input_data/#dataframes-support
I want to use the hasmethod function to find if an object t::T supports t[!, something] syntax.
The key is something can be of many types and I don't want to check them all, I just want a way to express that hasmethod(getindex, Tuple{T, typeof{!}, S}) regardless of what S is.
How do I do that?
I think the way to get the list of methods is:
methods(getindex, Tuple{Any, typeof(!), Any})
The only problem is that if the second argument allowed by getindex is a supertype of typeof(!) it will be also listed. I do not think it can be avoided though, as you cannot rule out that such getindex definition actually allows ! to be passed as a first argument.
For instance if no packages are loaded the following is the result of the call above:
julia> methods(getindex, Tuple{Any, typeof(!), Any})
# 10 methods for generic function "getindex":
[1] getindex(md::Markdown.MD, args...) in Markdown at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Markdown\src\parse\parse.jl:24
[2] getindex(r::Distributed.Future, args...) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Distributed\src\remotecall.jl:624
[3] getindex(::Type{Any}, vals...) in Base at array.jl:357
[4] getindex(::Type{T}, x, y) where T in Base at array.jl:353
[5] getindex(::Type{T}, vals...) where T in Base at array.jl:344
[6] getindex(A::SparseArrays.SparseMatrixCSC, i, ::Colon) in SparseArrays at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\SparseArrays\src\sparsematrix.jl:1879
[7] getindex(A::AbstractArray, I...) in Base at abstractarray.jl:979
[8] getindex(t::AbstractDict, k1, k2, ks...) in Base at abstractdict.jl:476
[9] getindex(itr::Base.SkipMissing, I...) in Base at missing.jl:232
[10] getindex(r::Distributed.RemoteChannel, args...) in Distributed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Distributed\src\remotecall.jl:626
All of the cases are the ones where getindex does not put restrictions on the second argument so you cannot rule out that they could possibly allow ! as a valid value.
But if e.g. you load DataFrames.jl and restrict the first argument to AbstractDataFrame you get:
julia> methods(getindex, Tuple{AbstractDataFrame, typeof(!), Any})
# 5 methods for generic function "getindex":
[1] getindex(df::DataFrame, ::typeof(!), col_ind::Symbol) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:367
[2] getindex(df::DataFrame, ::typeof(!), col_ind::Union{Signed, Unsigned}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:358
[3] getindex(df::DataFrame, row_ind::typeof(!), col_inds::Union{Colon, Regex, AbstractArray{T,1} where T, All, Between, InvertedIndex}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:405
[4] getindex(sdf::SubDataFrame, ::typeof(!), colind::Union{Signed, Symbol, Unsigned}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\subdataframe\subdataframe.jl:127
[5] getindex(df::SubDataFrame, row_ind::typeof(!), col_inds::Union{Colon, Regex, AbstractArray{T,1} where T, All, Between, InvertedIndex}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\subdataframe\subdataframe.jl:137
which is now more informative, because in DataFrames.jl we try to be careful not to leave indexing arguments free (i.e. allow them to be Any and only internally check what is valid).
Additionally you can use methodswith to check which methods accept ! explicitly and filter only getindex instances. Here is the result after loading DataFrames.jl:
julia> filter(x -> x.name == :getindex, methodswith(typeof(!)))
[1] getindex(df::DataFrame, ::typeof(!), col_ind::Symbol) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:367
[2] getindex(df::DataFrame, ::typeof(!), col_ind::Union{Signed, Unsigned}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:358
[3] getindex(df::DataFrame, row_ind::typeof(!), col_inds::Union{Colon, Regex, AbstractArray{T,1} where T, All, Between, InvertedIndex}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\dataframe\dataframe.jl:405
[4] getindex(sdf::SubDataFrame, ::typeof(!), colind::Union{Signed, Symbol, Unsigned}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\subdataframe\subdataframe.jl:127
[5] getindex(df::SubDataFrame, row_ind::typeof(!), col_inds::Union{Colon, Regex, AbstractArray{T,1} where T, All, Between, InvertedIndex}) in DataFrames at D:\AppData\.julia\packages\DataFrames\yH0f6\src\subdataframe\subdataframe.jl:137
Finally note that x[!, y] syntax can also mean a view (if preceded by e.g. #view) or a setindex! operation (if it appears on LHS of assignment) so you might wan to check also these functions if they accept ! (and in DataFrames.jl they actually do).
You can do that in theory, if you get the syntax right:
julia> hasmethod(getindex, Tuple{Vector{Int}, typeof(!), Any})
true
This should work since Tuples are covariant.
But its returning true is obviously nonsense:
julia> getindex([1], !, 1)
ERROR: ArgumentError: invalid index: ! of type typeof(!)
Stacktrace:
[1] to_index(::Function) at ./indices.jl:270
[2] to_index(::Array{Int64,1}, ::Function) at ./indices.jl:247
[3] to_indices(::Array{Int64,1}, ::Tuple{Base.OneTo{Int64}}, ::Tuple{typeof(!),Int64}) at ./indices.jl:298
[4] to_indices at ./indices.jl:294 [inlined]
[5] getindex(::Array{Int64,1}, ::Function, ::Int64) at ./abstractarray.jl:981
[6] top-level scope at REPL[26]:1
The reason is that it delegates to other methods internally.
I think it's better to constrain T to some abstract type(s) for which this kind of indexing is known to work. Or, as a last resort, use try.