Error in variable define

ERROR: UndefVarError: view not defined
in partboost at /home//Desktop/test.jl:75
in findpartition at /home/Desktop/test.jl:200

Kindly suggest how to solve this problem, I’m new in this programming.

It would be helpful if you could share the code in test.jl which you tried to execute

1 Like

Is this all of the code?
The error message you get, is complaining about row 75 and row 200 of test.jl. Can you indicate which rows these are?

What does the function findpartition do? (it is not contained in the code you posted above).
It seems you might use view as a variable instead of using it as a function.
Maybe you can search for all appearances of view and find out if you have a typo somewhere?

module PartDBM
import BoltzmannMachines
import Distributions

"""Standardizes a data matrix x """
function standardize!(x;standardize::Bool=true)
   xmean = mean(x,1)
   xstd = std(x,1)

   for j in 1:size(x)[2]
      for i in 1:size(x)[1]
         if standardize
            x[i,j] = (x[i,j] - xmean[j])/xstd[j]
         else
            x[i,j] = (x[i,j] - xmean[j])
         end
      end
   end
   nothing
end


""" Calculates univariate betas, given an explanatory matrix x and a response y """
function calcunibeta(x,y)
   n, p = size(x)
   unibeta = zeros(p)

   for i=1:p
      denom = 0.0
       for j=1:n
         unibeta[i] += x[j,i]*y[j]
         denom += x[j,i]*x[j,i]
      end
      unibeta[i] /= denom
   end
   unibeta
end

""" Calculates univariate betas, between a selected variable (candidate) in x and all other variables in x. Betas that have been calculated in advance are stored in the matrix covcache."""
function calcunibeta(x,candidate,covcache)
   n, p = size(x)
   unibeta = zeros(p-1)

   for j=1:(p-1)
      targetj = j < candidate ? j : j+1

      if isnan(covcache[candidate,targetj])
          unibeta[j] = sum(x[:,targetj].*x[:,candidate])
          denom = sum(x[:,targetj].*x[:,targetj])

         covcache[candidate,targetj] = covcache[targetj,candidate] = unibeta[j]
      else
          unibeta[j] = covcache[candidate,targetj]
          denom = sum(x[:,targetj].*x[:,targetj])
      end
      unibeta[j] /= denom 
   end
   unibeta
end


""" For each variable "candidate" in inmat, the variables of inmat are selected that best explain the state of "candidate", given stepno steps."""
function partboost(inmat; 
		stepno::Int=10,nu::Float64=0.1,csf::Float64=0.9)
   n, inp = size(inmat)
   p = inp - 1
   betamat = zeros(Float64,inp,inp)

   covcache = fill(NaN,inp,inp)

   indenom = var(inmat,1)*(n-1)

   for candidate=1:inp

       y = view(inmat,:,candidate)
       x = view(inmat,:,[1:(candidate-1); (candidate+1):inp]) 
      denom = view(indenom,[1:(candidate-1); (candidate+1):inp])

      nuvec = fill(nu,p)
      penvec = fill(n*(1/nu-1),p)
     
      actualnom = calcunibeta(inmat,candidate,covcache)
      beta = view(betamat,candidate,[1:(candidate-1); (candidate+1):inp])

      actualupdate = 0.0
      actualsel = -1

      for step in 1:stepno
          if step > 1
            for j=1:p
               inj = j < candidate ? j : j+1
               insel = actualsel < candidate ? actualsel : actualsel+1

               if isnan(covcache[inj,insel])
                   covval = sum(x[:,j] .* x[:,actualsel])
                   covcache[inj,insel] = covcache[insel,inj] = covval
               else
                  covval = covcache[inj,insel]
               end
               actualnom[j] -= actualupdate*covval/denom[j] 
            end
         end

          actualsel = indmax(((actualnom.*denom) ./ (denom.+penvec)).^2)
         actualupdate = nuvec[actualsel]*actualnom[actualsel]
         beta[actualsel] += actualupdate
         nuvec[actualsel] = 1-(1-nuvec[actualsel])^csf
         penvec[actualsel] = n*(1/nuvec[actualsel]-1)

      end
   end

   betamat
end


""" Aggregates distances in distmat using aggfun."""
function clusterdist(distmat,indices1,indices2,aggfun=mean)
    aggfun(view(distmat,indices1,indices2))
end

""" Hierarchically clusters variables based on their pairwise dissimilarity stored in distmat. Minimum and maximum cluster size is given by minsize and maxsize respectively. By default, clustering is performed based on average linkage (aggfun=mean). Returns a dictionary of clusters, where each cluster is represented by a vector containing the cluster mebmbers."""
function clusterbeta(distmat,aggfun=mean;minsize::Int=2 ,maxsize::Int=size(distmat)[2])
   p = size(distmat)[2]
   maxdist = maximum(distmat) + 1.0 

   cldist = copy(distmat) 
   cldict = Dict{Int,Vector{Int}}()
   for i=1:p
      cldict[i] = [i]
   end
 

   while true
      cllen = map(key -> length(cldict[key]),keys(cldict)) 
    
      if minimum(cllen) >= minsize
         break
      end
       
       _keys = sort(collect(keys(cldict)))
       source, target, actualmin = findmindist(cldist,_keys)
      
      if (length(cldict[source]) + length(cldict[target]) > maxsize &&
        
          length(cldict[source]) >= minsize &&
          length(cldict[target]) >= minsize)
      
          cldist[source,target] = cldist[target,source] = maxdist
         continue
      end

      append!(cldict[target],cldict[source]) 
      delete!(cldict,source) 
      for key in keys(cldict)
         if key != target 
            cldist[target,key] = cldist[key,target] =
               clusterdist(distmat,cldict[target],cldict[key],aggfun) 
         end
      end
   end

    cldict
end

"""Makes an asymmetric similarity matrix symmetric. The distance (D) between i and j or j and i is replaced by max(|D_ij|,|D_ji|). """
function calcbetawish(betamat)
   p = size(betamat)[2]
   wishmat = zeros(p,p)
   for i=1:(p-1)
      for j=(i+1): p
         wishmat[i,j] = wishmat[j,i] = max(abs(betamat[i,j]),abs(betamat[j,i]))
      end
   end
   wishmat
end

""" Identifies the variables besti and bestjj with the lowest distance curmin given in the distance matrix distmat."""
function findmindist(distmat,indices=1:size(distmat,2))
  
   curmin = -1
   besti = bestj = -1

   for i=1:(length(indices)-1)
      for j=(i+1):length(indices)
         if (i == 1 && j == 2) || distmat[indices[i],indices[j]] < curmin
            besti = indices[i]
            bestj = indices[j]
            curmin = distmat[indices[i],indices[j]]
         end
      end
   end

   besti, bestj, curmin
end

""" Identifies clusters of related SNPs using stagewise regression. SNP clusters are allowed to have a maximum of maxsize SNPs while at least are required to have a minimum of minsize SNPs. stepno indicates the number of steps in stagewise regression."""
function findpartition(mat;minsize::Int=5,maxsize::Int=20,stepno::Int=100)
    standardize!(mat,standardize=true)
    betamat = partboost(mat,stepno=stepno)
    betawish = calcbetawish(betamat)
    cldict = clusterbeta(-betawish,minsize=minsize,maxsize=maxsize)
    mvisibleindex = Array{Array{Int,1}}(length(cldict))
    clkeys = collect(keys(cldict))
    for i=1:length(clkeys)
        mvisibleindex[i] = cldict[clkeys[i]]
    end
    mvisibleindex
end




""" Identifies adequate size of hidden units for each separate DBM"""
function partitionhiddens(partitions,nhiddens)
    nparts = length(partitions)
    p= length(cat(1,partitions...))
    nhiddensmat = zeros(Int,nparts,length(nhiddens))
    for i=1:(nparts-1)
        nhiddensmat[i,:] = floor(nhiddens ./ (p/length(partitions[i])))
    end
    nhiddensmat[nparts,:] = nhiddens .- vec(sum(nhiddensmat,1))
    nhiddensmat
end



""" Given the partitions, identified by findpartition, a separate DBM is trained on each partition. The overall DBM architecture is given by nhiddens. The units in nhiddens are distributed across the partitions, such that each partition has at least p units in hidden layer 1 and at least a single unit in hidden layer 2."""
function trainpartdbm(mat;partitions::Array{Array{Int64,1},1}=[collect(1:size(mat)[2])],
                      nhiddens::Array{Int64,1}=[size(mat)[2],div(size(mat)[2],10)],
                      epochs::Int64=20,
                      nparticles::Int64=100,
                      learningrate::Float64=0.005)

    partDBMS = Vector{BoltzmannMachines.BasicDBM}()
    nhiddensmat = partitionhiddens(partitions,nhiddens)
    println(nhiddensmat)
     for i=1:length(partitions)
        partx = mat[:,partitions[i]]
        
        pdbm = BoltzmannMachines.fitdbm(partx,
                                        nhiddens=nhiddensmat[i,:],
                                        epochs =epochs,
                                        nparticles=nparticles,
                                        learningrate=learningrate
                                        )
        push!(partDBMS,pdbm)
    end
    jointdbm = BoltzmannMachines.joindbms(partDBMS,partitions)
    jointdbm,partDBMS
end
end




#### abobe code is full. kindly see it once

Which Julia version are you on? Consider versioninfo()
Try quoting your code with tripe backticks `

Is view defined at all (through any of the packages you load, or standard lib).
Can you try this line:

view(rand(4,4),2,:)

I note that I am not familiar with the package BoltzmannMachines.
You may want to try and run tests for that package (or update it)

I note that the syntax var(inmat,1) is not work for me on Julia 1.0, it needs a dims keyword. But it might be that this worked in earlier Julia versions.

1 Like

@ahmedbulbul52, please quote your code in the future. (I edited your post above to quote your code.)