Пример #1
0
def main(args):
    sensor, impact = args
    print(sensor, impact)

    rbase, ibase = get_signals(sensor, 0)
    nbaseline, nbin = rbase.shape

    rsig, isig = get_signals(sensor, impact)
    t = np.linspace(0, 1, window_size + 1)

    # Define the classification pipeline
    pipe = Pipeline([('aug', utils.FeatureAugment()),
                     ('norm', StandardScaler()),
                     ('clf', utils.MultiOCSVM(eta=0.2, kernel='rbf', nu=0.01, gamma=0.2))])

    det = []
    # perform monte carlo
    for i in range(n_mc):
        temperatures = utils.gp_simple(t, sigma=150, mean=10, kernel='laplacian')
        temperatures[temperatures < 0] = 0
        temperatures[temperatures > nbaseline - 1] = nbaseline - 1

        res = 0
        for j in range(nbin):
            # training
            rpart = rbase.iloc[:, j](temperatures[:-1], ext=3).values
            ipart = ibase.iloc[:, j](temperatures[:-1], ext=3).values
            pipe.fit(rpart + 1j * ipart)

            # prediction
            rpart = rsig.iloc[:, j](temperatures[-1], ext=3)
            ipart = isig.iloc[:, j](temperatures[-1], ext=3)
            res += pipe.predict(np.array([rpart + 1j * ipart]))[0]
        det.append((res >= zeta * nbin).astype(int))
    return (sensor, impact), 1 - np.mean(det)
""" A schematic of the classification approach, showing the classifier along with the defect
signal."""
from packages import utkit, scihdf, utils
from matplotlib import pyplot
from os.path import join
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

np.random.seed(10)
baselines = utils.make_range_signals(15, [0.9, 1.1], [-np.pi / 5, np.pi / 5])
damage = utils.make_random_signals(2, 0.02, np.pi / 10,
                                   0.9)[0] + 0.2 * np.exp(1j * np.pi / 5)

pipe = Pipeline([('aug', utils.FeatureAugment()), ('norm', StandardScaler()),
                 ('clf',
                  utils.MultiOCSVM(eta=0.8, kernel='rbf', nu=0.01,
                                   gamma=0.4))])
pipe.fit(baselines)

pyplot.style.use(join('..', 'plot_styles.mplstyle'))
utils.plot_svm_frontiers(pipe, damage, figsize=(2, 2))
ax = pyplot.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlabel('Real', labelpad=-4)
ax.set_ylabel('Imaginary', labelpad=-4)
pyplot.savefig('demo_classification.svg')
# pyplot.show()
Пример #3
0
    rho = 0.

    # Total number of defects to test
    n = 500

    # Amplitude of the defect signals
    defect_amp = 2
    nbaselines = 20
    eta, gamma = 0.8, 0.2

    angs = np.linspace(-np.pi, np.pi, 20)

    # Make the SVM
    pipe = Pipeline([('aug', utils.FeatureAugment()),
                     ('norm', StandardScaler()),
                     ('clf', utils.MultiOCSVM(eta=eta, kernel='rbf', nu=0.01, gamma=gamma))])

    fig, axarr = pyplot.subplots(4, 5)
    for ax, (amp, phase) in zip(*(axarr.ravel(), itertools.product(amp_sigma, phi_sigma))):
    # for amp, phase in itertools.product(amp_sigma, phi_sigma):
        print(amp, phase/np.pi)
        # Generate the training data
        p = 2
        baseline = utils.make_range_signals(nbaselines,
                                            amp_range=[amp_mean - p * amp, amp_mean + p * amp],
                                            phase_range=[phi_mean - p * phase, phi_mean + p * phase],
                                            corr=1)
        # Train the SVM
        pipe.fit(baseline)
        det, fa = [], []
        for ang in angs:
Пример #4
0
feat = feat.applymap(complex)

idx, out = [], []
for sid in conf['guided_waves']['experiment']['sensor_id']:
    for k in range(1, 7):
        baseline = feat.loc[pd.IndexSlice[sid, 0, :], :].values[::k, :]
        nbaseline, nbin = baseline.shape
        print(nbaseline)
        pipes = []
        for i in range(baseline.shape[1]):
            pipes.append(
                Pipeline([('aug', utils.FeatureAugment()),
                          ('norm', StandardScaler()),
                          ('clf',
                           utils.MultiOCSVM(eta=0.55,
                                            kernel='rbf',
                                            nu=0.01,
                                            gamma=0.05))]))
            pipes[-1].fit(baseline[:, i])

        for i in range(10):
            res = 0
            for j in range(nbin):
                defect = feat.loc[pd.IndexSlice[sid, i, :], :].values[:, j]
                res += pipes[j].predict(defect)

            res = (res >= 0.8 * nbin).astype(int)
            out.append(1 - np.mean(res))
            idx.append([sid, nbaseline, i])

# make the dataframe and sort its index
mux = pd.MultiIndex.from_arrays(list(zip(*idx)),