text:
#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

#labels = ['S\nxlink', 'PD\nxlink', 'S', 'PD']

# Sandwich xlink, PD xlink, Sandwich, PD
#IC = [12.5 * 10 ** -5, 8.03 * 10 **-5, 16*10**-5, 8.1 * 10 **-5]
#IC_error = [.11 * 10 ** -5, .06 * 10 ** -5, 0.2 * 10 ** -5, 0.1 * 10 ** -5]
#p2p = [ 4.1, 4.1, 4.18, 4.17]
#p2p_error = [0.08, 0.10, 0.13, 0.12]
#dbwl = [4.378, 4.22, 4.33, 4.27]
#dbwl_error = [0.1, 0.07, 0.03, 0.04]
#correlation_length = [4, 8.77, 4.5, 15.5]
#correlation_length_error = [0.62, 2.02, 0.4, 1.9]

# different order
labels = ['S', 'S\nxlink', 'PD', 'PD\nxlink']
print(labels)

# Sandwich, Sandwich xlink, PD, PD xlink
IC = [16*10**-5, 12.5*10**-5, 8.1*10**-5, 8.03 * 10 **-5]
IC_error = [0.2*10**-5, .11 * 10 ** -5, 0.1*10**-5, .06*10**-5]
p2p = [4.18, 4.1, 4.17, 4.1]
p2p_error = [0.13, 0.08, 0.12, 0.10]
dbwl = [4.27, 4.378, 4.33, 4.22]
dbwl_error = [0.03, 0.1, 0.04, 0.07]
correlation_length = [4.5, 4, 15.5, 8.77]
correlation_length_error = [0.4, 0.62, 1.9, 2.02]

centers = [1, 2, 3, 4]
width = 0.9
opacity = 0.9
colors = ['xkcd:blue', 'xkcd:green', 'xkcd:red', 'xkcd:orange']

# plot data
f, axarr = plt.subplots(2, 2)
barlist1 = axarr[0, 0].bar(centers, IC, width, yerr=IC_error, alpha=opacity)
barlist2 = axarr[0, 1].bar(centers, p2p, width, yerr=p2p_error, alpha=opacity)
barlist3 = axarr[1, 0].bar(centers, dbwl, width, yerr=dbwl_error, alpha=opacity)
barlist4 = axarr[1, 1].bar(centers, correlation_length, width, yerr=correlation_length_error, alpha=opacity)
for i in range(4):
	barlist1[i].set_color(colors[i])
	barlist2[i].set_color(colors[i])
	barlist3[i].set_color(colors[i])
	barlist4[i].set_color(colors[i])

# format axes
# IC plot
axarr[0, 0].set_yscale('log')
axarr[0, 0].set_ylim(1*10**-5, 5*10**-4)
axarr[0, 0].set_ylabel('Ionic Conductivity (S/m)')

# p2p plot
axarr[0, 1].set_ylim(3.5, 4.5)
axarr[0, 1].set_ylabel('Pore spacing (nm)')

# dbwl plot
axarr[1, 0].set_ylim(4.0, 4.6)
axarr[1, 0].set_ylabel('Monomer stacking distance ($\AA$)')
axarr[1, 0].set_xticks(centers)
axarr[1, 0].set_xticklabels(labels)

# correlation length plot
axarr[1, 1].set_ylim(0, 20)
axarr[1, 1].set_ylabel('Correlation Length ($\AA$)')
axarr[1, 1].set_xticks(centers)
axarr[1, 1].set_xticklabels(labels)

# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
#plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
plt.tight_layout()
plt.savefig('xlink_charts.pdf')
plt.show()

ext: py


text:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Power by Zongsheng Yue 2020-03-30 14:59:29

import torch
from scipy.special import gammaln
from torch.autograd import Function as autoF

class LogGamma(autoF):
    '''
    Implement of the logarithm of gamma Function.
    '''
    @staticmethod
    def forward(ctx, input):
        ctx.save_for_backward(input)
        if input.is_cuda:
            input_np = input.detach().cpu().numpy()
        else:
            input_np = input.detach().numpy()
        out = gammaln(input_np)
        out = torch.from_numpy(out).to(device=input.device).type(dtype=input.dtype)

        return out

    @staticmethod
    def backward(ctx, grad_output):
        input, = ctx.saved_tensors
        grad_input = torch.digamma(input) * grad_output

        return grad_input

max_stars_repo_name: zsyOAOA/VIRNet

max_issues_repo_name: zsyOAOA/VIRNet

max_forks_repo_name: zsyOAOA/VIRNet

reason: from scipy


text:
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import scipy.io as sio
from matplotlib import cm
from matplotlib.colors import Normalize
import beta_slopes

'''################### Set direcotories and open files #####################'''
# set system type as macOS or linux; set project directory; set downstream dirs
# set all these directory variables first

home_dir = os.path.expanduser("~")
project_path = 'projects/biophys_glm_show'
project_dir = os.path.join(home_dir, project_path)
bhalla_dir = os.path.join(project_dir, 'data/beta_slopes', 'bhalla')
alon_dir = os.path.join(project_dir, 'data/beta_slopes', 'alon')
figure_dir = os.path.join(project_dir, 'analysis/figures/')
table_dir = os.path.join(project_dir, 'analysis/tables/')

# run loop for pulling out lambda values and making a dataframe
def channels_lambda_to_pandas(data_dir):
    channel_file_list = os.listdir(data_dir)
    channel_file_list.sort()

    model_slopes = pd.DataFrame()

    for file in channel_file_list:
        channel_beta_slopes = beta_slopes.beta_slopes_ml_to_np(data_dir, file)
        channel_slopes_lambda_star = channel_beta_slopes.get_optimal_values()
        model_slopes = pd.concat([model_slopes, channel_slopes_lambda_star], ignore_index=True)

    model_slopes.set_index(['model', 'channel'], inplace=True)

    return model_slopes

def get_channel_list(model_slopes):
    model_name = model_slopes.index[0][0]
    channel_list = model_slopes.loc[model_name].index.unique().tolist()

    return channel_list

# basic test
alon = beta_slopes.optimal_slopes(alon_dir)

plt.figure()
for channel in alon.channels:
    channel_lambda_star = alon.df.loc[alon.model, channel]
    channel_stimulus = channel_lambda_star['stimulus'].values
    plt.plot(channel_stimulus)

reason: import numpy,import scipy


text:
## Name: PNG to NPY
## Description: For the sketchy dataset, reads every image in the
##              sub folders and save the images both in vectorized
##              form and in matrix form for easier loading later on
## Author: Kiran Bhattacharyya
## Directions: To use this code. First download skecthy dataset from
##              http://sketchy.eye.gatech.edu/
##              Then place this code within the sketchy folder but
##              not in the subfolders. When you run this, it will save
##              two new files in every subfolder as within the directory
##              in the variable 'pngLoc' on line 20 below.

import glob
from scipy import misc
import numpy as np

# location of folders containing sketches
# that are 256 by 256 grayscale images
pngLoc = '256X256/sketch/tx_000100000000'

# grab folder names with sketches
allFolders = glob.glob(pngLoc + '/*')

# get names of folders for class names
classNames = [name.split('\\')[1] for name in allFolders]

# go through each folder and get names of images
# to create a list of lists
imgFilesPerFolder = []
for folder in allFolders:
    allImgs = glob.glob(folder + '/*.png')
    imgFilesPerFolder.append(allImgs)

# read each image in every folder to create
# 1. a stack of images that retains image shape 256 by 256 by num_of_images
# 2. reshape the image to a vector to have (256*256) 65536 by num_of_images
# 1. can be used to train convolutional neural nets
# 2. can be used to train basic MLPs and other autoregressive algorithms
folderNum = 0
for imgStack in imgFilesPerFolder:
    print('Processing folder number ' + str(folderNum+1) + ' of ' + str(len(allFolders)) + '.')
    num_of_images = len(imgStack)
    img_matStack = np.zeros((256, 256, num_of_images))
    img_vecStack = np.zeros((num_of_images, 65536))
    imgNum = 0
    for img in imgStack:
        thisImg = misc.imread(img)
        thisImg = np.mean(thisImg, axis=2) # average r,g,b channels
        thisImg = thisImg > 125 # thresholds image to make binary image
        thisImg_reshape = np.reshape(thisImg, 65536)
        img_matStack[:, :, imgNum] = thisImg
        img_vecStack[imgNum, :] = thisImg_reshape
        imgNum += 1
    np.save(allFolders[folderNum] + '/matStack.npy', img_matStack)
    np.save(allFolders[folderNum] + '/vecStack.npy', img_vecStack)
    print('        finished process.')
    folderNum += 1

max_stars_repo_name: MiningMyBusiness/SketchyExperiments

max_issues_repo_name: MiningMyBusiness/SketchyExperiments

max_forks_repo_name: MiningMyBusiness/SketchyExperiments

reason: import numpy,from scipy


text:
# ==============================================================================
# Load data
# Copyright 2017 Kyoto Univ. Okuno lab. . All Rights Reserved.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import numpy as np
import tensorflow as tf

class dotdict(dict):
    """dot.notation access to dictionary attributes"""

    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

def load_data(
    config,
    with_shuffle=True,
    with_train_test=True,
    test_flag=False,
    output_dict_flag=True,
):
    time_major = config["time_major"]
    l = None
    if not test_flag:
        x = np.load(config["data_train_npy"])
        if config["mask_train_npy"] is None:
            m = np.ones_like(x)
        else:
            m = np.load(config["mask_train_npy"])
        if config["steps_train_npy"] is None:
            s = [len(x[i]) for i in range(len(x))]
            s = np.array(s)
        else:
            s = np.load(config["steps_train_npy"])
        if config["label_train_npy"] is not None:
            l = np.load(config["label_train_npy"])
    else:
        x = np.load(config["data_test_npy"])
        if config["mask_test_npy"] is None:
            m = np.ones_like(x)
        else:
            m = np.load(config["mask_test_npy"])
        if config["steps_test_npy"] is None:
            s = [len(x[i]) for i in range(len(x))]
            s = np.array(s)
        else:
            s = np.load(config["steps_test_npy"])
        if config["label_test_npy"] is not None:
            l = np.load(config["label_test_npy"])
    if not time_major:
        x = x.transpose((0, 2, 1))
        m = m.transpose((0, 2, 1))
    # train / validatation/ test
    data_num = x.shape[0]
    data_idx = list(range(data_num))
    if with_shuffle:
        np.random.shuffle(data_idx)
    # split train/test
    sep = [0.0, 1.0]
    if with_train_test:
        sep = config["train_test_ratio"]
    prev_idx = 0
    sum_r = 0.0
    sep_idx = []
    for r in sep:
        sum_r += r
        idx = int(data_num * sum_r)
        sep_idx.append([prev_idx, idx])
        prev_idx = idx
    print("#training data:", sep_idx[0][1] - sep_idx[0][0])
    print("#valid data:", sep_idx[1][1] - sep_idx[1][0])

    tr_idx = data_idx[sep_idx[0][0] : sep_idx[0][1]]
    te_idx = data_idx[sep_idx[1][0] : sep_idx[1][1]]
    # storing data
    train_data = dotdict({})
    valid_data = dotdict({})
    tr_x = x[tr_idx]
    te_x = x[te_idx]
    train_data.x = tr_x
    valid_data.x = te_x
    tr_m = m[tr_idx]
    te_m = m[te_idx]
    train_data.m = tr_m
    valid_data.m = te_m
    tr_s = s[tr_idx]
    te_s = s[te_idx]
    train_data.s = tr_s
    valid_data.s = te_s
    if l is not None:
        tr_l = l[tr_idx]
        te_l = l[te_idx]
        train_data.l = tr_l
        valid_data.l = te_l
    train_data.num = tr_x.shape[0]
    valid_data.num = te_x.shape[0]
    train_data.n_steps = tr_x.shape[1]
    valid_data.n_steps = te_x.shape[1]
    train_data.dim = tr_x.shape[2]
    valid_data.dim = tr_x.shape[2]
    return train_data, valid_data

max_stars_repo_name: clinfo/DeepKF

max_issues_repo_name: clinfo/DeepKF

max_forks_repo_name: clinfo/DeepKF

reason: import numpy


text:
#!/usr/bin/env python
import sys
import rospy
import numpy as np

from geometry_msgs.msg import PoseWithCovarianceStamped
from geometry_msgs.msg import Twist
from geometry_msgs.msg import PoseStamped
from turtlesim.msg import Pose

from tf.transformations import euler_from_quaternion, quaternion_from_euler

class ForceController:

    def __init__(self):
      self.poseflag=False
      self.turtlebot3_pose=Pose()
      self.goal_pose=Pose()
      self.goal=PoseStamped()
      self.vel_msg=Twist()
      #set in zero linear velocities
      self.vel_msg.linear.x=0
      self.vel_msg.linear.y=0
      self.vel_msg.linear.z=0
      #set in zero angular velocities
      self.vel_msg.angular.x=0
      self.vel_msg.angular.y=0
      self.vel_msg.angular.z=0
      #set map propierties
      self.map_size_x=250 #cm
      self.map_size_y=250 #cm
      self.resolution = 1 #cm
      self.lineal=0.07#.1 #velocidad lineal
      self.lim_angular=0.22 #limite velocidad angular.
      self.lane=1
      if (self.lane==1):
          self.matrix = np.load('/home/zarazua_rt/catkin_ws/src/tutoriales_basicos/scripts/TrayA1.npy')
      elif(self.lane==2):
          self.matrix = np.load('matrixDynamic_lane2.npy')
      else:
          self.matrix = np.load('matrixDynamic_lane2.npy')
      self.velocity_publisher=rospy.Publisher('/cmd_vel', Twist, queue_size=10)
      self.pose_subscriber=rospy.Subscriber("/amcl_pose", PoseWithCovarianceStamped,self.poseCallback,queue_size=1)
      self.rate = rospy.Rate(10) # 10hz

    def poseCallback(self,data):
        self.turtlebot3_pose.x=data.pose.pose.position.x+1.25
        self.turtlebot3_pose.y=data.pose.pose.position.y+1.25
        orientation_q = data.pose.pose.orientation
        orientation_list = [orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
        (roll, pitch, yaw) = euler_from_quaternion (orientation_list)
        self.turtlebot3_pose.theta=yaw
        print 'Posicion recibida'
        self.poseflag=True

    def getAngle(self,d1,d2):
        return np.arctan2(d2.y-d1.y,d2.x-d1.x)

    def move(self):
        x1=self.turtlebot3_pose.x
        y1=self.turtlebot3_pose.y
        x_index=np.int(x1*(100/self.resolution))#obtener el indice en x
        y_index=np.int(y1*(100/self.resolution))#obtener el indice en y
        #print 'xind=',x_index,'yind=',y_index
        yaw=self.turtlebot3_pose.theta #Guardar el angulo yaw
        #Definir limites del indice en x
        if (x_index<0):
            x_index = 0
        elif (x_index>((self.map_size_x/self.resolution)-1)):
            x_index=(self.map_size_x/self.resolution)-1

        #Definir limites del indice en y
        if (y_index<0):
            y_index = 0
        elif (y_index>((self.map_size_y/self.resolution)-1)):
            y_index=(self.map_size_y/self.resolution)-1

        x3, y3 = self.matrix[x_index,y_index,:]#Obtener la distancia a la que se quiere llegar.
        x2=x1+x3
        y2=y1+y3

        Ka=4.0
        distance_tolerance=0.05
        while True:
            x1=self.turtlebot3_pose.x
            y1=self.turtlebot3_pose.y
            yaw=self.turtlebot3_pose.theta #Guardar el angulo yaw
            dist=self.getDistance(x1,x2,y1,y2)
            goal_angle=np.arctan2(y2-y1,x2-x1)
            #Orientar la posicion del carro a la del sistema de referencia para trabajar en un plano absoluto.
            #f_x=np.cos(yaw)*x3 + np.sin(yaw)*y3#Rotacion en X.
            #f_y=-np.sin(yaw)*x3 + np.cos(yaw)*y3#Rotacion en Y
            #print 'fx=',f_x,'fy=',f_y

            print 'dist=',dist,'xi=',x1-1.25,'yi=',y1-1.25,'xf=',x2-1.25,'yf=',y2-1.25,'goal=',goal_angle,'yaw=',yaw
            angular=Ka*(goal_angle-yaw)#Ka*np.arctan(f_y/(2.5*f_x))
            #print 'goal=',goal_angle,'yaw=',yaw,'angular=',angular
            if angular>self.lim_angular:
                angular=self.lim_angular
            elif angular<-self.lim_angular:
                angular=-self.lim_angular
            #set and publish velocities
            self.vel_msg.linear.x=self.lineal
            self.vel_msg.angular.z=angular
            self.velocity_publisher.publish(self.vel_msg)
            self.rate.sleep()
            if not(dist>distance_tolerance):
                   break
        print 'Distancia alcanzada'

    def setStop(self):
        self.vel_msg.linear.x=0
        self.vel_msg.angular.z=0
        self.velocity_publisher.publish(self.vel_msg)

    def getDistance(self,x1,x2,y1,y2):
    	return np.sqrt((x2-x1)**2+(y2-y1)**2)

    def getFlag(self):
        return self.poseflag

def main():
        rospy.init_node('ForceController', anonymous=True)
        FC=ForceController() # constructor creates publishers / subscribers
        print 'Nodo inicializado'
        while FC.getFlag()==False:
            pass
        print FC.getFlag()
        t0=rospy.get_time()
        while FC.getFlag()==True:
            FC.move()
            t1=rospy.get_time()
            if not(t1-t0<30):
                break
        FC.setStop()

if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptException:
        pass

max_stars_repo_name: lfzarazuaa/LiderSeguidorA

max_issues_repo_name: lfzarazuaa/LiderSeguidorA

max_forks_repo_name: lfzarazuaa/LiderSeguidorA


text:
# coding=utf-8
# Copyright 2021 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Script to run AssembleNet++ with objects."""
import json

from absl import app
from absl import flags

import numpy as np

import tensorflow as tf  # tf

from assemblenet import assemblenet_plus

from assemblenet import model_structures

flags.DEFINE_string('precision', 'float32',
                    'Precision to use; one of: {bfloat16, float32}.')
flags.DEFINE_integer('num_frames', 64, 'Number of frames to use.')

flags.DEFINE_integer('num_classes', 157,
                     'Number of classes. 157 is for Charades')

flags.DEFINE_string('assemblenet_mode', 'assemblenet_plus',
                    '"assemblenet" or "assemblenet_plus" or "assemblenet_plus_lite"')  # pylint: disable=line-too-long

flags.DEFINE_string('model_structure', '[-1,1]',
                    'AssembleNet model structure in the string format.')
flags.DEFINE_string(
    'model_edge_weights', '[]',
    'AssembleNet model structure connection weights in the string format.')

flags.DEFINE_string('attention_mode', 'peer', '"peer" or "self" or None')

flags.DEFINE_float('dropout_keep_prob', None, 'Keep ratio for dropout.')
flags.DEFINE_bool(
    'max_pool_preditions', False,
    'Use max-pooling on predictions instead of mean pooling on features. It helps if you have more than 32 frames.')  # pylint: disable=line-too-long

flags.DEFINE_bool('use_object_input', True,
                  'Whether to use object input for AssembleNet++ or not')  # pylint: disable=line-too-long
flags.DEFINE_integer('num_object_classes', 151,
                     'Number of object classes, when using object inputs. 151 is for ADE-20k')  # pylint: disable=line-too-long

FLAGS = flags.FLAGS

def main(_):
  # Create model.

  batch_size = 2
  image_size = 256

  vid_placeholder = tf.placeholder(tf.float32,
                                   (batch_size*FLAGS.num_frames, image_size, image_size, 3))  # pylint: disable=line-too-long
  object_placeholder = tf.placeholder(tf.float32,
                                      (batch_size*FLAGS.num_frames, image_size, image_size, FLAGS.num_object_classes))  # pylint: disable=line-too-long
  input_placeholder = (vid_placeholder, object_placeholder)

  # We are using the full_asnp50_structure, since we feed both video and object.
  FLAGS.model_structure = json.dumps(model_structures.full_asnp50_structure)  # pylint: disable=line-too-long
  FLAGS.model_edge_weights = json.dumps(model_structures.full_asnp_structure_weights)  # pylint: disable=line-too-long

  network = assemblenet_plus.assemblenet_plus(
      assemblenet_depth=50,
      num_classes=FLAGS.num_classes,
      data_format='channels_last')

  # The model function takes the inputs and is_training.
  outputs = network(input_placeholder, False)

  with tf.Session() as sess:
    # Generate a random video to run on.
    # This should be replaced by a real video.
    sess.run(tf.global_variables_initializer())
    vid = np.random.rand(*vid_placeholder.shape)
    obj = np.random.rand(*object_placeholder.shape)
    logits = sess.run(outputs, feed_dict={input_placeholder: (vid, obj)})
    print(logits)
    print(np.argmax(logits, axis=1))

if __name__ == '__main__':
  app.run(main)

max_stars_repo_name: deepneuralmachine/google-research

max_issues_repo_name: deepneuralmachine/google-research

max_forks_repo_name: deepneuralmachine/google-research


text:
import numpy as np
import cv2
from multiprocessing.pool import ThreadPool

#Made by Ed Lynch licenced under the Apache 2.0 licence

pool = ThreadPool(processes=4)

#Percent Holder
percentage = [0,0,0,0]

 #Holds output of imageToJS function
imageCodeHolder = ["","","",""]

def imageToJS(start, end, x, addTo, image = []):
    imageCode = ""
    #Go through each pixel of the image
    for i in range(start,end):
        #Calculate pct complete
        pct = (float(i)/ float(end))*100
        #Update text if decimal of 5 (minimise calculations)
        if( round(pct) % 5 == 0 and pct !=0):
            #Assign the new percentage to the relavent index in the percentage array
            percentage[addTo] = int(round(pct))
            #Get the mean of all the percentages
            pct = sum(percentage)/4
            print( str(round(pct))+ "%")
        for j in range(0,x):
            #add code for this pixel
           imageCode += 'ctx.beginPath();\n'
           imageCode += 'ctx.fillRect('+str(j)+','+str(i)+',1,1);\n'
           imageCode += "ctx.fillStyle = 'rgb({},{},{})';\n".format(image[i][j][2], image[i][j][1], image[i][j][0])
           imageCode += 'ctx.stroke();\n'
           imageCode += 'ctx.closePath();\n'
    #Add to holder
    return imageCode

#Will make the image
def imageToSite(inputPath, outputPath):
    #Reset percentage
    percentage = [0,0,0,0]
    #Get the image and the size of it
    imagePath = inputPath
    image=cv2.imread(imagePath)
    y=image.shape[0] #length in first dimension
    x=image.shape[1] #length in second dimension
    #Define the canvas with the size of the image
    canvas = '<canvas id="myCanvas" width="{}" height="{}"></canvas>'.format(x, y)
    #Setup the canvas
    imageCode = "var c = document.getElementById('myCanvas');var ctx = c.getContext('2d');\n"

    #Run code to transform image to JS code in 4 threads to speed it up a notch
    t1 = pool.apply_async(imageToJS, (0, y/4, x, 0, image))
    t2 = pool.apply_async(imageToJS, (y/4, y/2, x, 1, image))
    t3 = pool.apply_async(imageToJS, (y/2, (y/2+y/4), x, 2, image))
    t4 = pool.apply_async(imageToJS, ((y/2+y/4), y, x, 3, image))

    #Add image code from holder to imageCode variable
    imageCode += (t1.get()+t2.get()+t3.get()+t4.get())

    #html code
    html = '<!DOCTYPE html>\n\n\n<title>Image to Canvas</title>\n\n\n{}\n\n\n'.format(canvas)

    #Write to the html file
    f = open('index.html','w')
    f.write(html)
    f.close()

    #Write to the js file
    f = open('script.js','w')
    f.write(imageCode)
    f.close()

    print("Done")

#Holder for input and output paths
imageToSite("cat.jpg", "")


text:
# file for training on PCA-generated continua and testing on hand-fit continua

import numpy as np
import matplotlib.pyplot as plt
from data.load_data import load_synth_spectra, split_data, normalise_spectra
from models.network import Net
from learning.learning import Trainer, create_learners, train_scalers
from learning.testing import ResidualStatistics, CorrelationMatrix
import torch
plt.rcParams["font.family"] = "serif"

# first load and split the synthetic spectra with npca = 10
wave_grid, qso_cont, qso_flux = load_synth_spectra()
# normalise the synthetic spectra
qso_flux, qso_cont = normalise_spectra(wave_grid, qso_flux, qso_cont)
splitted = split_data(qso_flux, qso_cont)
X_train, X_valid, X_test, y_train, y_valid, y_test = splitted

# now load the hand-fit spectra
path = "/net/vdesk/data2/buiten/MRP2/code/qso_cont_ml/"
handfitfile = "/data/paris_noiselessflux_regridded.npy"
handfitdata = np.load(path+handfitfile)
wave_grid_handfit = handfitdata[0,:,0]
cont_handfit = handfitdata[:,:,1]
flux_handfit = handfitdata[:,:,2]

# normalise the hand-fit spectra
flux_handfit, cont_handfit = normalise_spectra(wave_grid_handfit, flux_handfit, cont_handfit)

# compare the two grids
print ("Training grid:", wave_grid.shape)
print ("Testing grid:", wave_grid_handfit.shape)

# train the model on the training set (with help of validation set)
n_features = len(wave_grid)
size_hidden = 100
model = Net(n_features, size_hidden, n_features)
optimizer, criterion = create_learners(model.parameters())
trainer = Trainer(model, optimizer, criterion)
#trainer.train(wave_grid, X_train, y_train, X_valid, y_valid)
#scaler_X = trainer.scaler_X
#scaler_y = trainer.scaler_y

# plot the loss
#fig, ax = trainer.plot_loss()
#fig.show()

# load the last model
modelfile = "/net/vdesk/data2/buiten/MRP2/code/qso_cont_ml/examples/simple_AdamW_net.pth"
checkpoint = torch.load(modelfile)
model.load_state_dict(checkpoint["model_state_dict"])
scaler_X = checkpoint["scaler_X"]
scaler_y = checkpoint["scaler_y"]

# hack for now: train separate scalers for the hand-fit spectra
#scaler_flux_handfit, scaler_cont_handfit = train_scalers(wave_grid_handfit, flux_handfit, cont_handfit)

# evaluate
residstats = ResidualStatistics(flux_handfit, cont_handfit, scaler_X,\
                                scaler_y, model)
fig1, ax1 = residstats.plot_means(wave_grid_handfit)
fig1.show()

fig2, ax2 = residstats.resid_hist()
fig2.show()

corrmat = CorrelationMatrix(flux_handfit, cont_handfit, scaler_X,\
                            scaler_y, model)
corrmat.show(wave_grid_handfit)

# plot a random example
rand_indx = np.random.randint(low=0, high=len(cont_handfit))
result = model.full_predict(flux_handfit[rand_indx], scaler_X,\
                           scaler_y)
fig3, ax3 = plt.subplots(figsize=(7,5), dpi=320)
ax3.plot(wave_grid_handfit, flux_handfit[rand_indx], alpha=0.8, label="Input", lw=1)
ax3.plot(wave_grid_handfit, cont_handfit[rand_indx], alpha=0.8, label="Target")
ax3.plot(wave_grid_handfit, result, alpha=0.8, label="Output", ls="--")
ax3.set_xlabel("Wavelength (Angstrom)")
ax3.set_ylabel("Flux")
ax3.legend()
ax3.grid()
fig3.show()


text:
from percolation import Percolation
import numpy as np

class PercolationStats(object):
	def __init__(self, n, T):
		self.n = n
		self.T = T
		self.open_list = np.array([])
		self.PercolationStats()

	def PercolationStats(self):
		for trial in range(self.T):
			grid = Percolation(self.n) # initiatae nxn grid
			percolated = False
			num_open = 0
			while percolated == False: # while not percolating
				grid.randomised_vacancy() # randomly open cell
				num_open = grid.num_open # check how many total open cells
				percolated = grid.percolate() # check if percolated, breaks loop when true
			self.open_list  = np.append(self.open_list, np.float64(num_open)/self.n**2) # update open_list make sure value is a float

	def mean(self): #sample mean of percolation threshold
		return np.mean(self.open_list)
	def stddev(self):
		return np.std(self.open_list)/(self.T-1)
	def confidenceLo(self):
		return (self.mean() - 1.96*self.stddev())
	def confidenceHi(self):
		return (self.mean() + 1.96*self.stddev())

'''
p = PercolationStats(200,100)
print('mean:', p.mean(), 'standard deviation:', p.stddev(), '95% ci',[p.confidenceLo(), p.confidenceHi()])
'''


text:
from ..fitdata import Condition
from .utilities import unique_idx
import numpy as np

def generate_conditions(data_sets, parameter_lookup, model_params):
    """
    This function builds a list of unique conditions from a list of data sets and a list of references pointing to the
    data that belongs to each condition.

    Parameters
    ----------
    data_sets : list of Data
        References to data
    parameter_lookup : OrderedDict[str, int]
        Lookup table for looking up parameter indices by name
    model_params : list of str
        Base model parameter names
    """
    # Quickly concatenate the parameter transformations corresponding to this condition
    str_conditions = []
    for data_set in data_sets.values():
        str_conditions.append(data_set.condition_string)

        assert set(data_set.transformations.keys()) == set(
            model_params
        ), "Source parameters in data parameter transformations are incompatible with the specified model parameters."

        target_params = [x for x in data_set.transformations.values() if isinstance(x, str)]
        assert set(target_params).issubset(
            parameter_lookup.keys()
        ), "Parameter transformations contain transformed parameter names that are not in the combined parameter list."

    # Determine unique parameter conditions and the indices to get the appropriate unique condition from data index.
    unique_condition_strings, indices = unique_idx(str_conditions)
    indices = np.asarray(indices)

    data_link = []
    keys = list(data_sets.keys())
    for condition_idx in np.arange(len(unique_condition_strings)):
        (data_indices,) = np.nonzero(np.equal(indices, condition_idx))
        data_link.append([data_sets[keys[x]] for x in data_indices])

    conditions = []
    for data_sets in data_link:
        transformations = data_sets[0].transformations
        conditions.append(Condition(transformations, parameter_lookup))

    return conditions, data_link

max_stars_repo_name: lumicks/pylake

max_issues_repo_name: lumicks/pylake

max_forks_repo_name: lumicks/pylake


text:
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import json
import random
from sample_imageinpainting_HiFill.GPU_CPU import HiFill_inpainting
import wand
from wand.image import Image

"""
# This _01_preprocessing_step receives the mask and key points of person images and removes
# some biometric information (i.e., shape, head area, geometric information)

To extract the body masks use the '0_mask_extraction.py' _01_preprocessing_step.
To extract body kepoints use https://colab.research.google.com/drive/1-invDDFpyVFlVuJSAV6AWyZgh4rNc3vF?usp=sharing
"""

def load_keypoints(json_dir, img_name, img_arry, mask, display=False):
    abs_name = img_name.split(".")[0]
    json_name = abs_name + ".json"
    try:
        with open(os.path.join(json_dir, json_name)) as json_file:
            data = json.load(json_file)
            _keypoints = data['bodies'][0]['joints']
    except FileNotFoundError:
        return None
    if display:
        display_keypoints(_keypoints, img_arry)
        cv2.imshow("img", img_arry)
        cv2.imshow("msk", mask)
        cv2.waitKey(0)
    return _keypoints

def load_mask(_masks_dir_, _img_name, display=False):
    _body_mask_array = cv2.imread(os.path.join(_masks_dir_, _img_name), cv2.IMREAD_GRAYSCALE)
    if display:
        cv2.imshow("_body_mask_", _body_mask_array)
        cv2.waitKey(0)
    return _body_mask_array

def load_img(_image_dir_, _img_name_, display=False):
    _img_array = cv2.imread(os.path.join(_image_dir_, _img_name_))
    if display:
        cv2.imshow("orig_img", _img_array)
        cv2.waitKey(0)
    return _img_array

def display_keypoints(keypoints, img_arry):
    """
    keypoint 0 : nose :             keypoints[0:3]
    keypoint 1 : neck :             keypoints[3:6]
    keypoint 2 : right shoulder :   keypoints[6:9]
    keypoint 3 : right elbow :      keypoints[9:12]
    keypoint 4 : right hand :       keypoints[12:15]
    keypoint 5 : left shoulder :    keypoints[15:18]
    keypoint 6 : left elbow :       keypoints[18:21]
    keypoint 7 : left hand :        keypoints[21:24]
    keypoint 8 : right hip :        keypoints[24:27]
    keypoint 9 : right knee :       keypoints[27:30]
    keypoint 10 : right foot :      keypoints[30:33]
    keypoint 11 : left hip :        keypoints[33:36]
    keypoint 12 : left knee :       keypoints[36:39]
    keypoint 13 : left foot :       keypoints[39:42]
    keypoint 14 : right eye :       keypoints[42:45]
    keypoint 15 : left eye :        keypoints[45:49]
    keypoint 16 : right ear :       keypoints[49:51]
    keypoint 17 : left ear :        keypoints[51:53]
    """
    for i in range(0, 54, 3):
        # round(255 * (keypoints[i + 2]))
        if (keypoints[i + 2]) >= 0.8:
            color = (0, 255, 0)
        elif 0.6 <= (keypoints[i + 2]) < 0.8:
            color = (0, 125, 0)
        elif 0.3 < (keypoints[i + 2]) < 0.6:
            color = (255, 0, 0)
        else:
            color = (0, 0, 255)

        cv2.circle(img=img_arry,
                   center=(round(keypoints[i]), round(keypoints[i + 1])),
                   radius=4,
                   color=color,
                   thickness=-1)
        if i == 53:
            break

def get_head_mask(orig_mask, keypoints, display=False):
    # we can take the roi above the shoulders
    shoulder_r = keypoints[6:9]
    shoulder_l = keypoints[15:18]
    roi = [0,
           0,
           orig_mask.shape[0],
           round(max(shoulder_l[1], shoulder_r[1]))]  # x1, y1, x2, y2
    roi_img = np.copy(orig_mask[roi[1]:roi[3], roi[0]: roi[2]])  # use numpy slicing to get the image of the head
    head_mask = np.zeros(orig_mask.shape, np.uint8)  # make all the mask black
    head_mask[roi[1]:roi[3], roi[0]: roi[2]] = roi_img  # copy back only the roi

    kernel = np.ones((5, 5), np.uint8)
    head_mask = cv2.dilate(head_mask, kernel)

    if display:
        cv2.imshow("new_mask", head_mask)
        cv2.waitKey(0)

    return head_mask

def get_mask_boarder(orig_mask, display=False):
    dilated_mask = mask_dilation_erosion(orig_mask, 'dilate', proportion=0.03, _iter=1)
    eroded_mask = mask_dilation_erosion(orig_mask, 'erode', proportion=0.033, _iter=2)

    # eroded_mask = ~eroded_mask
    eroded_mask = cv2.bitwise_not(eroded_mask)
    boarder_area = cv2.bitwise_and(dilated_mask, eroded_mask)
    if display:
        cv2.imshow("boarder_area", boarder_area)
        cv2.waitKey(0)

    return boarder_area, dilated_mask, eroded_mask

def get_final_mask(head_mask, mask_boarder, display=False):
    # assert head_mask.shape==mask_boarder.shape
    # assert head_mask.dtype==mask_boarder.dtype
    final_mask = cv2.bitwise_or(head_mask, mask_boarder)
    if display:
        cv2.imshow("final_mask", final_mask)
        cv2.waitKey(0)
    return final_mask

def do_inpainting(img, mask, display=False):
    radius = int(max(np.array(mask.shape)) * 0.1)
    inpainted_img = cv2.inpaint(img, mask, radius, cv2.INPAINT_TELEA)  # cv2.INPAINT_NS
    if display:
        cv2.imshow("inpainted_img", inpainted_img)
        cv2.waitKey(0)
    return inpainted_img

def mask_dilation_erosion(mask, flag, proportion=0.03, _iter=1):
    kernel_size = (np.array(mask.shape) * proportion).astype(int)
    kernel = np.ones(kernel_size, np.uint8)
    if flag == 'dilate':
        _mask = cv2.dilate(mask, kernel, iterations=_iter)
    elif flag == 'erode':
        _mask = cv2.erode(mask, kernel, iterations=_iter)
    else:
        print("Specify the flag correctly")
        raise ValueError

    return _mask

def perturbed_mesh(row, column, desplay=False):
    # the idea has been taken from paper https://www.juew.org/publication/DocUNet.pdf
    mr = row
    mc = column

    xx = np.arange(mr - 1, -1, -1)
    yy = np.arange(0, mc, 1)

    # xx1 = np.random.randint(0,mr,(1,mr))[0]
    # yy1 =np.random.randint(0,mc,(1,mc))[0]

    [y, X] = np.meshgrid(xx, yy)
    X_flatten = X.flatten('F')
    Y_flatten = y.flatten('F')
    XY_mat = [X_flatten, Y_flatten]
    XY_mat_arr = np.asarray(XY_mat)
    ms = np.transpose(XY_mat_arr, (1, 0))

    perturbed_mesh_ = ms
    nv = np.random.randint(20) - 1
    for k in range(nv):
        # Choosing one vertex randomly
        vidx = np.random.randint(np.shape(ms)[0])
        vtex = ms[vidx, :]
        # Vector between all vertices and the selected one
        xv = perturbed_mesh_ - vtex
        # Random movement
        mv = (np.random.rand(1, 2) - 0.5) * 20
        hxv = np.zeros((np.shape(xv)[0], np.shape(xv)[1] + 1))
        hxv[:, :-1] = xv
        hmv = np.tile(np.append(mv, 0), (np.shape(xv)[0], 1))
        d = np.cross(hxv, hmv)
        d = np.absolute(d[:, 2])
        d = d / (np.linalg.norm(mv, ord=2))
        wt = d

        curve_type = np.random.rand(1)
        if curve_type > 0.3:
            alpha = np.random.rand(1) * 50 + 50
            wt = alpha / (wt + alpha)
        else:
            alpha = np.random.rand(1) + 1
            wt = 1 - (wt / 100) ** alpha
        msmv = mv * np.expand_dims(wt, axis=1)
        perturbed_mesh_ = perturbed_mesh_ + msmv
    if desplay:
        plt.scatter(perturbed_mesh_[:, 0], perturbed_mesh_[:, 1], c=np.arange(0, mr * mc))
        plt.show()
    return perturbed_mesh_[:, 0], perturbed_mesh_[:, 1]

def make_same_sizes(img1, img2, img2_mask):
    h1, w1 = img1.shape[0:2]
    h2, w2 = img2.shape[0:2]
    if h1 > h2:  # increase the height of img2
        img2_p = cv2.copyMakeBorder(img2, int((h1 - h2) / 2), int((h1 - h2) / 2), 0, 0, borderType=cv2.BORDER_REFLECT)
        img2_m_p = cv2.copyMakeBorder(img2_mask, int((h1 - h2) / 2), int((h1 - h2) / 2), 0, 0,
                                      borderType=cv2.BORDER_REFLECT)
        img1_p = np.copy(img1)
    elif h2 > h1:  # increase the height of img1
        img2_p = np.copy(img2)
        img2_m_p = np.copy(img2_mask)
        img1_p = cv2.copyMakeBorder(img1, int((h2 - h1) / 2), int((h2 - h1) / 2), 0, 0, borderType=cv2.BORDER_REFLECT)
    else:
        img1_p = np.copy(img1)
        img2_p = np.copy(img2)
        img2_m_p = np.copy(img2_mask)

    if w1 > w2:  # increase the width of img2
        img2_p = cv2.copyMakeBorder(img2_p, 0, 0, int((w1 - w2) / 2), int((w1 - w2) / 2), borderType=cv2.BORDER_REFLECT)
        img2_m_p = cv2.copyMakeBorder(img2_m_p, 0, 0, int((w1 - w2) / 2), int((w1 - w2) / 2),
                                      borderType=cv2.BORDER_REFLECT)
    elif w2 > w1:  # increase the width of img1
        img1_p = cv2.copyMakeBorder(img1_p, 0, 0, int((w2 - w1) / 2), int((w2 - w1) / 2), borderType=cv2.BORDER_REFLECT)
    else:
        pass

    if img1_p.shape[0:2] == img2_p.shape[0:2] == img2_m_p.shape[0:2]:
        return img1_p, img2_p, img2_m_p
    else:  # make them the same size
        # h,w = img1_p.shape[0:2]
        img1_p = cv2.resize(img1_p, (w1, h1))
        img2_p = cv2.resize(img2_p, (w1, h1))
        img2_m_p = cv2.resize(img2_m_p, (w1, h1))
        return img1_p, img2_p, img2_m_p

def do_deformed_mesh(background, img, mask, display=False):
    nw, nh = img.shape[0:2]
    dw = nw // 2
    dh = nh // 2

    gray_mask = np.zeros(mask.shape[0:2], dtype=np.uint8)
    gray_mask[:, :] = mask[:, :, 0]
    body_area = cv2.bitwise_and(img, img, mask=gray_mask)

    # when performing the deformation, sometimes the some parts of the RoI are placed out of the image boarders.
    # Therefore, to avoid it, we first enlarge the image and its mask.
    extended_body_area = cv2.copyMakeBorder(body_area, dh, dh, dw, dw, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
    extended_body_area_mask = cv2.copyMakeBorder(gray_mask, dh, dh, dw, dw, borderType=cv2.BORDER_CONSTANT,
                                                 value=(0, 0, 0))

    nw, nh = extended_body_area.shape[0:2]

    xs, ys = perturbed_mesh(nh, nw, False)  # the result is like np.meshgrid
    xs = xs.reshape(nh, nw).astype(np.float32)
    ys = ys.reshape(nh, nw).astype(np.float32)

    deformed_extended_img = cv2.remap(extended_body_area, ys, xs, cv2.INTER_CUBIC)
    deformed_extended_img = cv2.rotate(deformed_extended_img, cv2.ROTATE_90_CLOCKWISE)

    deformed_extended_msk = cv2.remap(extended_body_area_mask, ys, xs, cv2.INTER_CUBIC)
    deformed_extended_msk = cv2.rotate(deformed_extended_msk, cv2.ROTATE_90_CLOCKWISE)
    # minimum rectangular contour (We capture the deformed RoI and then we will make it the same size of the original
    # image. Then, we past it on the original background)
    deformed_image = cv2.cvtColor(deformed_extended_img, cv2.COLOR_BGR2GRAY)
    x, y, w, h = cv2.boundingRect(deformed_image)

    # rectangular contour of image ==> should be resized to original image size
    deformed_image = deformed_extended_img[y:y + h, x:x + w, :]
    # rectangular contour of mask ==> should be resized to original image size
    deformed_mask = deformed_extended_msk[y:y + h, x:x + w]

    # background variable has the same size of the original image
    bakgrand, deformed_image, deformed_mask = make_same_sizes(background, deformed_image, deformed_mask)

    kernel = np.ones((5, 5), np.uint8)
    mask2 = cv2.erode(deformed_mask, kernel)
    mask_3 = np.expand_dims(mask2, axis=2)
    mask4 = mask_3 * np.ones((1, 1, 3))

    background1 = np.copy(bakgrand)
    background2 = np.copy(bakgrand)

    seed = random.randint(0, 1000000000)
    cropped_dst_scratched = do_scratch(deformed_image, 0.85, 1.8, seed, display)
    mask4_scratched = do_scratch(mask4, 0.85, 1.8, seed, display)
    mask4_scratched_bool = np.copy(mask4_scratched)
    mask4_scratched_bool = mask4_scratched_bool > 127
    np.copyto(background1, cropped_dst_scratched, casting='unsafe', where=mask4_scratched_bool)

    mask4 = mask4 > 127
    np.copyto(background2, deformed_image, casting='unsafe', where=mask4)

    if display:
        cv2.imshow("scratched", background1)
        cv2.imshow("No_scratched", background2)
        cv2.imshow("mask_No_scratched", mask2)
        cv2.imshow("mask_scratched", mask4_scratched)
        cv2.waitKey(0)

    return background1, mask4_scratched, background2, mask2

def do_scratch(img, min_prop, max_prop, seed, display=False):
    # this function scratches the input image and return the output with the same size
    if min_prop >= 1:
        raise ValueError("select a value LESS than 1")
    if max_prop <= 1:
        raise ValueError("select a value MORE than 1")

    h1, w1 = img.shape[0:2]
    random.seed(seed)
    w2 = random.randint(int(min_prop * w1), int(max_prop * w1))
    h2 = random.randint(int(min_prop * h1), int(max_prop * h1))
    resized_img = cv2.resize(img, (w2, h2))

    p1 = h1 / w1
    p2 = h2 / w2

    if p1 > p2:
        add_to_length = (w2 * p1) - h2
        resized_img = cv2.copyMakeBorder(resized_img, int(add_to_length / 2), int(add_to_length / 2), 0, 0,
                                         cv2.BORDER_CONSTANT, (0, 0, 0))
        resized_img = cv2.resize(resized_img, (w1, h1))
        assert img.shape == resized_img.shape
        if display:
            cv2.imshow("resized_img", resized_img)
            cv2.waitKey(0)
        return resized_img
    elif p2 > p1:
        add_to_width = (h2 / p1) - w2
        resized_img = cv2.copyMakeBorder(resized_img, 0, 0, int(add_to_width / 2), int(add_to_width / 2),
                                         cv2.BORDER_CONSTANT, (0, 0, 0))
        resized_img = cv2.resize(resized_img, (w1, h1))
        assert img.shape == resized_img.shape

        if display:
            cv2.imshow("resized_img", resized_img)
            cv2.waitKey(0)
        return resized_img
    else:
        resized_img = cv2.resize(resized_img, (w1, h1))
        assert img.shape == resized_img.shape

        if display:
            cv2.imshow("resized_img", resized_img)
            cv2.waitKey(0)
        return resized_img

def write_img(_dir, img_array, img_name):
    os.makedirs(_dir, exist_ok=True)
    if not os.path.isfile(os.path.join(_dir, img_name)):
        cv2.imwrite(os.path.join(_dir, img_name), img_array)
    else:
        print("file exists and did not over-writen")

def do_distorstion(img_array, mask_array, seed, img_name, display=False, save=False):
    # img_name = os.path.basename(dir_to_img)
    img = Image.from_array(img_array)
    msk = Image.from_array(mask_array)

    # with Image(filename=dir_to_img) as img:
    img.virtual_pixel = 'mirror'
    random.seed(seed)
    rand = random.uniform(-0.6, 0.8)

    img.implode(rand)
    if save:
        img.save(filename='{}_imploded_img.jpg'.format(img_name))
    # convert to opencv/numpy array format
    img = np.array(img)
    # image_ = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    # Perform the same operation on mask
    msk.virtual_pixel = 'mirror'
    random.seed(seed)
    rand = random.uniform(-0.6, 0.8)
    msk.implode(rand)
    if save:
        msk.save(filename='{}_imploded_mask.jpg'.format(img_name))
    # convert to opencv/numpy array format
    msk = np.array(msk)
    # mask_ = cv2.cvtColor(msk, cv2.COLOR_RGB2BGR)

    # display result with opencv
    if display:
        cv2.imshow("imploded_Image", img)
        cv2.imshow("imploded_Mask", msk)
        cv2.waitKey(0)

    return img, msk

def transform_one_image():
    source_dir = "../images_for_test"
    dir_to_save_noneID = "../images_for_test/processed"

    img1 = "img_001_1_c4_015857.png"
    maks1 = "mask_001_1_c4_015857.png"
    key1 = "001_1_c4_015857.json"
    img2 = "img_006_1_c4_000137.png"
    maks2 = "mask_006_1_c4_000137.png"
    key2 = "006_1_c4_000137.json"

    img_name = img2
    mask_name = maks2
    key = key2

    for i in range(1, 20, 1):
        dir_ = os.path.join(dir_to_save_noneID, str(i))
        os.makedirs(dir_, exist_ok=True)

        img_arry = load_img(source_dir, img_name)
        orig_mask = load_mask(source_dir, mask_name)
        keypoints = load_keypoints(source_dir, key, img_arry, orig_mask, False)

        background = HiFill_inpainting.HiFill(img_arry, orig_mask, False)
        write_img(dir_, background, "{}_background.jpg".format(img_name))

        head_mask = get_head_mask(orig_mask, keypoints, False)
        write_img(dir_, head_mask, "{}_head_mask.jpg".format(img_name))

        mask_boarder, dilated_mask, eroded_mask = get_mask_boarder(orig_mask, False)
        write_img(dir_, mask_boarder, "{}_boarder_area.jpg".format(img_name))
        write_img(dir_, dilated_mask, "{}_dilated_mask.jpg".format(img_name))
        write_img(dir_, eroded_mask, "{}_eroded_mask.jpg".format(img_name))

        final_mask = get_final_mask(head_mask, mask_boarder, False)
        write_img(dir_, final_mask, "{}_final_mask.jpg".format(img_name))

        HiFill_head_border = HiFill_inpainting.HiFill(img_arry, final_mask, False)
        write_img(dir_, HiFill_head_border, "{}_boarder_head_inpainted.jpg".format(img_name))

        seed = random.randint(1, 10000000000)
        imploded_img, imploded_mask = do_distorstion(HiFill_head_border, orig_mask, seed, img_name, display=False)
        write_img(dir_, imploded_img, "{}_imploded_img.jpg".format(img_name))
        write_img(dir_, imploded_mask, "{}_imploded_mask.jpg".format(img_name))

        img_deformed_scratched, mask_deformed_scratched, img_deformed_NOscratched, mask_deformed_NOscratched = do_deformed_mesh(
            background=background, img=imploded_img, mask=imploded_mask, display=False)  # can be done on mask and img
        write_img(dir_, img_deformed_scratched, "{}_img_deformed_scratched.jpg".format(img_name))
        write_img(dir_, mask_deformed_scratched, "{}_mask_deformed_scratched.jpg".format(img_name))
        write_img(dir_, img_deformed_NOscratched, "{}_img_deformed_NOscratched.jpg".format(img_name))
        write_img(dir_, mask_deformed_NOscratched, "{}_mask_deformed_NOscratched.jpg".format(img_name))

if __name__ == '__main__':
    # _img_name_ = "img_006_1_c4_000137.png"
    # img_array = cv2.imread(os.path.join('../images_for_test', _img_name_), cv2.COLOR_BGR2RGB)
    # img = Image.from_array(img_array)
    # with Image(img) as myimg:
    #     x = img_array.shape[1]
    #     y = img_array.shape[0]
    #     args = (
    #         x / 10, y / 10, x / 15, y / 15,  # Point 1: (10, 10) => (15,  15)
    #         5 * x / 10, 5 * y / 10, 5 * x / 15, 5 * y / 15,  # Point 2: (139, 0) => (100, 20)
    #         9 * x / 10, 9 * y / 10, 9 * x / 15, 9 * y / 15  # Point 3: (0,  92) => (50,  80)
    #     )
    #     myimg.distort('shepards', args)
    #     myimg.save(filename='../images_for_test/affineTransformedImage.png')

    # mask_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_masks"
    # image_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_orig"
    # json_files_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_keypoints"
    # dir_to_save_noneID = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_nonID_Last"

    # mask_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/prcc/prcc_masks/test"
    # image_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/prcc/prcc_orig/test"
    # json_files_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/prcc/prcc_keypoints/test"
    # dir_to_savenoneID = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/prcc/prcc_nonID_new/scratching/test"

    # mask_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/celeb_reid/celebreid_masks"
    # image_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/celeb_reid/celebreid_orig"
    # json_files_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/celeb_reid/celebreid_keypoints"
    # dir_to_save_noneID = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/celeb_reid/celebreid_nonID_new/scratching"

    # paper_fig_mask_dir = mask_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_masks/train"
    # paper_fig_image_dir = image_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/A_PROJECTS/LOCAL/cvpr2021/YLD_YouLookDifferent/images_for_test/images"
    # paper_fig_json_files_dir = json_files_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/LTCC_ReID/LTCC_keypoints/train"
    # paper_fig_dir_to_save_noneID = dir_to_save_noneID = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/A_PROJECTS/LOCAL/cvpr2021/YLD_YouLookDifferent/images_for_test/Transformed_images"

    mask_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/NKUP/NKUP_masks/query"
    image_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/NKUP/NKUP_orig/query"
    json_files_dir = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/NKUP/NKUP_keypoints/query"
    dir_to_save_noneID = "/media/socialab157/2cbae9f1-6394-4fa9-b963-5ef890eee044/B_DATASETS/Long_term_datasets/NKUP/NKUP_nonID/query"

    os.makedirs(dir_to_save_noneID, exist_ok=True)
    two_sub_folders = False
    one_sub_folder = False
    without_sub_folder = True
    One_time = False

    for repetition in range (11):
        if one_sub_folder:
            folder_names = os.listdir(mask_dir)
            for F_index, fld_name in enumerate(folder_names):
                #flag = 0
                images_names = os.listdir(os.path.join(mask_dir, fld_name))
                for index, img_name in enumerate(images_names):

                    if os.path.isfile(os.path.join(dir_to_save_noneID, fld_name, img_name)):
                        print("image {} is existing".format(img_name))
                        continue

                    img_arry = load_img(os.path.join(image_dir, fld_name), img_name)
                    orig_mask = load_mask(os.path.join(mask_dir, fld_name), img_name)
                    keypoints = load_keypoints(os.path.join(json_files_dir, fld_name), img_name, img_arry, orig_mask, False)
                    if keypoints is None or orig_mask is None or img_arry is None:
                        print("(img/mask/keypoint)data did not find: {}".format(img_name))
                        continue

                    background = HiFill_inpainting.HiFill(img_arry, orig_mask, False)
                    head_mask = get_head_mask(orig_mask, keypoints, False)
                    mask_boarder, dilated_mask, eroded_mask = get_mask_boarder(orig_mask, False)
                    final_mask = get_final_mask(head_mask, mask_boarder, False)
                    HiFill_head_border = HiFill_inpainting.HiFill(img_arry, final_mask, False)
                    seed = random.randint(1, 10000000000)
                    imploded_img, imploded_mask = do_distorstion(HiFill_head_border, orig_mask, seed, img_name, display=False)
                    img_deformed_scratched, mask_deformed_scratched, img_deformed_NOscratched, mask_deformed_NOscratched = do_deformed_mesh(
                        background=background, img=imploded_img, mask=imploded_mask,
                        display=False)  # can be done on mask and img

                    write_img(os.path.join(dir_to_save_noneID, fld_name),
                              img_deformed_scratched,
                              "_{}_{}".format(repetition, img_name))

                    if index % 100 == 0:
                        print("Save Generated Images to {}: {}/{}".format(
                            os.path.join(dir_to_save_noneID, fld_name), index, len(images_names)))

        if two_sub_folders:
            parent_fld = os.listdir(mask_dir)
            for p_index, p_fld_name in enumerate(parent_fld):
                folder_names = os.listdir(os.path.join(mask_dir, p_fld_name))
                flag = 0
                for F_index, fld_name in enumerate(folder_names):
                    flag2 = 0
                    images_names = os.listdir(os.path.join(mask_dir, p_fld_name, fld_name))
                    for index, img_name in enumerate(images_names):

                        if os.path.isfile(os.path.join(dir_to_save_noneID, p_fld_name, fld_name, img_name)):
                            print("image {} is existing".format(img_name))
                            continue

                        img_arry = load_img(os.path.join(image_dir, p_fld_name, fld_name), img_name)
                        orig_mask = load_mask(os.path.join(mask_dir, p_fld_name, fld_name), img_name)
                        keypoints = load_keypoints(os.path.join(json_files_dir, p_fld_name, fld_name), img_name, img_arry, orig_mask, False)
                        if keypoints is None or orig_mask is None or img_arry is None:
                            print("(img/mask/keypoint)data did not find: {}".format(img_name))
                            continue

                        background = HiFill_inpainting.HiFill(img_arry, orig_mask, False)
                        head_mask = get_head_mask(orig_mask, keypoints, False)
                        mask_boarder, dilated_mask, eroded_mask = get_mask_boarder(orig_mask, False)
                        final_mask = get_final_mask(head_mask, mask_boarder, False)
                        HiFill_head_border = HiFill_inpainting.HiFill(img_arry, final_mask, False)
                        seed = random.randint(1, 10000000000)
                        imploded_img, imploded_mask = do_distorstion(HiFill_head_border, orig_mask, seed, img_name, display=False)
                        img_deformed_scratched, mask_deformed_scratched, img_deformed_NOscratched, mask_deformed_NOscratched = do_deformed_mesh(
                            background=background, img=imploded_img, mask=imploded_mask,
                            display=False)  # can be done on mask and img

                        write_img(os.path.join(dir_to_save_noneID, p_fld_name, fld_name),
                                  img_deformed_scratched,
                                  "_{}_{}".format(repetition, img_name))

                        if index % 5 == 0:
                            print("Save Generated Images to {}: {}/{}".format(
                                os.path.join(dir_to_save_noneID, p_fld_name, fld_name), index, len(images_names)))

        if without_sub_folder:
            images_names = os.listdir(image_dir)
            for index, img_name in enumerate(images_names):

                if os.path.isfile(os.path.join(dir_to_save_noneID, img_name)):
                    print("image {} is existing".format(img_name))
                    continue

                img_arry = load_img(os.path.join(image_dir), img_name)
                orig_mask = load_mask(os.path.join(mask_dir), img_name)
                keypoints = load_keypoints(os.path.join(json_files_dir), img_name, img_arry, orig_mask, False)
                if keypoints is None or orig_mask is None or img_arry is None:
                    print("(img/mask/keypoint)data did not find: {}".format(img_name))
                    continue

                background = HiFill_inpainting.HiFill(img_arry, orig_mask, False)
                head_mask = get_head_mask(orig_mask, keypoints, False)
                mask_boarder, dilated_mask, eroded_mask = get_mask_boarder(orig_mask, False)
                final_mask = get_final_mask(head_mask, mask_boarder, False)
                HiFill_head_border = HiFill_inpainting.HiFill(img_arry, final_mask, False)
                seed = random.randint(1, 10000000000)
                imploded_img, imploded_mask = do_distorstion(HiFill_head_border, orig_mask, seed, img_name,
                                                             display=False)
                img_deformed_scratched, mask_deformed_scratched, img_deformed_NOscratched, mask_deformed_NOscratched = do_deformed_mesh(
                    background=background, img=imploded_img, mask=imploded_mask,
                    display=False)  # can be done on mask and img

                write_img(dir_to_save_noneID,
                          img_deformed_scratched,
                          "_{}_{}".format(repetition, img_name))

                if index % 100 == 0:
                    print("Save Generated Images to {}: {}/{}".format(
                        os.path.join(dir_to_save_noneID), index, len(images_names)))

        if One_time:
            print("if you want a loop for generating images, set the One_time variable to False.")
            break


text:
import sys

import json
import numpy as np
import logging
import argparse
import os
import time
import numpy as np
import glob

import torch
import torch.nn as nn
import torch.optim as optim
from tensorboardX import SummaryWriter

import src.evaluation as evaluation
from src.cuda import CUDA
import src.data as data
import src.models as models

parser = argparse.ArgumentParser()
parser.add_argument(
    "--config",
    help="path to json config.",
    required=True
)
parser.add_argument(
    "--checkpoint",
    help="path to model checkpoint",
    required=True
)

args = parser.parse_args()
config = json.load(open(args.config, 'r'))

working_dir = config['data']['working_dir']

if not os.path.exists(working_dir):
    os.makedirs(working_dir)

config_path = os.path.join(working_dir, 'config.json')
if not os.path.exists(config_path):
    with open(config_path, 'w') as f:
        json.dump(config, f)

# set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    filename='%s/train_log' % working_dir,
)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

logging.info('Reading data ...')

src, tgt = data.read_nmt_data(
    src=config['data']['src'],
    config=config,
    tgt=config['data']['tgt'],
    attribute_vocab=config['data']['attribute_vocab'],
    ngram_attributes=config['data']['ngram_attributes']
)

src_test, tgt_test = data.read_nmt_data(
    src=config['data']['src_test'],
    config=config,
    tgt=config['data']['tgt_test'],
    attribute_vocab=config['data']['attribute_vocab'],
    ngram_attributes=config['data']['ngram_attributes'],
    train_src=src,
    train_tgt=tgt
)
logging.info('...done!')

src_vocab_size = len(src['tok2id'])
tgt_vocab_size = len(tgt['tok2id'])

torch.manual_seed(config['training']['random_seed'])
np.random.seed(config['training']['random_seed'])

model = models.SeqModel(
    src_vocab_size=src_vocab_size,
    tgt_vocab_size=tgt_vocab_size,
    pad_id_src=src['tok2id']['<pad>'],
    pad_id_tgt=tgt['tok2id']['<pad>'],
    config=config
)

logging.info('MODEL HAS %s params' %  model.count_params())
model, start_epoch = models.attempt_load_model(
    model=model,
    checkpoint_path=args.checkpoint)
if CUDA:
    model = model.cuda()

start = time.time()
model.eval()
dev_loss = evaluation.evaluate_lpp(
        model, src_test, tgt_test, config)

bleu, edit_distance, inputs, preds, golds, auxs = evaluation.inference_metrics(
    model, src_test, tgt_test, config)

with open(working_dir + '/auxs', 'w') as f:
    f.write('\n'.join(auxs) + '\n')
with open(working_dir + '/inputs', 'w') as f:
    f.write('\n'.join(inputs) + '\n')
with open(working_dir + '/preds', 'w') as f:
    f.write('\n'.join(preds) + '\n')
with open(working_dir + '/golds', 'w') as f:
    f.write('\n'.join(golds) + '\n')

logging.info('INFERENCE DONE. Outputs written to %s' %  (working_dir + "/preds"))
logging.info('\tEdit distance: ' + str(edit_distance))
logging.info('\tBLEU: ' + str(bleu))
logging.info('\tLPP: ' + str(dev_loss))


text:
import csv
import cv2
import numpy as np
from sklearn.utils import shuffle
import sklearn

# import driving data from file-------------------------------------------------
lines = []
with open('data1/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        lines.append(line)
images = []
measurements = []
# use left, middle and right camera data here
for line in lines:
    image = cv2.imread(line[0])
    image_l = cv2.imread(line[1])
    image_r = cv2.imread(line[2])
    measurement = float(line[3])
    measurement_l = float(line[3]) + 0.2
    measurement_r = float(line[3]) - 0.2
    images.append(image)
    images.append(image_l)
    images.append(image_r)
    measurements.append(measurement)
    measurements.append(measurement_l)
    measurements.append(measurement_r)

# augment the dataset by flip all images and steering angle
augmented_images, augmented_measurements = [], []
for image, measurement in zip(images, measurements):
    augmented_images.append(image)
    augmented_measurements.append(measurement)
    augmented_images.append(cv2.flip(image,1))
    augmented_measurements.append(measurement*-1.0)

# original images and labels
X_all = np.array(augmented_images)
y_all = np.array(augmented_measurements)
size_all = X_all.size
# shuffle all data
X_all, y_all = shuffle(X_all, y_all)
# use first 90% data as Training data, remaining 10% test
X_train = X_all[:int(size_all * 0.9)]
y_train = y_all[:int(size_all * 0.9)]
X_test = X_all[int(size_all * 0.9):]
y_test = y_all[int(size_all * 0.9):]

# Training model----------------------------------------------------------------
from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.layers.core import Activation, Dropout
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
import matplotlib.pyplot as plt

model = Sequential()
# the probability to drop data is 0.1
drop_prob = 0.1
# normalize x with zero mean 160x320x3
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160,320,3)))
# cropping image output 65x320x3
model.add(Cropping2D(cropping=((70,25),(0,0))))
# NVIDIA CNN architecture
# 2×2 stride and a 5×5 kernel convolutional layers
model.add(Conv2D(24, kernel_size=(5, 5), strides=(2, 2), activation='relu'))
model.add(Dropout(drop_prob))
model.add(Conv2D(36, kernel_size=(5, 5), strides=(2, 2), activation='relu'))
model.add(Dropout(drop_prob))
model.add(Conv2D(48, kernel_size=(5, 5), strides=(2, 2), activation='relu'))
model.add(Dropout(0.1))
# 3×3 kernel convolutional layes
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Dropout(drop_prob))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Dropout(drop_prob))
model.add(Flatten())
# fully connected
model.add(Dense(100))
model.add(Dropout(drop_prob))
model.add(Dense(50))
model.add(Dropout(drop_prob))
model.add(Dense(10))
model.add(Dropout(drop_prob))
model.add(Dense(1))

# compile and fit the model
model.compile(loss='mse', optimizer='adam')
history_object = model.fit(X_train, y_train, validation_split=0.2, shuffle=True, epochs=25, verbose = 1)
model.save('model.h5')
# visualize loss
# print the keys contained in the history object
print(history_object.history.keys())

# plot the training and validation loss for each epoch
plt.plot(history_object.history['loss'])
plt.plot(history_object.history['val_loss'])
plt.title('model mean squared error loss')
plt.ylabel('mean squared error loss')
plt.xlabel('epoch')
plt.legend(['training set', 'validation set'], loc='upper right')
plt.show()

# evaluate model against the test data
# result = model.evaluate(X_test, y_test)
# print('Scalar test loss:', result)


text:
"""Functions related to statistics and uncertainties"""
from __future__ import (division, unicode_literals, absolute_import,
                        print_function)

from scipy.stats import chi2_contingency
import numpy as np

def sample_var_var(std, n):
    """
    The variance of the sample variance of a distribution.

    Assumes the samples are normally distributed.

    From: //math.stackexchange.com/q/72975

    `std`: Distribution's standard deviation
    `n`: Number of samples
    """
    return 2.0 * std ** 4 / (n - 1.0)

def g_test(observed_frequencies):
    return chi2_contingency(observed_frequencies, correction=True,
                            lambda_='log-likelihood')

def p_subset_different(nr_A_sub, nr_A_all, nr_B_sub, nr_B_all):
    contin = np.array([
                       [nr_A_sub, nr_A_all - nr_A_sub],
                       [nr_B_sub, nr_B_all - nr_B_sub],
                      ])
    try:
        test_stat, p, dof, expected = g_test(contin)
    except ValueError:
        p = np.nan
    return p

def p_subset_different_row(row, sub_A_col, sub_B_col, all_A_col, all_B_col):
    return p_subset_different(row[sub_A_col], row[all_A_col],
                              row[sub_B_col], row[all_B_col])

def weighted_covariance(x, y, w):
    """Weighted Covariance"""
    return np.sum(w *
                  (x - np.average(x, weights=w)) *
                  (y - np.average(y, weights=w))) / np.sum(w)

def weighted_correlation(x, y, w):
    """Weighted Correlation"""
    return (weighted_covariance(x, y, w) /
            np.sqrt(weighted_covariance(x, x, w) *
                    weighted_covariance(y, y, w)))

def normalize(v):
    return (v - v.mean()) / v.std()

def bootstrap_statistic(v, stat_func, n_samples=100, random_state=None):
    if random_state is None:
        random_state = np.random.RandomState()
    stats = []
    for i in range(n_samples):
        v_ = v.sample(frac=1.0, replace=True, random_state=random_state)
        stats.append(stat_func(v_))
    return stats

def bootstrap_percentile_err(v, percentile, stat_func, offsets=False,
                             *args, **kwargs):
    if len(v) == 0:
        return np.nan
    stats = bootstrap_statistic(v, stat_func, *args, **kwargs)
    values = np.percentile(stats, percentile)
    if offsets:
        values -= stat_func(v)
    return values

max_stars_repo_name: eddiejessup/ciabatta

max_issues_repo_name: eddiejessup/ciabatta

max_forks_repo_name: eddiejessup/ciabatta


text:
"""Script to run the espresso shot management device.

The script builds and uploads the Arduino sketch to the device, then starts
listening to serial communication on the upload port and records measurement
series to JSON files.

Example usage:

    $ python espresso-shot.py --fqbn <FQBN> -p <UPLOAD PORT>

The space key toggles between saving measurement series to disk and simply
displaying data on screen.
"""
import argparse
import collections
import curses
import datetime
import functools
import json
import struct
import time

import numpy as np
import serial

import utils

def main_loop(stdscr, port, simulate):
  """Runs the main loop.

  Args:
    stdscr: curses window object.
    port: str, upload port.
    simulate: bool, whether to simulate a connected device.
  """
  serial_class = utils.MockSerial if simulate else serial.Serial
  serial_port = serial_class(port=port, baudrate=9600)

  # We average temperatures over the previous 100 measurements.
  basket_temperatures = collections.deque(maxlen=100)
  group_temperatures = collections.deque(maxlen=100)

  curses.curs_set(0)
  stdscr.nodelay(True)
  stdscr.clear()

  record_mode = False

  while True:
    # Read serial one measurement at a time.
    measurement = utils.read_measurement(serial_port)
    elapsed_time = measurement[0]
    basket_temperature, group_temperature, state = measurement[-3:]

    basket_temperatures.append(basket_temperature)
    group_temperatures.append(group_temperature)

    # The space key toggles the recording mode.
    try:
      key = stdscr.getkey()
    except:
      key = None
    if key == ' ':
      record_mode = not record_mode

    # Update the terminal display
    stdscr.clear()
    num_rows, num_cols = stdscr.getmaxyx()
    section_width = num_cols // 4

    stdscr.addstr(0, 0, 'Elapsed time', curses.A_BOLD)
    stdscr.addstr(1, 0, '{:.2f}'.format(elapsed_time))

    stdscr.addstr(0, section_width, 'Group temperature', curses.A_BOLD)
    mean = np.mean(group_temperatures)
    stdscr.addstr(1, section_width,
                  ('{:.3f}C'.format(mean) if mean > -273.0 else '---C'))

    stdscr.addstr(0, 2 * section_width, 'Basket temperature', curses.A_BOLD)
    mean = np.mean(basket_temperatures)
    stdscr.addstr(1, 2 * section_width,
                  ('{:.3f}C'.format(mean) if mean > -273.0 else '---C'))

    stdscr.addstr(0, 3 * section_width, 'State', curses.A_BOLD)
    stdscr.addstr(1, 3 * section_width, str(utils.State(state)))

    record_mode_string = 'Recording' if record_mode else 'Not recording'
    stdscr.addstr(num_rows - 1, num_cols - len(record_mode_string) - 1,
                  record_mode_string)
    stdscr.refresh()

    # A new measurement series begins with the state "START".
    if state == utils.State.START:
      # We will write the measurement series to a JSON file with the current
      # date and time as its name.
      file_path = 'data/{}.json'.format(''.join(
          datetime.datetime.now().isoformat('-', timespec='seconds').split(':')
      ))
      # The shot data to be serialized to JSON.
      shot_data = {
        'posix time': time.time(),
        'description': "",
        'time': [],
        'basket_temperature': [],
        'group_temperature': [],
      }
    # A measurement series ends with the state "STOP".
    elif state == utils.State.STOP:
      # When the measurement series ends, we serialize it to a JSON file.
      if record_mode and not simulate:
        with open(file_path, 'w') as f:
          json.dump(shot_data, f)
    # When running, we record shot data.
    elif state == utils.State.RUNNING:
      shot_data['time'].append(elapsed_time)
      shot_data['basket_temperature'].append(basket_temperature)
      shot_data['group_temperature'].append(group_temperature)

if __name__ == '__main__':
  parser = argparse.ArgumentParser(
      description='Run the espresso shot management device.')
  parser.add_argument(
      '--fqbn', type=str, default='arduino:mbed:nano33ble',
      help='Fully Qualified Board Name.')
  parser.add_argument(
      '-p', dest='port', type=str, default=None,
      help='Upload port, e.g.: COM10 or /dev/ttyACM0')
  parser.add_argument(
      '--recompile', action='store_true',
      help='Recompile the program and upload it to the Arduino device.')
  parser.add_argument(
      '--simulate', action='store_true',
      help='Simulate a connected Arduino device.')
  args = parser.parse_args()

  fqbn = args.fqbn
  recompile = args.recompile
  simulate = args.simulate
  port = None if simulate else utils.find_port_if_not_specified(fqbn, args.port)

  if recompile and not simulate:
    utils.compile_and_upload(fqbn=fqbn, port=port)
    # Give the Arduino device some time to become operational.
    time.sleep(2.0)

  curses.wrapper(functools.partial(
      main_loop,
      port=port,
      simulate=simulate,
  ))

max_stars_repo_name: vdumoulin/espresso-shot

max_issues_repo_name: vdumoulin/espresso-shot

max_forks_repo_name: vdumoulin/espresso-shot


text:
import numpy as np
import processer
from collections import deque
import sys

file_name = sys.argv[1]
max_count = -1
if len(sys.argv) >= 3:
   max_count = int(sys.argv[2])

input_file = '' + file_name + '.mp4'
output_file = 'out_' + file_name + '.avi'
emphasis_factor = 1.0
threshold = 2.0
neighbour_threshold = 15
average_threshold = 3.0

frames_to_save = 30
saved_frames_frequency = 3
frames_to_skip = 300

processer = processer.Processer(input_file, output_file, max_count=max_count)
width = processer.width
height = processer.height

last_frames = deque()
running_total = np.full((width, height, 3), 0)

def scale_by_factor(image):
    return image * emphasis_factor

def to_pixel_form(image):
    image = np.uint32(image)
    return (image[:,:,0] + image[:,:,1] + image[:,:,2]) / 3

def from_pixel_form(image):
    image = np.minimum(image, 255)
    image = np.repeat(image, 3)
    image = np.resize(image, (width, height, 3))
    image = np.uint8(image)

    return image

def get_sum_of_neighbours(image):
    bot = np.pad(image[1:,:], ((0, 1), (0, 0)), constant_values=(0))
    top = np.pad(image[:-1,:], ((1, 0), (0, 0)), constant_values=(0))
    right = np.pad(image[:,1:], ((0, 0), (0, 1)), constant_values=(0))
    left = np.pad(image[:,:-1], ((0, 0), (1, 0)), constant_values=(0))
    return bot + top + right + left

def filter_by_neighbours(image, threshold=None):
    if threshold is None:
        threshold = neighbour_threshold
    neighbours = get_sum_of_neighbours(image)
    return (neighbours >= threshold) * image

with open(file_name + '_total.npy', 'rb') as f:
    total = np.load(f, allow_pickle=True)
    total = np.resize(total, (width, height, 3))
    total = to_pixel_form(total)

average_total = np.mean(total)
total = total * (total >= (average_total * average_threshold))

def apply_threshold(image):
    return (image >= threshold) * image

def filter_out_by_total(image):
    image = image * (total == 0)
    return image

last_frame = np.full((width, height), 0)
def filter_by_last_empty(image):
    global last_frame
    temp_frame = image * (last_frame > 0.0)
    last_frame = image
    image = temp_frame
    return image

def filter_by_non_empty_neighbours(image):
    neigh = get_sum_of_neighbours(image)
    image = image * (neigh > 0.1)
    return image

def calculate_frame(image, count, width, height):
    global last_frames, running_total

    original_image = np.int32(image)

    should_save_frame = count >= frames_to_skip and count % saved_frames_frequency == 0

    if len(last_frames) >= frames_to_save:
        average_frame = running_total / frames_to_save
        image = np.absolute(np.subtract(original_image, average_frame))

        if should_save_frame:
            first_frame = last_frames.popleft()
            running_total = np.subtract(running_total, first_frame)
    else:
        image = np.full((width, height, 3), 0)

    if should_save_frame:
        last_frames.append(original_image)
        running_total = np.add(running_total, original_image)

    image = to_pixel_form(image)

    image = filter_out_by_total(image)
    image = apply_threshold(image)

    image = filter_by_neighbours(image)
    image = filter_by_last_empty(image)
    image = image * (get_sum_of_neighbours(image) > 0.0)

    for _ in range(5):
        image = image / 3 + get_sum_of_neighbours(image) / 6

    image = scale_by_factor(image)

    image = from_pixel_form(image)

    return image

if __name__ == "__main__":
    processer.run(calculate_frame)