How to read .mat output files in JModelica? - modelica

To save the time, I'm trying to read the .mat file rather than simulate the model again.
I used scipy.io.loadmat but it didn't work well:
res = loadmat('ChatteringControl_result.mat')
res.keys()
['Aclass', 'dataInfo', 'name', 'data_2', 'data_1', 'description']
The keys are not variable names, and I don't know how to get the variable values.
Then I searched for resolutions, and found DyMat, it works well for other variables but cannot get time.
res1 = DyMat.DyMatFile('ChatteringControl_result.mat')
T = res1['T']
t = res1['time']
KeyError: 'time'
So, how can I get all the results in JModelica?(Without open Matlab of course.)Like, a built-in function in JModelica?
BIG THANKS!

To load the mat file using JModelica you can use this code:
from pyfmi.common.io import ResultDymolaBinary
res = ResultDymolaBinary("MyResult.mat")
var = res.get_variable_data("myVar")
var.t #Time trajectory
var.x #Variable trajectory

https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/technical_details.html#the-matv4-result-file-format describes the format. I think you can also look in a Dymola manual for more details.
As for DyMat, there is no reason to get the time trajectory because you typically lookup what value a variable has at a certain time. The start and stop-times are in the data_1 matrix as far as I remember (or typically get it from the first trajectory in the data_2 matrix). (The data_2 matrix may be interpolated, so the time values stored in it may not reflect the actual steps taken internally by the solvers)

Related

How to handle flag/exception values

In Paraview, I am working with a dataset that uses the value -99999 as a flag value. I'd like to be able to manipulate the dataset without these values causing issues with things like glyphs and colorbars. Nominally, I'd like the data to be "ignored".
A little about the data: I've got both scalar and vector point data, sitting on a fixed 2D spatial mesh at set temporal intervals.
Although -99999 is very far beyond the values the data might otherwise show, using a threshold filter isn't an option because the flag can occur at different places at different times. The way Paraview's threshold filter works means that the point ID to a fixed point in space will change as the number of filtered points changes through time.
In case it matters, data are in a netCDF file that is read in via an XMF header file and the XDMF Reader since the CF reader doesn't work (possibly because of my unstructured triangular mesh). The netCDF data have the _FillValue global attribute, however this doesn't appear to be getting picked up on by Paraview.
You could use a Programmable Filter to replace values below -99999 by NaN. Providing the data is not a vtkMultiblockDataSet, you can use the following script in the programmable filter :
import numpy as np
from vtk.numpy_interface import dataset_adapter as dsa
# name of the array
name = 'name'
# limit
limit = -99999
array = inputs[0].PointData[name].copy()
array[array<=limit] = np.nan
out = dsa.WrapDataObject(self.GetOutput())
out.PointData.append(array, name)
Note: if data of interest is a Cell Data, replace PointData by CellData in the script.
Note 2: the script was tested on ParaView 5.6.

convert struct type to matrix octave

I have a myfile.mat that contains 3000x35 size data. When I load it into a variable as :
a = load('myfile.mat')
It gives struct of size 1x1 . How can I get exact form as matrix. This is required because I need to change certain column values.
(This answer is valid for MATLAB, I am not sure it works exactly the same way in Octave)
You have several options:
Option 1:
If the .mat file only contains one variable, you can do:
a = struct2array(load('myfile.mat')); % MATLAB syntax
a = [struct2cell(load('myfile.mat')){:}]; % Octave syntax
Option 2:
You need to know the name of the variable at the time of saving, which is now a name of a field in this struct. Then you would access it using:
a = load('myfile.mat'); a = a.data;
Option 3 (unrecommended!):
Just remove the a = part of the expression,
load('myfile.mat');
Then the variables inside this file will "magically spawn" in your workspace. This is not recommended because it makes it difficult (impossible?) to see when certain variables are created.
I found the solution myself. I did like this
a = load('myfile.mat')
a = a.data
So that now I can access values of a as matrix with indexing, a(1,2) like this.
It is in octave. I hope it is similar in matlab.

Extracting variables while reading in data files

I am quite new to data analysis, so if this is a rookie question, I'm sorry, I am learning as I go.
I have just started doing some work in variable star astronomy. I have about 100 files for every night of observation that all contain the same basic information (star coordinates, magnitude, etc.). I am loading all of the files into my workspace as arrays using a for-loop
files = dir('*.out');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
I'm only really interested in two columns in each file. Is there a way to extract a column and set it to a vector while this for-loop is running? I'm sure that it's possible, but the actual syntax for it is escaping me.
try using load as a function and save it's output to a variable
files = dir('*.out');
twoCols = {};
for ii=1:length(files)
data = load( files(ii).name, '-ascii' ); % load file into "data"
twoCols{ii} = data(:,1:2); % take only two columns
end
Now variable twoCols holds the two columns of each file in a different cell.
You have to assign the load result to a new variable. Then if lets say your variable is starsInfo you can use
onlyTwoFirst = starsInfo(:,1:2)
That means take all the rows, but only columns 1 and 2.

MATLAB Saving and Loading Feature Vectors

I am trying to load feature vectors into classifiers such as a k-nearest neighbors classifier.
I have my code for GLCM, so I get contrast, correlation, energy, homogeneity in numbers (feature vectors).
My question is, how can I save every set of feature vectors from all the training images? I have seen somewhere that people had a .set file to load into classifiers (may be it is a special case for the particular classifier toolbox).
load 'mydata.set';
for example.
I suppose it does not have to be a .set file.
I'd just need a way to store all the feature vectors from all the training images in a separate file that can be loaded.
I've google,
and I found this that may be useful
but I am not entirely sure.
Thanks for your time and help in advance.
Regards.
If you arrange your feature vectors as the columns of an array called X, then just issue the command
save('some_description.mat','X');
Alternatively, if you want the save file to be readable, say in ASCII, then just use this instead:
save('some_description.txt', 'X', '-ASCII');
Later, when you want to re-use the data, just say
var = {'X'}; % <-- You can modify this if you want to load multiple variables.
load('some_description.mat', var{:});
load('some_description.txt', var{:}); % <-- Use this if you saved to .txt file.
Then the variable named 'X' will be loaded into the workspace and its columns will be the same feature vectors you computed before.
You will want to replace the some_description part of each file name above and instead use something that allows you to easily identify which data set's feature vectors are saved in the file (if you have multiple data sets). Your array of feature vectors may also be called something besides X, so you can change the name accordingly.

Numerical problems in Matlab: Same input, same code -> different output?

I am experiencing problems when I compare results from different runs of my Matlab software with the same input. To narrow the problem, I did the following:
save all relevant variables using Matlab's save() method
call a method which calculates something
save all relevant output variables again using save()
Without changing the called method, I did another run with
load the variables saved above and compare with the current input variables using isequal()
call my method again with the current input variables
load the out variables saved above and compare.
I can't believe the comparison in the last "line" detects slight differences. The calculations include single and double precision numbers, the error is in the magnitude of 1e-10 (the output is a double number).
The only possible explanation I could imagine is that either Matlab looses some precision when saving the variables (which I consider very unlikely, I use the default binary Matlab format) or that there are calculations included like a=b+c+d, which can be calculated as a=(b+c)+d or a=b+(c+d) which might lead to numerical differences.
Do you know what might be the reason for the observations described above?
Thanks a lot!
it really seems to be caused by the single/double mix in the calculations. Since I have switched to double precision only, the problem did not occur anymore. Thanks to everybody for your thoughts.
these could be rounding errors. you can find the floating point accuracy of you system like so:
>> eps('single')
ans =
1.1921e-07
On my system this reports 10^-7 which would explain discrepancies of your order
To ensure reproducible results, especially if you are using any random generating functions (either directly or indirectly), you should restore the same state at the beginning of each run:
%# save state (do this once)
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save rndStream.mat savedState
%# load state (do this at before each run)
load rndStream.mat savedState
defaultStream = RandStream.getDefaultStream();
defaultStream.State = savedState;