text:
"""
module documentation
goes here
"""
from __future__ import division
import community
import networkx as nx
import matplotlib.pyplot as plt
from telesphorus.helpers import stats
from telesphorus import abstractions
from telesphorus import obscurity

PATH_DELIMITER = '->'

def graph_stats(trace_graph):
    """General graph statistics"""
    print "\nA graph with %d nodes and %d edges created." % \
        (trace_graph.order(), trace_graph.size())
    print "Total POSIX API invokations: %d" % \
        stats.posix_invocations(trace_graph)
#    print "Average path length: %d\n\n" % \
#        stats.average_full_path_len(trace_graph)
#    print "Average libraries per path: %d\n\n" % \
#        stats.average_libs_per_path(trace_graph)
#    G = trace_graph.to_undirected()
#    partition = community.best_partition(G)
#    print "Modularity:", community.modularity(partition, G)
#    print "Partitions: ", len(set(partition.values()))
#    print "\n\n"
#    for com in set(partition.values()):
#        list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == com]
#        print list_nodes
#        print ""

def lib_stats(trace_graph, settings):
    """For each lib print :
        1) The amount of times each POSIX call invoked by the above lib.
        2) Total POSIX invokations the above lib accounted for.
    """
    threashold = settings.LIB_THREASHOLD
    posix_total = stats.posix_invocations(trace_graph)
    for (lib_name, metadata) in stats.libs(trace_graph):
        total = sum(map(lambda x: x[1], metadata.items()))
        for posix_call in sorted(metadata, key=metadata.get, reverse=True):
            #if 100*metadata[posix_call]/total >= threashold:
            if metadata[posix_call] >= threashold:
                print ("Lib: %s invoked POSIX call: %s %.2f %% of "
                       "times (%d).") % (lib_name, posix_call,
                                         100 * metadata[posix_call] / total,
                                         metadata[posix_call])
    print '------'
    # comment
    for (lib_name, metadata) in stats.libs(trace_graph):
        total = sum(map(lambda x: x[1], metadata.items()))
        if 100 * total / posix_total > 0.0:
            print ("Lib: %s accounted for %.2f %% (%d) of "
                   "total POSIX invocations.") % (lib_name,
                                                 100 * total / posix_total,
                                                 total)

def lib_call_stats(trace_graph, settings):
    """For each library call, print all POXIX calls invoked"""
    threashold = settings.LIB_CALL_THREASHOLD
    #why I had the generator on sorted??
    for (lib_call, metadata) in stats.lib_calls(trace_graph):
        total = sum(map(lambda x: x[1], metadata.items()))
        for posix_call in sorted(metadata, key=metadata.get, reverse=True):
            #if 100*metadata[posix_call]/total >= threashold:
            if metadata[posix_call] >= threashold:
                print "Lib call: %s invoked POSIX call: %s %.2f %% " \
                    "of times (%d)" % (lib_call, posix_call,
                                       100 * metadata[posix_call] / total,
                                       metadata[posix_call])

def stack_depth_stats(trace_graph, settings):
    """print timing info"""
    for (depth, metadata) in stats.stack_depth(trace_graph):
        print ("STACK-DEPTH: %s %d") % (depth, metadata)

def posix_timing_stats(trace_graph, settings):
    """print timing info"""
    total = 0
    for (posix_call, metadata) in stats.posix_timing(trace_graph):
        total += sum(metadata)

    import statistics
    nl = 0
    print "#call,ptime,ttime,min,max,mean,stdev,nl(time,in,nanoseconds)"
    for (posix_call, metadata) in stats.posix_timing(trace_graph):
        nl += 1
        perc = 100*float(sum(metadata))/total
        if perc == 0.0:
            perc = 0.0000001
        elif perc <= 0.1:
            perc = 0.1000000

        if len(metadata) >= 2:
            stdev = statistics.stdev(metadata)
        else:
            stdev = 0.0
        print ("%s,%.7f,%d,%d,%d,%.2f,%.2f,%d") % (posix_call.split(':')[0], \
                perc, sum(metadata), min(metadata), max(metadata), int(sum(metadata)/len(metadata)),stdev, nl)

    print ("Total Time: %d") % total

def posix_call_stats(trace_graph, settings):
    """For each POSIX call, print:
        1) All library function callers
        2) Aggregate results groupeing caller into libraries
    """
    threashold = settings.POSIX_CALL_THREASHOLD
    posix_total = stats.posix_invocations(trace_graph)
    # why I had sorted(trace_graph.posix_calls(trace_graph)))?
    for (posix_call, metadata) in stats.posix_calls(trace_graph):
        total = sum(map(lambda x: x[1], metadata.items()))
        # nasty workaround
        if  posix_call.split(':')[1] != 'libc.so':# or total == 1:
            continue
        print ("POSIX call: %s invoked in total: -> %d <- times that is %.2f"
               " %% of total POSIX invocations.") % (posix_call,
                                                     total,
                                                     100 * total / posix_total)
        for lib_call in sorted(metadata, key=metadata.get, reverse=True):
            if 100 * metadata[lib_call] / total >= threashold:
                print "POSIX call: %s invoked by Lib call: %s %.2f %%" \
                    " of times (%d)" % (posix_call, lib_call,
                                        100 * metadata[lib_call] / total,
                                        metadata[lib_call])

    for (posix_call, metadata) in stats.posix_calls_per_lib(trace_graph):
        total = sum(map(lambda x: x[1], metadata.items()))
        for lib in sorted(metadata, key=metadata.get, reverse=True):
            if 100 * metadata[lib] / total >= threashold:
                print "POSIX call: %s invoked by Library: %s %.2f %%" \
                    " of times (%d)" % (posix_call, lib,
                                        100 * metadata[lib] / total,
                                        metadata[lib])

def full_path_stats(trace_graph, settings):
    """Print fullpaths"""
    threashold = settings.FULL_PATH_THREASHOLD
    for (full_path, count) in stats.full_paths(trace_graph):
        if count > threashold:
            print "Full path: %s occured %d times." % (full_path, count)

def hidden_abstractions(trace_graph, settings):
    """doc goes here"""
    for (posix_call, lib_call, metadata, total) in \
            abstractions.hidden_abstraction(trace_graph, settings):
        print "<HIDDEN ABSTRACTION> POSIX call: %s invoked by Lib: " \
            "%s %.2f %% of times (%d)" % (posix_call, lib_call,
                                          100 * metadata / total, metadata)

def obsolete_abstractions(graph, settings):
    """doc goes here"""
    for (posix_call, call_total, posix_total) in \
            abstractions.obsolete_abstraction(graph, settings):
        print "<OBSOLETE ABSTRACTION> POSIX call: %s invoked %.2f %% of " \
            "times (%d)" % (posix_call, 100 * call_total / posix_total,
                            call_total)

def missing_abstractions(graph, settings):
    """doc goes here"""
    for pattern, count in abstractions.missing_abstractions(graph, settings):
        print "<MISSING ABSTRACTION> chain:",
        for item in pattern.split('->')[:]:
                print item.split(':')[2],
        print count

def circular_abstractions(graph, settings):
    """doc goes here"""
    for pattern, count in abstractions.circular_abstractions(graph, settings):
        print "<LIBRARY CIRCLE:> chain:", pattern, count

def obscurity_ratio(graph, settings):
    """doc goes here"""
    for lib, val in obscurity.obscurity_ratio(graph, settings):
        print "Library: %s Obscurity Ratio: %.2f" % (lib, val)

max_stars_repo_name: columbia/libtrack

max_issues_repo_name: columbia/libtrack

max_forks_repo_name: columbia/libtrack

reason: import networkx


text:
import pytest
import numpy as np
import oloid.geometric_functions

def test_get_orthogonal_raise():
    with pytest.raises(TypeError):
        oloid.geometric_functions.get_orthogonal_vector([0,1,2])

@pytest.mark.parametrize("input_vector", [(np.array([0, 1, 0])),
                                                   (np.array([1, 0, 0])),
                                                   (np.array([0, 0, 1])),
                                                   (np.array([2, 1, 3])),
                                                   (np.array([1, 1, 1])),
                                                   ])
def test_get_orthogonal(input_vector):
    ortho_vector = oloid.geometric_functions.get_orthogonal_vector(np.array(input_vector))
    print(ortho_vector)
    assert np.dot(input_vector, ortho_vector) == 0

ext: py

max_stars_repo_name: schrma/oloid

max_issues_repo_name: schrma/oloid

max_forks_repo_name: schrma/oloid

reason: import numpy


text:
import numpy as np
from sklearn import metrics
from sklearn import cluster

from clumpy.cluster import auto_kmeans

def compute_improvement(ref, new):
    return float(ref - new) / ref

def incremental_kmeans(X,
                       clusterer=None,
                       min_improvement=0.01,
                       min_cluster_samples=None,
                       max_n_clusters=30,
                       random_state=1234,
                       n_jobs=1):
    """incremental_kmeans.

    Split worst cluster based on the silhouette score. This algorithm
    proceeds as follows:

        1. Start with n_clusters = K
        2. Identify the worst cluster based on an unsupervised measure
           (e.g. silhouette score)
        3. Split the worst cluster into 2 sub-clusters
        4. Measure the global improvement with the new clusters
        5. If you get an improvement continue adding clusters.

    Parameters
    ----------
    X : np.ndarray (n_samples, n_features)
        The dataset to cluster.

    clusterer : scikit-learn style clusterer or None
        A pre-fitted cluster class (KMeans) fitted on the dataset `X`.
        If None then fit a KMeans cluster based on `auto_kmeans`.

    min_improvement : float
        Minimum score improvement to continue incrementing the clusters

    max_n_clusters : int
        Maximum number of clusters to form before terminating the iteration.
        Set to None to continue indefintiely until `min_improvement` is met.

    random_state : int
        Seed to the random number generator

    n_jobs : int
        Number of jobs to run in parallel. Used by the clustering object.
    """
    if clusterer is None:
        clusterer = auto_kmeans(X, n_clusters=[2], n_jobs=n_jobs)

    n_clusters = clusterer.n_clusters
    if n_clusters >= max_n_clusters:
        return clusterer

    labels = clusterer.labels_

    # compute the distributions of the labels
    labels_ratio = np.histogram(labels, bins=np.unique(labels).shape[0])[0]
    labels_ratio = np.array(labels_ratio, dtype=np.float64) / labels.shape[0]
    scores = metrics.silhouette_samples(X, labels, metric='euclidean')
    score = scores.mean()

    # measure global performance of each cluster
    k_scores = np.zeros(n_clusters)
    for k in range(n_clusters):
        k_scores[k] = scores[np.where(labels == k)].mean()

    # identify the cluster to split
    index = np.where(labels_ratio > 0.01)[0]
    worst_score = k_scores[index].max()
    worst_index = np.where(k_scores == worst_score)[0]
    worst_k = worst_index[0]

    # split worst cluster
    X_k = X[np.where(labels == worst_k)[0]]
    if min_cluster_samples and len(X_k) <= min_cluster_samples:  # not enough datapoints to split
        return clusterer

    # split into two clusters
    kmeans = cluster.KMeans(
        n_clusters=2, random_state=2, n_jobs=n_jobs, n_init=5, max_iter=10)
    kmeans.fit(X_k)

    # measure improvement (if any)
    ikmeans = cluster.KMeans(n_clusters=n_clusters+1, random_state=random_state)
    new_centers = np.array(clusterer.cluster_centers_).tolist()
    new_centers.remove(new_centers[worst_k])
    for center in kmeans.cluster_centers_:
        new_centers.append(center)
    ikmeans.cluster_centers_ = np.array(new_centers)
    new_labels = ikmeans.predict(X)
    ikmeans.labels_ = new_labels
    new_score = metrics.silhouette_score(X, ikmeans.labels_, metric='euclidean')

    improvement = compute_improvement(score, new_score)

    if improvement > min_improvement:
        print("increase k (%2.2f%%)" % (improvement * 100))
        return incremental_kmeans(
            X, clusterer=ikmeans, min_improvement=min_improvement,
            max_n_clusters=max_n_clusters,
            random_state=random_state, n_jobs=n_jobs)
    else:
        print("best k = %i (score = %.3f)" % (n_clusters, new_score))
        return clusterer

max_stars_repo_name: joshloyal/ClumPy

max_issues_repo_name: joshloyal/ClumPy

max_forks_repo_name: joshloyal/ClumPy


text:
# Script for processing the images.
# Compute plots of energy for varying parameters

import os, glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

def main():
    target_folder = "D:/Research/ModeTransformation/Data/05_10_2018/cropped"
    results_folder = os.path.join(target_folder, "csv")
    os.makedirs(results_folder, exist_ok=True)

    file_mask = "I0820A*P130*"
    # reference_file = "I0280A330P040*"
    file_extension = ".png"

    images_list = glob.glob(os.path.join(target_folder, file_mask + file_extension))

    energy = []
    for itr, item in enumerate(images_list):
        print("Reading image: ", item)
        image = cv2.imread(item, cv2.IMREAD_UNCHANGED)
        array = cv2.fastNlMeansDenoising(image, 10,10,7,21)
        # array = np.array(image)
        # cv2.imshow('Filtered', array)
        # cv2.waitKey(0)
        energy.append(np.sum(array))

    analyzer_position = range(180, 370, 10)                          # Analyzer plot
    # t = [0, 280, 580, 820, 1030, 1210]             # Current / Temperature plot

    print("energy length", len(energy))
    print("analyzer length", len(analyzer_position))

    # Store data as csv files with Panda
    raw_data = {'analyzer_position': analyzer_position,
                'energy': energy}
    df = pd.DataFrame(raw_data, columns=['analyzer_position', 'energy'])
    df.to_csv(os.path.join(results_folder, file_mask.replace('*', '_') + '.csv'))

    # Analyzer position plot
    # fig, ax = plt.subplots()
    # ax.plot(analyzer_position, energy, "r--")
    #
    # ax.set(xlabel='Analyzer '+r"$^{o}$", ylabel='Energy (A.U.)',
    #        title='')
    # plt.xticks(analyzer_position)
    # ax.grid()

    # Store figure
    # mng = plt.get_current_fig_manager()
    # mng.resize(*mng.window.maxsize())
    # fig.savefig(os.path.join(target_folder, file_mask+file_extension))

    # plt.show()
    print('Done....Goodbye!')

def compute_centroid(array):
    """"Compute centroid of the array"""
    X, Y = np.meshgrid(np.linspace(0,array.shape[1]-1, array.shape[1]), np.linspace(0,array.shape[0]-1, array.shape[0]))
    X += 0.5
    Y += 0.5
    centroid_x = np.sum(array * X) / np.sum(array)
    centroid_y = np.sum(array * Y) / np.sum(array)
    return centroid_x, centroid_y

def compute_energy(array):
    return np.sum(array)

if __name__ == "__main__":
    main()


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Neural Network Verification Model Translation Tool (NNVMT)

@author:
  Patrick Musau(patrick.musau@vanderbilt.edu)
  Diego Manzanas Lopez (diego.manzanas.lopez@vanderbilt.edu)
"""

from __future__ import division, print_function, unicode_literals
import numpy as np
import os
from src.NeuralNetParser import NeuralNetParser
import scipy.io as sio
from onnx import *
import onnx
import tensorflow as tf
from onnx_tf.backend import prepare

#abstract class for ONNX printers
class onnxPrinter(NeuralNetParser):
    #function for creating the matfile
    #get the name of the file without the end extension
    def __init__(self,pathToOriginalFile, OutputFilePath,*vals):
        filename = os.path.basename(os.path.normpath(pathToOriginalFile))
        filename=filename.replace('.onnx','')
        #save the filename and path to file as a class variable
        self.originalFilename=filename
        # save original input to pass as an argument to the parse function
        self.pathToOriginalFile=pathToOriginalFile
        self.originalFile=open(pathToOriginalFile,"r")
        self.outputFilePath=OutputFilePath
        tf.reset_default_graph()

    #function for creating the matfile
    def create_matfile(self):
        self.final_output_path=os.path.join(self.outputFilePath, self.originalFilename+".mat")
        self.parse_nn(self.pathToOriginalFile,self.final_output_path)
        self.originalFile.close()

    # Load ONNX model
    def load_model(self,input_path):
        onnx_model = onnx.load(input_path)  # load onnx model
        tf_rep = prepare(onnx_model)
        return tf_rep

    #Get the dictionary and list of model's tensors
    def inform(self,model):
        #initialize empty lists
        temp = []
        tensor_list = []
        tensor_dict = model.tensor_dict # Dictionary of tensors
        for key, value in tensor_dict.items(): # Get names of tensors in a list
            temp = [key,value]
            tensor_list.append(temp)
        return tensor_dict,tensor_list

    # Get weight, bias and activation functions
    def parameters(self,tensor_dict,tensor_list,model):
        # Get graph and initialize session
        gr = model.graph
        sess = tf.Session(graph = gr)
        #empty lists to store weights, biases and act functions
        W = []
        b = []
        act_fcns = []
        # After creating session, we will get the weights, activation layers and bias
        for i in range(len(tensor_dict)):
            if 'W' in tensor_list[i][0] or 'weight' in tensor_list[i][0]:
                W.append(np.float64(sess.run(tensor_dict[tensor_list[i][0]])))
            elif 'B' in tensor_list[i][0] or 'bias' in tensor_list[i][0]:
                b.append(np.float64(sess.run(tensor_dict[tensor_list[i][0]])).reshape(-1,1))
            elif 'elu' in tensor_list[i][1].op.name:
                act_fcns.append('relu')
            elif 'inear' in tensor_list[i][1].op.name:
                act_fcns.append('linear')
            elif 'anh' in tensor_list[i][1].op.name:
                act_fcns.append('tanh')
            elif 'igmoid' in tensor_list[i][1].op.name:
                act_fcns.append('sigmoid')
            elif 'oftmax' in tensor_list[i][1].op.name:
                act_fcns.append('softmax')
            elif 'add' in tensor_list[i][1].op.name:
                try:
                    if (not 'anh' in tensor_list[i+1][1].op.name) and (not 'elu' in tensor_list[i+1][1].op.name) and (not 'oftmax' in tensor_list[i+1][1].op.name) and not 'igmoid' in tensor_list[i+1][1].op.name:
                        act_fcns.append('linear')
                except:
                    act_fcns.append('linear')

        return W,b,act_fcns

    def reshape(self,W,b):
        if len(b[0]) != len(W[0]):
            for i in range(len(W)):
                W[i] = W[i].T
        return W,b

    def check_input(self,W):
	    ws = W[0].shape
	    print(ws)
	    if len(ws) == 4:
		    if ws[1] == 1 and ws[2] == 1:
		        W[0] = W[0].reshape(ws[0],ws[3],ws[1],ws[2])
		    elif ws[1] == 1 and ws[3] == 1:
		        W[0] = W[0].reshape(ws[0],ws[2],ws[1],ws[3])
	    return W

    # Save the nn information in a mat file
    def save_nn_mat_file(self,W,b,lfs,output_path):
        nn1 = dict({'W':W,'b':b,'act_fcns':lfs})
        sio.savemat(output_path, nn1)

    # parse the nn imported ONNX with tensorflow backend
    def parse_nn(self,input_path,output_path):
        model = self.load_model(input_path)
        [tensor_dict,tensor_list] = self.inform(model)
        [W,b,lfs] = self.parameters(tensor_dict,tensor_list,model)
        [W,b] = self.reshape(W,b)
        W = self.check_input(W)
        self.save_nn_mat_file(W,b,lfs,output_path)

max_stars_repo_name: verivital/nnmvt

max_issues_repo_name: verivital/nnmvt

max_forks_repo_name: verivital/nnmvt

reason: import numpy,import scipy


text:
# encoding=utf8

"""Implementation of Ackley problem."""

import numpy as np

from niapy.problems.problem import Problem

__all__ = ['Ackley']

class Ackley(Problem):
    r"""Implementation of Ackley function.

    Date: 2018

    Author: Lucija Brezočnik

    License: MIT

    Function: **Ackley function**

        :math:`f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}\sum_{i=1}^D x_i^2}\right)
        - \exp\left(\frac{1}{D}\sum_{i=1}^D \cos(c\;x_i)\right) + a + \exp(1)`

        **Input domain:**
        The function can be defined on any input domain but it is usually
        evaluated on the hypercube :math:`x_i ∈ [-32.768, 32.768]`, for all :math:`i = 1, 2,..., D`.

        **Global minimum:** :math:`f(\textbf{x}^*) = 0`, at  :math:`x^* = (0,...,0)`

    LaTeX formats:
        Inline:
            $f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}
            \sum_{i=1}^D x_i^2}\right) - \exp\left(\frac{1}{D}
            \sum_{i=1}^D cos(c\;x_i)\right) + a + \exp(1)$

        Equation:
            \begin{equation}f(\mathbf{x}) =
            -a\;\exp\left(-b \sqrt{\frac{1}{D} \sum_{i=1}^D x_i^2}\right) -
            \exp\left(\frac{1}{D} \sum_{i=1}^D \cos(c\;x_i)\right) +
            a + \exp(1) \end{equation}

        Domain:
            $-32.768 \leq x_i \leq 32.768$

    Reference:
        https://www.sfu.ca/~ssurjano/ackley.html

    """

    def __init__(self, dimension=4, lower=-32.768, upper=32.768, a=20.0, b=0.2, c=2 * np.pi, *args, **kwargs):
        r"""Initialize Ackley problem.

        Args:
            dimension (Optional[int]): Dimension of the problem.
            lower (Optional[Union[float, Iterable[float]]]): Lower bounds of the problem.
            upper (Optional[Union[float, Iterable[float]]]): Upper bounds of the problem.
            a (Optional[float]): a parameter.
            b (Optional[float]): b parameter.
            c (Optional[float]): c parameter.

        See Also:
            :func:`niapy.problems.Problem.__init__`

        """
        super().__init__(dimension, lower, upper, *args, **kwargs)
        self.a = a
        self.b = b
        self.c = c

    @staticmethod
    def latex_code():
        r"""Return the latex code of the problem.

        Returns:
            str: Latex code.

        """
        return r'''$f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}
                \sum_{i=1}^D x_i^2}\right) - \exp\left(\frac{1}{D}
                \sum_{i=1}^D \cos(c\;x_i)\right) + a + \exp(1)$'''

    def _evaluate(self, x):
        val1 = np.sum(np.square(x))
        val2 = np.sum(np.cos(self.c * x))

        temp1 = -self.b * np.sqrt(val1 / self.dimension)
        temp2 = val2 / self.dimension

        return -self.a * np.exp(temp1) - np.exp(temp2) + self.a + np.exp(1)


text:
from statistics import linear_regression
import numpy as np
from PIL import Image
import sys
from pathlib import Path
import os
import math

PATH = Path(sys.argv[1])
tileSize = int(sys.argv[2])

designDBUs = {
	"8t1" : ((0, 390800), (0, 383040)),
	"8t2" : ((0, 1301600), (0, 1148360)),
	"8t3" : ((0, 1977170), (0, 1410020)),
	"8t4" : ((0, 1777200), (0, 1210000)),
	"8t5" : ((0, 1857000), (0, 1840000)),
	"8t6" : ((0, 1715200), (0, 1062400)),
	"8t7" : ((0, 2716400), (0, 2650880)),
	"8t8" : ((0, 2716400), (0, 2650880)),
	"8t9" : ((0, 1820000), (0, 1568800)),
	"8t10" : ((0, 1820000), (0, 1568800)),
	"9t1" : ((0, 296800), (0, 292000)),
	"9t2" : ((0, 1745600), (0, 1178400)),
	"9t3" : ((0, 390600), (0, 390000)),
	"9t4" : ((-2000, 1602000), (-2000, 1552000)),
	"9t5" : ((0, 906000), (0, 906000)),
	"9t6" : ((0, 2716400), (0, 2650880)),
	"9t7" : ((0, 3161400), (0, 3033600)),
	"9t8" : (),
	"9t9" : (),
	"9t10" : ()
}

grSizes = {
	"8t1" : (80, 80)
}

def getCongMapChannel(dir, rule, gridSize, value):
	print(rule)
	vios = []
	designName = str(dir).split("/")[-1]
	# print("Get " + rule + " of: " + str(dir))
	longName = "ispd1" + designName.split('t')[0] + "_test" + designName.split("t")[1] + ".solution.def"
	fileName = longName + "." + rule
	file = open(os.path.join(dir, fileName))
	logName = "ispd1" + designName.split('t')[0] + "_test" + designName.split("t")[1] + ".log"
	logFile = open(os.path.join(dir, logName))
	logLines = logFile.readlines()
	logLineStart = 0
	while logLines[logLineStart][11:16] != "METAL":
		logLineStart += 1
	logLineStart += 1
	layer = 0
	# trackStart = 0
	# trackPitch = 0
	# cpStart = 0
	# cpPitch = 0
	direction = 'X'
	channel = np.zeros(gridSize)
	while True:
		line = file.readline()
		if not line:
			break
		if (line[0:5] == "Layer"):
			layer = int(line[6])
			trackNum = int(logLines[logLineStart+layer].split('), crossPts=(locs=')[0].split(',#=')[1])
			# trackStart = int(logLines[logLineStart+layer].split('-')[0].split('=')[-1])
			# trackPitch = int(logLines[logLineStart+layer].split('pitch=')[1].split(',')[0])
			# cpStart = int(logLines[logLineStart+layer].split('crossPts=(locs=')[1].split('-')[0])
			# cpEnd = int(logLines[logLineStart+layer].split('crossPts=(locs=')[1].split('-')[1].split(',#=')[0])
			cpNum = int(logLines[logLineStart+layer].split(',#=')[-1].split('), #grids=')[0])
			# cpPitch = (cpEnd - cpStart) / (cpNum - 1)
			direction = logLines[logLineStart+layer][23]
			continue
		# trackDBU = int(line.split(" ")[0]) * trackPitch + trackStart
		# cpDBU = int(line.split(" ")[1]) * cpPitch + cpStart
		# if (direction == 'X'):
		# 	vios.append([layer, trackDBU, cpDBU])
		# else:
		# 	vios.append([layer, cpDBU, trackDBU])
		trackId = int(line.split(" ")[0])
		cpId = int(line.split(" ")[1])
		if (direction == 'X'):
			channel[int(trackId//math.ceil(trackNum/gridSize[0]))][int(cpId//math.ceil(cpNum/gridSize[1]))] = value
		else:
			channel[int(cpId//math.ceil(cpNum/gridSize[0]))][int(trackId//math.ceil(trackNum/gridSize[1]))] = value
	file.close()
	logFile.close()
	return channel

def getFeatsInDBU(dir, feature):
	designName = str(dir).split("/")[-1]
	# print("Get " + feature + " of: " + str(dir))
	longName = "ispd1" + designName.split('t')[0] + "_test" + designName.split("t")[1] + ".solution.def"
	fileName = longName + "." + feature
	file = open(os.path.join(dir, fileName))
	res = []
	lineList = file.read().splitlines()
	for line in lineList:
		lineSplit = line.split(" ")
		layer = int(lineSplit[0])
		lx = int(lineSplit[1])
		hx = int(lineSplit[2])
		ly = int(lineSplit[3])
		hy = int(lineSplit[4])
		res.append([layer, lx, hx, ly, hy])
	file.close()
	return res

def generateHyperImg(dir, netPins, obs, unUsedPins):
	# TODO: the channel of obs???
	designName = str(dir).split("/")[-1]
	sizeDBU = designDBUs[designName]
	xSizeDBU = sizeDBU[0][1] - sizeDBU[0][0]
	ySizeDBU = sizeDBU[1][1] - sizeDBU[1][0]
	grSize = (math.ceil(xSizeDBU / tileSize), math.ceil(ySizeDBU / tileSize))
	print('Generating hyper image of %s, size: %d x %d' % (designName, grSize[0], grSize[1]))
	image = np.zeros((9, grSize[0], grSize[1]))
	for pinBox in netPins:
		for i in range(pinBox[1] // tileSize, int(pinBox[2] / tileSize) if pinBox[2] % tileSize == 0 else int(pinBox[2] / tileSize) + 1):
			for j in range(pinBox[3] // tileSize, pinBox[4] // tileSize):
				image[pinBox[0]][i][j] = 1
	for pinBox in obs:
		for i in range(pinBox[1] // tileSize, int(pinBox[2] / tileSize) if pinBox[2] % tileSize == 0 else int(pinBox[2] / tileSize) + 1):
			for j in range(pinBox[3] // tileSize, pinBox[4] // tileSize + 1):
				image[pinBox[0]][i][j] = 2
	for pinBox in unUsedPins:
		for i in range(pinBox[1] // tileSize, int(pinBox[2] / tileSize) if pinBox[2] % tileSize == 0 else int(pinBox[2] / tileSize) + 1):
			for j in range(pinBox[3] // tileSize, pinBox[4] // tileSize + 1):
				image[pinBox[0]][i][j] = 3
	return image

def genCongMap(dir):
	designName = str(dir).split("/")[-1]
	sizeDBU = designDBUs[designName]
	xSizeDBU = sizeDBU[0][1] - sizeDBU[0][0]
	ySizeDBU = sizeDBU[1][1] - sizeDBU[1][0]
	mapSize = (math.ceil(xSizeDBU / tileSize), math.ceil(ySizeDBU / tileSize))
	print('Generating congestion map of %s, size: %d x %d' % (designName, mapSize[0], mapSize[1]))
	image = np.zeros((9, mapSize[0], mapSize[1]))
	# for point in poorWireInDBU:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 1
	# for point in wireShortVio:
	# 	if (int(point[1]//tileSize) >= mapSize[0] or int(point[2]//tileSize) >= mapSize[1]):
	# 		print((point))
	# 		continue
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 2
	# for point in wireSpaceVio:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 3
	# for point in poorVia:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 4
	# for point in sameLayerViaVios:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 5
	# for point in viaBotWireVios:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 6
	# for point in viaTopViaVios:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 7
	# for point in viaTopWireVios:
	# 	image[point[0]][int(point[1]//tileSize)][int(point[2]//tileSize)] = 8
	vios = ['poorWire', 'wireShortVio', 'wireSpaceVio', 'poorVia', 'sameLayerViaVios', 'viaBotWireVios', 'viaTopViaVios', 'viaTopWireVios']
	for i in range(len(vios)):
		image[i] = getCongMapChannel(dir, vios[i], mapSize, i+1)
	return image

for dir in PATH.glob("*"):
	designName = str(dir).split("/")[-1]
	# longName = "ispd1" + designName.split('t')[0] + "_test" + designName.split("t")[1] + ".solution.def"
	# poorWireInDBU = getViosInDBU(dir, "poorWire")
	# wireShortVio = getViosInDBU(dir, "wireShortVio")
	# wireSpaceVio = getViosInDBU(dir, "wireSpaceVio")
	# poorVia = getViosInDBU(dir, "poorVia")
	# sameLayerViaVios = getViosInDBU(dir, "sameLayerViaVios")
	# viaBotWireVios = getViosInDBU(dir, "viaBotWireVios")
	# viaTopViaVios = getViosInDBU(dir, "viaTopViaVios")
	# viaTopWireVios = getViosInDBU(dir, "viaTopWireVios")
	# netPins = getFeatsInDBU(dir, "netPins")

	# print(len(netPins))
	obs = getFeatsInDBU(dir, "obs")
	unUsedPins = getFeatsInDBU(dir, "unUsedPins")

	# hyperImage = generateHyperImg(dir, netPins, obs, unUsedPins)
	congMap = genCongMap(dir)
	# np.save("hyperImage_"+designName, hyperImage)
	# np.save("congMap_"+designName, congMap)


text:
# coding: utf-8

# TODO: - init_fem is called whenever we call discretize. we should check that
#         nderiv has not been changed. shall we add quad_order too?

# TODO: avoid using os.system and use subprocess.call

import sys
import os
import importlib
from mpi4py import MPI

from psydac.api.ast.fem         import AST
from psydac.api.ast.parser      import parse
from psydac.api.printing.pycode import pycode
from psydac.api.settings        import PSYDAC_BACKENDS, PSYDAC_DEFAULT_FOLDER
from psydac.api.utilities       import mkdir_p, touch_init_file, random_string, write_code

__all__ = ('BasicCodeGen', 'BasicDiscrete')

#==============================================================================
# TODO have it as abstract class
class BasicCodeGen:
    """ Basic class for any discrete concept that needs code generation """

    def __init__(self, expr, **kwargs):

        # Get default backend from environment, or use 'python'.
        default_backend = PSYDAC_BACKENDS.get(os.environ.get('PSYDAC_BACKEND'))\
                       or PSYDAC_BACKENDS['python']

        namespace = kwargs.pop('namespace', globals())
        backend   = kwargs.pop('backend', default_backend)
        folder    = kwargs.pop('folder', None)
        comm      = kwargs.pop('comm', None)
        root      = kwargs.pop('root', None)
        # ...
        if not( comm is None):
            if root is None:
                root = 0

            assert isinstance( comm, MPI.Comm )
            assert isinstance( root, int      )

            if comm.rank == root:
                tag = random_string( 8 )
                ast = self._create_ast( expr, tag, comm=comm, backend=backend, **kwargs )
                max_nderiv = ast.nderiv
                func_name = ast.expr.name
                arguments = ast.expr.arguments.copy()
                free_args = arguments.pop('fields', ()) +  arguments.pop('constants', ())
                free_args = tuple(str(i) for i in free_args)

            else:
                tag = None
                ast = None
                max_nderiv = None
                func_name  = None
                free_args  = None

            comm.Barrier()
            tag = comm.bcast( tag, root=root )
            max_nderiv = comm.bcast( max_nderiv, root=root )
            func_name  = comm.bcast(func_name, root=root)
            free_args  = comm.bcast(free_args, root=root)
            #user_functions = comm.bcast( user_functions, root=root )
        else:
            tag = random_string( 8 )
            ast = self._create_ast( expr, tag, backend=backend, **kwargs )
            max_nderiv = ast.nderiv
            func_name = ast.expr.name
            arguments = ast.expr.arguments.copy()
            free_args = arguments.pop('fields', ()) +  arguments.pop('constants', ())
            free_args = tuple(str(i) for i in free_args)
        # ...
        user_functions = None
        self._expr = expr
        self._tag = tag
        self._ast = ast
        self._func_name = func_name
        self._free_args = free_args
        self._user_functions = user_functions
        self._backend = backend
        self._folder = self._initialize_folder(folder)
        self._comm = comm
        self._root = root
        self._max_nderiv = max_nderiv
        self._code = None
        self._func = None
        self._dependencies_modname = 'dependencies_{}'.format(self.tag)
        self._dependencies_fname   = '{}.py'.format(self._dependencies_modname)
        # ...

        # ... when using user defined functions, there must be passed as
        #     arguments of discretize. here we create a dictionary where the key
        #     is the function name, and the value is a valid implementation.
        # if user_functions:
        #     for f in user_functions:
        #         if not hasattr(f, '_imp_'):
        #             # TODO raise appropriate error message
        #             raise ValueError('can not find {} implementation'.format(f))

        if ast:
            self._save_code(self._generate_code(), backend=self.backend['name'])

        if comm is not None: comm.Barrier()

        # compile code
        self._compile(namespace)

    @property
    def expr(self):
        return self._expr

    @property
    def tag(self):
        return self._tag

    @property
    def user_functions(self):
        return self._user_functions

    @property
    def free_args(self):
        return self._free_args

    @property
    def ast(self):
        return self._ast

    @property
    def code(self):
        return self._interface_code

    @property
    def func(self):
        return self._func

    @property
    def backend(self):
        return self._backend

    @property
    def comm(self):
        return self._comm

    @property
    def root(self):
        return self._root

    @property
    def folder(self):
        return self._folder

    @property
    def dependencies_fname(self):
        return self._dependencies_fname

    @property
    def dependencies_modname(self):
        return self._dependencies_modname

    def _create_ast(self, **kwargs):
        raise NotImplementedError('Must be implemented')

    def _initialize_folder(self, folder=None):
        # ...
        if folder is None:
            basedir = os.getcwd()
            folder = PSYDAC_DEFAULT_FOLDER
            folder = os.path.join( basedir, folder )

            # ... add __init__ to all directories to be able to
            touch_init_file('__pycache__')
            for root, dirs, files in os.walk(folder):
                touch_init_file(root)
            # ...

        else:
            raise NotImplementedError('user output folder not yet available')

        folder = os.path.abspath( folder )
        mkdir_p(folder)
        # ...

        return folder

    def _generate_code(self):
        # ... generate code that can be pyccelized
        code = ''
        if self.backend['name'] == 'pyccel':
            code = 'from pyccel.decorators import types'
        elif self.backend['name'] == 'numba':
            code = 'from numba import njit'

        ast = self.ast
        expr = parse(ast.expr, settings={'dim': ast.dim, 'nderiv': ast.nderiv, 'mapping':ast.mapping, 'target':ast.domain}, backend=self.backend)

        code = '{code}\n{dep}'.format(code=code, dep=pycode(expr))

        return code

    def _save_code(self, code, backend=None):
        # ...
        write_code(self._dependencies_fname, code, folder = self.folder)

    def _compile_pythran(self, namespace, mod):
        raise NotImplementedError('Pythran is not available')

    def _compile_pyccel(self, namespace, mod, verbose=False):

        # ... convert python to fortran using pyccel
        compiler       = self.backend['compiler']
        fflags         = self.backend['flags']
        _PYCCEL_FOLDER = self.backend['folder']

        from pyccel.epyccel import epyccel

        fmod = epyccel(mod,
                       compiler    = compiler,
                       fflags      = fflags,
                       comm        = self.comm,
                       bcast       = True,
                       folder      = _PYCCEL_FOLDER,
                       verbose     = verbose)

        return fmod

    def _compile(self, namespace):

        module_name = self.dependencies_modname
        sys.path.append(self.folder)
        package = importlib.import_module( module_name )
        sys.path.remove(self.folder)

        if self.backend['name'] == 'pyccel':
            package = self._compile_pyccel(namespace, package)
        elif self.backend['name'] == 'pythran':
            package = self._compile_pythran(namespace, package)

        self._func = getattr(package, self._func_name)

#==============================================================================
class BasicDiscrete(BasicCodeGen):
    """ mapping is the symbolic mapping here.
    kwargs is used to pass user defined functions for the moment.
    """

    def __init__(self, expr, kernel_expr, **kwargs):

        kwargs['kernel_expr'] = kernel_expr

        BasicCodeGen.__init__(self, expr, **kwargs)
        # ...
        self._kernel_expr = kernel_expr
        # ...

    @property
    def kernel_expr(self):
        return self._kernel_expr

    @property
    def target(self):
        return self._target

    @property
    def mapping(self):
        return self._mapping

    @property
    def is_rational_mapping(self):
        return self._is_rational_mapping

    @property
    def max_nderiv(self):
        return self._max_nderiv

    def _create_ast(self, expr,tag, **kwargs):
        discrete_space      = kwargs.pop('discrete_space', None)
        kernel_expr         = kwargs['kernel_expr']
        quad_order          = kwargs.pop('quad_order', None)
        is_rational_mapping = kwargs.pop('is_rational_mapping', None)
        mapping             = kwargs.pop('mapping', None)
        mapping_space       = kwargs.pop('mapping_space', None)

        return AST(expr, kernel_expr, discrete_space, mapping_space=mapping_space, tag=tag, quad_order=quad_order,
                    mapping=mapping, is_rational_mapping=is_rational_mapping)

max_stars_repo_name: pyccel/psydac

max_issues_repo_name: pyccel/psydac

max_forks_repo_name: pyccel/psydac

reason: from numba


text:
# Class adapted from:
# https://github.com/TheMrGhostman/InceptionTime-Pytorch
# and
# https://github.com/okrasolar/pytorch-timeseries
import numpy as np
from typing import cast, Union, List

import torch
from torch import nn

from DeepLearning.deep_learning_utils import Conv1dSamePadding

class ShortNetwork(nn.Module):
    """A PyTorch implementation of the InceptionTime model.
    From https://arxiv.org/abs/1909.04939
    Attributes
    ----------
    num_blocks:
        The number of inception blocks to use. One inception block consists
        of 3 convolutional layers, (optionally) a bottleneck and (optionally) a residual
        connector
    in_channels:
        The number of input channels (i.e. input.shape[-1])
    number_channel_out_conv:
        The number of "hidden channels" to use. Can be a list (for each block) or an
        int, in which case the same value will be applied to each block
    bottleneck_channels:
        The number of channels to use for the bottleneck. Can be list or int. If 0, no
        bottleneck is applied
    kernel_sizes:
        The size of the kernels to use for each inception block. Within each block, each
        of the 3 convolutional layers will have kernel size
        `[kernel_size // (2 ** i) for i in range(3)]`
    num_pred_classes:
        The number of output classes
    """

    def __init__(self, num_blocks: int, in_channels: int, number_channel_out_conv: Union[List[int], int],
                 bottleneck_channels: Union[List[int], int], kernel_sizes: Union[List[int], int],
                 use_residuals: Union[List[bool], bool, str] = 'default',
                 num_pred_classes: int = 2, activation_function=nn.SiLU
                 ) -> None:
        super().__init__()
        # for easier saving and loading
        self._input_args = {
            'num_blocks': num_blocks,
            'in_channels': in_channels,
            'out_channels': number_channel_out_conv,
            'bottleneck_channels': bottleneck_channels,
            'kernel_sizes': kernel_sizes,
            'use_residuals': use_residuals,
            'num_pred_classes': num_pred_classes
        }
        bottleneck_channels = cast(List[int], self._expand_to_blocks(bottleneck_channels,
                                                                     num_blocks))
        kernel_sizes = cast(List[int], self._expand_to_blocks(kernel_sizes, num_blocks))
        if use_residuals == 'default':
            use_residuals = [True if i % 3 == 2 else False for i in range(num_blocks)]

        use_residuals = cast(List[bool], self._expand_to_blocks(
            cast(Union[bool, List[bool]], use_residuals), num_blocks)
                             )

        blocks = []
        for i in range(num_blocks):
            if i == 0:
                blocks.append(InceptionBlock(in_channels=in_channels, out_channels=number_channel_out_conv,
                                             residual=use_residuals[i], bottleneck_channels=bottleneck_channels[i],
                                             kernel_size=kernel_sizes[i], activation_function=activation_function))
            else:
                blocks.append(
                    InceptionBlock(in_channels=number_channel_out_conv * 4, out_channels=number_channel_out_conv,
                                   residual=use_residuals[i], bottleneck_channels=bottleneck_channels[i],
                                   kernel_size=kernel_sizes[i], activation_function=activation_function))
        self._blocks = nn.Sequential(*blocks)
        self._linear = nn.Linear(in_features=number_channel_out_conv * 4, out_features=num_pred_classes)
        print(self)
        print("Number Parameters: ", self.get_n_params())

    def get_n_params(self):
        model_parameters = filter(lambda p: p.requires_grad, self.parameters())
        number_params = sum([np.prod(p.size()) for p in model_parameters])
        return number_params

    def forward(self, input: torch.Tensor, get_features=False) -> torch.Tensor:  # type: ignore
        x = input
        x = torch.swapaxes(x, 2, 1)
        x = self._blocks(x).mean(dim=-1)  # the mean is the global average pooling
        if get_features:
            return x
        else:
            return self._linear(x)

    @staticmethod
    def _expand_to_blocks(value: Union[int, bool, List[int], List[bool]],
                          num_blocks: int) -> Union[List[int], List[bool]]:
        if isinstance(value, list):
            assert len(value) == num_blocks, \
                f'Length of inputs lists must be the same as num blocks, ' \
                f'expected length {num_blocks}, got {len(value)}'
        else:
            value = [value] * num_blocks
        return value

class InceptionBlock(nn.Module):
    """An inception block consists of an (optional) bottleneck, followed
    by 3 conv1d layers. Optionally residual
    """

    def __init__(self, in_channels: int, out_channels: int,
                 residual: bool, activation_function, stride: int = 1, bottleneck_channels: int = 32,
                 kernel_size: int = 41) -> None:
        super().__init__()
        self._use_bottleneck = bottleneck_channels > 0
        if self._use_bottleneck:
            self._bottleneck = Conv1dSamePadding(in_channels, bottleneck_channels,
                                                 kernel_size=1, bias=False)
        kernel_size_s = [kernel_size // (2 ** i) for i in range(3)]
        start_channels = bottleneck_channels if self._use_bottleneck else in_channels

        self._conv1 = Conv1dSamePadding(in_channels=start_channels, out_channels=out_channels,
                                        kernel_size=kernel_size_s[0], stride=stride, bias=False)
        self._conv2 = Conv1dSamePadding(in_channels=start_channels, out_channels=out_channels,
                                        kernel_size=kernel_size_s[1] + 1, stride=stride, bias=False)
        self._conv3 = Conv1dSamePadding(in_channels=start_channels, out_channels=out_channels,
                                        kernel_size=kernel_size_s[2] + 1, stride=stride, bias=False)
        self._maxpool = nn.MaxPool1d(kernel_size=3, stride=1, padding=1)
        self._conv_from_maxpool = Conv1dSamePadding(in_channels=start_channels, out_channels=out_channels,
                                                    kernel_size=1, stride=1, bias=False)
        self._activation = activation_function()
        self._batch_norm = nn.BatchNorm1d(num_features=out_channels * 4)

        self._use_residual = residual
        if residual:
            self._residual = nn.Sequential(*[
                Conv1dSamePadding(in_channels=in_channels, out_channels=out_channels * 4,
                                  kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm1d(out_channels * 4),
                activation_function()
            ])

    def forward(self, x: torch.Tensor) -> torch.Tensor:  # type: ignore
        original_x = x
        if self._use_bottleneck:
            x = self._bottleneck(x)
        z_maxpool = self._maxpool(x)
        z1 = self._conv1(x)
        z2 = self._conv2(x)
        z3 = self._conv3(x)
        z4 = self._conv_from_maxpool(z_maxpool)
        z_concatenated = torch.cat([z1, z2, z3, z4], 1)
        z_concatenated = self._activation(self._batch_norm(z_concatenated))
        if self._use_residual:
            z_concatenated = z_concatenated + self._residual(original_x)
        return z_concatenated


text:
"""
.. _ref_field:

Field
=====
"""
import numpy as np

from ansys import dpf
from ansys.grpc.dpf import (field_pb2, field_pb2_grpc, base_pb2)
from ansys.dpf.core.common import natures, types, locations
from ansys.dpf.core import scoping, meshed_region, time_freq_support
from ansys.dpf.core.plotter import Plotter
from ansys.dpf.core import errors
from ansys.dpf.core.field_definition import FieldDefinition
from ansys.dpf.core.field_base import _FieldBase, _LocalFieldBase
from ansys.dpf.core.dimensionnality import Dimensionnality

class Field(_FieldBase):
    """Class representing the main simulation data container.
    It can be evaluated data from a ``ansys.dpf.core.Operator``
    or created (by a factory and directly by an instance of this class).
    Field's data is always associated to its scoping (entities associated to each value)
    and support (subset of the model where is the data),
    making the field a self-describing piece of data.

    Parameters
    ----------
    nentities : int
        Number of entities reserved

    nature : natures, optional
        Nature of the field.

    location : str, optional
        Location of the field.  For example:

        - ``"Nodal"``
        - ``"Elemental"``
        - ``"ElementalNodal"``

    field : ansys.grpc.dpf.field_pb2.Field, optional
        Field message generated from a grpc stub.

    server : server.DPFServer, optional
        Server with channel connected to the remote or local instance. When
        ``None``, attempts to use the the global server.

    Examples
    --------
    >>> # 1. Create a field from scratch
    >>> from ansys.dpf.core import fields_factory
    >>> from ansys.dpf.core import locations
    >>> from ansys.dpf import core as dpf
    >>> field_with_classic_api = dpf.Field()
    >>> field_with_classic_api.location = locations.nodal
    >>> field_with_factory = fields_factory.create_scalar_field(10)

    >>> # 2. Extract a displacement field from a transient result file.
    >>> from ansys.dpf import core as dpf
    >>> from ansys.dpf.core import examples
    >>> transient = examples.download_transient_result()
    >>> model = dpf.Model(transient)
    >>> disp = model.results.displacement()
    >>> fields_container = disp.outputs.fields_container()
    >>> field = fields_container[0]
    >>> len(field)
    11460
    >>> field.component_count
    3
    >>> field.elementary_data_count
    3820

    >>> # 3. Create a displacement field
    >>> from ansys.dpf import core as dpf
    >>> import numpy as np
    >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal)
    >>> my_field.data = np.zeros(30)
    >>> my_field.scoping.ids = range(1,11)

    >>> # 4. Set data
    >>> from ansys.dpf import core as dpf
    >>> from ansys.dpf.core import examples
    >>> transient = examples.download_transient_result()
    >>> model = dpf.Model(transient)
    >>> disp = model.results.displacement()
    >>> fields_container = disp.outputs.fields_container()
    >>> field = fields_container[0]
    >>> field.data[2]
    array([-0.00672665, -0.03213735,  0.00016716])

    """

    def __init__(self, nentities=0, nature=natures.vector,
                 location=locations.nodal, field=None, server=None):
        """Initialize the field with either optional field message, or
        by connecting to a stub.
        """
        super().__init__(nentities, nature, location, False, field, server)
        self._field_definition = self._load_field_definition()

    def as_local_field(self):
        """Creates a deep copy of the field locally so that the user can access
        and modify it locally without any request sent to the server.
        This should be used in a with statement, so that the local field
        is released and the data is sent to the server in one shot.
        If it's not used in a with statement, the method release_data()
        should be used to actually update the field.

        Warning
        -------
        If this as_local_field method is not used as a context manager in a
        with statement or if the method release_data() is not called,
        the data will not be actually updated.

        Returns
        -------
        local_field : Field

        Examples
        --------
        >>> from ansys.dpf import core as dpf
        >>> num_entities = 3
        >>> field_to_local = dpf.fields_factory.create_3d_vector_field(num_entities, location=dpf.locations.elemental_nodal)
        >>> with field_to_local.as_local_field() as f:
        ...     for i in range(1,num_entities+1):
        ...         f.append([[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]],i)
        ...         f.get_entity_data(i-1),[[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]]
        (array([[0.1, 0.2, 0.3],
               [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]])
        (array([[0.2, 0.4, 0.6],
               [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]])
        (array([[0.3, 0.6, 0.9],
               [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]])

        """
        return _LocalField(self)

    @property
    def location(self):
        """Return the field location.

        Returns
        -------
        location : str
            Location string.  Either ``'Nodal'``, ``'Elemental'``, or
            ``'ElementalNodal'``.

        Examples
        --------
        Location for a stress field evaluated at nodes

        >>> from ansys.dpf import core as dpf
        >>> from ansys.dpf.core import examples
        >>> model = dpf.Model(examples.download_transient_result())
        >>> s_op = model.results.stress()
        >>> s_fc = s_op.outputs.fields_container()
        >>> field = s_fc[0]
        >>> field.location
        'ElementalNodal'

        """
        if self.field_definition:
            return self.field_definition.location

    @location.setter
    def location(self, value):
        """Change the field location.

        Parameters
        -------
        location : str or locations
            Location string.  Either ``'Nodal'``, ``'Elemental'``,
            ``'ElementalNodal'``...

        Examples
        --------
        Location for a field evaluated at nodes

        >>> from ansys.dpf import core as dpf
        >>> import numpy as np
        >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal)
        >>> my_field.data = np.zeros(30)
        >>> my_field.scoping.ids = range(1,11)
        >>> my_field.location
        'Nodal'
        >>> my_field.location = dpf.locations.elemental_nodal
        >>> my_field.location
        'ElementalNodal'

        """
        fielddef = self.field_definition
        fielddef.location = value
        self.field_definition = fielddef

    @property
    def shell_layers(self):
        """Return the field's shell layers order (or lack of shell layers)
        that defines how the field's data is ordered.

        Returns
        -------
        Enum : shell_layers
        """
        if self.field_definition:
            return self.field_definition.shell_layers

    @shell_layers.setter
    def shell_layers(self, value):
        """Change the field's shell layers.

        Parameters
        -------
        Enum : shell_layer
        """
        fielddef = self.field_definition
        fielddef.shell_layers = value
        self.field_definition = fielddef

    def to_nodal(self):
        """Convert this field to one with a Nodal location.

        Only valid when this field's location is ElementalNodal or
        Elemental.

        Returns
        -------
        nodal_field : Field
            with ``location=='Nodal'``.
        """
        if self.location == 'Nodal':
            raise errors.LocationError('Location is already "Nodal"')

        op = dpf.core.Operator("to_nodal")
        op.inputs.connect(self)
        return op.outputs.field()

    def plot(self, notebook=None, shell_layers=None):
        """Plot the field/fields container on mesh support if exists.

        Warning
        -------
        This is primarily added out of convenience as plotting
        directly from the field can be slower than extracting the
        meshed region and plotting the field on top of that.  It is
        more efficient to plot with:

        >>> from ansys.dpf import core as dpf
        >>> from ansys.dpf.core import examples
        >>> transient = examples.download_transient_result()
        >>> model = dpf.Model(transient)
        >>> mesh = model.metadata.meshed_region
        >>> disp = model.results.displacement()
        >>> fields_container = disp.outputs.fields_container()
        >>> field = fields_container[0]
        >>> mesh.plot(field)
        [(1.675707321585373, 1.675707321585373, 2.425707321585373),
         (0.0, 0.0, 0.75),
         (0.0, 0.0, 1.0)]

        Parameters
        ----------
        notebook : bool, optional
            Bool, that specifies if the plotting is in the notebook as
            a static image or or as a dynamic plot outside of the
            notebook.

        shell_layers : shell_layers, optional
            Enum used to set the shell layers if the model to plot
            contains shell elements.
        """
        pl = Plotter(self.meshed_region)
        pl.plot_contour(self, notebook, shell_layers)

    def resize(self, nentities, datasize):
        """Allocate memory.

        Parameters
        ----------
        nentities : int
            num ids in the scoping

        datasize : int
            data vector size
        """
        request = field_pb2.UpdateSizeRequest()
        request.field.CopyFrom(self._message)
        request.size.scoping_size = nentities
        request.size.data_size = datasize
        self._stub.UpdateSize(request)

    def _load_field_definition(self):
        """Attempt to load the field definition for this field"""
        try:
            request=field_pb2.GetRequest()
            request.field.CopyFrom(self._message)
            out = self._stub.GetFieldDefinition(request)
            return FieldDefinition(out.field_definition, self._server)
        except:
            return

    @property
    def unit(self):
        """Units of the field

        Returns
        ----------
        unit : str

        Examples
        --------
        Units of a displacement field

        >>> from ansys.dpf import core as dpf
        >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal)
        >>> my_field.unit = "m"
        >>> my_field.unit
        'm'

        """
        if self.field_definition:
            return self.field_definition.unit

    @unit.setter
    def unit(self, value):
        """Change the unit of the field

        Parameters
        ----------
        unit : str

        Examples
        --------
        Units of a displacement field

        >>> from ansys.dpf import core as dpf
        >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal)
        >>> my_field.unit = "m"
        >>> my_field.unit
        'm'

        """
        fielddef = self.field_definition
        fielddef.unit = value
        self.field_definition = fielddef

    @property
    def dimensionnality(self):
        """The Dimensionnality represents the shape of an elementary
        data contains in the Field.

        Returns
        -------
        dimensionnality : Dimensionnality
            nature and size of the elementary data
        """
        if self.field_definition:
            return self.field_definition.dimensionnality

    @dimensionnality.setter
    def dimensionnality(self, value):
        """
        Parameters
        ----------
        dimensionnality : dpf.core.Dimensionnality
            nature and size of the elementary data
        """
        fielddef = self.field_definition
        fielddef.dimensionnality = value
        self.field_definition = fielddef

    @property
    def name(self):
        request = field_pb2.GetRequest()
        request.field.CopyFrom(self._message)
        out = self._stub.GetFieldDefinition(request)
        return out.name

    def _set_field_definition(self, field_definition):
        """
        Parameters
        ----------
        field_definition : FieldDefinition
        """
        request = field_pb2.UpdateFieldDefinitionRequest()
        request.field_def.CopyFrom(field_definition._messageDefinition)
        request.field.CopyFrom(self._message)
        self._stub.UpdateFieldDefinition(request)

    @property
    def field_definition(self):
        """The field definition defines what is the field: its location, unit, dimensionnality, shell layers...
        """
        return self._field_definition

    @field_definition.setter
    def field_definition(self, value):
        """The field definition defines what is the field: its location, unit, dimensionnality, shell layers...

        Parameters
        ----------
        field_definition : FieldDefinition
        """
        return self._set_field_definition(value)

    def _get_meshed_region(self):
        """
        Returns
        -------
        meshed_region : MeshedRegion
        """
        request = field_pb2.SupportRequest()
        request.field.CopyFrom(self._message)
        request.type = base_pb2.Type.Value("MESHED_REGION")
        try:
            message = self._stub.GetSupport(request)
            return meshed_region.MeshedRegion(mesh=message, server = self._server)
        except:
            raise RuntimeError("The field's support is not a mesh.  Try a time_freq_support.")

    def _get_time_freq_support(self):
        """
        Returns
        -------
        time_freq_support : TimeFreqSupport
        """
        request = field_pb2.SupportRequest()
        request.field.CopyFrom(self._message)
        request.type = base_pb2.Type.Value("TIME_FREQ_SUPPORT")
        try:
            message = self._stub.GetSupport(request)
            return time_freq_support.TimeFreqSupport(time_freq_support=message, server = self._server)
        except:
            raise RuntimeError("The field's support is not a timefreqsupport.  Try a mesh.")

    def _set_support(self, support, support_type: str):
        """
        Set field support as time_freq_support or meshed_region.
        """
        request = field_pb2.SetSupportRequest()
        request.field.CopyFrom(self._message)
        request.support.type = base_pb2.Type.Value(support_type)
        request.support.id = support._message.id
        self._stub.SetSupport(request)

    @property
    def time_freq_support(self):
        """
        Time_freq_support of the associated field.

        Returns
        -------
        time_freq_support : TimeFreqSupport
            Describe the time frequency support of the field.
        """
        return self._get_time_freq_support()

    @time_freq_support.setter
    def time_freq_support(self, value):
        """
        Set the time_freq_support of the associated field.

        Parameters
        ----------
        time_freq_support : TimeFreqSupport
            Describe the time frequency support of the field.
        """
        self._set_support(value, "TIME_FREQ_SUPPORT")

    @property
    def meshed_region(self):
        """
        Meshed_region of the associated field.

        Return
        ------
        meshed_region : MeshedRegion
            Describe the mesh support of the field.
        """
        return self._get_meshed_region()

    @meshed_region.setter
    def meshed_region(self, value):
        """
        Set the meshed_region of the associated field.

        Parameters
        ----------
        meshed_region : MeshedRegion
            Describe the mesh support of the field.
        """
        self._set_support(value, "MESHED_REGION")

    def __add__(self, field_b):
        """Adds two fields together

        Returns
        -------
        add : operators.math.add
        """
        from ansys.dpf.core import dpf_operator
        from ansys.dpf.core import operators
        if hasattr(operators, "math") and  hasattr(operators.math, "add") :
            op= operators.math.add()
        else :
            op= dpf_operator.Operator("add")
        op.connect(0,self)
        op.connect(1, field_b)
        return op

    def __pow__(self, value):
        if value != 2:
            raise ValueError('DPF only the value is "2" suppported')
        from ansys.dpf.core import dpf_operator
        from ansys.dpf.core import operators
        if hasattr(operators, "math") and  hasattr(operators.math, "sqr") :
            op= operators.math.sqr(self)
        else :
            op= dpf_operator.Operator("sqr")
            op.connect(0,self)
        return op

    def __mul__(self, value):
        """Multiplies two fields together

        Returns
        -------
        mul : operators.math.generalized_inner_product
        """
        from ansys.dpf.core import dpf_operator
        from ansys.dpf.core import operators
        if hasattr(operators, "math") and  hasattr(operators.math, "generalized_inner_product") :
            op= operators.math.generalized_inner_product()
        else :
            op= dpf_operator.Operator("generalized_inner_product")
        op.connect(0,self)
        op.connect(1, value)
        return op

    def __sub__(self, fields_b):
        """Subtract two fields together

        Returns
        -------
        minus : operators.math.minus
        """
        from ansys.dpf.core import dpf_operator
        from ansys.dpf.core import operators
        if hasattr(operators, "math") and  hasattr(operators.math, "minus") :
            op= operators.math.minus()
        else :
            op= dpf_operator.Operator("minus")
        op.connect(0,self)
        op.connect(1, fields_b)
        return op

    def _min_max(self):
        from ansys.dpf.core import dpf_operator
        op = dpf_operator.Operator("min_max")
        op.connect(0, self)
        return op

    def min(self):
        """Component-wise minimum over this field

        Returns
        -------
        min : Field
            Component-wise minimum field.
        """
        return self._min_max().get_output(0, types.field)

    def max(self):
        """Component-wise maximum over this field

        Returns
        -------
        max : Field
            Component-wise maximum field.
        """
        return self._min_max().get_output(1, types.field)

    def deep_copy(self,server=None):
        """Creates a deep copy of the field's data on a given server.
        This can be useful to pass data from one server instance to another.

        Parameters
        ----------
        server : DPFServer, optional

        Returns
        -------
        field_copy : Field

        Examples
        --------
        >>> from ansys.dpf import core as dpf
        >>> from ansys.dpf.core import examples
        >>> transient = examples.download_transient_result()
        >>> model = dpf.Model(transient)
        >>> disp = model.results.displacement()
        >>> fields_container = disp.outputs.fields_container()
        >>> field = fields_container[0]
        >>> other_server = dpf.start_local_server(as_global=False)
        >>> deep_copy = field.deep_copy(server=other_server)

        """
        f = Field(nentities=len(self.scoping), location=self.location,nature=self.field_definition.dimensionnality.nature, server=server)
        f.scoping = self.scoping.deep_copy(server)
        f.data = self.data
        f.unit = self.unit
        f.location = self.location

        try:
            f._data_pointer = self._data_pointer
        except:
            pass
        try:
            f.meshed_region = self.meshed_region.deep_copy(server=server)
        except:
            pass
        try:
            f.time_freq_support = self.time_freq_support.deep_copy(server=server)
        except:
            pass

        return f

class _LocalField(_LocalFieldBase,Field):
    """Class only created by a field to cache the internal data of the field,
    modify it locallly, and send a single update request to the server
    when the local field is deleted

    Parameters
    ----------
    field : Field
        field to copy

    Examples
    --------
    >>> from ansys.dpf import core as dpf
    >>> import numpy as np
    >>> num_entities = 3
    >>> field_to_local = dpf.fields_factory.create_3d_vector_field(num_entities, location=dpf.locations.elemental_nodal)
    >>> with field_to_local.as_local_field() as f:
    ...     for i in range(1,num_entities+1):
    ...         f.append(np.array([[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]]),i)
    ...         f.get_entity_data(i-1),[[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]]
    (array([[0.1, 0.2, 0.3],
           [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]])
    (array([[0.2, 0.4, 0.6],
           [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]])
    (array([[0.3, 0.6, 0.9],
           [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]])

    """

    def __init__(self, field):
        super().__init__(field)

max_stars_repo_name: hoangxuyenle/DPF-Core

max_issues_repo_name: hoangxuyenle/DPF-Core

max_forks_repo_name: hoangxuyenle/DPF-Core


text:
import numpy as np
from igraph import Graph, plot
from itertools import combinations
import matplotlib
matplotlib.use('SVG')
import sys
reload(sys)  # Reload does the trick!
sys.setdefaultencoding('UTF8')

__author__ = 'Erica-Li'

class hash_tag_graph:
    """
    hash tag graph class
    """

    def __init__(self,window_duration=60,verbose=True):
        """
        Initialize the class

        :return: hash_tag_graph object
        """
        self.t_window = window_duration
        self.latest_time = 0
        self.graph = Graph()
        self.verbose=verbose

    def trim(self):
        """
        remove edges outside time window

        :return:  None
        """

        # identify edges outside window

        min_time = self.latest_time - self.t_window
        edges_to_trim = self.graph.es.select(time_lt=min_time)
        if self.verbose: print("Edges to trim: "+str(edges_to_trim))

        # remove edges outside of t_window
        self.graph.delete_edges(edges_to_trim)

        # identify vertices with 0 degree to delete
        vertices_to_trim = self.graph.vs.select(_degree=0)
        if self.verbose: print("Vertices to trim: "+str(vertices_to_trim._name_index))
        self.graph.delete_vertices(vertices_to_trim)

    def add_tweet(self,hash_tag_tuple,epoch_time):
        """

        Adds tweet to hash tag graph and updates graph such that it only contains tweets
        withing window_duration of the latest in time tweet. If tweet is outside of the window_duration
        than it is not added to the graph and nothing happens

        :return:
        """
        # Check if tweet is in order, inside the window duration, or outside
        t_diff = self.latest_time - epoch_time > self.t_window

        if t_diff <= self.t_window:
            self.latest_time = max(epoch_time,self.latest_time)

            current_vertices = self.graph.vs._name_index
            if self.verbose:
                print('Graph name index: '+str(current_vertices))
                print('Graph name index type: '+str(type(current_vertices)))

            # current vertivces will have none type when it is initilazed empty
            if current_vertices is not None:

                # Add hashtag to graph as vertex, if its already exists, nothing happens
                for hash_tag in hash_tag_tuple:
                    # only add hashtag if it isn't already in the graph
                    if hash_tag not in current_vertices:
                        if self.verbose: print("Adding Vertex: "+str(hash_tag))
                        self.graph.add_vertex(name=hash_tag)
            else:
                # Add hashtag to graph as vertex, if its already exists, nothing happens
                for hash_tag in hash_tag_tuple:
                    if self.verbose: print("Adding Vertex: "+str(hash_tag))
                    self.graph.add_vertex(name=hash_tag)

            # Add edges with associated epoch time
            for edge in combinations(hash_tag_tuple,r=2):
                if self.verbose: print('Adding Edge Pair:'+str(edge)+" Time:"+str(epoch_time))

                self.graph.add_edge(source=edge[0],target=edge[1],time=epoch_time)

            self.trim()

        # if tweet is outside of the time window than toss it
        else:
            return

        return

    def get_mean_degree(self):
        """
        Compute the average degree

        :return: np.float, average graph degree
        """

        return np.mean(self.graph.degree())

    def draw_graph(self,path):
        """
        Utlity for visualizing the graph.

        :return: 0
        """
        print("Drawing HashTag Graph!")

        layout = self.graph.layout("kk")

        self.graph.vs["label"] = self.graph.vs["name"]

        visual_style = {}
        visual_style["bbox"] = (1600,1600)
        visual_style["vertex_size"] = 10
        visual_style["edge_width"] = 1
        visual_style["edge_color"] = 'blue'
        visual_style["layout"] = layout
        visual_style["margin"] = 100

        graph_plot = plot(self.graph, **visual_style)
        graph_plot.save(fname=path)

        return 0

max_stars_repo_name: Li-Erica/Insight-Challenge

max_issues_repo_name: Li-Erica/Insight-Challenge

max_forks_repo_name: Li-Erica/Insight-Challenge


text:
'''
Description: open3D显示点云 https://blog.csdn.net/weixin_41281151/article/details/107119326  显示空白
Author: HCQ
Company(School): UCAS
Email: 1756260160@qq.com
Date: 2021-03-06 17:46:00
LastEditTime: 2021-04-27 10:51:30
FilePath: /Point-Clouds-Visualization/2open3D/ply/02open3d_load_ply.py
'''

import open3d as o3d  # 点云可视化
import os
import numpy as np # 读取txt文件
# import matplotlib.pyplot as plt # 没用到
# from pandas import DataFrame #  DataFrame(point_cloud_raw[:, 0:3]) 选取点云前三列
from pyntcloud import PyntCloud # pyntcloud是用于处理 3D 点云的python 库

# # o3d读取ply文件===================================
# print("Load a ply point cloud, print it, and render it")
# pcd = o3d.io.read_point_cloud("/home/hcq/python/Point-Clouds-Visualization/2open3D/data/fragment.ply")
# # print(pcd)
# # print(np.asarray(pcd.points))
# o3d.visualization.draw_geometries([pcd], zoom=0.3412,
#                                   front=[0.4257, -0.2125, -0.8795],
#                                   lookat=[2.6172, 2.0475, 1.532],
#                                   up=[-0.0694, -0.9768, 0.2024])

# ##########################################2 @@@@######################################

# 1 PyntCloud加载自己的点云文件=================================
file_name = "/home/hcq/python/Point-Clouds-Visualization/2open3D/data/fragment.ply"
point_cloud_pynt = PyntCloud.from_file(file_name)

## 2 np加载自己的点云文件=========================================
# points = np.genfromtxt("/home/hcq/data/modelnet40/modelnet40_normal_resampled/cup/cup_0006.txt", delimiter=",")
# points = DataFrame(points[:, 0:3]) # 前三列
# points.columns = ['x', 'y', 'z'] # 标题
# point_cloud_pynt = PyntCloud(points) # ================

## 转成open3d能识别的格式
point_cloud_o3d = point_cloud_pynt.to_instance("open3d", mesh=False)
o3d.visualization.draw_geometries([point_cloud_o3d]) # 显示原始点云


text:
#! /usr/bin/env python3

import os
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from multiprocessing import Pool
from datetime import datetime
import arrow

data_dir = 'clean_data/'
out_dir = 'curves/'
out_dir = os.path.dirname(out_dir) + '/'

if out_dir:
    os.makedirs(out_dir, exist_ok=True)

def decode_to_bool(bytes_to_decode):
    if bytes_to_decode == b'True': return True
    else: return False

def plot_data(title, times, data):
    #monthsloc = mdates.MonthLocator()
    #daysloc= mdates.DayLocator()
    #monthsfmt = mdates.DateFormatter('%B')
    #daysfmt   = mdates.DateFormatter('%d')

    fig, ax = plt.subplots()
    ax.set_title(title)
    #ax.xaxis.set_major_locator(monthsloc)
    #ax.xaxis.set_major_formatter(monthsfmt)
    #ax.xaxis.set_minor_locator(daysloc)
    #ax.xaxis.set_minor_formatter(daysfmt)
    ax.plot(times, data)

def plot_full_data(fname, utc_diff):
    print(fname)
    #data =  np.loadtxt(data_dir + fname, dtype = 'bool', delimiter=',', usecols=1, converters = {1:decode_to_bool})
    #times = np.loadtxt(data_dir + fname, dtype = 'datetime64', delimiter=',', usecols=0, converters = {0:np.datetime64})
    array_load = np.load(data_dir + fname)
    times = array_load[:,0].astype('datetime64[s]').astype('datetime64[m]')
    times = times + np.timedelta64(utc_diff, 'h')
    data = array_load[:,1]

    # convert to hour granularity
    times = times.astype('datetime64[h]').astype(datetime)
    unique_times = np.unique(times)
    bins = []
    for hour in unique_times:
        bins.append(np.sum(data[times == hour]))
    bins = np.asarray(bins)
    plot_data(fname, unique_times, bins)
    plt.show()

def generate_average_event_curve(fname, utc_diff, date_range):
    print(fname)
    #data =  np.loadtxt(data_dir + fname, dtype = 'bool', delimiter=',', usecols=1, converters = {1:decode_to_bool})
    #times = np.loadtxt(data_dir + fname, dtype = 'datetime64', delimiter=',', usecols=0, converters = {0:np.datetime64})
    #times = times + np.timedelta64(utc_diff, 'h')
    array_load = np.load(data_dir + fname)
    times = array_load[:,0].astype('datetime64[s]').astype('datetime64[m]')
    times = times + np.timedelta64(utc_diff, 'h')
    data = array_load[:,1]

    # convert to hour granularity
    times = times.astype('datetime64[h]').astype(datetime)
    # limit to range we're interested in
    selection = np.logical_and(times >= date_range[0], times < date_range[1])
    times = times[selection]
    data = data[selection]
    # get days of activity
    times_days = times.astype('datetime64[D]').astype('datetime64[h]').astype(datetime)
    days = np.unique(times_days)
    avg_curve = []
    for day in days:
        times_in_day = times[times_days == day]
        unique_times = np.unique(times_in_day)
        bins = []
        for hour in unique_times:
            bins.append(np.sum(data[times == hour]))
        bins = np.asarray(bins)
        avg_curve.append(bins)
    avg_curve = np.asarray(avg_curve)
    avg_curve = np.average(avg_curve, axis = 0)
    return avg_curve

fname = 'motion-Blink-c098e5900064-3rd_Floor_2016-07-27_clean.npy'
curve = generate_average_event_curve(fname, -4, (datetime(2016, 7, 30), datetime(2016, 8, 6)))
plot_data(fname, np.arange(curve.size), curve/np.max(curve))
np.save(out_dir+fname.split('-')[2] + '_reactive_curve', curve/np.max(curve))
plt.show()