Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

lcocea
This post was updated on .
Anybody know where to find a tool like Sliceomatic (https://www.mathworks.com/matlabcentral/fileexchange/764-sliceomatic) for Octave? Any help will be greatly appreciated. I couldn't find anything similar on Google so yesterday afternoon I wrote some simple code for my project to visualize "slices" of a function f(x, y, z) using scatter3.

The parameters of the 3d plotting function are the x, y, z, and f vectors, and the number of slices n. The algorithm divides the max(f) - min(f) interval into n - 1 intervals, or "slices". Note that the term "slice" here is different from that used in Sliceomatic. It then creates n subplots, one per "slice", and assigns to each subplot only the values of f that fall within the corresponding "slice".

Here are a few screenshots:

This is a 4-slice plot of the normal distribution function f(x, y, z) = exp(-(x^2 + y^2 + z^2)):

The rainbow color map was used, so blue and purple shades in the fourth slice point to the local maximum.

This is a 3-slice plot of the "quadratic" function f(x, y, z) = x^2 + y^2 - z^2:

The red shades in the first slice indicate the proximity of one of the two local minima.

The following two screenshots show the function f(x, y, z) = sin(x)/x + sin(y)/y + sin(z)/z:


The second screen shot is a "zoom in" on the first one where the x, y, and z axis ranges are [-5, 5] instead of [-10, 10]. Notice the 7 local maxima (blue) in the first screen shot; only the central maximum is captured in the second one.

Here is the code of the first function (normal distribution) to play with:

----
%
% Demo mySlices3D - Normal distribution
% f(x, y, z) = exp(-(x^2 + y^2 + z^2))
%

% Compute x, y, z, c
s = linspace(0, 1, 20);
m = length(s);
x = []; y = []; z = []; f = [];
ct = pi;  % arbitrary constant

for i = 1:m
    for j = 1:m
        for k = 1:m 
            x = [x, s(i)];
            y = [y, s(j)];
            z = [z, s(k)];
            f = [f, ct * exp(-(s(i)^2 + s(j)^2 + s(k)^2))];       
        end
    end
end

[result, errmsg] = mySlices3D(x, y, z, f, 4);
if (result != 1)
    disp(errmsg);
end
----

Here is the code of the mySlices3D function: (Updated on Aug 7, 2017)

----
function [result, errmsg] = mySlices3D(xv, yv, zv, fv, n)

% Expecting the best
result = 1;
errmsg = "";

lx = length(xv);
ly = length(yv);
lz = length(zv);
lf = length(fv);

% n(slices) must be in 1..12
if !((n > 0) && (n <= 12))
    result = 0;
    errmsg = "Number of slices must be between 1 and 12";
    return;
end
nslices = n;

% Vectors x, y, z, c must have the same length
if !((lx == ly) && (ly == lz) && (lz == lf))
    result = 0;
    errmsg = "Vectors x, y, z, f must have the same length";
    return;
end
m = lx;

% Vector length must be >= 2
if (lx < 2)
    result = 0;
    errmsg = "Vector lenght must be at least 2";
    return;
end

% Get limits for vectors xv, yv, zv: deltas must be > 0
xlimits = [min(xv), max(xv)];
ylimits = [min(yv), max(yv)];
zlimits = [min(zv), max(zv)];
if !((xlimits(1) < xlimits(2)) && (ylimits(1) < ylimits(2)) && (zlimits(1) < zlimits(2)))
    result = 0;
    errmsg = "Vectors x, y, z must have different minimum and maximum values";
    return;
end
flimits = [min(fv), max(fv)];

% Normalize fv: [flimits(1), flimits(2)] -> [0, 1];
delta = flimits(2) - flimits(1);
for i = 1:length(fv)
    fv(i) = (fv(i) - flimits(1)) / delta;
end
flimits = [min(fv), max(fv)];

%  Setup color palette
ncolours = 512;
colmap = rainbow(ncolours);

% Initialize x, y, z, c
slice = linspace(0, 1, nslices + 1);  % [0, 1]
x = {};
y = {};
z = {};
c = {};

% Compute x, y, z, c for scatter3
for i = 1:m
    for idx = 1:nslices
        idx_next = idx + 1;
        if ( ((slice(idx) <= fv(i)) && (fv(i) < slice(idx_next))) || ((idx == nslices) && (fv(i) == slice(idx_next))))
            if length(x) < idx
                x{idx} = [];
                y{idx} = [];
                z{idx} = [];
                c{idx} = [];
            endif
            x{idx} = [x{idx}, xv(i)];
            y{idx} = [y{idx}, yv(i)];
            z{idx} = [z{idx}, zv(i)];
            c{idx} = [c{idx}, fv(i)];
        endif
    end
end

% Convert c -> palette colours
for i = 1:length(c)
    temp = [];
    for j = 1:length(c{i})
        temp = [temp; colmap(max(1, round(c{i}(j) * ncolours)), :)]; 
    end
    c{i} = temp;
end

% Scatter 3D plots
figure;
for i=1:nslices
    subplot(1, nslices, i);
    scatter3(x{i}', y{i}', z{i}', 512, c{i}, "filled");
    xlim(xlimits); ylim(ylimits); zlim(zlimits);
    xlabel('x'); ylabel('y'); zlabel('z'); 
end
----

Feel free to change the number of colors (currently 512) and the color map (currently "rainbow") as you like.

As I think that even a grade 6 student could write this kind of code these days, the code above is free to use as you see fit and in any kind of project, commercial or non-commercial. Cheers!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

Doug Stewart-4


On Sat, Aug 5, 2017 at 11:40 PM, lcocea <[hidden email]> wrote:
Anybody know where to find a tool like Sliceomatic
(https://www.mathworks.com/matlabcentral/fileexchange/764-sliceomatic) for
Octave? Any help will be greatly appreciated. I couldn't find anything
similar on Google so yesterday afternoon I wrote some simple code for my
project to visualize "slices" of a function f(x, y, z) using scatter3.

The parameters of the 3d plotting function are the x, y, z, and f vectors,
and the number of slices n. The algorithm divides the max(f) - min(f)
interval into n - 1 intervals, or "slices". Note that the term "slice" here
is different from that used in Sliceomatic. It then creates n subplots, one
per "slice", and assigns to each subplot only the values of f that fall
within the corresponding "slice".

Here are a few screenshots:

This is a 4-slice plot of the normal distribution function f(x, y, z) =
exp(-(x^2 + y^2 + z^2)):
<http://octave.1599824.n4.nabble.com/file/n4684342/demo_normal.png>
The rainbow color map was used, so blue and purple shades in the fourth
slice point to the local maximum.

This is 3-slice plot of the "quadratic" function f(x, y, z) = x^2 + y^2 -
z^2:
<http://octave.1599824.n4.nabble.com/file/n4684342/demo_multipit1.png>
The red shades in the first slice indicate the proximity of one of the two
local minima.

The following two screenshots show the function f(x, y, z) = sin(x)/x +
sin(y)/y + sin(z)/z:
<http://octave.1599824.n4.nabble.com/file/n4684342/demo_multipit2_b.png>
<http://octave.1599824.n4.nabble.com/file/n4684342/demo_multipit2_a.png>
The second one is a "zoom in" of the first one where the x, y, and z axis
ranges are [-5, 5] instead of [-10, 10].

Here is the code of the first function to play with:

----

----

Here is the code of the myScatter3d function:

----

----

I don't see the code!!! please send it.

 

Feel free to change the number of colors (currently 512) and the color map
(currently "rainbow") as you like.

As I think that even a grade 6 could write this kind of code these days, the
code above is free to use as you see fit and in any kind of project,
commercial or non-commercial. Cheers!



--
View this message in context: http://octave.1599824.n4.nabble.com/Sliceomatic-for-Octave-Plotting-slices-of-f-x-y-z-tp4684342.html
Sent from the Octave - General mailing list archive at Nabble.com.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

lcocea
Hi Doug,

Try another browser, the code is there and I can see it even without logging in, both on my desktop (Chrome, Edge) and on my iPhone (Safari).
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

lcocea
In reply to this post by Doug Stewart-4
Doug, just wondering if the code is not shown in your reply because it was originally formatted as "raw text" and it was distributed to the mailing list in text-only mode. Can you see the screen shots? I posted the message at http://octave.1599824.n4.nabble.com/Sliceomatic-for-Octave-Plotting-quot-slices-quot-of-f-x-y-z-td4684342.html please open this link and let me know if this works for you. Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

Doug Stewart-4


On Sun, Aug 6, 2017 at 10:36 AM, lcocea <[hidden email]> wrote:
Doug, just wondering if the code is not shown in your reply because it was
originally formatted as "raw text" and it was distributed to the mailing
list in text-only mode. Can you see the screen shots? I posted the message
at
http://octave.1599824.n4.nabble.com/Sliceomatic-for-Octave-Plotting-quot-slices-quot-of-f-x-y-z-td4684342.html
please open this link and let me know if this works for you. Thanks.



--
View this message in context: http://octave.1599824.n4.nabble.com/Sliceomatic-for-Octave-Plotting-slices-of-f-x-y-z-tp4684342p4684346.html
Sent from the Octave - General mailing list archive at Nabble.com.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

Yes I can see that, and no it is not visible in Ubuntu chrome or Firefox, or samsung android gmail or  samsung browser

--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

lcocea
This post was updated on .
OK, reposting the two code sections w/o formatting, I hope this helps.

----

function [result, errmsg] = mySlices3D(xv, yv, zv, fv, n)

% Expecting the best
result = 1;
errmsg = "";

lx = length(xv);
ly = length(yv);
lz = length(zv);
lf = length(fv);

% n(slices) must be in 1..12
if !((n > 0) && (n <= 12))
    result = 0;
    errmsg = "Number of slices must be between 1 and 12";
    return;
end
nslices = n;

% Vectors x, y, z, c must have the same length
if !((lx == ly) && (ly == lz) && (lz == lf))
    result = 0;
    errmsg = "Vectors x, y, z, f must have the same length";
    return;
end
m = lx;

% Vector length must be >= 2
if (lx < 2)
    result = 0;
    errmsg = "Vector lenght must be at least 2";
    return;
end

% Get limits for vectors xv, yv, zv: deltas must be > 0
xlimits = [min(xv), max(xv)];
ylimits = [min(yv), max(yv)];
zlimits = [min(zv), max(zv)];
if !((xlimits(1) < xlimits(2)) && (ylimits(1) < ylimits(2)) && (zlimits(1) < zlimits(2)))
    result = 0;
    errmsg = "Vectors x, y, z must have different minimum and maximum values";
    return;
end
flimits = [min(fv), max(fv)];

% Normalize fv: [flimits(1), flimits(2)] -> [0, 1];
delta = flimits(2) - flimits(1);
for i = 1:length(fv)
    fv(i) = (fv(i) - flimits(1)) / delta;
end
flimits = [min(fv), max(fv)];

%  Setup color palette
ncolours = 512;
colmap = rainbow(ncolours);

% Initialize x, y, z, c
slice = linspace(0, 1, nslices + 1);  % [0, 1]
x = {};
y = {};
z = {};
c = {};

% Compute x, y, z, c for scatter3
for i = 1:m
    for idx = 1:nslices
        idx_next = idx + 1;
        if ( ((slice(idx) <= fv(i)) && (fv(i) < slice(idx_next))) || ((idx == nslices) && (fv(i) == slice(idx_next))))
            if length(x) < idx
                x{idx} = [];
                y{idx} = [];
                z{idx} = [];
                c{idx} = [];
            endif
            x{idx} = [x{idx}, xv(i)];
            y{idx} = [y{idx}, yv(i)];
            z{idx} = [z{idx}, zv(i)];
            c{idx} = [c{idx}, fv(i)];
        endif
    end
end

% Convert c -> palette colours
for i = 1:length(c)
    temp = [];
    for j = 1:length(c{i})
        temp = [temp; colmap(max(1, round(c{i}(j) * ncolours)), :)];
    end
    c{i} = temp;
end

% Scatter 3D plots
figure;
for i=1:nslices
    subplot(1, nslices, i);
    scatter3(x{i}', y{i}', z{i}', 512, c{i}, "filled");
    xlim(xlimits); ylim(ylimits); zlim(zlimits);
    xlabel('x'); ylabel('y'); zlabel('z');
end

----

<quote author="Doug Stewart-4">
On Sun, Aug 6, 2017 at 10:36 AM, lcocea <[hidden email]> wrote:

> Doug, just wondering if the code is not shown in your reply because it was
> originally formatted as "raw text" and it was distributed to the mailing
> list in text-only mode. Can you see the screen shots? I posted the message
> at
> http://octave.1599824.n4.nabble.com/Sliceomatic-for-
> Octave-Plotting-quot-slices-quot-of-f-x-y-z-td4684342.html
> please open this link and let me know if this works for you. Thanks.
>
>
>
> --
> View this message in context: http://octave.1599824.n4.
> nabble.com/Sliceomatic-for-Octave-Plotting-slices-of-f-x-
> y-z-tp4684342p4684346.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>

Yes I can see that, and no it is not visible in Ubuntu chrome or Firefox,
or samsung android gmail or  samsung browser
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

Doug Stewart-4


On Sun, Aug 6, 2017 at 11:00 AM, lcocea <[hidden email]> wrote:
OK, reposting the two code sections w/o formatting, I hope this helps.

----
%
% Demo myScatter3d - Normal distribution
% f(x, y, z) = exp(-(x^2 + y^2 + z^2))
%

% Compute x, y, z, c
s = linspace(0, 1, 20);
m = length(s);
x = []; y = []; z = []; f = [];
ct = pi;  % arbitrary constant

for i = 1:m
    for j = 1:m
        for k = 1:m
            x = [x, s(i)];
            y = [y, s(j)];
            z = [z, s(k)];
            f = [f, ct * exp(-(s(i)^2 + s(j)^2 + s(k)^2))];
        end
    end
end

[result, errmsg] = myScatter3d(x, y, z, f, 4);
if (result != 1)
    disp(errmsg);
end
----

----
function [result, errmsg] = myScatter3d(xv, yv, zv, fv, n)

% Expecting the best
result = 1;
errmsg = "";

lx = length(xv);
ly = length(yv);
lz = length(zv);
lf = length(fv);

% n(slices) must be in 1..12
if !((n > 0) && (n <= 12))
    result = 0;
    errmsg = "Number of slices must be between 1 and 12";
    return;
end
nslices = n;

% Vectors x, y, z, c must have the same length
if !((lx = ly) && (ly = lz) && (lz = lf))
    result = 0;
    errmsg = "Vectors x, y, z, f must have the same length";
    return;
end
m = lx;

% Vector size must be >= 2
if (lx < 2)
    result = 0;
    errmsg = "Vector size must be at least 2";
    return;
end

% Get limits for vectors xv, yv, zv: must be > 0
xlimits = [min(xv), max(xv)];
ylimits = [min(yv), max(yv)];
zlimits = [min(zv), max(zv)];
if !((xlimits(1) < xlimits(2)) || (ylimits(1) < ylimits(2)) || (zlimits(1) <
zlimits(2)))
    result = 0;
    errmsg = "Vectors x, y, z must have different minimum and maximum
values";
    return;
end
flimits = [min(fv), max(fv)];

% Normalize fv: [flimits(1), flimits(2)] -> [0, 1];
delta = flimits(2) - flimits(1);
for i = 1:length(fv)
    fv(i) = (fv(i) - flimits(1)) / delta;
end
flimits = [min(fv), max(fv)];

%  Setup color palette
ncolours = 512;
colmap = rainbow(ncolours);

% Initialize x, y, z, c
slice = linspace(0, 1, nslices + 1);  % [0, 1]
x = {};
y = {};
z = {};
c = {};

% Compute x, y, z, c for scatter3
for i = 1:m
    for idx = 1:nslices
        if (slice(idx) <= fv(i)) && (fv(i) < slice(idx + 1))
            if length(x) < idx
                x{idx} = [];
                y{idx} = [];
                z{idx} = [];
                c{idx} = [];
            endif
            x{idx} = [x{idx}, xv(i)];
            y{idx} = [y{idx}, yv(i)];
            z{idx} = [z{idx}, zv(i)];
            c{idx} = [c{idx}, fv(i)];
        endif
    end
end

% Convert c -> palette colours
for i = 1:length(c)
    temp = [];
    for j = 1:length(c{i})
        temp = [temp; colmap(max(1, round(c{i}(j) * ncolours)), :)];
    end
    c{i} = temp;
end

% Scatter 3D plots
for i=1:nslices
    subplot(1, nslices, i);
    scatter3(x{i}', y{i}', z{i}', 512, c{i}, "filled");
    xlim(xlimits); ylim(ylimits); zlim(zlimits);
    xlabel('x'); ylabel('y'); zlabel('z');
end
----


Yes this is good. 


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sliceomatic for Octave? Plotting "slices" of f(x, y, z)

lcocea
This post was updated on .
Please note that the code of the plotting function was revised (bug fixes) on Aug 7, 2017. Also, the function was renamed to "mySlices3D". The previous posts of the OP in this thread (http://octave.1599824.n4.nabble.com/Sliceomatic-for-Octave-Plotting-quot-slices-quot-of-f-x-y-z-td4684342.html) were updated accordingly.
Loading...