ARIMA prediction always give zero values in python - arima

I'm t**rying to predict stock market using ARIMA but the predicted values are always zero if the differencing value is greater than zero how should i solve this problem.
another question when using forecast funtion instead of predict function it gives me another result. **
this is my code
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_absolute_error
import pandas as pd
df = pd.read_csv('D:\SBIN.csv', names = ['Date','Price'], header = 0, index_col = 0)
#series=pd.DataFrame(df)
print(len(df))
X = df.values
size = int(len(X) * 0.955)
print(size)
train = df[:size]
print(len(train))
test = df[size:]
history = [x for x in train.Price]
print(history)
predictions = list()
for t in range(len(test)):
history_df = pd.DataFrame(history)
model = ARIMA(history_df.astype(float), order=(1,1,1))
model_fit = model.fit()
print(model_fit.summary())
predicted = model_fit.predict(start=0,end=0)
yhat = predicted
predictions.append(yhat)
history.append(yhat)
predict=pd.DataFrame(predictions,index = test.index)
print(predict)
in other websites and papers they use history.append(test[i]) instead of history.append(yhat) but i think they should use the second as we should fit the model based on the predicted values not the original to predict the future.
the output of predict funtion is:
Date
12/17/2015 0.0
12/18/2015 0.0
12/21/2015 0.0
12/22/2015 0.0
12/23/2015 0.0
12/24/2015 0.0
12/28/2015 0.0
12/29/2015 0.0
12/30/2015 0.0
12/31/2015 0.0
1/1/2016 0.0
1/4/2016 0.0
1/5/2016 0.0
1/6/2016 0.0
1/7/2016 0.0
1/8/2016 0.0
1/11/2016 0.0
1/12/2016 0.0
1/13/2016 0.0
1/14/2016 0.0
1/15/2016 0.0
1/18/2016 0.0
1/19/2016 0.0
while the output of forecast is:
12/17/2015 227.398678
12/18/2015 227.343661
12/21/2015 227.374336
12/22/2015 227.357233
12/23/2015 227.366769
12/24/2015 227.361453
12/28/2015 227.364416
12/29/2015 227.362761
12/30/2015 227.363684
12/31/2015 227.363169
1/1/2016 227.363456
1/4/2016 227.363296
1/5/2016 227.363385
1/6/2016 227.363336
1/7/2016 227.363363
1/8/2016 227.363348
1/11/2016 227.363357
1/12/2016 227.363352
1/13/2016 227.363354
1/14/2016 227.363353
1/15/2016 227.363354
1/18/2016 227.363353
1/19/2016 227.363354
**trying to predict stock market using ARIMA but the predicted values are always zero if the differencing value is greater than zero how should i solve this problem.
another question when using forecast funtion instead of predict function it gives me another result. **

Related

How to convert sparse matrix to dense matrix in Julia

How do you convert a sparse matrix to a dense matrix in Julia? According to this I should be able to use full or Matrix, however full is evidently not standard in the SparseArrays module, and when I try to use Matrix:
I = []
J = []
A = []
for i in 1:3
push!(I, i)
push!(J, i^2)
push!(A, sqrt(i))
end
sarr = sparse(I, J, A, 10, 10)
arr = Matrix(sarr)
I get this error:
Exception has occurred: MethodError
MethodError: no method matching zero(::Type{Any})
It is enough to do collect(sarr) or Matrix(sarr).
Note, however that your code uses untyped containers which is not recommended. Indexes in arrays are Ints so it should be:
I = Int[]
J = Int[]
A = Float64[]
for i in 1:3
push!(I, i)
push!(J, i^2)
push!(A, sqrt(i))
end
sarr = sparse(I, J, A, 10, 10)
Now you can do:
julia> collect(sarr)
10×10 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 1.41421 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.73205 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

How can i use gekko python to control the level of a tank of a cstr by manipulating the inlet flow to the tank?- Part 2

I am trying to use GEKKO on PYTHON to control the level of a CSTR tank while manipulating the inlet flow q. I tried the same problem using a pid controller and it worked. However, on GEKKO the height is not tracking its setpoint. Once I did the doublet test: at a flow rate of 200, the height reached 800 and as I decreased the flowrate to 2, the height was about 0. However, when im putting the height setpoint in GEKKO as 700 or 800, the flowrate is not stopping at 200, it is continuously increasing indefinitely. in addition, I tried putting qout=5.0, Ac=30 and h0=50, it also didn't work.
Below is my code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from gekko import GEKKO
# Steady State Initial Condition
u2_ss=10.0
h_ss=50.0
x0 = np.empty(1)
x0[0]= h_ss
#%% GEKKO nonlinear MPC
m = GEKKO(remote=False)
m.time = [0,0.02,0.04,0.06,0.08,0.1,0.12,0.15,0.2]
Ac=30.0
# initial conditions
h0=50.0
q0=10.0
m.q=m.MV(value=q0,lb=0,ub=100)
m.h= m.CV(value=h0)
m.qout=m.Param(value=5)
m.Equation(m.h.dt()==(m.q- m.qout)/Ac)
#MV tuning
m.q.STATUS = 1
m.q.FSTATUS = 0
m.q.DMAX = 100
m.q.DMAXHI = 5
m.q.DMAXLO = -100
#CV tuning
m.h.STATUS = 1
m.h.FSTATUS = 1
m.h.TR_INIT = 2
m.h.TAU = 1.0
m.h.SP = 55.0
m.options.CV_TYPE = 2
m.options.IMODE = 6
m.options.SOLVER = 3
#%% define CSTR model
def cstr(x,t,u2,Ac):
q=u2
Ac=30.0
# States (2):
# the height of the tank (m)
h=x[0]
# Parameters:
# Calculate height derivative
dhdt=(q-5.0)/Ac
# Return xdot:
xdot = np.zeros(1)
xdot[0]= dhdt
return xdot
# Time Interval (min)
t = np.linspace(0,6,100)
# Store results for plotting
hsp=np.ones(len(t))*h_ss
h=np.ones(len(t))*h_ss
u2 = np.ones(len(t)) * u2_ss
# Set point steps
hsp[0:50] = 55.0
hsp[100:150]=70.0
# Create plot
plt.figure(figsize=(10,7))
plt.ion()
plt.show()
# Simulate CSTR
for i in range(len(t)-1):
# simulate one time period (0.05 sec each loop)
ts = [t[i],t[i+1]]
y = odeint(cstr,x0,ts,args=(u2[i+1],Ac))
# retrieve measurements
h[i+1]= y[-1][0]
# insert measurement
m.h.MEAS=h[i+1]
m.h.SP=hsp[i+1]
# solve MPC
m.solve(disp=True)
# retrieve new q value
u2[i+1] = m.q.NEWVAL
# update initial conditions
x0[0]= h[i+1]
#%% Plot the results
plt.clf()
plt.subplot(2,1,1)
plt.plot(t[0:i],u2[0:i],'b--',linewidth=3)
plt.ylabel('inlet flow')
plt.subplot(2,1,2)
plt.plot(t[0:i],hsp[0:i],'g--',linewidth=3,label=r'$h_{sp}$')
plt.plot(t[0:i],h[0:i],'k.-',linewidth=3,label=r'$h_{meas}$')
plt.xlabel('time')
plt.ylabel('tank level')
plt.legend(loc='best')
plt.draw()
plt.pause(0.01)
The problem is with your input to the simulator y = odeint(cstr,x0,ts,args=(u2[i+1],Ac)). It should be using the value from the prior loop: y = odeint(cstr,x0,ts,args=(u2[i],Ac)). Here is an updated script.
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from gekko import GEKKO
# Steady State Initial Condition
u2_ss=10.0
h_ss=50.0
x0 = np.empty(1)
x0[0]= h_ss
#%% GEKKO nonlinear MPC
m = GEKKO(remote=False)
m.time = [0,0.02,0.04,0.06,0.08,0.1,0.12,0.15,0.2]
Ac=30.0
# initial conditions
h0=50.0
q0=10.0
m.q=m.MV(value=q0,lb=0,ub=100)
m.h= m.CV(value=h0)
m.qout=m.Param(value=5)
m.Equation(Ac * m.h.dt()==m.q- m.qout)
#MV tuning
m.q.STATUS = 1
m.q.FSTATUS = 0
m.q.DMAX = 100
m.q.DMAXHI = 5
m.q.DMAXLO = -100
#CV tuning
m.h.STATUS = 1
m.h.FSTATUS = 1
m.h.TR_INIT = 2
m.h.TAU = 1.0
m.h.SP = 55.0
m.options.CV_TYPE = 2
m.options.IMODE = 6
m.options.SOLVER = 3
#%% define CSTR model
def cstr(x,t,u2,Ac):
q=u2
Ac=30.0
# States (2):
# the height of the tank (m)
h=x[0]
# Parameters:
# Calculate height derivative
dhdt=(q-5)/Ac
# Return xdot:
xdot = np.zeros(1)
xdot[0]= dhdt
return xdot
# Time Interval (min)
t = np.linspace(0,6,100)
# Store results for plotting
hsp=np.ones(len(t))*h_ss
h=np.ones(len(t))*h_ss
u2 = np.ones(len(t)) * u2_ss
# Set point steps
hsp[0:50] = 55.0
hsp[100:150]=70.0
# Create plot
plt.figure(figsize=(10,7))
plt.ion()
plt.show()
# Simulate CSTR
for i in range(len(t)-1):
# simulate one time period (0.05 sec each loop)
ts = [t[i],t[i+1]]
y = odeint(cstr,x0,ts,args=(u2[i],Ac))
# retrieve measurements
h[i+1]= y[-1][0]
# insert measurement
m.h.MEAS=h[i+1]
m.h.SP=hsp[i+1]
# solve MPC
m.solve(disp=False)
# retrieve new q value
u2[i+1] = m.q.NEWVAL
# update initial conditions
x0[0]= h[i+1]
#%% Plot the results
if i%10==0:
plt.clf()
plt.subplot(2,1,1)
plt.plot(t[0:i],u2[0:i],'b--',linewidth=3)
plt.ylabel('inlet flow')
plt.subplot(2,1,2)
plt.plot(t[0:i],hsp[0:i],'g--',linewidth=3,label=r'$h_{sp}$')
plt.plot(t[0:i],h[0:i],'k.-',linewidth=3,label=r'$h_{meas}$')
plt.xlabel('time')
plt.ylabel('tank level')
plt.legend(loc='best')
plt.draw()
plt.pause(0.01)

Julia: How to execute functions in parallel?

I want to run functions in parallel. These functions are executed many times in a loop.
coordSys = SharedArray{Bool}([true,false,true,true]);
dir = SharedArray{Int8}([1,2,3,2]);
load = SharedArray{Float64}([8,-7.5,7,-8.5]);
L = SharedArray{Float64}([400,450,600,500]);
r = SharedArray{Float64}([0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0]);
Obviously these vectors will be huge, but for simplicity I just put this limited size.
Operation without parallel computing:
function unifLoad(coordSys,dir,load,L,ri)
if coordSys == true
if dir == 1
Q = [load;0;0];
elseif dir == 2
Q = [0;load;0];
elseif dir == 3
Q = [0;0;load];
end
q = ri*Q; #matrix multiplication
P = q[1]*L/2;
V = q[2]*L/2;
M = -q[3]*L*L/12;
f = [P;V;M];
else
f = [1.0;1.0;1.0];
end
return f
end
running the loop:
var = zeros(12)
for i = 1:length(L)
var[3*(i-1)+1:3*i] = unifLoad(coordSys[i],dir[i],load[i],L[i],r[3*(i-1)+1:3*i,:]);
end
The returned value is:
var
12-element Array{Float64,1}:
0.0
0.0
-1.06667e5
1.0
1.0
1.0
2100.0
0.0
-0.0
0.0
2125.0
-0.0
Operation with parallel computing
I've been trying to implement the same function in parallel, but without getting the same results.
# addprocs(3)
#everywhere function unifLoad_Parallel(coordSys,dir,load,L,ri)
if coordSys == true
if dir == 1
Q = [load;0;0];
elseif dir == 2
Q = [0;load;0];
elseif dir == 3
Q = [0;0;load];
end
q = ri*Q; # Matrix multiplication (ri -> Array 3x3)
P = q[1]*L/2;
V = q[2]*L/2;
M = -q[3]*L*L/12;
f = [P;V;M];
else
f = [1.0;1.0;1.0];
end
return f
end
running the parallel loop:
var_parallel = SharedArray{Float64}(12);
#parallel for i = 1:length(L)
var_parallel[3*(i-1)+1:3*i] = unifLoad_Parallel(coordSys[i],dir[i],load[i],L[i],r[3*(i-1)+1:3*i,:]);
end
The returned value is:
var_parallel
12-element SharedArray{Float64,1}:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
On my Julia 0.6.3 the parallel code returns the same result so I am unable to reproduce the problem (also I do not encounter the issue #SalchiPapa reports).
However, I would like to note that this code actually should work faster with threads (I assume that the real problem is much larger). Here is the code you could use (I used an equivalent implementation to your which is a bit shorter - but the only significantly relevant change is that I wrap it in a function which provides dramatic performance gains). The crucial issue that all arrays except var are shared but only read. And var is written but only once at each entry and not read from. This is the case where it is safe to use threading which has a lower overhead.
Here is an example code (you have to define JULIA_NUM_TREADS environment variable before starting Julia and set it to number of threads you want - most probably 4 is what you want):
using Base.Threads
function experiment()
coordSys = [true,false,true,true];
dir = [1,2,3,2];
load = [8,-7.5,7,-8.5];
L = [400,450,600,500];
r = [0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0];
unifLoad(coordSys,dir,load,L,r, i) =
coordSys ? load * L * r[3*(i-1)+1:3*i, dir] .* [0.5, 0.5, -L/12] : [1.0, 1.0, 1.0]
var = zeros(12)
#threads for i = 1:length(L)
var[3*(i-1)+1:3*i] = unifLoad(coordSys[i],dir[i],load[i],L[i],r,i);
end
var
end
Also here is a bit simplified code for parallel processing using similar ideas:
coordSys = SharedArray{Bool}([true,false,true,true]);
dir = SharedArray{Int8}([1,2,3,2]);
load = SharedArray{Float64}([8,-7.5,7,-8.5]);
L = SharedArray{Float64}([400,450,600,500]);
r = SharedArray{Float64}([0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0
0.0 0.0 1.0; 0.0 -1.0 0.0; 1.0 0.0 0.0]);
#everywhere unifLoad(coordSys,dir,load,L,r,i) =
coordSys ? load * L * r[3*(i-1)+1:3*i, dir] .* [0.5, 0.5, -L/12] : [1.0, 1.0, 1.0]
vcat(pmap(i -> unifLoad(coordSys[i],dir[i],load[i],L[i],r,i), 1:length(L))...)
Here pmap is mostly used to simplify the code so that you do not need #sync.

Processing & Reactivision - Remove Movie from stage

I'm using Processing and Reactivision to make an interactive table, each symbol plays a video. The problem I have is that when you remove a symbol the video remains and doesn't reset the stage, so when another symbol is added it plays below the video that has already been played. What I basically need to do is, once a symbol is removed the stage resets / clears. Any help would be greatly appreciated. Code below
/*
TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
import java.util.*;
import processing.video.*;
TuioProcessing tuioClient;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 0;
float table_size = 760;
float scale_factor = 1;
PFont font;
PImage img;
Movie theMov;
Movie theMov2;
void setup()
{
//size(screen.width,screen.height);
size(720,480);
noStroke();
fill(0);
img = loadImage("background.jpeg");
theMov = new Movie(this, "gibi.mov");
theMov2 = new Movie(this, "rick.mov");
loop();
frameRate(30);
//noLoop();
font = createFont("Arial", 18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
}
// within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
// from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
void draw()
{
background(247,73,2);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
image(theMov, 0, 0);
image(theMov2, 0, 0);
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
pushMatrix();
//translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
popMatrix();
fill(255);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
if ( tobj.getSymbolID() == 0) {
theMov.play();
}
if ( tobj.getSymbolID() == 5) {
theMov2.play();
}
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
if ( tobj.getSymbolID() == 0) {
theMov.stop();
}
if ( tobj.getSymbolID() == 5) {
theMov2.stop();
}
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void movieEvent(Movie m) {
m.read();
}
TUIO: missing or wrong 'addTuioCursor(TuioCursor tcur)' method implementation
TUIO:missing or wrong 'removeTuioCursor(TuioCursor tcur)' method implementation
TUIO: missing or wrong 'updateTuioCursor(TuioCursor tcur)' method implementation
add object 0 (3) 0.37709242 0.8826896 0.0
update object 0 (3) 0.37709242 0.8737366 0.0742998 0.2419723 0.0 6.5397916 0.0
update object 0 (3) 0.37709242 0.867628 0.0742998 0.21064281 0.0 -1.080327 0.0
update object 0 (3) 0.37597793 0.86093885 0.0742998 0.19375265 0.0 -0.48257622 0.0
update object 0 (3) 0.37597793 0.8532117 0.0742998 0.23415594 0.0 1.2243422 0.0
update object 0 (3) 0.37896088 0.847547 0.0742998 0.15614879 0.0 -1.9026133 0.0
update object 0 (3) 0.38109976 0.84274954 0.0742998 0.1811261 0.0 0.86128664 0.0
update object 0 (3) 0.37981042 0.838495 0.0742998 0.116989255 0.0 -1.6878119 0.0
update object 0 (3) 0.38065997 0.838495 0.0742998 0.033981323 0.0 -3.3203173 0.0
update object 0 (3) 0.38252023 0.8398745 0.020077623 0.048248682 -0.17978598 0.29723665 -3.7455413
update object 0 (3) 0.38252023 0.8426642 0.020077623 0.09963129 0.0 1.835093 6.4209275
update object 0 (3) 0.38413188 0.8452217 0.020077623 0.10424032 0.0 0.1589322 0.0
update object 0 (3) 0.38413188 0.8452217 0.020077623 0.0 0.0 -4.009243 0.0
update object 0 (3) 0.38325775 0.8440129 0.020077623 0.033149652 0.0 0.73665893 0.0
update object 0 (3) 0.38325775 0.8427188 0.020077623 0.04313787 0.0 0.33294064 0.0
update object 0 (3) 0.38325775 0.8414247 0.020077623 0.05391985 0.0 0.4492492 0.0
update object 0 (3) 0.38222247 0.8414247 0.020077623 0.032352652 0.0 -0.67397493 0.0
update object 0 (3) 0.3830283 0.8414247 0.020077623 0.01714534 0.0 -0.32355985 0.0
update object 0 (3) 0.3830283 0.8424627 0.020077623 0.03992365 0.0 0.8760888 0.0
update object 0 (3) 0.3830283 0.8424627 0.020077623 0.0 0.0 -1.596946 0.0
update object 0 (3) 0.3830283 0.84123003 0.020077623 0.037354052 0.0 1.131941 0.0
update object 0 (3) 0.382105 0.84123003 0.020077623 0.026379995 0.0 -0.31354448 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.081238225 0.0 1.8286079 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.0 0.0 -2.3210924 0.0
update object 0 (3) 0.381026 0.83904475 0.020077623 0.0 0.0 0.0 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.06294223 0.0 1.9669446 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.0 0.0 -1.851242 0.0
update object 0 (3) 0.37948263 0.8377506 0.020077623 0.0 0.0 0.0 0.0
update object 0 (3) 0.37759778 0.8357668 0.020077623 0.08048391 0.0 2.367174 0.0
update object 0 (3) 0.37759778 0.8357668 6.280155 0.0 -0.11492891 -2.5151222 -3.5915282
update object 0 (3) 0.3761473 0.8357668 6.280155 0.04395427 0.0 1.3319477 3.4826941
update object 0 (3) 0.3761473 0.8336429 6.280155 0.060682636 0.0 0.47795326 0.0
update object 0 (3) 0.3761473 0.8336429 6.280155 0.0 0.0 -1.8963323 0.0
update object 0 (3) 0.3761473 0.8336429 6.280155 0.0 0.0 0.0 0.0
update object 0 (3) 0.37695312 0.8336429 0.0 0.02441912 0.014613922 0.7399733 0.44284612
update object 0 (3) 0.37695312 0.8336429 6.2801733 0.0 -0.013315989 -0.6783089 -0.77583086
update object 0 (3) 0.37695312 0.83588284 0.0010060358 0.072256215 0.020629212 2.3308456 1.0950066
update object 0 (3) 0.37695312 0.83588284 6.2821803 0.0 -0.010325174 -2.3308456 -0.99852866
update object 0 (3) 0.37695312 0.83588284 0.0080239 0.0 0.04105623 0.0 1.4680401
update object 0 (3) 0.37780267 0.83588284 6.2811527 0.026547907 -0.050016034 0.82962203 -2.846008
update object 0 (3) 0.37780267 0.83588284 0.0030150663 0.0 0.012552409 -0.41481102 0.97763187
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.040291037 0.0 1.1511725 -0.35864025
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.0 0.0 -1.2209406 0.0
update object 0 (3) 0.37780267 0.83447266 0.0030150663 0.0 0.0 0.0 0.0
update object 0 (3) 0.37780267 0.83548 0.0030150663 0.031478703 0.0 0.9837094 0.0
update object 0 (3) 0.37780267 0.8365726 0.0030150663 0.036420427 0.0 0.16472414 0.0
update object 0 (3) 0.37780267 0.8365726 0.0030150663 0.0 0.0 -1.0405836 0.0
update object 0 (3) 0.37780267 0.8365726 6.2781296 0.0 -0.03568064 0.0 -0.9911289
update object 0 (3) 0.37780267 0.83493704 6.2781296 0.054518383 0.0 1.8172795 1.1893547
update object 0 (3) 0.37780267 0.8339297 0.0010060358 0.02878053 0.027564982 -0.73536724 0.7875709
update object 0 (3) 0.37780267 0.8339297 6.274067 0.0 -0.051979534 -0.9284042 -2.565952
update object 0 (3) 0.37780267 0.83292246 6.274067 0.028778825 0.0 0.82225215 1.4851296
update object 0 (3) 0.37780267 0.83292246 0.001011122 0.0 0.048852257 -0.87208563 1.4803715
update object 0 (3) 0.37780267 0.83421654 6.27816 0.041744404 -0.030989556 1.3465937 -2.5755424
update object 0 (3) 0.37876967 0.83421654 0.0040691537 0.027628625 0.041355457 -0.40330797 2.0670004
update object 0 (3) 0.37876967 0.83421654 6.2811527 0.0 -0.029428765 -0.83723104 -2.1449766
update object 0 (3) 0.37876967 0.83421654 6.2811527 0.0 0.0 0.0 0.9196489
update object 0 (3) 0.37876967 0.8320927 6.2811527 0.06636977 0.0 2.0740552 0.0
update object 0 (3) 0.37876967 0.8320927 6.2811527 0.0 0.0 -1.8436048 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.032862026 0.0 0.995819 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.0 0.0 -1.0954009 0.0
update object 0 (3) 0.37768522 0.8320927 6.2811527 0.0 0.0 0.0 0.0
update object 0 (3) 0.37865222 0.8320927 6.2811527 0.02844123 0.0 0.8365067 0.0
update object 0 (3) 0.37865222 0.83096933 6.2811527 0.025531108 0.0 -0.06613915 0.0
update object 0 (3) 0.37865222 0.8295045 6.2811527 0.056340143 0.0 1.1849629 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0410527 0.0 -0.3474419 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0 0.0 -1.7105291 0.0
update object 0 (3) 0.37865222 0.8276982 6.2811527 0.0 0.0 0.0 0.0
update object 0 (3) 0.37865222 0.8256836 6.2811527 0.05444804 0.0 1.4715686 0.0
update object 0 (3) 0.37865222 0.8256836 6.2811527 0.0 0.0 -2.0941553 0.0
update object 0 (3) 0.37865222 0.8243041 6.2811527 0.04756862 0.0 1.6402973 0.0
update object 0 (3) 0.37865222 0.8243041 6.2811527 0.0 0.0 -1.3591034 0.0
update object 0 (3) 0.37865222 0.8262572 6.2811527 0.052787162 0.0 1.4266801 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.23362696 0.0 6.697769 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.0 0.0 -6.8713803 0.0
update object 0 (3) 0.38131282 0.8319766 6.2811527 0.0 0.0 0.0 0.0
remove object 0 (3)
add object 5 (4) 0.3838997 0.7569657 0.0
update object 5 (4) 0.3838997 0.74766445 1.4853827 0.26574987 0.0 7.5928535 0.0
update object 5 (4) 0.3820777 0.7410607 1.4853827 0.19572896 0.0 -2.000597 0.0
update object 5 (4) 0.3820777 0.7364408 1.4853827 0.10499765 0.0 -2.0620756 0.0
update object 5 (4) 0.3820777 0.73477453 1.4853827 0.057456825 0.0 -1.6393389 0.0
update object 5 (4) 0.38115984 0.73477453 1.4853827 0.035301536 0.0 -0.8521265 0.0
update object 5 (4) 0.38115984 0.7383939 1.4853827 0.097820885 0.0 1.6897122 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.10322214 0.0 0.15432154 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.0 0.0 -4.1288853 0.0
update object 5 (4) 0.38267866 0.7416719 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38355276 0.7416719 1.4853827 0.024974687 0.0 0.7135625 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.04751732 0.0 0.9392763 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.0 0.0 -1.3576376 0.0
update object 5 (4) 0.38355276 0.74281234 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38355276 0.7438743 1.4853827 0.03218109 0.0 0.9751846 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.07275257 0.0 1.229439 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.0 0.0 -2.2046237 0.0
update object 5 (4) 0.3850224 0.7457728 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.029562335 0.0 0.77795625 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.0 0.0 -0.9854112 0.0
update object 5 (4) 0.3850224 0.7446494 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.3850224 0.74582744 1.4853827 0.03681332 0.0 1.1504161 0.0
update object 5 (4) 0.38592112 0.74582744 1.4853827 0.028084962 0.0 -0.27276114 0.0
update object 5 (4) 0.38592112 0.74582744 1.4853827 0.0 0.0 -0.85105944 0.0
update object 5 (4) 0.38592112 0.74703616 1.4853827 0.03555066 0.0 1.0456077 0.0
update object 5 (4) 0.38592112 0.74703616 1.4853827 0.0 0.0 -1.1109581 0.0
update object 5 (4) 0.38592112 0.7480674 1.4853827 0.029463427 0.0 0.8418122 0.0
update object 5 (4) 0.38727325 0.7480674 1.4853827 0.04097404 0.0 0.34880644 0.0
update object 5 (4) 0.38727325 0.7504541 1.4853827 0.0769919 0.0 1.1618665 0.0
update object 5 (4) 0.38846976 0.751919 1.4853827 0.054039527 0.0 -0.6557822 0.0
update object 5 (4) 0.38846976 0.751919 1.4853827 0.0 0.0 -1.1497772 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.034368556 0.0 0.7471425 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.0 0.0 -1.2274483 0.0
update object 5 (4) 0.38846976 0.7534999 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38846976 0.7545311 1.4853827 0.042965014 0.0 1.7902089 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.048351288 0.0 0.21545097 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.0 0.0 -1.4220966 0.0
update object 5 (4) 0.38846976 0.75573987 1.4853827 0.0 0.0 0.0 0.0
update object 5 (4) 0.38936844 0.75927395 1.4853827 0.11395484 0.0 3.5610886 0.0
update object 5 (4) 0.39017427 0.76237434 1.4853827 0.04928318 0.0 -0.99494874 0.0
update object 5 (4) 0.39233226 0.76737326 1.4853827 0.15556657 0.0 3.0366688 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.2914553 0.0 4.11784 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.0 0.0 -7.87717 0.0
update object 5 (4) 0.3962385 0.7761623 1.4853827 0.0 0.0 0.0 0.0
remove object 5 (4)
so I played around with this some more and finally got it to work. The problem was I was continuously drawing the movies in the wrong place as they needed to be within the vector in the draw section, and I needed an if statement to call them when a certain symbol was shown. code below :
/*
TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
import java.util.*;
import processing.video.*;
TuioProcessing tuioClient;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 0;
float table_size = 760;
float scale_factor = 1;
PFont font;
PImage img;
Movie theMov;
Movie theMov2;
PImage bg;
void setup()
{
//size(screen.width,screen.height);
size(1280,720);
noStroke();
fill(0);
//bg = loadImage("bg.png");
theMov = new Movie(this, "isobar.mov");
loop();
frameRate(30);
//noLoop();
font = createFont("Arial", 18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
}
// within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
// from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
void draw()
{
//background(bg);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
pushMatrix();
//translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
popMatrix();
if (tobj.getSymbolID()==0){
image(theMov, 0, 0);
}
if (tobj.getSymbolID()==5){
image(theMov2, 0, 0);
}
//fill(255);
//text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
if (tobj.getSymbolID() == 0){
theMov.play();
}
if (tobj.getSymbolID() == 5){
theMov2.play();
}
if (tobj.getSymbolID()==1){
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
fill(255);
translate(tobj.getScreenX(width),tobj.getScreenY(height));
}
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
if ( tobj.getSymbolID() == 0) {
theMov.stop();
}
if ( tobj.getSymbolID() == 5) {
theMov2.stop();
}
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void movieEvent(Movie m) {
m.read();
}

Create Image from Text File RGB Data in Matlab

I have a text file with RGB data in the form of:
[Pixel 0,0] [Pixel 1,0] [Pixel 2,0]...
[Pixel 0,1] [Pixel 1,1] [Pixel 2,2]...
...
With an input of:
0.0 0.0 0.0 <-- this would be Pixel 0,0
1.0 0.0 0.0
1.0 0.9 0.0
I can create the flag of Germany in size 3x1 with:
%load the data to myData
Germany = reshape(myData,3,1,3);
image(Germany)
The 1px-wide pattern works good as show in picture, however, the goal is to be able to create multiple patterns, e.g. the Germany flag in 3x3 followed by Romania flag in 3x3 or any other pattern of any length and doing that! is where I can not find the proper way to reshape the matrix.
The input that should create the second example shown in picture is this:
|========= Germany Flag ==========| [ Blue ] [ Yellow ] [ Red ]
Black -> 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Red -> 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Yellow-> 1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
Any help is appreciated
Update: Asked by Marcin, the input files are literal as I explained above.
This is the content of the GermanyRomania.txt file:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.0 0.0 1.0 1.0 0.9 0.0 1.0 0.0 0.0
With that file I must create the 2nd pattern in picture (German+Romania Flag), there is ALL the RGB info required to do it.
I don't think you can achieve what you want by simply using the reshape function.
We must take into account that Matlab stores matrices in column-major order (you can read more about it here).
Therefore, before we can use the reshape function, we must have the data matrix in the following format:
[Pixel 0,0]
[Pixel 0,1]
...
[Pixel 1,0]
[Pixel 1,1]
...
[Pixel n,n]
Here's a possible solution:
# data stores the input
height = size(data, 1)
width = size(data, 2)
vertical_data_cell = mat2cell(data, height, 3 * ones(1, width / 3))'
vertical_data = cell2mat(vertical_data_cell)
flags = reshape(vertical_data, height, width / 3, 3)
image(flags)
Note that we make the matrix transformation on lines 4 and 5.
And here is the result for the input you provided:
It also works with different heights.
Here's the input for the flags of Germany, Argentina and Portugal.
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.46 0.66 0.85 0.46 0.66 0.85 0.46 0.66 0.85
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 0.98 0.75 0.29 1.0 1.0 1.0
1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.9 0.0 0.46 0.66 0.85 0.46 0.66 0.85 0.46 0.66 0.85
0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 1.0 0.9 0.0 1.0 0.9 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
And this is the result:

Resources