<commit_before>from time import sleep
import curses
import os

def plot_border():
    for a in [0,79]:
        for b in range(0,22):
            stdscr.addstr(b,a,"*", curses.color_pair(1))
    for a in [0,22]:
        for b in range(0,80):
            stdscr.addstr(a,b,"*", curses.color_pair(1))
    #stdscr.addstr(22,78,"*", curses.color_pair(1))

def check_keys():
    charin = stdscr.getch()
    if charin == ord('\\'):
        return "\\"
    if charin == ord('/'):
        return "/"
    return None

def plot_paddle(input_key):
    stdscr.addstr(ball_location[1],ball_location[0],input_key)
    scorechange = (1/ball_movement[2])
    ball_movement[2] = ball_movement[2]-0.001
    return scorechange

def check_paddle_collision():
    in_key = stdscr.inch(ball_location[1],ball_location[0])

    if in_key == ord("/") and ball_movement[0] == 1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = -1
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == -1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = 1
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == 0 and ball_movement[1] == 1:
        ball_movement[0] = -1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == 0 and ball_movement[1] == -1:
        ball_movement[0] = 1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = 1
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == -1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = -1
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 0 and ball_movement[1] == 1:
        ball_movement[0] = 1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 0 and ball_movement[1] == -1:
        ball_movement[0] = -1
        ball_movement[1] = 0
        move_ball()

    return

def check_wall_collision():
    if ball_location[0] == 0 or ball_location[0] == 79 or ball_location[1] == 24 or ball_location[1] == 0:
        return True
    else:
        return None

def move_ball():
    ball_location[0] = ball_location[0] + ball_movement[0]
    ball_location[1] = ball_location[1] + ball_movement[1]

def plot_ball():
    stdscr.addstr(ball_location[1],ball_location[0], "o") # plot ball

stdscr = curses.initscr()
curses.noecho()
curses.curs_set(0)
quit = False
ball_movement = [-1,0, 0.1] # x movement, y movement, delay (seconds)
ball_location = [40,12]
stdscr.nodelay(1)
collide = None
score = 0
curses.start_color()
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_YELLOW)
curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_RED)
plot_border()
stdscr.addstr(23,35,"SCORE: " + str(int(score)), curses.color_pair(2))

while not(collide):

    plot_ball()
    stdscr.refresh()
    sleep(ball_movement[2])

    stdscr.addstr(ball_location[1],ball_location[0], ' ') # erase ball
    move_ball()

    input_command = None
    collide = None
    input_command = check_keys()

    if input_command:
        scorediff = plot_paddle(input_command)
        score = score + scorediff
        stdscr.addstr(23,35,"SCORE: " + str(int(score)), curses.color_pair(2))

    check_paddle_collision() # is ball about to hit a paddle?
    collide = check_wall_collision() # is ball about to hit a wall?

curses.nocbreak()
stdscr.keypad(0)
curses.endwin()
os.system('clear')
print "You hit the side!"
print ""
print "Final score: " + str(int(score))<commit_msg>Couple of small gameplay changes.<commit_after>from time import sleep
import curses
import os
import ConfigParser

def plot_border():
    for a in [0,79]:
        for b in range(0,22):
            stdscr.addstr(b,a,"*", curses.color_pair(1))
    for a in [0,22]:
        for b in range(0,80):
            stdscr.addstr(a,b,"*", curses.color_pair(1))
    #stdscr.addstr(22,78,"*", curses.color_pair(1))

def check_keys():
    charin = stdscr.getch()
    if charin == ord('\\'):
        return "\\"
    if charin == ord('/'):
        return "/"
    return None

def plot_paddle(input_key):
    stdscr.addstr(ball_location[1],ball_location[0],input_key)
    scorechange = (1/ball_movement[2])
    ball_movement[2] = ball_movement[2]-0.004
    return scorechange

def check_paddle_collision():
    in_key = stdscr.inch(ball_location[1],ball_location[0])

    if in_key == ord("/") and ball_movement[0] == 1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = -1
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == -1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = 1
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == 0 and ball_movement[1] == 1:
        ball_movement[0] = -1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("/") and ball_movement[0] == 0 and ball_movement[1] == -1:
        ball_movement[0] = 1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = 1
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == -1 and ball_movement[1] == 0:
        ball_movement[0] = 0
        ball_movement[1] = -1
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 0 and ball_movement[1] == 1:
        ball_movement[0] = 1
        ball_movement[1] = 0
        move_ball()

    elif in_key == ord("\\") and ball_movement[0] == 0 and ball_movement[1] == -1:
        ball_movement[0] = -1
        ball_movement[1] = 0
        move_ball()

    return

def check_wall_collision():
    if ball_location[0] <= 0 or ball_location[0] >= 79 or ball_location[1] >= 22 or ball_location[1] <= 0:
        return True
    else:
        return None

def move_ball():
    ball_location[0] = ball_location[0] + ball_movement[0]
    ball_location[1] = ball_location[1] + ball_movement[1]

def plot_ball():
    stdscr.addstr(ball_location[1],ball_location[0], "o") # plot ball

stdscr = curses.initscr()
curses.noecho()
curses.curs_set(0)
quit = False
ball_movement = [-1,0, 0.1] # x movement, y movement, delay (seconds)
ball_location = [40,12]
stdscr.nodelay(1)
collide = None
score = 0
curses.start_color()
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_YELLOW)
curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_RED)
plot_border()
stdscr.addstr(23,35,"SCORE: " + str(int(score)), curses.color_pair(2))

while not(collide):

    plot_ball()
    stdscr.refresh()
    sleep(ball_movement[2])

    stdscr.addstr(ball_location[1],ball_location[0], ' ') # erase ball
    move_ball()

    input_command = None
    collide = None
    input_command = check_keys()

    if input_command:
        scorediff = plot_paddle(input_command)
        score = score + scorediff
        stdscr.addstr(23,35,"SCORE: " + str(int(score)), curses.color_pair(2))

    check_paddle_collision() # is ball about to hit a paddle?
    collide = check_wall_collision() # is ball about to hit a wall?

curses.nocbreak()
stdscr.keypad(0)
curses.endwin()
os.system('clear')
print "You hit the side!"
print ""
print "Final score: " + str(int(score))<|endoftext|>


<commit_before>from parcels import FieldSet, ParticleSet, ScipyParticle, JITParticle, AdvectionRK4
from datetime import timedelta as delta
from os import path
from glob import glob
import numpy as np
import pytest

ptype = {'scipy': ScipyParticle, 'jit': JITParticle}

def set_globcurrent_fieldset(filename=None, indices={}):
    if filename is None:
        filename = path.join(path.dirname(__file__), 'GlobCurrent_example_data',
                             '20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc')
    filenames = {'U': filename, 'V': filename}
    variables = {'U': 'eastward_eulerian_current_velocity', 'V': 'northward_eulerian_current_velocity'}
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}
    return FieldSet.from_netcdf(filenames, variables, dimensions, indices)

def test_globcurrent_fieldset():
    fieldset = set_globcurrent_fieldset()
    assert(fieldset.U.lon.size == 81)
    assert(fieldset.U.lat.size == 41)
    assert(fieldset.U.data.shape == (365, 41, 81))
    assert(fieldset.V.lon.size == 81)
    assert(fieldset.V.lat.size == 41)
    assert(fieldset.V.data.shape == (365, 41, 81))

    indices = {'lon': [5], 'lat': range(20, 30)}
    fieldsetsub = set_globcurrent_fieldset(indices=indices)
    assert np.allclose(fieldsetsub.U.lon, fieldset.U.lon[indices['lon']])
    assert np.allclose(fieldsetsub.U.lat, fieldset.U.lat[indices['lat']])
    assert np.allclose(fieldsetsub.V.lon, fieldset.V.lon[indices['lon']])
    assert np.allclose(fieldsetsub.V.lat, fieldset.V.lat[indices['lat']])

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
@pytest.mark.parametrize('dt, substart, subend, lonstart, latstart, irange', [
    (300., 0, 3, 25, -35, range(3, 9, 1)),
    (-300., 8, 10, 20, -39, range(7, 2, -1))
])
def test_globcurrent_fieldset_advancetime(mode, dt, substart, subend, lonstart, latstart, irange):
    basepath = path.join(path.dirname(__file__), 'GlobCurrent_example_data',
                         '20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc')
    files = glob(str(basepath))

    fieldsetsub = set_globcurrent_fieldset(files[substart:subend])
    psetsub = ParticleSet.from_list(fieldset=fieldsetsub, pclass=ptype[mode], lon=[lonstart], lat=[latstart])

    fieldsetall = set_globcurrent_fieldset(files[0:10])
    psetall = ParticleSet.from_list(fieldset=fieldsetall, pclass=ptype[mode], lon=[lonstart], lat=[latstart])
    if dt < 0:
        psetsub[0].time = fieldsetsub.U.time[-1]
        psetall[0].time = fieldsetall.U.time[-1]

    for i in irange:
        psetsub.execute(AdvectionRK4, starttime=psetsub[0].time, runtime=delta(days=1), dt=dt)
        fieldsetsub.advancetime(set_globcurrent_fieldset(files[i]))

        psetall.execute(AdvectionRK4, starttime=psetall[0].time, runtime=delta(days=1), dt=dt)

    assert abs(psetsub[0].lon - psetall[0].lon) < 1e-4

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_globcurrent_particles(mode):
    fieldset = set_globcurrent_fieldset()

    lonstart = [25]
    latstart = [-35]

    pset = ParticleSet(fieldset, pclass=ptype[mode], lon=lonstart, lat=latstart)

    pset.execute(AdvectionRK4, runtime=delta(days=1), dt=delta(minutes=5),
                 interval=delta(hours=1))

    assert(abs(pset[0].lon - 23.8) < 1)
    assert(abs(pset[0].lat - -35.3) < 1)
<commit_msg>Adding a unit test that shows how time_extrapolation errors are hard to read<commit_after>from parcels import FieldSet, ParticleSet, ScipyParticle, JITParticle, AdvectionRK4
from datetime import timedelta as delta
from os import path
from glob import glob
import numpy as np
import pytest

ptype = {'scipy': ScipyParticle, 'jit': JITParticle}

def set_globcurrent_fieldset(filename=None, indices={}):
    if filename is None:
        filename = path.join(path.dirname(__file__), 'GlobCurrent_example_data',
                             '20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc')
    filenames = {'U': filename, 'V': filename}
    variables = {'U': 'eastward_eulerian_current_velocity', 'V': 'northward_eulerian_current_velocity'}
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}
    return FieldSet.from_netcdf(filenames, variables, dimensions, indices)

def test_globcurrent_fieldset():
    fieldset = set_globcurrent_fieldset()
    assert(fieldset.U.lon.size == 81)
    assert(fieldset.U.lat.size == 41)
    assert(fieldset.U.data.shape == (365, 41, 81))
    assert(fieldset.V.lon.size == 81)
    assert(fieldset.V.lat.size == 41)
    assert(fieldset.V.data.shape == (365, 41, 81))

    indices = {'lon': [5], 'lat': range(20, 30)}
    fieldsetsub = set_globcurrent_fieldset(indices=indices)
    assert np.allclose(fieldsetsub.U.lon, fieldset.U.lon[indices['lon']])
    assert np.allclose(fieldsetsub.U.lat, fieldset.U.lat[indices['lat']])
    assert np.allclose(fieldsetsub.V.lon, fieldset.V.lon[indices['lon']])
    assert np.allclose(fieldsetsub.V.lat, fieldset.V.lat[indices['lat']])

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
@pytest.mark.parametrize('dt, substart, subend, lonstart, latstart, irange', [
    (300., 0, 3, 25, -35, range(3, 9, 1)),
    (-300., 8, 10, 20, -39, range(7, 2, -1))
])
def test_globcurrent_fieldset_advancetime(mode, dt, substart, subend, lonstart, latstart, irange):
    basepath = path.join(path.dirname(__file__), 'GlobCurrent_example_data',
                         '20*-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc')
    files = glob(str(basepath))

    fieldsetsub = set_globcurrent_fieldset(files[substart:subend])
    psetsub = ParticleSet.from_list(fieldset=fieldsetsub, pclass=ptype[mode], lon=[lonstart], lat=[latstart])

    fieldsetall = set_globcurrent_fieldset(files[0:10])
    psetall = ParticleSet.from_list(fieldset=fieldsetall, pclass=ptype[mode], lon=[lonstart], lat=[latstart])
    if dt < 0:
        psetsub[0].time = fieldsetsub.U.time[-1]
        psetall[0].time = fieldsetall.U.time[-1]

    for i in irange:
        psetsub.execute(AdvectionRK4, starttime=psetsub[0].time, runtime=delta(days=1), dt=dt)
        fieldsetsub.advancetime(set_globcurrent_fieldset(files[i]))

        psetall.execute(AdvectionRK4, starttime=psetall[0].time, runtime=delta(days=1), dt=dt)

    assert abs(psetsub[0].lon - psetall[0].lon) < 1e-4

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_globcurrent_particles(mode):
    fieldset = set_globcurrent_fieldset()

    lonstart = [25]
    latstart = [-35]

    pset = ParticleSet(fieldset, pclass=ptype[mode], lon=lonstart, lat=latstart)

    pset.execute(AdvectionRK4, runtime=delta(days=1), dt=delta(minutes=5),
                 interval=delta(hours=1))

    assert(abs(pset[0].lon - 23.8) < 1)
    assert(abs(pset[0].lat - -35.3) < 1)

@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_globcurrent_time_extrapolation_error(mode):
    fieldset = set_globcurrent_fieldset()

    pset = ParticleSet(fieldset, pclass=ptype[mode], lon=[25], lat=[-35])
    starttime = fieldset.U.time[0]-delta(days=1).total_seconds()

    pset.execute(AdvectionRK4, starttime=starttime, runtime=delta(days=1),
                 dt=delta(minutes=5), interval=delta(hours=1))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2014, LABS^N
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
----------------------------------
Example experiment analysis script
----------------------------------

This sample script shows how to preprocess a simple MEG experiment
from start to finish.

The experiment was a simple audio/visual oddball detection task. One
potential purpose would be e.g. functional localization of auditory and
visual cortices.

Note that you will need to change the "acq_ssh" and "sss_ssh" parameters
to reflect your username/password on the relevant machines. You will also
need to set up public key authentication between your machine and the
two remote machines (acquisition/minea and SSS/kasga). Tutorial here:

    * https://help.ubuntu.com/community/SSH/OpenSSH/Keys

The deidentified structural directories for the one subject is needed
to do the forward and inverse solutions, extract this into your
SUBJECTS_DIR directory:

    * http://lester.ilabs.uw.edu/files/AKCLEE_110_slim.tar.gz

"""

import mnefun
from score import score
import numpy as np

try:
    # Use niprov as handler for events if it's installed
    from niprov.mnefunsupport import handler
except ImportError:
    handler = None

params = mnefun.Params(tmin=-0.2, tmax=0.5, t_adjust=-4e-3,
                       n_jobs=6, n_jobs_mkl=1,
                       n_jobs_fir='cuda', n_jobs_resample='cuda',
                       decim=5, proj_sfreq=200, filter_length='5s')

params.subjects = ['subj_01', 'subj_02']
params.structurals = [None, 'AKCLEE_110_slim']  # None means use sphere
params.dates = [(2014, 2, 14), None]  # Use "None" to more fully anonymize
params.score = score  # Scoring function used to slice data into trials
params.subject_indices = np.arange(2)  # Define which subjects to run
params.plot_drop_logs = False  # Turn off so plots do not halt processing

# Set parameters for remotely connecting to acquisition computer
params.acq_ssh = 'minea'  # Could also be e.g., "eric@minea.ilabs.uw.edu"
# Pass list of paths to search and fetch raw data
params.acq_dir = ['/sinuhe_data01/eric_non_space',
                  '/data101/eric_non_space',
                  '/sinuhe/data01/eric_non_space',
                  '/sinuhe/data02/eric_non_space',
                  '/sinuhe/data03/eric_non_space']

# Set parameters for remotely connecting to SSS workstation ('sws')
params.sws_ssh = 'kasga'
params.sws_dir = '/data06/larsoner'

# Set the niprov handler to deal with events:
params.on_process = handler

params.run_names = ['%s_funloc']
params.get_projs_from = np.arange(1)
params.inv_names = ['%s']
params.inv_runs = [np.arange(1)]
params.runs_empty = ['%s_erm']  # Define empty room runs

# Define number of SSP projectors. Columns correspond to Grad/Mag/EEG chans
params.proj_nums = [[1, 1, 0],  # ECG
                    [1, 1, 2],  # EOG
                    [0, 0, 0]]  # Continuous (from ERM)
params.cov_method = 'shrunk'  # Cleaner noise covariance regularization
params.plot_head_position = True  # Plot cHPI data for single raw file
# python | maxfilter for choosing SSS applied using either Maxfilter or MNE
params.sss_type = 'python'
# The scoring function needs to produce an event file with these values
params.in_numbers = [10, 11, 20, 21]
# Those values correspond to real categories as:
params.in_names = ['Auditory/Standard', 'Visual/Standard',
                   'Auditory/Deviant', 'Visual/Deviant']

# Define how to translate the above event types into evoked files
params.analyses = [
    'All',
    'AV',
]
params.out_names = [
    ['All'],
    params.in_names,
]
params.out_numbers = [
    [1, 1, 1, 1],       # Combine all trials
    params.in_numbers,  # Leave events split the same way they were scored
]
params.must_match = [
    [],
    [0, 1],  # Only ensure the standard event counts match
]

# Set what processing steps will execute
mnefun.do_processing(
    params,
    fetch_raw=True,     # Fetch raw recording files from acquisition machine
    do_score=True,      # Do scoring to slice data into trials

    # Before running SSS, make SUBJ/raw_fif/SUBJ_prebad.txt file with
    # space-separated list of bad MEG channel numbers
    push_raw=True,      # Push raw files and SSS script to SSS workstation
    do_sss=True,        # Run SSS remotely (on sws) or locally with mne-python
    fetch_sss=True,     # Fetch SSSed files from SSS workstation
    do_ch_fix=True,     # Fix channel ordering

    # Before running SSP, examine SSS'ed files and make
    # SUBJ/bads/bad_ch_SUBJ_post-sss.txt; usually, this should only contain EEG
    # channels.
    gen_ssp=True,       # Generate SSP vectors
    apply_ssp=True,     # Apply SSP vectors and filtering
    plot_psd=True,      # Plot raw data power spectra
    plot_hp=True,       # plot head movement from raw data
    write_epochs=True,  # Write epochs to disk
    gen_covs=True,      # Generate covariances

    # Make SUBJ/trans/SUBJ-trans.fif using mne_analyze; needed for fwd calc.
    gen_fwd=True,       # Generate forward solutions (and src space if needed)
    gen_inv=True,       # Generate inverses
    gen_report=True,    # Write mne report html of results to disk
    print_status=True,  # Print completeness status update
)
<commit_msg>Fix processing booleans in example script<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2014, LABS^N
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
----------------------------------
Example experiment analysis script
----------------------------------

This sample script shows how to preprocess a simple MEG experiment
from start to finish.

The experiment was a simple audio/visual oddball detection task. One
potential purpose would be e.g. functional localization of auditory and
visual cortices.

Note that you will need to change the "acq_ssh" and "sss_ssh" parameters
to reflect your username/password on the relevant machines. You will also
need to set up public key authentication between your machine and the
two remote machines (acquisition/minea and SSS/kasga). Tutorial here:

    * https://help.ubuntu.com/community/SSH/OpenSSH/Keys

The deidentified structural directories for the one subject is needed
to do the forward and inverse solutions, extract this into your
SUBJECTS_DIR directory:

    * http://lester.ilabs.uw.edu/files/AKCLEE_110_slim.tar.gz

"""

import mnefun
from score import score
import numpy as np

try:
    # Use niprov as handler for events if it's installed
    from niprov.mnefunsupport import handler
except ImportError:
    handler = None

params = mnefun.Params(tmin=-0.2, tmax=0.5, t_adjust=-4e-3,
                       n_jobs=6, n_jobs_mkl=1,
                       n_jobs_fir='cuda', n_jobs_resample='cuda',
                       decim=5, proj_sfreq=200, filter_length='5s')

params.subjects = ['subj_01', 'subj_02']
params.structurals = [None, 'AKCLEE_110_slim']  # None means use sphere
params.dates = [(2014, 2, 14), None]  # Use "None" to more fully anonymize
params.score = score  # Scoring function used to slice data into trials
params.subject_indices = np.arange(2)  # Define which subjects to run
params.plot_drop_logs = False  # Turn off so plots do not halt processing

# Set parameters for remotely connecting to acquisition computer
params.acq_ssh = 'minea'  # Could also be e.g., "eric@minea.ilabs.uw.edu"
# Pass list of paths to search and fetch raw data
params.acq_dir = ['/sinuhe_data01/eric_non_space',
                  '/data101/eric_non_space',
                  '/sinuhe/data01/eric_non_space',
                  '/sinuhe/data02/eric_non_space',
                  '/sinuhe/data03/eric_non_space']

# Set parameters for remotely connecting to SSS workstation ('sws')
params.sws_ssh = 'kasga'
params.sws_dir = '/data06/larsoner'

# Set the niprov handler to deal with events:
params.on_process = handler

params.run_names = ['%s_funloc']
params.get_projs_from = np.arange(1)
params.inv_names = ['%s']
params.inv_runs = [np.arange(1)]
params.runs_empty = ['%s_erm']  # Define empty room runs

# Define number of SSP projectors. Columns correspond to Grad/Mag/EEG chans
params.proj_nums = [[1, 1, 0],  # ECG
                    [1, 1, 2],  # EOG
                    [0, 0, 0]]  # Continuous (from ERM)
params.cov_method = 'shrunk'  # Cleaner noise covariance regularization
params.plot_head_position = True  # Plot cHPI data for single raw file
# python | maxfilter for choosing SSS applied using either Maxfilter or MNE
params.sss_type = 'python'
# The scoring function needs to produce an event file with these values
params.in_numbers = [10, 11, 20, 21]
# Those values correspond to real categories as:
params.in_names = ['Auditory/Standard', 'Visual/Standard',
                   'Auditory/Deviant', 'Visual/Deviant']

# Define how to translate the above event types into evoked files
params.analyses = [
    'All',
    'AV',
]
params.out_names = [
    ['All'],
    params.in_names,
]
params.out_numbers = [
    [1, 1, 1, 1],       # Combine all trials
    params.in_numbers,  # Leave events split the same way they were scored
]
params.must_match = [
    [],
    [0, 1],  # Only ensure the standard event counts match
]

# Set what processing steps will execute
mnefun.do_processing(
    params,
    fetch_raw=True,     # Fetch raw recording files from acquisition machine
    do_score=True,      # Do scoring to slice data into trials

    # Before running SSS, make SUBJ/raw_fif/SUBJ_prebad.txt file with
    # space-separated list of bad MEG channel numbers
    push_raw=True,      # Push raw files and SSS script to SSS workstation
    do_sss=True,        # Run SSS remotely (on sws) or locally with mne-python
    fetch_sss=True,     # Fetch SSSed files from SSS workstation
    do_ch_fix=True,     # Fix channel ordering

    # Before running SSP, examine SSS'ed files and make
    # SUBJ/bads/bad_ch_SUBJ_post-sss.txt; usually, this should only contain EEG
    # channels.
    gen_ssp=True,       # Generate SSP vectors
    apply_ssp=True,     # Apply SSP vectors and filtering
    plot_psd=True,      # Plot raw data power spectra
    write_epochs=True,  # Write epochs to disk
    gen_covs=True,      # Generate covariances

    # Make SUBJ/trans/SUBJ-trans.fif using mne_analyze; needed for fwd calc.
    gen_fwd=True,       # Generate forward solutions (and src space if needed)
    gen_inv=True,       # Generate inverses
    gen_report=True,    # Write mne report html of results to disk
    print_status=True,  # Print completeness status update
)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright 2017, IBM.
#
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.

"""
Quantum circuit object.
"""

from collections import OrderedDict
from copy import deepcopy
import itertools
import warnings
import sys
import multiprocessing as mp

from qiskit.qasm import _qasm
from qiskit.exceptions import QiskitError
from .quantumregister import QuantumRegister
from .classicalregister import ClassicalRegister

class QuantumCircuit:
    """Quantum circuit."""
    instances = 0
    prefix = 'circuit'

    # Class variable OPENQASM header
    header = "OPENQASM 2.0;"

    # Class variable with gate definitions
    # This is a dict whose values are dicts with the
    # following keys:
    #   "print" = True or False
    #   "opaque" = True or False
    #   "n_args" = number of real parameters
    #   "n_bits" = number of qubits
    #   "args"   = list of parameter names
    #   "bits"   = list of qubit names
    #   "body"   = GateBody AST node
    definitions = OrderedDict()

    def __init__(self, *regs, name=None):
        """Create a new circuit.

        A circuit is a list of instructions bound to some registers.

        Args:
            *regs (Registers): registers to include in the circuit.
            name (str or None): the name of the quantum circuit. If
                None, an automatically generated string will be assigned.

        Raises:
            QiskitError: if the circuit name, if given, is not valid.
        """
        if name is None:
            name = self.cls_prefix() + str(self.cls_instances())
            # pylint: disable=not-callable
            # (known pylint bug: https://github.com/PyCQA/pylint/issues/1699)
            if sys.platform != "win32" and \
               isinstance(mp.current_process(), mp.context.ForkProcess):
                name += '-{}'.format(mp.current_process().pid)
        self._increment_instances()

        if not isinstance(name, str):
            raise QiskitError("The circuit name should be a string "
                              "(or None to auto-generate a name).")

        self.name = name

        # Data contains a list of instructions in the order they were applied.
        self.data = []

        # This is a map of registers bound to this circuit, by name.
        self.qregs = []
        self.cregs = []
        self.add_register(*regs)

    def __str__(self):
        return str(self.draw(output='text'))

    def __eq__(self, other):
        # TODO: removed the DAG from this function
        from qiskit.converters import circuit_to_dag
        return circuit_to_dag(self) == circuit_to_dag(other)

    @classmethod
    def _increment_instances(cls):
        cls.instances += 1

    @classmethod
    def cls_instances(cls):
        """Return the current number of instances of this class,
        useful for auto naming."""
        return cls.instances

    @classmethod
    def cls_prefix(cls):
        """Return the prefix to use for auto naming."""
        return cls.prefix

    def has_register(self, register):
        """
        Test if this circuit has the register r.

        Args:
            register (Register): a quantum or classical register.

        Returns:
            bool: True if the register is contained in this circuit.
        """
        has_reg = False
        if (isinstance(register, QuantumRegister) and
                register in self.qregs):
            has_reg = True
        elif (isinstance(register, ClassicalRegister) and
              register in self.cregs):
            has_reg = True
        return has_reg

    def combine(self, rhs):
        """
        Append rhs to self if self contains compatible registers.

        Two circuits are compatible if they contain the same registers
        or if they contain different registers with unique names. The
        returned circuit will contain all unique registers between both
        circuits.

        Return self + rhs as a new object.
        """
        # Check registers in LHS are compatible with RHS
        self._check_compatible_regs(rhs)

        # Make new circuit with combined registers
        combined_qregs = deepcopy(self.qregs)
        combined_cregs = deepcopy(self.cregs)

        for element in rhs.qregs:
            if element not in self.qregs:
                combined_qregs.append(element)
        for element in rhs.cregs:
            if element not in self.cregs:
                combined_cregs.append(element)
        circuit = QuantumCircuit(*combined_qregs, *combined_cregs)
        for gate in itertools.chain(self.data, rhs.data):
            gate.reapply(circuit)
        return circuit

    def extend(self, rhs):
        """
        Append rhs to self if self contains compatible registers.

        Two circuits are compatible if they contain the same registers
        or if they contain different registers with unique names. The
        returned circuit will contain all unique registers between both
        circuits.

        Modify and return self.
        """
        # Check registers in LHS are compatible with RHS
        self._check_compatible_regs(rhs)

        # Add new registers
        for element in rhs.qregs:
            if element not in self.qregs:
                self.qregs.append(element)
        for element in rhs.cregs:
            if element not in self.cregs:
                self.cregs.append(element)

        # Add new gates
        for gate in rhs.data:
            gate.reapply(self)
        return self

    def __add__(self, rhs):
        """Overload + to implement self.concatenate."""
        return self.combine(rhs)

    def __iadd__(self, rhs):
        """Overload += to implement self.extend."""
        return self.extend(rhs)

    def __len__(self):
        """Return number of operations in circuit."""
        return len(self.data)

    def __getitem__(self, item):
        """Return indexed operation."""
        return self.data[item]

    def _attach(self, instruction):
        """Attach an instruction."""
        self.data.append(instruction)
        return instruction

    def add_register(self, *regs):
        """Add registers."""
        for register in regs:
            if register in self.qregs or register in self.cregs:
                raise QiskitError("register name \"%s\" already exists"
                                  % register.name)
            if isinstance(register, QuantumRegister):
                self.qregs.append(register)
            elif isinstance(register, ClassicalRegister):
                self.cregs.append(register)
            else:
                raise QiskitError("expected a register")

    def add(self, *regs):
        """Add registers."""

        warnings.warn('The add() function is deprecated and will be '
                      'removed in a future release. Instead use '
                      'QuantumCircuit.add_register().', DeprecationWarning)
        self.add_register(*regs)

    def _check_qreg(self, register):
        """Raise exception if r is not in this circuit or not qreg."""
        if not isinstance(register, QuantumRegister):
            raise QiskitError("expected quantum register")
        if not self.has_register(register):
            raise QiskitError(
                "register '%s' not in this circuit" %
                register.name)

    def _check_qubit(self, qubit):
        """Raise exception if qubit is not in this circuit or bad format."""
        if not isinstance(qubit, tuple):
            raise QiskitError("%s is not a tuple."
                              "A qubit should be formated as a tuple." % str(qubit))
        if not len(qubit) == 2:
            raise QiskitError("%s is not a tuple with two elements, but %i instead" % len(qubit))
        if not isinstance(qubit[1], int):
            raise QiskitError("The second element of a tuple defining a qubit should be an int:"
                              "%s was found instead" % type(qubit[1]).__name__)
        self._check_qreg(qubit[0])
        qubit[0].check_range(qubit[1])

    def _check_creg(self, register):
        """Raise exception if r is not in this circuit or not creg."""
        if not isinstance(register, ClassicalRegister):
            raise QiskitError("Expected ClassicalRegister, but %s given" % type(register))
        if not self.has_register(register):
            raise QiskitError(
                "register '%s' not in this circuit" %
                register.name)

    def _check_dups(self, qubits):
        """Raise exception if list of qubits contains duplicates."""
        squbits = set(qubits)
        if len(squbits) != len(qubits):
            raise QiskitError("duplicate qubit arguments")

    def _check_compatible_regs(self, rhs):
        """Raise exception if the circuits are defined on incompatible registers"""

        list1 = self.qregs + self.cregs
        list2 = rhs.qregs + rhs.cregs
        for element1 in list1:
            for element2 in list2:
                if element2.name == element1.name:
                    if element1 != element2:
                        raise QiskitError("circuits are not compatible")

    def _gate_string(self, name):
        """Return a QASM string for the named gate."""
        out = ""
        if self.definitions[name]["opaque"]:
            out = "opaque " + name
        else:
            out = "gate " + name
        if self.definitions[name]["n_args"] > 0:
            out += "(" + ",".join(self.definitions[name]["args"]) + ")"
        out += " " + ",".join(self.definitions[name]["bits"])
        if self.definitions[name]["opaque"]:
            out += ";"
        else:
            out += "\n{\n" + self.definitions[name]["body"].qasm() + "}\n"
        return out

    def qasm(self):
        """Return OPENQASM string."""
        string_temp = self.header + "\n"
        for gate_name in self.definitions:
            if self.definitions[gate_name]["print"]:
                string_temp += self._gate_string(gate_name)
        for register in self.qregs:
            string_temp += register.qasm() + "\n"
        for register in self.cregs:
            string_temp += register.qasm() + "\n"
        for instruction in self.data:
            string_temp += instruction.qasm() + "\n"
        return string_temp

    def draw(self, scale=0.7, filename=None, style=None, output='text',
             interactive=False, line_length=None, plot_barriers=True,
             reverse_bits=False):
        """Draw the quantum circuit

        Using the output parameter you can specify the format. The choices are:
        0. text: ASCII art string
        1. latex: high-quality images, but heavy external software dependencies
        2. matplotlib: purely in Python with no external dependencies

        Defaults to an overcomplete basis, in order to not alter gates.

        Args:
            scale (float): scale of image to draw (shrink if < 1)
            filename (str): file path to save image to
            style (dict or str): dictionary of style or file name of style
                file. You can refer to the
                :ref:`Style Dict Doc <style-dict-doc>` for more information
                on the contents.
            output (str): Select the output method to use for drawing the
                circuit. Valid choices are `text`, `latex`, `latex_source`,
                `mpl`.
            interactive (bool): when set true show the circuit in a new window
                (for `mpl` this depends on the matplotlib backend being used
                supporting this). Note when used with either the `text` or the
                `latex_source` output type this has no effect and will be
                silently ignored.
            line_length (int): sets the length of the lines generated by `text`
            reverse_bits (bool): When set to True reverse the bit order inside
                registers for the output visualization.
            plot_barriers (bool): Enable/disable drawing barriers in the output
                circuit. Defaults to True.

        Returns:
            PIL.Image or matplotlib.figure or str or TextDrawing:
                * PIL.Image: (output `latex`) an in-memory representation of the
                  image of the circuit diagram.
                * matplotlib.figure: (output `mpl`) a matplotlib figure object
                  for the circuit diagram.
                * str: (output `latex_source`). The LaTeX source code.
                * TextDrawing: (output `text`). A drawing that can be printed as
                  ascii art

        Raises:
            VisualizationError: when an invalid output method is selected
        """
        from qiskit.tools import visualization
        return visualization.circuit_drawer(self, scale=scale,
                                            filename=filename, style=style,
                                            output=output,
                                            interactive=interactive,
                                            line_length=line_length,
                                            plot_barriers=plot_barriers,
                                            reverse_bits=reverse_bits)

    def size(self):
        """Return total number of operations in circuit."""
        # TODO: removed the DAG from this function
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.size()

    def depth(self):
        """Return circuit depth (i.e. length of critical path)."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.depth()

    def width(self):
        """Return number of qubits in circuit."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.width()

    def count_ops(self):
        """Count each operation kind in the circuit.

        Returns:
            dict: a breakdown of how many operations of each kind.
        """
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.count_ops()

    def num_tensor_factors(self):
        """How many non-entangled subcircuits can the circuit be factored to."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.num_tensor_factors()

    @staticmethod
    def from_qasm_file(path):
        """Take in a QASM file and generate a QuantumCircuit object.

        Args:
          path (str): Path to the file for a QASM program
        Return:
          QuantumCircuit: The QuantumCircuit object for the input QASM
        """
        qasm = _qasm.Qasm(filename=path)
        return _circuit_from_qasm(qasm)

    @staticmethod
    def from_qasm_str(qasm_str):
        """Take in a QASM string and generate a QuantumCircuit object.

        Args:
          qasm_str (str): A QASM program string
        Return:
          QuantumCircuit: The QuantumCircuit object for the input QASM
        """
        qasm = _qasm.Qasm(data=qasm_str)
        return _circuit_from_qasm(qasm)

def _circuit_from_qasm(qasm):
    # pylint: disable=cyclic-import
    from qiskit.converters import ast_to_dag
    from qiskit.converters import dag_to_circuit
    ast = qasm.parse()
    dag = ast_to_dag(ast)
    return dag_to_circuit(dag)
<commit_msg>A minor fix in a comment (#1608)<commit_after># -*- coding: utf-8 -*-

# Copyright 2017, IBM.
#
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.

"""
Quantum circuit object.
"""

from collections import OrderedDict
from copy import deepcopy
import itertools
import warnings
import sys
import multiprocessing as mp

from qiskit.qasm import _qasm
from qiskit.exceptions import QiskitError
from .quantumregister import QuantumRegister
from .classicalregister import ClassicalRegister

class QuantumCircuit:
    """Quantum circuit."""
    instances = 0
    prefix = 'circuit'

    # Class variable OPENQASM header
    header = "OPENQASM 2.0;"

    # Class variable with gate definitions
    # This is a dict whose values are dicts with the
    # following keys:
    #   "print" = True or False
    #   "opaque" = True or False
    #   "n_args" = number of real parameters
    #   "n_bits" = number of qubits
    #   "args"   = list of parameter names
    #   "bits"   = list of qubit names
    #   "body"   = GateBody AST node
    definitions = OrderedDict()

    def __init__(self, *regs, name=None):
        """Create a new circuit.

        A circuit is a list of instructions bound to some registers.

        Args:
            *regs (Registers): registers to include in the circuit.
            name (str or None): the name of the quantum circuit. If
                None, an automatically generated string will be assigned.

        Raises:
            QiskitError: if the circuit name, if given, is not valid.
        """
        if name is None:
            name = self.cls_prefix() + str(self.cls_instances())
            # pylint: disable=not-callable
            # (known pylint bug: https://github.com/PyCQA/pylint/issues/1699)
            if sys.platform != "win32" and \
               isinstance(mp.current_process(), mp.context.ForkProcess):
                name += '-{}'.format(mp.current_process().pid)
        self._increment_instances()

        if not isinstance(name, str):
            raise QiskitError("The circuit name should be a string "
                              "(or None to auto-generate a name).")

        self.name = name

        # Data contains a list of instructions in the order they were applied.
        self.data = []

        # This is a map of registers bound to this circuit, by name.
        self.qregs = []
        self.cregs = []
        self.add_register(*regs)

    def __str__(self):
        return str(self.draw(output='text'))

    def __eq__(self, other):
        # TODO: removed the DAG from this function
        from qiskit.converters import circuit_to_dag
        return circuit_to_dag(self) == circuit_to_dag(other)

    @classmethod
    def _increment_instances(cls):
        cls.instances += 1

    @classmethod
    def cls_instances(cls):
        """Return the current number of instances of this class,
        useful for auto naming."""
        return cls.instances

    @classmethod
    def cls_prefix(cls):
        """Return the prefix to use for auto naming."""
        return cls.prefix

    def has_register(self, register):
        """
        Test if this circuit has the register r.

        Args:
            register (Register): a quantum or classical register.

        Returns:
            bool: True if the register is contained in this circuit.
        """
        has_reg = False
        if (isinstance(register, QuantumRegister) and
                register in self.qregs):
            has_reg = True
        elif (isinstance(register, ClassicalRegister) and
              register in self.cregs):
            has_reg = True
        return has_reg

    def combine(self, rhs):
        """
        Append rhs to self if self contains compatible registers.

        Two circuits are compatible if they contain the same registers
        or if they contain different registers with unique names. The
        returned circuit will contain all unique registers between both
        circuits.

        Return self + rhs as a new object.
        """
        # Check registers in LHS are compatible with RHS
        self._check_compatible_regs(rhs)

        # Make new circuit with combined registers
        combined_qregs = deepcopy(self.qregs)
        combined_cregs = deepcopy(self.cregs)

        for element in rhs.qregs:
            if element not in self.qregs:
                combined_qregs.append(element)
        for element in rhs.cregs:
            if element not in self.cregs:
                combined_cregs.append(element)
        circuit = QuantumCircuit(*combined_qregs, *combined_cregs)
        for gate in itertools.chain(self.data, rhs.data):
            gate.reapply(circuit)
        return circuit

    def extend(self, rhs):
        """
        Append rhs to self if self contains compatible registers.

        Two circuits are compatible if they contain the same registers
        or if they contain different registers with unique names. The
        returned circuit will contain all unique registers between both
        circuits.

        Modify and return self.
        """
        # Check registers in LHS are compatible with RHS
        self._check_compatible_regs(rhs)

        # Add new registers
        for element in rhs.qregs:
            if element not in self.qregs:
                self.qregs.append(element)
        for element in rhs.cregs:
            if element not in self.cregs:
                self.cregs.append(element)

        # Add new gates
        for gate in rhs.data:
            gate.reapply(self)
        return self

    def __add__(self, rhs):
        """Overload + to implement self.combine."""
        return self.combine(rhs)

    def __iadd__(self, rhs):
        """Overload += to implement self.extend."""
        return self.extend(rhs)

    def __len__(self):
        """Return number of operations in circuit."""
        return len(self.data)

    def __getitem__(self, item):
        """Return indexed operation."""
        return self.data[item]

    def _attach(self, instruction):
        """Attach an instruction."""
        self.data.append(instruction)
        return instruction

    def add_register(self, *regs):
        """Add registers."""
        for register in regs:
            if register in self.qregs or register in self.cregs:
                raise QiskitError("register name \"%s\" already exists"
                                  % register.name)
            if isinstance(register, QuantumRegister):
                self.qregs.append(register)
            elif isinstance(register, ClassicalRegister):
                self.cregs.append(register)
            else:
                raise QiskitError("expected a register")

    def add(self, *regs):
        """Add registers."""

        warnings.warn('The add() function is deprecated and will be '
                      'removed in a future release. Instead use '
                      'QuantumCircuit.add_register().', DeprecationWarning)
        self.add_register(*regs)

    def _check_qreg(self, register):
        """Raise exception if r is not in this circuit or not qreg."""
        if not isinstance(register, QuantumRegister):
            raise QiskitError("expected quantum register")
        if not self.has_register(register):
            raise QiskitError(
                "register '%s' not in this circuit" %
                register.name)

    def _check_qubit(self, qubit):
        """Raise exception if qubit is not in this circuit or bad format."""
        if not isinstance(qubit, tuple):
            raise QiskitError("%s is not a tuple."
                              "A qubit should be formated as a tuple." % str(qubit))
        if not len(qubit) == 2:
            raise QiskitError("%s is not a tuple with two elements, but %i instead" % len(qubit))
        if not isinstance(qubit[1], int):
            raise QiskitError("The second element of a tuple defining a qubit should be an int:"
                              "%s was found instead" % type(qubit[1]).__name__)
        self._check_qreg(qubit[0])
        qubit[0].check_range(qubit[1])

    def _check_creg(self, register):
        """Raise exception if r is not in this circuit or not creg."""
        if not isinstance(register, ClassicalRegister):
            raise QiskitError("Expected ClassicalRegister, but %s given" % type(register))
        if not self.has_register(register):
            raise QiskitError(
                "register '%s' not in this circuit" %
                register.name)

    def _check_dups(self, qubits):
        """Raise exception if list of qubits contains duplicates."""
        squbits = set(qubits)
        if len(squbits) != len(qubits):
            raise QiskitError("duplicate qubit arguments")

    def _check_compatible_regs(self, rhs):
        """Raise exception if the circuits are defined on incompatible registers"""

        list1 = self.qregs + self.cregs
        list2 = rhs.qregs + rhs.cregs
        for element1 in list1:
            for element2 in list2:
                if element2.name == element1.name:
                    if element1 != element2:
                        raise QiskitError("circuits are not compatible")

    def _gate_string(self, name):
        """Return a QASM string for the named gate."""
        out = ""
        if self.definitions[name]["opaque"]:
            out = "opaque " + name
        else:
            out = "gate " + name
        if self.definitions[name]["n_args"] > 0:
            out += "(" + ",".join(self.definitions[name]["args"]) + ")"
        out += " " + ",".join(self.definitions[name]["bits"])
        if self.definitions[name]["opaque"]:
            out += ";"
        else:
            out += "\n{\n" + self.definitions[name]["body"].qasm() + "}\n"
        return out

    def qasm(self):
        """Return OPENQASM string."""
        string_temp = self.header + "\n"
        for gate_name in self.definitions:
            if self.definitions[gate_name]["print"]:
                string_temp += self._gate_string(gate_name)
        for register in self.qregs:
            string_temp += register.qasm() + "\n"
        for register in self.cregs:
            string_temp += register.qasm() + "\n"
        for instruction in self.data:
            string_temp += instruction.qasm() + "\n"
        return string_temp

    def draw(self, scale=0.7, filename=None, style=None, output='text',
             interactive=False, line_length=None, plot_barriers=True,
             reverse_bits=False):
        """Draw the quantum circuit

        Using the output parameter you can specify the format. The choices are:
        0. text: ASCII art string
        1. latex: high-quality images, but heavy external software dependencies
        2. matplotlib: purely in Python with no external dependencies

        Defaults to an overcomplete basis, in order to not alter gates.

        Args:
            scale (float): scale of image to draw (shrink if < 1)
            filename (str): file path to save image to
            style (dict or str): dictionary of style or file name of style
                file. You can refer to the
                :ref:`Style Dict Doc <style-dict-doc>` for more information
                on the contents.
            output (str): Select the output method to use for drawing the
                circuit. Valid choices are `text`, `latex`, `latex_source`,
                `mpl`.
            interactive (bool): when set true show the circuit in a new window
                (for `mpl` this depends on the matplotlib backend being used
                supporting this). Note when used with either the `text` or the
                `latex_source` output type this has no effect and will be
                silently ignored.
            line_length (int): sets the length of the lines generated by `text`
            reverse_bits (bool): When set to True reverse the bit order inside
                registers for the output visualization.
            plot_barriers (bool): Enable/disable drawing barriers in the output
                circuit. Defaults to True.

        Returns:
            PIL.Image or matplotlib.figure or str or TextDrawing:
                * PIL.Image: (output `latex`) an in-memory representation of the
                  image of the circuit diagram.
                * matplotlib.figure: (output `mpl`) a matplotlib figure object
                  for the circuit diagram.
                * str: (output `latex_source`). The LaTeX source code.
                * TextDrawing: (output `text`). A drawing that can be printed as
                  ascii art

        Raises:
            VisualizationError: when an invalid output method is selected
        """
        from qiskit.tools import visualization
        return visualization.circuit_drawer(self, scale=scale,
                                            filename=filename, style=style,
                                            output=output,
                                            interactive=interactive,
                                            line_length=line_length,
                                            plot_barriers=plot_barriers,
                                            reverse_bits=reverse_bits)

    def size(self):
        """Return total number of operations in circuit."""
        # TODO: removed the DAG from this function
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.size()

    def depth(self):
        """Return circuit depth (i.e. length of critical path)."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.depth()

    def width(self):
        """Return number of qubits in circuit."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.width()

    def count_ops(self):
        """Count each operation kind in the circuit.

        Returns:
            dict: a breakdown of how many operations of each kind.
        """
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.count_ops()

    def num_tensor_factors(self):
        """How many non-entangled subcircuits can the circuit be factored to."""
        from qiskit.converters import circuit_to_dag
        dag = circuit_to_dag(self)
        return dag.num_tensor_factors()

    @staticmethod
    def from_qasm_file(path):
        """Take in a QASM file and generate a QuantumCircuit object.

        Args:
          path (str): Path to the file for a QASM program
        Return:
          QuantumCircuit: The QuantumCircuit object for the input QASM
        """
        qasm = _qasm.Qasm(filename=path)
        return _circuit_from_qasm(qasm)

    @staticmethod
    def from_qasm_str(qasm_str):
        """Take in a QASM string and generate a QuantumCircuit object.

        Args:
          qasm_str (str): A QASM program string
        Return:
          QuantumCircuit: The QuantumCircuit object for the input QASM
        """
        qasm = _qasm.Qasm(data=qasm_str)
        return _circuit_from_qasm(qasm)

def _circuit_from_qasm(qasm):
    # pylint: disable=cyclic-import
    from qiskit.converters import ast_to_dag
    from qiskit.converters import dag_to_circuit
    ast = qasm.parse()
    dag = ast_to_dag(ast)
    return dag_to_circuit(dag)
<|endoftext|>


<commit_before>#!/usr/bin/python3

######### Imports #######
import string
import sys

######### Type definition #######
# A matrix is an array of arrays. More precisely, it
# is an array of lines

######### Utilities #########
def is_zero(n):
    return abs(n) < 1e-10

# Returns the index of the first non-zero element
# or len(l) if the array is full of zeros
def first_nonzero(l):
    for i in range(len(l)):
        if not is_zero(l[i]):
            return i
    return len(l)

######### LaTeX output ########
# The latex file :
latex_file = False

# Open the path file and start writing a solution
# in it.
def latex_begin(coeffs, path):
    global latex_file
    latex_file = open(path, "wt")
    if not latex_file:
        return False
    latex_file.write("\\begin{figure}\n")
    latex_file.write("\\begin{math}\n")
    latex_matrix(coeffs)
    latex_file.write("\\\\")
    return True

# End the latex presentation and close the file.
def latex_end(sol):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\end{math}\n")
    if not sol:
        latex_file.write("There is no solution.\n")
    else:
        latex_file.write("The solutions are : ")
        # TODO

    latex_file.write("\\caption{Solving a linear system}\n")
    latex_file.write("\\end{figure}\n")
    latex_file.close()
    latex_file = None

# Add an intermediary step to the latex file
def latex_step(m):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\Leftrightarrow")
    latex_matrix(m)
    latex_file.write("\\\\\n")

# Output a matrix in latex format to the latex file.
def latex_matrix(m):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\left[\\begin{array}{")
    latex_file.write("c" * len(m[0]))
    latex_file.write("} ")
    for j in range(len(m)):
        for i in range(len(m[j]) - 2):
            latex_file.write("{} & ".format(m[j][i]))
        latex_file.write("{} \\\\ ".format(m[j][len(m) - 1]))
    latex_file.write("\\end{array}\\right]")

    latex_file.write(" = \\left[\\begin{array}{c} ")
    for j in range(len(m)):
        latex_file.write("{} \\\\".format(m[j][-1]))
    latex_file.write(" \\end{array}\\right]")
    return None

######### IO #########
# Print results
def output_results(m):
    for i in range(len(m)):
        print("|", end="")
        print(repr(m[i][0]).rjust(6), end="")
        print(" |", end="")
        if len(m[i]) == 1:
            print("")
            continue
        if i == int(len(m)/2):
            print(" + Vect ", end="")
        else:
            print("        ", end="")
        print("|", end="")
        for j in range(1, len(m[i])):
            print(repr(m[i][j]).rjust(6), end=" ")
        print("|")

# Reads a matrix from a file : returns the matrix and a boolean indicating the success
def read_matrix(path):
    f = open(path, 'r')
    m = []
    if not f:
        return m, False
    x = -1
    for line in f:
        if len(line) == 0 or line[0] == '#':
            continue
        row = [float(s.strip()) for s in line.split()]
        if x < 0:
            x = len(row)
        elif len(row) != x:
            return m, False
        m.append(row)
    return m, True

def output_matrix(m):
    for i in range(len(m)):
        print("| ", end="")
        for j in range(len(m[i])):
            print(repr(m[i][j]).rjust(6), end=" ")
        print("|")

######### Matrix manipulation ########
# Swaps the contents of the i1 and i2 lines
def matrix_swap(m, i1, i2):
    if len(m) == 0 or len(m) <= max(i1, i2):
        return
    for i in range(len(m[0])):
        m[i1][i], m[i2][i] = m[i2][i], m[i1][i]

# Add n times the contents of the l2 line to the l1 one
def matrix_add(m, l1, n, l2):
    for i in range(len(m[0])):
        m[l1][i] += n*m[l2][i]

# Multiply each element of the l line by n
def matrix_mult(m, l, n):
    for i in range(len(m[l])):
        m[l][i] *= n

######### Gauss transformation ########
# Remove the x-eme element in lines ]y,end[
# Returns false if it was already removed
def gauss_remove(m, x, y):
    piv = None
    for j in range(y, len(m)):
        if is_zero(m[j][x]):
            continue
        if not piv:
            piv = m[j][x]
            if j != y:
                matrix_swap(m, j, y)
            continue
        matrix_add(m, j, -m[j][x]/piv, y)
    if piv:
        return True
    else:
        return False

# Triangularize the matrix with p coeffs using the
# gauss method
# Returns the list of the complementary vars
def gauss_trian(m, p):
    j = 0
    comp = []
    for i in range(p):
        if gauss_remove(m, i, j):
            j += 1
            latex_step(m)
        else:
            comp += [i]
    return comp

# Create a base for p variables, comp being the array
# of the complementary variables
def gauss_base(p, comp):
    ret = []
    for l in range(p):
        ret.append((len(comp) + 1) * [0.0])
    for i in range(len(comp)):
        ret[comp[i]][i+1] = 1.0
    return ret

# Solve a triangularized linear system, writing the solutions
# to base, p being the number of variables.
# Returns False if there are no solutions.
def gauss_solve(m, base, p):
    for i in range(len(m) - 1, -1, -1):
        z = first_nonzero(m[i])
        if z > p:
            continue
        elif z == p:
            return False
        base[z][0] = m[i][p]
        for j in range(z+1, p):
            matrix_add(base, z, -m[i][j], j)
        matrix_mult(base, z, 1.0/m[i][z])
    return True

######### Main loop ##########
if __name__ == "__main__":
    coeffs, b = read_matrix(sys.argv[1])
    if not b:
        print("Invalid file :", sys.argv[1])
        exit()
    p = len(coeffs[0]) - 1

    print("Solving :")
    output_matrix(coeffs)
    latex_begin(coeffs, "latex.matrix")
    print("Triangularized :")
    comp = gauss_trian(coeffs, p)
    base = gauss_base(p, comp)
    output_matrix(coeffs)
    if not gauss_solve(coeffs, base, p):
        print("No solution.")
        latex_end(None)
    else:
        print("Solution : ")
        output_results(base)
        latex_end(base)

<commit_msg>Improved LaTeX output of the gauss script.<commit_after>#!/usr/bin/python3

######### Imports #######
import string
import sys

######### Type definition #######
# A matrix is an array of arrays. More precisely, it
# is an array of lines

######### Utilities #########
def is_zero(n):
    return abs(n) < 1e-10

# Returns the index of the first non-zero element
# or len(l) if the array is full of zeros
def first_nonzero(l):
    for i in range(len(l)):
        if not is_zero(l[i]):
            return i
    return len(l)

######### LaTeX output ########
# The latex file :
latex_file = False

# Open the path file and start writing a solution
# in it.
def latex_begin(coeffs, path):
    global latex_file
    latex_file = open(path, "wt")
    if not latex_file:
        return False
    latex_file.write("\\begin{figure}\n")
    latex_file.write("\\begin{math}\n")
    latex_matrix(coeffs)
    latex_file.write("\\\\")
    return True

# End the latex presentation and close the file.
def latex_end(sol):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\end{math}\n")
    if not sol:
        latex_file.write("There is no solution.\n")
    else:
        latex_file.write("The solutions are : ")
        # TODO

    latex_file.write("\\caption{Solving a linear system}\n")
    latex_file.write("\\end{figure}\n")
    latex_file.close()
    latex_file = None

# Add an intermediary step to the latex file
def latex_step(m):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\Leftrightarrow")
    latex_matrix(m)
    latex_file.write("\\\\\n")

# Output a matrix in latex format to the latex file.
def latex_matrix(m):
    global latex_file
    if not latex_file:
        return
    latex_file.write("\\left|\\begin{array}{")
    latex_file.write("c" * (2*len(m[0]) - 1))
    latex_file.write("} ")
    for j in range(len(m)):
        for i in range(len(m[j]) - 2):
            if is_zero(m[j][i]):
                latex_file.write(" & & ")
            else:
                latex_file.write("{}x_{} & + & ".format(m[j][i], i)) # TODO handle indices >= 10
        if is_zero(m[j][-2]):
            latex_file.write(" & = & {} \\\\\n".format(m[j][-2], len(m[j]) - 2, m[j][-1]))
        else:
            latex_file.write("{}x_{} & = & {} \\\\\n".format(m[j][-2], len(m[j]) - 2, m[j][-1]))
    latex_file.write("\\end{array}\\right.")
    return None

######### IO #########
# Print results
def output_results(m):
    for i in range(len(m)):
        print("|", end="")
        print(repr(m[i][0]).rjust(6), end="")
        print(" |", end="")
        if len(m[i]) == 1:
            print("")
            continue
        if i == int(len(m)/2):
            print(" + Vect ", end="")
        else:
            print("        ", end="")
        print("|", end="")
        for j in range(1, len(m[i])):
            print(repr(m[i][j]).rjust(6), end=" ")
        print("|")

# Reads a matrix from a file : returns the matrix and a boolean indicating the success
def read_matrix(path):
    f = open(path, 'r')
    m = []
    if not f:
        return m, False
    x = -1
    for line in f:
        if len(line) == 0 or line[0] == '#':
            continue
        row = [float(s.strip()) for s in line.split()]
        if x < 0:
            x = len(row)
        elif len(row) != x:
            return m, False
        m.append(row)
    return m, True

def output_matrix(m):
    for i in range(len(m)):
        print("| ", end="")
        for j in range(len(m[i])):
            print(repr(m[i][j]).rjust(6), end=" ")
        print("|")

######### Matrix manipulation ########
# Swaps the contents of the i1 and i2 lines
def matrix_swap(m, i1, i2):
    if len(m) == 0 or len(m) <= max(i1, i2):
        return
    for i in range(len(m[0])):
        m[i1][i], m[i2][i] = m[i2][i], m[i1][i]

# Add n times the contents of the l2 line to the l1 one
def matrix_add(m, l1, n, l2):
    for i in range(len(m[0])):
        m[l1][i] += n*m[l2][i]

# Multiply each element of the l line by n
def matrix_mult(m, l, n):
    for i in range(len(m[l])):
        m[l][i] *= n

######### Gauss transformation ########
# Remove the x-eme element in lines ]y,end[
# Returns false if it was already removed
def gauss_remove(m, x, y):
    piv = None
    for j in range(y, len(m)):
        if is_zero(m[j][x]):
            continue
        if not piv:
            piv = m[j][x]
            if j != y:
                matrix_swap(m, j, y)
            continue
        matrix_add(m, j, -m[j][x]/piv, y)
    if piv:
        return True
    else:
        return False

# Triangularize the matrix with p coeffs using the
# gauss method
# Returns the list of the complementary vars
def gauss_trian(m, p):
    j = 0
    comp = []
    for i in range(p):
        if gauss_remove(m, i, j):
            j += 1
            latex_step(m)
        else:
            comp += [i]
    return comp

# Create a base for p variables, comp being the array
# of the complementary variables
def gauss_base(p, comp):
    ret = []
    for l in range(p):
        ret.append((len(comp) + 1) * [0.0])
    for i in range(len(comp)):
        ret[comp[i]][i+1] = 1.0
    return ret

# Solve a triangularized linear system, writing the solutions
# to base, p being the number of variables.
# Returns False if there are no solutions.
def gauss_solve(m, base, p):
    for i in range(len(m) - 1, -1, -1):
        z = first_nonzero(m[i])
        if z > p:
            continue
        elif z == p:
            return False
        base[z][0] = m[i][p]
        for j in range(z+1, p):
            matrix_add(base, z, -m[i][j], j)
        matrix_mult(base, z, 1.0/m[i][z])
    return True

######### Main loop ##########
if __name__ == "__main__":
    coeffs, b = read_matrix(sys.argv[1])
    if not b:
        print("Invalid file :", sys.argv[1])
        exit()
    p = len(coeffs[0]) - 1

    print("Solving :")
    output_matrix(coeffs)
    latex_begin(coeffs, "latex.matrix")
    print("Triangularized :")
    comp = gauss_trian(coeffs, p)
    base = gauss_base(p, comp)
    output_matrix(coeffs)
    if not gauss_solve(coeffs, base, p):
        print("No solution.")
        latex_end(None)
    else:
        print("Solution : ")
        output_results(base)
        latex_end(base)

<|endoftext|>


<commit_before>from __future__ import print_function

from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.models import (
    Plot, Range1d, WMTSTileSource, ColumnDataSource, WheelZoomTool,
    ResizeTool, PanTool, BoxZoomTool, HoverTool, Circle
)
from bokeh.plotting import output_file
from bokeh.resources import INLINE
from bokeh.sampledata.airports import data as airports
from bokeh.util.browser import view

title = "US Airports: Field Elevation > 1500m"
output_file("airports_map.html", title=title)

points_source = ColumnDataSource(airports)

# create tile source
tile_options = {}
tile_options['url'] = 'http://otile2.mqcdn.com/tiles/1.0.0/sat/{Z}/{X}/{Y}.png'
tile_source = WMTSTileSource(**tile_options)

# set to roughly extent of points
x_range = Range1d(start=airports['x'].min() - 10000, end=airports['x'].max() + 10000)
y_range = Range1d(start=airports['y'].min() - 10000, end=airports['y'].max() + 10000)

# create plot and add tools
hover_tool = HoverTool(tooltips=[("Name", "@name"), ("Elevation", "@elevation (m)")])
p = Plot(x_range=x_range, y_range=y_range, plot_height=690, plot_width=990, title=title)
p.add_tools(ResizeTool(), WheelZoomTool(), PanTool(), BoxZoomTool(), hover_tool)
p.add_tile(tile_source)

# create point glyphs
point_options = {}
point_options['x'] = 'x'
point_options['y'] = 'y'
point_options['size'] = 9
point_options['fill_color'] = "#60ACA1"
point_options['line_color'] = "#D2C4C1"
point_options['line_width'] = 1.5
points_glyph = Circle(**point_options)
p.add_glyph(points_source, points_glyph)

doc = Document()
doc.add_root(p)

if __name__ == "__main__":
    filename = "airports_map.html"
    with open(filename, "w") as f:
        f.write(file_html(doc, INLINE, "Bokeh Airports Example"))
    print("Wrote %s" % filename)
    view(filename)
<commit_msg>fixed panning/zooming and refactored to use figure instead of plot object for simplier code example<commit_after>from __future__ import print_function

from bokeh.models import (
    Range1d, WMTSTileSource, ColumnDataSource, HoverTool, Circle
)
from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.airports import data as airports

title = "US Airports: Field Elevation > 1500m"
output_file("airports_map.html", title=title)

points_source = ColumnDataSource(airports)

# create tile source
tile_options = {}
tile_options['url'] = 'http://otile2.mqcdn.com/tiles/1.0.0/sat/{Z}/{X}/{Y}.png'
tile_source = WMTSTileSource(**tile_options)

# set to roughly extent of points
x_range = Range1d(start=airports['x'].min() - 10000, end=airports['x'].max() + 10000, bounds=None)
y_range = Range1d(start=airports['y'].min() - 10000, end=airports['y'].max() + 10000, bounds=None)

# create plot and add tools
p = figure(tools='wheel_zoom,pan', x_range=x_range, y_range=y_range, title=title)
p.axis.visible = False
hover_tool = HoverTool(tooltips=[("Name", "@name"), ("Elevation", "@elevation (m)")])
p.add_tools(hover_tool)
p.add_tile(tile_source)

# create point glyphs
point_options = {}
point_options['x'] = 'x'
point_options['y'] = 'y'
point_options['size'] = 9
point_options['fill_color'] = "#60ACA1"
point_options['line_color'] = "#D2C4C1"
point_options['line_width'] = 1.5
points_glyph = Circle(**point_options)
p.add_glyph(points_source, points_glyph)

show(p)
<|endoftext|>


<commit_before>__version__ = '0.1'
<commit_msg>Bump version for development.<commit_after>__version__ = '0.2.dev0'
<|endoftext|>


<commit_before>import io
import shutil
import tempfile

import pyexiv2
from bidict import bidict

from madam.core import MetadataProcessor, UnsupportedFormatError

class Exiv2MetadataProcessor(MetadataProcessor):
    """
    Represents a metadata processor using the exiv2 library.
    """
    __metadata_key_to_exiv2_key = bidict({
        # Exif
        'image.artist': 'Exif.Image.Artist',
        # IPTC
        'bylines': 'Iptc.Application2.Byline',
        'byline_titles': 'Iptc.Application2.BylineTitle',
        'caption': 'Iptc.Application2.Caption',
        'contacts': 'Iptc.Application2.Contact',
        'copyright': 'Iptc.Application2.Copyright',
        'creation_date': 'Iptc.Application2.DateCreated',
        'creation_time': 'Iptc.Application2.TimeCreated',
        'credit': 'Iptc.Application2.Credit',
        'expiration_date': 'Iptc.Application2.ExpirationDate',
        'expiration_time': 'Iptc.Application2.ExpirationTime',
        'headline': 'Iptc.Application2.Headline',
        'image_orientation': 'Iptc.Application2.ImageOrientation',
        'keywords': 'Iptc.Application2.Keywords',
        'language': 'Iptc.Application2.Language',
        'release_date': 'Iptc.Application2.ReleaseDate',
        'release_time': 'Iptc.Application2.ReleaseTime',
        'source': 'Iptc.Application2.Source',
        'subjects': 'Iptc.Application2.Subject',
    })

    @property
    def formats(self):
        return 'exif', 'iptc'

    def read(self, file):
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(file.read())
            tmp.flush()
            metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown file format.')
        metadata_by_format = {}
        for format in self.formats:
            format_metadata = {}
            for key in getattr(metadata, format + '_keys'):
                madam_key = Exiv2MetadataProcessor.__metadata_key_to_exiv2_key.inv[key]
                format_metadata[madam_key] = metadata[key].value
            if format_metadata:
                metadata_by_format[format] = format_metadata
        return metadata_by_format

    def strip(self, file):
        result = io.BytesIO()
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(file.read())
            tmp.flush()
            metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown file format.')
            metadata.clear()
            metadata.write()
            tmp.seek(0)

            shutil.copyfileobj(tmp, result)
            result.seek(0)

        return result

    def combine(self, essence, metadata_by_type):
        result = io.BytesIO()
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(essence.read())
            tmp.flush()
            exiv2_metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                exiv2_metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown essence format.')
            for metadata_type, metadata in metadata_by_type.items():
                for key in metadata:
                    try:
                        exiv2_key = Exiv2MetadataProcessor.__metadata_key_to_exiv2_key[key]
                        value_list = metadata[key] if isinstance(metadata[key], list) else [metadata[key]]
                        exiv2_metadata[exiv2_key] = value_list
                    except KeyError:
                        raise UnsupportedFormatError('Invalid metadata to be combined with essence: %s=%s' %
                                                     (metadata_type, metadata))
            exiv2_metadata.write()
            tmp.flush()
            tmp.seek(0)

            shutil.copyfileobj(tmp, result)
            result.seek(0)

        return result
<commit_msg>Exiv2MetadataProcessor fails if metadata can not be written.<commit_after>import io
import shutil
import tempfile

import pyexiv2
from bidict import bidict

from madam.core import MetadataProcessor, UnsupportedFormatError

class Exiv2MetadataProcessor(MetadataProcessor):
    """
    Represents a metadata processor using the exiv2 library.
    """
    __metadata_key_to_exiv2_key = bidict({
        # Exif
        'image.artist': 'Exif.Image.Artist',
        # IPTC
        'bylines': 'Iptc.Application2.Byline',
        'byline_titles': 'Iptc.Application2.BylineTitle',
        'caption': 'Iptc.Application2.Caption',
        'contacts': 'Iptc.Application2.Contact',
        'copyright': 'Iptc.Application2.Copyright',
        'creation_date': 'Iptc.Application2.DateCreated',
        'creation_time': 'Iptc.Application2.TimeCreated',
        'credit': 'Iptc.Application2.Credit',
        'expiration_date': 'Iptc.Application2.ExpirationDate',
        'expiration_time': 'Iptc.Application2.ExpirationTime',
        'headline': 'Iptc.Application2.Headline',
        'image_orientation': 'Iptc.Application2.ImageOrientation',
        'keywords': 'Iptc.Application2.Keywords',
        'language': 'Iptc.Application2.Language',
        'release_date': 'Iptc.Application2.ReleaseDate',
        'release_time': 'Iptc.Application2.ReleaseTime',
        'source': 'Iptc.Application2.Source',
        'subjects': 'Iptc.Application2.Subject',
    })

    @property
    def formats(self):
        return 'exif', 'iptc'

    def read(self, file):
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(file.read())
            tmp.flush()
            metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown file format.')
        metadata_by_format = {}
        for format in self.formats:
            format_metadata = {}
            for key in getattr(metadata, format + '_keys'):
                madam_key = Exiv2MetadataProcessor.__metadata_key_to_exiv2_key.inv[key]
                format_metadata[madam_key] = metadata[key].value
            if format_metadata:
                metadata_by_format[format] = format_metadata
        return metadata_by_format

    def strip(self, file):
        result = io.BytesIO()
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(file.read())
            tmp.flush()
            metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown file format.')
            metadata.clear()
            try:
                metadata.write()
            except OSError:
                raise UnsupportedFormatError('Unknown file format.')
            tmp.seek(0)

            shutil.copyfileobj(tmp, result)
            result.seek(0)

        return result

    def combine(self, essence, metadata_by_type):
        result = io.BytesIO()
        with tempfile.NamedTemporaryFile() as tmp:
            tmp.write(essence.read())
            tmp.flush()
            exiv2_metadata = pyexiv2.ImageMetadata(tmp.name)
            try:
                exiv2_metadata.read()
            except OSError:
                raise UnsupportedFormatError('Unknown essence format.')
            for metadata_type, metadata in metadata_by_type.items():
                for key in metadata:
                    try:
                        exiv2_key = Exiv2MetadataProcessor.__metadata_key_to_exiv2_key[key]
                        value_list = metadata[key] if isinstance(metadata[key], list) else [metadata[key]]
                        exiv2_metadata[exiv2_key] = value_list
                    except KeyError:
                        raise UnsupportedFormatError('Invalid metadata to be combined with essence: %s=%s' %
                                                     (metadata_type, metadata))
            exiv2_metadata.write()
            tmp.flush()
            tmp.seek(0)

            shutil.copyfileobj(tmp, result)
            result.seek(0)

        return result
<|endoftext|>


<commit_before># -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from qiita_client import QiitaCommand

from .kneaddata import kneaddata

__all__ = ['kneaddata']

# Define the kneaddata command
req_params = {'input': ('artifact', ['per_sample_FASTQ'])}
opt_params = {
    # there are other parameters not included that will be ignored in this
    # configuration as we assume that the correct values were set in the env
    # by the admin installing the tools:
    # trimmomatic
    # bowtie2
    # --input # input FASTQ file (add a second argument for paired input files)
    # --output # directory to write output files
    # --output-prefix # prefix for output files [ DEFAULT : $SAMPLE_kneaddata ]
    # --log # filepath for log [ DEFAULT : $OUTPUT_DIR/$SAMPLE_kneaddata.log ]
    # --bowtie2 # path to bowtie executable
    # --bmtagger # path to bmtagger exectuable
    # --trf # path to TRF executable
    # --store-temp-output # store temp output files
    # --trimmomatic # the location of the trimmomatic (automatically
    #               # installed by pip install kneaddata)
    # --log-level
    'reference-db': ['choice:["default"]', 'default'],  # ref db
    'bypass-trim': ['boolean', 'False'],  # bypass the trim step
    'threads': ['integer', '1'],  # threads to run
    'processes': ['integer', '1'],  # processes to run
    'quality-scores': ['choice:["phred33", "phred64"]', 'phred33'],  # quality
    'run-bmtagger': ['boolean', 'False'],  # run BMTagger instead of Bowtie2
    'run-trf': ['boolean', 'False'],  # run TRF repeat finder tool
    'run-fastqc-start': ['boolean', 'True'],  # run FastQC on original data
    'run-fastqc-end': ['boolean', 'False'],  # run FastQC on filtered data
    # Trimmomatic options
    'max-memory': ['string', '500m'],  # max memory in mb [ DEFAULT : 500m ]
    # leaving as empty string to simply. note that in tests is not empty.
    'trimmomatic-options': ['string', ''],

    # Bowtie2 options
    # 'bowtie2-options': ['string', '--very-sensitive']
    # BMTagger options
    # # TRF options
    # 'match': ['integer', '2'], # matching weight
    # 'mismatch': ['integer', '7'], # mismatching penalty
    # 'delta': ['integer', '7'], # indel penalty
    # 'pm': ['integer', '80'], # match probability
    # 'pi': ['integer', '10'], # indel probability
    # 'minscore': ['integer', '50'], # mimimum alignment score to report
    # 'maxperiod': ['integer', '500m'] # maximum period size to report
    # FastQC options
    }
outputs = {
    'KneadData clean paired': 'per_sample_FASTQ',
    'KneadData unmatched R1': 'per_sample_FASTQ',
    'KneadData unmatched R2': 'per_sample_FASTQ',
    }
dflt_param_set = {
    'Defaults': {
        'reference-db': 'default', 'bypass-trim': False, 'threads': 1,
        'processes': 1, 'quality-scores': 'phred33', 'run-bmtagger': False,
        'run-trf': False, 'run-fastqc-start': True, 'run-fastqc-end': False,
        'max-memory': '500m', 'trimmomatic-options': ''
        }
}

kneaddata_cmd = QiitaCommand(
    'KneadData 0.5.1', "Sequence QC - Human sequence removal", kneaddata,
    req_params, opt_params, outputs, dflt_param_set)
<commit_msg>clean unmatched<commit_after># -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from qiita_client import QiitaCommand

from .kneaddata import kneaddata

__all__ = ['kneaddata']

# Define the kneaddata command
req_params = {'input': ('artifact', ['per_sample_FASTQ'])}
opt_params = {
    # there are other parameters not included that will be ignored in this
    # configuration as we assume that the correct values were set in the env
    # by the admin installing the tools:
    # trimmomatic
    # bowtie2
    # --input # input FASTQ file (add a second argument for paired input files)
    # --output # directory to write output files
    # --output-prefix # prefix for output files [ DEFAULT : $SAMPLE_kneaddata ]
    # --log # filepath for log [ DEFAULT : $OUTPUT_DIR/$SAMPLE_kneaddata.log ]
    # --bowtie2 # path to bowtie executable
    # --bmtagger # path to bmtagger exectuable
    # --trf # path to TRF executable
    # --store-temp-output # store temp output files
    # --trimmomatic # the location of the trimmomatic (automatically
    #               # installed by pip install kneaddata)
    # --log-level
    'reference-db': ['choice:["default"]', 'default'],  # ref db
    'bypass-trim': ['boolean', 'False'],  # bypass the trim step
    'threads': ['integer', '1'],  # threads to run
    'processes': ['integer', '1'],  # processes to run
    'quality-scores': ['choice:["phred33", "phred64"]', 'phred33'],  # quality
    'run-bmtagger': ['boolean', 'False'],  # run BMTagger instead of Bowtie2
    'run-trf': ['boolean', 'False'],  # run TRF repeat finder tool
    'run-fastqc-start': ['boolean', 'True'],  # run FastQC on original data
    'run-fastqc-end': ['boolean', 'False'],  # run FastQC on filtered data
    # Trimmomatic options
    'max-memory': ['string', '500m'],  # max memory in mb [ DEFAULT : 500m ]
    # leaving as empty string to simply. note that in tests is not empty.
    'trimmomatic-options': ['string', ''],

    # Bowtie2 options
    # 'bowtie2-options': ['string', '--very-sensitive']
    # BMTagger options
    # # TRF options
    # 'match': ['integer', '2'], # matching weight
    # 'mismatch': ['integer', '7'], # mismatching penalty
    # 'delta': ['integer', '7'], # indel penalty
    # 'pm': ['integer', '80'], # match probability
    # 'pi': ['integer', '10'], # indel probability
    # 'minscore': ['integer', '50'], # mimimum alignment score to report
    # 'maxperiod': ['integer', '500m'] # maximum period size to report
    # FastQC options
    }
outputs = {
    'KneadData clean paired': 'per_sample_FASTQ',
    'KneadData clean unmatched R1': 'per_sample_FASTQ',
    'KneadData clean unmatched R2': 'per_sample_FASTQ',
    }
dflt_param_set = {
    'Defaults': {
        'reference-db': 'default', 'bypass-trim': False, 'threads': 1,
        'processes': 1, 'quality-scores': 'phred33', 'run-bmtagger': False,
        'run-trf': False, 'run-fastqc-start': True, 'run-fastqc-end': False,
        'max-memory': '500m', 'trimmomatic-options': ''
        }
}

kneaddata_cmd = QiitaCommand(
    'KneadData 0.5.1', "Sequence QC - Human sequence removal", kneaddata,
    req_params, opt_params, outputs, dflt_param_set)
<|endoftext|>


<commit_before>import sys
import numpy as np
import re
import time
import json
import matplotlib.pyplot as plt
import math

from datetime import date

colors=['#D3132D', '#1527F5', '#1F76B4','#903864','#D61E28', '#C8C8C8', '#8C582D']

month = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6,'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}
reMonth = {v:k for k,v in month.items()}
name = {}

def monthsBetween(sdate, edate):
	if not (edate[2] == sdate[2]):
		return (12 - month[sdate[0]]+1) + month[edate[0]] + 12*(int(edate[2])-int(sdate[2])-1)
	else:
		return int(math.fabs(month[edate[0]] - month[sdate[0]]) + 1)

############
## Hourly ##
############
def hourlyG(hourlyC,names):
	row_sums = hourlyC.sum(axis=1)
	hourlyC = hourlyC/row_sums[:, np.newaxis]
	hour = np.arange(0,24)
	width = 0.35
	bars = []

	fig,ax = plt.subplots()

	for i in range(len(names)):
		b = ax.bar(hour+(i*width), hourlyC[i,:], width/(len(names)-1), color=colors[i], alpha=0.75)
		bars.append(b[0])

	ax.spines["top"].set_visible(False)
	ax.spines["right"].set_visible(False)
	ax.get_xaxis().tick_bottom()
	ax.get_yaxis().tick_left()

	ax.set_ylabel('Percentage of Messages sent')
	ax.set_xlabel('Hour o\' Day')

	ax.set_xticks(hour+width)
	h = np.array(hour, dtype=str)
	ax.set_xticklabels(h)

	ax.legend(tuple(bars),(n for n in names))
	plt.show()

#############
## Monthly ##
#############
def monthlyG(monthlyC, names):
	row_sums = monthlyC.sum(axis=1)
	monthlyC = monthlyC/row_sums[:, np.newaxis]
	mrange = np.arange(0,nmonths)
	width = 0.35

	fig,ax = plt.subplots()
	bars = []

	for i in range(len(names)):
		b = ax.bar(mrange+(i*width), monthlyC[i,:], width/(len(names)-1), color=colors[i], alpha=0.75)
		bars.append(b[0])

	ax.spines["top"].set_visible(False)
	ax.spines["right"].set_visible(False)
	ax.get_xaxis().tick_bottom()
	ax.get_yaxis().tick_left()

	ax.set_ylabel('Percentage of Messages sent')
	ax.set_xlabel('Month o\' Year')
	ax.set_xticks(mrange+width)

	em,ey,sm,sy = edate[0],edate[2],sdate[0],sdate[2]
	i = 1
	mlabels = ['' for x in range(nmonths)]
	mlabels[0] = sm+' \''+sy[-2:]

	if sm == 'Dec':
		sm = 'Jan'
		sy = str(int(sy)+1)
	else:
		sm = reMonth[month[sm]+1]

	while sm!=em or sy!=ey:
		if sm == 'Jan':
			mlabels[i] = sm+' \''+sy[-2:]
		else:
			mlabels[i] = sm

		if sm == 'Dec':
			sm = 'Jan'
			sy = str(int(sy)+1)
		else:
			sm = reMonth[month[sm]+1]
		i += 1

	if sm == 'Jan':
		mlabels[i] = sm+' \''+sy[-2:]
	else:
		mlabels[i] = sm

	m = np.array(mlabels, dtype=str)
	ax.set_xticklabels(m)

	ax.legend(tuple(bars),(n for n in names))
	plt.show()

##########################
### Message Extraction ###
##########################
term = sys.stdin
sys.stdin = open('./test files/mojo.txt')
# sys.stdout = open('b.out','w')
i = 0
msgs = []
text = sys.stdin.read()
# sys.stdin = term
inputs = re.split(r'\n(?=[\w]{3} [\d]+, [\d, ]*?[\d]+:[\d]+ [A|P]M)', text)
if len(inputs) == 1:
	inputs = re.split(r'\n(?=[\d]+:[\d]+[A|P]M, [\w]{3} [\d]+[, ]?[\d]*?)',text)
sys.stdin.close()

# format - [[date, time], person, message]
for line in inputs:
	try:
		dt = re.match(r'^.*M (?=-)', line).group()
		dt = ''.join(dt.split(',')).strip()
		dt = dt.split(' ')
	except:
		dt = re.match(r'^.*[\d]+[, ]?[\d]* (?=-)',line).group()

		dt = ''.join(dt.split(',')).strip()
		dt = dt.split(' ')

		dt = dt[1:] + [dt[0][:-2],dt[0][-2:]]

	if not len(dt) > 4:
		dt = dt[:2]+[time.strftime('%Y')]+dt[2:]

	dt = [' '.join(dt[:3]).strip(), ' '.join(dt[3:]).strip()]

	pe = re.search(r'(?<=- )[\w ]+(?=:)', line).group()

	me = re.search(r'(?<=: ).*', line, re.DOTALL).group()

	msgs +=[(dt, pe.strip(), me.strip())]

# Populate person list
pl = [msgs[0][1]]
for msg in msgs[1:]:
	if not msg[1] in pl:
		pl += [msg[1]]
	else:
		continue

# Populate name list
i = 0
for n in pl:
	name[n] = i
	i += 1

sdate,edate = msgs[0][0][0].split(' '),msgs[-1][0][0].split(' ')
nmonths = monthsBetween(sdate,edate)

hourlyC = np.zeros([len(pl),24], dtype=float)
monthlyC = np.zeros([len(pl),nmonths], dtype=float)
dailyC = [[] for _ in range(len(pl))]

currentDate = msgs[0][0][0]
i = 0

for msg in msgs:
	d = msg[0][0]
	ap = msg[0][1][-2:]
	h = int(re.match(r'\d+(?=\:)',msg[0][1]).group())
	sender = msg[1]
	off = monthsBetween(sdate,d.split(' ')) - 1

	if ap == 'AM':
		if h == 12: h = 0
	else:
		if not h == 12: h += 12

	if not d == currentDate:
		m0, d0, y0 = currentDate.split(' ')
		m0 = month[m0]
		d0 = int(d0)
		y0 = int(y0)

		m1, d1, y1 = d.split(' ')
		m1 = month[m1]
		d1 = int(d1)
		y1 = int(y1)

		diff = date(y1,m1,d1)-date(y0,m0,d0)
		i += diff.days
		currentDate = d

	hourlyC[name[sender]][h%24] += 1
	dailyC[name[sender]] += [i]
	monthlyC[name[sender]][off] += 1

hourlyG(hourlyC, pl)
monthlyG(monthlyC, pl)<commit_msg>better format handling<commit_after>import sys
import numpy as np
import re
import time
import json
import matplotlib.pyplot as plt
import math
import seaborn as sns

from datetime import date

colors=['#D3132D', '#1527F5', '#1F76B4','#903864','#D61E28', '#C8C8C8', '#8C582D']

month = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6,'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}
reMonth = {v:k for k,v in month.items()}
name = {}

def monthsBetween(sdate, edate):
	if not (edate[2] == sdate[2]):
		return (12 - month[sdate[0]]+1) + month[edate[0]] + 12*(int(edate[2])-int(sdate[2])-1)
	else:
		return int(math.fabs(month[edate[0]] - month[sdate[0]]) + 1)

############
## Hourly ##
############
def hourlyG(hourlyC,names):
	row_sums = hourlyC.sum(axis=1)
	hourlyC = hourlyC/row_sums[:, np.newaxis]
	hour = np.arange(0,24)
	width = 0.35
	bars = []

	fig,ax = plt.subplots()

	for i in range(len(names)):
		b = ax.bar(hour+(i*width), hourlyC[i,:], width/(len(names)-1), color=colors[i], alpha=0.75)
		bars.append(b[0])

	ax.spines["top"].set_visible(False)
	ax.spines["right"].set_visible(False)
	ax.get_xaxis().tick_bottom()
	ax.get_yaxis().tick_left()

	ax.set_ylabel('Percentage of Messages sent')
	ax.set_xlabel('Hour o\' Day')

	ax.set_xticks(hour+width)
	h = np.array(hour, dtype=str)
	ax.set_xticklabels(h)

	ax.legend(tuple(bars),(n for n in names), loc='best')
	plt.show()

#############
## Monthly ##
#############
def monthlyG(monthlyC, names):
	row_sums = monthlyC.sum(axis=1)
	monthlyC = monthlyC/row_sums[:, np.newaxis]
	mrange = np.arange(0,nmonths)
	width = 0.35
	bars = []

	fig,ax = plt.subplots()

	for i in range(len(names)):
		b = ax.bar(mrange+(i*width), monthlyC[i,:], width/(len(names)-1), color=colors[i], alpha=0.75)
		bars.append(b[0])

	ax.spines["top"].set_visible(False)
	ax.spines["right"].set_visible(False)
	ax.get_xaxis().tick_bottom()
	ax.get_yaxis().tick_left()

	ax.set_ylabel('Percentage of Messages sent')
	ax.set_xlabel('Month o\' Year')
	ax.set_xticks(mrange+width)

	em,ey,sm,sy = edate[0],edate[2],sdate[0],sdate[2]
	i = 1
	mlabels = ['' for x in range(nmonths)]
	mlabels[0] = sm+' \''+sy[-2:]

	if sm == 'Dec':
		sm = 'Jan'
		sy = str(int(sy)+1)
	else:
		sm = reMonth[month[sm]+1]

	while sm!=em or sy!=ey:
		if sm == 'Jan':
			mlabels[i] = sm+' \''+sy[-2:]
		else:
			mlabels[i] = sm

		if sm == 'Dec':
			sm = 'Jan'
			sy = str(int(sy)+1)
		else:
			sm = reMonth[month[sm]+1]
		i += 1

	if sm == 'Jan':
		mlabels[i] = sm+' \''+sy[-2:]
	else:
		mlabels[i] = sm

	m = np.array(mlabels, dtype=str)
	ax.set_xticklabels(m)

	ax.legend(tuple(bars),(n for n in names), loc='best')
	plt.show()

#############
### Daily ###
#############
def dailyG(dailyC):
	plt.figure()
	plt.hist([dailyC[0], dailyC[1]], bins=210,normed=1, histtype='stepfilled',color=colors[:len(pl)], cumulative=0, alpha=1)
	plt.show()

	dailyC = map(lambda x : np.array(x), dailyC)

	plt.figure()
	for n in pl:
		sns.kdeplot(dailyC[name[n]], bw=0.087, shade=True, kernel='tri', color=colors[pl.index(n)], label=n)

	plt.show()

##########################
### Message Extraction ###
##########################
term = sys.stdin
sys.stdin = open('./test files/mojo.txt')
# sys.stdout = open('b.out','w')
i = 0
msgs = []
text = sys.stdin.read()
# sys.stdin = term
inputs = re.split(r'\n(?=[\w]{3} [\d]+, [\d, ]*?[\d]+:[\d]+ [A|P]M)', text)
if len(inputs) == 1:
	inputs = re.split(r'\n(?=[\d]+:[\d]+[A|P]M, [\w]{3} [\d]+[, ]?[\d]*?)',text)

inputs = re.split(r'\n(?=[\w]{3} [\d]+, [\d, ]*?[\d]+:[\d]+ [A|P]M)', sys.stdin.read())
sys.stdin.close()

# format - [[date, time], person, message]
for line in inputs:
	try:
		dt = re.match(r'^.*M (?=-)', line).group()
		dt = ''.join(dt.split(',')).strip()
		dt = dt.split(' ')
	except:
		dt = re.match(r'^.*[\d]+[, ]?[\d]* (?=-)',line).group()

		dt = ''.join(dt.split(',')).strip()
		dt = dt.split(' ')

		dt = dt[1:] + [dt[0][:-2],dt[0][-2:]]

	if not len(dt) > 4:
		dt = dt[:2]+[time.strftime('%Y')]+dt[2:]

	dt = [' '.join(dt[:3]).strip(), ' '.join(dt[3:]).strip()]

	pe = re.search(r'(?<=- )[\w ]+(?=:)', line).group()

	me = re.search(r'(?<=: ).*', line, re.DOTALL).group()

	msgs +=[(dt, pe.strip(), me.strip())]

# Populate person list
pl = [msgs[0][1]]
for msg in msgs[1:]:
	if not msg[1] in pl:
		pl += [msg[1]]
	else:
		continue

# Populate name list
i = 0
for n in pl:
	name[n] = i
	i += 1

sdate,edate = msgs[0][0][0].split(' '),msgs[-1][0][0].split(' ')
nmonths = monthsBetween(sdate,edate)

hourlyC = np.zeros([len(pl),24], dtype=float)
monthlyC = np.zeros([len(pl),nmonths], dtype=float)
dailyC = [[] for _ in range(len(pl))]

currentDate = msgs[0][0][0]
i = 0

for msg in msgs:
	d = msg[0][0]
	ap = msg[0][1][-2:]
	h = int(re.match(r'\d+(?=\:)',msg[0][1]).group())
	sender = msg[1]
	off = monthsBetween(sdate,d.split(' ')) - 1

	if ap == 'AM':
		if h == 12: h = 0
	else:
		if not h == 12: h += 12

	if not d == currentDate:
		m0, d0, y0 = currentDate.split(' ')
		m0 = month[m0]
		d0 = int(d0)
		y0 = int(y0)

		m1, d1, y1 = d.split(' ')
		m1 = month[m1]
		d1 = int(d1)
		y1 = int(y1)

		diff = date(y1,m1,d1)-date(y0,m0,d0)
		i += diff.days
		currentDate = d

	hourlyC[name[sender]][h%24] += 1
	dailyC[name[sender]] += [i]
	monthlyC[name[sender]][off] += 1

hourlyG(hourlyC, pl)
monthlyG(monthlyC, pl)
monthlyG(monthlyC, pl)
dailyG(dailyC)<|endoftext|>