def test_apply_function_verbose(): """Test apply function verbosity """ n_chan = 2 n_times = 3 ch_names = [str(ii) for ii in range(n_chan)] raw = RawArray(np.zeros((n_chan, n_times)), create_info(ch_names, 1., 'mag')) # test return types in both code paths (parallel / 1 job) assert_raises(TypeError, raw.apply_function, bad_1, None, None, 1) assert_raises(ValueError, raw.apply_function, bad_2, None, None, 1) assert_raises(TypeError, raw.apply_function, bad_1, None, None, 2) assert_raises(ValueError, raw.apply_function, bad_2, None, None, 2) # check our arguments tempdir = _TempDir() test_name = op.join(tempdir, 'test.log') set_log_file(test_name) try: raw.apply_function(printer, None, None, 1, verbose=False) with open(test_name) as fid: assert_equal(len(fid.readlines()), 0) raw.apply_function(printer, None, None, 1, verbose=True) with open(test_name) as fid: assert_equal(len(fid.readlines()), n_chan) finally: set_log_file(None)
def test_calculate_chpi_positions(): """Test calculation of cHPI positions """ trans, rot, t = get_chpi_positions(pos_fname) with warnings.catch_warnings(record=True): raw = Raw(raw_fif_fname, allow_maxshield=True, preload=True) t -= raw.first_samp / raw.info['sfreq'] trans_est, rot_est, t_est = _calculate_chpi_positions(raw, verbose='debug') _compare_positions((trans, rot, t), (trans_est, rot_est, t_est)) # degenerate conditions raw_no_chpi = Raw(test_fif_fname) assert_raises(RuntimeError, _calculate_chpi_positions, raw_no_chpi) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['coord_frame'] = 999 break assert_raises(RuntimeError, _calculate_chpi_positions, raw_bad) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['r'] = np.ones(3) raw_bad.crop(0, 1., copy=False) tempdir = _TempDir() log_file = op.join(tempdir, 'temp_log.txt') set_log_file(log_file, overwrite=True) try: _calculate_chpi_positions(raw_bad) finally: set_log_file() with open(log_file, 'r') as fid: for line in fid: assert_true('0/5 acceptable' in line)
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" tempdir = _TempDir() raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 drop_log = op.join(op.dirname(tempdir), 'ica_drop.log') set_log_file(drop_log, overwrite=True) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) with open(drop_log) as fid: log = [l for l in fid if 'detected' in l] assert_equal(len(log), 1)
def test_cache_dir(): """Test use of cache dir """ tempdir = _TempDir() orig_dir = os.getenv("MNE_CACHE_DIR", None) orig_size = os.getenv("MNE_MEMMAP_MIN_SIZE", None) rng = np.random.RandomState(0) X = rng.randn(9, 2, 10) log_file = op.join(tempdir, "log.txt") try: os.environ["MNE_MEMMAP_MIN_SIZE"] = "1K" os.environ["MNE_CACHE_DIR"] = tempdir # Fix error for #1507: in-place when memmapping permutation_cluster_1samp_test( X, buffer_size=None, n_jobs=2, n_permutations=1, seed=0, stat_fun=ttest_1samp_no_p, verbose=False ) # ensure that non-independence yields warning stat_fun = partial(ttest_1samp_no_p, sigma=1e-3) set_log_file(log_file) permutation_cluster_1samp_test( X, buffer_size=10, n_jobs=2, n_permutations=1, seed=0, stat_fun=stat_fun, verbose=False ) with open(log_file, "r") as fid: assert_true("independently" in "".join(fid.readlines())) finally: if orig_dir is not None: os.environ["MNE_CACHE_DIR"] = orig_dir else: del os.environ["MNE_CACHE_DIR"] if orig_size is not None: os.environ["MNE_MEMMAP_MIN_SIZE"] = orig_size else: del os.environ["MNE_MEMMAP_MIN_SIZE"] set_log_file(None)
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 drop_log = op.join(op.dirname(tempdir), 'ica_drop.log') set_log_file(drop_log, overwrite=True) ica.decompose_raw(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) log = [l for l in open(drop_log) if 'detected' in l] assert_equal(len(log), 1)
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" tempdir = _TempDir() raw = io.Raw(raw_fname).crop(1.5, stop, False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude="bads") ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 drop_log = op.join(op.dirname(tempdir), "ica_drop.log") set_log_file(drop_log, overwrite=True) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) with open(drop_log) as fid: log = [l for l in fid if "detected" in l] assert_equal(len(log), 1)
def test_cache_dir(): """Test use of cache dir """ tempdir = _TempDir() orig_dir = os.getenv('MNE_CACHE_DIR', None) orig_size = os.getenv('MNE_MEMMAP_MIN_SIZE', None) rng = np.random.RandomState(0) X = rng.randn(9, 2, 10) log_file = op.join(tempdir, 'log.txt') try: os.environ['MNE_MEMMAP_MIN_SIZE'] = '1K' os.environ['MNE_CACHE_DIR'] = tempdir # Fix error for #1507: in-place when memmapping permutation_cluster_1samp_test(X, buffer_size=None, n_jobs=2, n_permutations=1, seed=0, stat_fun=ttest_1samp_no_p, verbose=False) # ensure that non-independence yields warning stat_fun = partial(ttest_1samp_no_p, sigma=1e-3) set_log_file(log_file) permutation_cluster_1samp_test(X, buffer_size=10, n_jobs=2, n_permutations=1, seed=0, stat_fun=stat_fun, verbose=False) with open(log_file, 'r') as fid: assert_true('independently' in ''.join(fid.readlines())) finally: if orig_dir is not None: os.environ['MNE_CACHE_DIR'] = orig_dir else: del os.environ['MNE_CACHE_DIR'] if orig_size is not None: os.environ['MNE_MEMMAP_MIN_SIZE'] = orig_size else: del os.environ['MNE_MEMMAP_MIN_SIZE'] set_log_file(None)
def configure_logging(path): """Set format to file logging and add stdout logging Log file messages will be: DATE - LEVEL - MESSAGE """ fname = op.join(op.dirname(path), 'eeg_cleaner.log') set_log_file(fname, overwrite=False) handlers = logger.handlers file_output_format = '%(asctime)s %(levelname)s %(message)s' date_format = '%d/%m/%Y %H:%M:%S' output_format = '%(message)s' for h in handlers: if not isinstance(h, logging.FileHandler): logger.removeHandler(h) print('Removing handler {}'.format(h)) else: h.setFormatter( logging.Formatter(file_output_format, datefmt=date_format)) lh = logging.StreamHandler(WrapStdOut()) lh.setFormatter(logging.Formatter(output_format)) logger.addHandler(lh)
def setup_provenance(script, results_dir, config=None, use_agg=True, run_id=None): """Setup provenance tracking Parameters ---------- script : str The script that was executed. results_dir : str The results directory. config : None | str The name of the config file. By default, the function expects the config to be under `__script__/' named `config.py`. It can also be another kind of textfile, e.g. .json. use_agg : bool Whether to use the 'Agg' backend for matplotlib or not. Returns ------- report : mne.report.Report The mne report. Side-effects ------------ - make results dir if it does not exists - sets log file for sterr output - writes log file with runtime information """ if use_agg is True: import matplotlib matplotlib.use('Agg') if not op.isfile(script): raise ValueError('sorry, this is not a script!') if not op.isdir(results_dir): results_dir = op.join(op.dirname(op.dirname(script)), results_dir) step = op.splitext(op.split(script)[1])[0] if not op.isabs(results_dir): results_dir = op.abspath(results_dir) start_path = op.dirname(results_dir) results_dir = op.join(results_dir, step) if not op.exists(results_dir): logger.info('generating results dir') _forec_create_dir(results_dir, start=start_path) if run_id is None: run_id = create_run_id() logger.info('generated run id: %s' % run_id) else: logger.info('using existing run id: %s' % run_id) logger.info('preparing logging:') logging_dir = op.join(results_dir, run_id) if not op.exists(logging_dir): logger.info('... making logging directory: %s' % logging_dir) os.mkdir(logging_dir) else: logger.info('... using logging directory: %s' % logging_dir) modules = get_versions(sys) runtime_log = op.join(logging_dir, 'run_time.json') with open(runtime_log, 'w') as fid: json.dump(modules, fid) logger.info('... writing runtime info to: %s' % runtime_log) script_code = op.join(logging_dir, 'script.py') if not op.isfile(script_code): with open(script_code, 'w') as fid: with open(script) as script_fid: source_code = script_fid.read() fid.write(source_code) logger.info('... logging source code of calling script') if config is None: config = 'config.py' if op.isabs(config): config_fname = config else: config_fname = op.join(op.dirname(script), config) config_code = op.join( # weird behavior of join if last arg is path results_dir, run_id, op.split(config_fname)[-1]) if not op.isfile(config_fname): logger.info('... No config found. Logging nothing.') elif op.isfile(config_code): logger.info('... Config already written. I assume that you are using' ' the same run_id for different runs of your script.') else: with open(config_code, 'w') as fid: with open(config_fname) as config_fid: source_code = config_fid.read() fid.write(source_code) logger.info('... logging source code of "%s".' % config_fname) logger.info('... preparing Report') report = Report(title=step) report.data_path = logging_dir std_logfile = op.join(logging_dir, 'run_output.log') logger.info('... setting logfile: %s' % std_logfile) set_log_file(std_logfile) return report, run_id, results_dir, logger
def test_logging(): """Test logging (to file) """ with open(fname_log, 'r') as old_log_file: old_lines = clean_lines(old_log_file.readlines()) with open(fname_log_2, 'r') as old_log_file_2: old_lines_2 = clean_lines(old_log_file_2.readlines()) if op.isfile(test_name): os.remove(test_name) # test it one way (printing default off) set_log_file(test_name) set_log_level('WARNING') # should NOT print evoked = Evoked(fname_evoked, condition=1) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = Evoked(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = Evoked(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = Evoked(fname_evoked, condition=1, verbose=True) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) set_log_file(None) # Need to do this to close the old file os.remove(test_name) # now go the other way (printing default on) set_log_file(test_name) set_log_level('INFO') # should NOT print evoked = Evoked(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = Evoked(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = Evoked(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) with open(fname_log, 'r') as old_log_file: assert_equal(new_lines, old_lines) # check to make sure appending works (and as default, raises a warning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') set_log_file(test_name, overwrite=False) assert len(w) == 0 set_log_file(test_name) assert len(w) == 1 evoked = Evoked(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines_2) # make sure overwriting works set_log_file(test_name, overwrite=True) # this line needs to be called to actually do some logging evoked = Evoked(fname_evoked, condition=1) del evoked with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines)
#!/usr/bin/env python2.7 from mne import utils utils.set_log_file(fname = '/home/kjs/logs/mne.log', overwrite=False) utils.set_log_level(verbose = True) utils.logger.propagate = True LOGGER = utils.logger import numpy as np from scipy.stats import skew, kurtosis import pyeeg import file_handler import csv import time from mne.filter import high_pass_filter, band_stop_filter import random from decs import coroutine from mpi4py import MPI ''' The feature extractor creates a features.csv file The file is organized row by column, sample by features Names of features must be know before writing the file For electrode specific features, we use the max_e If a sample uses less electrodes that max_e, then it will have NA values for these electrodes In cases where a calculation is recycled in 2+ features, ex first order differential the calc is stroed in a by electrode dictionary
def test_logging(): """Test logging (to file).""" pytest.raises(ValueError, set_log_level, 'foo') tempdir = _TempDir() test_name = op.join(tempdir, 'test.log') with open(fname_log, 'r') as old_log_file: # [:-1] used to strip an extra "No baseline correction applied" old_lines = clean_lines(old_log_file.readlines()) old_lines.pop(-1) with open(fname_log_2, 'r') as old_log_file_2: old_lines_2 = clean_lines(old_log_file_2.readlines()) old_lines_2.pop(14) old_lines_2.pop(-1) if op.isfile(test_name): os.remove(test_name) # test it one way (printing default off) set_log_file(test_name) set_log_level('WARNING') # should NOT print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert (fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1, verbose=True) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) set_log_file(None) # Need to do this to close the old file os.remove(test_name) # now go the other way (printing default on) set_log_file(test_name) set_log_level('INFO') # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert (fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) # check to make sure appending works (and as default, raises a warning) set_log_file(test_name, overwrite=False) with pytest.warns(RuntimeWarning, match='appended to the file'): set_log_file(test_name) evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines_2) # make sure overwriting works set_log_file(test_name, overwrite=True) # this line needs to be called to actually do some logging evoked = read_evokeds(fname_evoked, condition=1) del evoked with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines)
def setup_provenance(script, results_dir, config=None, use_agg=True, run_id=None): """Setup provenance tracking Parameters ---------- script : str The script that was executed. results_dir : str The results directory. config : None | str The name of the config file. By default, the function expects the config to be under `__script__/' named `config.py`. It can also be another kind of textfile, e.g. .json. use_agg : bool Whether to use the 'Agg' backend for matplotlib or not. Returns ------- report : mne.report.Report The mne report. Side-effects ------------ - make results dir if it does not exists - sets log file for sterr output - writes log file with runtime information """ if use_agg is True: import matplotlib matplotlib.use('Agg') if not callable(script): if not op.isfile(script): raise ValueError('sorry, this is not a script!') if not op.isdir(results_dir) and not callable(script): results_dir = op.join(op.dirname(op.dirname(script)), results_dir) else: results_dir = op.join(op.curdir, results_dir) if not callable(script): step = op.splitext(op.split(script)[1])[0] else: step = script.__name__ if not op.isabs(results_dir): results_dir = op.abspath(results_dir) start_path = op.dirname(results_dir) results_dir = op.join(results_dir, step) if not op.exists(results_dir): logger.info('generating results dir') _forec_create_dir(results_dir, start=start_path) if run_id is None: run_id = create_run_id() logger.info('generated run id: %s' % run_id) else: logger.info('using existing run id: %s' % run_id) logger.info('preparing logging:') logging_dir = op.join(results_dir, run_id) if not op.exists(logging_dir): logger.info('... making logging directory: %s' % logging_dir) os.mkdir(logging_dir) else: logger.info('... using logging directory: %s' % logging_dir) modules = get_versions(sys) runtime_log = op.join(logging_dir, 'run_time.json') with open(runtime_log, 'w') as fid: json.dump(modules, fid) logger.info('... writing runtime info to: %s' % runtime_log) script_code_out = op.join(logging_dir, 'script.py') if callable(script): script_code_in = inspect.getsourcefile(script) else: script_code_in = script if not op.isfile(script_code_out): with open(script_code_out, 'w') as fid: with open(script_code_in) as script_fid: source_code = script_fid.read() fid.write(source_code) logger.info('... logging source code of calling script') if config is None: config = 'config.py' if op.isabs(config): config_fname = config else: config_fname = '' config_code = op.join( # weird behavior of join if last arg is path results_dir, run_id, op.split(config_fname)[-1]) if not op.isfile(config_fname): logger.info('... No config found. Logging nothing.') elif op.isfile(config_code): logger.info('... Config already written. I assume that you are using' ' the same run_id for different runs of your script.') else: with open(config_code, 'w') as fid: with open(config_fname) as config_fid: source_code = config_fid.read() fid.write(source_code) logger.info('... logging source code of "%s".' % config_fname) logger.info('... preparing Report') report = Report(title=step) report.data_path = logging_dir std_logfile = op.join(logging_dir, 'run_output.log') logger.info('... setting logfile: %s' % std_logfile) set_log_file(std_logfile) return report, run_id, results_dir, logger
def test_logging(): """Test logging (to file).""" assert_raises(ValueError, set_log_level, 'foo') tempdir = _TempDir() test_name = op.join(tempdir, 'test.log') with open(fname_log, 'r') as old_log_file: # [:-1] used to strip an extra "No baseline correction applied" old_lines = clean_lines(old_log_file.readlines()) old_lines.pop(-1) with open(fname_log_2, 'r') as old_log_file_2: old_lines_2 = clean_lines(old_log_file_2.readlines()) old_lines_2.pop(14) old_lines_2.pop(-1) if op.isfile(test_name): os.remove(test_name) # test it one way (printing default off) set_log_file(test_name) set_log_level('WARNING') # should NOT print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1, verbose=True) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) set_log_file(None) # Need to do this to close the old file os.remove(test_name) # now go the other way (printing default on) set_log_file(test_name) set_log_level('INFO') # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) # check to make sure appending works (and as default, raises a warning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') set_log_file(test_name, overwrite=False) assert_equal(len(w), 0) set_log_file(test_name) assert_equal(len(w), 1) assert_true('test_utils.py' in w[0].filename) evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines_2) # make sure overwriting works set_log_file(test_name, overwrite=True) # this line needs to be called to actually do some logging evoked = read_evokeds(fname_evoked, condition=1) del evoked with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines)
def test_logging(): """Test logging (to file) """ assert_raises(ValueError, set_log_level, 'foo') tempdir = _TempDir() test_name = op.join(tempdir, 'test.log') with open(fname_log, 'r') as old_log_file: old_lines = clean_lines(old_log_file.readlines()) with open(fname_log_2, 'r') as old_log_file_2: old_lines_2 = clean_lines(old_log_file_2.readlines()) # we changed our logging a little bit old_lines = [o.replace('No baseline correction applied...', 'No baseline correction applied') for o in old_lines] old_lines_2 = [o.replace('No baseline correction applied...', 'No baseline correction applied') for o in old_lines_2] if op.isfile(test_name): os.remove(test_name) # test it one way (printing default off) set_log_file(test_name) set_log_level('WARNING') # should NOT print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1, verbose=True) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines) set_log_file(None) # Need to do this to close the old file os.remove(test_name) # now go the other way (printing default on) set_log_file(test_name) set_log_level('INFO') # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert_true(fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert_true(fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) with open(fname_log, 'r') as old_log_file: assert_equal(new_lines, old_lines) # check to make sure appending works (and as default, raises a warning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') set_log_file(test_name, overwrite=False) assert_equal(len(w), 0) set_log_file(test_name) assert_equal(len(w), 1) assert_true('test_utils.py' in w[0].filename) evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines_2) # make sure overwriting works set_log_file(test_name, overwrite=True) # this line needs to be called to actually do some logging evoked = read_evokeds(fname_evoked, condition=1) del evoked with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert_equal(new_lines, old_lines)
def setup_provenance(script, results_dir, use_agg=True): """Setup provenance tracking Parameters ---------- script : str The script that was executed. results_dir : str The results directory. use_agg : bool Whether to use the 'Agg' backend for matplotlib or not. Returns ------- report : mne.report.Report The mne report. Side-effects ------------ - make results dir if it does not exists - sets log file for sterr output - writes log file with runtime information """ if use_agg is True: import matplotlib matplotlib.use('Agg') if not op.isfile(script): raise ValueError('sorry, this is not a script!') step = op.splitext(op.split(script)[1])[0] results_dir = op.join(results_dir, step) if not op.exists(results_dir): logger.info('generating results dir') os.mkdir(results_dir) run_id = create_run_id() logger.info('generated run id: %s' % run_id) logger.info('preparing logging:') logging_dir = op.join(results_dir, run_id) logger.info('... making logging directory: %s' % logging_dir) os.mkdir(logging_dir) modules = get_versions(sys) runtime_log = op.join(logging_dir, 'run_time.json') with open(runtime_log, 'w') as fid: json.dump(modules, fid) logger.info('... writing runtime info to: %s' % runtime_log) std_logfile = op.join(logging_dir, 'run_output.log') script_code = op.join(logging_dir, 'script.py') with open(script_code, 'w') as fid: with open(script) as script_fid: source_code = script_fid.read() fid.write(source_code) logger.info('... logging source code') logger.info('... preparing Report') report = Report(title=step) report.data_path = logging_dir logger.info('... setting logfile: %s' % std_logfile) set_log_file(std_logfile) return report, run_id, results_dir, logger
def test_logging(tmpdir): """Test logging (to file).""" pytest.raises(ValueError, set_log_level, 'foo') tempdir = str(tmpdir) test_name = op.join(tempdir, 'test.log') with open(fname_log, 'r') as old_log_file: # [:-1] used to strip an extra "No baseline correction applied" old_lines = clean_lines(old_log_file.readlines()) old_lines.pop(-1) with open(fname_log_2, 'r') as old_log_file_2: old_lines_2 = clean_lines(old_log_file_2.readlines()) old_lines_2.pop(14) old_lines_2.pop(-1) if op.isfile(test_name): os.remove(test_name) # test it one way (printing default off) set_log_file(test_name) set_log_level('WARNING') # should NOT print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert (fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1, verbose=True) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert new_lines == old_lines set_log_file(None) # Need to do this to close the old file os.remove(test_name) # now go the other way (printing default on) set_log_file(test_name) set_log_level('INFO') # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING') with open(test_name) as fid: assert (fid.readlines() == []) # should NOT print evoked = read_evokeds(fname_evoked, condition=1, verbose=False) with open(test_name) as fid: assert (fid.readlines() == []) # SHOULD print evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert new_lines == old_lines # check to make sure appending works (and as default, raises a warning) set_log_file(test_name, overwrite=False) with pytest.warns(RuntimeWarning, match='appended to the file'): set_log_file(test_name) evoked = read_evokeds(fname_evoked, condition=1) with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert new_lines == old_lines_2 # make sure overwriting works set_log_file(test_name, overwrite=True) # this line needs to be called to actually do some logging evoked = read_evokeds(fname_evoked, condition=1) del evoked with open(test_name, 'r') as new_log_file: new_lines = clean_lines(new_log_file.readlines()) assert new_lines == old_lines