<commit_before>
'''
Generate a pbs script for job submission, submit the job, be happy
'''

import glob
import sys
import os
import shutil
import time

def return_template(output_direc, ms_name_1, ms_name_2, model_name,
                    mask_name, outname):

    # Emailing
    # PBS -m bea
    # PBS -M koch.eric.w@gmail.com

    template = \
        '''
#!/bin/bash

#PBS -S /bin/bash
#PBS -l pmem=1000m
#PBS -l nodes=1:ppn=12
#PBS -l walltime=7:00:00

source /home/ekoch/.bashrc

cd X1

echo "Starting at: `date`"
casa-4.4 --nologger --logfile X7 -c /home/ekoch/VLA_Lband/14B-088/HI/archival_combination/HI_single_channel_clean.py X2 X3 X4 X5 X6
echo "Exited with code $? at: `date`"
        '''

    template = template.strip()

    template = template.replace("X1", output_direc)
    template = template.replace("X2", ms_name_1)
    template = template.replace("X3", ms_name_2)
    template = template.replace("X4", model_name)
    template = template.replace("X5", mask_name)
    template = template.replace("X6", outname)

    # Create log file name
    logfile = outname + ".log"

    template = template.replace("X7", logfile)

    return template

def drop_last(ms_list):
    '''
    CASA is generally writing to the final MS in the folder, so skip it.
    '''

    max_num = 0
    for ms in ms_list:
        if int(ms.split("_")[-1][:-3]) > max_num:
            max_num_ms = ms
            max_num = int(ms.split("_")[-1][:-3])

    ms_list.remove(max_num_ms)

    return ms_list

# Set the directory to look in.
data_direc = sys.argv[1]

ms_channel_1 = os.path.join(data_direc, "14B-088_channel_ms/")
ms_channel_2 = os.path.join(data_direc, "AT0206_channel_ms/")
model_channels = os.path.join(
    data_direc, "model_channels/M33_model_channel_")
mask_channels = os.path.join(
    data_direc, "mask_channels/M33_newmask_channel_")
output_direc = os.path.join(data_direc, "single_channels/")

# Set the mode to use. Continuously checking for new splits, or a set number.
try:
    start = int(sys.argv[2])
    stop = int(sys.argv[3])
except IndexError:
    raise IndexError("Must provide a start and stop channel number.")

# Use mask and model? Disable when continuing to clean.
use_mask = True
# Due to the current issues with the Arecibo data as a SD model, skip using it
use_model = False

# Run channels in given range
channel_ms_1 = []
channel_ms_2 = []
for chan in xrange(start, stop + 1):
    channel_path = \
        os.path.join(ms_channel_1,
                     "14B-088_HI_LSRK_AT0206_regrid.ms.contsub_channel_" +
                     str(chan) + ".ms")
    channel_ms_1.append(channel_path)

    channel_path = \
        os.path.join(ms_channel_2,
                     "M33_b_c_LSRK_channel_" +
                     str(chan) + ".ms")
    channel_ms_2.append(channel_path)

# If there aren't any more split ms in the path, break and exit
if len(channel_ms_1) == 0 or len(channel_ms_2) == 0:
    raise ValueError("No more MSs found in the directory. Exiting.")

# Now loop through the existing channel ms
for chan_1, chan_2 in zip(channel_ms_1, channel_ms_2):
    chan_num_1 = int(chan_1.split("_")[-1][:-3])
    chan_num_2 = int(chan_2.split("_")[-1][:-3])

    assert chan_num_1 == chan_num_2
    chan_num = chan_num_1

    # adjust for numbering offset
    mod_mask_num = chan_num - 10

    channel_direc = os.path.join(output_direc, "channel_" + str(chan_num))

    if not os.path.exists(channel_direc):
        os.mkdir(channel_direc)
    if not os.path.exists(chan_1):
        shutil.move(chan_1, channel_direc)
    if not os.path.exists(chan_2):
        shutil.move(chan_2, channel_direc)

    model_name = model_channels + str(mod_mask_num) + ".image"
    if not os.path.exists(channel_direc) and use_model:
        shutil.move(model_name, channel_direc)

    mask_name = mask_channels + str(mod_mask_num) + ".image"
    if not os.path.exists(channel_direc) and use_mask:
        shutil.move(mask_name, channel_direc)

    chan_ms_1 = os.path.join(channel_direc, chan_1.split("/")[-1])
    chan_ms_2 = os.path.join(channel_direc, chan_2.split("/")[-1])

    if use_model:
        model_name = os.path.join(channel_direc,
                                  "M33_model_channel_"
                                  + str(mod_mask_num) + ".image")
    else:
        model_name = "None"

    if use_mask:
        mask_name = os.path.join(channel_direc,
                                 "M33_newmask_channel_"
                                 + str(mod_mask_num) + ".image")
    else:
        mask_name = "None"

    outname = \
        os.path.join(channel_direc,
                     "M33_HI_14B-088_AT0206_channel_" + str(chan_num))

    chan_template = return_template(channel_direc, chan_ms_1, chan_ms_2,
                                    model_name, mask_name, outname)

    # Write to file
    sub_file = os.path.join(channel_direc, "channel_" + str(chan_num) + ".sub")
    if not os.path.exists(sub_file):
        with open(sub_file, 'w') as f:
            f.write(chan_template)

    # Now submit!
    old_direc = os.getcwd()
    os.chdir(channel_direc)  # Switch to directory so log files are there
    os.system("qsub " + sub_file)
    os.chdir(old_direc)
<commit_msg>More updates for the combination job submission<commit_after>
'''
Generate a pbs script for job submission, submit the job, be happy
'''

import glob
import sys
import os
import shutil
import time

def return_template(output_direc, ms_name_1, ms_name_2, model_name,
                    mask_name, outname):

    # Emailing
    # PBS -m bea
    # PBS -M koch.eric.w@gmail.com

    template = \
        '''
#!/bin/bash

#PBS -S /bin/bash
#PBS -l pmem=1000m
#PBS -l nodes=1:ppn=12
#PBS -l walltime=7:00:00

source /home/ekoch/.bashrc

cd X1

echo "Starting at: `date`"
casa-4.4 --nologger --logfile X7 -c /home/ekoch/VLA_Lband/14B-088/HI/archival_combination/HI_single_channel_clean.py X2 X3 X4 X5 X6
echo "Exited with code $? at: `date`"
        '''

    template = template.strip()

    template = template.replace("X1", output_direc)
    template = template.replace("X2", ms_name_1)
    template = template.replace("X3", ms_name_2)
    template = template.replace("X4", model_name)
    template = template.replace("X5", mask_name)
    template = template.replace("X6", outname)

    # Create log file name
    logfile = outname + ".log"

    template = template.replace("X7", logfile)

    return template

def drop_last(ms_list):
    '''
    CASA is generally writing to the final MS in the folder, so skip it.
    '''

    max_num = 0
    for ms in ms_list:
        if int(ms.split("_")[-1][:-3]) > max_num:
            max_num_ms = ms
            max_num = int(ms.split("_")[-1][:-3])

    ms_list.remove(max_num_ms)

    return ms_list

# Set the directory to look in.
data_direc = sys.argv[1]

ms_channel_1 = os.path.join(data_direc, "14B-088_channel_ms/")
ms_channel_2 = os.path.join(data_direc, "AT0206_channel_ms/")
model_channel_name = os.path.join(
    data_direc, "model_channels/M33_model_channel_{}.image")
mask_channel_name = os.path.join(
    data_direc, "mask_channels/M33_newmask_channel_{}.image")
output_direc = os.path.join(data_direc, "single_channels/")

# Set the mode to use. Continuously checking for new splits, or a set number.
try:
    start = int(sys.argv[2])
    stop = int(sys.argv[3])
except IndexError:
    raise IndexError("Must provide a start and stop channel number.")

# Use mask and model? Disable when continuing to clean.
use_mask = True
# Due to the current issues with the Arecibo data as a SD model, skip using it
use_model = False

# Run channels in given range
channel_ms_1 = []
channel_ms_2 = []
for chan in xrange(start, stop + 1):
    channel_path = \
        os.path.join(ms_channel_1,
                     "14B-088_HI_LSRK_AT0206_regrid.ms.contsub_channel_" +
                     str(chan) + ".ms")
    channel_ms_1.append(channel_path)

    channel_path = \
        os.path.join(ms_channel_2,
                     "M33_b_c_LSRK_channel_" +
                     str(chan) + ".ms")
    channel_ms_2.append(channel_path)

# If there aren't any more split ms in the path, break and exit
if len(channel_ms_1) == 0 or len(channel_ms_2) == 0:
    raise ValueError("No more MSs found in the directory. Exiting.")

# Now loop through the existing channel ms
for chan_1, chan_2 in zip(channel_ms_1, channel_ms_2):
    chan_num_1 = int(chan_1.split("_")[-1][:-3])
    chan_num_2 = int(chan_2.split("_")[-1][:-3])

    assert chan_num_1 == chan_num_2
    chan_num = chan_num_1

    # adjust for numbering offset
    mod_mask_num = chan_num - 10

    channel_direc = os.path.join(output_direc, "channel_" + str(chan_num))

    if not os.path.exists(channel_direc):
        os.mkdir(channel_direc)

    base_ms_name_1 = os.path.basename(chan_1.rstrip("/"))
    chan_ms_1 = os.path.join(channel_direc, base_ms_name_1)

    base_ms_name_2 = os.path.basename(chan_2.rstrip("/"))
    chan_ms_2 = os.path.join(channel_direc, base_ms_name_2)

    base_model_name = \
        os.path.basename(model_channel_name.format(mod_mask_num))
    model_name = os.path.join(channel_direc, base_model_name)
    base_mask_name = \
        os.path.basename(mask_channel_name.format(mod_mask_num))
    mask_name = os.path.join(channel_direc, base_mask_name)

    if not os.path.exists(chan_ms_1):
        shutil.move(chan_1, channel_direc)
    if not os.path.exists(chan_ms_2):
        shutil.move(chan_2, channel_direc)

    if not os.path.exists(model_name) and use_model:
        shutil.move(model_channel_name.format(mod_mask_num), channel_direc)
    if not os.path.exists(mask_name) and use_mask:
        shutil.move(mask_channel_name.format(mod_mask_num), channel_direc)

    if not use_model:
        model_name = "None"

    if not use_mask:
        mask_name = "None"

    outname = \
        os.path.join(channel_direc,
                     "M33_HI_14B-088_AT0206_channel_" + str(chan_num))

    chan_template = return_template(channel_direc, chan_ms_1, chan_ms_2,
                                    model_name, mask_name, outname)

    # Write to file
    sub_file = os.path.join(channel_direc, "channel_" + str(chan_num) + ".sub")
    if not os.path.exists(sub_file):
        with open(sub_file, 'w') as f:
            f.write(chan_template)

    # Now submit!
    old_direc = os.getcwd()
    os.chdir(channel_direc)  # Switch to directory so log files are there
    os.system("qsub " + sub_file)
    os.chdir(old_direc)
<|endoftext|>


<commit_before>from bokeh.io import output_notebook
from bokeh.plotting import figure, show, gridplot, hplot, vplot, curdoc
import numpy as np
import os
import csv
from bokeh.client import push_session
import random
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.optimize as opt

def Dpointder(traj, n1, n2, n3, tn, nD):
    """
    Outputs the average diffusion coefficient at a desired timepoint.  User puts in a time.  The code finds the point
    closest to that time and gives the diffusion coefficient at that time.

    Note that this code is only for 2D datasets.  I need to modify it slightly for 3D datasets.

    n1: particle numbers (typically 0)
    n2: time (15 when using prettify)
    n3: MSDs or Deffs (9 for MSDs 17 for Deffs)
    tn: desired time
    nD: Either '2D', '1Dx', or '1Dy'

    Returns the values:

    MMSD: The diffusion coefficient at the desired time
    SD: The standard deviation
    total1: The number of particles taken into account in the calculation
    t: The timepoint at which the diffusion coefficient was calculated
    """

    # Creates an array 'particles' that contains the particle number at each frame.
    particles = traj[:, n1]
    total = int(max(particles))
    total1 = total + 1
    rawtime = traj[:, n2]
    bow = traj.shape[0]
    raw2DMSDs = np.zeros((bow, 4))
    raw2DMSDs[:, 0] = traj[:, n3]
    raw2DMSDs[:, 1:4] = traj[:, n3 + 3:n3 + 6]
    MSD = dict()
    time = dict()

    # Creates an array for each trajectory containing all xyz data
    for num in range(1, total1):

        hold = np.where(particles == num)
        itindex = hold[0]
        min1 = min(itindex)
        max1 = max(itindex)
        MSD[num] = (raw2DMSDs[min1+2:max1, :])
        time[num] = (rawtime[min1+2:max1])

    MMSD = MSD[1]
    for num in range(2, total1):
        MMSD = MMSD + MSD[num]
    MMSD = MMSD/total1
    SD = np.zeros(np.shape(MMSD))
    t = time[1][:]

    # Now to calculate the standard dev at each point:
    for num in range(1, total1):
        SDunit = (MSD[num] - MMSD)**2
        SD = SD + SDunit
    SD = np.sqrt(SD/total1)
    SE = SD/np.sqrt(total1)

    def find_nearest(array, value):
        idx = (np.abs(array-value)).argmin()
        return array[idx], idx

    td, idx = find_nearest(t, tn)

    if nD == '2D':
        D = MMSD[idx, 0]
        SDd = SD[idx, 0]
    elif nD == '1Dx':
        D = MMSD[idx, 1]
        SDd = SD[idx, 1]
    else:
        D = MMSD[idx, 2]
        SDd = SD[idx, 2]

    return D, SDd, total1, td
<commit_msg>Added a function that calculated diffusion coefficients multiple ways.<commit_after>from bokeh.io import output_notebook
from bokeh.plotting import figure, show, gridplot, hplot, vplot, curdoc
import numpy as np
import os
import csv
from bokeh.client import push_session
import random
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.optimize as opt

def Dpointder(traj, n1, n2, n3, tn, nD):
    """
    Outputs the average diffusion coefficient at a desired timepoint.  User puts in a time.  The code finds the point
    closest to that time and gives the diffusion coefficient at that time.

    Note that this code is only for 2D datasets.  I need to modify it slightly for 3D datasets.

    n1: particle numbers (typically 0)
    n2: time (15 when using prettify)
    n3: MSDs or Deffs (9 for MSDs 17 for Deffs)
    tn: desired time
    nD: Either '2D', '1Dx', or '1Dy'

    Returns the values:

    MMSD: The diffusion coefficient at the desired time
    SD: The standard deviation
    total1: The number of particles taken into account in the calculation
    t: The timepoint at which the diffusion coefficient was calculated
    """

    # Creates an array 'particles' that contains the particle number at each frame.
    particles = traj[:, n1]
    total = int(max(particles))
    total1 = total + 1
    rawtime = traj[:, n2]
    bow = traj.shape[0]
    raw2DMSDs = np.zeros((bow, 4))
    raw2DMSDs[:, 0] = traj[:, n3]
    raw2DMSDs[:, 1:4] = traj[:, n3 + 3:n3 + 6]
    MSD = dict()
    time = dict()

    # Creates an array for each trajectory containing all xyz data
    for num in range(1, total1):

        hold = np.where(particles == num)
        itindex = hold[0]
        min1 = min(itindex)
        max1 = max(itindex)
        MSD[num] = (raw2DMSDs[min1+2:max1, :])
        time[num] = (rawtime[min1+2:max1])

    MMSD = MSD[1]
    for num in range(2, total1):
        MMSD = MMSD + MSD[num]
    MMSD = MMSD/total1
    SD = np.zeros(np.shape(MMSD))
    t = time[1][:]

    # Now to calculate the standard dev at each point:
    for num in range(1, total1):
        SDunit = (MSD[num] - MMSD)**2
        SD = SD + SDunit
    SD = np.sqrt(SD/total1)
    SE = SD/np.sqrt(total1)

    def find_nearest(array, value):
        idx = (np.abs(array-value)).argmin()
        return array[idx], idx

    td, idx = find_nearest(t, tn)

    if nD == '2D':
        D = MMSD[idx, 0]
        SDd = SD[idx, 0]
    elif nD == '1Dx':
        D = MMSD[idx, 1]
        SDd = SD[idx, 1]
    else:
        D = MMSD[idx, 2]
        SDd = SD[idx, 2]

    return D, SDd, total1, td

def Dlinalg(traj, n1, n2, n3, nD):
    """
    Outputs the average diffusion coefficient at a desired timepoint.  User puts in a time.  The code finds the point
    closest to that time and gives the diffusion coefficient at that time.

    Note that this code is only for 2D datasets.  I need to modify it slightly for 3D datasets.

    n1: particle numbers (typically 0)
    n2: time (15 when using prettify)
    n3: MSDs or Deffs (9 for MSDs 17 for Deffs) **MUST be in MSDs in this case, not Deffs**
    nD: Either '2D', '1Dx', or '1Dy'

    Returns the values:

    MMSD: The diffusion coefficient at the desired time
    SD: The standard deviation
    total1: The number of particles taken into account in the calculation
    t: The timepoint at which the diffusion coefficient was calculated
    """

    # Creates an array 'particles' that contains the particle number at each frame.
    particles = traj[:, n1]
    total = int(max(particles))
    total1 = total + 1
    rawtime = traj[:, n2]
    bow = traj.shape[0]
    raw2DMSDs = np.zeros((bow, 4))
    raw2DMSDs[:, 0] = traj[:, n3]
    raw2DMSDs[:, 1:4] = traj[:, n3 + 3:n3 + 6]
    MSD = dict()
    time = dict()

    # Creates an array for each trajectory containing all xyz data
    for num in range(1, total1):

        hold = np.where(particles == num)
        itindex = hold[0]
        min1 = min(itindex)
        max1 = max(itindex)
        MSD[num] = (raw2DMSDs[min1+2:max1, :])
        time[num] = (rawtime[min1+2:max1])

    MMSD = MSD[1]
    for num in range(2, total1):
        MMSD = MMSD + MSD[num]
    MMSD = MMSD/total1
    SD = np.zeros(np.shape(MMSD))
    t = time[1][:]

    # Now to calculate the standard dev at each point:
    for num in range(1, total1):
        SDunit = (MSD[num] - MMSD)**2
        SD = SD + SDunit
    SD = np.sqrt(SD/total1)
    SE = SD/np.sqrt(total1)

    w = dict()
    cov = dict()
    sd = dict()
    line = dict()
    # A = np.ones((np.shape(t)[0], 2))
    # A[:, 0] = t
    # w[0] = np.linalg.lstsq(A, MMSD[:, 0])[0]
    # w[1] = np.linalg.lstsq(A, MMSD[:, 1])[0]
    # w[2] = np.linalg.lstsq(A, MMSD[:, 2])[0]

    w[0], cov[0] = np.polyfit(t, MMSD[:, 0], 1, cov=True)
    w[1], cov[1] = np.polyfit(t, MMSD[:, 1], 1, cov=True)
    w[2], cov[2] = np.polyfit(t, MMSD[:, 2], 1, cov=True)

    sd[0] = np.sqrt(np.diag(cov[0]))
    sd[1] = np.sqrt(np.diag(cov[1]))
    sd[2] = np.sqrt(np.diag(cov[2]))

    if nD == '2D':
        D = w[0][0]/4
        SDd = sd[0][0]/4
    elif nD == '1Dx':
        D = w[1][0]/2
        SDd = sd[1][0]/2
    else:
        D = w[2][0]/2
        SDd = sd[2][0]/2

    return D, SDd, total1

def Dlinalgw(traj, n1, n2, n3, nD):
    """
    Outputs the average diffusion coefficient at a desired timepoint.  User puts in a time.  The code finds the point
    closest to that time and gives the diffusion coefficient at that time.

    Note that this code is only for 2D datasets.  I need to modify it slightly for 3D datasets.

    n1: particle numbers (typically 0)
    n2: time (15 when using prettify)
    n3: MSDs or Deffs (9 for MSDs 17 for Deffs) **MUST be in MSDs in this case, not Deffs**
    nD: Either '2D', '1Dx', or '1Dy'

    Returns the values:

    MMSD: The diffusion coefficient at the desired time
    SD: The standard deviation
    total1: The number of particles taken into account in the calculation
    t: The timepoint at which the diffusion coefficient was calculated
    """

    # Creates an array 'particles' that contains the particle number at each frame.
    particles = traj[:, n1]
    total = int(max(particles))
    total1 = total + 1
    rawtime = traj[:, n2]
    bow = traj.shape[0]
    raw2DMSDs = np.zeros((bow, 4))
    raw2DMSDs[:, 0] = traj[:, n3]
    raw2DMSDs[:, 1:4] = traj[:, n3 + 3:n3 + 6]
    MSD = dict()
    time = dict()

    # Creates an array for each trajectory containing all xyz data
    for num in range(1, total1):

        hold = np.where(particles == num)
        itindex = hold[0]
        min1 = min(itindex)
        max1 = max(itindex)
        MSD[num] = (raw2DMSDs[min1+2:max1, :])
        time[num] = (rawtime[min1+2:max1])

    MMSD = MSD[1]
    for num in range(2, total1):
        MMSD = MMSD + MSD[num]
    MMSD = MMSD/total1
    SD = np.zeros(np.shape(MMSD))
    t = time[1][:]

    # Now to calculate the standard dev at each point:
    for num in range(1, total1):
        SDunit = (MSD[num] - MMSD)**2
        SD = SD + SDunit
    SD = np.sqrt(SD/total1)
    SE = SD/np.sqrt(total1)

    w = dict()
    cov = dict()
    sd = dict()
    line = dict()
    # A = np.ones((np.shape(t)[0], 2))
    # A[:, 0] = t
    # w[0] = np.linalg.lstsq(A, MMSD[:, 0])[0]
    # w[1] = np.linalg.lstsq(A, MMSD[:, 1])[0]
    # w[2] = np.linalg.lstsq(A, MMSD[:, 2])[0]

    weight = dict()
    weight[0] = [1.0 / ty for ty in SD[:, 0]]
    weight[1] = [1.0 / ty for ty in SD[:, 1]]
    weight[2] = [1.0 / ty for ty in SD[:, 2]]

    w[0], cov[0] = np.polyfit(t, MMSD[:, 0], 1, w=weight[0], full=False, cov=True)
    w[1], cov[1] = np.polyfit(t, MMSD[:, 1], 1, w=weight[1], full=False, cov=True)
    w[2], cov[2] = np.polyfit(t, MMSD[:, 2], 1, w=weight[2], full=False, cov=True)

    sd[0] = np.sqrt(np.diag(cov[0]))
    sd[1] = np.sqrt(np.diag(cov[1]))
    sd[2] = np.sqrt(np.diag(cov[2]))

    if nD == '2D':
        D = w[0][0]/4
        SDd = sd[0][0]/4
    elif nD == '1Dx':
        D = w[1][0]/2
        SDd = sd[1][0]/2
    else:
        D = w[2][0]/2
        SDd = sd[2][0]/2

    return D, SDd, total1
<|endoftext|>


<commit_before># ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

from __future__ import absolute_import, division, print_function

from skbio.util._metadata_repr import _MetadataReprBuilder
import math

class _TabularMSAReprBuilder(_MetadataReprBuilder):

    def _process_header(self):
        cls_name = self._obj.__class__.__name__
        if self._obj.dtype is not None:
            dtype_class = '<' + self._obj.dtype.__name__ + '>'
        else:
            dtype_class = ''
        self._lines.add_line(cls_name + dtype_class)
        self._lines.add_separator()
        self._ellipse_insert = ' ... '

    def _process_data(self):
        num_sequences = self._obj.shape.sequence
        num_positions = self._obj.shape.position

        # catch case of all empty sequences
        if num_positions > 0:
            # display all sequences if we can, else display the first two and
            # last two sequences separated by ellipsis
            if num_sequences <= 5:
                self._lines.add_lines(
                    self._format_sequences(range(num_sequences)))
            else:
                self._lines.add_lines(self._format_sequences(range(2)))
                self._lines.add_line('...')
                self._lines.add_lines(self._format_sequences(
                    range(num_sequences - 2, num_sequences)))

    def _format_sequences(self, line_indices):
        lines = []
        for line_index in line_indices:
            seq_str = str(self._obj._get_sequence(line_index))
            if len(seq_str) <= self._width:
                formatted_seq = seq_str
            else:
                formatted_seq = (
                    seq_str[0:self._num_characters_before_ellipse()] +
                    self._ellipse_insert +
                    seq_str[-self._num_characters_after_ellipse():]
                )
            lines.append(formatted_seq)
        return lines

    def _num_characters_before_ellipse(self):
        return math.floor(self._num_characters_to_display() / 2)

    def _num_characters_after_ellipse(self):
        return (self._num_characters_to_display() -
                self._num_characters_before_ellipse())

    def _num_characters_to_display(self):
        return self._width - len(self._ellipse_insert)
<commit_msg>Return int for indices<commit_after># ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

from __future__ import absolute_import, division, print_function

from skbio.util._metadata_repr import _MetadataReprBuilder
import math

class _TabularMSAReprBuilder(_MetadataReprBuilder):

    def _process_header(self):
        cls_name = self._obj.__class__.__name__
        if self._obj.dtype is not None:
            dtype_class = '<' + self._obj.dtype.__name__ + '>'
        else:
            dtype_class = ''
        self._lines.add_line(cls_name + dtype_class)
        self._lines.add_separator()
        self._ellipse_insert = ' ... '

    def _process_data(self):
        num_sequences = self._obj.shape.sequence
        num_positions = self._obj.shape.position

        # catch case of all empty sequences
        if num_positions > 0:
            # display all sequences if we can, else display the first two and
            # last two sequences separated by ellipsis
            if num_sequences <= 5:
                self._lines.add_lines(
                    self._format_sequences(range(num_sequences)))
            else:
                self._lines.add_lines(self._format_sequences(range(2)))
                self._lines.add_line('...')
                self._lines.add_lines(self._format_sequences(
                    range(num_sequences - 2, num_sequences)))

    def _format_sequences(self, line_indices):
        lines = []
        for line_index in line_indices:
            seq_str = str(self._obj._get_sequence(line_index))
            if len(seq_str) <= self._width:
                formatted_seq = seq_str
            else:
                formatted_seq = (
                    seq_str[0:self._num_characters_before_ellipse()] +
                    self._ellipse_insert +
                    seq_str[-self._num_characters_after_ellipse():]
                )
            lines.append(formatted_seq)
        return lines

    def _num_characters_before_ellipse(self):
        return int(self._num_characters_to_display() / 2)

    def _num_characters_after_ellipse(self):
        return (self._num_characters_to_display() -
                self._num_characters_before_ellipse())

    def _num_characters_to_display(self):
        return self._width - len(self._ellipse_insert)
<|endoftext|>


<commit_before>"""
SkCode footnotes tag definitions code.
"""

from html import escape as escape_html

from .base import TagOptions
from ..tools import (escape_attrvalue,
                     slugify)

# Footnote ID format for HTML rendering
FOOTNOTE_ID_HTML_FORMAT = 'footnote-%s'

# Footnote ID format for HTML rendering (back ref to declaration)
FOOTNOTE_ID_HTML_FORMAT_BACKREF = 'footnote-ref-%s'

class FootnoteDeclarationTagOptions(TagOptions):
    """ Footnote declaration tag options container class. """

    inline = True
    close_inlines = False

    # Footnote ID attribute name
    footnote_id_attr_name = 'id'

    def get_footnote_id_from_counter(self, tree_node):
        """
        Get the footnote ID from the counter stored in the root tree node or in the tree node cache.
        :param tree_node: The current tree node instance.
        :return: The footnote ID retrieved from the root tree node counter.
        """

        # Get the ID from the node cache if exists
        if hasattr(tree_node, '_cached_footnote_counter'):
            return str(getattr(tree_node, '_cached_footnote_counter'))

        # Got up to the root node
        root_tree_node = tree_node
        while root_tree_node.parent is not None:
            root_tree_node = root_tree_node.parent

        # Get the current counter value
        counter = getattr(root_tree_node, '_last_footnote_counter', 0)

        # Increment and store the counter
        counter += 1
        setattr(root_tree_node, '_last_footnote_counter', counter)

        # Store the ID in the node cache to avoid multiple ID generation
        setattr(tree_node, '_cached_footnote_counter', counter)

        # Return the ID for this footnote
        return str(counter)

    def get_footnote_id(self, tree_node, use_autogenerated_id=True):
        """
        Get the ID of this footnote.
        The ID can be set by setting the footnote_id_attr_name attribute of the tag or simply
        by setting the tag name attribute. If not set, an auto-generated ID will be used.
        The lookup order is: tag name (first), footnote_id_attr_name, auto generated ID.
        :param tree_node: The current tree node instance.
        :return: The ID of this footnote.
        """
        footnote_id = tree_node.attrs.get(tree_node.name, '')
        if not footnote_id:
            footnote_id = tree_node.attrs.get(self.footnote_id_attr_name, '')
        if not footnote_id and use_autogenerated_id:
            footnote_id = self.get_footnote_id_from_counter(tree_node)
        return slugify(footnote_id)

    def render_html(self, tree_node, inner_html, force_rel_nofollow=True):
        """
        Callback function for rendering HTML.
        :param force_rel_nofollow: If set, all links in the rendered HTML will have "rel=nofollow" (default to True).
        :param tree_node: Current tree node to be rendered.
        :param inner_html: Inner HTML of this tree node.
        :return Rendered HTML of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        return '[%s]' % (
            escape_html(FOOTNOTE_ID_HTML_FORMAT_BACKREF % footnote_id),
            escape_html(FOOTNOTE_ID_HTML_FORMAT % footnote_id),
            footnote_id)

    def render_text(self, tree_node, inner_text):
        """
        Callback function for rendering text.
        :param tree_node: Current tree node to be rendered.
        :param inner_text: Inner text of this tree node.
        :return Rendered text of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        return '[^%s]' % footnote_id

    def render_skcode(self, tree_node, inner_skcode):
        """
        Callback function for rendering SkCode.
        :param tree_node: Current tree node to be rendered.
        :param inner_skcode: Inner SkCode of this tree node.
        :return Rendered SkCode of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node, use_autogenerated_id=False)

        # Render ID
        if footnote_id:
            extra_attrs = ' %s=%s' % (self.footnote_id_attr_name,
                                      escape_attrvalue(footnote_id))
        else:
            extra_attrs = ''

        # Render the footnote
        node_name = tree_node.name
        return '[%s%s]%s[/%s]' % (node_name, extra_attrs, inner_skcode, node_name)

class FootnoteReferenceTagOptions(TagOptions):
    """ Footnote reference tag options container class. """

    inline = True
    close_inlines = False

    def get_footnote_id(self, tree_node):
        """
        Get the target ID of this footnote reference from the content of the node.
        :param tree_node: The current tree node instance.
        :return: The target ID of this footnote reference.
        """
        return slugify(tree_node.get_raw_content())

    def render_html(self, tree_node, inner_html, force_rel_nofollow=True):
        """
        Callback function for rendering HTML.
        :param force_rel_nofollow: If set, all links in the rendered HTML will have "rel=nofollow" (default to True).
        :param tree_node: Current tree node to be rendered.
        :param inner_html: Inner HTML of this tree node.
        :return Rendered HTML of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            return '[%s]' % (escape_html(FOOTNOTE_ID_HTML_FORMAT % footnote_id),
                                                          footnote_id)
        else:
            return ''

    def render_text(self, tree_node, inner_text):
        """
        Callback function for rendering text.
        :param tree_node: Current tree node to be rendered.
        :param inner_text: Inner text of this tree node.
        :return Rendered text of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            return '[^%s]' % footnote_id
        else:
            return inner_text

    def render_skcode(self, tree_node, inner_skcode):
        """
        Callback function for rendering SkCode.
        :param tree_node: Current tree node to be rendered.
        :param inner_skcode: Inner SkCode of this tree node.
        :return Rendered SkCode of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            node_name = tree_node.name
            return '[%s]%s[/%s]' % (node_name, footnote_id, node_name)
        else:
            return inner_skcode
<commit_msg>Fix docstring<commit_after>"""
SkCode footnotes tag definitions code.
"""

from html import escape as escape_html

from .base import TagOptions
from ..tools import (escape_attrvalue,
                     slugify)

# Footnote ID format for HTML rendering
FOOTNOTE_ID_HTML_FORMAT = 'footnote-%s'

# Footnote ID format for HTML rendering (back ref to declaration)
FOOTNOTE_ID_HTML_FORMAT_BACKREF = 'footnote-ref-%s'

class FootnoteDeclarationTagOptions(TagOptions):
    """ Footnote declaration tag options container class. """

    inline = True
    close_inlines = False

    # Footnote ID attribute name
    footnote_id_attr_name = 'id'

    def get_footnote_id_from_counter(self, tree_node):
        """
        Get the footnote ID from the counter stored in the root tree node or in the tree node cache.
        :param tree_node: The current tree node instance.
        :return: The footnote ID retrieved from the root tree node counter.
        """

        # Get the ID from the node cache if exists
        if hasattr(tree_node, '_cached_footnote_counter'):
            return str(getattr(tree_node, '_cached_footnote_counter'))

        # Got up to the root node
        root_tree_node = tree_node
        while root_tree_node.parent is not None:
            root_tree_node = root_tree_node.parent

        # Get the current counter value
        counter = getattr(root_tree_node, '_last_footnote_counter', 0)

        # Increment and store the counter
        counter += 1
        setattr(root_tree_node, '_last_footnote_counter', counter)

        # Store the ID in the node cache to avoid multiple ID generation
        setattr(tree_node, '_cached_footnote_counter', counter)

        # Return the ID for this footnote
        return str(counter)

    def get_footnote_id(self, tree_node, use_autogenerated_id=True):
        """
        Get the ID of this footnote.
        The ID can be set by setting the footnote_id_attr_name attribute of the tag or simply
        by setting the tag name attribute. If not set, an auto-generated ID will be used.
        The lookup order is: tag name (first), footnote_id_attr_name, auto generated ID.
        :param tree_node: The current tree node instance.
        :param use_autogenerated_id: Set to True (default) to use an auto-generated ID if no user ID available.
        :return: The ID of this footnote.
        """
        footnote_id = tree_node.attrs.get(tree_node.name, '')
        if not footnote_id:
            footnote_id = tree_node.attrs.get(self.footnote_id_attr_name, '')
        if not footnote_id and use_autogenerated_id:
            footnote_id = self.get_footnote_id_from_counter(tree_node)
        return slugify(footnote_id)

    def render_html(self, tree_node, inner_html, force_rel_nofollow=True):
        """
        Callback function for rendering HTML.
        :param force_rel_nofollow: If set, all links in the rendered HTML will have "rel=nofollow" (default to True).
        :param tree_node: Current tree node to be rendered.
        :param inner_html: Inner HTML of this tree node.
        :return Rendered HTML of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        return '[%s]' % (
            escape_html(FOOTNOTE_ID_HTML_FORMAT_BACKREF % footnote_id),
            escape_html(FOOTNOTE_ID_HTML_FORMAT % footnote_id),
            footnote_id)

    def render_text(self, tree_node, inner_text):
        """
        Callback function for rendering text.
        :param tree_node: Current tree node to be rendered.
        :param inner_text: Inner text of this tree node.
        :return Rendered text of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        return '[^%s]' % footnote_id

    def render_skcode(self, tree_node, inner_skcode):
        """
        Callback function for rendering SkCode.
        :param tree_node: Current tree node to be rendered.
        :param inner_skcode: Inner SkCode of this tree node.
        :return Rendered SkCode of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node, use_autogenerated_id=False)

        # Render ID
        if footnote_id:
            extra_attrs = ' %s=%s' % (self.footnote_id_attr_name,
                                      escape_attrvalue(footnote_id))
        else:
            extra_attrs = ''

        # Render the footnote
        node_name = tree_node.name
        return '[%s%s]%s[/%s]' % (node_name, extra_attrs, inner_skcode, node_name)

class FootnoteReferenceTagOptions(TagOptions):
    """ Footnote reference tag options container class. """

    inline = True
    close_inlines = False

    def get_footnote_id(self, tree_node):
        """
        Get the target ID of this footnote reference from the content of the node.
        :param tree_node: The current tree node instance.
        :return: The target ID of this footnote reference.
        """
        return slugify(tree_node.get_raw_content())

    def render_html(self, tree_node, inner_html, force_rel_nofollow=True):
        """
        Callback function for rendering HTML.
        :param force_rel_nofollow: If set, all links in the rendered HTML will have "rel=nofollow" (default to True).
        :param tree_node: Current tree node to be rendered.
        :param inner_html: Inner HTML of this tree node.
        :return Rendered HTML of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            return '[%s]' % (escape_html(FOOTNOTE_ID_HTML_FORMAT % footnote_id),
                                                          footnote_id)
        else:
            return ''

    def render_text(self, tree_node, inner_text):
        """
        Callback function for rendering text.
        :param tree_node: Current tree node to be rendered.
        :param inner_text: Inner text of this tree node.
        :return Rendered text of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            return '[^%s]' % footnote_id
        else:
            return inner_text

    def render_skcode(self, tree_node, inner_skcode):
        """
        Callback function for rendering SkCode.
        :param tree_node: Current tree node to be rendered.
        :param inner_skcode: Inner SkCode of this tree node.
        :return Rendered SkCode of this node.
        """

        # Get the footnote ID
        footnote_id = self.get_footnote_id(tree_node)

        # Render the footnote
        if footnote_id:
            node_name = tree_node.name
            return '[%s]%s[/%s]' % (node_name, footnote_id, node_name)
        else:
            return inner_skcode
<|endoftext|>


<commit_before>from random import choice
from cards import *

class Flush:
    def __str__(self):
        return "Flush"

    def check(cards):
        suits = {c.suit for c in cards}
        return len(suits) == 1

class Pair:
    def __str__(self):
        return "Pair"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 4

class TwoPairs:
    def __str__(self):
        return "TwoPairs"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 3 and max(nom.values()) == 2 and min(nom.values()) == 1

class Set:
    def __str__(self):
        return "Set"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 3 and max(nom.values()) == 3 and min(nom.values()) == 1

class FullHouse:
    def __str__(self):
        return "FullHouse"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 2 and max(nom.values()) == 3

class Quads:
    def __str__(self):
        return "Quads"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 2 and max(nom.values()) == 4

class Straight:
    def __str__(self):
        return "Straight"

    def check(cards):
        s = list(map(lambda x: x.value.value, cards))
        return not Pair.check(cards) and max(s) - min(s) == 4

class High:
    def check(cards):
        return True

class StraightFlush():
    def __str__(self):
        return "StraightFlush"

    def check(cards):
        return Straight.check(cards) and Flush.check(cards)

def compare(cards):
    for c in combinations:
        if c.check(cards):
            return c()

combinations = [StraightFlush, Quads, FullHouse, Flush, Straight, Set, TwoPairs, Pair, High]

Suits = [Diamonds, Clubs, Hearts, Spades]
cards = [Card(2, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(2, Diamonds)]
#cards = [Card(i, Diamonds) for i in range(2, 7)]

print(compare(cards))
print(' '.join(map(str, cards)))
<commit_msg>сравнение карт пока не работает<commit_after>from random import choice
from cards import *

class Flush:
    def __str__(self):
        return "Flush"

    def check(cards):
        suits = {c.suit for c in cards}
        return len(suits) == 1

class Pair:
    def __str__(self):
        return "Pair"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 4

class TwoPairs:
    def __str__(self):
        return "TwoPairs"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 3 and max(nom.values()) == 2 and min(nom.values()) == 1

class Set:
    def __str__(self):
        return "Set"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 3 and max(nom.values()) == 3 and min(nom.values()) == 1

class FullHouse:
    def __str__(self):
        return "FullHouse"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 2 and max(nom.values()) == 3

class Quads:
    def __str__(self):
        return "Quads"

    def check(cards):
        nom = {}
        for v in map(lambda x: x.value.value, cards):
            nom[v] = nom.get(v, 0) + 1
        return len(nom) == 2 and max(nom.values()) == 4

class Straight:
    def __str__(self):
        return "Straight"

    def check(cards):
        s = list(map(lambda x: x.value.value, cards))
        return not Pair.check(cards) and max(s) - min(s) == 4

class High:
    def check(cards):
        return True

class StraightFlush():
    def __str__(self):
        return "StraightFlush"

    def check(cards):
        return Straight.check(cards) and Flush.check(cards)

combinations = [StraightFlush, Quads, FullHouse, Flush, Straight, Set, TwoPairs, Pair, High]

class Comparator:
    def combine(self, cards):
        r = []
        for i in range(6):
            for j in range(i + 1, 7):
                 r.append(cards[:i] + cards[i + 1: j] + cards[j + 1:])
        return r

    def compare(self, clients, table):
        res = {}
        for client_id, cards in clients:
            res[max(map(max_combine, combine(cards + table)))].append(client_id)
        ans = []
        for key in sorted(res):
            ans.append(res[key])
        return ans

    def max_combine(cards):
        for c in combinations:
            if c.check(cards):
                return c()

Suits = [Diamonds, Clubs, Hearts, Spades]

cards = [
        Card(2, Diamonds),
        Card(5, Diamonds),
        Card(9, Diamonds),
        Card(11, Diamonds),
        Card(0, Diamonds),
        Card(2, Hearts),
        Card(5, Hearts),
        Card(5, Clubs),
        Card(5, Clubs),
        Card(2, Clubs)
]

#cards = [Card(i, Diamonds) for i in range(2, 7)]

print(*cards)
<|endoftext|>


<commit_before>'''This module contains code for computing the complexity of various
operations.

'''

import math
import random
import matplotlib.pyplot as plt
import numpy as np
import stats

# Source: http://web.eecs.utk.edu/~plank/plank/papers/FAST-2005.pdf
LDPC_RATE = 1.0496
LDPC_XOR_PER_WORD = 30 / 4
RS_XOR_PER_WORD = 44.6

# Finite field arithmetic complexity for fields of size 2^100
# Source: Zhi, L., Higgins, J., & Clement, M. (2001).
# Performance of finite field arithmetic in an elliptic curve cryptosystem.

# Ignore addition cost to simplify analysis
# ADDITION_COMPLEXITY = 0.13e-6
# ADDITION_COMPLEXITY = 0
# MULTIPLICATION_COMPLEXITY = 87e-6
ADDITION_COMPLEXITY = 0
MULTIPLICATION_COMPLEXITY = 1

def partitioned_encode_delay(parameters, partitions=None):
    '''Compute delay incurred by the encoding phase. Assumes a shifted exponential
    distribution.

    Args:

    parameters: System parameters.

    partitions: The number of partitions. If None, the value in parameters is
    used.

    Returns: The reduce delay.

    '''
    assert partitions is None or partitions % 1 == 0
    if partitions is None:
        partitions = parameters.num_partitions

    delay = stats.order_mean_shiftexp(
        parameters.num_servers,
        parameters.num_servers,
    )

    # Scale by encoding complexity per server
    delay *= parameters.num_source_rows / partitions
    delay *= parameters.num_coded_rows * parameters.num_columns
    delay /= parameters.num_servers
    return delay

def block_diagonal_encoding_complexity(parameters, partitions=None):
    assert partitions is None or partitions % 1 == 0
    if partitions is None:
        partitions = parameters.num_partitions
    multiplications = parameters.num_source_rows / partitions
    multiplications *= parameters.num_coded_rows * parameters.num_columns
    additions = parameters.num_source_rows / partitions - 1
    additions *= parameters.num_coded_rows * parameters.num_columns
    return MULTIPLICATION_COMPLEXITY * multiplications + ADDITION_COMPLEXITY * additions

def stragglerc_encode_delay(parameters):
    '''Compute reduce delay for a system using only straggler coding, i.e., using
    an erasure code to deal with stragglers but no coded multicasting.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''
    partitions = parameters.num_source_rows / parameters.q
    return partitioned_encode_delay(parameters, partitions=partitions)

def partitioned_reduce_delay(parameters, partitions=None):
    '''Compute delay incurred by the reduce phase. Assumes a shifted
    exponential distribution.

    Args:

    parameters: System parameters.

    partitions: The number of partitions. If None, the value in
    parameters is used.

    Returns: The reduce delay.

    '''
    assert partitions is None or (isinstance(partitions, int) and partitions > 0)
    if partitions is None:
        partitions = parameters.num_partitions

    delay = stats.order_mean_shiftexp(
        parameters.q,
        parameters.q,
    )

    # Scale by decoding complexity per server
    delay *= block_diagonal_decoding_complexity(
        parameters.num_coded_rows,
        1,
        1 - parameters.q / parameters.num_servers,
        partitions,
    )
    delay *= parameters.num_outputs / parameters.q
    return delay

def stragglerc_reduce_delay(parameters):
    '''Compute reduce delay for a system using only straggler coding,
    i.e., using an erasure code to deal with stragglers but no coded
    multicasting.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''
    # TODO: Evaluate partitioned_reduce_delay for correct T instead
    delay = stats.order_mean_shiftexp(parameters.q, parameters.q)

    # Scale by decoding complexity
    rows_per_server = parameters.num_source_rows / parameters.q
    delay *= block_diagonal_decoding_complexity(parameters.num_servers,
                                                rows_per_server,
                                                1 - parameters.q / parameters.num_servers,
                                                1)
    delay *= parameters.num_outputs / parameters.q
    return delay

def encoding_complexity_from_density(parameters=None, density=None):
    '''compute encoding complexity from the density of the encoding matrix

    args:

    parameters: system parameters

    density: average fraction of non-zero entries in the encoding matrix.

    returns: complexity of the encoding.

    '''
    assert 0 < density <= 1
    multiplications = parameters.num_source_rows * density
    multiplications *= parameters.num_coded_rows * parameters.num_columns
    additions = parameters.num_source_rows * density - 1
    additions *= parameters.num_coded_rows * parameters.num_columns
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY

def lt_reduce_delay(parameters):
    '''Compute delay incurred by the reduce phase using an LT code.
    Assumes a shifted exponential distribution.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''

def rs_decoding_complexity(code_length, packet_size, erasure_prob):
    '''Compute the decoding complexity of Reed-Solomon codes

    Return the number of operations (additions and multiplications)
    required to decode a Reed-Solomon code over the packet erasure
    channel, and using the Berelkamp-Massey algorithm.

    Args:

    code_length: The length of the code in number of coded symbols.

    packet_size: The size of a packet in number of symbols.

    erasure_prob: The erasure probability of the packet erasure channel.

    Returns: The total complexity of decoding.

    '''
    additions = code_length * (erasure_prob * code_length - 1) * packet_size
    multiplications = pow(code_length, 2) * erasure_prob * packet_size;
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY

def block_diagonal_decoding_complexity(code_length, packet_size, erasure_prob, partitions):
    '''Compute the decoding complexity of a block-diagonal code

    Return the number of operations (additions and multiplications)
    required to decode a block-diagonal code over the packet erasure
    channel, and using the Berelkamp-Massey algorithm. This function
    considers the asymptotic case as the packet size approaches
    infinity.

    Args:

    code_length: The length of the code in number of coded symbols.

    packet_size: The size of a packet in number of symbols.

    erasure_prob: The erasure probability of the packet erasure channel.

    partitions: The number of block-diagonal code partitions.

    Returns: The total complexity of decoding.

    '''
    assert isinstance(code_length, int)
    assert isinstance(packet_size, int) or isinstance(packet_size, float)
    assert isinstance(erasure_prob, float)
    assert isinstance(partitions, int)
    assert code_length % partitions == 0, 'Partitions must divide code_length.'
    partition_length = code_length / partitions
    partition_complexity = rs_decoding_complexity(partition_length, packet_size, erasure_prob)
    return partition_complexity * partitions

def matrix_vector_complexity(rows=None, cols=None):
    '''Compute the complexity of matrix-vector multiplication

    Return the complexity of multiplying a matrix A with number of
    rows and columns as given in the argument by a vector x. The
    multiplication is done as A*x.

    Args:

    rows: The number of rows of the matrix.

    cols: The number of columns of the matrix.

    Returns: The complexity of the multiplication.

    '''
    additions = cols * rows - 1
    multiplications = cols * rows
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY
<commit_msg>separate method for encoding complexity<commit_after>'''This module contains code for computing the complexity of various
operations.

'''

import math
import random
import matplotlib.pyplot as plt
import numpy as np
import stats

# Source: http://web.eecs.utk.edu/~plank/plank/papers/FAST-2005.pdf
LDPC_RATE = 1.0496
LDPC_XOR_PER_WORD = 30 / 4
RS_XOR_PER_WORD = 44.6

# Finite field arithmetic complexity for fields of size 2^100
# Source: Zhi, L., Higgins, J., & Clement, M. (2001).
# Performance of finite field arithmetic in an elliptic curve cryptosystem.

# Ignore addition cost to simplify analysis
# ADDITION_COMPLEXITY = 0.13e-6
# ADDITION_COMPLEXITY = 0
# MULTIPLICATION_COMPLEXITY = 87e-6
ADDITION_COMPLEXITY = 0
MULTIPLICATION_COMPLEXITY = 1

def partitioned_encode_delay(parameters, partitions=None):
    '''Compute delay incurred by the encoding phase. Assumes a shifted exponential
    distribution.

    Args:

    parameters: System parameters.

    partitions: The number of partitions. If None, the value in parameters is
    used.

    Returns: The reduce delay.

    '''
    assert partitions is None or partitions % 1 == 0
    if partitions is None:
        partitions = parameters.num_partitions

    delay = stats.order_mean_shiftexp(
        parameters.num_servers,
        parameters.num_servers,
    )

    # Scale by encoding complexity per server
    delay *= block_diagonal_encoding_complexity(
        parameters,
        partitions=partitions,
    )

    # split the work over all servers
    delay /= parameters.num_servers

    return delay

def block_diagonal_encoding_complexity(parameters, partitions=None):
    assert partitions is None or partitions % 1 == 0
    if partitions is None:
        partitions = parameters.num_partitions
    multiplications = parameters.num_source_rows / partitions
    multiplications *= parameters.num_coded_rows * parameters.num_columns
    additions = parameters.num_source_rows / partitions - 1
    additions *= parameters.num_coded_rows * parameters.num_columns
    return MULTIPLICATION_COMPLEXITY * multiplications + ADDITION_COMPLEXITY * additions

def stragglerc_encode_delay(parameters):
    '''Compute reduce delay for a system using only straggler coding, i.e., using
    an erasure code to deal with stragglers but no coded multicasting.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''
    partitions = parameters.num_source_rows / parameters.q
    return partitioned_encode_delay(parameters, partitions=partitions)

def partitioned_reduce_delay(parameters, partitions=None):
    '''Compute delay incurred by the reduce phase. Assumes a shifted
    exponential distribution.

    Args:

    parameters: System parameters.

    partitions: The number of partitions. If None, the value in
    parameters is used.

    Returns: The reduce delay.

    '''
    assert partitions is None or (isinstance(partitions, int) and partitions > 0)
    if partitions is None:
        partitions = parameters.num_partitions

    delay = stats.order_mean_shiftexp(
        parameters.q,
        parameters.q,
    )

    # Scale by decoding complexity per server
    delay *= block_diagonal_decoding_complexity(
        parameters.num_coded_rows,
        1,
        1 - parameters.q / parameters.num_servers,
        partitions,
    )
    delay *= parameters.num_outputs / parameters.q
    return delay

def stragglerc_reduce_delay(parameters):
    '''Compute reduce delay for a system using only straggler coding,
    i.e., using an erasure code to deal with stragglers but no coded
    multicasting.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''
    # TODO: Evaluate partitioned_reduce_delay for correct T instead
    delay = stats.order_mean_shiftexp(parameters.q, parameters.q)

    # Scale by decoding complexity
    rows_per_server = parameters.num_source_rows / parameters.q
    delay *= block_diagonal_decoding_complexity(parameters.num_servers,
                                                rows_per_server,
                                                1 - parameters.q / parameters.num_servers,
                                                1)
    delay *= parameters.num_outputs / parameters.q
    return delay

def encoding_complexity_from_density(parameters=None, density=None):
    '''compute encoding complexity from the density of the encoding matrix

    args:

    parameters: system parameters

    density: average fraction of non-zero entries in the encoding matrix.

    returns: complexity of the encoding.

    '''
    assert 0 < density <= 1
    multiplications = parameters.num_source_rows * density
    multiplications *= parameters.num_coded_rows * parameters.num_columns
    additions = parameters.num_source_rows * density - 1
    additions *= parameters.num_coded_rows * parameters.num_columns
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY

def lt_reduce_delay(parameters):
    '''Compute delay incurred by the reduce phase using an LT code.
    Assumes a shifted exponential distribution.

    Args:

    parameters: System parameters.

    Returns: The reduce delay.

    '''

def rs_decoding_complexity(code_length, packet_size, erasure_prob):
    '''Compute the decoding complexity of Reed-Solomon codes

    Return the number of operations (additions and multiplications)
    required to decode a Reed-Solomon code over the packet erasure
    channel, and using the Berelkamp-Massey algorithm.

    Args:

    code_length: The length of the code in number of coded symbols.

    packet_size: The size of a packet in number of symbols.

    erasure_prob: The erasure probability of the packet erasure channel.

    Returns: The total complexity of decoding.

    '''
    additions = code_length * (erasure_prob * code_length - 1) * packet_size
    multiplications = pow(code_length, 2) * erasure_prob * packet_size;
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY

def block_diagonal_decoding_complexity(code_length, packet_size, erasure_prob, partitions):
    '''Compute the decoding complexity of a block-diagonal code

    Return the number of operations (additions and multiplications)
    required to decode a block-diagonal code over the packet erasure
    channel, and using the Berelkamp-Massey algorithm. This function
    considers the asymptotic case as the packet size approaches
    infinity.

    Args:

    code_length: The length of the code in number of coded symbols.

    packet_size: The size of a packet in number of symbols.

    erasure_prob: The erasure probability of the packet erasure channel.

    partitions: The number of block-diagonal code partitions.

    Returns: The total complexity of decoding.

    '''
    assert isinstance(code_length, int)
    assert isinstance(packet_size, int) or isinstance(packet_size, float)
    assert isinstance(erasure_prob, float)
    assert isinstance(partitions, int)
    assert code_length % partitions == 0, 'Partitions must divide code_length.'
    partition_length = code_length / partitions
    partition_complexity = rs_decoding_complexity(partition_length, packet_size, erasure_prob)
    return partition_complexity * partitions

def matrix_vector_complexity(rows=None, cols=None):
    '''Compute the complexity of matrix-vector multiplication

    Return the complexity of multiplying a matrix A with number of
    rows and columns as given in the argument by a vector x. The
    multiplication is done as A*x.

    Args:

    rows: The number of rows of the matrix.

    cols: The number of columns of the matrix.

    Returns: The complexity of the multiplication.

    '''
    additions = cols * rows - 1
    multiplications = cols * rows
    return additions * ADDITION_COMPLEXITY + multiplications * MULTIPLICATION_COMPLEXITY
<|endoftext|>


<commit_before>#!/usr/bin/env python

from misc_parser import parse_misc
from gh_parser import parse_gh
from asset_parser import parse_asset
from docs_parser import parse_docs
from deploy_setup import deploy_setup

import copy, os

parse_misc()

try: #check if the env already exists, it's better not to mess with existing stuff
    os.environ["wait_to_deploy"]
except KeyError:
    if os.environ["always_run"] == "True":
        os.environ["wait_to_deploy"] = "False"
    else:
        os.environ["wait_to_deploy"] = "True"
else:
    print "\"wait_to_deploy\" already exists as an env variable. change it to something else."
    exit(1)

rebuild_yml = {
    "language": ["objective-c"],
    "install": ["sh ./CI/unity_install.sh"],
    "script": ["sh ./CI/unity_build.sh"],
    "before_deploy": ["sh ./CI/pre_deploy.sh"],
    "deploy": [],
    "env": {
        "global": ["verbose=%s" % os.environ["verbose"]],
            ["packagename=%s" % os.environ["packagename"]],
            ["include_version=%s" % os.environ["include_version"]]
        ]
    }
}

if os.environ["TRAVIS_PULL_REQUEST"] == "false" and os.environ["TRAVIS_TAG"].strip():

    deploy_yml = copy.deepcopy(rebuild_yml) #yes it's slow, but there's no real substitute from what I could gather

    ini_docs = parse_docs()
    if ini_docs:
        deploy_yml["deploy"].append(ini_docs)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Github Pages accepted. -----------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    ini_gh = parse_gh()
    if ini_gh:
        deploy_yml["deploy"].append(ini_gh)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Github Releases accepted. --------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    ini_asset = parse_asset()
    if ini_asset:
        deploy_yml["deploy"].append(ini_asset)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Unity's Asset Store accepted. ----------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    if rebuild_yml == deploy_yml:
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Skipping deployment. ---------------------------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'
    else:
        os.environ["wait_to_deploy"] = "True"
        deploy_setup(os.environ["GH_TOKEN"], deploy_yml)

else:
    print '------------------------------------------------------------------------------------------------------------------------'
    print "Skipping deployment. ---------------------------------------------------------------------------------------------------"
    print '------------------------------------------------------------------------------------------------------------------------'

#you only get here if there is no deployment since deploy_setup calls exit on success.
os.system("sh ./CI/unity_install.sh") #move on to the build steps. This needs to be invoked like this to be able to pass the env vars created here.
os.system("sh ./CI/unity_build.sh")
<commit_msg>Debugging rebuild.<commit_after>#!/usr/bin/env python

from misc_parser import parse_misc
from gh_parser import parse_gh
from asset_parser import parse_asset
from docs_parser import parse_docs
from deploy_setup import deploy_setup

import copy, os

parse_misc()

try: #check if the env already exists, it's better not to mess with existing stuff
    os.environ["wait_to_deploy"]
except KeyError:
    if os.environ["always_run"] == "True":
        os.environ["wait_to_deploy"] = "False"
    else:
        os.environ["wait_to_deploy"] = "True"
else:
    print "\"wait_to_deploy\" already exists as an env variable. change it to something else."
    exit(1)

rebuild_yml = {
    "language": ["objective-c"],
    "install": ["sh ./CI/unity_install.sh"],
    "script": ["sh ./CI/unity_build.sh"],
    "before_deploy": ["sh ./CI/pre_deploy.sh"],
    "deploy": [],
    "env": {
        "global": [
            ["verbose=%s" % os.environ["verbose"]],
            ["packagename=%s" % os.environ["packaename"]],
            ["include_version=%s" % os.environ["include_version"]]
        ]
    }
}

if os.environ["TRAVIS_PULL_REQUEST"] == "false" and os.environ["TRAVIS_TAG"].strip():

    deploy_yml = copy.deepcopy(rebuild_yml) #yes it's slow, but there's no real substitute from what I could gather

    ini_docs = parse_docs()
    if ini_docs:
        deploy_yml["deploy"].append(ini_docs)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Github Pages accepted. -----------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    ini_gh = parse_gh()
    if ini_gh:
        deploy_yml["deploy"].append(ini_gh)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Github Releases accepted. --------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    ini_asset = parse_asset()
    if ini_asset:
        deploy_yml["deploy"].append(ini_asset)
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Deployment to Unity's Asset Store accepted. ----------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'

    if rebuild_yml == deploy_yml:
        print '------------------------------------------------------------------------------------------------------------------------'
        print "Skipping deployment. ---------------------------------------------------------------------------------------------------"
        print '------------------------------------------------------------------------------------------------------------------------'
    else:
        os.environ["wait_to_deploy"] = "True"
        deploy_setup(os.environ["GH_TOKEN"], deploy_yml)

else:
    print '------------------------------------------------------------------------------------------------------------------------'
    print "Skipping deployment. ---------------------------------------------------------------------------------------------------"
    print '------------------------------------------------------------------------------------------------------------------------'

#you only get here if there is no deployment since deploy_setup calls exit on success.
os.system("sh ./CI/unity_install.sh") #move on to the build steps. This needs to be invoked like this to be able to pass the env vars created here.
os.system("sh ./CI/unity_build.sh")
<|endoftext|>


<commit_before>import os

if __name__ == "__main__":
    # NOTE: on posix systems, this *has* to be here and in the
    # `__name__ == "__main__"` clause to run XGBoost in parallel processes
    # using fork, if XGBoost was built with OpenMP support. Otherwise, if you
    # build XGBoost without OpenMP support, you can use fork, which is the
    # default backend for joblib, and omit this.
    try:
        from multiprocessing import set_start_method
    except ImportError:
        raise ImportError("Unable to import multiprocessing.set_start_method."
                          " This example only runs on Python 3.4")
    set_start_method("forkserver")

    import numpy as np
    from sklearn.grid_search import GridSearchCV
    from sklearn.datasets import load_boston
    import xgboost as xgb

    rng = np.random.RandomState(31337)

    print("Parallel Parameter optimization")
    boston = load_boston()

    os.environ["OMP_NUM_THREADS"] = "2"  # or to whatever you want
    y = boston['target']
    X = boston['data']
    xgb_model = xgb.XGBRegressor()
    clf = GridSearchCV(xgb_model, {'max_depth': [2, 4, 6],
                                   'n_estimators': [50, 100, 200]}, verbose=1,
                       n_jobs=2)
    clf.fit(X, y)
    print(clf.best_score_)
    print(clf.best_params_)
<commit_msg>Updated sklearn_parallel.py for soon-to-be-deprecated modules (#2134)<commit_after>import os

if __name__ == "__main__":
    # NOTE: on posix systems, this *has* to be here and in the
    # `__name__ == "__main__"` clause to run XGBoost in parallel processes
    # using fork, if XGBoost was built with OpenMP support. Otherwise, if you
    # build XGBoost without OpenMP support, you can use fork, which is the
    # default backend for joblib, and omit this.
    try:
        from multiprocessing import set_start_method
    except ImportError:
        raise ImportError("Unable to import multiprocessing.set_start_method."
                          " This example only runs on Python 3.4")
    set_start_method("forkserver")

    import numpy as np
    from sklearn.model_selection import GridSearchCV
    from sklearn.datasets import load_boston
    import xgboost as xgb

    rng = np.random.RandomState(31337)

    print("Parallel Parameter optimization")
    boston = load_boston()

    os.environ["OMP_NUM_THREADS"] = "2"  # or to whatever you want
    y = boston['target']
    X = boston['data']
    xgb_model = xgb.XGBRegressor()
    clf = GridSearchCV(xgb_model, {'max_depth': [2, 4, 6],
                                   'n_estimators': [50, 100, 200]}, verbose=1,
                       n_jobs=2)
    clf.fit(X, y)
    print(clf.best_score_)
    print(clf.best_params_)
<|endoftext|>


<commit_before>from math import pi
from bokeh.plotting import Figure
from bokeh.models import Legend
from bokeh.models import DatetimeTickFormatter

def init_time_series_plot(hover):
    """
    Defaults for time series plot
    """
    plot = Figure(
        plot_height=400,
        plot_width=900,
        responsive=True,
        tools="xpan,xwheel_zoom,xbox_zoom,reset,tap",
        x_axis_type='datetime',
        min_border_top=0,
        min_border_right=10,
        min_border_bottom=50,
        min_border_left=50,
        outline_line_color=None,
    )
    plot.add_tools(hover)
    plot.x_range.follow = "end"
    plot.x_range.range_padding = 0
    plot.xaxis.axis_label = "Date"
    plot.xaxis.major_label_orientation = pi/4
    plot.xaxis.formatter = DatetimeTickFormatter(formats=dict(
        hours=["%d %B"],
        days=["%d %B"],
        months=["%d %B"],
        years=["%d %B"],
    ))
    return plot

def init_legend(legends):
    return Legend(
        legends=legends,
        location='top_right',
        border_line_color=None,
        background_fill_alpha=0.7
    )
<commit_msg>Fixes after update to bokeh 0.12.1<commit_after>from bokeh.plotting import Figure
from bokeh.models import Legend

def init_time_series_plot(hover):
    """
    Defaults for time series plot
    """
    plot = Figure(
        plot_height=400,
        plot_width=900,
        sizing_mode="scale_width",
        toolbar_location="above",
        tools="xpan,xwheel_zoom,xbox_zoom,reset,tap",
        x_axis_type="datetime",
        min_border_top=0,
        min_border_right=10,
        min_border_bottom=50,
        min_border_left=50,
        outline_line_color=None,
    )
    plot.add_tools(hover)
    plot.x_range.follow = "end"
    plot.x_range.range_padding = 0
    plot.xaxis.axis_label = "Date"

    return plot

def init_legend(legends):
    return Legend(
        legends=legends,
        location='top_right',
        border_line_color=None,
        background_fill_alpha=0.7
    )
<|endoftext|>


<commit_before>import warnings
import functools
import sys
import numpy as np

import six

from ._warnings import all_warnings

__all__ = ['deprecated', 'get_bound_method_class', 'all_warnings',
           'safe_as_int', 'assert_nD']

class skimage_deprecation(Warning):
    """Create our own deprecation class, since Python >= 2.7
    silences deprecations by default.

    """
    pass

class deprecated(object):
    """Decorator to mark deprecated functions with warning.

    Adapted from <http://wiki.python.org/moin/PythonDecoratorLibrary>.

    Parameters
    ----------
    alt_func : str
        If given, tell user what function to use instead.
    behavior : {'warn', 'raise'}
        Behavior during call to deprecated function: 'warn' = warn user that
        function is deprecated; 'raise' = raise error.
    """

    def __init__(self, alt_func=None, behavior='warn'):
        self.alt_func = alt_func
        self.behavior = behavior

    def __call__(self, func):

        alt_msg = ''
        if self.alt_func is not None:
            alt_msg = ' Use ``%s`` instead.' % self.alt_func

        msg = 'Call to deprecated function ``%s``.' % func.__name__
        msg += alt_msg

        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            if self.behavior == 'warn':
                func_code = six.get_function_code(func)
                warnings.simplefilter('always', skimage_deprecation)
                warnings.warn_explicit(msg,
                    category=skimage_deprecation,
                    filename=func_code.co_filename,
                    lineno=func_code.co_firstlineno + 1)
            elif self.behavior == 'raise':
                raise skimage_deprecation(msg)
            return func(*args, **kwargs)

        # modify doc string to display deprecation warning
        doc = '**Deprecated function**.' + alt_msg
        if wrapped.__doc__ is None:
            wrapped.__doc__ = doc
        else:
            wrapped.__doc__ = doc + '\n\n    ' + wrapped.__doc__

        return wrapped

def get_bound_method_class(m):
    """Return the class for a bound method.

    """
    return m.im_class if sys.version < '3' else m.__self__.__class__

def safe_as_int(val, atol=1e-3):
    """
    Attempt to safely cast values to integer format.

    Parameters
    ----------
    val : scalar or iterable of scalars
        Number or container of numbers which are intended to be interpreted as
        integers, e.g., for indexing purposes, but which may not carry integer
        type.
    atol : float
        Absolute tolerance away from nearest integer to consider values in
        ``val`` functionally integers.

    Returns
    -------
    val_int : NumPy scalar or ndarray of dtype `np.int64`
        Returns the input value(s) coerced to dtype `np.int64` assuming all
        were within ``atol`` of the nearest integer.

    Notes
    -----
    This operation calculates ``val`` modulo 1, which returns the mantissa of
    all values. Then all mantissas greater than 0.5 are subtracted from one.
    Finally, the absolute tolerance from zero is calculated. If it is less
    than ``atol`` for all value(s) in ``val``, they are rounded and returned
    in an integer array. Or, if ``val`` was a scalar, a NumPy scalar type is
    returned.

    If any value(s) are outside the specified tolerance, an informative error
    is raised.

    Examples
    --------
    >>> _safe_as_int(7.0)
    7

    >>> _safe_as_int([9, 4, 2.9999999999])
    array([9, 4, 3], dtype=int32)

    >>> _safe_as_int(53.01)
    Traceback (most recent call last):
        ...
    ValueError: Integer argument required but received 53.1, check inputs.

    >>> _safe_as_int(53.01, atol=0.01)
    53

    """
    mod = np.asarray(val) % 1                # Extract mantissa

    # Check for and subtract any mod values > 0.5 from 1
    if mod.ndim == 0:                        # Scalar input, cannot be indexed
        if mod > 0.5:
            mod = 1 - mod
    else:                                    # Iterable input, now ndarray
        mod[mod > 0.5] = 1 - mod[mod > 0.5]  # Test on each side of nearest int

    try:
        np.testing.assert_allclose(mod, 0, atol=atol)
    except AssertionError:
        raise ValueError("Integer argument required but received "
                         "{0}, check inputs.".format(val))

    return np.round(val).astype(np.int64)

def assert_nD(array, arg_name='image', ndim=2):
    array = np.asanyarray(array)
    msg = "The parameter `%s` must be a %s-dimensional array"
    if isinstance(ndim, int):
        ndim = [ndim]
    if not array.ndim in ndim:
        msg = "The parameter `%s` must be a %s-dimensional array"
        raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim])))
<commit_msg>Add docstring to assert_nD<commit_after>import warnings
import functools
import sys
import numpy as np

import six

from ._warnings import all_warnings

__all__ = ['deprecated', 'get_bound_method_class', 'all_warnings',
           'safe_as_int', 'assert_nD']

class skimage_deprecation(Warning):
    """Create our own deprecation class, since Python >= 2.7
    silences deprecations by default.

    """
    pass

class deprecated(object):
    """Decorator to mark deprecated functions with warning.

    Adapted from <http://wiki.python.org/moin/PythonDecoratorLibrary>.

    Parameters
    ----------
    alt_func : str
        If given, tell user what function to use instead.
    behavior : {'warn', 'raise'}
        Behavior during call to deprecated function: 'warn' = warn user that
        function is deprecated; 'raise' = raise error.
    """

    def __init__(self, alt_func=None, behavior='warn'):
        self.alt_func = alt_func
        self.behavior = behavior

    def __call__(self, func):

        alt_msg = ''
        if self.alt_func is not None:
            alt_msg = ' Use ``%s`` instead.' % self.alt_func

        msg = 'Call to deprecated function ``%s``.' % func.__name__
        msg += alt_msg

        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            if self.behavior == 'warn':
                func_code = six.get_function_code(func)
                warnings.simplefilter('always', skimage_deprecation)
                warnings.warn_explicit(msg,
                    category=skimage_deprecation,
                    filename=func_code.co_filename,
                    lineno=func_code.co_firstlineno + 1)
            elif self.behavior == 'raise':
                raise skimage_deprecation(msg)
            return func(*args, **kwargs)

        # modify doc string to display deprecation warning
        doc = '**Deprecated function**.' + alt_msg
        if wrapped.__doc__ is None:
            wrapped.__doc__ = doc
        else:
            wrapped.__doc__ = doc + '\n\n    ' + wrapped.__doc__

        return wrapped

def get_bound_method_class(m):
    """Return the class for a bound method.

    """
    return m.im_class if sys.version < '3' else m.__self__.__class__

def safe_as_int(val, atol=1e-3):
    """
    Attempt to safely cast values to integer format.

    Parameters
    ----------
    val : scalar or iterable of scalars
        Number or container of numbers which are intended to be interpreted as
        integers, e.g., for indexing purposes, but which may not carry integer
        type.
    atol : float
        Absolute tolerance away from nearest integer to consider values in
        ``val`` functionally integers.

    Returns
    -------
    val_int : NumPy scalar or ndarray of dtype `np.int64`
        Returns the input value(s) coerced to dtype `np.int64` assuming all
        were within ``atol`` of the nearest integer.

    Notes
    -----
    This operation calculates ``val`` modulo 1, which returns the mantissa of
    all values. Then all mantissas greater than 0.5 are subtracted from one.
    Finally, the absolute tolerance from zero is calculated. If it is less
    than ``atol`` for all value(s) in ``val``, they are rounded and returned
    in an integer array. Or, if ``val`` was a scalar, a NumPy scalar type is
    returned.

    If any value(s) are outside the specified tolerance, an informative error
    is raised.

    Examples
    --------
    >>> _safe_as_int(7.0)
    7

    >>> _safe_as_int([9, 4, 2.9999999999])
    array([9, 4, 3], dtype=int32)

    >>> _safe_as_int(53.01)
    Traceback (most recent call last):
        ...
    ValueError: Integer argument required but received 53.1, check inputs.

    >>> _safe_as_int(53.01, atol=0.01)
    53

    """
    mod = np.asarray(val) % 1                # Extract mantissa

    # Check for and subtract any mod values > 0.5 from 1
    if mod.ndim == 0:                        # Scalar input, cannot be indexed
        if mod > 0.5:
            mod = 1 - mod
    else:                                    # Iterable input, now ndarray
        mod[mod > 0.5] = 1 - mod[mod > 0.5]  # Test on each side of nearest int

    try:
        np.testing.assert_allclose(mod, 0, atol=atol)
    except AssertionError:
        raise ValueError("Integer argument required but received "
                         "{0}, check inputs.".format(val))

    return np.round(val).astype(np.int64)

def assert_nD(array, arg_name='image', ndim=2):
    """
    Verify an arry meets the desired ndims.

    Parameters
    ----------
    array : array-like
        Input array to be validated
    arg_name : str
        The name of the array in the original function.
    ndim : int or array-like
        Allowable ndim or ndims for the array.
    """
    array = np.asanyarray(array)
    msg = "The parameter `%s` must be a %s-dimensional array"
    if isinstance(ndim, int):
        ndim = [ndim]
    if not array.ndim in ndim:
        msg = "The parameter `%s` must be a %s-dimensional array"
        raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim])))
<|endoftext|>