Skip to content

Commit e9873b3

Browse files
committed
Add JS testing area
1 parent 5bd71be commit e9873b3

41 files changed

Lines changed: 3121 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

JSunitRootTesting/CreateIRFPlots.m

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
disp( 'This code requires ffmpeg.exe from http://ffmpeg.zeranoe.com/builds/ to be placed in the same folder.' );
2+
3+
ShockNames = { 'epsilon_AT_5_5', 'epsilon_GA' 'epsilon_GN' 'epsilon_tau' 'epsilon_phi' 'epsilon_beta' };
4+
VariableNames = { 'C', 'K', 'I', 'E', 'F', 'Q', 'J', 'L', 'H', 'N', 'SN', 'SD', 'muN', 'U' };
5+
SpecificIndices = [ 1, 1; 5, 5 ];
6+
SpatialShockIndices = 1;
7+
8+
IRFLength = 400;
9+
ShockScale = 10;
10+
11+
InterpolationAmount = 3;
12+
13+
SpatialDimensions = 2;
14+
SpatialPointsPerDimension = 8;
15+
16+
SpatialNumPoints = SpatialPointsPerDimension ^ SpatialDimensions;
17+
18+
SpatialIndices = cell( 1, SpatialDimensions );
19+
[ SpatialIndices{:} ] = ndgrid( 1:SpatialPointsPerDimension );
20+
SpatialIndices = cellfun( @( c ) c(:), SpatialIndices, 'UniformOutput', false );
21+
SpatialIndices = cell2mat( SpatialIndices );
22+
23+
GridSizeCell = repmat( { SpatialPointsPerDimension }, 1, SpatialDimensions );
24+
25+
XIRF = ( 1:IRFLength ) / 4;
26+
ZeroIRF = zeros( IRFLength, 1 );
27+
28+
if SpatialDimensions == 2
29+
InterpolationMultiplier = 2 ^ InterpolationAmount;
30+
[ SurfaceX, SurfaceY ] = meshgrid( ( 0 : ( SpatialPointsPerDimension * InterpolationMultiplier ) ) / ( SpatialPointsPerDimension * InterpolationMultiplier ) );
31+
end
32+
33+
FigureHandle = figure( 1 );
34+
figure( FigureHandle );
35+
robot = java.awt.Robot;
36+
robot.keyPress(java.awt.event.KeyEvent.VK_ALT); %// send ALT
37+
robot.keyPress(java.awt.event.KeyEvent.VK_SPACE); %// send SPACE
38+
robot.keyRelease(java.awt.event.KeyEvent.VK_SPACE); %// release SPACE
39+
robot.keyRelease(java.awt.event.KeyEvent.VK_ALT); %// release ALT
40+
robot.keyPress(java.awt.event.KeyEvent.VK_X); %// send X
41+
robot.keyRelease(java.awt.event.KeyEvent.VK_X); %// release X
42+
pause( 0.1 );
43+
44+
FigureHandle.PaperPositionMode = 'auto';
45+
FigureHandle.Color = [ 1 1 1 ];
46+
colormap( parula( 2 ^ 16 ) );
47+
48+
FolderName = 'Results';
49+
[ mkdirStatus, ~, ~ ] = mkdir( FolderName );
50+
assert( mkdirStatus == 1 );
51+
cd( FolderName );
52+
53+
for ShockIdx = 1 : length( ShockNames )
54+
ShockName = ShockNames{ ShockIdx };
55+
for VariableIdx = 1 : length( VariableNames )
56+
VariableName = VariableNames{ VariableIdx };
57+
58+
FolderName = [ VariableName '_' ShockName ];
59+
[ mkdirStatus, ~, ~ ] = mkdir( FolderName );
60+
assert( mkdirStatus == 1 );
61+
cd( FolderName );
62+
63+
AggregatedVariableName = [ 'log_' VariableName '_' ShockName ];
64+
if isfield( oo_.irfs, AggregatedVariableName )
65+
CurrentPercentIRF = 100 * ShockScale * oo_.irfs.( AggregatedVariableName )( 1:IRFLength );
66+
plot( XIRF, CurrentPercentIRF, 'k-' );
67+
hold on;
68+
plot( XIRF, ZeroIRF, 'r--' );
69+
hold off;
70+
drawnow;
71+
savefig( FigureHandle, 'Aggregate', 'compact' );
72+
saveas( FigureHandle, 'Aggregate', 'meta' );
73+
end
74+
75+
PercentIRF = zeros( GridSizeCell{:}, IRFLength );
76+
for Point = 1 : SpatialNumPoints
77+
CurrentIndices = SpatialIndices( Point, : );
78+
CurrentIndicesCell = num2cell( CurrentIndices );
79+
CurrentIndicesString = sprintf( repmat( '_%d', 1, SpatialDimensions ), CurrentIndices );
80+
PercentIRF( CurrentIndicesCell{:}, : ) = 100 * ShockScale * oo_.irfs.( [ 'log_' VariableName CurrentIndicesString '_' ShockName ] )( 1:IRFLength );
81+
end
82+
83+
for SpecificIndexIdx = 1 : size( SpecificIndices, 1 )
84+
CurrentIndices = SpecificIndices( SpecificIndexIdx, : );
85+
CurrentIndicesCell = num2cell( CurrentIndices );
86+
CurrentIndicesString = sprintf( repmat( '_%d', 1, SpatialDimensions ), CurrentIndices );
87+
CurrentPercentIRF = squeeze( PercentIRF( CurrentIndicesCell{:}, : ) );
88+
plot( XIRF, CurrentPercentIRF, 'k-' );
89+
hold on;
90+
plot( XIRF, ZeroIRF, 'r--' );
91+
hold off;
92+
drawnow;
93+
savefig( FigureHandle, CurrentIndicesString, 'compact' );
94+
saveas( FigureHandle, CurrentIndicesString, 'meta' );
95+
end
96+
97+
if ( SpatialDimensions == 2 ) && ismember( ShockIdx, SpatialShockIndices )
98+
99+
Video = VideoWriter( 'Video', 'Archival' ); %#ok<TNMLP>
100+
Video.FrameRate = 4 * InterpolationMultiplier;
101+
% Video.Quality = 100;
102+
open( Video );
103+
104+
[ mkdirStatus, ~, ~ ] = mkdir( 'Frames' );
105+
assert( mkdirStatus == 1 );
106+
cd( 'Frames' );
107+
108+
PercentIRF = [ PercentIRF( :, :, : ), PercentIRF( :, 1, : ); PercentIRF( 1, :, : ), PercentIRF( 1, 1, : ) ];
109+
PercentIRF = interp3( PercentIRF, InterpolationAmount );
110+
minPercentIRF = min( PercentIRF(:) );
111+
maxPercentIRF = max( PercentIRF(:) );
112+
113+
for Period = 1 : size( PercentIRF, 3 )
114+
CurrentSurface = PercentIRF( :, :, Period );
115+
pcolor( SurfaceX, SurfaceY, CurrentSurface );
116+
shading interp;
117+
axis square;
118+
caxis( [ minPercentIRF, maxPercentIRF ] );
119+
colorbar;
120+
drawnow;
121+
122+
if ( mod( Period - 1, 4 * InterpolationMultiplier ) == 0 ) || ( Period == size( PercentIRF, 3 ) )
123+
FileName = num2str( Period );
124+
savefig( FigureHandle, FileName, 'compact' );
125+
saveas( FigureHandle, FileName, 'png' );
126+
end
127+
128+
% Axis = gca;
129+
% Axis.Units = 'pixels';
130+
% AxisPosition = ceil( Axis.Position );
131+
% Margin = max( ceil( Axis.TightInset ) );
132+
% Rectangle = [ -Margin, -Margin, AxisPosition(3) + 2 * Margin, AxisPosition(4) + 2 * Margin ];
133+
% Frame = getframe( Axis, Rectangle );
134+
135+
Frame = getframe( FigureHandle );
136+
137+
writeVideo( Video, Frame );
138+
end
139+
140+
cd( '..' );
141+
142+
close( Video );
143+
144+
dos( '..\ffmpeg.exe -i Video.mj2 -filter "setsar=sar=1" -crf 15 -preset slow Video.mp4', '-echo' );
145+
delete Video.mj2
146+
147+
end
148+
149+
cd( '..' );
150+
end
151+
end
152+
153+
cd( '..' );
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
disp( 'This code requires ffmpeg.exe from http://ffmpeg.zeranoe.com/builds/ to be placed in the same folder.' );
2+
3+
VariableNames = { 'C', 'K', 'I', 'E', 'F', 'Q', 'J', 'L', 'H', 'N', 'SN', 'SD', 'muN', 'U' };
4+
5+
SimulationLength = 4000;
6+
7+
InterpolationAmount = 3;
8+
9+
SpatialDimensions = 2;
10+
SpatialPointsPerDimension = 8;
11+
12+
SpatialNumPoints = SpatialPointsPerDimension ^ SpatialDimensions;
13+
14+
SpatialIndices = cell( 1, SpatialDimensions );
15+
[ SpatialIndices{:} ] = ndgrid( 1:SpatialPointsPerDimension );
16+
SpatialIndices = cellfun( @( c ) c(:), SpatialIndices, 'UniformOutput', false );
17+
SpatialIndices = cell2mat( SpatialIndices );
18+
19+
GridSizeCell = repmat( { SpatialPointsPerDimension }, 1, SpatialDimensions );
20+
21+
XSimulation = ( 1:SimulationLength ) / 4;
22+
ZeroSimulation = zeros( SimulationLength, 1 );
23+
24+
if SpatialDimensions == 2
25+
InterpolationMultiplier = 2 ^ InterpolationAmount;
26+
[ SurfaceX, SurfaceY ] = meshgrid( ( 0 : ( SpatialPointsPerDimension * InterpolationMultiplier ) ) / ( SpatialPointsPerDimension * InterpolationMultiplier ) );
27+
end
28+
29+
FigureHandle = figure( 1 );
30+
figure( FigureHandle );
31+
robot = java.awt.Robot;
32+
robot.keyPress(java.awt.event.KeyEvent.VK_ALT); %// send ALT
33+
robot.keyPress(java.awt.event.KeyEvent.VK_SPACE); %// send SPACE
34+
robot.keyRelease(java.awt.event.KeyEvent.VK_SPACE); %// release SPACE
35+
robot.keyRelease(java.awt.event.KeyEvent.VK_ALT); %// release ALT
36+
robot.keyPress(java.awt.event.KeyEvent.VK_X); %// send X
37+
robot.keyRelease(java.awt.event.KeyEvent.VK_X); %// release X
38+
pause( 0.1 );
39+
40+
FigureHandle.PaperPositionMode = 'auto';
41+
FigureHandle.Color = [ 1 1 1 ];
42+
colormap( parula( 2 ^ 16 ) );
43+
44+
EndoNames = cellstr( M_.endo_names );
45+
PercentSimulations = 100 * bsxfun( @minus, oo_.endo_simul( :, 1:SimulationLength ), oo_.steady_state );
46+
47+
FolderName = 'Results';
48+
[ mkdirStatus, ~, ~ ] = mkdir( FolderName );
49+
assert( mkdirStatus == 1 );
50+
cd( FolderName );
51+
52+
for VariableIdx = 1 : length( VariableNames )
53+
VariableName = VariableNames{ VariableIdx };
54+
55+
FolderName = [ VariableName '_Simulation' ];
56+
[ mkdirStatus, ~, ~ ] = mkdir( FolderName );
57+
assert( mkdirStatus == 1 );
58+
cd( FolderName );
59+
60+
EndoVariableIndex = find( strcmp( [ 'log_' VariableName ], EndoNames ), 1 );
61+
if ~isempty( EndoVariableIndex )
62+
CurrentPercentSimulation = PercentSimulations( EndoVariableIndex, : );
63+
plot( XSimulation, CurrentPercentSimulation, 'k-' );
64+
hold on;
65+
plot( XSimulation, ZeroSimulation, 'r--' );
66+
hold off;
67+
drawnow;
68+
savefig( FigureHandle, 'Aggregate', 'compact' );
69+
saveas( FigureHandle, 'Aggregate', 'meta' );
70+
end
71+
72+
PercentSimulation = zeros( GridSizeCell{:}, SimulationLength );
73+
for Point = 1 : SpatialNumPoints
74+
CurrentIndices = SpatialIndices( Point, : );
75+
CurrentIndicesCell = num2cell( CurrentIndices );
76+
CurrentIndicesString = sprintf( repmat( '_%d', 1, SpatialDimensions ), CurrentIndices );
77+
EndoVariableIndex = find( strcmp( [ 'log_' VariableName CurrentIndicesString ], EndoNames ), 1 );
78+
assert( ~isempty( EndoVariableIndex ) );
79+
PercentSimulation( CurrentIndicesCell{:}, : ) = PercentSimulations( EndoVariableIndex, : );
80+
end
81+
82+
if SpatialDimensions == 2
83+
84+
Video = VideoWriter( 'Video', 'Archival' ); %#ok<TNMLP>
85+
Video.FrameRate = 40;
86+
% Video.Quality = 100;
87+
open( Video );
88+
89+
[ mkdirStatus, ~, ~ ] = mkdir( 'Frames' );
90+
assert( mkdirStatus == 1 );
91+
cd( 'Frames' );
92+
93+
PercentSimulation = [ PercentSimulation( :, :, : ), PercentSimulation( :, 1, : ); PercentSimulation( 1, :, : ), PercentSimulation( 1, 1, : ) ];
94+
minPercentSimulation = min( PercentSimulation(:) );
95+
maxPercentSimulation = max( PercentSimulation(:) );
96+
97+
for Period = 1 : size( PercentSimulation, 3 )
98+
CurrentSurface = interp2( PercentSimulation( :, :, Period ), InterpolationAmount );
99+
pcolor( SurfaceX, SurfaceY, CurrentSurface );
100+
shading interp;
101+
axis square;
102+
caxis( [ minPercentSimulation, maxPercentSimulation ] );
103+
colorbar;
104+
drawnow;
105+
106+
if ( mod( Period - 1, 40 ) == 0 ) || ( Period == size( PercentSimulation, 3 ) )
107+
FileName = num2str( Period );
108+
savefig( FigureHandle, FileName, 'compact' );
109+
saveas( FigureHandle, FileName, 'png' );
110+
end
111+
112+
% Axis = gca;
113+
% Axis.Units = 'pixels';
114+
% AxisPosition = ceil( Axis.Position );
115+
% Margin = max( ceil( Axis.TightInset ) );
116+
% Rectangle = [ -Margin, -Margin, AxisPosition(3) + 2 * Margin, AxisPosition(4) + 2 * Margin ];
117+
% Frame = getframe( Axis, Rectangle );
118+
119+
Frame = getframe( FigureHandle );
120+
121+
writeVideo( Video, Frame );
122+
end
123+
124+
cd( '..' );
125+
126+
close( Video );
127+
128+
dos( '..\ffmpeg.exe -i Video.mj2 -filter "setsar=sar=1" -crf 15 -preset slow Video.mp4', '-echo' );
129+
delete Video.mj2
130+
131+
end
132+
133+
cd( '..' );
134+
end
135+
136+
cd( '..' );

0 commit comments

Comments
 (0)