In [4]:
import numpy as np
from bokeh.plotting import HBox, VBox, figure, show, output_file, GridPlot
from bokeh.models.mappers import LinearColorMapper
from bokeh.models import BasicTicker, Grid
from sklearn import metrics
from sklearn import preprocessing
from sklearn.datasets import fetch_olivetti_faces
from sklearn.utils.validation import check_random_state
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.cross_validation import train_test_split
from sklearn.covariance import GraphLassoCV, ledoit_wolf
from sklearn.grid_search import GridSearchCV
from scipy.spatial import distance
import scipy
import sklearn
import OVFM.Model as md
import OVFM.FeatureMap as fm
import OVFM.Risk as rsk
import OVFM.LearningRate as lr
import OVFM.DataGeneration as dg
import OVFM.SGD as sgd
import time
import sys
In [2]:
q_os = scipy.io.loadmat( 'GP_mag/data/q_os.mat' )
data = scipy.io.loadmat( 'GP_mag/data/scanOneDeskMagCorrected.mat' )
In [118]:
# from pylab import *
mag_field = data[ 'U' ][ 0, 0 ][ 3 ] # 0: acc, 1: gyro, 2: mag, 3: mag corrected
pos_field = data[ 'pos_mt_l' ]
N = mag_field.shape[ 0 ]
# pos_field = pos_field[ np.floor( N / 20 ):np.floor( 3.8 * N / 4 ), : ]
# mag_field = mag_field[ np.floor( N / 20 ):np.floor( 3.8 * N / 4 ), : ]
pos_field = pos_field[ np.floor( N / 10 ):np.floor( 3 * N / 4 ), : ]
mag_field = mag_field[ np.floor( N / 10 ):np.floor( 3 * N / 4 ), : ]
pos_field = preprocessing.StandardScaler( ).fit_transform( pos_field )
# mag_field = preprocessing.StandardScaler( ).fit_transform( mag_field )
In [119]:
output_file( "scanOneDeskMagCorrected.html", title = "scanOneDeskMagCorrected" )
p1 = figure( )
p1.line( pos_field[ :, 0 ], pos_field[ :, 1 ], line_color = "gray", line_width = 1, alpha = 0.2 )
speed = np.sqrt( mag_field[ :, 0 ] ** 2 + mag_field[ :, 1 ] ** 2 )
theta = np.arctan2( mag_field[ :, 1 ], mag_field[ :, 0 ] )
length = speed / 50
angle = theta
x1 = pos_field[ :, 0 ] + length * np.cos( angle )
y1 = pos_field[ :, 1 ] + length * np.sin( angle )
cm = np.array( [ "#C7E9B4", "#7FCDBB", "#41B6C4", "#1D91C0", "#225EA8", "#0C2C84" ] )
ix = ( ( length - length.min( ) ) / ( length.max( ) - length.min( ) ) * 5 ).astype( 'int' )
colors = cm[ ix ]
p1.segment( pos_field[ :, 0 ], pos_field[ :, 1 ], x1, y1, color=colors, line_width = 1 )
show( p1 )
In [81]:
samples = 10
phi_cf = fm.CurlFreeFF( 0.01, 3, 20 )
phi_df = fm.DivFreeFF( 0.01, 3, 20 )
phi_cf_pos_field = phi_cf.map( pos_field[::samples,:] )
phi_df_pos_field = phi_df.map( pos_field[::samples,:] )
phi_pos_field = np.asarray( np.bmat( [ [ phi_df_pos_field, np.zeros( phi_cf_pos_field.shape ) ], [ phi_df_pos_field, phi_cf_pos_field ] ] ) )
In [82]:
# phi_cf_pos_field_inv = np.linalg.pinv( phi_cf_pos_field )
# phi_df_pos_field_inv = np.linalg.pinv( phi_df_pos_field )
phi_pos_field_inv = np.linalg.pinv( phi_pos_field )
In [83]:
augmented_mag_field = np.concatenate( ( mag_field[::samples,:], np.zeros( mag_field[::samples,:].shape ) ), axis = 0 )
w = np.dot( phi_pos_field_inv, augmented_mag_field.ravel( ) )
In [84]:
augmented_mag_field_p = np.dot( phi_pos_field, w.T ).reshape( augmented_mag_field.shape )
print 'train MSE', ( ( augmented_mag_field - augmented_mag_field_p ) ** 2 ).mean( )
print 'train B-field MSE', 2 * ( ( augmented_mag_field[ :augmented_mag_field_p.shape [ 0 ] / 2, : ] - augmented_mag_field_p[ :augmented_mag_field_p.shape [ 0 ] / 2, : ] ) ** 2 ).mean( )
print 'train M-field MSE', 2 * ( ( augmented_mag_field[ augmented_mag_field_p.shape [ 0 ] / 2:, : ] - augmented_mag_field_p[ augmented_mag_field_p.shape [ 0 ] / 2:, : ] ) ** 2 ).mean( )
In [85]:
np.linalg.norm( w )
Out[85]:
In [86]:
phi_cf_pos_field = phi_cf.map( pos_field[::samples/10,:] )
phi_df_pos_field = phi_df.map( pos_field[::samples/10,:] )
phi_pos_field = np.asarray( np.bmat( [ [ phi_df_pos_field, np.zeros( phi_cf_pos_field.shape ) ], [ phi_df_pos_field, phi_cf_pos_field ] ] ) )
In [87]:
augmented_mag_field = np.concatenate( ( mag_field[::samples/10,:], np.zeros( mag_field[::samples/10,:].shape ) ), axis = 0 )
augmented_mag_field_p = np.dot( phi_pos_field, w.T ).reshape( augmented_mag_field.shape )
In [88]:
print 'train MSE', ( ( augmented_mag_field - augmented_mag_field_p ) ** 2 ).mean( )
print 'train B-field MSE', 2 * ( ( augmented_mag_field[ :augmented_mag_field_p.shape [ 0 ] / 2, : ] - augmented_mag_field_p[ :augmented_mag_field_p.shape [ 0 ] / 2, : ] ) ** 2 ).mean( )
print 'train M-field MSE', 2 * ( ( augmented_mag_field[ augmented_mag_field_p.shape [ 0 ] / 2:, : ] - augmented_mag_field_p[ augmented_mag_field_p.shape [ 0 ] / 2:, : ] ) ** 2 ).mean( )
In [122]:
D = 1000
gamma = 1. / ( 2 * .15 ** 2 )
L = np.eye( 3 )
C = 0
eta0 = .05
samples = 10
df = fm.DecomposableFF( gamma, pos_field[::samples,:].shape[ 1 ], D, L )
dff = fm.DivFreeFF( gamma, pos_field[::samples,:].shape[ 1 ], D )
cff = fm.CurlFreeFF( gamma, pos_field[::samples,:].shape[ 1 ], D )
dcff = fm.MultipleFF( np.array( [ [ dff, None ], [ dff, cff ] ] ), np.array( [ .3, .3 ] ) )
modelD = md.Model( df )
modelDF = md.Model( dff )
modelCF = md.Model( cff )
modelCDF = md.Model( dcff )
print modelCDF.coefs.shape
risk = rsk.Ridge( C, 0 )
lc = lr.Constant( 1 * eta0 )
lb = lr.Constant( 0.0 * eta0 )
est = sgd.SGD( risk, 5.0, lc, lb, 10, 10000 )
In [123]:
modelCDF.reset( )
est.fit( modelCDF, pos_field[::samples,:], np.concatenate( ( mag_field[::samples,:], np.zeros( mag_field[::samples,:].shape ) ), axis = 1 ) )
In [125]:
print ( ( modelCDF( pos_field[::samples,:] )[:,:3] - mag_field[::samples,:] ) ** 2 ).mean( )
print ( modelCDF( pos_field[::samples,:] )[:,3:] ** 2 ).mean( )
In [126]:
print modelCDF( pos_field[::samples,:] )
print mag_field[::samples,:]
In [281]:
In [5]:
def quiver( title, X, Y, U, V, scale ):
p = figure( title = title )
speed = np.sqrt( U ** 2 + V ** 2 )
theta = np.arctan2( V, U )
length = speed * scale
angle = theta
X1 = X + length * np.cos( angle )
Y1 = Y + length * np.sin( angle )
cm = np.array( [ "#C7E9B4", "#7FCDBB", "#41B6C4", "#1D91C0", "#225EA8", "#0C2C84" ] )
ix = ( ( length - length.min( ) ) / ( length.max( ) - length.min( ) ) * 5 ).ravel( ).astype( 'int' )
colors = cm[ ix ]
p.scatter( X.ravel( ), Y.ravel( ), size = 2 )
p.segment( X.ravel( ), Y.ravel( ), X1.ravel( ), Y1.ravel( ), color=colors, line_width = 1 )
return p
In [6]:
def magnetic_sphere( X, Y, a, M0 ):
mu0 = 4 * np.pi * 1e-7
rm = np.sqrt( X ** 2 + Y ** 2 )
tm = np.arctan2( Y, X )
MU = np.zeros( Y.shape )
MV = np.full( X.shape, M0 )
mU = 4. / 3. * np.pi * a ** 3 * MU * np.cos( tm )
mV = 4. / 3. * np.pi * a ** 3 * MV * np.sin( tm )
# Outside the sphere
BU = mu0 / ( 4 * np.pi ) * ( -mU / rm ** 3 + 3 * ( mU * np.cos( tm ) + mV * np.sin( tm ) ) * np.cos( tm ) / rm ** 3 )
BV = mu0 / ( 4 * np.pi ) * ( -mV / rm ** 3 + 3 * ( mU * np.cos( tm ) + mV * np.sin( tm ) ) * np.sin( tm ) / rm ** 3 )
BU[ Y < 0 ] = -BU[ Y < 0 ]
BV[ Y < 0 ] = -BV[ Y < 0 ]
HU = BU / mu0
HV = BV / mu0
MU[ rm >= a ] = 0
MV[ rm >= a ] = 0
# Inside the sphere
BU[ rm < a ] = ( 2. / 3. * mu0 * MU )[ rm < a ]
BV[ rm < a ] = ( 2. / 3. * mu0 * MV )[ rm < a ]
HU[ rm < a ] = ( -1. / 3. * MU )[ rm < a ]
HV[ rm < a ] = ( -1. / 3. * MV )[ rm < a ]
return MU, MV, BU, BV, HU, HV
In [7]:
x = np.linspace( -5, 5, 100 )
y = np.linspace( -5, 5, 100 )
Y, X = np.meshgrid( y, x )
mu0 = 4 * np.pi * 1e-7
a = 2.5
MU, MV, BU, BV, HU, HV = magnetic_sphere( X, Y, a, -1 )
BU = BU / mu0
BV = BV / mu0
In [283]:
output_file( "scanOneDeskMagCorrected.html", title = "scanOneDeskMagCorrected" )
p1 = quiver( 'M-field', X, Y, MU, MV, 0.5 )
p2 = quiver( 'B-field', X, Y, BU, BV, 0.5 )
p3 = quiver( 'H-field', X, Y, HU, HV, 0.5 )
show( HBox( p1, p2, p3 ) )
In [8]:
x = np.vstack( ( X.ravel( ), Y.ravel( ) ) ).T
y = np.vstack( ( BU.ravel( ), BV.ravel( ) ) ).T
In [9]:
x_out = x[ np.sqrt( x[ :, 0 ] ** 2 + x[ :, 1 ] ** 2 ) > a, : ]
y_out = y[ np.sqrt( x[ :, 0 ] ** 2 + x[ :, 1 ] ** 2 ) > a, : ]
In [10]:
D = 500
gamma = 1
L = np.eye( 2 )
C = 0
eta0 = 2.
df = fm.DecomposableFF( gamma, x_out.shape[ 1 ], D, L )
dff = fm.DivFreeFF( gamma, x_out.shape[ 1 ], D )
cff = fm.CurlFreeFF( gamma, x_out.shape[ 1 ], D )
dcff = fm.MultipleFF( np.array( [ [ dff, None ], [ dff, cff ] ] ), np.array( [ 1. / 4, 1. / 4 ] ) )
modelD = md.Model( df )
modelDF = md.Model( dff )
modelCF = md.Model( cff )
modelCDF = md.Model( dcff )
risk = rsk.Ridge( C, 0 )
lc = lr.Constant( 1. * eta0 )
lb = lr.Constant( 0.0 * eta0 )
est = sgd.SGD( risk, 1.0, lc, lb, 10, 10000 )
In [11]:
modelD.reset( )
est.fit( modelD, x_out, y_out )
yp = modelD( x )
In [12]:
BUDP = yp[ :, 0 ].reshape( ( 100, 100 ) )
BVDP = yp[ :, 1 ].reshape( ( 100, 100 ) )
In [13]:
modelDF.reset( )
est.fit( modelDF, x_out, y_out )
yp = modelDF( x )
In [14]:
BUDFP = yp[ :, 0 ].reshape( ( 100, 100 ) )
BVDFP = yp[ :, 1 ].reshape( ( 100, 100 ) )
In [15]:
modelCF.reset( )
est.fit( modelCF, x_out, y_out )
yp = modelCF( x )
In [16]:
BUCFP = yp[ :, 0 ].reshape( ( 100, 100 ) )
BVCFP = yp[ :, 1 ].reshape( ( 100, 100 ) )
In [17]:
modelCDF.reset( )
est.fit( modelCDF, x_out, np.concatenate( ( y_out, np.zeros( y_out.shape ) ), axis = 1 ) )
yp = modelCDF( x )
In [18]:
BUCDFP = yp[ :, 0 ].reshape( ( 100, 100 ) )
BVCDFP = yp[ :, 1 ].reshape( ( 100, 100 ) )
print yp[ :, 2: ]
In [19]:
XP = x[ :, 0 ].reshape( ( 100, 100 ) )
YP = x[ :, 1 ].reshape( ( 100, 100 ) )
out = XP ** 2 + YP ** 2 != a ** 2
In [20]:
output_file( "predicted_fields.html", title = "predicted_fields" )
p1 = quiver( 'B-field', XP[ out ], YP[ out ], BU[ out ], BV[ out ], 0.5 )
p1.line( np.linspace( -5, 5, 1000 ), np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p1.line( np.linspace( -5, 5, 1000 ), -np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p2 = quiver( 'pred B-field decom', XP[ out ], YP[ out ], BUDP[ out ], BVDP[ out ], 0.5 )
p2.line( np.linspace( -5, 5, 1000 ), np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p2.line( np.linspace( -5, 5, 1000 ), -np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p3 = quiver( 'pred B-field divf', XP[ out ], YP[ out ], BUDFP[ out ], BVDFP[ out ], 0.5 )
p3.line( np.linspace( -5, 5, 1000 ), np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p3.line( np.linspace( -5, 5, 1000 ), -np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p4 = quiver( 'pred B-field curlf', XP[ out ], YP[ out ], BUCFP[ out ], BVCFP[ out ], 0.5 )
p4.line( np.linspace( -5, 5, 1000 ), np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p4.line( np.linspace( -5, 5, 1000 ), -np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p5 = quiver( 'pred B-field divf-curlf', XP[ out ], YP[ out ], BUCDFP[ out ], BVCDFP[ out ], 0.5 )
p5.line( np.linspace( -5, 5, 1000 ), np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
p5.line( np.linspace( -5, 5, 1000 ), -np.sqrt( a ** 2 - np.linspace( -5, 5, 1000 ) ** 2 ), color= "red" )
show( HBox( p1, p2, p3, p4, p5 ) )
In [32]:
A = np.random.rand( 5 )
print A
A = np.outer( A, A )
print A
print scipy.linalg.svd( A )
In [ ]: