Wednesday, 4 November 2015

1D surgery case

One dimensional surgery simulation has finally been successfully coded, taking out a segment and leaving that as space with a boundary as the simulation, we also tried stitching the function bak together, so there are two cases, in small tumours, the appropriate case would be stiching together, as the brain biomechanics would allow that degree of deformation, but in larger resections, the former case is more biologically appropriate. 

Code:

%%1D-Fisher equation

%define constants
D = 2.5e-6;     %Mean Cancer-cell Diffusion (mm^2/s)
rho = 4e-7;         %Mean Cancer-cell growth rate (cells/second/voxel)
k = 2.4e5;          %Maximum number of cells per unit volume (cells/mm^3)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          



%% Define Domain

%The size of DFull is 128*128*60*6. We choose n, m and p so that at full
%resolution (r=1) the size of D is 130*130*62*6, and we add a single layer
%of empty cells around the brain. This is just some insurance, because if
%the brain is touching the boundary of the domain, errors can occur.
%based on DTI data, 2.5mm voxels

                               %xpoints
xrange = [2.5:2.5:130*2.5];                  %All these dimensions are in mm, and come straight from the MRI data
dx = 2.5;      %space step size
xmax=max(xrange);
%right boundary
%m = floor(130);                       %ypoints
%p = floor(62);                        %zpoints
tsize =200;                              %tsteps.  Unlike the space steps, this is variable. The stability condition requires it varies.
trange = [0, (2e8)];                   %The time dimension in seconds
tmax=max(trange);
dt = (trange(2)-trange(1))/tsize;          %time step size
tsteps = 1:dt:trange(2);
%stability
%% Initial Conditions

% Define Initial Condition as a function of x. Remember the units
% of c are cells per mm^3

% ic = @(x) 1*exp(-((x-mean(xrange))^2/1));
C=zeros(1,132); %initialise C
N = length(C);
C(62:67)=k/4; %I.C. cells here

CC = zeros(1,N); %post surgery function
%% Solve equation 1D FTCS scheme
  %Explicitly step through time with anisotropic diffusion
  %Equation dc/ct=D(\del^2)c + rho*c(1 -c/k), the Fisher KPP equation
  %becomes in FTCS scheme
  % c(i,k+1) = rc(i-1,k) + (1-2r)c(i,k) + rc(i+1,k)
  % where r = Ddt/(dx)^2
  %so set
  r=((D*dt)/(dx)^2)
   % Define C as a vector
    %dCdt = (-2*D/dx^2).*C;
 %C = C + dCdt*dt;
 %C = C + dt*rho.*abs(C).*(1-abs(C)/k);

 C(1) = 0;
C(132) = 0;
Cnew1 = zeros(1,N);
Cnew2 = zeros(1,N);
%C = C + dCdt*dt;
   

 figure
 hold off
 ConcSumUncut=zeros(1,tsize);

for t=1:tsize
    CC=C;
   
 


       for i = 3:(N-2)
             C(i) = r*CC(i+1) + (1-2*r)*CC(i) + r*CC(i-1);
       end
          
  
       %Also add a cell growth term - the Fisher Equation.
    %Note the absolute values are just back-ups to ensure stability. We
    %know that the explicit diffusion equation is numerically stable, as
    %small disturbances will die down. But with an extra growth term, small
    %disturbances may lead to negative numbers, which are then inflated.
    %This is just a safety-measure.
            C = C + dt*rho.*abs(C).*(1-abs(C)/k);
            plot(CC)  % ./k to normalise
            axis([0 132 0 k])
            pause(0.1)
            disp(t)
            %while t <= 30
                %continue
              

%   if t == 30  %then do surgery
                  
%is=find(C>0.2*k,1)
%for i=1:N
   % if C(i) < 0.2*k
        %CC(i) = C(i);
   % end
%end
%plot(N,CC)
%legend('Tumour', 'Resected tumour')


%for i = 1:(is-1)
     Cnew1(i+((N/2)-is)+1) = CC(i);
   
%end
%for i = (N-is):N
    Cnew2(i-(N/2-is)) = CC(i);

%end
%C = Cnew1 + Cnew2;



 %feed this back into main loop
    ConcSumUncut(t)=sum(C);
    end
          

            
 %plot(Csurg)
 

figure
plot(ConcSumUncut)
%end
%%Surgery function
%cuts C for C>is and stiches back together the ends

%%%One Dimensional tumour resection simulation.  Boundary at cut edges.
%%post-resection.
%%1D-Fisher equation

%define constants
D = 2.5e-6;     %Mean Cancer-cell Diffusion (mm^2/s)
rho = 4e-7;         %Mean Cancer-cell growth rate (cells/second/voxel)
k = 2.4e5;          %Maximum number of cells per unit volume (cells/mm^3)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          



%% Define Domain

%The size of DFull is 128*128*60*6. We choose n, m and p so that at full
%resolution (r=1) the size of D is 130*130*62*6, and we add a single layer
%of empty cells around the brain. This is just some insurance, because if
%the brain is touching the boundary of the domain, errors can occur.
%based on DTI data, 2.5mm voxels

                               %xpoints
xrange = [2.5:2.5:130*2.5];                  %All these dimensions are in mm, and come straight from the MRI data
dx = 2.5;      %space step size
xmax=max(xrange);
%right boundary
%m = floor(130);                       %ypoints
%p = floor(62);                        %zpoints
tsize =200;                              %tsteps.  Unlike the space steps, this is variable. The stability condition requires it varies.
trange = [0, (2e8)];                   %The time dimension in seconds
tmax=max(trange);
dt = (trange(2)-trange(1))/tsize;          %time step size
tsteps = 1:dt:trange(2);
%stability

%% Initial Conditions

% Define Initial Condition as a function of x. Remember the units
% of c are cells per mm^3

% ic = @(x) 1*exp(-((x-mean(xrange))^2/1));
C=zeros(1,132); %initialise C
N = length(C);
C(62:67)=k/4; %I.C. cells here

CC = zeros(1,N); %post surgery function

%% Solve equation 1D FTCS scheme
  %Explicitly step through time with anisotropic diffusion
  %Equation dc/ct=D(\del^2)c + rho*c(1 -c/k), the Fisher KPP equation
  %becomes in FTCS scheme
  % c(i,k+1) = rc(i-1,k) + (1-2r)c(i,k) + rc(i+1,k)
  % where r = Ddt/(dx)^2
  %so set
  r=((D*dt)/(dx)^2)
   % Define C as a vector
    %dCdt = (-2*D/dx^2).*C;
 %C = C + dCdt*dt;
 %C = C + dt*rho.*abs(C).*(1-abs(C)/k);

 C(1) = 0;
C(132) = 0;

   

 figure
 hold off
 ConcSum=zeros(1,tsize);

for t=1:30
    CC=C;
   
 


       for i = 2:(N-1)
             C(i) = r*CC(i+1) + (1-2*r)*CC(i) + r*CC(i-1);
       end
          
  
       %Also add a cell growth term - the Fisher Equation.
    %Note the absolute values are just back-ups to ensure stability. We
    %know that the explicit diffusion equation is numerically stable, as
    %small disturbances will die down. But with an extra growth term, small
    %disturbances may lead to negative numbers, which are then inflated.
    %This is just a safety-measure.
            C = C + dt*rho.*abs(C).*(1-abs(C)/k);
         % C = C + dt*0.001*rho.*abs(C).*(1-abs(C)/k).*(abs((C) - 0.2*k));
            plot(CC)  % ./k to normalise
            axis([0 132 0 k])
            pause(0.1)
            disp(t)
            %while t <= 30
                %continue
              

   if t == 30  %then do surgery
                  
CC = zeros(1,N);
is=find(C>0.2*k,1);
for i=1:N
    if C(i) < 0.2*k
        CC(i) = C(i);
    end
end
C = CC;
plot(CC)
%C(is+1) = C(is);
%C(N-is-1) = C(N-is);
 %feed this back into main loop
    
    end
          

ConcSum(t)=sum(C);
            
 %plot(Csurg)
 
end
               if  i == is-1;
             C(i) = (1-r)*CC(i) + r*CC(i-1);
               if   i ==(N-is+1);
               C(i) = r*CC(i+1) + (1-r)*CC(i);
               end
               end
              
for t=31:tsize
     CC=C;
   
 
 for i=2:(is-2)
       C(i) = r*CC(i+1) + (1-2*r)*CC(i) + r*CC(i-1);
          
         
 end

 for i=(N-is):N-1
     
       C(i) = r*CC(i+1) + (1-2*r)*CC(i) + r*CC(i-1);
     
      
 end
   %for i = (N-is+1):(N-1)
      % C(i) = r*CC(i+1) + (1-2*r)*CC(i) + r*CC(i-1);
  


       %Also add a cell growth term - the Fisher Equation.
    %Note the absolute values are just back-ups to ensure stability. We
    %know that the explicit diffusion equation is numerically stable, as
    %small disturbances will die down. But with an extra growth term, small
    %disturbances may lead to negative numbers, which are then inflated.
    %This is just a safety-measure.
            C = C + dt*rho.*abs(C).*(1-(abs(C)/k));
           %Cubic growth term?
           %C = C + dt*0.001*rho.*abs(C).*(1-abs(C)/k).*abs((C-(0.02*k)./C));
            plot(CC)  % ./k to normalise
            axis([0 132 0 (k)])
            pause(0.1)
            disp(t)
            ConcSum(t) = sum(C);
end
figure
plot(ConcSum)
%end
%%Surgery function
%cuts C for C>is and stiches back together the ends

Sunday, 11 October 2015

Summary of current issues:

Working on segmenting T1 and T2 images from Simon to export to MATLAB and try to quantify difference in tumour spread for boundary.  
  1. Need a different tool?  Check is FSL or MRTrix will do it, but I think they will only make a mask and brain extraction for diffusion data?  
  2. analysis of the data in MATLAB, converting scalar field to meaning
  3. differentiating between tumour and (say) bone of other dense material with same image value.
  4. Maybe using FreeSurfer and/or 3D Slicer to do segmentatin and extraction.
Growing tumours:

Have found optimal values for stable solutions generally seem to exist at around Stability condition =  0.02 or thereabouts. Can simulate tumours quite effectively when parameters are right.

Simulation issues:  
  1. Need to confirm how anisotropic the in vivo tumours are, scaling anisotropy appears to make things 'jagged' but still more like a spiky ball as opposed to strictly following any structures.  Investigate this further and visually compare with images.

Tuesday, 8 September 2015

Almost totally sure that the problem is too many negative eigenvalues in the tensor.  An analysis showed that around 8% of the eigenvalues of the tensor are negative (using tensor MouseTensorNyFiles, from Nyoman's data). 

Will check the other tensors and think on how to fix it.


Sunday, 6 September 2015

 

Isotropic case for mouse brain did not yield any answers, although some tumours grew better especially when the number of time steps was increased. Tumours still stopped growing and concentration dropped to zero or negative.The upper video is of one of the best tumours, will run with same parameters to see if it can become any bigger.


A good example of an instability.   Will check data around this time that instability appears.

Monday, 24 August 2015

Mouse model instabilities 25/08/15

Progress: Simulations with conservative parameters of delta t have still been yielding unstable results.

As proliferation parameter rho is slowed down, the tumours look more real on the visualisation tools, to an extent.  They never grow to fill the whole brain, the ones that look solid even have an instability where they start shrinking and the concentration drops to zero, which implies an unstable data point somewhere in the tensor(?)

e.g.


The simulations are taking now 40+ minutes to run on my desktop PC and are becoming unrunnable apart from for time scales longer than about 23 days on my MacBook.

Data files are very large now due to the higher resolution so until I get stable solutions, I am just saving a record of the parameters I have varied with notes on how the solution behaved. 

Looking at the diffusion tensor variable Mean_Diff, surface plot of slice (:,:,101), I will try running simulations at around (40,40,101) which will be away from the two spikes, although they are only a factor of two or so larger than the surrounding points.

Saturday, 8 August 2015

Mouse Brain Simulations part 1:

After meetings at CAI and separately with Nyoman I have been working on processing the mouse brain data to simulate some tumours.  Initial results were flawed, after  a second attempt, where I understood the orientation and data better, I have produced a first simulation.

The anisotropy and diffusion are very apparent in this, presumably due to the much higher resolution images and more defined tracts.

Next step will be to compare this to tractography and make some slices, also repeat the process with the other mouse brain data Nyoman gave me.

A 7T Human image would be fantastic next.

EDIT:  I realised that the max tumour cell concentration was completely wrong as the human value had been used and the voxel size is 11680 times smaller in the mouse scan!

Hence the max tumour cell conc/voxel is around 20 in the mice scan using the same parameters.  Running this data now.

Tuesday, 4 August 2015

Centre for Advanced Imaging meeting and Mouse Brain Data

  1. Following a meeting at CAI on Monday 03/08/2015, they have agreed to work with us and scan some of Simon Puttick's immunodeficient glioblastoma mice for us, to obtain a longitudinal data series. 
  2. Nyoman Kurniawan, the small animal imaging expert at CAI has shared a 9.44T mouse brain atlas with us, which I have been working to put into the code and simulate tumours. I have done some of the preliminary processing, extracted the diffusion tensor, now to put in the new scan dimensions into our code.  The mouse brain has much smaller voxels and  is a different shape, the dimensions of the scan are 72x90x170, with the long axis inferior-superior (z-axis) because the mice are scanned vertically with the nose pointing to the sky. The hard bit here is lining up all the data, looking at the way it is organised in mrinfo for example to extract the right coordinates and so on.