Is the problem the function or the dataset I'm trying to use?

I’m new to Julia programming, so researching for my thesis I found the following code, but I can’t seem to grasp what the error is. The function main is supposed to take a dataset and then plot some results, but when I try to call the function into action:

info = readdlm("DataSet.txt")

By the way, DataSet is a:

30×6 Array{Float64,2}

Then, when I call the function:

mymain(info,5)

It returns the following:

MethodError: no method matching readdlm_auto(::Array{Float64,2}, ::Char, ::Type{Float64}, ::Char, ::Bool)
Closest candidates are:
readdlm_auto(!Matched::Array{UInt8,1}, ::AbstractChar, ::Type, ::AbstractChar, ::Bool;  ...) 

I’ve tried to modify my data, but I don’t understand where is the error. The whole function is as follows:

function mymain(filename,nsamples)
start_time=time()

M=readdlm(filename)
ts,A=M[:,1],M[:,2:end]
(nsweeps,N)=size(A)

dx=0.01;
x=[minimum(collect(A)):dx:maximum(collect(A))];
bx=[x-dx/2,x[end]+dx/2];
(bx,hA)=hist(A[:],bx);

f1=figure()
subplot(2,1,1); plot(ts,A,"-o"); xlabel("Time [ms]"); ylabel("Amps 
[mV]");

subplot(2,1,2); plot(x,hA,"-");  xlabel("Amps [mV]"); 
ylabel("Density");draw()

nparams=8			  

Sx=Array(ASCIIString,1,nparams)		
Rx=zeros(2,nparams)			
nx=zeros(Int,1,nparams)			

Sx[1,1]="p";		Rx[1:2,1]=[0.02,0.98];		nx[1]=49
Sx[1,2]="n"; 	 	Rx[1:2,2]=[1,20];		nx[2]=20		
Sx[1,3]="tD"; 		Rx[1:2,3]=[50,200]; 		nx[3]=46
Sx[1,4]="a"; 		Rx[1:2,4]=[0.05,0.5]; 		nx[4]=46
Sx[1,5]="siga"; 	Rx[1:2,5]=[0.01,0.2]; 		nx[5]=39
Sx[1,6]="sigb"; 	Rx[1:2,6]=[0.01,0.1]; 		nx[6]=19
Sx[1,7]="tauf";		Rx[1:2,7]=[50,200];		nx[7]=46
Sx[1,8]="u1";		Rx[1:2,8]=Rx[1:2,1];		nx[8]=nx[1] 

x=zeros(maximum(nx),nparams)
p=zeros(maximum(nx),nparams)
dx=zeros(1,nparams)

for j=1:nparams
x[1:nx[j],j]=linspace(Rx[1,j],Rx[2,j],nx[j])'
dx[j]=x[2,j]-x[1,j]
end

S=zeros(Int,nsamples,nparams)      

sold=zeros(Int,1,nparams)
for j=1:nparams
sold[j]=rand(1:nx[j])
end

while x[sold[4],4]<=x[sold[5],5]	
sold[4]=rand(1:nx[4])
sold[5]=rand(1:nx[5])
end

while x[sold[8],8]<=x[sold[1],1]	
sold[1]=rand(1:nx[1])
sold[8]=rand(1:nx[8])
end

xold=zeros(1,nparams)
xnew=zeros(1,nparams)
for j=1:nparams
xold[j]=x[sold[j],j]
end
llold=myloglikelihood(xold,ts,A)	

for k=1:nsamples

snew=sold+rand(-1:1,1,nparams)   

if all(ones(1,nparams).<=snew.<=nx)  	       

allowed2=x[snew[4],4]>x[snew[5],5]        
allowed3=x[snew[8],8]>x[snew[1],1]	       

if allowed2&allowed3

for j=1:nparams
xnew[j]=x[snew[j],j]
end

llnew=myloglikelihood(xnew,ts,A)	 

if rand()<exp(llnew-llold) 		 
sold,llold=snew,llnew
end

end
end

S[k,:]=sold

end
for k=1:nsamples
for j=1:nparams
p[S[k,j],j]+=1/(nsamples*dx[j])
end
end 

f2=figure()
for j=1:nparams
subplot(2,4,j)
plot(x[1:nx[j],j],p[1:nx[j],j]);
xlabel(Sx[j])
end

diff_time=time()-start_time;
println("Total runtime 
",round(diff_time,3),"s=",round(diff_time/60,1),"mins." );

return S

end    

Perhaps this a very rookie mistake, but I have searched within the manuals but even so, I’m not being able to grasp the whole of it. Any help would be much appreciated.

The issue appears to be with trying to read in the file twice , that is , when you set

info = readdlm("DataSet.txt")

you get a Julia object, like you said, a 30×6 Array{Float64,2}. So the effective result inside the function is

M =  readdlm(readdlm("DataSet.txt"))

The function call should just be

mymain("DataSet.txt",5)

or

info_filename = "DataSet.txt"
mymain(info_filename,5)
1 Like

Thank you so much for your help.

I tried what you say and now it goes something like this:
ArgumentError: reducing over an empty collection is not allowed

Could you copy and paste the contents of “DataSet.txt” here? That way I can try your code.

Yes, it’s a txt file that contains:

0	1.67120684980318	1.45098387997959	1.17908652139119	1.4017158076366	0.565397507133258
0.0344827586206897	0.408243965303102	1.26206259909648	0.226283327648793	0.284539521690238	1.34472634330185
0.0689655172413793	0.371564360993164	0.344625040529852	0.51414245189949	0.218476822613526	0.410632463984759
0.103448275862069	-0.0810097537391375	-0.00925411917509679	0.146431834706219	-0.0350531189263829	-0.0571987141449754
0.137931034482759	0.211737429395792	0.0641501126583847	0.515296750790343	0.0304817753713977	0.22381378734227
0.172413793103448	0.496390679294804	0.204183075623072	-0.0170799828129661	0.36206992821387	0.180460773734507
0.206896551724138	0.00915792333026091	0.0761777527429678	0.305423598684223	0.312596293261318	0.278800983701527
0.241379310344828	0.378604953133542	0.675542296133498	0.328999582245926	0.490531088284776	0.75548541100967
0.275862068965517	0.685814202355724	0.265119839638792	0.263485918490875	0.0442628677879349	0.24719295965412
0.310344827586207	0.264148884734373	0.135078854132944	-0.0246115062378757	0.220362729343097	0.03477883154747
0.344827586206897	0.326414892468055	-0.0359206548824366	0.292942697150654	-0.0255960503440987	0.527660054548807
0.379310344827586	0.300861299688613	-0.0351443649669127	-0.0820681250243618	0.290924571069368	0.220640844275509
0.413793103448276	0.21940174463706	-0.0419932345986639	0.055385409529963	0.0155850775410969	0.557364883775845
0.448275862068966	0.0163625507232599	0.0535548703471156	0.355442211238449	0.0651870086695201	0.0474808964201807
0.482758620689655	-0.0735271978937539	0.172477307385758	0.602048000884589	-0.0480825671385276	0.267725479261608
0.517241379310345	0.138502075602254	0.162689687098432	0.0410649441131892	-0.0481303411667667	0.0114034414652414
0.551724137931034	0.854777925594685	0.931383742433005	0.402214897129628	-0.0520690646925456	0.361718713440413
0.586206896551724	0.395943545421214	0.586693268806567	0.643505141948141	0.406919167548478	0.62107910461489
0.620689655172414	0.37792464312718	0.221674380149521	0.0149472589857626	0.18442849472651	0.183785113772701
0.655172413793103	0.267533596292511	0.58301482305093	-0.0428894221875498	0.22639922275863	0.262740464914941
0.689655172413793	-0.000478859298383711	0.0247122079199306	0.0212096865930236	-0.067596731433843	0.207779611914199
0.724137931034483	0.248342587001703	0.0538735880341137	-0.0235777148332754	-0.0942357991204027	-0.0280417188991256
0.758620689655172	0.266856021267506	0.362010748874029	-0.0379930614291293	0.163603844327712	-0.00880925551167524
0.793103448275862	0.362879330197601	-0.0642477739104888	0.0855176760014185	0.117255270122216	0.484001921797438
0.827586206896552	0.365898669125264	0.191634324556554	0.513433123868423	-0.0271165800489151	0.120474388061289
0.862068965517241	0.0337996208068564	0.570195798441892	0.40235145918539	0.133809217280299	0.191047785177785
0.896551724137931	0.233923826596119	-0.0907931043524204	0.455444590372732	0.0360791482911116	0.0233351527151204
0.931034482758621	-0.0536688042594715	0.152766231610487	0.466137060415199	0.0562302005516882	0.81734478371204
0.96551724137931	0.118254987850955	0.231136993926323	0.26165903598923	0.365554599076719	-0.0405021366334597
1	0.162582954301355	0.616050177732085	0.535516675704569	0.0853909998500637	0.226986365274539

Ok, got it. Can you put all the packages you are using or importing at the beginning? As well as any other set up code. The whole example needs to be self-contained for others to have a chance of running it to help debug.
For example, I think you would have
using DelimitedFiles
etc…
This is needed to know how to use the functions hist, figure, subplots and so in the code.

“something like” is not much use. The error message will tell you which line fails.

1 Like

Sure, I’ll add it all right away, also, I’ve been running the code line by line and the problem seems to be in the definition of ‘x’ and consecuently of ‘bx’.

Here’s the packages required and the data:

using Plots
using DelimitedFiles
data = readdlm("DatasSet.txt")

'Here goes the body of the function that's above'

As for ‘x’ and ‘bx’ goes, I’m running the code in the following way:

M= data
ts,A=M[:,1],M[:,2:end]
(nsweeps,N)=size(A)
dx=0.01;
x=[minimum(collect(A)):dx:maximum(collect(A))];
bx=[x.-dx/2,x[end]+dx/2];

Returns the error:

MethodError: no method matching -(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, 
 ::Float64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
 Closest candidates are:
 -(!Matched::BigFloat, ::Union{Float16, Float32, Float64}) at mpfr.jl:437
 -(!Matched::Complex{Bool}, ::Real) at complex.jl:307
 -(!Matched::Missing, ::Number) at missing.jl:115
 ...

So, I redefine ‘x’, and then try for ‘bx’:

x=minimum(A):dx:maximum(A);
bx=[x.-dx/2,x[end]+dx/2];

No problem so far, so I try to plot, and the error now is:

Cannot convert Float64 to series data for plotting

That error is telling you that you are trying to plot a single number, while Plots is expecting a vector.

1 Like

Thank you very much. So, the problem lies within the definition of ‘bx’?

Maybe I should try to look for a way and define it as a vector.

I’m not able to look through the whole code, but the start might look like this in Julia (the existing code seems to be written in something like Matlab?). Check out the documentation:
https://docs.juliaplots.org/latest/tutorial/
(and also GitHub - JuliaPlots/StatsPlots.jl: Statistical plotting recipes for Plots.jl for “shortcuts” to more complex plots.)

using DelimitedFiles
using Plots

info_filename = "DataSet.txt"
mymain(info_filename,5)
## where you have already loaded:
function mymain(filename,nsamples)

    start_time=time()

    M=readdlm(filename)
    ts,A=M[:,1],M[:,2:end]
    (nsweeps,N)=size(A)

    dx=0.01;
    # x=[minimum(collect(A)):dx:maximum(collect(A))];
    x=minimum(A):dx:maximum(A)
    bx=collect(x[1] .- dx/2:dx:x[end] .+ dx/2)
    display(histogram(A[:],bins=bx,
    layout = (2,1),
    subplot=1))

    plot!(ts, A,
    markershape = :circle, markerstrokestyle = :dashdot,
    xlabel="Time [ms]", ylabel="Amps [mV]",
    subplot = 2 )

end