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

#title           :spark.py
#description     :The current python file reads the data from s3 and runs job on top of it
#author		     :Ajay Krishna Teja Kavuri
#date            :02012017
#version         :0.1
#==============================================================================

# Libraries
from pyspark import SparkContext
import sys
import traceback
#==============================================================================

# Implementation

class Spark():

    # Initialize
    def __init__(self,clusterURL = "local",appName = "AUTH"):
        # Define the file location
        self.source = "/home/ubuntu/MyInsightRepo/README.md"

        # Define the cluster url
        self.clusterURL = clusterURL

        # Define the app name
        self.appName = appName

        # Debug statement
        print "Initialized"

    # Method to call for spark context
    def getContext(self):
        # get back the spark context
        self.sparkCntxt = SparkContext(self.clusterURL,self.appName)

        return self.sparkCntxt

    # Method would return the source context for spark
    def getDataCache(self):
        # pass the source with context
        cacheData = self.sparkCntxt.textFile(self.source).cache()

        return cacheData

    # Method has sets the bucket name and file path
    def setS3Attrs(self,bucketName,filePath):
        # Set the attributes
        self.bucketName = bucketName
        self.filePath = filePath

    # Fetch data from s3 instead of manual text files
    def getDataS3(self):

        try:
            # build the query from S3
            s3Uri = "s3n://"+str(self.bucketName)+"/"+str(self.filePath)
            # Get the file from S3
            s3Data = self.sparkCntxt.textFile(s3Uri)

            return s3Data

        # Exception
        except Exception:
            traceback.print_exc()
            return False

    # Method that will implement the actual jobs
    def runJobWithCache(self,cacheData):
        # Simple filter for testing
        numOfAs = cacheData.filter(lambda s: 'a' in s).count()

        # Debug statements
        print "Number of A's:" + str(numOfAs)

    # Job for counting consumption
    def runJob(self,data):
        print data.count()

# main method
if __name__ == '__main__':

    # Get the arguments that you need
    bucketName = str(sys.argv[1])
    filePath = str(sys.argv[2])

    # Initialize the class
    thisSparkJob = Spark()

    # get the context
    sc = thisSparkJob.getContext()

    # Set s3 parameters
    thisSparkJob.setS3Attrs(bucketName,filePath)

    # get the data cache
    data = thisSparkJob.getDataS3()

    # run the job
    thisSparkJob.runJob(data)

    # stop the job
    sc.stop()
<commit_msg>Added the job for summing by user id<commit_after>#!/usr/bin/env python

#title           :spark.py
#description     :The current python file reads the data from s3 and runs job on top of it
#author		     :Ajay Krishna Teja Kavuri
#date            :02012017
#version         :0.1
#==============================================================================

# Libraries
from pyspark import SparkContext
import sys
import traceback
#==============================================================================

# Implementation

class Spark():

    # Initialize
    def __init__(self,clusterURL = "local",appName = "AUTH"):
        # Define the file location
        self.source = "/home/ubuntu/MyInsightRepo/README.md"

        # Define the cluster url
        self.clusterURL = clusterURL

        # Define the app name
        self.appName = appName

        # Debug statement
        print "Initialized"

    # Method to call for spark context
    def getContext(self):
        # get back the spark context
        self.sparkCntxt = SparkContext(self.clusterURL,self.appName)

        return self.sparkCntxt

    # Method would return the source context for spark
    def getDataCache(self):
        # pass the source with context
        cacheData = self.sparkCntxt.textFile(self.source).cache()

        return cacheData

    # Method has sets the bucket name and file path
    def setS3Attrs(self,bucketName,filePath):
        # Set the attributes
        self.bucketName = bucketName
        self.filePath = filePath

    # Fetch data from s3 instead of manual text files
    def getDataS3(self):

        try:
            # build the query from S3
            s3Uri = "s3n://"+str(self.bucketName)+"/"+str(self.filePath)
            # Get the file from S3
            s3Data = self.sparkCntxt.textFile(s3Uri)

            return s3Data

        # Exception
        except Exception:
            traceback.print_exc()
            return False

    # Method that will implement the actual jobs
    def runJobWithCache(self,cacheData):

        try:
            # Simple filter for testing
            numOfAs = cacheData.filter(lambda s: 'a' in s).count()

            # Debug statements
            print "Number of A's:" + str(numOfAs)

        # Exception
        except Exception:
            traceback.print_exc()
            return False

    # Job for counting consumption
    def runJob(self,data):

        try:
            # Run the job, key by user_id and add the consumption
            cnsmptnByUser = data.flatMap(lambda x:x.split('\n'))
                                .map(lambda x:(str(x.split(',')[0]),float(x.split(',')[5])))
                                .reduceByKey(add)

            # Collect the results
            output = cnsmptnByUser.collect()

            return output

        # Exception
        except Exception:
            traceback.print_exc()
            return False

# main method
if __name__ == '__main__':

    # Get the arguments that you need
    bucketName = str(sys.argv[1])
    filePath = str(sys.argv[2])

    # Initialize the class
    thisSparkJob = Spark()

    # get the context
    sc = thisSparkJob.getContext()

    # Set s3 parameters
    thisSparkJob.setS3Attrs(bucketName,filePath)

    # get the data cache
    data = thisSparkJob.getDataS3()

    # run the job
    output = thisSparkJob.runJob(data)
    for key,value in output:
        print "User: "+str(key)+"has consumption of: "+str(value)

    # stop the job
    sc.stop()
<|endoftext|>


<commit_before>import numpy as np

# ==============================================================================
#                                                              PIXELS_WITH_VALUE
# ==============================================================================
def pixels_with_value(img, val):
    """ Given an image as a numpy array, and a value representing the
        pixel values, eg [128,255,190] in an RGB image, then it returns
        a 2D boolean array with a True for every pixel position that has
        that value.
    """
    return np.all(img==np.array(val), axis=2)

# ==============================================================================
#                                                                   RGB2SEGLABEL
# ==============================================================================
def rgb2seglabel(img, colormap, channels_axis=False):
    """ Given an RGB image stored as a numpy array, and a colormap that
        maps from label id to an RGB color for that label, it returns a
        new numpy array with color chanel size of 1 where the pixel
        intensity values represent the class label id.

    Args:
        img:            (np array)
        colormap:       (list) list of pixel values for each class label id
        channels_axis:  (bool)(default=False) Should it return an array with a
                        third (color channels) axis of size 1?
    """
    height, width, _ = img.shape
    if channels_axis:
        label = np.zeros([height, width,1], dtype=np.uint8)
    else:
        label = np.zeros([height, width], dtype=np.uint8)
    for id in range(len(colormap)):
        label[np.all(img==np.array(idcolormap[id]), axis=2)] = id
    return label
<commit_msg>FEAT: Add load_image_and_seglabels()<commit_after>import numpy as np

# ==============================================================================
#                                                              PIXELS_WITH_VALUE
# ==============================================================================
def pixels_with_value(img, val):
    """ Given an image as a numpy array, and a value representing the
        pixel values, eg [128,255,190] in an RGB image, then it returns
        a 2D boolean array with a True for every pixel position that has
        that value.
    """
    return np.all(img==np.array(val), axis=2)

# ==============================================================================
#                                                                   RGB2SEGLABEL
# ==============================================================================
def rgb2seglabel(img, colormap, channels_axis=False):
    """ Given an RGB image stored as a numpy array, and a colormap that
        maps from label id to an RGB color for that label, it returns a
        new numpy array with color chanel size of 1 where the pixel
        intensity values represent the class label id.

    Args:
        img:            (np array)
        colormap:       (list) list of pixel values for each class label id
        channels_axis:  (bool)(default=False) Should it return an array with a
                        third (color channels) axis of size 1?
    """
    height, width, _ = img.shape
    if channels_axis:
        label = np.zeros([height, width,1], dtype=np.uint8)
    else:
        label = np.zeros([height, width], dtype=np.uint8)
    for id in range(len(colormap)):
        label[np.all(img==np.array(idcolormap[id]), axis=2)] = id
    return label

def load_image_and_seglabels(input_files, label_files, colormap, shape=(32,32), n_channels=3, label_chanel_axis=False):
    # Dummy proofing
    assert n_channels in {1,3}, "Incorrect value for n_channels. Must be 1 or 3. Got {}".format(n_channels)

    # Image dimensions
    width, height = shape
    n_samples = len(label_files)

    # Initialize input and label batch arrays
    X = np.zeros([n_samples, height, width, n_channels], dtype=np.uint8)
    if label_chanel_axis:
        Y = np.zeros([n_samples, height, width, 1], dtype=np.uint8)
    else:
        Y = np.zeros([n_samples, height, width], dtype=np.uint8)

    for i in range(n_samples):
        # Get filenames of input and label
        img_file = input_files[i]
        label_file = label_files[i]

        # Resize input and label images
        img = PIL.Image.open(img_file).resize(shape, resample=PIL.Image.CUBIC)
        label_img = PIL.Image.open(label_file).resize(shape, resample=PIL.Image.NEAREST)

        # Convert back to numpy arrays
        img = np.asarray(img, dtype=np.uint8)
        label_img = np.asarray(label_img, dtype=np.uint8)

        # Convert label image from RGB to single value int class labels
        if colormap is not None:
            label_img = rgb2seglabel(label_img, colormap=colormap, channels_axis=label_chanel_axis)

        # Add processed images to batch arrays
        X[i] = img
        Y[i] = label_img

    return X, Y
<|endoftext|>


<commit_before>#!/usr/bin/python
# coding:utf-8

import cPickle
import bisect
from pymongo import MongoClient

class BaseInfo():
    def dump_self(self):
        return cPickle.dumps(self)

class ChannelInfo(BaseInfo):
    def __init__(self):
        # {["FF38::1:1", "2001:1::20"]: {"datapath1":ChannelSwitchInfoのインスタンス, ... } という形
        self.channel_info = {}
#        self.accessor = DatabaseAccessor()

    def add_info(self, mc_addr, serv_ip, data_path, port_no, cid):
        """
          視聴端末を追加。さらに、
            1. ch視聴ユーザが当該swにおける最初の視聴ユーザだった場合、エッジルータへ
               report(ADD_NEW_RESOURCESおよびCHANGE_TO_INCLUDE)を投げる。
               また、エッジSWおよび収容SWへFlowMod
            2. ch視聴ユーザが当該swの当該ポートにおける最初の視聴ユーザだった場合
               (他ポートには既存ユーザがいる)、収容SWへFlowMod
          FlowModは本関数の戻り値で返す？本関数内で送信までやりたくない。もっといえば
          FlowModの組み立ても本関数外でやりたい。BEサービスか品質保証かの判定も外で。
        """

        # チャンネル存在チェック
        if (mc_addr, serv_ip) not in self.channel_info:
            # 当該チャンネルが存在しない場合
            sw_info = ChannelSwitchInfo(data_path, port_no, cid)
            self.channel_info[(mc_addr, serv_ip)] = {data_path: sw_info}
            return 2  # エッジSW、収容SW両方へのFlowMod、およびエッジルータへのReport Todo:即値やめろ

        # 当該チャンネルが既に存在する場合
        # DataPath存在チェック
        sw_info = self.channel_info[(mc_addr, serv_ip)]
        if data_path not in sw_info:
            new_sw_info = ChannelSwitchInfo(data_path, port_no, cid)
            sw_info[data_path] = new_sw_info
            return 1  # 収容SWへのFlowModが必要 Todo:即値やめろ

        # 当該チャンネルにこの収容SWの情報がある場合
        ch_sw_info = sw_info[data_path]  # ChannelSwitchInfoクラスのインスタンス
        return ch_sw_info.add_info(port_no, cid)

    def remove_info(self, mc_addr, serv_ip, data_path, port_no, cid):
        """
          視聴端末を削除。さらに、
            1. 当該sw、当該ポートの視聴ユーザが0になった場合、
               収容SWにFlowMod
            2. 当該swの視聴ユーザが0になった場合
               エッジSW、収容SWへFlowMod
               エッジルータへReport(BLOCK_OLD_SOURCES)を投げる
          FlowModは本関数の戻り値で返す？本関数内で送信までやりたくない。もっといえば
          FlowModの組み立ても本関数外でやりたい。BEサービスか品質保証かの判定も外で。
        """

        # チャンネルおよびDataPath存在チェック
        # 存在しなければ何もしない
        if (mc_addr, serv_ip) not in self.channel_info \
            or data_path not in self.channel_info[mc_addr]:
                return 0  # FlowModの必要なし Todo: 即値やめろ

        # 存在する場合
        ch_sw_info = self.channel_info[(mc_addr, serv_ip)][data_path]
        ret = ch_sw_info.remove_info(port_no, cid)
        if ret == 2:
            # 当該SWの視聴ユーザが0の場合、DataPathに対応する情報を削除する
            self.channel_info[mc_addr].pop(data_path)

        return ret

class ChannelSwitchInfo(BaseInfo):
    def __init__(self, data_path, port_no=-1, cid=-1):
        self.data_path = data_path
        self.port_info = {}
        if cid != -1:
            self.port_info[port_no] = [cid]

    def add_info(self, port_no, cid):
        # port_infoにユーザ情報を追加
        if port_no in self.port_info:
            # 当該ポートに視聴ユーザが存在する場合
            # 当該CIDが存在しない場合はCIDを追加
            # ソートを維持して挿入しておく(探索時にbinary searchを使いたい)
            cid_list = self.port_info[port_no]
            if self.find(cid_list, cid) == -1:
                pos = bisect.bisect(cid_list, cid)
                bisect.insort(cid_list, cid)
            # Todo: 既にCIDが存在する場合に無視する処理でよいか精査
            return 0  # FlowMod必要なし Todo: 即値やめろ
        else:
            # 当該ポートに視聴ユーザが存在しない場合
            self.port_info[port_no] = [cid]
            return 1  # 収容SWへのFlowModが必要 Todo: 即値やめろ

    def remove_info(self, port_no, cid):
        # port_infoから当該ユーザ情報を検索し削除
        # ch_infoを更新
        #   当該chを視聴しているユーザがいなくなった場合
        if port_no not in self.port_info:
            # 当該ポートにユーザがそもそも存在しない場合
            # 何もせず抜ける Todo: 本当にそれでよいか精査
            return 0  # FlowMod必要なし Todo: 即値やめろ

        # 当該ポートにユーザが存在する場合
        # cidを探索し、存在すれば削除
        # ユーザが0になればポート情報も削除
        cid_list = self.port_info[port_no]
        idx = self.find(cid_list, cid)
        if idx == -1:
            # 指定されたCIDが存在しなければ何もせず抜ける Todo: 本当にそれでよいか精査
            return 0  # FlowMod必要なし Todo: 即値やめろ
        cid_list.pop(idx)
        if len(cid_list) == 0:
            self.port_info.pop(port_no)
            return 1  # 収容SWへのFlowModが必要 Todo: 即値やめろ
        if len(self.port_info) == 0:
            return 2  # エッジSW、収容SW両方へのFlowMod、およびエッジルータへのReport Todo:即値やめろ

    """
    ソート済み配列からキー値を探索
    arrayはソート済みであること
    Todo: 共通クラスに移動すべきか検討
    """
    def find(self, array, value):
        idx = bisect.bisect_left(array, value)
        if idx != len(array) and array[idx] == value:
            return idx
        return -1

class DatabaseAccessor:
    def __init__(self, connect_str):
        self.client = MongoClient(connect_str)
        # Todo: DB名、コレクション名は別途検討
        self.db = self.client.viewerdb
        self.col = self.db.serialized_data

    def insert(self, key, inserted_obj):
        # Todo: DB上のデータ形式は別途検討
        dump = inserted_obj.dump_self(inserted_obj)
        self.col.update({key: dump})

    def query(self, key):
        result = self.col.find_one()
        dump = result[key]
        return cPickle.loads(dump)

"""
class UserInfo:
    def __init__(self, port_no=-1):
        self.port_no = port_no
        self.float = float
        self.array = [1, 2, "123"]
        self.dict = {"key1": page(), "key2": page(str="test")}

if '__main__' == __name__:
    a = hoge(int=5)
    b = hoge()
    b.int = 2
    print a.int
    print b.int

    # open mongodb
    client = MongoClient("mongodb://localhost:27017")
    db = client.testdb
    col = db.posts

    # serialize
    dump_a = cpickle.dumps(a)
    dump_b = cpickle.dumps(b)

    c = cpickle.loads(dump_a)

    # insert
    for i in range(0, 10000):
        dict_a = {"switch_name": "s1", "data": dump_a}
        col.update({"switch_name": "s1"}, \
                   {"$set": {"data": dump_a}}, \
                   upsert=True)

    # query
    result = col.find_one({"switch_name": "s1"})
    dump_result = result["data"]
    load_result = cpickle.loads(dump_result)

    # check
    print load_result.int
    print load_result.float
    print load_result.dict["key1"].str
    print load_result.dict["key2"].str
"""
<commit_msg>削除時の動作を修正<commit_after>#!/usr/bin/python
# coding:utf-8

import cPickle
import bisect
from pymongo import MongoClient

class BaseInfo():
    def dump_self(self):
        return cPickle.dumps(self)

class ChannelInfo(BaseInfo):
    def __init__(self):
        print("ChannelInfo : __init__")
        # {["FF38::1:1", "2001:1::20"]: {"datapath1":ChannelSwitchInfoのインスタンス, ... } という形
        self.channel_info = {}
#        self.accessor = DatabaseAccessor()

    def add_info(self, mc_addr, serv_ip, data_path, port_no, cid):
        print("ChannelInfo : add_info(%s, %s, %s, %s, %s)" %
               (mc_addr, serv_ip, str(data_path), str(port_no), str(cid)))
        print("self.channel_info : %s" % self.channel_info)
        """
          視聴端末を追加。さらに、
            1. ch視聴ユーザが当該swにおける最初の視聴ユーザだった場合、エッジルータへ
               report(ADD_NEW_RESOURCESおよびCHANGE_TO_INCLUDE)を投げる。
               また、エッジSWおよび収容SWへFlowMod
            2. ch視聴ユーザが当該swの当該ポートにおける最初の視聴ユーザだった場合
               (他ポートには既存ユーザがいる)、収容SWへFlowMod
          FlowModは本関数の戻り値で返す？本関数内で送信までやりたくない。もっといえば
          FlowModの組み立ても本関数外でやりたい。BEサービスか品質保証かの判定も外で。
        """

        # チャンネル存在チェック
        if (mc_addr, serv_ip) not in self.channel_info:
            # 当該チャンネルが存在しない場合
            sw_info = ChannelSwitchInfo(data_path, port_no, cid)
            self.channel_info[(mc_addr, serv_ip)] = {data_path: sw_info}
            print("added self.channel_info : %s" % self.channel_info)
            return 2  # エッジSW、収容SW両方へのFlowMod、およびエッジルータへのReport Todo:即値やめろ

        # 当該チャンネルが既に存在する場合
        # DataPath存在チェック
        sw_info = self.channel_info[(mc_addr, serv_ip)]
        if data_path not in sw_info:
            new_sw_info = ChannelSwitchInfo(data_path, port_no, cid)
            sw_info[data_path] = new_sw_info
            print("added self.channel_info : %s" % self.channel_info)
            return 1  # 収容SWへのFlowModが必要 Todo:即値やめろ

        # 当該チャンネルにこの収容SWの情報がある場合
        ch_sw_info = sw_info[data_path]  # ChannelSwitchInfoクラスのインスタンス
        print("added self.channel_info : %s" % self.channel_info)
        return ch_sw_info.add_info(port_no, cid)

    def remove_info(self, mc_addr, serv_ip, data_path, port_no, cid):
        print("ChannelInfo : remove_info(%s, %s, %s, %s, %s)" %
               (mc_addr, serv_ip, str(data_path), str(port_no), str(cid)))
        print("self.channel_info : %s" % self.channel_info)
        """
          視聴端末を削除。さらに、
            1. 当該sw、当該ポートの視聴ユーザが0になった場合、
               収容SWにFlowMod
            2. 当該swの視聴ユーザが0になった場合
               エッジSW、収容SWへFlowMod
               エッジルータへReport(BLOCK_OLD_SOURCES)を投げる
          FlowModは本関数の戻り値で返す？本関数内で送信までやりたくない。もっといえば
          FlowModの組み立ても本関数外でやりたい。BEサービスか品質保証かの判定も外で。
        """

        # チャンネルおよびDataPath存在チェック
        # 存在しなければ何もしない
        if (mc_addr, serv_ip) not in self.channel_info \
            or data_path not in self.channel_info[(mc_addr, serv_ip)]:
                print "remove target is nothing."
                return 0  # FlowModの必要なし Todo: 即値やめろ

        # 存在する場合
        ch_sw_info = self.channel_info[(mc_addr, serv_ip)][data_path]
        ret = ch_sw_info.remove_info(port_no, cid)
        if ret == 1 and len(ch_sw_info.port_info.keys()) == 0:
            # 当該SWの視聴ユーザが0の場合、DataPathに対応する情報を削除する
            self.channel_info[(mc_addr, serv_ip)].pop(data_path)
            print("removed datapath : %s" % data_path)
            print("removed self.channel_info[(mc_addr, serv_ip)] : %s"
                   % self.channel_info[(mc_addr, serv_ip)])

            if len(self.channel_info[(mc_addr, serv_ip)]) == 0:
                # 当該mcグループの視聴ユーザが0の場合、mcグループに対応する情報を削除する
                self.channel_info.pop((mc_addr, serv_ip))
                ret = 2

        print("removed self.channel_info : %s" % self.channel_info)
        return ret

class ChannelSwitchInfo(BaseInfo):
    def __init__(self, data_path, port_no=-1, cid=-1):
        print("ChannelSwitchInfo : __init__")
        self.data_path = data_path
        self.port_info = {}
        if cid != -1:
            self.port_info[port_no] = [cid]

    def add_info(self, port_no, cid):
        print("ChannelSwitchInfo : add_info(%s, %s)" % (str(port_no), str(cid)))
        print("self.port_info : %s" % self.port_info)
        # port_infoにユーザ情報を追加
        if port_no in self.port_info:
            # 当該ポートに視聴ユーザが存在する場合
            # 当該CIDが存在しない場合はCIDを追加
            # ソートを維持して挿入しておく(探索時にbinary searchを使いたい)
            cid_list = self.port_info[port_no]
            if self.find(cid_list, cid) == -1:
                pos = bisect.bisect(cid_list, cid)
                bisect.insort(cid_list, cid)
            # Todo: 既にCIDが存在する場合に無視する処理でよいか精査
            return 0  # FlowMod必要なし Todo: 即値やめろ
        else:
            # 当該ポートに視聴ユーザが存在しない場合
            self.port_info[port_no] = [cid]
            print("added self.port_info : %s" % self.port_info)
            return 1  # 収容SWへのFlowModが必要 Todo: 即値やめろ

    def remove_info(self, port_no, cid):
        print("ChannelSwitchInfo : remove_info(%s, %s)" % (str(port_no), str(cid)))
        print("self.port_info : %s" % self.port_info)
        # port_infoから当該ユーザ情報を検索し削除
        # ch_infoを更新
        #   当該chを視聴しているユーザがいなくなった場合
        if port_no not in self.port_info:
            # 当該ポートにユーザがそもそも存在しない場合
            # 何もせず抜ける Todo: 本当にそれでよいか精査
            return 0  # FlowMod必要なし Todo: 即値やめろ

        # 当該ポートにユーザが存在する場合
        # cidを探索し、存在すれば削除
        # ユーザが0になればポート情報も削除
        cid_list = self.port_info[port_no]
        idx = self.find(cid_list, cid)
        if idx == -1:
            # 指定されたCIDが存在しなければ何もせず抜ける Todo: 本当にそれでよいか精査
            print "remove target is nothing."
            return 0  # FlowMod必要なし Todo: 即値やめろ
        cid_list.pop(idx)
        """
        if len(self.port_info) == 0:
            print("removed self.port_info : %s" % self.port_info)
            print("removed cid_list : %s, return 2" % cid_list)
            return 2  # エッジSW、収容SW両方へのFlowMod、およびエッジルータへのReport Todo:即値やめろ
        """
        if len(cid_list) == 0:
            self.port_info.pop(port_no)
            print("removed self.port_info : %s" % self.port_info)
            print("removed cid_list : %s, return 1" % cid_list)
            return 1  # 収容SWへのFlowModが必要 Todo: 即値やめろ

    """
    ソート済み配列からキー値を探索
    arrayはソート済みであること
    Todo: 共通クラスに移動すべきか検討
    """
    def find(self, array, value):
        idx = bisect.bisect_left(array, value)
        if idx != len(array) and array[idx] == value:
            return idx
        return -1

class DatabaseAccessor:
    def __init__(self, connect_str):
        self.client = MongoClient(connect_str)
        # Todo: DB名、コレクション名は別途検討
        self.db = self.client.viewerdb
        self.col = self.db.serialized_data

    def insert(self, key, inserted_obj):
        # Todo: DB上のデータ形式は別途検討
        dump = inserted_obj.dump_self(inserted_obj)
        self.col.update({key: dump})

    def query(self, key):
        result = self.col.find_one()
        dump = result[key]
        return cPickle.loads(dump)

"""
class UserInfo:
    def __init__(self, port_no=-1):
        self.port_no = port_no
        self.float = float
        self.array = [1, 2, "123"]
        self.dict = {"key1": page(), "key2": page(str="test")}

if '__main__' == __name__:
    a = hoge(int=5)
    b = hoge()
    b.int = 2
    print a.int
    print b.int

    # open mongodb
    client = MongoClient("mongodb://localhost:27017")
    db = client.testdb
    col = db.posts

    # serialize
    dump_a = cpickle.dumps(a)
    dump_b = cpickle.dumps(b)

    c = cpickle.loads(dump_a)

    # insert
    for i in range(0, 10000):
        dict_a = {"switch_name": "s1", "data": dump_a}
        col.update({"switch_name": "s1"}, \
                   {"$set": {"data": dump_a}}, \
                   upsert=True)

    # query
    result = col.find_one({"switch_name": "s1"})
    dump_result = result["data"]
    load_result = cpickle.loads(dump_result)

    # check
    print load_result.int
    print load_result.float
    print load_result.dict["key1"].str
    print load_result.dict["key2"].str
"""
<|endoftext|>


<commit_before>"""
==============================================
Phase-Amplitude Coupling tutorial on sEEG data
==============================================

In this example, we illustrate how to conduct a PAC analysis on real data. The
data used here are taken from Combrisson et al. 2017
:cite:`combrisson2017intentions`. The task is a center-out motor task where the
subject have to reach a target on a screen with the mouse. A single trial
consists in three periods :

    * From [-1000, 0]ms it is the baseline period (REST)
    * From [0, 1500]ms the subject have to prepare the movement (MOTOR
      PLANNING period)
    * From [1500, 4000]ms the subject perform the movement to reach the target
      on the screen (MOTOR EXECUTION period)

The recorded electrophysiological data comes from an epileptic subject with
electrodes deep inside the brain (intracranial EEG or
stereoelectroencephalography). Here, the provided data contains only a single
recording contact for one subject. This contact is located in the premotor
cortex.

When working with phase-amplitude coupling, there are basically three questions
you should try to answer :

    1. What is the range of phase frequencies supporting this coupling?
    2. Where in time this coupling occurs?
    3. What is the range of amplitude frequencies supporting this coupling?

In this tutorial we propose to answer to those three questions using following
structure :

    1. Compute the inter-trial coherence (ITC) and see if the phases are
       realigned at a given time point (:class:`tensorpac.utils.ITC`)
    2. Compute the Power Spectrum Density (PSD) to try to find the phase
       frequency range (:class:`tensorpac.utils.PSD`)
    3. Compute the Event-Related PAC (ERPAC) to have a first idea of where the
       coupling occurs in time, especially if it start at a given time point
       (:class:`tensorpac.EventRelatedPac`)
    4. Realign time-frequency representations based on the starting time-point
       found with the ERPAC and see if the gamma burst follow the rhythm
       imposed by a phase. This step should also gives and idea of the
       amplitude frequency range (:class:`tensorpac.utils.PeakLockedTF`)
    5. Compute the comodulogram and statistics in order to test if the
       hypothetic coupling can be considered as statistically different from
       PAC that could be obtained by chance (:class:`tensorpac.Pac`)
"""
import os
import urllib

import numpy as np
from scipy.io import loadmat

from tensorpac import Pac, EventRelatedPac
from tensorpac.utils import PeakLockedTF, PSD, ITC

import matplotlib.pyplot as plt

###############################################################################
# Download the data
###############################################################################
# Lets first start by downloading the data. The file should be relatively
# small to download (1.18M). The file is going to be saved in the same folder
# where this script is launched. The data are downloaded only if the file is
# not present in the current folder

filename = os.path.join(os.getcwd(), 'seeg_data_pac.npz')
if not os.path.isfile(filename):
    print('Downloading the data')
    url = "https://www.dropbox.com/s/dn51xh7nyyttf33/seeg_data_pac.npz?dl=1"
    urllib.request.urlretrieve(url, filename=filename)

arch = np.load(filename)
data = arch['data']       # data of a single sEEG contact
sf = float(arch['sf'])    # sampling frequency
times = arch['times']     # time vector

print(f"DATA: (n_trials, n_times)={data.shape}; SAMPLING FREQUENCY={sf}Hz; "
      f"TIME VECTOR: n_times={len(times)}")

###############################################################################
# Plot the raw data
###############################################################################
# In this section, we simply plot the mean of the raw data across the 160
# trials

# function for adding the sections rest / planning / execution to each figure
def add_motor_condition(y_text, fontsize=14, color='k', ax=None):
    x_times = [-.5, 0.750, 2.250]
    x_conditions = ['REST', 'MOTOR\nPLANNING', 'MOTOR\nEXECUTION']
    if ax is None: ax = plt.gca()  # noqa
    plt.sca(ax)
    plt.axvline(0., lw=2, color=color)
    plt.axvline(1.5, lw=2, color=color)
    for x_t, t_t in zip(x_times, x_conditions):
        plt.text(x_t, y_text, t_t, color=color, fontsize=fontsize, ha='center',
                 va='center', fontweight='bold')

###############################################################################

plt.figure(figsize=(8, 6))
plt.plot(times, data.mean(0))
plt.autoscale(axis='x', tight=True)
plt.title("Mean raw data across trials of a premotor sEEG site", fontsize=18)
plt.xlabel('Times (in seconds)', fontsize=15)
plt.ylabel('V', fontsize=15)
plt.ylim(-600, 800)
add_motor_condition(700.)

plt.show()

###############################################################################
# Compute and plot Inter Trial Coherence
###############################################################################
# The Inter Trial Coherence (ITC) returns a factor that indicates how much
# the phases are consistent across trials. Here, we compute the ITC for
# multiple phase frequencies

itc = ITC(data, sf, f_pha=(2, 20, 1, .2))

###############################################################################

itc.plot(times=times, cmap='plasma', fz_labels=15, fz_title=18)
add_motor_condition(18, color='white')
plt.show()

###############################################################################
# For this sEEG site, we can see that the very low frequency phase (~3Hz) are
# realigned at the beginning of the execution period (~1500ms)

###############################################################################
# Compute and plot the Power Spectrum Density
###############################################################################
# Then, we compute the Power Spectrum Density (PSD) over all of the time-points
# and plot the mean PSD over the 160 trials

psd = PSD(data, sf)

###############################################################################

plt.figure(figsize=(8, 6))
ax = psd.plot(confidence=95, f_min=5, f_max=100, log=True, grid=True)
plt.axvline(8, lw=2, color='red')
plt.axvline(12, lw=2, color='red')
plt.show()

###############################################################################
# From the PSD above, we can see a clear peak around 10hz that could indicate
# an alpha <-> gamma coupling. This peak is essentially comprised between
# [8, 12]Hz. This range of frequencies is then gonig to be used to see if there
# is indeed an alpha <-> gamma coupling (Aru et al. :cite:`aru2015untangling`)

###############################################################################
# Compute and plot the Event-Related PAC
###############################################################################
# To go one step further we can use the Event-Related PAC (ERPAC) in order to
# isolate the gamma range that is coupled with the alpha phase such as when, in
# time, this coupling occurs. Here, we compute the ERPAC using the
# Gaussian-Copula mutual information # (Ince et al. 2017
# :cite:`ince2017statistical`), between the alpha [8, 12]Hz and several gamma
# amplitudes, at each time point.

rp_obj = EventRelatedPac(f_pha=[8, 12], f_amp=(30, 160, 30, 2))
erpac = rp_obj.filterfit(sf, data, method='gc', smooth=100)

###############################################################################

plt.figure(figsize=(8, 6))
rp_obj.pacplot(erpac.squeeze(), times, rp_obj.yvec, xlabel='Time',
               ylabel='Amplitude frequency (Hz)',
               title='Event-Related PAC occurring for alpha phase',
               fz_labels=15, fz_title=18)
add_motor_condition(135, color='white')
plt.show()

###############################################################################
# As you can see from the image above, there is an increase of alpha <-> gamma
# (~90Hz) coupling that is occurring especially during the planning phase (i.e
# between [0, 1500]ms)

###############################################################################
# Align time-frequency map based on alpha phase peak
###############################################################################
# to confirm the previous result showing a potential alpha <-> gamma coupling
# occuring during the planning phase, we can realign time-frequency
# representations (TFR) based on the alpha peak at the beginning of the
# planning phase (i.e at time code 0s)

peak = PeakLockedTF(data, sf, 0., times=times, f_pha=[8, 12],
                    f_amp=(5, 160, 30, 2))

###############################################################################

plt.figure(figsize=(8, 8))
ax_1, ax_2 = peak.plot(zscore=True, baseline=(250, 750), cmap='Spectral_r',
                       vmin=-1, vmax=2)
add_motor_condition(135, color='black', ax=ax_1)
plt.tight_layout()
plt.show()

###############################################################################
# From the TFR bellow we can see the relative to baseline gamma increase such
# as the beta desynchronization during the execution period, which is typical
# for a motor site. Once realign on alpha phase, we can also see that gamma
# burst are regularly spaced, following the alpha rhythm, especially the gamma
# in [40, 120]Hz. this confirm that indeed, there is the presence of
# alpha <-> gamma PAC occurring during the planning phase

###############################################################################
# Compute and compare PAC that is occurring during rest, planning and execution
###############################################################################
# we now know that an alpha [8, 12]Hz <-> gamma (~90Hz) should occur
# specifically during the planning phase. An other way to inspect this result
# is to compute the PAC, across time-points, during the rest, motor planning
# and motor execution periods. Bellow, we first extract several phases and
# amplitudes, then we compute the Gaussian-Copula PAC inside the three motor
# periods

p_obj = Pac(idpac=(6, 0, 0), f_pha=(6, 15, 4, .2), f_amp=(50, 120, 20, 2))
# extract all of the phases and amplitudes
pha_p = p_obj.filter(sf, data, ftype='phase')
amp_p = p_obj.filter(sf, data, ftype='amplitude')
# define time indices where rest, planning and execution are defined
time_rest = slice(0, 1000)
time_prep = slice(1000, 2500)
time_exec = slice(2500, 4000)
# compute PAC inside rest, planning, and execution
pac_rest = p_obj.fit(pha_p[..., time_rest], amp_p[..., time_rest]).mean(-1)
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep]).mean(-1)
pac_exec = p_obj.fit(pha_p[..., time_exec], amp_p[..., time_exec]).mean(-1)

###############################################################################

vmax = np.max([pac_rest.max(), pac_prep.max(), pac_exec.max()])
kw = dict(vmax=vmax, vmin=.04, cmap='viridis')
plt.figure(figsize=(14, 4))
plt.subplot(131)
p_obj.comodulogram(pac_rest, title="PAC Rest [-1, 0]s", **kw)
plt.subplot(132)
p_obj.comodulogram(pac_prep, title="PAC Planning [0, 1.5]s", **kw)
plt.ylabel('')
plt.subplot(133)
p_obj.comodulogram(pac_exec, title="PAC Execution [1.5, 3]s", **kw)
plt.ylabel('')
plt.tight_layout()
plt.show()

###############################################################################
# From the three comodulograms above, you can see that, during the planning
# period there is an alpha [8, 12]Hz <-> gamma [80, 100]Hz that is not
# present during the rest and execution periods

###############################################################################
# Test if the alpha-gamma PAC is significant during motor planning
###############################################################################
# finally, here, we are going to test if the peak PAC that is occurring during
# the planning period is significantly different for a surrogate distribution.
# To this end, and as recommended by Aru et al. 2015, :cite:`aru2015untangling`
# the surrogate distribution is obtained by cutting an amplitude at a random
# time-point and then swap the two blocks of amplitudes (Bahramisharif et al.
# 2013, :cite:`bahramisharif2013propagating`). This procedure is then repeated
# multiple times (e.g 200 or 1000 times) in order to obtained the distribution.
# Finally, the p-value is inferred by computing the proportion exceeded by the
# true coupling. In addition, the correction for multiple comparison is
# obtained using the FDR.

# still using the Gaussian-Copula PAC but this time, we also select the method
# for computing the permutations
p_obj.idpac = (6, 2, 0)
# compute pac and 200 surrogates
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep], n_perm=200)
# get the p-values
mcp = 'maxstat'
pvalues = p_obj.infer_pvalues(p=0.05, mcp=mcp)

###############################################################################

# sphinx_gallery_thumbnail_number = 7
plt.figure(figsize=(8, 6))
title = (r"Significant alpha$\Leftrightarrow$gamma coupling occurring during "
         f"the motor planning phase\n(p<0.05, {mcp}-corrected for multiple "
          "comparisons)")
# plot the non-significant pac in gray
pac_prep_ns = pac_prep.mean(-1).copy()
pac_prep_ns[pvalues < .05] = np.nan
p_obj.comodulogram(pac_prep_ns, cmap='gray', vmin=np.nanmin(pac_prep_ns),
                   vmax=np.nanmax(pac_prep_ns), colorbar=False)
# plot the significant pac in color
pac_prep_s = pac_prep.mean(-1).copy()
pac_prep_s[pvalues >= .05] = np.nan
p_obj.comodulogram(pac_prep_s, cmap='Spectral_r', vmin=np.nanmin(pac_prep_s),
                   vmax=np.nanmax(pac_prep_s), title=title)
plt.gca().invert_yaxis()
plt.show()
<commit_msg>decrease ending phase + increase starting amp<commit_after>"""
==============================================
Phase-Amplitude Coupling tutorial on sEEG data
==============================================

In this example, we illustrate how to conduct a PAC analysis on real data. The
data used here are taken from Combrisson et al. 2017
:cite:`combrisson2017intentions`. The task is a center-out motor task where the
subject have to reach a target on a screen with the mouse. A single trial
consists in three periods :

    * From [-1000, 0]ms it is the baseline period (REST)
    * From [0, 1500]ms the subject have to prepare the movement (MOTOR
      PLANNING period)
    * From [1500, 4000]ms the subject perform the movement to reach the target
      on the screen (MOTOR EXECUTION period)

The recorded electrophysiological data comes from an epileptic subject with
electrodes deep inside the brain (intracranial EEG or
stereoelectroencephalography). Here, the provided data contains only a single
recording contact for one subject. This contact is located in the premotor
cortex.

When working with phase-amplitude coupling, there are basically three questions
you should try to answer :

    1. What is the range of phase frequencies supporting this coupling?
    2. Where in time this coupling occurs?
    3. What is the range of amplitude frequencies supporting this coupling?

In this tutorial we propose to answer to those three questions using following
structure :

    1. Compute the inter-trial coherence (ITC) and see if the phases are
       realigned at a given time point (:class:`tensorpac.utils.ITC`)
    2. Compute the Power Spectrum Density (PSD) to try to find the phase
       frequency range (:class:`tensorpac.utils.PSD`)
    3. Compute the Event-Related PAC (ERPAC) to have a first idea of where the
       coupling occurs in time, especially if it start at a given time point
       (:class:`tensorpac.EventRelatedPac`)
    4. Realign time-frequency representations based on the starting time-point
       found with the ERPAC and see if the gamma burst follow the rhythm
       imposed by a phase. This step should also gives and idea of the
       amplitude frequency range (:class:`tensorpac.utils.PeakLockedTF`)
    5. Compute the comodulogram and statistics in order to test if the
       hypothetic coupling can be considered as statistically different from
       PAC that could be obtained by chance (:class:`tensorpac.Pac`)
"""
import os
import urllib

import numpy as np
from scipy.io import loadmat

from tensorpac import Pac, EventRelatedPac
from tensorpac.utils import PeakLockedTF, PSD, ITC

import matplotlib.pyplot as plt

###############################################################################
# Download the data
###############################################################################
# Lets first start by downloading the data. The file should be relatively
# small to download (1.18M). The file is going to be saved in the same folder
# where this script is launched. The data are downloaded only if the file is
# not present in the current folder

filename = os.path.join(os.getcwd(), 'seeg_data_pac.npz')
if not os.path.isfile(filename):
    print('Downloading the data')
    url = "https://www.dropbox.com/s/dn51xh7nyyttf33/seeg_data_pac.npz?dl=1"
    urllib.request.urlretrieve(url, filename=filename)

arch = np.load(filename)
data = arch['data']       # data of a single sEEG contact
sf = float(arch['sf'])    # sampling frequency
times = arch['times']     # time vector

print(f"DATA: (n_trials, n_times)={data.shape}; SAMPLING FREQUENCY={sf}Hz; "
      f"TIME VECTOR: n_times={len(times)}")

###############################################################################
# Plot the raw data
###############################################################################
# In this section, we simply plot the mean of the raw data across the 160
# trials

# function for adding the sections rest / planning / execution to each figure
def add_motor_condition(y_text, fontsize=14, color='k', ax=None):
    x_times = [-.5, 0.750, 2.250]
    x_conditions = ['REST', 'MOTOR\nPLANNING', 'MOTOR\nEXECUTION']
    if ax is None: ax = plt.gca()  # noqa
    plt.sca(ax)
    plt.axvline(0., lw=2, color=color)
    plt.axvline(1.5, lw=2, color=color)
    for x_t, t_t in zip(x_times, x_conditions):
        plt.text(x_t, y_text, t_t, color=color, fontsize=fontsize, ha='center',
                 va='center', fontweight='bold')

###############################################################################

plt.figure(figsize=(8, 6))
plt.plot(times, data.mean(0))
plt.autoscale(axis='x', tight=True)
plt.title("Mean raw data across trials of a premotor sEEG site", fontsize=18)
plt.xlabel('Times (in seconds)', fontsize=15)
plt.ylabel('V', fontsize=15)
plt.ylim(-600, 800)
add_motor_condition(700.)

plt.show()

###############################################################################
# Compute and plot Inter Trial Coherence
###############################################################################
# The Inter Trial Coherence (ITC) returns a factor that indicates how much
# the phases are consistent across trials. Here, we compute the ITC for
# multiple phase frequencies

itc = ITC(data, sf, f_pha=(2, 20, 1, .2))

###############################################################################

itc.plot(times=times, cmap='plasma', fz_labels=15, fz_title=18)
add_motor_condition(18, color='white')
plt.show()

###############################################################################
# For this sEEG site, we can see that the very low frequency phase (~3Hz) are
# realigned at the beginning of the execution period (~1500ms)

###############################################################################
# Compute and plot the Power Spectrum Density
###############################################################################
# Then, we compute the Power Spectrum Density (PSD) over all of the time-points
# and plot the mean PSD over the 160 trials

psd = PSD(data, sf)

###############################################################################

plt.figure(figsize=(8, 6))
ax = psd.plot(confidence=95, f_min=5, f_max=100, log=True, grid=True)
plt.axvline(8, lw=2, color='red')
plt.axvline(12, lw=2, color='red')
plt.show()

###############################################################################
# From the PSD above, we can see a clear peak around 10hz that could indicate
# an alpha <-> gamma coupling. This peak is essentially comprised between
# [8, 12]Hz. This range of frequencies is then gonig to be used to see if there
# is indeed an alpha <-> gamma coupling (Aru et al. :cite:`aru2015untangling`)

###############################################################################
# Compute and plot the Event-Related PAC
###############################################################################
# To go one step further we can use the Event-Related PAC (ERPAC) in order to
# isolate the gamma range that is coupled with the alpha phase such as when, in
# time, this coupling occurs. Here, we compute the ERPAC using the
# Gaussian-Copula mutual information # (Ince et al. 2017
# :cite:`ince2017statistical`), between the alpha [8, 12]Hz and several gamma
# amplitudes, at each time point.

rp_obj = EventRelatedPac(f_pha=[8, 12], f_amp=(30, 160, 30, 2))
erpac = rp_obj.filterfit(sf, data, method='gc', smooth=100)

###############################################################################

plt.figure(figsize=(8, 6))
rp_obj.pacplot(erpac.squeeze(), times, rp_obj.yvec, xlabel='Time',
               ylabel='Amplitude frequency (Hz)',
               title='Event-Related PAC occurring for alpha phase',
               fz_labels=15, fz_title=18)
add_motor_condition(135, color='white')
plt.show()

###############################################################################
# As you can see from the image above, there is an increase of alpha <-> gamma
# (~90Hz) coupling that is occurring especially during the planning phase (i.e
# between [0, 1500]ms)

###############################################################################
# Align time-frequency map based on alpha phase peak
###############################################################################
# to confirm the previous result showing a potential alpha <-> gamma coupling
# occuring during the planning phase, we can realign time-frequency
# representations (TFR) based on the alpha peak at the beginning of the
# planning phase (i.e at time code 0s)

peak = PeakLockedTF(data, sf, 0., times=times, f_pha=[8, 12],
                    f_amp=(5, 160, 30, 2))

###############################################################################

plt.figure(figsize=(8, 8))
ax_1, ax_2 = peak.plot(zscore=True, baseline=(250, 750), cmap='Spectral_r',
                       vmin=-1, vmax=2)
add_motor_condition(135, color='black', ax=ax_1)
plt.tight_layout()
plt.show()

###############################################################################
# From the TFR bellow we can see the relative to baseline gamma increase such
# as the beta desynchronization during the execution period, which is typical
# for a motor site. Once realign on alpha phase, we can also see that gamma
# burst are regularly spaced, following the alpha rhythm, especially the gamma
# in [40, 120]Hz. this confirm that indeed, there is the presence of
# alpha <-> gamma PAC occurring during the planning phase

###############################################################################
# Compute and compare PAC that is occurring during rest, planning and execution
###############################################################################
# we now know that an alpha [8, 12]Hz <-> gamma (~90Hz) should occur
# specifically during the planning phase. An other way to inspect this result
# is to compute the PAC, across time-points, during the rest, motor planning
# and motor execution periods. Bellow, we first extract several phases and
# amplitudes, then we compute the Gaussian-Copula PAC inside the three motor
# periods

p_obj = Pac(idpac=(6, 0, 0), f_pha=(6, 13, 4, .2), f_amp=(60, 120, 20, 2))
# extract all of the phases and amplitudes
pha_p = p_obj.filter(sf, data, ftype='phase')
amp_p = p_obj.filter(sf, data, ftype='amplitude')
# define time indices where rest, planning and execution are defined
time_rest = slice(0, 1000)
time_prep = slice(1000, 2500)
time_exec = slice(2500, 4000)
# compute PAC inside rest, planning, and execution
pac_rest = p_obj.fit(pha_p[..., time_rest], amp_p[..., time_rest]).mean(-1)
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep]).mean(-1)
pac_exec = p_obj.fit(pha_p[..., time_exec], amp_p[..., time_exec]).mean(-1)

###############################################################################

vmax = np.max([pac_rest.max(), pac_prep.max(), pac_exec.max()])
kw = dict(vmax=vmax, vmin=.04, cmap='viridis')
plt.figure(figsize=(14, 4))
plt.subplot(131)
p_obj.comodulogram(pac_rest, title="PAC Rest [-1, 0]s", **kw)
plt.subplot(132)
p_obj.comodulogram(pac_prep, title="PAC Planning [0, 1.5]s", **kw)
plt.ylabel('')
plt.subplot(133)
p_obj.comodulogram(pac_exec, title="PAC Execution [1.5, 3]s", **kw)
plt.ylabel('')
plt.tight_layout()
plt.show()

###############################################################################
# From the three comodulograms above, you can see that, during the planning
# period there is an alpha [8, 12]Hz <-> gamma [80, 100]Hz that is not
# present during the rest and execution periods

###############################################################################
# Test if the alpha-gamma PAC is significant during motor planning
###############################################################################
# finally, here, we are going to test if the peak PAC that is occurring during
# the planning period is significantly different for a surrogate distribution.
# To this end, and as recommended by Aru et al. 2015, :cite:`aru2015untangling`
# the surrogate distribution is obtained by cutting an amplitude at a random
# time-point and then swap the two blocks of amplitudes (Bahramisharif et al.
# 2013, :cite:`bahramisharif2013propagating`). This procedure is then repeated
# multiple times (e.g 200 or 1000 times) in order to obtained the distribution.
# Finally, the p-value is inferred by computing the proportion exceeded by the
# true coupling. In addition, the correction for multiple comparison is
# obtained using the FDR.

# still using the Gaussian-Copula PAC but this time, we also select the method
# for computing the permutations
p_obj.idpac = (6, 2, 0)
# compute pac and 200 surrogates
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep], n_perm=200)
# get the p-values
mcp = 'maxstat'
pvalues = p_obj.infer_pvalues(p=0.05, mcp=mcp)

###############################################################################

# sphinx_gallery_thumbnail_number = 7
plt.figure(figsize=(8, 6))
title = (r"Significant alpha$\Leftrightarrow$gamma coupling occurring during "
         f"the motor planning phase\n(p<0.05, {mcp}-corrected for multiple "
          "comparisons)")
# plot the non-significant pac in gray
pac_prep_ns = pac_prep.mean(-1).copy()
pac_prep_ns[pvalues < .05] = np.nan
p_obj.comodulogram(pac_prep_ns, cmap='gray', vmin=np.nanmin(pac_prep_ns),
                   vmax=np.nanmax(pac_prep_ns), colorbar=False)
# plot the significant pac in color
pac_prep_s = pac_prep.mean(-1).copy()
pac_prep_s[pvalues >= .05] = np.nan
p_obj.comodulogram(pac_prep_s, cmap='Spectral_r', vmin=np.nanmin(pac_prep_s),
                   vmax=np.nanmax(pac_prep_s), title=title)
plt.gca().invert_yaxis()
plt.show()
<|endoftext|>


<commit_before>"""
==============================================
Phase-Amplitude Coupling tutorial on sEEG data
==============================================

In this example, we illustrate how to conduct a PAC analysis on real data. The
data used here are taken from Combrisson et al. 2017
:cite:`combrisson2017intentions`. The task is a center-out motor task where the
subject have to reach a target on a screen with the mouse. A single trial
consists in three periods :

    * From [-1000, 0]ms it is the baseline period (REST)
    * From [0, 1500]ms the subject have to prepare the movement (MOTOR
      PLANNING period)
    * From [1500, 4000]ms the subject perform the movement to reach the target
      on the screen (MOTOR EXECUTION period)

The recorded electrophysiological data comes from an epileptic subject with
electrodes deep inside the brain (intracranial EEG or
stereoelectroencephalography). Here, the provided data contains only a single
recording contact for one subject. This contact is located in the premotor
cortex.

When working with phase-amplitude coupling, there are basically three questions
you should try to answer :

    1. What is the range of phase frequencies supporting this coupling?
    2. Where in time this coupling occurs?
    3. What is the range of amplitude frequencies supporting this coupling?

In this tutorial we propose to answer to those three questions using following
structure :

    1. Compute the inter-trial coherence (ITC) and see if the phases are
       realigned at a given time point (:class:`tensorpac.utils.ITC`)
    2. Compute the Power Spectrum Density (PSD) to try to find the phase
       frequency range (:class:`tensorpac.utils.PSD`)
    3. Compute the Event-Related PAC (ERPAC) to have a first idea of where the
       coupling occurs in time, especially if it start at a given time point
       (:class:`tensorpac.EventRelatedPac`)
    4. Realign time-frequency representations based on the starting time-point
       found with the ERPAC and see if the gamma burst follow the rhythm
       imposed by a phase. This step should also gives and idea of the
       amplitude frequency range (:class:`tensorpac.utils.PeakLockedTF`)
    5. Compute the comodulogram and statistics in order to test if the
       hypothetic coupling can be considered as statistically different from
       PAC that could be obtained by chance (:class:`tensorpac.Pac`)
"""
import os
import urllib.request
import urllib

import numpy as np
from scipy.io import loadmat

from tensorpac import Pac, EventRelatedPac
from tensorpac.utils import PeakLockedTF, PSD, ITC

import matplotlib.pyplot as plt

###############################################################################
# Download the data
###############################################################################
# Lets first start by downloading the data. The file should be relatively
# small to download (1.18M). The file is going to be saved in the same folder
# where this script is launched. The data are downloaded only if the file is
# not present in the current folder

filename = os.path.join(os.getcwd(), 'seeg_data_pac.npz')
if not os.path.isfile(filename):
    print('Downloading the data')
    url = "https://www.dropbox.com/s/dn51xh7nyyttf33/seeg_data_pac.npz?dl=1"
    urllib.request.urlretrieve(url, filename=filename)

arch = np.load(filename)
data = arch['data']       # data of a single sEEG contact
sf = float(arch['sf'])    # sampling frequency
times = arch['times']     # time vector

print(f"DATA: (n_trials, n_times)={data.shape}; SAMPLING FREQUENCY={sf}Hz; "
      f"TIME VECTOR: n_times={len(times)}")

###############################################################################
# Plot the raw data
###############################################################################
# In this section, we simply plot the mean of the raw data across the 160
# trials

# function for adding the sections rest / planning / execution to each figure
def add_motor_condition(y_text, fontsize=14, color='k', ax=None):
    x_times = [-.5, 0.750, 2.250]
    x_conditions = ['REST', 'MOTOR\nPLANNING', 'MOTOR\nEXECUTION']
    if ax is None: ax = plt.gca()  # noqa
    plt.sca(ax)
    plt.axvline(0., lw=2, color=color)
    plt.axvline(1.5, lw=2, color=color)
    for x_t, t_t in zip(x_times, x_conditions):
        plt.text(x_t, y_text, t_t, color=color, fontsize=fontsize, ha='center',
                 va='center', fontweight='bold')

###############################################################################

plt.figure(figsize=(8, 6))
plt.plot(times, data.mean(0))
plt.autoscale(axis='x', tight=True)
plt.title("Mean raw data across trials of a premotor sEEG site", fontsize=18)
plt.xlabel('Times (in seconds)', fontsize=15)
plt.ylabel('V', fontsize=15)
plt.ylim(-600, 800)
add_motor_condition(700.)

plt.show()

###############################################################################
# Compute and plot Inter Trial Coherence
###############################################################################
# The Inter Trial Coherence (ITC) returns a factor that indicates how much
# the phases are consistent across trials. Here, we compute the ITC for
# multiple phase frequencies

itc = ITC(data, sf, f_pha=(2, 20, 1, .2))

###############################################################################

itc.plot(times=times, cmap='plasma', fz_labels=15, fz_title=18)
add_motor_condition(18, color='white')
plt.show()

###############################################################################
# For this sEEG site, we can see that the very low frequency phase (~3Hz) are
# realigned at the beginning of the execution period (~1500ms)

###############################################################################
# Compute and plot the Power Spectrum Density
###############################################################################
# Then, we compute the Power Spectrum Density (PSD) over all of the time-points
# and plot the mean PSD over the 160 trials

psd = PSD(data, sf)

###############################################################################

plt.figure(figsize=(8, 6))
ax = psd.plot(confidence=95, f_min=5, f_max=100, log=True, grid=True)
plt.axvline(8, lw=2, color='red')
plt.axvline(12, lw=2, color='red')
plt.show()

###############################################################################
# From the PSD above, we can see a clear peak around 10hz that could indicate
# an alpha <-> gamma coupling. This peak is essentially comprised between
# [8, 12]Hz. This range of frequencies is then gonig to be used to see if there
# is indeed an alpha <-> gamma coupling (Aru et al. :cite:`aru2015untangling`)

###############################################################################
# Compute and plot the Event-Related PAC
###############################################################################
# To go one step further we can use the Event-Related PAC (ERPAC) in order to
# isolate the gamma range that is coupled with the alpha phase such as when, in
# time, this coupling occurs. Here, we compute the ERPAC using the
# Gaussian-Copula mutual information # (Ince et al. 2017
# :cite:`ince2017statistical`), between the alpha [8, 12]Hz and several gamma
# amplitudes, at each time point.

rp_obj = EventRelatedPac(f_pha=[8, 12], f_amp=(30, 160, 30, 2))
erpac = rp_obj.filterfit(sf, data, method='gc', smooth=100)

###############################################################################

plt.figure(figsize=(8, 6))
rp_obj.pacplot(erpac.squeeze(), times, rp_obj.yvec, xlabel='Time',
               ylabel='Amplitude frequency (Hz)',
               title='Event-Related PAC occurring for alpha phase',
               fz_labels=15, fz_title=18)
add_motor_condition(135, color='white')
plt.show()

###############################################################################
# As you can see from the image above, there is an increase of alpha <-> gamma
# (~90Hz) coupling that is occurring especially during the planning phase (i.e
# between [0, 1500]ms)

###############################################################################
# Align time-frequency map based on alpha phase peak
###############################################################################
# to confirm the previous result showing a potential alpha <-> gamma coupling
# occuring during the planning phase, we can realign time-frequency
# representations (TFR) based on the alpha peak at the beginning of the
# planning phase (i.e at time code 0s)

peak = PeakLockedTF(data, sf, 0., times=times, f_pha=[8, 12],
                    f_amp=(5, 160, 30, 2))

###############################################################################

plt.figure(figsize=(8, 8))
ax_1, ax_2 = peak.plot(zscore=True, baseline=(250, 750), cmap='Spectral_r',
                       vmin=-1, vmax=2)
add_motor_condition(135, color='black', ax=ax_1)
plt.tight_layout()
plt.show()

###############################################################################
# From the TFR bellow we can see the relative to baseline gamma increase such
# as the beta desynchronization during the execution period, which is typical
# for a motor site. Once realign on alpha phase, we can also see that gamma
# burst are regularly spaced, following the alpha rhythm, especially the gamma
# in [40, 120]Hz. this confirm that indeed, there is the presence of
# alpha <-> gamma PAC occurring during the planning phase

###############################################################################
# Compute and compare PAC that is occurring during rest, planning and execution
###############################################################################
# we now know that an alpha [8, 12]Hz <-> gamma (~90Hz) should occur
# specifically during the planning phase. An other way to inspect this result
# is to compute the PAC, across time-points, during the rest, motor planning
# and motor execution periods. Bellow, we first extract several phases and
# amplitudes, then we compute the Gaussian-Copula PAC inside the three motor
# periods

p_obj = Pac(idpac=(6, 0, 0), f_pha=(6, 15, 4, .2), f_amp=(50, 120, 20, 2))
# extract all of the phases and amplitudes
pha_p = p_obj.filter(sf, data, ftype='phase')
amp_p = p_obj.filter(sf, data, ftype='amplitude')
# define time indices where rest, planning and execution are defined
time_rest = slice(0, 1000)
time_prep = slice(1000, 2500)
time_exec = slice(2500, 4000)
# compute PAC inside rest, planning, and execution
pac_rest = p_obj.fit(pha_p[..., time_rest], amp_p[..., time_rest]).mean(-1)
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep]).mean(-1)
pac_exec = p_obj.fit(pha_p[..., time_exec], amp_p[..., time_exec]).mean(-1)

###############################################################################

vmax = np.max([pac_rest.max(), pac_prep.max(), pac_exec.max()])
kw = dict(vmax=vmax, vmin=.04, cmap='viridis')
plt.figure(figsize=(14, 4))
plt.subplot(131)
p_obj.comodulogram(pac_rest, title="PAC Rest [-1, 0]s", **kw)
plt.subplot(132)
p_obj.comodulogram(pac_prep, title="PAC Planning [0, 1.5]s", **kw)
plt.ylabel('')
plt.subplot(133)
p_obj.comodulogram(pac_exec, title="PAC Execution [1.5, 3]s", **kw)
plt.ylabel('')
plt.tight_layout()
plt.show()

###############################################################################
# From the three comodulograms above, you can see that, during the planning
# period there is an alpha [8, 12]Hz <-> gamma [80, 100]Hz that is not
# present during the rest and execution periods

###############################################################################
# Test if the alpha-gamma PAC is significant during motor planning
###############################################################################
# finally, here, we are going to test if the peak PAC that is occurring during
# the planning period is significantly different for a surrogate distribution.
# To this end, and as recommended by Aru et al. 2015, :cite:`aru2015untangling`
# the surrogate distribution is obtained by cutting an amplitude at a random
# time-point and then swap the two blocks of amplitudes (Bahramisharif et al.
# 2013, :cite:`bahramisharif2013propagating`). This procedure is then repeated
# multiple times (e.g 200 or 1000 times) in order to obtained the distribution.
# Finally, the p-value is inferred by computing the proportion exceeded by the
# true coupling. In addition, the correction for multiple comparison is
# obtained using the FDR.

# still using the Gaussian-Copula PAC but this time, we also select the method
# for computing the permutations
p_obj.idpac = (6, 2, 0)
# compute pac and 200 surrogates
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep], n_perm=200)
# get the p-values
pvalues = p_obj.infer_pvalues(p=0.05, mcp='fdr')

###############################################################################

# sphinx_gallery_thumbnail_number = 7
plt.figure(figsize=(8, 6))
title = (r"Significant alpha$\Leftrightarrow$gamma coupling occurring during "
         "the motor planning phase\n(p<0.05, FDR-corrected for multiple "
         "comparisons)")
# plot the non-significant pac in gray
pac_prep_ns = pac_prep.mean(-1).copy()
pac_prep_ns[pvalues < .05] = np.nan
p_obj.comodulogram(pac_prep_ns, cmap='gray', vmin=np.nanmin(pac_prep_ns),
                   vmax=np.nanmax(pac_prep_ns), colorbar=False)
# plot the significant pac in color
pac_prep_s = pac_prep.mean(-1).copy()
pac_prep_s[pvalues >= .05] = np.nan
p_obj.comodulogram(pac_prep_s, cmap='Spectral_r', vmin=np.nanmin(pac_prep_s),
                   vmax=np.nanmax(pac_prep_s), title=title)
plt.gca().invert_yaxis()
plt.show()
<commit_msg>reset to maxstat<commit_after>"""
==============================================
Phase-Amplitude Coupling tutorial on sEEG data
==============================================

In this example, we illustrate how to conduct a PAC analysis on real data. The
data used here are taken from Combrisson et al. 2017
:cite:`combrisson2017intentions`. The task is a center-out motor task where the
subject have to reach a target on a screen with the mouse. A single trial
consists in three periods :

    * From [-1000, 0]ms it is the baseline period (REST)
    * From [0, 1500]ms the subject have to prepare the movement (MOTOR
      PLANNING period)
    * From [1500, 4000]ms the subject perform the movement to reach the target
      on the screen (MOTOR EXECUTION period)

The recorded electrophysiological data comes from an epileptic subject with
electrodes deep inside the brain (intracranial EEG or
stereoelectroencephalography). Here, the provided data contains only a single
recording contact for one subject. This contact is located in the premotor
cortex.

When working with phase-amplitude coupling, there are basically three questions
you should try to answer :

    1. What is the range of phase frequencies supporting this coupling?
    2. Where in time this coupling occurs?
    3. What is the range of amplitude frequencies supporting this coupling?

In this tutorial we propose to answer to those three questions using following
structure :

    1. Compute the inter-trial coherence (ITC) and see if the phases are
       realigned at a given time point (:class:`tensorpac.utils.ITC`)
    2. Compute the Power Spectrum Density (PSD) to try to find the phase
       frequency range (:class:`tensorpac.utils.PSD`)
    3. Compute the Event-Related PAC (ERPAC) to have a first idea of where the
       coupling occurs in time, especially if it start at a given time point
       (:class:`tensorpac.EventRelatedPac`)
    4. Realign time-frequency representations based on the starting time-point
       found with the ERPAC and see if the gamma burst follow the rhythm
       imposed by a phase. This step should also gives and idea of the
       amplitude frequency range (:class:`tensorpac.utils.PeakLockedTF`)
    5. Compute the comodulogram and statistics in order to test if the
       hypothetic coupling can be considered as statistically different from
       PAC that could be obtained by chance (:class:`tensorpac.Pac`)
"""
import os
import urllib

import numpy as np
from scipy.io import loadmat

from tensorpac import Pac, EventRelatedPac
from tensorpac.utils import PeakLockedTF, PSD, ITC

import matplotlib.pyplot as plt

###############################################################################
# Download the data
###############################################################################
# Lets first start by downloading the data. The file should be relatively
# small to download (1.18M). The file is going to be saved in the same folder
# where this script is launched. The data are downloaded only if the file is
# not present in the current folder

filename = os.path.join(os.getcwd(), 'seeg_data_pac.npz')
if not os.path.isfile(filename):
    print('Downloading the data')
    url = "https://www.dropbox.com/s/dn51xh7nyyttf33/seeg_data_pac.npz?dl=1"
    urllib.request.urlretrieve(url, filename=filename)

arch = np.load(filename)
data = arch['data']       # data of a single sEEG contact
sf = float(arch['sf'])    # sampling frequency
times = arch['times']     # time vector

print(f"DATA: (n_trials, n_times)={data.shape}; SAMPLING FREQUENCY={sf}Hz; "
      f"TIME VECTOR: n_times={len(times)}")

###############################################################################
# Plot the raw data
###############################################################################
# In this section, we simply plot the mean of the raw data across the 160
# trials

# function for adding the sections rest / planning / execution to each figure
def add_motor_condition(y_text, fontsize=14, color='k', ax=None):
    x_times = [-.5, 0.750, 2.250]
    x_conditions = ['REST', 'MOTOR\nPLANNING', 'MOTOR\nEXECUTION']
    if ax is None: ax = plt.gca()  # noqa
    plt.sca(ax)
    plt.axvline(0., lw=2, color=color)
    plt.axvline(1.5, lw=2, color=color)
    for x_t, t_t in zip(x_times, x_conditions):
        plt.text(x_t, y_text, t_t, color=color, fontsize=fontsize, ha='center',
                 va='center', fontweight='bold')

###############################################################################

plt.figure(figsize=(8, 6))
plt.plot(times, data.mean(0))
plt.autoscale(axis='x', tight=True)
plt.title("Mean raw data across trials of a premotor sEEG site", fontsize=18)
plt.xlabel('Times (in seconds)', fontsize=15)
plt.ylabel('V', fontsize=15)
plt.ylim(-600, 800)
add_motor_condition(700.)

plt.show()

###############################################################################
# Compute and plot Inter Trial Coherence
###############################################################################
# The Inter Trial Coherence (ITC) returns a factor that indicates how much
# the phases are consistent across trials. Here, we compute the ITC for
# multiple phase frequencies

itc = ITC(data, sf, f_pha=(2, 20, 1, .2))

###############################################################################

itc.plot(times=times, cmap='plasma', fz_labels=15, fz_title=18)
add_motor_condition(18, color='white')
plt.show()

###############################################################################
# For this sEEG site, we can see that the very low frequency phase (~3Hz) are
# realigned at the beginning of the execution period (~1500ms)

###############################################################################
# Compute and plot the Power Spectrum Density
###############################################################################
# Then, we compute the Power Spectrum Density (PSD) over all of the time-points
# and plot the mean PSD over the 160 trials

psd = PSD(data, sf)

###############################################################################

plt.figure(figsize=(8, 6))
ax = psd.plot(confidence=95, f_min=5, f_max=100, log=True, grid=True)
plt.axvline(8, lw=2, color='red')
plt.axvline(12, lw=2, color='red')
plt.show()

###############################################################################
# From the PSD above, we can see a clear peak around 10hz that could indicate
# an alpha <-> gamma coupling. This peak is essentially comprised between
# [8, 12]Hz. This range of frequencies is then gonig to be used to see if there
# is indeed an alpha <-> gamma coupling (Aru et al. :cite:`aru2015untangling`)

###############################################################################
# Compute and plot the Event-Related PAC
###############################################################################
# To go one step further we can use the Event-Related PAC (ERPAC) in order to
# isolate the gamma range that is coupled with the alpha phase such as when, in
# time, this coupling occurs. Here, we compute the ERPAC using the
# Gaussian-Copula mutual information # (Ince et al. 2017
# :cite:`ince2017statistical`), between the alpha [8, 12]Hz and several gamma
# amplitudes, at each time point.

rp_obj = EventRelatedPac(f_pha=[8, 12], f_amp=(30, 160, 30, 2))
erpac = rp_obj.filterfit(sf, data, method='gc', smooth=100)

###############################################################################

plt.figure(figsize=(8, 6))
rp_obj.pacplot(erpac.squeeze(), times, rp_obj.yvec, xlabel='Time',
               ylabel='Amplitude frequency (Hz)',
               title='Event-Related PAC occurring for alpha phase',
               fz_labels=15, fz_title=18)
add_motor_condition(135, color='white')
plt.show()

###############################################################################
# As you can see from the image above, there is an increase of alpha <-> gamma
# (~90Hz) coupling that is occurring especially during the planning phase (i.e
# between [0, 1500]ms)

###############################################################################
# Align time-frequency map based on alpha phase peak
###############################################################################
# to confirm the previous result showing a potential alpha <-> gamma coupling
# occuring during the planning phase, we can realign time-frequency
# representations (TFR) based on the alpha peak at the beginning of the
# planning phase (i.e at time code 0s)

peak = PeakLockedTF(data, sf, 0., times=times, f_pha=[8, 12],
                    f_amp=(5, 160, 30, 2))

###############################################################################

plt.figure(figsize=(8, 8))
ax_1, ax_2 = peak.plot(zscore=True, baseline=(250, 750), cmap='Spectral_r',
                       vmin=-1, vmax=2)
add_motor_condition(135, color='black', ax=ax_1)
plt.tight_layout()
plt.show()

###############################################################################
# From the TFR bellow we can see the relative to baseline gamma increase such
# as the beta desynchronization during the execution period, which is typical
# for a motor site. Once realign on alpha phase, we can also see that gamma
# burst are regularly spaced, following the alpha rhythm, especially the gamma
# in [40, 120]Hz. this confirm that indeed, there is the presence of
# alpha <-> gamma PAC occurring during the planning phase

###############################################################################
# Compute and compare PAC that is occurring during rest, planning and execution
###############################################################################
# we now know that an alpha [8, 12]Hz <-> gamma (~90Hz) should occur
# specifically during the planning phase. An other way to inspect this result
# is to compute the PAC, across time-points, during the rest, motor planning
# and motor execution periods. Bellow, we first extract several phases and
# amplitudes, then we compute the Gaussian-Copula PAC inside the three motor
# periods

p_obj = Pac(idpac=(6, 0, 0), f_pha=(6, 15, 4, .2), f_amp=(50, 120, 20, 2))
# extract all of the phases and amplitudes
pha_p = p_obj.filter(sf, data, ftype='phase')
amp_p = p_obj.filter(sf, data, ftype='amplitude')
# define time indices where rest, planning and execution are defined
time_rest = slice(0, 1000)
time_prep = slice(1000, 2500)
time_exec = slice(2500, 4000)
# compute PAC inside rest, planning, and execution
pac_rest = p_obj.fit(pha_p[..., time_rest], amp_p[..., time_rest]).mean(-1)
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep]).mean(-1)
pac_exec = p_obj.fit(pha_p[..., time_exec], amp_p[..., time_exec]).mean(-1)

###############################################################################

vmax = np.max([pac_rest.max(), pac_prep.max(), pac_exec.max()])
kw = dict(vmax=vmax, vmin=.04, cmap='viridis')
plt.figure(figsize=(14, 4))
plt.subplot(131)
p_obj.comodulogram(pac_rest, title="PAC Rest [-1, 0]s", **kw)
plt.subplot(132)
p_obj.comodulogram(pac_prep, title="PAC Planning [0, 1.5]s", **kw)
plt.ylabel('')
plt.subplot(133)
p_obj.comodulogram(pac_exec, title="PAC Execution [1.5, 3]s", **kw)
plt.ylabel('')
plt.tight_layout()
plt.show()

###############################################################################
# From the three comodulograms above, you can see that, during the planning
# period there is an alpha [8, 12]Hz <-> gamma [80, 100]Hz that is not
# present during the rest and execution periods

###############################################################################
# Test if the alpha-gamma PAC is significant during motor planning
###############################################################################
# finally, here, we are going to test if the peak PAC that is occurring during
# the planning period is significantly different for a surrogate distribution.
# To this end, and as recommended by Aru et al. 2015, :cite:`aru2015untangling`
# the surrogate distribution is obtained by cutting an amplitude at a random
# time-point and then swap the two blocks of amplitudes (Bahramisharif et al.
# 2013, :cite:`bahramisharif2013propagating`). This procedure is then repeated
# multiple times (e.g 200 or 1000 times) in order to obtained the distribution.
# Finally, the p-value is inferred by computing the proportion exceeded by the
# true coupling. In addition, the correction for multiple comparison is
# obtained using the FDR.

# still using the Gaussian-Copula PAC but this time, we also select the method
# for computing the permutations
p_obj.idpac = (6, 2, 0)
# compute pac and 200 surrogates
pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep], n_perm=200)
# get the p-values
mcp = 'maxstat'
pvalues = p_obj.infer_pvalues(p=0.05, mcp=mcp)

###############################################################################

# sphinx_gallery_thumbnail_number = 7
plt.figure(figsize=(8, 6))
title = (r"Significant alpha$\Leftrightarrow$gamma coupling occurring during "
         f"the motor planning phase\n(p<0.05, {mcp}-corrected for multiple "
          "comparisons)")
# plot the non-significant pac in gray
pac_prep_ns = pac_prep.mean(-1).copy()
pac_prep_ns[pvalues < .05] = np.nan
p_obj.comodulogram(pac_prep_ns, cmap='gray', vmin=np.nanmin(pac_prep_ns),
                   vmax=np.nanmax(pac_prep_ns), colorbar=False)
# plot the significant pac in color
pac_prep_s = pac_prep.mean(-1).copy()
pac_prep_s[pvalues >= .05] = np.nan
p_obj.comodulogram(pac_prep_s, cmap='Spectral_r', vmin=np.nanmin(pac_prep_s),
                   vmax=np.nanmax(pac_prep_s), title=title)
plt.gca().invert_yaxis()
plt.show()
<|endoftext|>


<commit_before>import maya.OpenMayaUI as omui
from PySide import QtCore, QtGui

# generates bindings for C++ libraries using CPython source code.
from shiboken import wrapInstance

# function to return the converted c++ QWidget pointer of Maya
def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)

##
class mcr_ui(QtGui.QWidget):
    ##
    def __init__(self, parent = maya_main_window()):
        super(mcr_ui, self).__init__(parent)
        self.setWindowTitle("Mocap Retargeting")
        self.setWindowFlags(QtCore.Qt.Window)
        self.resize(QtCore.QSize(500, 5))

    ##
    def create_widgets(self):
        self.get_path_line_edit = QtGui.QLineEdit("Enter mocap file path ...")
        self.get_path_push_button = QtGui.QPushButton("Get File")

        self.mocap_group_box = QtGui.QGroupBox("Mocap Objects")
        self.mocap_get_scene_push_button = QtGui.QPushButton("Load Selected")
        self.mocap_get_path_push_button = QtGui.QPushButton("Load Path")
        self.mocap_clear_push_button = QtGui.QPushButton("Clear")
        self.mocap_tree = QtGui.QTreeView()

        self.target_group_box = QtGui.QGroupBox("Target Objects")
        self.target_get_scene_push_button = QtGui.QPushButton("Load Selected")
        self.target_clear_push_button = QtGui.QPushButton("Clear")
        self.target_tree = QtGui.QTreeView()

    ##
    def create_layouts(self):
        main_layout = QtGui.QVBoxLayout()

        path_layout = QtGui.QHBoxLayout()
        path_layout.addWidget(self.get_path_line_edit)
        path_layout.addWidget(self.get_path_push_button)

        group_layout = QtGui.QHBoxLayout()
        group_layout.addWidget(self.mocap_group_box)
        group_layout.addWidget(self.target_group_box)

        main_layout.addLayout(path_layout)
        main_layout.addLayout(group_layout)

        main_layout.addStretch()

        self.setLayout(main_layout)

    ##
    def draw(self):
        self.create_widgets()
        self.create_layouts()

###
if __name__ == "__main__":

    try:
        test_ui.close()
    except:
        pass

    test_ui = mcr_ui()
    test_ui.draw()
    test_ui.show()

 <commit_msg>ui in progress..<commit_after>import maya.OpenMayaUI as omui
from PySide import QtCore, QtGui

# generates bindings for C++ libraries using CPython source code.
from shiboken import wrapInstance

# function to return the converted c++ QWidget pointer of Maya
def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)

##
class mcr_ui(QtGui.QWidget):
    ##
    def __init__(self, parent = maya_main_window()):
        super(mcr_ui, self).__init__(parent)
        self.setWindowTitle("Mocap Retargeting")
        self.setWindowFlags(QtCore.Qt.Window)
        self.resize(QtCore.QSize(500, 5))

    ##
    def create_widgets(self):
        self.get_path_line_edit = QtGui.QLineEdit("Enter mocap file path ...")
        self.get_path_push_button = QtGui.QPushButton("Get File")

        self.mocap_group_box = QtGui.QGroupBox("Mocap Objects")
        self.mocap_get_scene_push_button = QtGui.QPushButton("Load Selected")
        self.mocap_get_path_push_button = QtGui.QPushButton("Load Path")
        self.mocap_clear_push_button = QtGui.QPushButton("Clear")
        self.mocap_tree = QtGui.QTreeView()
        self.mocap_tree.setMinimumHeight(400)

        self.match_push_button = QtGui.QPushButton("<<")
        self.match_push_button.setMinimumHeight(150)
        self.match_retarget_push_button = QtGui.QPushButton("RETARGET")
        self.match_retarget_push_button.setMinimumHeight(50)

        self.target_group_box = QtGui.QGroupBox("Target Objects")
        self.target_get_scene_push_button = QtGui.QPushButton("Load Selected")
        self.target_clear_push_button = QtGui.QPushButton("Clear")
        self.target_tree = QtGui.QTreeView()

    ##
    def create_layouts(self):
        main_layout = QtGui.QVBoxLayout()

        path_layout = QtGui.QHBoxLayout()
        path_layout.addWidget(self.get_path_line_edit)
        path_layout.addWidget(self.get_path_push_button)

        match_layout = QtGui.QVBoxLayout()
        match_layout.addWidget(self.match_push_button)

        group_layout = QtGui.QHBoxLayout()
        group_layout.addWidget(self.mocap_group_box)
        group_layout.addLayout(match_layout)
        group_layout.addWidget(self.target_group_box)
        group_layout.setStretch(0, 2)
        group_layout.setStretch(2, 2)

        mocap_layout = QtGui.QVBoxLayout()
        mocap_layout.addWidget(self.mocap_get_path_push_button)
        mocap_layout.addWidget(self.mocap_get_scene_push_button)
        mocap_layout.addWidget(self.mocap_tree)
        mocap_layout.addWidget(self.mocap_clear_push_button)

        self.mocap_group_box.setLayout(mocap_layout)

        target_layout = QtGui.QVBoxLayout()
        target_layout.addWidget(self.target_get_scene_push_button)
        target_layout.addWidget(self.target_tree)
        target_layout.addWidget(self.target_clear_push_button)

        self.target_group_box.setLayout(target_layout)

        main_layout.addLayout(path_layout)
        main_layout.addLayout(group_layout)
        main_layout.addWidget(self.match_retarget_push_button)

        main_layout.addStretch()

        self.setLayout(main_layout)

    ##
    def draw(self):
        self.create_widgets()
        self.create_layouts()

###
if __name__ == "__main__":

    try:
        test_ui.close()
    except:
        pass

    test_ui = mcr_ui()
    test_ui.draw()
    test_ui.show()

<|endoftext|>