<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function

import ctypes
import os
import sys

import numpy as np
import six

import acq4.util.clibrary as clibrary
from .base import NIDAQError

dtypes = {  # for converting numpy dtypes to nidaq type strings
    "<f8": "F64",
    "<i2": "I16",
    "<i4": "I32",
    "<u2": "U16",
    "<u4": "U32",
    "|u1": "U8",
    # float64.descr[0][1]: 'F64',
    # int16.descr[0][1]: 'I16',
    # int32.descr[0][1]: 'I32',
    # uint16.descr[0][1]: 'U16',
    # uint32.descr[0][1]: 'U32',
    # uint8.descr[0][1]: 'U8'
}
for d in list(dtypes.keys()):
    dtypes[dtypes[d]] = d
    dtypes[np.dtype(d)] = dtypes[d]

def init():
    modDir = os.path.dirname(__file__)
    headerFiles = [os.path.join(modDir, "NIDAQmx.h")]
    # xmlFiles = [os.path.join(os.path.dirname(__file__), "NIDAQmx.xml")]
    # defs = cheader.getDefs(headerFiles)

    # cache files appear to be platform-dependent due to some pickling bug..
    cacheFile = os.path.join(modDir, "NIDAQmx_headers_%s.cache" % sys.platform)

    global DEFS
    DEFS = clibrary.CParser(headerFiles, cache=cacheFile, types={"__int64": "long long"}, verbose=False)
    global LIB
    LIB = clibrary.CLibrary(ctypes.windll.nicaiu, DEFS, prefix=["DAQmx", "DAQmx_"])

    global NIDAQ
    NIDAQ = _NIDAQ()
    # for k in defs:
    #     setattr(sys.modules[__name__], re.sub('^DAQmx_?', '', k), defs[k])
    # NIDAQ.functions = cheader.getFuncs(xmlFiles)
    # time.clock()

class _NIDAQ:
    NIDAQ_CREATED = False

    def __init__(self):
        if _NIDAQ.NIDAQ_CREATED:
            raise Exception("Will not create another nidaq instance--use the pre-existing NIDAQ object.")
        self.devices = {}

        # cached tasks used for scalar AO/AI operations
        # (this shaves a few ms from the cost of reading/writing scalars)
        self._scalarTasks = {}

        # :TODO: initialize the driver
        _NIDAQ.NIDAQ_CREATED = True

    def __repr__(self):
        return "<niDAQmx driver wrapper>"

    def listDevices(self):
        return self.GetSysDevNames().split(b", ")

    def __getattr__(self, attr):
        try:
            return LIB("values", "DAQmx_" + attr)
        except NameError:
            LIB("functions", "DAQmx" + attr)  # try again for NameError
            return lambda *args: self.call(attr, *args)

    def call(self, func, *args):
        func = "DAQmx" + func
        fn = LIB("functions", func)
        retType, argSig = fn.sig

        returnValue = None
        if func[:8] == "DAQmxGet":  # byref arguments will be handled automatically.
            # functions that return char* can be called with a null pointer to get the size of the buffer needed.
            if (argSig[-2][1] == ["char", "*"] or argSig[-2][1] == ["char", [-1]]) and argSig[-1][0] == "bufferSize":
                returnValue = argSig[-2][0]
                extra = {returnValue: None, "bufferSize": 0}
                buffSize = fn(*args, **extra)()
                ret = ctypes.create_string_buffer(b"\0" * buffSize)
                args += (ret, buffSize)

        # Python 3 requires bytes instead of str arguments here
        args = list(args)
        for i, arg in enumerate(args):
            if isinstance(arg, str):
                args[i] = arg.encode()

        # if there is a 'reserved' argument, it MUST be 0 (don't let clibrary try to fill it for us)
        if argSig[-1][0] == "reserved":
            ret = fn(*args, reserved=None)
        else:
            ret = fn(*args)

        errCode = ret()

        if errCode < 0:
            msg = "NiDAQ Error while running function '%s%s':\n%s" % (func, str(args), self.error())
            raise NIDAQError(errCode, msg)
        elif errCode > 0:
            print("NiDAQ Warning while running function '%s%s'" % (func, str(args)))
            print(self.error(errCode))

        if returnValue is not None:  # If a specific return value was indicated, return it now
            return ret[returnValue]

        # otherwise, try to guess which values should be returned
        vals = ret.auto()
        if len(vals) == 1:
            return vals[0]
        elif len(vals) > 1:
            return vals

    def _call(self, func, *args, **kargs):
        try:
            return getattr(self.nidaq, func)(*args, **kargs)
        except:
            print(func, args)
            raise

    def error(self, errCode=None):
        """Return a string with error information. If errCode is None, then the currently 'active' error will be used."""
        if errCode is None:
            err = self.GetExtendedErrorInfo().decode("ascii")
        else:
            err = self.GetErrorString(errCode).decode("ascii")
        err.replace("\\n", "\n")
        return err

    def __del__(self):
        self.__class__.NIDAQ_CREATED = False

    def createTask(self, name=""):
        return Task(self, name)

    def createSuperTask(self):
        from . import SuperTask

        return SuperTask.SuperTask(self)

    def interpretMode(self, mode):
        modes = {
            "rse": LIB.Val_RSE,
            "nrse": LIB.Val_NRSE,
            "diff": LIB.Val_Diff,
            "chanperline": LIB.Val_ChanPerLine,
            "chanforalllines": LIB.Val_ChanForAllLines,
        }
        if isinstance(mode, six.string_types):
            mode = mode.lower()
            mode = modes.get(mode, None)
        return mode

    def writeAnalogSample(self, chan, value, vRange=(-10.0, 10.0), timeout=10.0):
        """Set the value of an AO port"""
        key = ("ao", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateAOVoltageChan(chan, "", vRange[0], vRange[1], LIB.Val_Volts, None)
            self._scalarTasks[key] = t
        t.WriteAnalogScalarF64(True, timeout, value)

    def readAnalogSample(self, chan, mode=None, vRange=(-10.0, 10.0), timeout=10.0):
        """Get the value of an AI port"""
        if mode is None:
            mode = LIB.Val_Cfg_Default
        else:
            mode = self.interpretMode(mode)

        key = ("ai", mode, chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateAIVoltageChan(chan, "", mode, vRange[0], vRange[1], LIB.Val_Volts, None)
            self._scalarTasks[key] = t
        return t.ReadAnalogScalarF64(timeout)

    def writeDigitalSample(self, chan, value, timeout=10.0):
        """Set the value of an AO or DO port"""
        key = ("do", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateDOChan(chan, "", LIB.Val_ChanForAllLines)
            self._scalarTasks[key] = t
        t.WriteDigitalScalarU32(True, timeout, value)

    def readDigitalSample(self, chan, timeout=10.0):
        """Get the value of a DI port"""
        key = ("di", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateDIChan(chan, "", LIB.Val_ChanForAllLines)
            self._scalarTasks[key] = t
        return t.ReadDigitalScalarU32(timeout)

    def listAIChannels(self, dev=None):
        return self.GetDevAIPhysicalChans(dev).split(", ")

    def listAOChannels(self, dev):
        return self.GetDevAOPhysicalChans(dev).split(", ")

    def listDILines(self, dev):
        return self.GetDevDILines(dev).split(", ")

    def listDIPorts(self, dev):
        return self.GetDevDIPorts(dev).split(", ")

    def listDOLines(self, dev):
        return self.GetDevDOLines(dev).split(", ")

    def listDOPorts(self, dev):
        return self.GetDevDOPorts(dev).split(", ")

init()

chTypes = {
    LIB.Val_AI: "AI",
    LIB.Val_AO: "AO",
    LIB.Val_DI: "DI",
    LIB.Val_DO: "DO",
    LIB.Val_CI: "CI",
    LIB.Val_CO: "CO",
}

class Task:
    # TaskHandle = None

    def __init__(self, nidaq, taskName=""):
        self.nidaq = nidaq
        self.handle = self.nidaq.CreateTask(taskName)

    def __del__(self):
        self.nidaq.ClearTask(self.handle)

    def __getattr__(self, attr):
        func = getattr(self.nidaq, attr)
        return lambda *args: func(self.handle, *args)

    def __repr__(self):
        return "<Task: %s>" % str(self.GetTaskChannels())

    def start(self):
        self.StartTask()

    def stop(self):
        self.StopTask()

    def isDone(self):
        return self.IsTaskDone()

    def read(self, samples=None, timeout=10.0, dtype=None):
        # reqSamps = samples
        # if samples is None:
        #    samples = self.GetSampQuantSampPerChan()
        #    reqSamps = -1
        if samples is None:
            samples = self.GetSampQuantSampPerChan()
        reqSamps = samples

        numChans = self.GetTaskNumChans()

        shape = (numChans, samples)
        # print "Shape: ", shape

        # Determine the default dtype based on the task type
        tt = self.taskType()
        if dtype is None:
            if tt in [LIB.Val_AI, LIB.Val_AO]:
                dtype = np.float64
            elif tt in [LIB.Val_DI, LIB.Val_DO]:
                dtype = np.uint32  # uint8 / 16 might be sufficient, but don't seem to work anyway.
            else:
                raise Exception("No default dtype for %s tasks." % chTypes[tt])

        buf = np.empty(shape, dtype=dtype)
        # samplesRead = ctypes.c_long()

        # Determine the correct function name to call based on the dtype requested
        fName = "Read"
        if tt == LIB.Val_AI:
            if dtype == np.float64:
                fName += "Analog"
            elif dtype in [np.int16, np.uint16, np.int32, np.uint32]:
                fName += "Binary"
            else:
                raise Exception(
                    "dtype %s not allowed for AI channels (must be float64, int16, uint16, int32, or uint32)"
                    % str(dtype)
                )
        elif tt == LIB.Val_DI:
            if dtype in [np.uint8, np.uint16, np.uint32]:
                fName += "Digital"
            else:
                raise Exception("dtype %s not allowed for DI channels (must be uint8, uint16, or uint32)" % str(dtype))
        elif tt == LIB.Val_CI:
            fName += "Counter"
        else:
            raise Exception("read() not allowed for this task type (%s)" % chTypes[tt])

        fName += dtypes[np.dtype(dtype).descr[0][1]]

        self.SetReadRelativeTo(LIB.Val_FirstSample)
        self.SetReadOffset(0)

        # buf.ctypes is a c_void_p, but the function requires a specific pointer type so we are forced to recast the pointer:
        fn = LIB("functions", fName)
        cbuf = ctypes.cast(buf.ctypes, fn.argCType("readArray"))

        nPts = getattr(self, fName)(reqSamps, timeout, LIB.Val_GroupByChannel, cbuf, buf.size)
        return buf, nPts

    def write(self, data, timeout=10.0):
        numChans = self.GetTaskNumChans()
        # samplesWritten = c_long()

        # Determine the correct write function to call based on dtype and task type
        fName = "Write"
        tt = self.taskType()
        if tt == LIB.Val_AO:
            if data.dtype == np.float64:
                fName += "Analog"
            elif data.dtype in [np.int16, np.uint16]:
                fName += "Binary"
            else:
                raise Exception(
                    "dtype %s not allowed for AO channels (must be float64, int16, or uint16)" % str(data.dtype)
                )
        elif tt == LIB.Val_DO:
            if data.dtype in [np.uint8, np.uint16, np.uint32]:
                fName += "Digital"
            else:
                raise Exception(
                    "dtype %s not allowed for DO channels (must be uint8, uint16, or uint32)" % str(data.dtype)
                )
        else:
            raise Exception("write() not implemented for this task type (%s)" % chTypes[tt])

        fName += dtypes[data.dtype.descr[0][1]]

        # buf.ctypes is a c_void_p, but the function requires a specific pointer type so we are forced to recast the pointer:
        fn = LIB("functions", fName)
        cbuf = ctypes.cast(data.ctypes, fn.argCType("writeArray"))

        nPts = getattr(self, fName)(data.size / numChans, False, timeout, LIB.Val_GroupByChannel, cbuf)
        return nPts

    def absChannelName(self, n):
        parts = n.lstrip("/").split("/")
        devs = self.GetTaskDevices().split(", ")
        if parts[0] not in devs:
            if len(devs) != 1:
                raise Exception("Cannot determine device to prepend on channel '%s'" % n)
            parts = [devs[0]] + parts
        return "/" + "/".join(parts)

    def taskType(self):
        # print "taskType:"
        ch = self.GetTaskChannels().split(", ")
        # print ch
        ch = self.absChannelName(ch[0])
        # print "First task channel:", ch
        return self.GetChanType(ch)

    def isInputTask(self):
        return self.taskType() in [LIB.Val_AI, LIB.Val_DI]

    def isOutputTask(self):
        return self.taskType() in [LIB.Val_AO, LIB.Val_DO]
<commit_msg>import PyDAQmx and use it as much as seems obvious<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function

import ctypes

import numpy as np
import six
import PyDAQmx

from .base import NIDAQError

dtypes = {  # for converting numpy dtypes to nidaq type strings
    "<f8": "F64",
    "<i2": "I16",
    "<i4": "I32",
    "<u2": "U16",
    "<u4": "U32",
    "|u1": "U8",
    # float64.descr[0][1]: 'F64',
    # int16.descr[0][1]: 'I16',
    # int32.descr[0][1]: 'I32',
    # uint16.descr[0][1]: 'U16',
    # uint32.descr[0][1]: 'U32',
    # uint8.descr[0][1]: 'U8'
}
for d in list(dtypes.keys()):
    dtypes[dtypes[d]] = d
    dtypes[np.dtype(d)] = dtypes[d]

def init():
    global NIDAQ
    NIDAQ = _NIDAQ()

class _NIDAQ:
    NIDAQ_CREATED = False

    def __init__(self):
        if _NIDAQ.NIDAQ_CREATED:
            raise Exception("Will not create another nidaq instance--use the pre-existing NIDAQ object.")
        self.devices = {}

        # cached tasks used for scalar AO/AI operations
        # (this shaves a few ms from the cost of reading/writing scalars)
        self._scalarTasks = {}

        # :TODO: initialize the driver
        _NIDAQ.NIDAQ_CREATED = True

    def __repr__(self):
        return "<niDAQmx driver wrapper>"

    def listDevices(self):
        return self.GetSysDevNames().split(b", ")

    def __getattr__(self, attr):
        if hasattr(PyDAQmx, attr):
            return lambda *args: getattr(PyDAQmx, attr)(*args)
        else:
            raise NameError("{} not found among DAQmx constants or functions")

    def call(self, func, *args):
        return getattr(PyDAQmx, func)(*args)
        # fn = getattr(PyDAQmx, func)
        # retType, argSig = fn.sig
        #
        # returnValue = None
        # if func[:8] == "DAQmxGet":  # byref arguments will be handled automatically.
        #     # functions that return char* can be called with a null pointer to get the size of the buffer needed.
        #     if (argSig[-2][1] == ["char", "*"] or argSig[-2][1] == ["char", [-1]]) and argSig[-1][0] == "bufferSize":
        #         returnValue = argSig[-2][0]
        #         extra = {returnValue: None, "bufferSize": 0}
        #         buffSize = fn(*args, **extra)()
        #         ret = ctypes.create_string_buffer(b"\0" * buffSize)
        #         args += (ret, buffSize)
        #
        # # Python 3 requires bytes instead of str arguments here
        # args = list(args)
        # for i, arg in enumerate(args):
        #     if isinstance(arg, str):
        #         args[i] = arg.encode()
        #
        # # if there is a 'reserved' argument, it MUST be 0 (don't let clibrary try to fill it for us)
        # if argSig[-1][0] == "reserved":
        #     ret = fn(*args, reserved=None)
        # else:
        #     ret = fn(*args)
        #
        # errCode = ret()
        #
        # if errCode < 0:
        #     msg = "NiDAQ Error while running function '%s%s':\n%s" % (func, str(args), self.error())
        #     raise NIDAQError(errCode, msg)
        # elif errCode > 0:
        #     print("NiDAQ Warning while running function '%s%s'" % (func, str(args)))
        #     print(self.error(errCode))
        #
        # if returnValue is not None:  # If a specific return value was indicated, return it now
        #     return ret[returnValue]
        #
        # # otherwise, try to guess which values should be returned
        # vals = ret.auto()
        # if len(vals) == 1:
        #     return vals[0]
        # elif len(vals) > 1:
        #     return vals

    def _call(self, func, *args, **kargs):
        try:
            return getattr(self.nidaq, func)(*args, **kargs)
        except:
            print(func, args)
            raise

    def error(self, errCode=None):
        """Return a string with error information. If errCode is None, then the currently 'active' error will be used."""
        if errCode is None:
            err = self.GetExtendedErrorInfo().decode("ascii")
        else:
            err = self.GetErrorString(errCode).decode("ascii")
        err.replace("\\n", "\n")
        return err

    def __del__(self):
        self.__class__.NIDAQ_CREATED = False

    def createTask(self, name=""):
        return Task(self, name)

    def createSuperTask(self):
        from . import SuperTask

        return SuperTask.SuperTask(self)

    def interpretMode(self, mode):
        modes = {
            "rse": PyDAQmx.Val_RSE,
            "nrse": PyDAQmx.Val_NRSE,
            "diff": PyDAQmx.Val_Diff,
            "chanperline": PyDAQmx.Val_ChanPerLine,
            "chanforalllines": PyDAQmx.Val_ChanForAllLines,
        }
        if isinstance(mode, six.string_types):
            mode = mode.lower()
            mode = modes.get(mode, None)
        return mode

    def writeAnalogSample(self, chan, value, vRange=(-10.0, 10.0), timeout=10.0):
        """Set the value of an AO port"""
        key = ("ao", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateAOVoltageChan(chan, "", vRange[0], vRange[1], PyDAQmx.Val_Volts, None)
            self._scalarTasks[key] = t
        t.WriteAnalogScalarF64(True, timeout, value)

    def readAnalogSample(self, chan, mode=None, vRange=(-10.0, 10.0), timeout=10.0):
        """Get the value of an AI port"""
        if mode is None:
            mode = PyDAQmx.Val_Cfg_Default
        else:
            mode = self.interpretMode(mode)

        key = ("ai", mode, chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateAIVoltageChan(chan, "", mode, vRange[0], vRange[1], PyDAQmx.Val_Volts, None)
            self._scalarTasks[key] = t
        return t.ReadAnalogScalarF64(timeout)

    def writeDigitalSample(self, chan, value, timeout=10.0):
        """Set the value of an AO or DO port"""
        key = ("do", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateDOChan(chan, "", PyDAQmx.Val_ChanForAllLines)
            self._scalarTasks[key] = t
        t.WriteDigitalScalarU32(True, timeout, value)

    def readDigitalSample(self, chan, timeout=10.0):
        """Get the value of a DI port"""
        key = ("di", chan)
        t = self._scalarTasks.get(key, None)
        if t is None:
            t = self.createTask()
            t.CreateDIChan(chan, "", PyDAQmx.Val_ChanForAllLines)
            self._scalarTasks[key] = t
        return t.ReadDigitalScalarU32(timeout)

    def listAIChannels(self, dev=None):
        return self.GetDevAIPhysicalChans(dev).split(", ")

    def listAOChannels(self, dev):
        return self.GetDevAOPhysicalChans(dev).split(", ")

    def listDILines(self, dev):
        return self.GetDevDILines(dev).split(", ")

    def listDIPorts(self, dev):
        return self.GetDevDIPorts(dev).split(", ")

    def listDOLines(self, dev):
        return self.GetDevDOLines(dev).split(", ")

    def listDOPorts(self, dev):
        return self.GetDevDOPorts(dev).split(", ")

init()

chTypes = {
    PyDAQmx.Val_AI: "AI",
    PyDAQmx.Val_AO: "AO",
    PyDAQmx.Val_DI: "DI",
    PyDAQmx.Val_DO: "DO",
    PyDAQmx.Val_CI: "CI",
    PyDAQmx.Val_CO: "CO",
}

class Task:
    # TaskHandle = None

    def __init__(self, nidaq, taskName=""):
        self.nidaq = nidaq
        self.handle = self.nidaq.CreateTask(taskName)

    def __del__(self):
        self.nidaq.ClearTask(self.handle)

    def __getattr__(self, attr):
        func = getattr(self.nidaq, attr)
        return lambda *args: func(self.handle, *args)

    def __repr__(self):
        return "<Task: %s>" % str(self.GetTaskChannels())

    def start(self):
        self.StartTask()

    def stop(self):
        self.StopTask()

    def isDone(self):
        return self.IsTaskDone()

    def read(self, samples=None, timeout=10.0, dtype=None):
        # reqSamps = samples
        # if samples is None:
        #    samples = self.GetSampQuantSampPerChan()
        #    reqSamps = -1
        if samples is None:
            samples = self.GetSampQuantSampPerChan()
        reqSamps = samples

        numChans = self.GetTaskNumChans()

        shape = (numChans, samples)
        # print "Shape: ", shape

        # Determine the default dtype based on the task type
        tt = self.taskType()
        if dtype is None:
            if tt in [PyDAQmx.Val_AI, PyDAQmx.Val_AO]:
                dtype = np.float64
            elif tt in [PyDAQmx.Val_DI, PyDAQmx.Val_DO]:
                dtype = np.uint32  # uint8 / 16 might be sufficient, but don't seem to work anyway.
            else:
                raise Exception("No default dtype for %s tasks." % chTypes[tt])

        buf = np.empty(shape, dtype=dtype)
        # samplesRead = ctypes.c_long()

        # Determine the correct function name to call based on the dtype requested
        fName = "Read"
        if tt == PyDAQmx.Val_AI:
            if dtype == np.float64:
                fName += "Analog"
            elif dtype in [np.int16, np.uint16, np.int32, np.uint32]:
                fName += "Binary"
            else:
                raise Exception(
                    "dtype %s not allowed for AI channels (must be float64, int16, uint16, int32, or uint32)"
                    % str(dtype)
                )
        elif tt == PyDAQmx.Val_DI:
            if dtype in [np.uint8, np.uint16, np.uint32]:
                fName += "Digital"
            else:
                raise Exception("dtype %s not allowed for DI channels (must be uint8, uint16, or uint32)" % str(dtype))
        elif tt == PyDAQmx.Val_CI:
            fName += "Counter"
        else:
            raise Exception("read() not allowed for this task type (%s)" % chTypes[tt])

        fName += dtypes[np.dtype(dtype).descr[0][1]]

        self.SetReadRelativeTo(PyDAQmx.Val_FirstSample)
        self.SetReadOffset(0)

        # buf.ctypes is a c_void_p, but the function requires a specific pointer type so we are forced to recast the pointer:
        fn = getattr(PyDAQmx, fName)
        cbuf = ctypes.cast(buf.ctypes, fn.argCType("readArray"))

        nPts = getattr(self, fName)(reqSamps, timeout, PyDAQmx.Val_GroupByChannel, cbuf, buf.size)
        return buf, nPts

    def write(self, data, timeout=10.0):
        numChans = self.GetTaskNumChans()
        # samplesWritten = c_long()

        # Determine the correct write function to call based on dtype and task type
        fName = "Write"
        tt = self.taskType()
        if tt == PyDAQmx.Val_AO:
            if data.dtype == np.float64:
                fName += "Analog"
            elif data.dtype in [np.int16, np.uint16]:
                fName += "Binary"
            else:
                raise Exception(
                    "dtype %s not allowed for AO channels (must be float64, int16, or uint16)" % str(data.dtype)
                )
        elif tt == PyDAQmx.Val_DO:
            if data.dtype in [np.uint8, np.uint16, np.uint32]:
                fName += "Digital"
            else:
                raise Exception(
                    "dtype %s not allowed for DO channels (must be uint8, uint16, or uint32)" % str(data.dtype)
                )
        else:
            raise Exception("write() not implemented for this task type (%s)" % chTypes[tt])

        fName += dtypes[data.dtype.descr[0][1]]

        # buf.ctypes is a c_void_p, but the function requires a specific pointer type so we are forced to recast the pointer:
        fn = getattr(PyDAQmx, fName)
        cbuf = ctypes.cast(data.ctypes, fn.argCType("writeArray"))

        nPts = getattr(self, fName)(data.size / numChans, False, timeout, PyDAQmx.Val_GroupByChannel, cbuf)
        return nPts

    def absChannelName(self, n):
        parts = n.lstrip("/").split("/")
        devs = self.GetTaskDevices().split(", ")
        if parts[0] not in devs:
            if len(devs) != 1:
                raise Exception("Cannot determine device to prepend on channel '%s'" % n)
            parts = [devs[0]] + parts
        return "/" + "/".join(parts)

    def taskType(self):
        # print "taskType:"
        ch = self.GetTaskChannels().split(", ")
        # print ch
        ch = self.absChannelName(ch[0])
        # print "First task channel:", ch
        return self.GetChanType(ch)

    def isInputTask(self):
        return self.taskType() in [PyDAQmx.Val_AI, PyDAQmx.Val_DI]

    def isOutputTask(self):
        return self.taskType() in [PyDAQmx.Val_AO, PyDAQmx.Val_DO]
<|endoftext|>


<commit_before>import os

# Accepted file formats regex
# markdown accepted
# reStructuredText not accepted for now
ACCEPTED_FILE_FORMATS = "^.*\.md$"  # .md

POSTS_GIT_REPO = 'https://github.com/Fuchida/posts.git'
POSTS_GIT_REPO_SECRET = '1qaz@WSX'

# Git Repo Config
# Fetched via environmental variables
GIT_REPO_URL = os.environ.get('POSTS_GIT_REPO', POSTS_GIT_REPO)

POSTS_REPO_NAME = GIT_REPO_URL.split('/')[-1].replace('.git', '')
SOURCE_DIRECTORY = os.getcwd()
GIT_REPO_DIR_PATH = os.path.join(SOURCE_DIRECTORY, POSTS_REPO_NAME)

GIT_REPO_SECRET = os.environ.get('POSTS_GIT_REPO_SECRET', POSTS_GIT_REPO_SECRET)

# Server Config
PORT = 5000
HOST = '0.0.0.0'
DEBUG_STATUS = False
<commit_msg>clean up<commit_after>import os

# Accepted file formats regex
# markdown accepted
# reStructuredText not accepted for now
ACCEPTED_FILE_FORMATS = "^.*\.md$"  # .md

POSTS_GIT_REPO = None
POSTS_GIT_REPO_SECRET = None

# Git Repo Config
# Fetched via environmental variables
GIT_REPO_URL = os.environ.get('POSTS_GIT_REPO', POSTS_GIT_REPO)

POSTS_REPO_NAME = GIT_REPO_URL.split('/')[-1].replace('.git', '')
SOURCE_DIRECTORY = os.getcwd()
GIT_REPO_DIR_PATH = os.path.join(SOURCE_DIRECTORY, POSTS_REPO_NAME)

GIT_REPO_SECRET = os.environ.get('POSTS_GIT_REPO_SECRET', POSTS_GIT_REPO_SECRET)

# Server Config
PORT = 5000
HOST = '0.0.0.0'
DEBUG_STATUS = False
<|endoftext|>


<commit_before>'''
  agda-pkg
  ~~~~~~~~~~~~~~~~~~~~~~~~~~
'''

from pathlib import Path

import click
import git
import subprocess

__author__  = "Jonathan Prieto-Cubides & Camilo Rodriguez"

# -----------------------------------------------------------------------------

LEGACY_MODE = True

AGDA_PKG_PATH = Path().home().joinpath('.apkg')
GITHUB_USER   = "apkgbot"

# The github repository index of all agda packages
INDEX_REPOSITORY_NAME = "agda-packages"
INDEX_REPOSITORY_URL  = \
  "https://github.com/"+ GITHUB_USER + "/" + INDEX_REPOSITORY_NAME + ".git"
INDEX_REPOSITORY_BRANCH = "master"
INDEX_REPOSITORY_PATH   = AGDA_PKG_PATH.joinpath(INDEX_REPOSITORY_NAME)

# We want to search fast queries using a database
DATABASE_FILE_NAME = INDEX_REPOSITORY_NAME + ".db"
DATABASE_FILE_PATH = AGDA_PKG_PATH.joinpath(DATABASE_FILE_NAME)

# -- AGDA DIRECTORIES

AGDA_DIR_PATH = Path().home().joinpath(".agda")
AGDA_DEFAULTS_PATH  = AGDA_DIR_PATH.joinpath("defaults")
AGDA_LIBRARIES_PATH = AGDA_DIR_PATH.joinpath("libraries")
AGDA_VERSION  = ""

REPO = None

# -----------------------------------------------------------------------------

def setup():
  global REPO

  if not AGDA_PKG_PATH.exists():
    AGDA_PKG_PATH.mkdir()
    print("AGDA_PKG created at ", AGDA_PKG_PATH.as_posix())

  if not INDEX_REPOSITORY_PATH.exists():
    INDEX_REPOSITORY_PATH.mkdir()
    print("Packages Index created at ", INDEX_REPOSITORY_PATH.as_posix())

  try:
    REPO = git.Repo.clone_from(INDEX_REPOSITORY_URL, INDEX_REPOSITORY_PATH)
  except:
    REPO = git.Repo(INDEX_REPOSITORY_PATH)

  if not DATABASE_FILE_PATH.exists():
    DATABASE_FILE_PATH.touch()
    # run ponywhoosh...

  try:
    result = subprocess.run(["agda", "--version"], stdout=subprocess.PIPE)
    AGDA_VERSION  = result.stdout.split()[2].decode()
    print("Agda found! Version "+ AGDA_VERSION)
  except Exception(FileNotFoundError):
    print("Agda is not installed on this machine")

  if not AGDA_DIR_PATH.exists():
    AGDA_DIR_PATH.mkdir()
    print("AGDA_DIR created at " + AGDA_DIR_PATH.as_posix())

    AGDA_DEFAULTS_PATH.touch()
    print("defaults file created at " + AGDA_DEFAULTS_PATH.as_posix())

    AGDA_LIBRARIES_PATH.touch()
    print("libraries file created at " + AGDA_LIBRARIES_PATH.as_posix())

  return REPO<commit_msg>Supporting agda version for libraries file<commit_after>'''
  agda-pkg
  ~~~~~~~~~~~~~~~~~~~~~~~~~~
'''

from pathlib import Path

import click
import git
import subprocess

__author__  = "Jonathan Prieto-Cubides & Camilo Rodriguez"

# -----------------------------------------------------------------------------

LEGACY_MODE = True

AGDA_PKG_PATH = Path().home().joinpath('.apkg')
GITHUB_USER   = "apkgbot"

# The github repository index of all agda packages
INDEX_REPOSITORY_NAME = "agda-packages"
INDEX_REPOSITORY_URL  = \
  "https://github.com/"+ GITHUB_USER + "/" + INDEX_REPOSITORY_NAME + ".git"
INDEX_REPOSITORY_BRANCH = "master"
INDEX_REPOSITORY_PATH   = AGDA_PKG_PATH.joinpath(INDEX_REPOSITORY_NAME)

# We want to search fast queries using a database
DATABASE_FILE_NAME = INDEX_REPOSITORY_NAME + ".db"
DATABASE_FILE_PATH = AGDA_PKG_PATH.joinpath(DATABASE_FILE_NAME)

# -- AGDA DIRECTORIES

AGDA_DIR_PATH = Path().home().joinpath(".agda")
AGDA_DEFAULTS_PATH  = AGDA_DIR_PATH.joinpath("defaults")
AGDA_LIBRARIES_PATH = AGDA_DIR_PATH.joinpath("libraries")
AGDA_VERSION  = ""

REPO = None

# -----------------------------------------------------------------------------

def setup():
  global REPO, AGDA_LIBRARIES_PATH

  if not AGDA_PKG_PATH.exists():
    AGDA_PKG_PATH.mkdir()
    print("AGDA_PKG created at ", AGDA_PKG_PATH.as_posix())

  if not INDEX_REPOSITORY_PATH.exists():
    INDEX_REPOSITORY_PATH.mkdir()
    print("Packages Index created at ", INDEX_REPOSITORY_PATH.as_posix())

  try:
    REPO = git.Repo.clone_from(INDEX_REPOSITORY_URL, INDEX_REPOSITORY_PATH)
  except:
    REPO = git.Repo(INDEX_REPOSITORY_PATH)

  if not DATABASE_FILE_PATH.exists():
    DATABASE_FILE_PATH.touch()
    # run ponywhoosh...

  try:
    result = subprocess.run(["agda", "--version"], stdout=subprocess.PIPE)
    AGDA_VERSION  = result.stdout.split()[2].decode()
    AGDA_LIBRARIES_PATH = AGDA_DIR_PATH.joinpath("libraries-"+AGDA_VERSION)

    print("Agda found! Version "+ AGDA_VERSION)
  except Exception(FileNotFoundError):
    print("Agda is not installed on this machine")

  if not AGDA_DIR_PATH.exists():
    AGDA_DIR_PATH.mkdir()
    print("AGDA_DIR created at " + AGDA_DIR_PATH.as_posix())

    AGDA_DEFAULTS_PATH.touch()
    print("defaults file created at " + AGDA_DEFAULTS_PATH.as_posix())

    AGDA_LIBRARIES_PATH.touch()
    print("libraries file created at " + AGDA_LIBRARIES_PATH.as_posix())

  return REPO<|endoftext|>


<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-
from common.logLib import *
from common.msgSend import *
from common.queryUserPreference import *
import time
TM_INTERVAL=60
Exception_INTERVAL=3600
phonenum_list=['18665817689','18665910949']
msg_content="userPreference:getResponse failed"
cnt=1

while 1:
    status=queryUserPreference()
    if(status == 0 and cnt == 3):
        cnt=0
        time.sleep(TM_INTERVAL)
    elif(status == -1 and cnt ==3):
        logging.error(msg_content+str(cnt)+' times')
        msgSend(phonenum_list,msg_content)
        cnt=0
        time.sleep(Exception_INTERVAL)
    elif(status == -1 and cnt !=3):
        logging.error(msg_content+str(cnt)+' times')
        cnt+=1
        time.sleep(TM_INTERVAL)
    else:
        time.sleep(TM_INTERVAL)

<commit_msg>update<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-
from common.logLib import *
from common.msgSend import *
from common.queryUserPreference import *
import time
TM_INTERVAL=60
Exception_INTERVAL=3600
phonenum_list=['18665817689','18665910949','18666662305','13632979619']
msg_content="userPreference:getResponse failed"
cnt=1

while 1:
    status=queryUserPreference()
    if(status == 0 and cnt == 3):
        cnt=0
        time.sleep(TM_INTERVAL)
    elif(status == -1 and cnt ==3):
        logging.error(msg_content+str(cnt)+' times')
        msgSend(phonenum_list,msg_content)
        cnt=0
        time.sleep(Exception_INTERVAL)
    elif(status == -1 and cnt !=3):
        logging.error(msg_content+str(cnt)+' times')
        cnt+=1
        time.sleep(TM_INTERVAL)
    else:
        time.sleep(TM_INTERVAL)

<|endoftext|>


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

import roslib
import rospy
import readline
import sys
import os
import signal

from optparse import OptionParser, OptionGroup
from moveit_commander import MoveGroupCommandInterpreter, MoveGroupInfoLevel, roscpp_initialize, roscpp_shutdown

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

class SimpleCompleter(object):

    def __init__(self, options):
        self.options = options

    def set_options(self, options):
        self.options = options

    def complete(self, text, state):
        response = None
        cmds = readline.get_line_buffer().split()
        prefix = ""
        if len(cmds) > 0:
            prefix = cmds[0]
            if not self.options.has_key(prefix):
                prefix = ""

        if state == 0:
            # This is the first time for this text, so build a match list.
            if text:
                if len(prefix) == 0:
                    self.matches = sorted([s
                                           for s in self.options.keys()
                                           if s and s.startswith(text)])
                else:
                    self.matches = sorted([s
                                           for s in self.options[prefix]
                                           if s and s.startswith(text)])
            else:
                if len(prefix) == 0:
                    self.matches = sorted(self.options.keys())
                else:
                    self.matches = self.options[prefix]

        # Return the state'th item from the match list,
        # if we have that many.
        try:
            response = self.matches[state] + " "
        except IndexError:
            response = None
        return response

def print_message(level, msg):
    if level == MoveGroupInfoLevel.FAIL:
        print bcolors.FAIL + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.WARN:
        print bcolors.WARNING + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.SUCCESS:
        print bcolors.OKGREEN + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.DEBUG:
        print bcolors.OKBLUE + msg + bcolors.ENDC
    else:
        print msg

def get_context_keywords(interpreter):
    kw = interpreter.get_keywords()
    kw["quit"] = []
    return kw

def run_interactive(group_names):
    c = MoveGroupCommandInterpreter()
    for g in group_names:
        c.execute( "use " + g)
    completer = SimpleCompleter(get_context_keywords(c))
    readline.set_completer(completer.complete)

    print
    print bcolors.HEADER + "Waiting for commands. Type 'help' to get a list of known commands." + bcolors.ENDC
    print
    readline.parse_and_bind('tab: complete')

    while not rospy.is_shutdown():
        cmd = ""
        try:
            name = ""
            ag = c.get_active_group()
            if ag != None:
                name = ag.get_name()
            cmd = raw_input(bcolors.OKBLUE + name + '> ' + bcolors.ENDC)
        except:
            break
        cmdorig = cmd.strip()
        if cmdorig == "":
            continue
        cmd = cmdorig.lower()

        if cmd == "q" or cmd == "quit" or cmd == "exit":
            break
        if cmd == "host":
            print_message(MoveGroupInfoLevel.INFO, "Master is '" + os.environ['ROS_MASTER_URI'] + "'")
            continue

        (level, msg) = c.execute(cmdorig)
        print_message(level, msg)
        # update the set of keywords
        completer.set_options(get_context_keywords(c))

def run_service(group_names):
    c = MoveGroupCommandInterpreter()
    for g in group_names:
        c.execute("use " + g)
    # add service stuff
    print "Running ROS service"
    rospy.spin()

def stop_ros(reason):
    rospy.signal_shutdown(reason)
    roscpp_shutdown()

def sigint_handler(signal, frame):
    stop_ros("Ctrl+C pressed")
    # this won't actually exit, but trigger an exception to terminate raw_input
    sys.exit(0)

if __name__=='__main__':

    signal.signal(signal.SIGINT, sigint_handler)

    roscpp_initialize(sys.argv)

    rospy.init_node('move_group_interface_cmdline', anonymous=True, disable_signals=True)

    usage = """%prog [options] <group_name_1> [<group_name_2> ...]"""
    parser = OptionParser(usage)
    parser.add_option("-i", "--interactive", action="store_true", dest="interactive", default=True,
                      help="Run the command processing script in interactive mode")
    parser.add_option("-s", "--service", action="store_true", dest="service", default=False,
                      help="Run the command processing script as a ROS service")
    (options, args) = parser.parse_args()

    if options.service:
        run_service(args)
    else:
        run_interactive(args)

    stop_ros("Done")

    print "Bye bye!"
<commit_msg>switch to argparse<commit_after>#!/usr/bin/env python

import roslib
import rospy
import readline
import sys
import os
import signal

import argparse
from moveit_commander import MoveGroupCommandInterpreter, MoveGroupInfoLevel, roscpp_initialize, roscpp_shutdown

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

class SimpleCompleter(object):

    def __init__(self, options):
        self.options = options

    def set_options(self, options):
        self.options = options

    def complete(self, text, state):
        response = None
        cmds = readline.get_line_buffer().split()
        prefix = ""
        if len(cmds) > 0:
            prefix = cmds[0]
            if not self.options.has_key(prefix):
                prefix = ""

        if state == 0:
            # This is the first time for this text, so build a match list.
            if text:
                if len(prefix) == 0:
                    self.matches = sorted([s
                                           for s in self.options.keys()
                                           if s and s.startswith(text)])
                else:
                    self.matches = sorted([s
                                           for s in self.options[prefix]
                                           if s and s.startswith(text)])
            else:
                if len(prefix) == 0:
                    self.matches = sorted(self.options.keys())
                else:
                    self.matches = self.options[prefix]

        # Return the state'th item from the match list,
        # if we have that many.
        try:
            response = self.matches[state] + " "
        except IndexError:
            response = None
        return response

def print_message(level, msg):
    if level == MoveGroupInfoLevel.FAIL:
        print bcolors.FAIL + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.WARN:
        print bcolors.WARNING + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.SUCCESS:
        print bcolors.OKGREEN + msg + bcolors.ENDC
    elif level == MoveGroupInfoLevel.DEBUG:
        print bcolors.OKBLUE + msg + bcolors.ENDC
    else:
        print msg

def get_context_keywords(interpreter):
    kw = interpreter.get_keywords()
    kw["quit"] = []
    return kw

def run_interactive(group_name):
    c = MoveGroupCommandInterpreter()
    if len(group_name) > 0:
        c.execute("use " + group_name)
    completer = SimpleCompleter(get_context_keywords(c))
    readline.set_completer(completer.complete)

    print
    print bcolors.HEADER + "Waiting for commands. Type 'help' to get a list of known commands." + bcolors.ENDC
    print
    readline.parse_and_bind('tab: complete')

    while not rospy.is_shutdown():
        cmd = ""
        try:
            name = ""
            ag = c.get_active_group()
            if ag != None:
                name = ag.get_name()
            cmd = raw_input(bcolors.OKBLUE + name + '> ' + bcolors.ENDC)
        except:
            break
        cmdorig = cmd.strip()
        if cmdorig == "":
            continue
        cmd = cmdorig.lower()

        if cmd == "q" or cmd == "quit" or cmd == "exit":
            break
        if cmd == "host":
            print_message(MoveGroupInfoLevel.INFO, "Master is '" + os.environ['ROS_MASTER_URI'] + "'")
            continue

        (level, msg) = c.execute(cmdorig)
        print_message(level, msg)
        # update the set of keywords
        completer.set_options(get_context_keywords(c))

def run_service(group_name):
    c = MoveGroupCommandInterpreter()
    if len(group_name) > 0:
        c.execute("use " + group_name)
    # add service stuff
    print "Running ROS service"
    rospy.spin()

def stop_ros(reason):
    rospy.signal_shutdown(reason)
    roscpp_shutdown()

def sigint_handler(signal, frame):
    stop_ros("Ctrl+C pressed")
    # this won't actually exit, but trigger an exception to terminate raw_input
    sys.exit(0)

if __name__=='__main__':

    signal.signal(signal.SIGINT, sigint_handler)

    roscpp_initialize(sys.argv)

    rospy.init_node('move_group_interface_cmdline', anonymous=True, disable_signals=True)

    parser = argparse.ArgumentParser(usage = """%(prog)s [options] [<group_name>]""",
                                     description = "Command Line Interface to MoveIt!")
    parser.add_argument("-i", "--interactive", action="store_true", dest="interactive", default=True,
                        help="Run the command processing script in interactive mode (default)")
    parser.add_argument("-s", "--service", action="store_true", dest="service", default=False,
                        help="Run the command processing script as a ROS service")
    parser.add_argument("group_name", type=str, default="", nargs='?', help="Group name to initialize the CLI for.")

    opt = parser.parse_args(rospy.myargv()[1:])

    if opt.service:
        run_service(opt.group_name)
    else:
        run_interactive(opt.group_name)

    stop_ros("Done")

    print "Bye bye!"
<|endoftext|>


<commit_before>import numpy as np
from nipy.testing import *

from nipy.core.reference.coordinate_map import CoordinateMap, Affine, \
    compose, CoordinateSystem, reorder_input, reorder_output, product, \
    replicate, linearize

class empty:
    pass

E = empty()

def setup():
    def f(x):
        return 2*x
    def g(x):
        return x/2.0
    x = CoordinateSystem('x', 'x')
    E.a = CoordinateMap(f, x, x)
    E.b = CoordinateMap(f, x, x, inverse_mapping=g)
    E.c = CoordinateMap(g, x, x)
    E.d = CoordinateMap(g, x, x, inverse_mapping=f)
    E.e = Affine.identity('ijk')

    A = np.identity(4)
    A[0:3] = np.random.standard_normal((3,4))
    E.mapping = Affine.from_params('ijk' ,'xyz', A)

    E.singular = Affine.from_params('ijk', 'xyzt',
                                    np.array([[ 0,  1,  2,  3],
                                              [ 4,  5,  6,  7],
                                              [ 8,  9, 10, 11],
                                              [ 8,  9, 10, 11],
                                              [ 0,  0,  0,  1]]))

def test_call():
    value = np.array([1., 2., 3.])
    result_a = E.a(value)
    result_b = E.b(value)
    result_c = E.c(value)
    result_d = E.d(value)
    yield assert_true, np.allclose(result_a, 2*value)
    yield assert_true, np.allclose(result_b, 2*value)
    yield assert_true, np.allclose(result_c, value/2)
    yield assert_true, np.allclose(result_d, value/2)

def test_str():
    s_a = str(E.a)
    s_b = str(E.b)
    s_c = str(E.c)
    s_d = str(E.d)

def test_compose():
    value = np.array([[1., 2., 3.]]).T
    aa = compose(E.a, E.a)
    yield assert_true, aa.inverse is None
    yield assert_true, np.allclose(aa(value), 4*value)
    ab = compose(E.a,E.b)
    yield assert_true, ab.inverse is None
    assert_true, np.allclose(ab(value), 4*value)
    ac = compose(E.a,E.c)
    yield assert_true, ac.inverse is None
    yield assert_true, np.allclose(ac(value), value)
    bb = compose(E.b,E.b)
    yield assert_true, bb.inverse is not None

def test_isinvertible():
    assert_false(E.a.inverse)
    assert_true(E.b.inverse)
    assert_false(E.c.inverse)
    assert_true(E.d.inverse)

def test_inverse1():
    inv = lambda a: a.inverse
    yield assert_true, inv(E.a) is None
    yield assert_true, inv(E.c) is None
    inv_b = E.b.inverse
    inv_d = E.d.inverse
    ident_b = compose(inv_b,E.b)
    ident_d = compose(inv_d,E.d)
    value = np.array([[1., 2., 3.]]).T
    yield assert_true, np.allclose(ident_b(value), value)
    yield assert_true, np.allclose(ident_d(value), value)

def test_call():
    value = np.array([1., 2., 3.])
    assert_true(np.allclose(E.e(value), value))

def test_mul():
    value = np.array([1., 2., 3.])
    b = compose(E.e, E.e)
    assert_true(np.allclose(b(value), value))

def test_str():
    s = str(E.e)

def test_invertible():
    assert_true(E.e.inverse)

def test_inverse2():
    assert_true(np.allclose(E.e.affine, E.e.inverse.inverse.affine))

def test_isinvertible():
    yield assert_true, E.mapping.inverse
    yield assert_false, E.singular.inverse

def voxel_to_world():
    # utility function for generating trivial CoordinateMap
    incs = CoordinateSystem('ijk', 'voxels')
    outcs = CoordinateSystem('xyz', 'world')
    map = lambda x: x + 1
    inv = lambda x: x - 1
    return incs, outcs, map, inv

def test_comap_init():
    # Test mapping and non-mapping functions
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    yield assert_equal, cm.mapping, map
    yield assert_equal, cm.input_coords, incs
    yield assert_equal, cm.output_coords, outcs
    yield assert_equal, cm.inverse_mapping, inv
    yield assert_raises, ValueError, CoordinateMap, 'foo', incs, outcs, inv
    yield assert_raises, ValueError, CoordinateMap, map, incs, outcs, 'bar'

def test_comap_copy():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    cmcp = cm.copy()
    yield assert_equal, cmcp.mapping, cm.mapping
    yield assert_equal, cmcp.input_coords, cm.input_coords
    yield assert_equal, cmcp.output_coords, cm.output_coords
    yield assert_equal, cmcp.inverse_mapping, cm.inverse_mapping

#
# Affine tests
#

def affine_v2w():
    # utility function
    incs = CoordinateSystem('ijk', 'voxels')
    outcs = CoordinateSystem('xyz', 'world')
    aff = np.diag([1, 2, 4, 1])
    aff[:3, 3] = [11, 12, 13]
    """array([[ 1,  0,  0, 11],
       [ 0,  2,  0, 12],
       [ 0,  0,  4, 13],
       [ 0,  0,  0,  1]])
    """
    return incs, outcs, aff

def test_affine_init():
    incs, outcs, aff = affine_v2w()
    cm = Affine(aff, incs, outcs)
    yield assert_equal, cm.input_coords, incs
    yield assert_equal, cm.output_coords, outcs
    yield assert_equal, cm.affine, aff
    badaff = np.diag([1,2])
    yield assert_raises, ValueError, Affine, badaff, incs, outcs

def test_affine_inverse():
    incs, outcs, aff = affine_v2w()
    inv = np.linalg.inv(aff)
    cm = Affine(aff, incs, outcs)
    invmap = cm.inverse_mapping
    x = np.array([10, 20, 30])
    x_roundtrip = cm.mapping(invmap(x))
    yield assert_equal, x_roundtrip, x
    badaff = np.array([[1,2,3],[4,5,6]])
    badcm = Affine(aff, incs, outcs)
    badcm._affine = badaff
    yield assert_raises, ValueError, getattr, badcm, 'inverse_mapping'

def test_affine_from_params():
    incs, outcs, aff = affine_v2w()
    cm = Affine.from_params('ijk', 'xyz', aff)
    yield assert_equal, cm.affine, aff
    badaff = np.array([[1,2,3],[4,5,6]])
    yield assert_raises, ValueError, Affine.from_params, 'ijk', 'xyz', badaff

def test_affine_start_step():
    incs, outcs, aff = affine_v2w()
    start = aff[:3, 3]
    step = aff.diagonal()[:3]
    cm = Affine.from_start_step(incs.coord_names, outcs.coord_names,
                                start, step)
    yield assert_equal, cm.affine, aff
    yield assert_raises, ValueError, Affine.from_start_step, 'ijk', 'xy', \
        start, step

def test_affine_identity():
    aff = Affine.identity('ijk')
    yield assert_equal, aff.affine, np.eye(4)
    yield assert_equal, aff.input_coords, aff.output_coords
    x = np.array([3, 4, 5])
    y = aff.mapping(x)
    yield assert_equal, y, x

def test_affine_copy():
    incs, outcs, aff = affine_v2w()
    cm = Affine(aff, incs, outcs)
    cmcp = cm.copy()
    yield assert_equal, cmcp.affine, cm.affine
    yield assert_equal, cmcp.input_coords, cm.input_coords
    yield assert_equal, cmcp.output_coords, cm.output_coords

#
# Module level functions
#

def test_reorder_input():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    recm = reorder_input(cm, 'jki')
    yield assert_equal, recm.input_coords.coord_names, ('j', 'k', 'i')
    yield assert_equal, recm.output_coords.coord_names, outcs.coord_names
    yield assert_equal, recm.input_coords.name, incs.name+'-reordered'
    yield assert_equal, recm.output_coords.name, outcs.name
    # default reverse reorder
    recm = reorder_input(cm)
    yield assert_equal, recm.input_coords.coord_names, ('k', 'j', 'i')
    # reorder with order as indices
    recm = reorder_input(cm, [2,0,1])
    yield assert_equal, recm.input_coords.coord_names, ('k', 'i', 'j')

def test_reorder_output():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    recm = reorder_output(cm, 'yzx')
    yield assert_equal, recm.input_coords.coord_names, incs.coord_names
    yield assert_equal, recm.output_coords.coord_names, ('y', 'z', 'x')
    yield assert_equal, recm.input_coords.name, incs.name
    yield assert_equal, recm.output_coords.name, outcs.name+'-reordered'
    # default reverse order
    recm = reorder_output(cm)
    yield assert_equal, recm.output_coords.coord_names, ('z', 'y', 'x')
    # reorder with indicies
    recm = reorder_output(cm, [2,0,1])
    yield assert_equal, recm.output_coords.coord_names, ('z', 'x', 'y')

def test_product():
    cm1 = Affine.from_params('i', 'x', np.diag([2, 1]))
    cm2 = Affine.from_params('j', 'y', np.diag([3, 1]))
    cm = product(cm1, cm2)
    yield assert_equal, cm.input_coords.coord_names, ('i', 'j')
    yield assert_equal, cm.output_coords.coord_names, ('x', 'y')
    yield assert_equal, cm.affine, np.diag([2, 3, 1])

def test_compose():
    aff1 = np.diag([1,2,3,1])
    cm1 = Affine.from_params('ijk', 'xyz', aff1)
    aff2 = np.diag([4,5,6,1])
    cm2 = Affine.from_params('xyz', 'abc', aff2)
    # compose mapping from 'ijk' to 'abc'
    compcm = compose(cm2, cm1)
    yield assert_equal, compcm.input_coords.coord_names, ('i', 'j', 'k')
    yield assert_equal, compcm.output_coords.coord_names, ('a', 'b', 'c')
    yield assert_equal, compcm.affine, np.dot(aff2, aff1)
    # check invalid coordinate mappings
    yield assert_raises, ValueError, compose, cm1, cm2

def test_replicate():
    # FIXME: Implement test when this function works
    yield assert_raises, NotImplementedError, replicate, 'foo', 'bar'

def test_linearize():
    aff = np.diag([1,2,3,1])
    cm = Affine.from_params('ijk', 'xyz', aff)
    lincm = linearize(cm.mapping, cm.ndim[0])
    yield assert_equal, lincm, aff
    origin = np.array([10, 20, 30], dtype=cm.input_coords.coord_dtype)
    lincm = linearize(cm.mapping, cm.ndim[0], origin=origin)
    xform = np.array([[  1.,   0.,   0.,  10.],
                      [  0.,   2.,   0.,  40.],
                      [  0.,   0.,   3.,  90.],
                      [  0.,   0.,   0.,   1.]])
    yield assert_equal, lincm, xform
    # dtype mismatch
    #origin = np.array([10, 20, 30], dtype=np.int16)
    #yield assert_raises, UserWarning, linearize, cm.mapping, cm.ndim[0], \
    #    1, origin
<commit_msg>Combine duplicate test functions<commit_after>import numpy as np
from nipy.testing import *

from nipy.core.reference.coordinate_map import CoordinateMap, Affine, \
    compose, CoordinateSystem, reorder_input, reorder_output, product, \
    replicate, linearize

class empty:
    pass

E = empty()

def setup():
    def f(x):
        return 2*x
    def g(x):
        return x/2.0
    x = CoordinateSystem('x', 'x')
    E.a = CoordinateMap(f, x, x)
    E.b = CoordinateMap(f, x, x, inverse_mapping=g)
    E.c = CoordinateMap(g, x, x)
    E.d = CoordinateMap(g, x, x, inverse_mapping=f)
    E.e = Affine.identity('ijk')

    A = np.identity(4)
    A[0:3] = np.random.standard_normal((3,4))
    E.mapping = Affine.from_params('ijk' ,'xyz', A)

    E.singular = Affine.from_params('ijk', 'xyzt',
                                    np.array([[ 0,  1,  2,  3],
                                              [ 4,  5,  6,  7],
                                              [ 8,  9, 10, 11],
                                              [ 8,  9, 10, 11],
                                              [ 0,  0,  0,  1]]))

def test_call():
    value = 10
    yield assert_true, np.allclose(E.a(value), 2*value)
    yield assert_true, np.allclose(E.b(value), 2*value)
    yield assert_true, np.allclose(E.c(value), value/2)
    yield assert_true, np.allclose(E.d(value), value/2)
    value = np.array([1., 2., 3.])
    yield assert_true, np.allclose(E.e(value), value)

def test_compose():
    value = np.array([[1., 2., 3.]]).T
    aa = compose(E.a, E.a)
    yield assert_true, aa.inverse is None
    yield assert_true, np.allclose(aa(value), 4*value)
    ab = compose(E.a,E.b)
    yield assert_true, ab.inverse is None
    assert_true, np.allclose(ab(value), 4*value)
    ac = compose(E.a,E.c)
    yield assert_true, ac.inverse is None
    yield assert_true, np.allclose(ac(value), value)
    bb = compose(E.b,E.b)
    yield assert_true, bb.inverse is not None
    aff1 = np.diag([1,2,3,1])
    cm1 = Affine.from_params('ijk', 'xyz', aff1)
    aff2 = np.diag([4,5,6,1])
    cm2 = Affine.from_params('xyz', 'abc', aff2)
    # compose mapping from 'ijk' to 'abc'
    compcm = compose(cm2, cm1)
    yield assert_equal, compcm.input_coords.coord_names, ('i', 'j', 'k')
    yield assert_equal, compcm.output_coords.coord_names, ('a', 'b', 'c')
    yield assert_equal, compcm.affine, np.dot(aff2, aff1)
    # check invalid coordinate mappings
    yield assert_raises, ValueError, compose, cm1, cm2

def test_isinvertible():
    yield assert_false, E.a.inverse
    yield assert_true, E.b.inverse
    yield assert_false, E.c.inverse
    yield assert_true, E.d.inverse
    yield assert_true, E.e.inverse
    yield assert_true, E.mapping.inverse
    yield assert_false, E.singular.inverse

def test_inverse1():
    inv = lambda a: a.inverse
    yield assert_true, inv(E.a) is None
    yield assert_true, inv(E.c) is None
    inv_b = E.b.inverse
    inv_d = E.d.inverse
    ident_b = compose(inv_b,E.b)
    ident_d = compose(inv_d,E.d)
    value = np.array([[1., 2., 3.]]).T
    yield assert_true, np.allclose(ident_b(value), value)
    yield assert_true, np.allclose(ident_d(value), value)

def test_mul():
    value = np.array([1., 2., 3.])
    b = compose(E.e, E.e)
    assert_true(np.allclose(b(value), value))

def test_inverse2():
    assert_true(np.allclose(E.e.affine, E.e.inverse.inverse.affine))

def voxel_to_world():
    # utility function for generating trivial CoordinateMap
    incs = CoordinateSystem('ijk', 'voxels')
    outcs = CoordinateSystem('xyz', 'world')
    map = lambda x: x + 1
    inv = lambda x: x - 1
    return incs, outcs, map, inv

def test_comap_init():
    # Test mapping and non-mapping functions
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    yield assert_equal, cm.mapping, map
    yield assert_equal, cm.input_coords, incs
    yield assert_equal, cm.output_coords, outcs
    yield assert_equal, cm.inverse_mapping, inv
    yield assert_raises, ValueError, CoordinateMap, 'foo', incs, outcs, inv
    yield assert_raises, ValueError, CoordinateMap, map, incs, outcs, 'bar'

def test_comap_copy():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    cmcp = cm.copy()
    yield assert_equal, cmcp.mapping, cm.mapping
    yield assert_equal, cmcp.input_coords, cm.input_coords
    yield assert_equal, cmcp.output_coords, cm.output_coords
    yield assert_equal, cmcp.inverse_mapping, cm.inverse_mapping

#
# Affine tests
#

def affine_v2w():
    # utility function
    incs = CoordinateSystem('ijk', 'voxels')
    outcs = CoordinateSystem('xyz', 'world')
    aff = np.diag([1, 2, 4, 1])
    aff[:3, 3] = [11, 12, 13]
    """array([[ 1,  0,  0, 11],
       [ 0,  2,  0, 12],
       [ 0,  0,  4, 13],
       [ 0,  0,  0,  1]])
    """
    return incs, outcs, aff

def test_affine_init():
    incs, outcs, aff = affine_v2w()
    cm = Affine(aff, incs, outcs)
    yield assert_equal, cm.input_coords, incs
    yield assert_equal, cm.output_coords, outcs
    yield assert_equal, cm.affine, aff
    badaff = np.diag([1,2])
    yield assert_raises, ValueError, Affine, badaff, incs, outcs

def test_affine_inverse():
    incs, outcs, aff = affine_v2w()
    inv = np.linalg.inv(aff)
    cm = Affine(aff, incs, outcs)
    invmap = cm.inverse_mapping
    x = np.array([10, 20, 30])
    x_roundtrip = cm.mapping(invmap(x))
    yield assert_equal, x_roundtrip, x
    badaff = np.array([[1,2,3],[4,5,6]])
    badcm = Affine(aff, incs, outcs)
    badcm._affine = badaff
    yield assert_raises, ValueError, getattr, badcm, 'inverse_mapping'

def test_affine_from_params():
    incs, outcs, aff = affine_v2w()
    cm = Affine.from_params('ijk', 'xyz', aff)
    yield assert_equal, cm.affine, aff
    badaff = np.array([[1,2,3],[4,5,6]])
    yield assert_raises, ValueError, Affine.from_params, 'ijk', 'xyz', badaff

def test_affine_start_step():
    incs, outcs, aff = affine_v2w()
    start = aff[:3, 3]
    step = aff.diagonal()[:3]
    cm = Affine.from_start_step(incs.coord_names, outcs.coord_names,
                                start, step)
    yield assert_equal, cm.affine, aff
    yield assert_raises, ValueError, Affine.from_start_step, 'ijk', 'xy', \
        start, step

def test_affine_identity():
    aff = Affine.identity('ijk')
    yield assert_equal, aff.affine, np.eye(4)
    yield assert_equal, aff.input_coords, aff.output_coords
    x = np.array([3, 4, 5])
    y = aff.mapping(x)
    yield assert_equal, y, x

def test_affine_copy():
    incs, outcs, aff = affine_v2w()
    cm = Affine(aff, incs, outcs)
    cmcp = cm.copy()
    yield assert_equal, cmcp.affine, cm.affine
    yield assert_equal, cmcp.input_coords, cm.input_coords
    yield assert_equal, cmcp.output_coords, cm.output_coords

#
# Module level functions
#

def test_reorder_input():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    recm = reorder_input(cm, 'jki')
    yield assert_equal, recm.input_coords.coord_names, ('j', 'k', 'i')
    yield assert_equal, recm.output_coords.coord_names, outcs.coord_names
    yield assert_equal, recm.input_coords.name, incs.name+'-reordered'
    yield assert_equal, recm.output_coords.name, outcs.name
    # default reverse reorder
    recm = reorder_input(cm)
    yield assert_equal, recm.input_coords.coord_names, ('k', 'j', 'i')
    # reorder with order as indices
    recm = reorder_input(cm, [2,0,1])
    yield assert_equal, recm.input_coords.coord_names, ('k', 'i', 'j')

def test_reorder_output():
    incs, outcs, map, inv = voxel_to_world()
    cm = CoordinateMap(map, incs, outcs, inv)
    recm = reorder_output(cm, 'yzx')
    yield assert_equal, recm.input_coords.coord_names, incs.coord_names
    yield assert_equal, recm.output_coords.coord_names, ('y', 'z', 'x')
    yield assert_equal, recm.input_coords.name, incs.name
    yield assert_equal, recm.output_coords.name, outcs.name+'-reordered'
    # default reverse order
    recm = reorder_output(cm)
    yield assert_equal, recm.output_coords.coord_names, ('z', 'y', 'x')
    # reorder with indicies
    recm = reorder_output(cm, [2,0,1])
    yield assert_equal, recm.output_coords.coord_names, ('z', 'x', 'y')

def test_product():
    cm1 = Affine.from_params('i', 'x', np.diag([2, 1]))
    cm2 = Affine.from_params('j', 'y', np.diag([3, 1]))
    cm = product(cm1, cm2)
    yield assert_equal, cm.input_coords.coord_names, ('i', 'j')
    yield assert_equal, cm.output_coords.coord_names, ('x', 'y')
    yield assert_equal, cm.affine, np.diag([2, 3, 1])

def test_replicate():
    # FIXME: Implement test when this function works
    yield assert_raises, NotImplementedError, replicate, 'foo', 'bar'

def test_linearize():
    aff = np.diag([1,2,3,1])
    cm = Affine.from_params('ijk', 'xyz', aff)
    lincm = linearize(cm.mapping, cm.ndim[0])
    yield assert_equal, lincm, aff
    origin = np.array([10, 20, 30], dtype=cm.input_coords.coord_dtype)
    lincm = linearize(cm.mapping, cm.ndim[0], origin=origin)
    xform = np.array([[  1.,   0.,   0.,  10.],
                      [  0.,   2.,   0.,  40.],
                      [  0.,   0.,   3.,  90.],
                      [  0.,   0.,   0.,   1.]])
    yield assert_equal, lincm, xform
    # dtype mismatch
    #origin = np.array([10, 20, 30], dtype=np.int16)
    #yield assert_raises, UserWarning, linearize, cm.mapping, cm.ndim[0], \
    #    1, origin
<|endoftext|>


<commit_before>#!usr/bin/env python3
__author__ = 'David Kisluk'

import argparse
import json
import nltk

def main():
    # parse args
    input_file = ''
    output_file = ''

    args = parse_args()

    input_file = args.infile
    output_file = args.outfile

    # In comes the text (filename)
    with open(input_file, 'r') as f:
        raw_text = f.read()

    # Use nltk to tokenize text.
    tokens = nltk.word_tokenize(raw_text)

    # Normalize tokenize text (lowercase)
    words = [w.lower() for w in tokens if w.isalpha()]

    # mark words with POS tags
    tagged_words = nltk.pos_tag(words)

    # Gather nouns => Stem nouns
    nouns = [w for w in tagged_words if w[1].startswith('N')]
    untagged_nouns = nltk.Text([noun[0] for noun in nouns])
    noun_freq_dist = nltk.FreqDist(untagged_nouns)

    # Gather verbs =>
    verbs = [w for w in tagged_words if w[1].startswith('V')]
    untagged_verbs = nltk.Text([verb[0] for verb in verbs])
    verbs_freq_dist = nltk.FreqDist(untagged_verbs)

    # Algorithm to go through nouns and verbs to relate them

    # Filter results using hapaxes

def parse_args():
    """Parses command line arguments and returns a collection of them"""

    parser = argparse.ArgumentParser()

    format_group = parser.add_mutually_exclusive_group()
    format_group.add_argument("-j", "--json", action="store_true")
    format_group.add_argument("-x", "--xml", action="store_true")

    parser.add_argument("infile", help="the input file")
    parser.add_argument("outfile", help="the output file")

    return parser.parse_args()

if __name__ == "__main__":
    main()

<commit_msg>Write to json<commit_after>#!usr/bin/env python3
__author__ = 'David Kisluk'

import argparse
import json
import nltk

def main():
    # parse args
    input_file = ''
    output_file = ''

    args = parse_args()

    input_file = args.infile
    output_file = args.outfile

    # In comes the text (filename)
    with open(input_file, 'r') as f:
        raw_text = f.read()

    # Use nltk to tokenize text.
    tokens = nltk.word_tokenize(raw_text)

    # Normalize tokenize text (lowercase)
    words = [w.lower() for w in tokens if w.isalpha()]

    # mark words with POS tags
    tagged_words = nltk.pos_tag(words)

    # Gather nouns => Stem nouns
    nouns = [w for w in tagged_words if w[1].startswith('N')]
    untagged_nouns = nltk.Text([noun[0] for noun in nouns])
    noun_freq_dist = nltk.FreqDist(untagged_nouns)

    # Gather verbs =>
    verbs = [w for w in tagged_words if w[1].startswith('V')]
    untagged_verbs = nltk.Text([verb[0] for verb in verbs])
    verbs_freq_dist = nltk.FreqDist(untagged_verbs)

    # Algorithm to go through nouns and verbs to relate them

    # Filter results using hapaxes
    out_array = [noun_freq_dist.hapaxes(), verbs_freq_dist.hapaxes()]

    # write the verbs/nouns to a JSON file
    with open(output_file, 'w') as json_file:
        json.dump(out_array, json_file)

def parse_args():
    """Parses command line arguments and returns a collection of them"""

    parser = argparse.ArgumentParser()

    format_group = parser.add_mutually_exclusive_group()
    format_group.add_argument("-j", "--json", action="store_true")
    format_group.add_argument("-x", "--xml", action="store_true")

    parser.add_argument("infile", help="the input file")
    parser.add_argument("outfile", help="the output file")

    return parser.parse_args()

if __name__ == "__main__":
    main()

<|endoftext|>


<commit_before>import cv2
import sys
import numpy as np
import time

class SLIC:
    def __init__(self, img, step, nc):
        self.img = img
        self.height, self.width = img.shape[:2]
        self.labimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB).astype(np.float64)
        self.step = step
        self.nc = nc
        self.ns = step
        self.FLT_MAX = 1000000
        self.ITERATIONS = 10
        #self._initData()

    def generateSuperPixels(self):
        self._initData()
        print self.centers.shape, "number of centers"
        print self.clusters.shape, "number of clusters"
        print self.center_counts.shape, "number of center_counts"
        print self.centers
        #raw_input("centers:")

        indnp = np.mgrid[0:self.height,0:self.width].swapaxes(0,2).swapaxes(0,1)
        for i in range(self.ITERATIONS):
            self.distances = self.FLT_MAX * np.ones(self.img.shape[:2])
            for j in xrange(self.centers.shape[0]):
                """
                for k in xrange(self.centers[j][3]-self.step, self.centers[j][3]+self.step):
                    for l in xrange(self.centers[j][4]-self.step, self.centers[j][4]+self.step):
                        print j, k, l
                        if k >=0 and k < self.width and l >=0 and l < self.height:
                            color = self.labimg[l, k]
                            d = self._computeDist(j, (k, l), color)
                            print d
                            if d < self.distances[k][l]:
                                self.distances[k][l] = d
                                self.clusters[k][l] = j
                """
                xlow, xhigh = int(self.centers[j][3] - self.step), int(self.centers[j][3] + self.step)
                ylow, yhigh = int(self.centers[j][4] - self.step), int(self.centers[j][4] + self.step)

                if xlow <= 0:
                    xlow = 0
                if xhigh > self.width:
                    xhigh = self.width
                if ylow <=0:
                    ylow = 0
                if yhigh > self.height:
                    yhigh = self.height

                cropimg = self.labimg[ylow : yhigh , xlow : xhigh]
                colordiff = cropimg - self.labimg[self.centers[j][4], self.centers[j][3]]
                colorDist = np.sqrt(np.sum(np.square(colordiff), axis=2))

                yy, xx = np.ogrid[ylow : yhigh, xlow : xhigh]
                pixdist = ((yy-self.centers[j][4])**2 + (xx-self.centers[j][3])**2)**0.5
                dist = ((colorDist/self.nc)**2 + (pixdist/self.ns)**2)**0.5

                distanceCrop = self.distances[ylow : yhigh, xlow : xhigh]
                idx = dist < distanceCrop
                distanceCrop[idx] = dist[idx]
                self.distances[ylow : yhigh, xlow : xhigh] = distanceCrop
                self.clusters[ylow : yhigh, xlow : xhigh][idx] = j

            # clear the center values
            # save some more time by not clearing it
            #print "clear the center values"
            #self.centers = np.zeros((self.centers.shape[0], 5))
            #self.center_counts = np.zeros(self.centers.shape[0])

            # compute new cluster values
            print "compute new cluster values"
            t = time.time()
            for k in xrange(len(self.centers)):
                idx = (self.clusters == k)
                colornp = self.labimg[idx]
                distnp = indnp[idx]
                self.centers[k][0:3] = np.sum(colornp, axis=0)
                sumy, sumx = np.sum(distnp, axis=0)
                self.centers[k][3:] = sumx, sumy
                #self.centers[k][0:3] = np.average(colornp, axis=0)
                #self.centers[k][3:] = np.average(distnp, axis=0)
                # Normalize here
                self.centers[k] /= np.sum(idx)
                #self.center_counts[k] = np.sum(idx)
            #print self.centers
            t11 = time.time()
            print t11 - t, "time taken for clusters"
            """
            for j in xrange(self.width):
                for k in xrange(self.height):
                    c_id = self.clusters[k, j]
                    #print c_id, "c_id"
                    if c_id != -1:
                        color = self.labimg[k, j]
                        self.centers[c_id][0:3] += color
                        self.centers[c_id][3:] += (j, k)
                        self.center_counts[c_id] +=1
            """
            # Normalize the cluster
            """
            print "Normalize the cluster"
            for j in xrange(self.centers.shape[0]):
                self.centers[j] /= self.center_counts[j]
            """
        print self.centers

    def _initData(self):
        print "_initData"
        print self.step, "step"
        self.clusters = -1 * np.ones(self.img.shape[:2])
        self.distances = self.FLT_MAX * np.ones(self.img.shape[:2])

        centers = []
        for i in xrange(self.step, self.width - self.step/2, self.step):
            for j in xrange(self.step, self.height - self.step/2, self.step):
                #print i, j, "_findLocalMinimum"
                nc = self._findLocalMinimum(center=(i, j))
                color = self.labimg[nc[1], nc[0]]
                center = [color[0], color[1], color[2], nc[0], nc[1]]
                centers.append(center)
        self.center_counts = np.zeros(len(centers))
        self.centers = np.array(centers)
        print self.centers.shape, "number of centers"
        print "_intiData finished"

    def createConnectivity(self):
        print "createConnectivity"
        label = 0
        adjlabel = 0
        lims = self.width * self.height / self.centers.shape[0]
        dx4 = [-1, 0, 1, 0]
        dy4 = [0, -1, 0, 1]
        new_clusters = -1 * np.ones(self.img.shape[:2]).astype(np.int64)
        print lims, "lims"
        #idx = new_clusters == -1
        elements = []
        for i in xrange(self.width):
            for j in xrange(self.height):
                #print new_clusters[j, i]
                #print j, i
                if new_clusters[j, i] == -1:
                    elements = []
                    elements.append((j, i))
                    for dx, dy in zip(dx4, dy4):
                        x = elements[0][1] + dx
                        y = elements[0][0] + dy
                        if (x>=0 and x < self.width and
                            y>=0 and y < self.height and
                            new_clusters[y, x] >=0):
                            adjlabel = new_clusters[y, x]
                #print elements
                count = 1
                """
                for dx, dy in zip(dx4, dy4):
                    x = elements[0][1] + dx
                    y = elements[0][0] + dy

                    if (x>=0 and x<self.width and y>=0 and y<self.height):
                        if new_clusters[y, x] == -1 and self.clusters[j, i] == self.clusters[y, x]:
                            elements.append((y, x))
                            new_clusters[y, x] = label
                            count+=1
                """

                #for c in range(count):
                c = 0
                while c < count:
                    for dx, dy in zip(dx4, dy4):
                        x = elements[c][1] + dx
                        y = elements[c][0] + dy

                        if (x>=0 and x<self.width and y>=0 and y<self.height):
                            if new_clusters[y, x] == -1 and self.clusters[j, i] == self.clusters[y, x]:
                                elements.append((y, x))
                                new_clusters[y, x] = label
                                count+=1
                    c+=1
                #print count
                if (count <= lims >> 2):
                    for c in range(count):
                        new_clusters[elements[c]] = adjlabel
                    label-=1
                label+=1
        print label
        self.new_clusters = new_clusters

    def displayContours(self, color):
        print "displayContours"
        dx8 = [-1, -1, 0, 1, 1, 1, 0, -1]
        dy8 = [0, -1, -1, -1, 0, 1, 1, 1]

        isTaken = np.zeros(self.img.shape[:2], np.bool)
        contours = []

        for i in xrange(self.width):
            for j in xrange(self.height):
                nr_p = 0
                for dx, dy in zip(dx8, dy8):
                    x = i + dx
                    y = j + dy
                    if x>=0 and x < self.width and y>=0 and y < self.height:
                        if isTaken[y, x] == False and self.clusters[j, i] != self.clusters[y, x]:
                            #print self.clusters[j, i]
                            #print self.clusters[y, x]
                            nr_p += 1
                #print nr_p, "nr_p", j, i
                if nr_p >= 2:
                    isTaken[j, i] = True
                    contours.append([j, i])
        #print contours
        #print len(contours)
        for i in xrange(len(contours)):
            self.img[contours[i][0], contours[i][1]] = color

    def _findLocalMinimum(self, center):
        min_grad = self.FLT_MAX
        loc_min = center
        #print "_findLocalMinimum"
        #print center
        for i in xrange(center[0] - 1, center[0] + 2):
            for j in xrange(center[1] - 1, center[1] + 2):
                #print i, j
                c1 = self.labimg[j+1, i]
                c2 = self.labimg[j, i+1]
                c3 = self.labimg[j, i]
                #print ((c1[0] - c3[0])**2)**0.5
                #print ((c2[0] - c3[0])**2)**0.5
                if ((c1[0] - c3[0])**2)**0.5 + ((c2[0] - c3[0])**2)**0.5 < min_grad:
                    min_grad = abs(c1[0] - c3[0]) + abs(c2[0] - c3[0])
                    loc_min = [i, j]
        #print loc_min, "loc_min"
        return loc_min

    def _computeDist(self, ci, pixel, color):
        dc = (sum((self.centers[ci][:3] - color)**2))**0.5
        ds = (sum((self.centers[ci][3:] - pixel)**2))**0.5
        return ((dc/self.nc)**2 + (ds/self.ns)**2)**0.5

img = cv2.imread(sys.argv[1])
nr_superpixels = int(sys.argv[2])
nc = int(sys.argv[3])
#img = cv2.resize(img, (300, 200))
step = int((img.shape[0]*img.shape[1]/nr_superpixels)**0.5)
print step, "step"
slic = SLIC(img, step, nc)
slic.generateSuperPixels()
slic.createConnectivity()
cv2.imshow("cluster", slic.clusters.astype(np.uint8))
print slic.clusters
print slic.new_clusters
cv2.imshow("img", slic.img)
slic.displayContours((255, 0, 0))
cv2.imshow("fin", slic.img)
cv2.waitKey(0)
cv2.imwrite("SLICimg.jpg", slic.img)<commit_msg>cleaning up<commit_after>import numpy as np

class SLIC:
    def __init__(self, img, step, nc):
        self.img = img
        self.height, self.width = img.shape[:2]
        self._convertToLAB()
        self.step = step
        self.nc = nc
        self.ns = step
        self.FLT_MAX = 1000000
        self.ITERATIONS = 10

    def _convertToLAB(self):
        try:
            import cv2
            self.labimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB).astype(np.float64)
        except ImportError:
            self.labimg = np.copy(self.img)
            for i in xrange(self.labimg.shape[0]):
                for j in xrange(self.labimg.shape[1]):
                    rgb = self.labimg[i, j]
                    self.labimg[i, j] = self._rgb2lab(tuple(reversed(rgb)))

    def _rgb2lab ( self, inputColor ) :

       num = 0
       RGB = [0, 0, 0]

       for value in inputColor :
           value = float(value) / 255

           if value > 0.04045 :
               value = ( ( value + 0.055 ) / 1.055 ) ** 2.4
           else :
               value = value / 12.92

           RGB[num] = value * 100
           num = num + 1

       XYZ = [0, 0, 0,]

       X = RGB [0] * 0.4124 + RGB [1] * 0.3576 + RGB [2] * 0.1805
       Y = RGB [0] * 0.2126 + RGB [1] * 0.7152 + RGB [2] * 0.0722
       Z = RGB [0] * 0.0193 + RGB [1] * 0.1192 + RGB [2] * 0.9505
       XYZ[ 0 ] = round( X, 4 )
       XYZ[ 1 ] = round( Y, 4 )
       XYZ[ 2 ] = round( Z, 4 )

       XYZ[ 0 ] = float( XYZ[ 0 ] ) / 95.047         # ref_X =  95.047   Observer= 2°, Illuminant= D65
       XYZ[ 1 ] = float( XYZ[ 1 ] ) / 100.0          # ref_Y = 100.000
       XYZ[ 2 ] = float( XYZ[ 2 ] ) / 108.883        # ref_Z = 108.883

       num = 0
       for value in XYZ :

           if value > 0.008856 :
               value = value ** ( 0.3333333333333333 )
           else :
               value = ( 7.787 * value ) + ( 16 / 116 )

           XYZ[num] = value
           num = num + 1

       Lab = [0, 0, 0]

       L = ( 116 * XYZ[ 1 ] ) - 16
       a = 500 * ( XYZ[ 0 ] - XYZ[ 1 ] )
       b = 200 * ( XYZ[ 1 ] - XYZ[ 2 ] )

       Lab [ 0 ] = round( L, 4 )
       Lab [ 1 ] = round( a, 4 )
       Lab [ 2 ] = round( b, 4 )

       return Lab

    def generateSuperPixels(self):
        self._initData()
        indnp = np.mgrid[0:self.height,0:self.width].swapaxes(0,2).swapaxes(0,1)
        for i in range(self.ITERATIONS):
            self.distances = self.FLT_MAX * np.ones(self.img.shape[:2])
            for j in xrange(self.centers.shape[0]):
                xlow, xhigh = int(self.centers[j][3] - self.step), int(self.centers[j][3] + self.step)
                ylow, yhigh = int(self.centers[j][4] - self.step), int(self.centers[j][4] + self.step)

                if xlow <= 0:
                    xlow = 0
                if xhigh > self.width:
                    xhigh = self.width
                if ylow <=0:
                    ylow = 0
                if yhigh > self.height:
                    yhigh = self.height

                cropimg = self.labimg[ylow : yhigh , xlow : xhigh]
                colordiff = cropimg - self.labimg[self.centers[j][4], self.centers[j][3]]
                colorDist = np.sqrt(np.sum(np.square(colordiff), axis=2))

                yy, xx = np.ogrid[ylow : yhigh, xlow : xhigh]
                pixdist = ((yy-self.centers[j][4])**2 + (xx-self.centers[j][3])**2)**0.5
                dist = ((colorDist/self.nc)**2 + (pixdist/self.ns)**2)**0.5

                distanceCrop = self.distances[ylow : yhigh, xlow : xhigh]
                idx = dist < distanceCrop
                distanceCrop[idx] = dist[idx]
                self.distances[ylow : yhigh, xlow : xhigh] = distanceCrop
                self.clusters[ylow : yhigh, xlow : xhigh][idx] = j

            for k in xrange(len(self.centers)):
                idx = (self.clusters == k)
                colornp = self.labimg[idx]
                distnp = indnp[idx]
                self.centers[k][0:3] = np.sum(colornp, axis=0)
                sumy, sumx = np.sum(distnp, axis=0)
                self.centers[k][3:] = sumx, sumy
                self.centers[k] /= np.sum(idx)

    def _initData(self):
        self.clusters = -1 * np.ones(self.img.shape[:2])
        self.distances = self.FLT_MAX * np.ones(self.img.shape[:2])

        centers = []
        for i in xrange(self.step, self.width - self.step/2, self.step):
            for j in xrange(self.step, self.height - self.step/2, self.step):

                nc = self._findLocalMinimum(center=(i, j))
                color = self.labimg[nc[1], nc[0]]
                center = [color[0], color[1], color[2], nc[0], nc[1]]
                centers.append(center)
        self.center_counts = np.zeros(len(centers))
        self.centers = np.array(centers)

    def createConnectivity(self):
        label = 0
        adjlabel = 0
        lims = self.width * self.height / self.centers.shape[0]
        dx4 = [-1, 0, 1, 0]
        dy4 = [0, -1, 0, 1]
        new_clusters = -1 * np.ones(self.img.shape[:2]).astype(np.int64)
        elements = []
        for i in xrange(self.width):
            for j in xrange(self.height):
                if new_clusters[j, i] == -1:
                    elements = []
                    elements.append((j, i))
                    for dx, dy in zip(dx4, dy4):
                        x = elements[0][1] + dx
                        y = elements[0][0] + dy
                        if (x>=0 and x < self.width and
                            y>=0 and y < self.height and
                            new_clusters[y, x] >=0):
                            adjlabel = new_clusters[y, x]
                count = 1
                c = 0
                while c < count:
                    for dx, dy in zip(dx4, dy4):
                        x = elements[c][1] + dx
                        y = elements[c][0] + dy

                        if (x>=0 and x<self.width and y>=0 and y<self.height):
                            if new_clusters[y, x] == -1 and self.clusters[j, i] == self.clusters[y, x]:
                                elements.append((y, x))
                                new_clusters[y, x] = label
                                count+=1
                    c+=1
                if (count <= lims >> 2):
                    for c in range(count):
                        new_clusters[elements[c]] = adjlabel
                    label-=1
                label+=1
        self.new_clusters = new_clusters

    def displayContours(self, color):
        dx8 = [-1, -1, 0, 1, 1, 1, 0, -1]
        dy8 = [0, -1, -1, -1, 0, 1, 1, 1]

        isTaken = np.zeros(self.img.shape[:2], np.bool)
        contours = []

        for i in xrange(self.width):
            for j in xrange(self.height):
                nr_p = 0
                for dx, dy in zip(dx8, dy8):
                    x = i + dx
                    y = j + dy
                    if x>=0 and x < self.width and y>=0 and y < self.height:
                        if isTaken[y, x] == False and self.clusters[j, i] != self.clusters[y, x]:
                            nr_p += 1

                if nr_p >= 2:
                    isTaken[j, i] = True
                    contours.append([j, i])

        for i in xrange(len(contours)):
            self.img[contours[i][0], contours[i][1]] = color

    def _findLocalMinimum(self, center):
        min_grad = self.FLT_MAX
        loc_min = center
        for i in xrange(center[0] - 1, center[0] + 2):
            for j in xrange(center[1] - 1, center[1] + 2):
                c1 = self.labimg[j+1, i]
                c2 = self.labimg[j, i+1]
                c3 = self.labimg[j, i]
                if ((c1[0] - c3[0])**2)**0.5 + ((c2[0] - c3[0])**2)**0.5 < min_grad:
                    min_grad = abs(c1[0] - c3[0]) + abs(c2[0] - c3[0])
                    loc_min = [i, j]
        return loc_min<|endoftext|>


<commit_before>
class Slice:
    def __init__(self, theMatrix, x1, y1):
        self.theMatrix = theMatrix
        self.x1 = x1
        self.y1 = y1
        self.x2 = x1
        self.y2 = y1
        self.numOfCells = 1

    def addCells(self, x, y):
        pass

    def countIngredient(self, inredient):
        pass<commit_msg>adding more support for slices logic<commit_after>
class Slice:
    def __init__(self, theMatrix, x1, y1):
        self.theMatrix = theMatrix
        self.x1 = x1
        self.y1 = y1
        self.x2 = x1
        self.y2 = y1
        self.numOfCells = 1

    def addCells(self, x, y):
        if not self.checkPosition(x, y):
            raise Exception("collision on adding")
        if self.x1 > x:
            self.x1 = x
        if self.x2 < x:
            self.x2 = x
        if self.y1 > y:
            self.y1 = y
        if self.y2 < y:
            self.y2 = y

    def countIngredient(self, ingredient):
        counter = 0
        for i in range(self.x1, self.x2 + 1):
            for j in range(self.y1, self.y2 + 1):
                if self.theMatrix[i][j] == ingredient:
                    counter += 1
        return counter

    def checkInterception(self, otherSlice):
        theResult = False
        for i in range(self.x1, self.x2 + 1):
            for j in range(self.y1, self.y2 + 1):
                theResult = theResult or self.checkPosition(i, j)
        return theResult

    def checkPosition(self, x, y):
        if (self.x1 < x < self.x2 or self.x2 < x < self.x1) and (self.y1 < y < self.y1 or self.y2 < y < self.y1):
            return False
        return True<|endoftext|>


<commit_before>#--coding:utf-8--
import re
import sys
import json
import time
import chardet
import getpass
import keyring
import mechanize

typeEncode = sys.getfilesystemencoding()

class Spider:

    def __init__(self, DownloaderQueue, InfoExtracter, debug = False):

        self.DownloaderQueue = DownloaderQueue

        self.Prefix = "https://m.facebook.com/"

        #Browser
        self.br = mechanize.Browser()
        self.cookies = mechanize.CookieJar()

        #options
        self.br.set_handle_robots(False)

        #Follows refresh 0 but not hangs on refresh > 0
        self.br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

        #debugging?
        self.br.set_debug_http(debug)
        self.br.set_debug_redirects(debug)
        self.br.set_debug_responses(debug)

        #User-Agent (this is cheating, ok?)

        self.br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

        # Info Extracter
        self.Extracter = InfoExtracter

    def Login(self, Email = None, Pass = None, UsingSavedAccount = False, UsingSavedPass = False):
        self.br.open("http://m.facebook.com/")
        self.br.select_form(nr=0)
        #self.br['locale'] = 'en_US'
        #self.br['non_com_login'] = ''
        if (Email != None):
            self.br['email'] = Email
        elif UsingSavedAccount:
            self.br['email'] = keyring.get_password('FbSpider', 'Account')
        else:
            self.br['email'] = raw_input('E-mail Address: ')

        if (Pass != None):
            self.br['pass'] = Pass
        elif UsingSavedPass:
            self.br['pass'] = keyring.get_password('FbSpider', self.br['email'])
        else:
            self.br['pass'] = getpass.getpass('Password: ')
        #self.br['lsd'] = '20TO1'
        response = self.br.submit()
        if (self.Load(self.Prefix).find('个人主页')) > 0:
            print "Info: Login Success!"
        else:
            print "Warning: Login Insuccess"

    def FileDownload(self, URL, path):
        self.DownloaderQueue.put((URL, path))

    def Download(self, URL, path):
        content = self.br.open(URL).read()
        infoencode = chardet.detect(content).get('encoding','utf-8')
        A = content.decode(infoencode,'ignore').encode(typeEncode)
        f = open(path,'w')
        f.write(A)
        f.close()

    def Load(self, URL):
        print "URL = %s" %URL
        content = ''
        try:
            content = self.br.open(URL).read()
        except mechanize._mechanize.LinkNotFoundError, Info:
            print "Error: mechanize._mechanize.LinkNotFoundError ", Info
            return ''

        infoencode = chardet.detect(content).get('encoding', 'utf-8')
        return content.decode(infoencode, 'ignore').encode(typeEncode)

    def UserInit(self, username, idType = 'username', startindex = '0'):
        self.CurrentUser = username
        self.idType = idType
        self.StartIndex = startindex
        self.UserPrefix = ''
        if (idType == 'username'):
            self.UserPrefix = self.Prefix + username + "/friends?all=1&startindex="
        elif (idType == 'uid'):
            self.UserPrefix = self.Prefix + 'profile.php?v=friends&all=1&id=' + self.CurrentUser + '&startindex='
        else:
            print "Error: Unknown User Identification Type!"

    def Scan(self, username, idType = 'username', startindex = '0'):
        startindex = str(startindex)
        self.UserInit(username, idType = idType, startindex = startindex)
        self.Extracter.UserInit(username = username, idType = idType)

        # Get Personal Info
        string = self.Load(self.Prefix + self.CurrentUser + '/')
        self.Extracter.ScanProfile(string)

        # Get Friends Profile Info
        string = self.Load(self.UserPrefix + self.StartIndex)

        self.Extracter.ScanFriendsProfile(string)

        # Specifically for First Page
        self.StartIndex = self.Extracter.ScanFriends(string, FirstPage = True)

        # Read Friends List
        while (True):
            print "Info: Sleep: 5 Sec"
            time.sleep(5)
            try:
                string = self.Load(self.UserPrefix + self.StartIndex)
            except TypeError:
                return

            self.StartIndex = self.Extracter.ScanFriends(string)

    def Output(self, WriteHandle):
        json.dump(self.Extracter.ContentMake(), WriteHandle)
<commit_msg>Change a name of a variable<commit_after>#--coding:utf-8--
import re
import sys
import json
import time
import chardet
import getpass
import keyring
import mechanize

typeEncode = sys.getfilesystemencoding()

class Spider:

    def __init__(self, DownloaderQueue, InfoExtracter, debug = False):

        self.DownloaderQueue = DownloaderQueue

        self.Prefix = "https://m.facebook.com/"

        #Browser
        self.br = mechanize.Browser()
        self.cookies = mechanize.CookieJar()

        #options
        self.br.set_handle_robots(False)

        #Follows refresh 0 but not hangs on refresh > 0
        self.br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

        #debugging?
        self.br.set_debug_http(debug)
        self.br.set_debug_redirects(debug)
        self.br.set_debug_responses(debug)

        #User-Agent (this is cheating, ok?)

        self.br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

        # Info Extracter
        self.Extracter = InfoExtracter

    def Login(self, Email = None, Pass = None, UsingSavedAccount = False, UsingSavedPass = False):
        self.br.open("http://m.facebook.com/")
        self.br.select_form(nr=0)
        #self.br['locale'] = 'en_US'
        #self.br['non_com_login'] = ''
        if (Email != None):
            self.br['email'] = Email
        elif UsingSavedAccount:
            self.br['email'] = keyring.get_password('FbSpider', 'Account')
        else:
            self.br['email'] = raw_input('E-mail Address: ')

        if (Pass != None):
            self.br['pass'] = Pass
        elif UsingSavedPass:
            self.br['pass'] = keyring.get_password('FbSpider', self.br['email'])
        else:
            self.br['pass'] = getpass.getpass('Password: ')
        #self.br['lsd'] = '20TO1'
        response = self.br.submit()
        if (self.Load(self.Prefix).find('个人主页')) > 0:
            print "Info: Login Success!"
        else:
            print "Warning: Login Insuccess"

    def FileDownload(self, URL, path):
        self.DownloaderQueue.put((URL, path))

    def Download(self, URL, path):
        content = self.br.open(URL).read()
        infoencode = chardet.detect(content).get('encoding','utf-8')
        A = content.decode(infoencode,'ignore').encode(typeEncode)
        f = open(path,'w')
        f.write(A)
        f.close()

    def Load(self, URL):
        print "URL = %s" %URL
        content = ''
        try:
            content = self.br.open(URL).read()
        except mechanize._mechanize.LinkNotFoundError, Info:
            print "Error: mechanize._mechanize.LinkNotFoundError ", Info
            return ''

        infoencode = chardet.detect(content).get('encoding', 'utf-8')
        return content.decode(infoencode, 'ignore').encode(typeEncode)

    def UserInit(self, username, idType = 'username', startindex = '0'):
        self.CurrentUser = username
        self.idType = idType
        self.StartIndex = startindex
        self.UserPrefix = ''
        if (idType == 'username'):
            self.UserPrefix = self.Prefix + self.CurrentUser + "/friends?all=1&startindex="
        elif (idType == 'uid'):
            self.UserPrefix = self.Prefix + 'profile.php?v=friends&all=1&id=' + self.CurrentUser + '&startindex='
        else:
            print "Error: Unknown User Identification Type!"

    def Scan(self, username, idType = 'username', startindex = '0'):
        startindex = str(startindex)
        self.UserInit(username, idType = idType, startindex = startindex)
        self.Extracter.UserInit(username = username, idType = idType)

        # Get Personal Info
        string = self.Load(self.Prefix + self.CurrentUser + '/')
        self.Extracter.ScanProfile(string)

        # Get Friends Profile Info
        string = self.Load(self.UserPrefix + self.StartIndex)

        self.Extracter.ScanFriendsProfile(string)

        # Specifically for First Page
        self.StartIndex = self.Extracter.ScanFriends(string, FirstPage = True)

        # Read Friends List
        while (True):
            print "Info: Sleep: 5 Sec"
            time.sleep(5)
            try:
                string = self.Load(self.UserPrefix + self.StartIndex)
            except TypeError:
                return

            self.StartIndex = self.Extracter.ScanFriends(string)

    def Output(self, WriteHandle):
        json.dump(self.Extracter.ContentMake(), WriteHandle)
<|endoftext|>