#  -*- coding: utf-8 -*-
"""
Created on Wed Oct  8 14:45:02 2014.

@author: mje
"""
import mne
import socket
import numpy as np
import os
import sys

from mne.io import Raw
from mne.preprocessing import ICA, create_ecg_epochs, create_eog_epochs

from my_settings import *

subject = sys.argv[1]

import matplotlib
matplotlib.use('Agg')

# SETTINGS
n_jobs = 1
l_freq, h_freq = 1, 98  # High and low frequency setting for the band pass
n_freq = 50  # notch filter frequency
decim = 7  # decim value


raw = Raw(maxfiltered_folder + "%s_data_mc_raw_tsss.fif" % subject,
          preload=True)
raw.del_proj(0)
raw.drop_channels(raw.info["bads"])
raw.add_eeg_average_proj()

raw.notch_filter(n_freq, n_jobs=n_jobs)
raw.filter(l_freq, h_freq, n_jobs=n_jobs)

raw.save(save_folder + "%s_filtered_data_mc_raw_tsss.fif" % subject,
         overwrite=True)

# ICA Part
ica = ICA(n_components=0.95, method='fastica', max_iter=256)

picks = mne.pick_types(raw.info, meg=True, eeg=True,
                       stim=False, exclude='bads')

ica.fit(raw, picks=picks, decim=decim, reject=reject_params)

# maximum number of components to reject
n_max_ecg, n_max_eog = 3, 1

##########################################################################
# 2) identify bad components by analyzing latent sources.
title = 'Sources related to %s artifacts (red) for sub: %s'

# generate ECG epochs use detection via phase statistics
ecg_epochs = create_ecg_epochs(raw, ch_name="ECG002",
                               tmin=-.5, tmax=.5, picks=picks)
n_ecg_epochs_found = len(ecg_epochs.events)
sel_ecg_epochs = np.arange(0, n_ecg_epochs_found, 10)
ecg_epochs = ecg_epochs[sel_ecg_epochs]

ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps')
fig = ica.plot_scores(scores, exclude=ecg_inds,
                      title=title % ('ecg', subject))
fig.savefig(save_folder + "pics/%s_ecg_scores.png" % subject)

if ecg_inds:
    show_picks = np.abs(scores).argsort()[::-1][:5]

    fig = ica.plot_sources(raw, show_picks, exclude=ecg_inds,
                           title=title % ('ecg', subject), show=False)
    fig.savefig(save_folder + "pics/%s_ecg_sources.png" % subject)
    fig = ica.plot_components(ecg_inds, title=title % ('ecg', subject),
                              colorbar=True)
    fig.savefig(save_folder + "pics/%s_ecg_component.png" % subject)

    ecg_inds = ecg_inds[:n_max_ecg]
    ica.exclude += ecg_inds

# estimate average artifact
ecg_evoked = ecg_epochs.average()
del ecg_epochs

# plot ECG sources + selection
fig = ica.plot_sources(ecg_evoked, exclude=ecg_inds)
fig.savefig(save_folder + "pics/%s_ecg_sources_ave.png" % subject)

# plot ECG cleaning
ica.plot_overlay(ecg_evoked, exclude=ecg_inds)
fig.savefig(save_folder + "pics/%s_ecg_sources_clean_ave.png" % subject)

# DETECT EOG BY CORRELATION
# HORIZONTAL EOG
eog_epochs = create_eog_epochs(raw, ch_name="EOG001")
eog_inds, scores = ica.find_bads_eog(raw)
fig = ica.plot_scores(scores, exclude=eog_inds,
                      title=title % ('eog', subject))
fig.savefig(save_folder + "pics/%s_eog_scores.png" % subject)

fig = ica.plot_components(eog_inds, title=title % ('eog', subject),
                          colorbar=True)
fig.savefig(save_folder + "pics/%s_eog_component.png" % subject)

eog_inds = eog_inds[:n_max_eog]
ica.exclude += eog_inds

del eog_epochs

##########################################################################
# Apply the solution to Raw, Epochs or Evoked like this:
raw_ica = ica.apply(raw, copy=False)
ica.save(save_folder + "%s-ica.fif" % subject)  # save ICA componenets
# Save raw with ICA removed
raw_ica.save(save_folder + "%s_filtered_ica_mc_raw_tsss.fif" % subject,
             overwrite=True)
plt.close("all")

@@ -3704,21 +3704,4 @@
 ue)%0A
-plt.close(%22all%22)%0A

Remove redundet call to plt