text:
import numpy as np
import scipy.misc
import h5py
import os

from px2graph.data.genome.driver import local as vg

data_dir = os.path.dirname(__file__)
class_labels, relationships = [], []
with open(data_dir + '/sorted_objects.txt', 'r') as f:
    for line in f: class_labels += [line[:-1]]
with open(data_dir + '/sorted_predicates.txt', 'r') as f:
    for line in f: relationships += [line[:-1]]

# Flip reference - this is used to swap potential relationships that would be
# affected by flipping an image (e.g. on left of) but this isn't necessary
# with the current set of predicates that we are training on.
rel_flip_ref = np.arange(len(relationships))

# Load image info
img_info = vg.GetAllImageData()
# Remove images that are corrupted? according to scene-graph-TF-release
bad_idxs = [4616, 4615, 1721, 1591]
for idx in bad_idxs: del img_info[idx]

max_objs = 50
max_rels = 50           # (arbitrary) max number of rels for one object
max_total_rels = 200    # max total number of rels in single example
num_cats = len(class_labels)
num_rels = len(relationships)
num_examples = len(img_info)

# Load preprocessed data
with h5py.File(data_dir + '/VG-SGG.h5','r') as f:
    bb1024 = f['boxes_1024'][:]
    obj_idxs = [f['img_to_first_box'][:], f['img_to_last_box'][:]]
    rel_idxs = [f['img_to_first_rel'][:], f['img_to_last_rel'][:]]
    obj_labels = f['labels'][:]
    rel_labels = f['predicates'][:]
    rel_sbj_obj = f['relationships'][:]
    train_val_split = f['split'][:]

# Convert from center, width, height to x1, y1, x2, y2
bb1024[:,:2] = bb1024[:,:2] - bb1024[:,2:] / 2
bb1024[:,2:] = bb1024[:,:2] + bb1024[:,2:]

# RPN proposal info (only loaded if opt.use_rpn is set)
im_scales, im_to_roi_idx, num_rois, rpn_rois, rpn_scores = [None]*5

def setup_val_split(opt):
    ref_idxs = np.arange(num_examples)

    valid_idxs = np.loadtxt(data_dir + '/valid_ids.txt', dtype=int)
    valid_mask = np.ones(num_examples, bool)
    valid_mask[valid_idxs] = 0

    train_idxs = ref_idxs[(train_val_split == 0) * valid_mask]
    test_idxs = ref_idxs[train_val_split == 2]

    return train_idxs, valid_idxs, test_idxs

def initialize(opt):
    global im_scales, im_to_roi_idx, num_rois, rpn_rois, rpn_scores
    opt.idx_ref = {}
    opt.idx_ref['train'], opt.idx_ref['valid'], opt.idx_ref['test'] = setup_val_split(opt)

    if opt.use_rpn:
        print("Loading region proposals")
        with h5py.File(data_dir + '/proposals.h5', 'r') as roi:
            im_scales = roi['im_scales'][:]
            im_to_roi_idx = roi['im_to_roi_idx'][:]
            num_rois = roi['num_rois'][:]
            rpn_rois = roi['rpn_rois'][:]
            rpn_scores = roi['rpn_scores'][:]

def get_id(idx):
    return img_info[idx].id

def load_image(idx):
    return scipy.misc.imread(data_dir + '/VG_100K/%d.jpg' % get_id(idx), mode='RGB')

def image_dims(idx):
    return img_info[idx].height, img_info[idx].width

def get_rpn_proposals(idx):
    im_h, im_w = image_dims(idx)
    tmp_idx = im_to_roi_idx[idx]
    tmp_rois = rpn_rois[tmp_idx:tmp_idx+num_rois[idx]] * max(im_w, im_h) / 1024
    tmp_scores = rpn_scores[tmp_idx:tmp_idx+num_rois[idx]]
    return tmp_rois, tmp_scores

def get_graph(idx):
    sg = {'obj_class':[], 'obj_bbox':None, 'rel_class':[], 'rel_sbj':None, 'rel_obj':None}
    o_idx = [obj_idxs[0][idx], obj_idxs[1][idx] + 1]
    r_idx = [rel_idxs[0][idx], rel_idxs[1][idx] + 1]
    im_h, im_w = image_dims(idx)
    if o_idx[0] != -1:
        sg['obj_class'] = (obj_labels[o_idx[0]:o_idx[1]] - 1).flatten().tolist()
        sg['obj_bbox'] = bb1024[o_idx[0]:o_idx[1]].reshape((-1,2,2))
        sg['obj_bbox'] = sg['obj_bbox'] * max(im_w, im_h) / 1024
    if r_idx[0] != -1:
        sg['rel_class'] = (rel_labels[r_idx[0]:r_idx[1]] - 1).flatten().tolist()
        tmp_so = rel_sbj_obj[r_idx[0]:r_idx[1]]
        sg['rel_sbj'] = tmp_so[:,0] - o_idx[0]
        sg['rel_obj'] = tmp_so[:,1] - o_idx[0]
    return sg

def preload_sample_info(idx):
    return get_graph(idx)

def num_objects(idx, sg=None):
    if sg is None: sg = get_graph(idx)
    return len(sg['obj_class'])

def get_obj_classes(idx, sg=None):
    if sg is None: sg = get_graph(idx)
    return sg['obj_class']

def get_bboxes(idx, sg=None):
    # Return num_objs x 2 x 2 tensor
    # objs x (upper left, bottom right) x (x, y)
    if sg is None: sg = get_graph(idx)
    if num_objects(idx, sg) > 0: return sg['obj_bbox']
    else: np.zeros((1,2,2))

def get_rels(idx, sg=None):
    if sg is None: sg = get_graph(idx)
    num_objs = num_objects(idx, sg)
    if num_objs == 0: return []
    # Loop through and get tuples
    total_rel_count = 0
    all_rels = [[] for _ in range(num_objs)]
    for i,p_idx in enumerate(sg['rel_class']):
        s_idx = sg['rel_sbj'][i]
        o_idx = sg['rel_obj'][i]
        if (not [o_idx, p_idx] in all_rels[s_idx]
            and len(all_rels[s_idx]) < max_rels
            and total_rel_count < max_total_rels):
            all_rels[s_idx] += [[o_idx, p_idx]]
            total_rel_count += 1
    return all_rels

def get_gt_triplets(idx, sg=None):
    if sg is None: sg = get_graph(idx)
    obj_bboxes = get_bboxes(idx, sg).reshape((-1,4))
    obj_classes = get_obj_classes(idx, sg)
    num_rels = len(sg['rel_class'])
    gt_triplets = np.zeros((num_rels,3))
    gt_triplet_boxes = np.zeros((num_rels,8))

    for i in range(num_rels):
        s_, o_ = sg['rel_sbj'][i], sg['rel_obj'][i]
        gt_triplets[i] = [obj_classes[s_], sg['rel_class'][i], obj_classes[o_]]
        gt_triplet_boxes[i,:4] = obj_bboxes[s_]
        gt_triplet_boxes[i,4:] = obj_bboxes[o_]

    return gt_triplets, gt_triplet_boxes


text:
import torch
from PIL import Image

from torchvision import transforms
from .pose_resnet import *
from operator import itemgetter
import copy
import matplotlib.pyplot as plt
import cv2
import numpy as np
import time

get_detached = lambda x: copy.deepcopy(x.cpu().detach().numpy())
get_keypoints = lambda pose_layers: map(itemgetter(1, 3), [cv2.minMaxLoc(pose_layer) for pose_layer in pose_layers])

JOINTS = ['r-ankle', 'r-knee', 'r-hip', 'l-hip',
        'l-knee', 'l-ankle', 'pelvis', 'thorax',
        'upper-neck', 'head-top', 'r-wrist', 'r-elbow',
        'r-shoulder', 'l-shoulder', 'l-elbow', 'l-wrist']

POSE_PAIRS = [
# UPPER BODY
            [9, 8],
            [8, 7],
            [7, 3],
            [7, 2],
# LOWER BODY
            [6, 2],
            [2, 1],
            [1, 0],

            [6, 3],
            [3, 4],
            [4, 5],
# ARMS
            [8, 12],
            [12, 11],
            [11, 10],
            [8, 13],
            [13, 14],
            [14, 15]
]

POSE_PAIRS_COL = [
# UPPER BODY
    (65,190,115), # Green
    (50,55,235), # Red
    (110,230,255), # Yellow
    (195,125,40), # Blue
# LOWER BODY
    (180,75,160), # Purple
    (255,225,110), # Cyan
    (65,190,115), # Green

    (180,75,160), # Purple
    (50,55,240), # Red
    (195,125,40), # Blue
# ARMS
    (180,75,160), # Purple
    (110,230,255), # Yellow
    (195,125,40), # Blue
    (255,225,110), # Cyan
    (50,55,240), # Red
    (65,190,115) # Green
]

class HPEInference():
    """ Docstring
    """

    def __init__(self,cfg):

        self.model = get_pose_net(cfg, is_train=False)
        self.model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE, map_location=torch.device('cpu')))
        self.IMAGE_SIZE = cfg.MODEL.IMAGE_SIZE

        self.OUT_WIDTH,self.OUT_HEIGHT = cfg.MODEL.EXTRA.HEATMAP_SIZE

        self.transform = transforms.Compose([
            transforms.Resize(self.IMAGE_SIZE),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
            ])

    def gen_output(self,img):
        tr_img = self.transform(img)
        output = self.model(tr_img.unsqueeze(0))
        output = output.squeeze(0)
        _, OUT_HEIGHT, OUT_WIDTH = output.shape
        print(output.shape)

        return output

    def heat_map(self,img):
        since = time.time()
        plt.figure(figsize=(15, 15))

        output = self.gen_output(img)

        for idx, pose_layer in enumerate(get_detached(output)):
            # print(pose_layer.shape)
            plt.subplot(4, 4, idx + 1)
            plt.title(f'{idx} - {JOINTS[idx]}')
            plt.imshow(img.resize((self.OUT_WIDTH, self.OUT_HEIGHT)), cmap='gray', interpolation='bicubic')
            plt.imshow(pose_layer, alpha=0.5, cmap='jet', interpolation='bicubic')
            plt.axis('off')
        plt.show()

        time_elapsed = time.time() - since
        print('Inference complete in {:4.2f}ms'.format(
        time_elapsed*1000))

    def vis_pose(self,img,threshold = 0.5):
        since = time.time()

        output = self.gen_output(img)

        THRESHOLD = threshold

        image_p = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)

        OUT_SHAPE = (self.OUT_HEIGHT, self.OUT_WIDTH)
        IMG_HEIGHT, IMG_WIDTH, _ = image_p.shape

        scale_x = IMG_WIDTH / OUT_SHAPE[0]
        scale_y = IMG_HEIGHT / OUT_SHAPE[1]

        pose_layers = get_detached(x=output)
        key_points = list(get_keypoints(pose_layers=pose_layers))
        key_points = [(thres,(int(x*scale_x),int(y*scale_y))) for thres,(x,y) in key_points]

        i=0
        for from_j, to_j in POSE_PAIRS:
            from_thr, (from_x_j, from_y_j) = key_points[from_j]
            to_thr, (to_x_j, to_y_j) = key_points[to_j]

            if from_thr > THRESHOLD and to_thr > THRESHOLD:
                # this is a joint connection, plot a line
                cv2.line(image_p, (from_x_j, from_y_j), (to_x_j, to_y_j), POSE_PAIRS_COL[i], 3)
            i+=1

        for thres,(x,y) in key_points:
            if thres > THRESHOLD:
                # this is a joint
                cv2.ellipse(image_p, (x, y), (5, 5), 0, 0, 360, (255, 255, 255), cv2.FILLED)

        time_elapsed = time.time() - since
        print('Inference complete in {:4.2f}ms'.format(
        time_elapsed*1000))

        return Image.fromarray(cv2.cvtColor(image_p, cv2.COLOR_RGB2BGR))

    def export_onnx_model(self, model_name = "simple_pose_estimation.onnx",quantization = False):
        torch_model = copy.deepcopy(self.model)
        batch_size = 1
        rand_inp = torch.randn(batch_size, 3, *self.IMAGE_SIZE, requires_grad=True)

        # Export the model
        torch.onnx.export(torch_model,               # model being run
                        rand_inp,                    # model input (or a tuple for multiple inputs)
                        model_name,   # where to save the model (can be a file or file-like object)
                        export_params=True,        # store the trained parameter weights inside the model file
                        opset_version=10,          # the ONNX version to export the model to
                        do_constant_folding=True,  # whether to execute constant folding for optimization
                        input_names = ['input'],   # the model's input names
                        output_names = ['output'], # the model's output names
                        dynamic_axes={'input' : {0 : 'batch_size'},    # variable lenght axes
                                      'output' : {0 : 'batch_size'}})

        if quantization:
            import onnx
            from onnxruntime.quantization import quantize

            onnx_model = onnx.load(model_name)
            quantized_model = quantize(onnx_model)
            onnx.save(quantized_model, model_name.replace(".onnx",".8bit_quantized.onnx"))

ext: py

reason: import numpy


text:
"""
AIA Carrington map
==================
"""
###############################################################################
# Import required functions
import astropy.units as u
import matplotlib.pyplot as plt
import sunpy.map

from solarsynoptic.data import aia_start_of_day_map
from solarsynoptic.reprojection import reproject_carrington
from solarsynoptic.reprojection.database import DATABASE

###############################################################################
# Define the output of the shape map. This is number of pixels in latitude and
# longitude.
shape_out = [180, 360]
###############################################################################
# Fetch a single AIA map. This will download the first map observed on the
# given day.
# map_in = aia_start_of_day_map('2020-01-01', 193 * u.Angstrom)
map_in = sunpy.map.Map('/Users/dstansby/sunpy/data/aia_lev1_193a_2020_01_01t00_00_04_84z_image_lev1.fits')
map_out = reproject_carrington(map_in, [180, 360], cache=True)
print(map_out)

max_stars_repo_name: dstansby/solarsynoptic

max_issues_repo_name: dstansby/solarsynoptic

max_forks_repo_name: dstansby/solarsynoptic

reason: import astropy


text:
from typing import List, Optional
import numpy as np
from math import cos, radians, sin, sqrt
from util import gamma, normalize, Ray, T, zero3f

class Transform:

    m: np.ndarray
    minv: np.ndarray

    def __init__(self, m: np.ndarray, minv: np.ndarray):
        self.m = m
        self.minv = minv

    def __applyTo3f__(self, p: np.ndarray, pError: Optional[List[np.ndarray]] = None) -> np.ndarray:
        m: np.ndarray = self.m
        x: float = p[0]
        y: float = p[1]
        z: float = p[2]
        xp: float = m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3]
        yp: float = m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3]
        zp: float = m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3]
        wp: float = m[3][0] * x + m[3][1] * y + m[3][2] * z + m[3][3]

        if pError:
            xAbsSum: float = (abs(m[0][0] * x) + abs(m[0][1] * y) +
                              abs(m[0][2] * z) + abs(m[0][3]))
            yAbsSum: float = (abs(m[1][0] * x) + abs(m[1][1] * y) +
                              abs(m[1][2] * z) + abs(m[1][3]))
            zAbsSum: float = (abs(m[2][0] * x) + abs(m[2][1] * y) +
                              abs(m[2][2] * z) + abs(m[2][3]))

            pError[0] = gamma(3) * np.array([xAbsSum, yAbsSum, zAbsSum])

        if wp == 1:
            return np.array([xp, yp, zp])
        else:
            return np.array([xp, yp, zp]) / wp

    def __applyToRay__(self, r: Ray) -> Ray:
        oError: List[np.ndarray] = [zero3f()]
        o: np.ndarray = self(r.o, oError)
        d: np.ndarray = self(r.d)

        lengthSquared: float = np.inner(d, d)
        tMax: float = r.tMax
        if lengthSquared > 0:
            dt: float = np.dot(np.abs(d), oError[0]) / lengthSquared
            o += d * dt
            tMax -= dt
        return Ray(o, d, tMax, r.time, r.medium)

    def __call__(self, x: T, pError: Optional[List[np.ndarray]] = None) -> T:
        if isinstance(x, Ray):
            return self.__applyToRay__(x)
        return self.__applyTo3f__(x, pError)

def Inverse(x: Transform) -> Transform:
    return Transform(x.minv, x.m)

def CoordinateSystem(v1: np.ndarray, v2: List[np.ndarray], v3: List[np.ndarray]):
    if abs(v1[0]) > abs(v1[1]):
        v2[0] = np.array([-v1[2], 0, v1[0]]) / \
            np.sqrt(v1[0] * v1[0] + v1[2] * v1[2])
    else:
        v2[0] = np.array([0, v1[2], -v1[1]]) / \
            sqrt(v1[0] * v1[0] + v1[2] * v1[2])
    v3[0] = np.cross(v1, v2[0])

def LookAt(pos: np.ndarray, look: np.ndarray, up: np.ndarray) -> Transform:
    cameraToWorld: np.ndarray = np.identity(4)
    cameraToWorld[0][3] = pos[0]
    cameraToWorld[1][3] = pos[1]
    cameraToWorld[2][3] = pos[2]
    cameraToWorld[3][3] = 1.
    dir: np.ndarray = normalize(look - pos)
    right: np.ndarray = normalize(np.cross(normalize(up), dir))
    newUp: np.ndarray = np.cross(dir, right)
    cameraToWorld[0][0] = right[0]
    cameraToWorld[1][0] = right[1]
    cameraToWorld[2][0] = right[2]
    cameraToWorld[3][0] = 0.
    cameraToWorld[0][1] = newUp[0]
    cameraToWorld[1][1] = newUp[1]
    cameraToWorld[2][1] = newUp[2]
    cameraToWorld[3][1] = 0.
    cameraToWorld[0][2] = dir[0]
    cameraToWorld[1][2] = dir[1]
    cameraToWorld[2][2] = dir[2]
    cameraToWorld[3][2] = 0.

    return Transform(np.linalg.inv(cameraToWorld), cameraToWorld)

def RotateZ(theta: float) -> Transform:
    sinTheta: float = sin(radians(theta))
    cosTheta: float = cos(radians(theta))
    m: np.ndarray = np.array([[cosTheta, -sinTheta, 0., 0.],
                              [sinTheta, cosTheta, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
    return Transform(m, m.transpose())

def Rotate(theta: float, axis: np.ndarray) -> Transform:
    a: np.ndarray = normalize(axis)
    sinTheta: float = sin(radians(theta))
    cosTheta: float = cos(radians(theta))
    m: np.ndarray = np.identity(4)

    # Compute rotation of first basis vector
    m[0][0] = a[0] * a[0] + (1. - a[0] * a[0]) * cosTheta
    m[0][1] = a[0] * a[1] + (1. - cosTheta) - a[2] * sinTheta
    m[0][2] = a[0] * a[2] + (1. - cosTheta) + a[1] * sinTheta

    # Compute rotations of second and third basis vectors
    m[1][0] = a[0] * a[1] + (1. - cosTheta) + a[2] * sinTheta
    m[1][1] = a[1] * a[1] + (1. - a[1] * a[1]) * cosTheta
    m[1][2] = a[1] * a[2] + (1. - cosTheta) - a[0] * sinTheta

    m[2][0] = a[0] * a[2] + (1. - cosTheta) - a[1] * sinTheta
    m[2][1] = a[1] * a[2] + (1. - cosTheta) + a[0] * sinTheta
    m[2][2] = a[2] * a[2] + (1. - a[2] * a[2]) * cosTheta

    return Transform(m, m.transpose())

max_stars_repo_name: rlefkowitz/ellcurverefbuilder

max_issues_repo_name: rlefkowitz/ellcurverefbuilder

max_forks_repo_name: rlefkowitz/ellcurverefbuilder


text:
from keras.preprocessing import image
import warnings
warnings.filterwarnings("ignore")
import time
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
import json
import cv2
from keras import backend as K
import keras
import tensorflow as tf

#from basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
#from extendedmodels import Age, Gender, Race, Emotion
#from commons import functions, realtime, distance as dst

from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
from deepface.extendedmodels import Age, Gender, Race, Emotion
from deepface.commons import functions, realtime, distance as dst

def verify(img1_path, img2_path=''
	, model_name ='VGG-Face', distance_metric = 'cosine', model = None):

	tic = time.time()

	if type(img1_path) == list:
		bulkProcess = True
		img_list = img1_path.copy()
	else:
		bulkProcess = False
		img_list = [[img1_path, img2_path]]

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

	if model == None:
		if model_name == 'VGG-Face':
			print("Using VGG-Face model backend and", distance_metric,"distance.")
			model = VGGFace.loadModel()

		elif model_name == 'OpenFace':
			print("Using OpenFace model backend", distance_metric,"distance.")
			model = OpenFace.loadModel()

		elif model_name == 'Facenet':
			print("Using Facenet model backend", distance_metric,"distance.")
			model = Facenet.loadModel()

		elif model_name == 'DeepFace':
			print("Using FB DeepFace model backend", distance_metric,"distance.")
			model = FbDeepFace.loadModel()

		else:
			raise ValueError("Invalid model_name passed - ", model_name)
	else: #model != None
		print("Already built model is passed")

	#------------------------------
	#face recognition models have different size of inputs
	input_shape = model.layers[0].input_shape[1:3]

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

	#tuned thresholds for model and metric pair
	threshold = functions.findThreshold(model_name, distance_metric)

	#------------------------------
	resp_objects = []
	for instance in img_list:
		if type(instance) == list and len(instance) >= 2:
			img1_path = instance[0]
			img2_path = instance[1]

			#----------------------
			#crop and align faces

			img1 = functions.detectFace(img1_path, input_shape)
			img2 = functions.detectFace(img2_path, input_shape)

			#----------------------
			#find embeddings

			img1_representation = model.predict(img1)[0,:]
			img2_representation = model.predict(img2)[0,:]

			#----------------------
			#find distances between embeddings

			if distance_metric == 'cosine':
				distance = dst.findCosineDistance(img1_representation, img2_representation)
			elif distance_metric == 'euclidean':
				distance = dst.findEuclideanDistance(img1_representation, img2_representation)
			elif distance_metric == 'euclidean_l2':
				distance = dst.findEuclideanDistance(dst.l2_normalize(img1_representation), dst.l2_normalize(img2_representation))
			else:
				raise ValueError("Invalid distance_metric passed - ", distance_metric)

			#----------------------
			#decision

			if distance <= threshold:
				identified =  "true"
			else:
				identified =  "false"

			#----------------------
			#response object

			resp_obj = "{"
			resp_obj += "\"verified\": "+identified
			resp_obj += ", \"distance\": "+str(distance)
			resp_obj += ", \"max_threshold_to_verify\": "+str(threshold)
			resp_obj += ", \"model\": \""+model_name+"\""
			resp_obj += ", \"similarity_metric\": \""+distance_metric+"\""
			resp_obj += "}"

			resp_obj = json.loads(resp_obj) #string to json

			if bulkProcess == True:
				resp_objects.append(resp_obj)
			else:
				#K.clear_session()
				return resp_obj
			#----------------------

		else:
			raise ValueError("Invalid arguments passed to verify function: ", instance)

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

	toc = time.time()

	#print("identification lasts ",toc-tic," seconds")

	if bulkProcess == True:
		resp_obj = "{"

		for i in range(0, len(resp_objects)):
			resp_item = json.dumps(resp_objects[i])

			if i > 0:
				resp_obj += ", "

			resp_obj += "\"pair_"+str(i+1)+"\": "+resp_item
		resp_obj += "}"
		resp_obj = json.loads(resp_obj)
		return resp_obj
		#return resp_objects

def analyze(img_path, actions = [], models = {}):

	if type(img_path) == list:
		img_paths = img_path.copy()
		bulkProcess = True
	else:
		img_paths = [img_path]
		bulkProcess = False

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

	#if a specific target is not passed, then find them all
	if len(actions) == 0:
		actions= ['emotion', 'age', 'gender', 'race']

	print("Actions to do: ", actions)

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

	if 'emotion' in actions:
		if 'emotion' in models:
			print("already built emotion model is passed")
			emotion_model = models['emotion']
		else:
			emotion_model = Emotion.loadModel()

	if 'age' in actions:
		if 'age' in models:
			print("already built age model is passed")
			age_model = models['age']
		else:
			age_model = Age.loadModel()

	if 'gender' in actions:
		if 'gender' in models:
			print("already built gender model is passed")
			gender_model = models['gender']
		else:
			gender_model = Gender.loadModel()

	if 'race' in actions:
		if 'race' in models:
			print("already built race model is passed")
			race_model = models['race']
		else:
			race_model = Race.loadModel()
	#---------------------------------

	resp_objects = []
	for img_path in img_paths:

		resp_obj = "{"

		#TO-DO: do this in parallel

		pbar = tqdm(range(0,len(actions)), desc='Finding actions')

		action_idx = 0
		img_224 = None # Set to prevent re-detection
		#for action in actions:
		for index in pbar:
			action = actions[index]
			pbar.set_description("Action: %s" % (action))

			if action_idx > 0:
				resp_obj += ", "

			if action == 'emotion':
				emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
				img = functions.detectFace(img_path, (48, 48), True)

				emotion_predictions = emotion_model.predict(img)[0,:]

				sum_of_predictions = emotion_predictions.sum()

				emotion_obj = "\"emotion\": {"
				for i in range(0, len(emotion_labels)):
					emotion_label = emotion_labels[i]
					emotion_prediction = 100 * emotion_predictions[i] / sum_of_predictions

					if i > 0: emotion_obj += ", "

					emotion_obj += "\"%s\": %s" % (emotion_label, emotion_prediction)

				emotion_obj += "}"

				emotion_obj += ", \"dominant_emotion\": \"%s\"" % (emotion_labels[np.argmax(emotion_predictions)])

				resp_obj += emotion_obj

			elif action == 'age':
				if img_224 is None:
					img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images
				#print("age prediction")
				age_predictions = age_model.predict(img_224)[0,:]
				apparent_age = Age.findApparentAge(age_predictions)

				resp_obj += "\"age\": %s" % (apparent_age)

			elif action == 'gender':
				if img_224 is None:
					img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images
				#print("gender prediction")

				gender_prediction = gender_model.predict(img_224)[0,:]

				if np.argmax(gender_prediction) == 0:
					gender = "Woman"
				elif np.argmax(gender_prediction) == 1:
					gender = "Man"

				resp_obj += "\"gender\": \"%s\"" % (gender)

			elif action == 'race':
				if img_224 is None:
					img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images
				race_predictions = race_model.predict(img_224)[0,:]
				race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']

				sum_of_predictions = race_predictions.sum()

				race_obj = "\"race\": {"
				for i in range(0, len(race_labels)):
					race_label = race_labels[i]
					race_prediction = 100 * race_predictions[i] / sum_of_predictions

					if i > 0: race_obj += ", "

					race_obj += "\"%s\": %s" % (race_label, race_prediction)

				race_obj += "}"
				race_obj += ", \"dominant_race\": \"%s\"" % (race_labels[np.argmax(race_predictions)])

				resp_obj += race_obj

			action_idx = action_idx + 1

		resp_obj += "}"

		resp_obj = json.loads(resp_obj)

		if bulkProcess == True:
			resp_objects.append(resp_obj)
		else:
			return resp_obj

	if bulkProcess == True:
		resp_obj = "{"

		for i in range(0, len(resp_objects)):
			resp_item = json.dumps(resp_objects[i])

			if i > 0:
				resp_obj += ", "

			resp_obj += "\"instance_"+str(i+1)+"\": "+resp_item
		resp_obj += "}"
		resp_obj = json.loads(resp_obj)
		return resp_obj
		#return resp_objects

def detectFace(img_path):
	img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3)
	return img[:, :, ::-1] #bgr to rgb

def stream(db_path, model_name ='VGG-Face', distance_metric = 'cosine', enable_face_analysis = True):
	realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis)

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

functions.allocateMemory()

functions.initializeFolder()

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

max_stars_repo_name: sinanurun/deepface

max_issues_repo_name: yunziche/deepface

max_forks_repo_name: yunziche/deepface


text:
'''
数据类型对象描述了对应于数组的固定内存块的解释, 取决于一下方面:
	数组类型(整数, 浮点或Python对象)
	数据大小
	字节序(小端或大端)
	在结构类型的情况下, 字段的名称, 每个字段的数据类型, 和每个字段占用的内存块部分
	如果数据类型是子序列, 他的形状和数据类型

字节顺序取决于数据类型的前缀<或>.<意味着编码是小端(最小的有效字节存储在最小地址中)
.>意味着编码是大端(最大的有效地址存储在最次奥地址中)
'''

import numpy as np
# dtype可由以下语法构建
np.dtype(object, align=True, copy=True)
'''
	object 被转换为数据类型的对象
	align 如果true,则向字段添加间隔, 使其类似于C的结构体
	copy 生成dtype对象的新副本, 如果为flase, 结果是内建数据类型对象的引用
'''

# example 1
dt = np.dtype(np.int32)
print(dt)
# example 2
# int8, int16, int32, int64可以替换为等价的字符串'i1', 'i2', 'i4'以及其他
dt = np.dtype('i8')
print(dt)
# example 3
# 使用端号标记
dt = np.dtype('>i4')
print(dt)
# example 4
dt = np.dtype([('age', np.int8)])
print(dt)
# example 5
dt = np.dtype([('age', np.int8)])
a = np.array([(10,), (20, ), (30,)], dtype=dt)
print(a)
# example 6
dt = np.dtype([('age', np.int8)])
a = np.array([(10,), (20, ), (30,)], dtype=dt)
print(a['age'])
# example 7
student = np.dtype([('name', 'S20'), ('age', 'i1'), ('mark', 'f4')])
print(student)
# example 8
student = np.dtype([('name', 'S20'), ('age', 'i1'), ('mark', 'f4')])
a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
print(a)
'''
每个内建类型都有一个唯一定义它的字符代码
	'b': 布尔型
	'i': 符号整数
	'u': 无符号整数
	'f': 浮点
	'c': 负数浮点
	'm': 时间间隔
	'M': 日期时间
	'O': Python对象
	'S', 'a': 字符串
	'U': Unicode
	'V': 原始数据(void)
'''


text:
#!/usr/bin/env python
"""
Blanks (sets to a given value) those voxels of a tomogram that correspond to
given segments.

This script may be placed anywhere in the directory tree.

$Id$
Author: Vladan Lucic (Max Planck Institute for Biochemistry)
"""
from __future__ import unicode_literals

__version__ = "$Revision$"

import os

import numpy

import pyto
import pyto.scripts.common as common

##############################################################
#
# Parameters
#
##############################################################

############################################################
#
# Image (grayscale) file. If it isn't in em or mrc format, format related
# variables (shape, data type, byte order and array order) need to be given
# (see labels file section below).
#

# name of the image file
image_file_name = "../3d/tomo.mrc"

#####################################################
#
# Labels file. If the file is in em or mrc format shape, data type, byte order
# and array order are not needed (should be set to None). If these variables
# are specified they will override the values given in em or mrc headers.
#

# name of the labels file (file containing segmented volume)
labels_file_name = "labels_1.raw"

# labels file dimensions
labels_shape = None     # shape given in header of the disc file (if em
                        # or mrc), or in the tomo (grayscale image) header
#shape = (100, 120, 90) # shape given here

# labels file data type (e.g. 'int8', 'int16', 'int32', 'float16', 'float64')
labels_data_type = 'int8'

# labels file byteOrder ('<' for little-endian, '>' for big-endian)
labels_byte_order = '<'

# labels file array order ('F' for x-axis fastest, 'C' for z-axis fastest)
labels_array_order = 'F'

#########################################
#
# Calculation related parameters
#

# ids of all labels that need to be blanked (removed)
ids = [2,3,4]       # individual ids
#ids = range(2,5)   # range of ids, same as above

# value to which the blanked elements are set
value = -1.

###########################################################
#
# Output (blanked) file. The output file name is formed as:
#
#   labels_directory + labels_base_name + <connSuffix> + labels_extension
#

# blanked tomo file name suffix
blank_suffix = '_1'

################################################################
#
# Work
#
################################################################

################################################
#
#  Main function
#

def main():

    # read image file
    #imageFile = pyto.io.ImageIO()
    #imageFile.read(file=image_file_name)
    image = common.read_image(
        file_name=image_file_name, header=True, memmap=True)
    new_shape = common.find_shape(
        file_name=image_file_name, shape=labels_shape,
        suggest_shape=image.data.shape)

    # read labels file
    #labels = pyto.io.ImageIO()
    #labels.read(
    labels = pyto.segmentation.Segment.read(
        file=labels_file_name, byteOrder=labels_byte_order,
        dataType=labels_data_type, arrayOrder=labels_array_order,
        shape=new_shape)

    # calculations
    blank_data = image.data.copy()
    for id in ids:
        blank_data[labels.data==id] = value

    # extract parts from the image_file_name and make blank image name
    (dir, base) = os.path.split(image_file_name)
    (root, ext) = os.path.splitext(base)
    blank_file_name = os.path.join(dir, root+blank_suffix+ext)

    # write the blank file with the same header as in imageFile
    image_io = pyto.io.ImageIO()
    image_io.readHeader(file=image_file_name)
    image_io.setData(data=blank_data)
    image_io.write(file=blank_file_name)

# run if standalone
if __name__ == '__main__':
    main()


text:
# coding: utf8

from pytest import raises
from sortedcontainers import SortedList
import numpy as np

from src.combopt.shortest_paths import ParetoFrontier,Label_feillet2004
from src.combopt.graph import Grafo, Node, LinkedNode, Grafo_consumos
vertex = 0
lista = [(4,2),(5,0.5),(1,5),(3,3),(2,4),(2,2.8),(2.5,5),(2.5,2.7),(0.5,2.7),(6,0.4),(6,5),(0.5,6)]

def test_ParetoFrontier_add():
    # test inicialización sin lista de inicio y adición progresiva de etiquetas
    Descartados = set()
    pareto = ParetoFrontier()
    for etiqueta in lista:
        pareto, indicador, descartados = pareto.add(etiqueta, pure_pareto=True)
        Descartados = Descartados.union(descartados)
    assert pareto.show_pareto() == {0:np.inf, 0.5:2.7, 2.5:2.7, 4:2, 5:0.5, 6:0.4}
    assert Descartados == {(1,5), (3,3), (2,2.8)}
    print('conjunto de descartados', Descartados)

def test_ParetoFrontier_init():
    # test para inicialización con lista no vacía
    pareto = ParetoFrontier(lista)
    pareto_mapa, sucesores_mapa, predecesores_mapa = pareto.show_pareto2()
    print('mapa', pareto_mapa)
    print('sucesores', sucesores_mapa)
    print('predecesores', predecesores_mapa)
    #assert pareto.show_pareto() == {0:np.inf, 0.5:2.7, 2.5:2.7, 4:2, 5:0.5, 6:0.4}

def test_ParetoFrontier_delete():
    pareto = ParetoFrontier(lista)
    pareto = pareto.Delete_label((2.5, 2.7))
    pareto_mapa, sucesores_mapa, predecesores_mapa = pareto.show_pareto2()
    print('mapa', pareto_mapa)
    print('sucesores', sucesores_mapa)
    print('predecesores', predecesores_mapa)
    A = pareto.list_frontlabels()
    print(A)

def test_Label_feillet2004():
    vertices = [0, 1, 2, 3, 4]
    arcos = [(0,1), (0,2), (1, 2), (1, 3), (2, 3), (3, 2), (2, 4), (3, 4)]
    tiempo_nodos = {v: 0 for v in vertices}  # no hay tiempo de espera en los nodos
    demanda_nodos = {v: 1 for v in vertices}  # supongamos demanda unitaria
    tiempo_arcos = {(0, 1): 1, (0, 2): 2, (1, 2): 2, (1, 3): 2, (2, 3): 1, (3, 2): 1, (2, 4): 2, (3, 4): 2}
    demanda_arcos = {a: 0 for a in arcos}  # no hay demanda en los arcos, sólo en los nodos
    ventanas_tiempo = {0: [0, 10], 1: [0, 10], 2: [0, 10], 3: [0, 10], 4: [0, 10], }
    ventanas_demanda = {v: 5 for v in vertices}
    # costos_arcos = {arco: 1 for arco in arcos}

    # será qué se puede incluir en las etiquetas de las aristas¡? mirar networkx
    costos_arcos = {(0, 1): 2, (0, 2): 2, (1, 2): 2, (1, 3): 2, (2, 3): 1, (3, 2): 1, (2, 4): 2, (3, 4): 2}

    recursos_nodos = {'tiempo': tiempo_nodos, 'demanda': demanda_nodos}
    recursos_arcos = {'tiempo': tiempo_arcos, 'demanda': demanda_arcos}
    restricciones_nodos = {'tiempo': ventanas_tiempo, 'demanda': ventanas_demanda}

    mi_grafo_consumos = Grafo_consumos(vertices,
                                       arcos,
                                       directed=True,
                                       recursos_nodos=recursos_nodos,
                                       recursos_arcos=recursos_arcos,
                                       restricciones_nodos=restricciones_nodos,
                                       costos_arcos=costos_arcos)

    etiqueta = Label_feillet2004(nodo_rel=0, G=mi_grafo_consumos)

    assert etiqueta.label_recursos == {'tiempo' : 0, 'demanda': 1}

    assert etiqueta.label_visitas == {0: 1, 1: 0, 2: 0, 3: 0, 4: 0}

    assert etiqueta.label == {'tiempo': 0, 'demanda': 1, 0: 1, 1: 0, 2: 0, 3: 0, 4: 0}

    assert etiqueta.nodo_rel == 0

    assert etiqueta.longitud == 9

    etiqueta.update_nodo_rel(2)
    assert etiqueta.nodo_rel == 2

    etiqueta.update_label_recursos({'demanda': 1, 'tiempo': 2})
    assert etiqueta.label_recursos['demanda'] == 1
    assert etiqueta.label_recursos['tiempo'] == 2
    assert etiqueta.label['tiempo'] == 2

    etiqueta.update_label_visitas([1,2])
    assert etiqueta.label_visitas[2] ==1
    assert etiqueta.label[1] == 1
    assert etiqueta.conteo == 3


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 31 09:54:25 2016

@author: edouard.duchesnay@cea.fr
"""

'''
SVM & Kernel methods
====================
'''
import numpy as np
from numpy.linalg import norm

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sklearn.metrics as metrics
#%matplotlib inline
#%matplotlib qt

class KernDensity:
    def __init__(self, sigma=1):
        self.sigma = sigma

    def fit(self, X, y, alphas=None):
        self.X = X
        self.y = y
        if alphas is None:
            alphas = np.ones(X.shape[0])
        self.alphas = alphas

    def predict(self, X):
        y_pred = np.zeros((X.shape[0]))
        for j, x in enumerate(X):
            for i in range(self.X.shape[0]):
                #print(j, i, x)
                y_pred[j] += self.alphas[i] * self.y[i] * np.exp( - (norm(self.X[i, :] - x) ** 2) / (2 * self.sigma ** 2))
        return(y_pred)

## Plot 3D
def plot3d(coord_x, coord_y, coord_z, points, y, zlim=None, ax=None, fig=None, xylabelsize=33):
    # Plot
    from matplotlib import cm
    if fig is None:
        fig = plt.figure()
    if ax is None:
        ax = fig.add_subplot(111, projection='3d')
    z_min = np.min(coord_z) - np.max(coord_z) * 2
    ax.plot_surface(coord_x, coord_y, coord_z, rstride=2, cstride=2,
                    #vmin=Z.min(), vmax=Z.max(),
                    cmap=cm.coolwarm,
                    linewidth=1, antialiased=True)
    cset = ax.contourf(coord_x, coord_y, coord_z, zdir='z', offset=z_min-10,
                       cmap=cm.coolwarm)
    argmin = coord_x.ravel()[coord_z.argmin()], coord_y.ravel()[coord_z.argmin()]
    print("argmin", argmin)
    # add point and cross at defined point
    colors = {-1:'b', 1:'r'}
    for lev in np.unique(y):
        pts = points[y==lev, :]
        ax.plot(pts[:, 0], pts[:, 1], 'o', color=colors[lev], zs=[z_min]*pts.shape[0], ms=10)
    ax.set_xlabel(r'$x^0$', size=xylabelsize)
    ax.set_ylabel(r'$x^1$', size=xylabelsize)
    #ax.set_zlabel(r'$Kernel density$', size=xylabelsize)
    ax.set_zlim(z_min, np.max(coord_z))
    return ax, z_min, argmin

## Dataset
##########

im = np.array(
      [[ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.]])

x0, y0 = np.where(im == 0)
x1, y1 = np.where(im == 1)

X = np.column_stack([
    np.concatenate([x0, x1]),
    np.concatenate([y0, y1])])
y = np.array([-1] * len(x0) + [1] * len(x1))

xmin, xmax, ymin, ymax = 0, im.shape[0]-1, 0, im.shape[1]-1
coord_x, coord_y = np.mgrid[xmin:xmax:50j, ymin:ymax:50j]
XX = np.column_stack([coord_x.ravel(), coord_y.ravel()])

# Kernel mapping
################

self = KernDensity(sigma=.2)
self.fit(X, y)
y_pred_kde = self.predict(XX)
coord_z_kde = y_pred_kde.reshape(coord_x.shape)
points=X

# View 2D
if False:
    plt.imshow(np.rot90(coord_z_kde), cmap=plt.cm.coolwarm, extent=[xmin, xmax, ymin, ymax], aspect='auto')
    plt.plot(X[y==1, 0], X[y==1, 1], 'o', color='r')#, zs=[z_min], ms=20)
    plt.plot(X[y==-1, 0], X[y==-1, 1], 'o', color='b')#, zs=[z_min], ms=20)

fig = plt.figure(figsize=(30, 15))

ax=fig.add_subplot(121, projection='3d')
ax, z_min, argmin = plot3d(coord_x, coord_y, coord_z_kde, points=X, y=y, ax=ax, fig=fig)
plt.title(r'$x \rightarrow K(x_i, x) = \exp\left(-\frac{||x_i - x_j||^2}{2\sigma^2}\right)$', size=33)
# set camera to fixed point of view
print(ax.azim, ax.elev, ax.dist)
#(-152.49214958606902, 21.717791411042867, 10)
#ax.view_init(azim=-152, elev=21) #Reproduce view
#ax.view_init(azim=-14.1935483871, elev=29.6875, dist=10)

# SV
#####

from sklearn.svm import SVC
#1.0 / X.shape[1] 0.5
#(1/(2 *.2)) : 2.5
clf = SVC(kernel='rbf')#, gamma=1)
clf.fit(X, y)
clf.support_vectors_.shape

print(clf.support_.shape)

np.all(X[clf.support_,:] == clf.support_vectors_)

Xsv = clf.support_vectors_
y_sv = y[clf.support_]

y_pred_svm = clf.predict(XX)
#self = KernDensity(sigma=.2)
#self.fit(X, y)
#y_pred = self.predict(XX)
coord_z_svm = y_pred_svm.reshape(coord_x.shape)

# View 2D
if False:
    plt.imshow(np.rot90(coord_z_svm), cmap=plt.cm.coolwarm, extent=[xmin, xmax, ymin, ymax], aspect='auto')
    plt.plot(Xsv[y_sv==1, 0], Xsv[y_sv==1, 1], 'o', color='r')#, zs=[z_min], ms=20)
    plt.plot(Xsv[y_sv==-1, 0], Xsv[y_sv==-1, 1], 'o', color='b')#, zs=[z_min], ms=20)

#fig = plt.figure(figsize=(15, 15))
ax=fig.add_subplot(122, projection='3d')
ax, z_min, argmin = plot3d(coord_x, coord_y, coord_z_svm, points=Xsv, y=y_sv, ax=ax, fig=fig)
plt.title(r'$f(x) = sign \left(\sum_{i \in SV}\alpha_i y_i \exp\left(-\frac{||x_i - x_j||^2}{2\sigma^2}\right)\right)$', size=33)
# set camera to fixed point of view
#ax.azim, ax.elev, ax.dist
#(-152.49214958606902, 21.717791411042867, 10)
#ax.view_init(azim=-152, elev=21) #Reproduce view

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

import numpy as np
from sklearn.svm import SVC
from sklearn import datasets
import matplotlib.pyplot as plt

# dataset
X, y = datasets.make_classification(n_samples=10, n_features=2,n_redundant=0,
                                    n_classes=2,
                                    random_state=1,
                                    shuffle=False)
clf = SVC(kernel='rbf')#, gamma=1)
clf.fit(X, y)
print("#Errors: %i" % np.sum(y != clf.predict(X)))

clf.decision_function(X)

# Usefull internals:
# Array of support vectors
clf.support_vectors_

# indices of support vectors within original X
np.all(X[clf.support_,:] == clf.support_vectors_)

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

from sklearn.ensemble import RandomForestClassifier

forest = RandomForestClassifier(n_estimators = 100)
forest.fit(X, y)

print("#Errors: %i" % np.sum(y != forest.predict(X)))

max_stars_repo_name: gautard/pystatsml

max_issues_repo_name: gautard/pystatsml

max_forks_repo_name: gautard/pystatsml

reason: import numpy,from numpy


text:
#!/usr/bin/env python3

# Note from/to Dan. This driver appears to take a premade transfer function for
# each mixer. The trick is then to load in the transfer function and perform the
# actual predistortion. We therefore need to find the correct transfer function
# and save this transfer function to file. These transfer functions need to be
# saved as Transfer function #1, etc.

# The predistoriton program as it stands only produces the transfer function of
# one mixer at a time. A challenge will be finding a good way to add the
# transfer functions to a common file in a convenient way.

import cmath

import numpy as np
from numpy.fft import fft, fftfreq, fftshift, ifft, ifftshift
from scipy.interpolate import interp1d
import scipy.signal as signal

import write_configuration as CONST

class Predistortion(object):
    """This class is used to predistort I/Q waveforms for qubit XY control."""

    def __init__(self, waveform_number=0):
        # define variables
        self.transfer_path = ''
        # keep track of which Labber waveform this predistortion refers to
        self.waveform_number = waveform_number
        # TODO(dan): define variables for predistortion algorithm

    def set_parameters(self, config={}):
        """Set base parameters using config from from Labber driver.

        Parameters
        ----------
        config : dict
            Configuration as defined by Labber driver configuration window

        """
        # Labber configuration contains multiple predistortions, get right one
        path = config.get('Transfer function #%d' % (self.waveform_number + 1))
        # only reload tranfser function if file changed
        if path != self.transfer_path:
            self.import_transfer_function(path)

        self.dt = 1 / config.get('Sample rate')

    def import_transfer_function(self, path):
        """Import transfer function data.

        Parameters
        ----------
        path : str
            Path to file containing transfer function data

        """
        # store new path
        self.transfer_path = path

        # return directly if not in use, look for both '' and '.'
        if self.transfer_path.strip() in ('', '.'):
            return
        import Labber
        f = Labber.LogFile(self.transfer_path)
        self.vResponse_freqs, self.vFilteredResponse_FFT_I = f.getTraceXY(
            y_channel=0)
        self.vResponse_freqs, self.vFilteredResponse_FFT_Q = f.getTraceXY(
            y_channel=1)
        # TODO(dan): load transfer function data

    def predistort(self, waveform):
        """Predistort input waveform.

        Parameters
        ----------
        waveform : complex numpy array
            Waveform data to be pre-distorted

        Returns
        -------
        waveform : complex numpy array
            Pre-distorted waveform

        """
        # find timespan of waveform
        self.tvals = np.arange(0, self.dt * len(waveform), self.dt)

        response_I = ifft(ifftshift(self.vFilteredResponse_FFT_I))
        response_FFT_I_r = fftshift(fft(complex(1, 0) * response_I.real))
        response_FFT_I_i = fftshift(fft(complex(1, 0) * response_I.imag))

        response_Q = ifft(ifftshift(self.vFilteredResponse_FFT_Q))
        response_FFT_Q_r = fftshift(fft(complex(1, 0) * response_Q.real))
        response_FFT_Q_i = fftshift(fft(complex(1, 0) * response_Q.imag))

        # {{a, b},{c, d}}, determinant is ad-bc, plus sign comes from
        # additional i tacked on to the Q by the IQ mixer.
        # I removed this factor of i from the FFT of the response function.
        determinant = response_FFT_I_r * response_FFT_Q_i - \
            response_FFT_Q_r * response_FFT_I_i

        Za = response_FFT_Q_i / determinant
        Zb = -response_FFT_Q_r / determinant
        Zc = -response_FFT_I_i / determinant
        Zd = response_FFT_I_r / determinant

        Inverse_A = interp1d(self.vResponse_freqs, Za)
        Inverse_B = interp1d(self.vResponse_freqs, Zb)
        Inverse_C = interp1d(self.vResponse_freqs, Zc)
        Inverse_D = interp1d(self.vResponse_freqs, Zd)

        # applies the interpolated inverse function to the AWG signal

        fft_vals, fft_signal_r = self.apply_FFT(
            self.tvals, complex(1, 0) * waveform.real)
        fft_vals, fft_signal_i = self.apply_FFT(
            self.tvals, complex(1, 0) * waveform.imag)

        fft_signal = (fft_signal_r * Inverse_A(fft_vals) + fft_signal_i *
                      Inverse_B(fft_vals) + 1j *
                      (fft_signal_r * Inverse_C(fft_vals) +
                       fft_signal_i * Inverse_D(fft_vals)))
        corr_signal = ifft(ifftshift(fft_signal))

        vI = np.array(corr_signal.real, dtype='float64')
        vQ = np.array(corr_signal.imag, dtype='float64')

        return vI + 1j * vQ

    def apply_FFT(self, tvals, signal):
        fft_signal = fftshift(fft(signal))
        fft_vals = fftshift(fftfreq(len(signal), tvals[1] - tvals[0]))
        return fft_vals, fft_signal

class Filter:
    r"""Filter signal with an IIR filters and a FIR filter.

    The IIR filter is constucted from continous time transfer function in
    zero-pole-gain form.

    Parameters
    ----------
    fs : float
        Sample rate
    z : array_like
    p : array_like
    k : float
        zeros, poles and gain of the IIR filter
    F : array_like, optional
        Coefficients for FIR filter
    gain : :obj:`float`, optional
    offset : :obj:`float`, optional
        Gain and offset of input signal. The signal actually input to filter is
        `(data-offset)/gain`
    """
    F = None
    gain = 1
    offset = 0

    def __init__(self, fs, z, p, k, F=None, gain=1, offset=0):
        self.fs = fs
        self.z = np.array(z)
        self.p = np.array(p)
        self.k = k
        if F is not None:
            self.F = np.array(F, dtype='f8')
        self.gain = gain
        self.offset = offset
        self._cache_sos()

    def _cache_sos(self):
        """Converts continuous time zpk filter to discrete time sos filter."""
        z = self.z
        p = self.p
        k = self.k
        self._sos = signal.zpk2sos(*signal.bilinear_zpk(z, p, k, self.fs))

    def lfilter(self, data):
        """Applys filter to data.

        Parameters
        ----------
        data：array_like
            Waveform to apply filter

        Returns
        -------
        waveform: ndarray
            Filtered waveform
        """
        data = (data-self.offset)/self.gain
        data = signal.sosfilt(self._sos, data)
        if self.F is not None:
            data = signal.lfilter(self.F, [1.], data)
        return data

    def get_response_time(self):
        p = self.p
        p = p[p!=0]
        return 6 * (1/np.abs(p)).max()

    def __repr__(self):
        return (
            f'Filter(fs={self.fs}, z={self.z!r}, p={self.p!r}, k={self.k}, '
            f'F={self.F!r}, gain={self.gain}, offset={self.offset})'
        )

def _convert_parameters_to_list(params, n_real, n_comp):
    """Extracts IIR filter parameters from dict."""
    zeros = []
    poles = []
    # First order, zero z<0
    for i in range(n_real):
        tauB = params[f'tauB{i+1}']
        B = params[f'B{i+1}']
        zeros.append(-1/tauB/(1+B))
        poles.append(-1/tauB)
    # Second order
    for i in range(n_comp):
        tauA = params[f'tauA{i+1}']
        A = params[f'A{i+1}']
        T = params[f'TA{i+1}']
        phi = params[f'phiA{i+1}']
        poles.extend([-1/tauA-2j*np.pi/T, -1/tauA+2j*np.pi/T])
        # solve for zeros
        a = 1 + A*np.cos(phi)
        b = (1+a)/tauA - 2*np.pi*A*np.sin(phi)/T
        c = 1/tauA**2 + (2*np.pi/T)**2
        d = cmath.sqrt(b**2 - 4*a*c)
        zeros.extend([(-b-d)/(2*a), (-b+d)/(2*a)])
    return zeros, poles

def get_invfilter_by_parameters(params, fs, n_real, n_comp, block_DC):
    """Construct inverse IIR filter from dict."""
    z, p = _convert_parameters_to_list(params, n_real, n_comp)
    k = np.real(np.prod(p) / np.prod(z))
    F = None
    try:
        gain = params['gain']
        offset = params['offset']
    except:
        gain = 1
        offset = 0
    if block_DC:
        tauC = params['tauC']
        leakC = params['leakC']
        z.append(-leakC/tauC)
        p.append(-1/tauC)
    # exchange zeros and poles to get inverse filter
    return Filter(fs, p, z, 1/k, F, gain, offset)

class ExponentialPredistortion:
    """Implement predistortion on the Z waveforms.

    Parameters
    ----------
    waveform_number : int
        The waveform number to predistort.

    Attributes
    ----------
    A1 : float
        Amplitude for the first pole.
    tau1 : float
        Time constant for the first pole.
    A2 : float
        Amplitude for the second pole.
    tau2 : float
        Time constant for the second pole.
    A3 : float
        Amplitude for the third pole.
    tau3 : float
        Time constant for the third pole.
    A4 : float
        Amplitude for the fourth pole.
    tau4 : float
        Time constant for the fourth pole.
    tauC : float
        Time constant for capacitor.
    from_str : bool
        If true, use ``filter_str`` to construct :obj:`Filter`.
    filter_str: str
        String representation of :obj:`Filter`.
    fs : float
        Sample rate for the waveform.
    """

    def __init__(self, waveform_number):
        self.fs = 1
        self.n = int(waveform_number)

    def set_parameters(self, config={}):
        """Set base parameters using config from from Labber driver.

        Parameters
        ----------
        config : dict
            Configuration as defined by Labber driver configuration window

        """
        if bool(config.get('Uniform predistort Z')):
            m=''
        else:
            m = self.n + 1
        self.fs = config.get('Sample rate')
        use_str = bool(config.get(f'Predistort Z{m} - from string'))
        if use_str:
            filter_str = config.get(f'Predistort Z{m} - string')
            from numpy import array
            try:
                self._filter = eval(filter_str)
            except:
                self._filter = Filter(self.fs, [], [], 1)
        else:
            params = dict()
            n_comp = 0
            n_real = 0
            for i in range(CONST.Z_PREDISTORTION_TERMS_COMP):
                Ai = config.get(f'Predistort Z{m} - A{i+1}')
                tauAi = config.get(f'Predistort Z{m} - tauA{i+1}')
                TAi = config.get(f'Predistort Z{m} - TA{i+1}')
                phiAi = config.get(f'Predistort Z{m} - phiA{i+1}')
                if Ai != 0 and tauAi > 0 and TAi > 0:
                    n_comp += 1
                    params[f'A{n_comp}'] = Ai
                    params[f'tauA{n_comp}'] = tauAi
                    params[f'TA{n_comp}'] = TAi
                    params[f'phiA{n_comp}'] = phiAi * np.pi/180
            for i in range(CONST.Z_PREDISTORTION_TERMS):
                Bi = config.get(f'Predistort Z{m} - B{i+1}')
                tauBi = config.get(f'Predistort Z{m} - tauB{i+1}')
                if Bi != 0 and tauBi > 0:
                    n_real += 1
                    params[f'B{n_real}'] = Bi
                    params[f'tauB{n_real}'] = tauBi
            tauC = config.get(f'Predistort Z{m} - tauC')
            block_DC = False
            if tauC > 0:
                params[f'tauC'] = tauC
                params['leakC'] = config.get(f'Predistort Z{m} - leakC')
                block_DC = True
            self._filter = get_invfilter_by_parameters(params, self.fs, n_real, n_comp, block_DC)

    def predistort(self, waveform):
        """Predistort input waveform.

        Parameters
        ----------
        waveform : complex numpy array
            Waveform data to be pre-distorted

        Returns
        -------
        waveform : complex numpy array
            Pre-distorted waveform

        """
        # # pad with zeros at end to make sure response has time to go to zero
        # pad_time = self._filter.get_response_time()
        # padded = np.pad(
        #     waveform,
        #     (0, round(pad_time * self.fs)),
        #     mode='constant'
        #     constant_values=0,
        # )
        return self._filter.lfilter(waveform)

if __name__ == '__main__':
    pass

max_stars_repo_name: kahojyun/Drivers

max_issues_repo_name: kahojyun/Drivers

max_forks_repo_name: kahojyun/Drivers

reason: import numpy,from numpy,import scipy,from scipy


text:
import numpy as np

x = np.array([[-9, -5, +5],
              [+4, -7, -11],
              [+7, +6, -1],
              [-9, -5, +4],
              [-5, -6, -1],
              [-4, -4, -8],
              [+5, +7, -9],
              [+2, -4, +3],
              [-6, +1, +7],
              [-10, +6, -7]],
             dtype=np.float32)

y_hat = np.array([0, 0, 1, 1, 1, 0, 1, 0, 0, 1],
                 dtype=np.float32)

W = np.array([-0.1, -0.3, 0.2],
             dtype=np.float32)
b = 2
d = 10
eta = 0.02
ones = np.ones(10)

y = np.matmul(x, W) + b
delta = y - y_hat

W_new = W - eta * np.matmul(x.T, delta) * (1 / d)
b_new = b - eta * np.matmul(ones.T, delta) * (1 / d)

MSE = (1 / (2 * d)) * np.matmul(delta.T, delta)

print("New weights = ", W_new.round(2), " \nNew bias = %.2f \nMSE = %.2f" % (b_new, MSE))

max_stars_repo_name: GiorgiaAuroraAdorni/neural-networks

max_issues_repo_name: GiorgiaAuroraAdorni/neural-networks

max_forks_repo_name: GiorgiaAuroraAdorni/neural-networks