text:
import time
import random
import numpy as np
import copy
import math
import logging
import os
import sys
import pickle
import glob
from pathlib import Path

from PIL import Image
import cv2

from scipy.spatial.transform import Rotation as R
import scipy.misc
import scipy.io as scio

import torchvision.transforms as transforms
import torch

# From costume modules
from helper import re_quat
from loaders_v2 import Backend
from deep_im import ViewpointManager
from helper import get_bb_from_depth, get_bb_real_target, backproject_points
from rotations import *

# For flow calculation
import trimesh
from trimesh.ray.ray_pyembree import RayMeshIntersector
from scipy.interpolate import griddata
import scipy.ndimage as nd

class YCB(Backend):
    def __init__(self, cfg_d, cfg_env):
        super(YCB, self).__init__(cfg_d, cfg_env)
        self._cfg_d = cfg_d
        self._cfg_env = cfg_env
        self._p_ycb = cfg_env['p_ycb']
        self._pcd_cad_dict, self._name_to_idx, self._name_to_idx_full = self._get_pcd_cad_models()
        self._batch_list = self._get_batch_list()
        self._h = 480
        self._w = 640

        self._num_pt = cfg_d.get('num_points', 1000)
        self._trancolor_background = transforms.ColorJitter(0.2, 0.2, 0.2, 0.05)

        if cfg_d['output_cfg'].get('color_jitter_real', {}).get('active', False):
            self._color_jitter_real = transforms.Compose([
                transforms.ToPILImage(),
                transforms.ColorJitter(**cfg_d['output_cfg'].get('color_jitter_real', {}).get('cfg', False)),
                transforms.ToTensor()
            ]
            )
        if cfg_d['output_cfg'].get('color_jitter_render', {}).get('active', False):
            self._color_jitter_render = transforms.Compose([
                transforms.ToPILImage(),
                transforms.ColorJitter(**cfg_d['output_cfg'].get('color_jitter_render', {}).get('cfg', False)),
                transforms.ToTensor()
            ]
            )
        if cfg_d['output_cfg'].get('norm_real', False):
            self._norm_real = transforms.Normalize(
                mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        if cfg_d['output_cfg'].get('norm_render', False):
            self._norm_render = transforms.Normalize(
                mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

        self._min_visible_mask_size = 50

        if self._cfg_d['output_cfg'].get('vm_in_dataloader', False):
            self._use_vm = True
            store = cfg_env['p_ycb'] + '/viewpoints_renderings'
            self._vm = ViewpointManager(
                store=copy.deepcopy(store),
                name_to_idx=copy.deepcopy(self._name_to_idx),
                nr_of_images_per_object=5000,
                device='cpu',
                load_images=False)
            self.up = torch.nn.UpsamplingBilinear2d(size=(self._h, self._w))
            self.up_nearest = torch.nn.UpsamplingNearest2d(size=(self._h, self._w))
            self.K_ren = self.get_camera('data_syn/0019', K=True)

        if self._cfg_d['noise_cfg'].get('use_input_jitter', False):
            n = self._cfg_d['noise_cfg']
            self._input_jitter = transforms.ColorJitter(
                n['jitter_brightness'],
                n['jitter_contrast'],
                n['jitter_saturation'],
                n['jitter_hue'])
        self._input_grey = transforms.RandomGrayscale(
            p=self._cfg_d['noise_cfg'].get('p_grey', 0))
        self._load_background()
        self._load_flow()
        self.err = False

    def getElement(self, desig, obj_idx, h_real_est=None):
        """
        desig : sequence/idx
        two problems we face. What is if an object is not visible at all -> meta['obj'] = None
        obj_idx is elemnt 1-21 !!!
        """
        img = Image.open(
            '{0}/{1}-color.png'.format(self._p_ycb, desig))
        depth = np.array(Image.open(
            '{0}/{1}-depth.png'.format(self._p_ycb, desig)))
        label = np.array(Image.open(
            '{0}/{1}-label.png'.format(self._p_ycb, desig)))
        meta = scio.loadmat(
            '{0}/{1}-meta.mat'.format(self._p_ycb, desig))
        K_cam= self.get_camera(desig, K=True, idx=False)

        img_copy = copy.copy( np.array( img) )
        if self._cfg_d['noise_cfg'].get('use_input_jitter', False):
            img = self._input_jitter(img)
        if self._cfg_d['noise_cfg'].get('p_grey', 0) > 0:
            img = self._input_grey(img)

        obj = meta['cls_indexes'].flatten().astype(np.int32)

        obj_idx_in_list = int(np.argwhere(obj == obj_idx))

        h_gt = np.eye(4)
        h_gt[:3,:4] =  meta['poses'][:, :, obj_idx_in_list]
        unique_desig = (desig, obj_idx)

        if np.sum( label == obj_idx) <= self._min_visible_mask_size:
            if self.err:
                print("Violating in min number points in get Element")
            return False

        dellist = [j for j in range(0, len(self._pcd_cad_dict[obj_idx]))]
        dellist = random.sample(dellist, len(
            self._pcd_cad_dict[obj_idx]) - self._num_pt_mesh_large)
        model_points = np.delete(self._pcd_cad_dict[obj_idx], dellist, axis=0).astype(np.float32)

        # Desig, CAD, Idx
        tup = ( unique_desig,
                torch.from_numpy(model_points),
                torch.LongTensor([int(obj_idx) - 1]))

        if self._use_vm:
            cam_flag = self.get_camera(desig,K=False,idx=True)
            new_tup = self.get_rendered_data( np.array(img)[:,:,:3], depth, label, model_points, int(obj_idx), K_cam, cam_flag, h_gt, h_real_est)
            if new_tup is False:
                if self.err:
                    print("Violation in get render data")
                return False
            else:
                tup += new_tup

            if self._cfg_d['output_cfg']['visu']['status']:
                # Depth map # Label # Image
                tup += (torch.from_numpy(img_copy.astype(np.float32))[:,:,:3],
                        torch.from_numpy(depth),\
                        torch.from_numpy(label) )

        else:
            raise AssertionError

        return tup

    def get_rendered_data(self, img, depth_real, label, model_points, obj_idx, K_real, cam_flag, h_gt, h_real_est=None):
        """Get Rendered Data

        Args:
            img ([np.array numpy.uint8]): H,W,3
            depth_real ([np.array numpy.int32]): H,W
            label ([np.array numpy.uint8]): H,W
            model_points ([np.array numpy.float32]): 2300,3
            obj_idx: (Int)
            K_real ([np.array numpy.float32]): 3,3
            cam_flag (Bool)
            h_gt ([np.array numpy.float32]): 4,4
            h_real_est ([np.array numpy.float32]): 4,4
        Returns:
            real_img ([torch.tensor torch.float32]): H,W,3
            render_img ([torch.tensor torch.float32]): H,W,3
            real_d ([torch.tensor torch.float32]): H,W
            render_d ([torch.tensor torch.float32]): H,W
            gt_label_cropped ([torch.tensor torch.long]): H,W
            u_cropped_scaled ([torch.tensor torch.float32]): H,W
            v_cropped_scaled([torch.tensor torch.float32]): H,W
            valid_flow_mask_cropped([torch.tensor torch.bool]): H,W
            bb ([tuple]) containing torch.tensor( real_tl, dtype=torch.int32) , torch.tensor( real_br, dtype=torch.int32) , torch.tensor( ren_tl, dtype=torch.int32) , torch.tensor( ren_br, dtype=torch.int32 )
            h_render ([torch.tensor torch.float32]): 4,4
            h_init ([torch.tensor torch.float32]): 4,4
        """
        h = self._h
        w = self._w

        st = time.time()
        if not  ( h_real_est is None ):
            h_init = h_real_est
        else:
            nt = self._cfg_d['output_cfg'].get('noise_translation', 0.03)
            nr = self._cfg_d['output_cfg'].get('noise_rotation', 10)
            h_init = add_noise( h_gt, nt, nr)

        # transform points
        pred_points = (model_points @ h_init[:3,:3].T) + h_init[:3,3]

        render_img = torch.zeros((3, h, w))
        render_d = torch.empty((1, h, w))

        init_rot_wxyz = torch.tensor( re_quat( R.from_matrix(h_init[:3,:3]).as_quat(), 'xyzw') )
        idx = torch.LongTensor([int(obj_idx) - 1])
        img_ren, depth_ren, h_render = self._vm.get_closest_image_batch(
            i=idx[None], rot=init_rot_wxyz[None,:], conv='wxyz')

        # rendered data BOUNDING BOX Computation
        bb_lsd = get_bb_from_depth(depth_ren)
        b_ren = bb_lsd[0]
        tl, br = b_ren.limit_bb()
        if br[0] - tl[0] < 30 or br[1] - tl[1] < 30 or b_ren.violation():
            if self.err:
                print("Violate BB in get render data for rendered bb")
            return False
        center_ren = backproject_points(
            h_render[0, :3, 3].view(1, 3), K=self.K_ren)
        center_ren = center_ren.squeeze()
        b_ren.move(-center_ren[1], -center_ren[0])
        b_ren.expand(1.1)
        b_ren.expand_to_correct_ratio(w, h)
        b_ren.move(center_ren[1], center_ren[0])
        ren_h = b_ren.height()
        ren_w = b_ren.width()
        ren_tl = b_ren.tl
        render_img = b_ren.crop(img_ren[0], scale=True, mode="bilinear") # Input H,W,C
        render_d = b_ren.crop(depth_ren[0][:,:,None], scale=True, mode="nearest") # Input H,W,C

        # real data BOUNDING BOX Computation
        u_cropped = torch.zeros((h, w), dtype=torch.long)
        v_cropped = torch.zeros((h, w), dtype=torch.long)
        gt_label_cropped = torch.zeros((h, w), dtype=torch.long)
        bb_lsd = get_bb_real_target(torch.from_numpy( pred_points[None,:,:] ), K_real[None])
        b_real = bb_lsd[0]
        tl, br = b_real.limit_bb()
        if br[0] - tl[0] < 30 or br[1] - tl[1] < 30 or b_real.violation():
            if self.err:
                print("Violate BB in get render data for real bb")
            return False
        center_real = backproject_points(
            torch.from_numpy( h_init[:3,3][None] ), K=K_real)
        center_real = center_real.squeeze()
        b_real.move(-center_real[0], -center_real[1])
        b_real.expand(1.1)
        b_real.expand_to_correct_ratio(w, h)
        b_real.move(center_real[0], center_real[1])
        real_h = b_real.height()
        real_w = b_real.width()
        real_tl = b_real.tl
        real_img = b_real.crop(torch.from_numpy(img).type(torch.float32) , scale=True, mode="bilinear")
        real_d = b_real.crop(torch.from_numpy(depth_real[:, :,None]).type(
            torch.float32), scale=True, mode="nearest")
        gt_label_cropped = b_real.crop(torch.from_numpy(label[:, :, None]).type(
            torch.float32), scale=True, mode="nearest").type(torch.int32)

        #get flow
        if self._cfg_d['flow_cfg'].get('fast', True):
            flow = self._get_flow_fast(h_render[0].numpy(), h_gt, obj_idx, label, cam_flag, b_real, b_ren, K_real)
        else:
            flow = self._get_flow(h_render[0].numpy(), h_gt, obj_idx, label, cam_flag, b_real, b_ren )

        if type( flow ) is bool:
            if self.err:
                print("Flow calc failed")
            return False

        u_cropped = b_real.crop( torch.from_numpy( flow[0][:,:,None] ).type(
            torch.float32), scale=True, mode="bilinear").numpy()
        v_cropped =  b_real.crop(  torch.from_numpy( flow[1][:,:,None]).type(
            torch.float32), scale=True, mode="bilinear").numpy()
        valid_flow_mask_cropped =  b_real.crop(  torch.from_numpy( flow[2][:,:,None]).type(
            torch.float32), scale=True, mode="nearest").type(torch.bool).numpy()

        # scale the u and v so this is not in the uncropped space !
        v_cropped_scaled = np.zeros( v_cropped.shape, dtype=np.float32 )
        u_cropped_scaled = np.zeros( u_cropped.shape, dtype=np.float32)

        nr1 = np.full((h,w), float(w/real_w) , dtype=np.float32)
        nr2 = np.full((h,w), float(real_tl[1])  , dtype=np.float32)
        nr3 = np.full((h,w), float(ren_tl[1]) , dtype=np.float32 )
        nr4 = np.full((h,w), float(w/ren_w) , dtype=np.float32 )
        # v_cropped_scaled = (self._grid_y -((np.multiply((( np.divide( self._grid_y , nr1)+nr2) +(v_cropped[:,:,0]).astype(np.long)) - nr3 , nr4)).astype(np.long))).astype(np.long)
        v_cropped_scaled = (self._grid_y.astype(np.float32) -((np.multiply((( np.divide( self._grid_y.astype(np.float32) , nr1)+nr2) +(v_cropped[:,:,0])) - nr3 , nr4))))

        nr1 = np.full((h,w), float( h/real_h) , dtype=np.float32)
        nr2 = np.full((h,w), float( real_tl[0]) , dtype=np.float32)
        nr3 = np.full((h,w), float(ren_tl[0]) , dtype=np.float32)
        nr4 = np.full((h,w), float(h/ren_h) , dtype=np.float32)
        u_cropped_scaled = self._grid_x.astype(np.float32) -(np.round((((self._grid_x.astype(np.float32) /nr1)+nr2) +np.round(u_cropped[:,:,0]))-nr3)*(nr4))

        if self._cfg_d['output_cfg'].get('color_jitter_render', {}).get('active', False):
            render_img = self._color_jitter_render( render_img.permute(2,0,1) ).permute(1,2,0).type(torch.float32)
        if self._cfg_d['output_cfg'].get('color_jitter_real', {}).get('active', False):
            real_img = self._color_jitter_real( real_img.permute(2,0,1)).permute(1,2,0).type(torch.float32)
        if self._cfg_d['output_cfg'].get('norm_render', False):
            render_img = self._norm_render(render_img)
        if self._cfg_d['output_cfg'].get('norm_real', False):
            real_img = self._norm_real(real_img)

        tup = (real_img, render_img, \
                real_d[:,:,0], render_d[:,:,0],
                gt_label_cropped.type(torch.long)[:,:,0],
                torch.from_numpy( u_cropped_scaled[:,:] ).type(torch.float32),
                torch.from_numpy( v_cropped_scaled[:,:]).type(torch.float32),
                torch.from_numpy(valid_flow_mask_cropped[:,:,0]),
                flow[-4:],
                h_render[0].type(torch.float32),
                torch.from_numpy( h_init ).type(torch.float32),
                torch.from_numpy(h_gt).type(torch.float32),
                torch.from_numpy(K_real.astype(np.float32)))
        if self._cfg_d['output_cfg']['visu']['status']:
            tup += (img_ren[0], depth_ren[0])

        return tup

    def _get_flow(self, h_render, h_real, idx, label_img, cam, b_real, b_ren):
        st___ = time.time()
        f_1 = label_img == int( idx)

        min_vis_size = self._cfg_d.get('flow_cfg', {}).get('min_vis_size',200)
        if np.sum(f_1) < min_vis_size:
            # to little of the object is visible
            return False
        st = time.time()
        m_real = copy.deepcopy(self._mesh[idx])
        m_render = copy.deepcopy(self._mesh[idx])

        m_real = transform_mesh(m_real, h_real)
        m_render = transform_mesh(m_render, h_render)

        rmi_real = RayMeshIntersector(m_real)
        rmi_render = RayMeshIntersector(m_render)
        # locations index_ray index_tri

        # crop the rays to the bounding box of the object to compute less rays
        # subsample to even compute less rays !
        sub = self._cfg_d.get('flow_cfg', {}).get('sub',2)

        tl, br = b_real.limit_bb()
        h_idx_real = np.reshape( self._grid_x [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub], (-1) )
        w_idx_real = np.reshape( self._grid_y [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub], (-1) )

        rays_origin_real = self._rays_origin_real[cam]  [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]
        rays_dir_real =self._rays_dir[cam] [int(tl[0]) : int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]

        tl, br = b_ren.limit_bb()
        rays_origin_render = self._rays_origin_real[0] [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]
        rays_dir_render = self._rays_dir[0] [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]
        h_idx_render = np.reshape( self._grid_x [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub], (-1) )
        w_idx_render = np.reshape( self._grid_y [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub], (-1) )

        st_ = time.time()
        # ray traceing
        render_res_mesh_id = rmi_render.intersects_first(ray_origins= np.reshape( rays_origin_render, (-1,3) ),
            ray_directions=np.reshape(rays_dir_render ,(-1,3)))
        real_res_mesh_id = rmi_real.intersects_first(ray_origins=np.reshape( rays_origin_real, (-1,3) ) ,
            ray_directions=np.reshape(rays_dir_real, (-1,3)))

        # a = np.reshape( rays_origin_render, (-1,3) )
        # b = np.reshape( a, rays_origin_render.shape )

        # tl, br = b_real.limit_bb()
        # c1 = np.zeros ( self._grid_x.shape ) - 1
        # c1 [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]  = np.reshape ( real_res_mesh_id  , (rays_origin_real.shape[0], rays_origin_real.shape[1]) )

        # tl, br = b_ren.limit_bb()
        # c2 = np.zeros ( self._grid_x.shape ) - 1
        # c2 [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]  = np.reshape ( render_res_mesh_id  , (rays_origin_render.shape[0], rays_origin_render.shape[1]) )

        f_real = real_res_mesh_id != -1
        f_render = render_res_mesh_id != -1

        render_res_mesh_id = render_res_mesh_id[f_render]
        h_idx_render = h_idx_render[f_render]
        w_idx_render = w_idx_render[f_render]

        real_res_mesh_id = real_res_mesh_id[f_real]
        h_idx_real = h_idx_real[f_real]
        w_idx_real = w_idx_real[f_real]

        real_res_mesh_id.shape[0]

        disparity_pixels = np.zeros((self._h,self._w,2))-999
        matches = 0
        i = 0
        idx_pre = np.random.permutation( np.arange(0,real_res_mesh_id.shape[0]) ).astype(np.long)
        while matches < self._max_matches and i < self._max_iterations and i < real_res_mesh_id.shape[0]:
            r_id = idx_pre[i]
            mesh_id = int(  real_res_mesh_id [r_id] )
            s = np.where(  render_res_mesh_id == mesh_id )
            if s[0].shape[0] > 0:
                j = s[0][0]
                _h = h_idx_real[r_id]
                _w = w_idx_real[r_id]

                if _h == -1 or _w == -1 or h_idx_render[j] == -1  or w_idx_render[j] == -1:
                    pass
                    # print('encountered invalid pixel')
                else:
                    disparity_pixels[_h,_w,0] = h_idx_render[j] - _h
                    disparity_pixels[_h,_w,1] = w_idx_render[j] - _w
                    matches += 1
            i += 1

        # print(f'Rays origin real: {rays_origin_real.shape},  Rays dir: {rays_dir_real.shape}')
        # try:
            # print(f'IDX REAL max{np.max ( h_idx_real[ h_idx_real !=  -1 ] )}')
            # print(f'IDX REAL min{np.min ( h_idx_real[ h_idx_real !=  -1 ] )}')
        # except:
            # pass
        f_2 = disparity_pixels[:,:,0] != -999
        f_3 = f_2  * f_1
        points = np.where(f_3!=False)
        points = np.stack( [np.array(points[0]), np.array( points[1]) ], axis=1)

        min_matches = self._cfg_d.get('flow_cfg', {}).get('min_matches',50)
        if np.sum(f_3) < min_matches:
            # print(f'not enough matches{matches}, F3 {np.sum(f_3)}, REAL {h_idx_real.shape}')
            # print(render_res, rays_dir_render2.shape, rays_origin_render2.shape )
            return False

        u_map = griddata(points, disparity_pixels[f_3][:,0], (self._grid_x, self._grid_y), method='nearest')
        v_map = griddata(points, disparity_pixels[f_3][:,1], (self._grid_x, self._grid_y), method='nearest')

        dil_kernel_size = self._cfg_d.get('flow_cfg', {}).get('dil_kernel_size',2)
        inp = np.uint8( f_3*255 )
        kernel = np.ones((dil_kernel_size,dil_kernel_size),np.uint8)
        valid_flow_mask = ( cv2.dilate(inp, kernel, iterations = 1) != 0 )
        valid_flow_mask = valid_flow_mask * f_1

        real_tl = np.zeros( (2) )
        real_tl[0] = int(b_real.tl[0])
        real_tl[1] = int(b_real.tl[1])
        real_br = np.zeros( (2) )
        real_br[0] = int(b_real.br[0])
        real_br[1] = int(b_real.br[1])
        ren_tl = np.zeros( (2) )
        ren_tl[0] = int(b_ren.tl[0])
        ren_tl[1] = int(b_ren.tl[1])
        ren_br = np.zeros( (2) )
        ren_br[0] = int( b_ren.br[0] )
        ren_br[1] = int( b_ren.br[1] )

        return u_map, v_map, valid_flow_mask, torch.tensor( real_tl, dtype=torch.int32) , torch.tensor( real_br, dtype=torch.int32) , torch.tensor( ren_tl, dtype=torch.int32) , torch.tensor( ren_br, dtype=torch.int32 )

    def _get_flow_fast(self, h_render, h_real, idx, label_img, cam, b_real, b_ren, K_real):
        m_real = copy.deepcopy(self._mesh[idx])
        m_real = transform_mesh(m_real, h_real)

        rmi_real = RayMeshIntersector(m_real)
        sub = 1
        tl, br = b_real.limit_bb()

        rays_origin_real = self._rays_origin_real[cam]  [int(tl[0]): int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]
        rays_dir_real = self._rays_dir[cam] [int(tl[0]) : int(br[0]), int(tl[1]): int(br[1])][::sub,::sub]

        real_locations, real_index_ray, real_res_mesh_id = rmi_real.intersects_location(ray_origins=np.reshape( rays_origin_real, (-1,3) ) ,
            ray_directions=np.reshape(rays_dir_real, (-1,3)),multiple_hits=False)

        h_trafo =h_render @ np.linalg.inv( h_real )
        ren_locations = (real_locations @ h_trafo[:3,:3].T) + h_trafo[:3,3]

        uv_ren = backproject_points_np(ren_locations, K=self.K_ren)
        uv_real =  backproject_points_np(real_locations, K=K_real)
        dis = uv_ren-uv_real

        uv_real = np.uint32(uv_real)
        idx = np.uint32(uv_real[:,0]*(self._w) + uv_real[:,1])
        disparity_pixels = np.zeros((self._h,self._w,2))-999
        disparity_pixels = np.reshape( disparity_pixels, (-1,2) )
        disparity_pixels[idx] = dis
        disparity_pixels = np.reshape( disparity_pixels, (self._h,self._w,2) )

        f_3 = disparity_pixels[:,:,0] != -999

        u_map = disparity_pixels[:,:,0]
        v_map = disparity_pixels[:,:,1]
        u_map = fill( u_map, u_map == -999 )
        v_map = fill( v_map, v_map == -999 )

        real_tl = np.zeros( (2) )
        real_tl[0] = int(b_real.tl[0])
        real_tl[1] = int(b_real.tl[1])
        real_br = np.zeros( (2) )
        real_br[0] = int(b_real.br[0])
        real_br[1] = int(b_real.br[1])
        ren_tl = np.zeros( (2) )
        ren_tl[0] = int(b_ren.tl[0])
        ren_tl[1] = int(b_ren.tl[1])
        ren_br = np.zeros( (2) )
        ren_br[0] = int( b_ren.br[0] )
        ren_br[1] = int( b_ren.br[1] )

        return u_map, v_map, f_3, torch.tensor( real_tl, dtype=torch.int32) , torch.tensor( real_br, dtype=torch.int32) , torch.tensor( ren_tl, dtype=torch.int32) , torch.tensor( ren_br, dtype=torch.int32 )

    def _get_desig(self, path):
        desig = []
        with open(path) as f:
            for line in f:
                if line[-1:] == '\n':
                    desig.append(line[:-1])
                else:
                    desig.append(line)
        return desig

    def _convert_desig_to_batch_list(self, desig, lookup_desig_to_obj):
        """ only works without sequence setting """
        if self._cfg_d['batch_list_cfg']['seq_length'] == 1:
            seq_list = []
            for d in desig:
                for o in lookup_desig_to_obj[d]:

                    obj_full_path = d[:-7]
                    obj_name = o
                    index_list = []
                    index_list.append(d.split('/')[-1])
                    seq_info = [obj_name, obj_full_path, index_list]
                    seq_list.append(seq_info)
        else:
            seq_added = 0
            # this method assumes that the desig list is sorted correctly
            # only adds synthetic data if present in desig list if fixed lendth = false

            seq_list = []
            # used frames keep max length to 10000 d+str(o) is the content
            used_frames = []
            mem_size = 100 * self._cfg_d['batch_list_cfg']['seq_length']
            total = len(desig)
            start = time.time()
            for j, d in enumerate(desig[::self._cfg_d['batch_list_cfg']['sub_sample']]):
                if j % 100 == 0:
                    print(f'progress: {j}/{total} time: {time.time()-start}')
                # limit memory for faster in search
                if len(used_frames) > mem_size:
                    used_frames = used_frames[-mem_size:]

                # tries to generate s sequence out of each object in the frame
                # memorize which frames we already added to a sequence
                for o in lookup_desig_to_obj[d]:

                    if not d + '_obj_' + str(o) in used_frames:
                        # try to run down the full sequence

                        if d.find('syn') != -1:
                            # synthetic data detected
                            if not self._cfg_d['batch_list_cfg']['fixed_length']:
                                # add the frame to seq_list
                                # object_name, full_path, index_list
                                seq_info = [o, d, [d.split('/')[-1]]]
                                seq_list.append(seq_info)
                                used_frames.append(d + '_obj_' + str(o))

                                # cant add synthetic data because not in sequences

                        else:

                            # no syn data
                            seq_idx = []
                            store = False
                            used_frames_tmp = []
                            used_frames_tmp.append(d + '_obj_' + str(o))

                            seq = int(d.split('/')[1])

                            seq_idx.append(int(desig[j*self._cfg_d['batch_list_cfg']['sub_sample']].split('/')[-1]))
                            k = j*self._cfg_d['batch_list_cfg']['sub_sample']
                            while len(seq_idx) < self._cfg_d['batch_list_cfg']['seq_length']:
                                k += self._cfg_d['batch_list_cfg']['sub_sample']
                                # check if same seq or object is not present anymore
                                if k < total:
                                    if seq != int(desig[k].split('/')[1]) or not (o in lookup_desig_to_obj[desig[k]]):
                                        if self._cfg_d['batch_list_cfg']['fixed_length']:
                                            store = False
                                            break
                                        else:
                                            store = True
                                            break
                                    else:
                                        seq_idx.append(
                                            int(desig[k].split('/')[-1]))

                                        used_frames_tmp.append(
                                            desig[k] + '_obj_' + str(o))

                                        # used_frames_tmp.append(
                                        #     desig[k] + '_obj_' + str(o))
                                else:
                                    if self._cfg_d['batch_list_cfg']['fixed_length']:
                                        store = False
                                        break
                                    else:
                                        store = True
                                        break

                            if len(seq_idx) == self._cfg_d['batch_list_cfg']['seq_length']:
                                store = True

                            if store:

                                seq_info = [o, d[:-7], seq_idx]
                                seq_list.append(seq_info)
                                used_frames += used_frames_tmp
                                store = False
        return seq_list

    def _load_background(self):
        p = self._cfg_env['p_background']
        self.background = [str(p) for p in Path(p).rglob('*.jpg')]

    def _get_background_image(self):
        seed = random.choice(self.background)
        img = Image.open(seed).convert("RGB")
        w, h = img.size
        w_g, h_g = 640, 480
        if w / h < w_g / h_g:
            h = int(w * h_g / w_g)
        else:
            w = int(h * w_g / h_g)
        crop = transforms.CenterCrop((h, w))
        img = crop(img)
        img = img.resize((w_g, h_g))
        return np.array(self._trancolor_background(img))

    def _get_batch_list(self):
        """create batch list based on cfg"""
        lookup_arr = np.load(
            self._cfg_env['p_ycb_lookup_desig_to_obj'], allow_pickle=True)
        arr = np.array(['data_syn/000000', [20, 6, 2, 16, 8, 4]])[None, :]

        lookup_arr = np.concatenate([arr, lookup_arr])
        lookup_dict = {}
        for i in range(lookup_arr.shape[0]):
            lookup_dict[lookup_arr[i, 0]] = lookup_arr[i, 1]

        if self._cfg_d['batch_list_cfg']['mode'] == 'dense_fusion_test':
            desig_ls = self._get_desig(self._cfg_env['p_ycb_dense_test'])
            self._cfg_d['batch_list_cfg']['fixed_length'] = True
            self._cfg_d['batch_list_cfg']['seq_length'] = 1

        elif self._cfg_d['batch_list_cfg']['mode'] == 'dense_fusion_train':
            desig_ls = self._get_desig(self._cfg_env['p_ycb_dense_train'])
            self._cfg_d['batch_list_cfg']['fixed_length'] = True
            self._cfg_d['batch_list_cfg']['seq_length'] = 1

        elif self._cfg_d['batch_list_cfg']['mode'] == 'train':
            desig_ls = self._get_desig(self._cfg_env['p_ycb_seq_train'])

        elif self._cfg_d['batch_list_cfg']['mode'] == 'train_inc_syn':
            desig_ls = self._get_desig(self._cfg_env['p_ycb_seq_train_inc_syn'])

        elif self._cfg_d['batch_list_cfg']['mode'] == 'test':
            desig_ls = self._get_desig(self._cfg_env['p_ycb_seq_test'])
        else:
            raise AssertionError

        # this is needed to add noise during runtime
        self._syn = self._get_desig(self._cfg_env['p_ycb_syn'])
        self._real = self._get_desig(self._cfg_env['p_ycb_seq_train'])
        name = str(self._cfg_d['batch_list_cfg'])
        name = name.replace("""'""", '')
        name = name.replace(" ", '')
        name = name.replace(",", '_')
        name = name.replace("{", '')
        name = name.replace("}", '')
        name = name.replace(":", '')
        name = self._cfg_env['p_ycb_config'] + '/' + name + '.pkl'
        try:

            with open(name, 'rb') as f:
                batch_ls = pickle.load(f)
        except:
            batch_ls = self._convert_desig_to_batch_list(desig_ls, lookup_dict)

            pickle.dump(batch_ls, open(name, "wb"))
        return batch_ls

    def get_camera(self, desig, K=False, idx=False):
        """
        make this here simpler for cameras
        """

        if desig[:8] != 'data_syn' and int(desig[5:9]) >= 60:
            cx_2 = 323.7872
            cy_2 = 279.6921
            fx_2 = 1077.836
            fy_2 = 1078.189
            if K :
                return np.array([[fx_2,0,cx_2],[0,fy_2,cy_2],[0,0,1]])
            elif idx:
                return 1
            else:
                return np.array([cx_2, cy_2, fx_2, fy_2])

        else:
            cx_1 = 312.9869
            cy_1 = 241.3109
            fx_1 = 1066.778
            fy_1 = 1067.487
            if K:
                return np.array([[fx_1,0,cx_1],[0,fy_1,cy_1],[0,0,1]])
            elif idx:
                return 0
            else:
                return np.array([cx_1, cy_1, fx_1, fy_1])

    def _load_flow(self):
        self._load_rays_dir()
        self._load_meshes()

        self._max_matches = self._cfg_d.get('flow_cfg', {}).get('max_matches',1500)
        self._max_iterations =  self._cfg_d.get('flow_cfg', {}).get('max_iterations',10000)
        self._grid_x, self._grid_y = np.mgrid[0:self._h, 0:self._w]

    def _load_rays_dir(self):
        K1 = self.get_camera('data_syn/000001', K=True)
        K2 = self.get_camera('data/0068/000001',  K=True)

        self._rays_origin_real = []
        self._rays_origin_render = []
        self._rays_dir = []

        for K in [K1,K2]:
            u_cor = np.arange(0,self._h,1)
            v_cor = np.arange(0,self._w,1)
            K_inv = np.linalg.inv(K)
            rays_dir = np.zeros((self._w,self._h,3))
            nr = 0
            rays_origin_render = np.zeros((self._w,self._h,3))
            rays_origin_real = np.zeros((self._w,self._h,3))
            for u in v_cor:
                for v in u_cor:
                    n = K_inv @ np.array([u,v, 1])
                    #n = np.array([n[1],n[0],n[2]])
                    rays_dir[u,v,:] = n * 0.6 - n * 0.25
                    rays_origin_render[u,v,:] = n * 0.1
                    rays_origin_real[u,v,:] =  n * 0.25
                    nr += 1
            rays_origin_render
            self._rays_origin_real.append( np.swapaxes(rays_origin_real,0,1) )
            self._rays_origin_render.append( np.swapaxes(rays_origin_render,0,1) )
            self._rays_dir.append( np.swapaxes( rays_dir,0,1) )

    def _load_meshes(self):
        st = time.time()
        p = self._p_ycb + '/models'
        cad_models = [str(p) for p in Path(p).rglob('*scaled.obj')] #textured
        self._mesh = {}
        for pa in cad_models:
            try:
                idx = self._name_to_idx[pa.split('/')[-2]]
                self._mesh[ idx ] = trimesh.load(pa)
            except:
                pass
        # print(f'Finished loading meshes {time.time()-st}')

    def _get_pcd_cad_models(self):
        p = self._cfg_env['p_ycb_obj']
        class_file = open(p)
        cad_paths = []
        obj_idx = 1

        name_to_idx = {}
        name_to_idx_full = {}
        while 1:
            class_input = class_file.readline()
            if not class_input:
                break
            name_to_idx_full[class_input[:-1]] = obj_idx
            if self._obj_list_fil is not None:
                if obj_idx in self._obj_list_fil:
                    cad_paths.append(
                        self._cfg_env['p_ycb'] + '/models/' + class_input[:-1])
                    name_to_idx[class_input[:-1]] = obj_idx
            else:
                cad_paths.append(
                    self._cfg_env['p_ycb'] + '/models/' + class_input[:-1])
                name_to_idx[class_input[:-1]] = obj_idx

            obj_idx += 1

        if len(cad_paths) == 0:
            raise AssertionError

        cad_dict = {}

        for path in cad_paths:
            input_file = open(
                '{0}/points.xyz'.format(path))

            cld = []
            while 1:
                input_line = input_file.readline()
                if not input_line:
                    break
                input_line = input_line[:-1].split(' ')
                cld.append([float(input_line[0]), float(
                    input_line[1]), float(input_line[2])])
            cad_dict[name_to_idx[path.split('/')[-1]]] = np.array(cld)
            input_file.close()

        return cad_dict, name_to_idx, name_to_idx_full

    @ property
    def visu(self):
        return self._cfg_d['output_cfg']['visu']['status']

    @ visu.setter
    def visu(self, vis):
        self._cfg_d['output_cfg']['visu']['status'] = vis

    @ property
    def refine(self):
        return self._cfg_d['output_cfg']['refine']

    @ refine.setter
    def refine(self, refine):
        self._cfg_d['output_cfg']['refine'] = refine

def transform_mesh(mesh, H):
    """ directly operates on mesh and does not create a copy!"""
    t = np.ones((mesh.vertices.shape[0],4))
    t[:,:3] = mesh.vertices
    H[:3,:3] = H[:3,:3]
    mesh.vertices = (t @ H.T)[:,:3]
    return mesh

def rel_h (h1,h2):
    'Input numpy arrays'
    from pytorch3d.transforms import so3_relative_angle
    return so3_relative_angle(torch.tensor( h1 ) [:3,:3][None], torch.tensor( h2 ) [:3,:3][None])

def add_noise(h, nt = 0.01, nr= 30):
    h_noise =np.eye(4)
    while  True:
        h_noise[:3,:3] = R.from_euler('zyx', np.random.uniform( -nr, nr, (1, 3) ) , degrees=True).as_matrix()[0]
        h_noise[:3,:3] = h_noise[:3,:3] @ h[:3,:3]
        if abs( float( rel_h(h[:3,:3], h_noise[:3,:3])/(2* float( np.math.pi) )* 360) ) < nr:
            break
    h_noise[:3,3] = np.random.normal(loc=h[:3,3], scale=nt)
    return h_noise

def fill(data, invalid=None):
    """
    Replace the value of invalid 'data' cells (indicated by 'invalid')
    by the value of the nearest valid data cell

    Input:
        data:    numpy array of any dimension
        invalid: a binary array of same shape as 'data'. True cells set where data
                 value should be replaced.
                 If None (default), use: invalid  = np.isnan(data)

    Output:
        Return a filled array.
    """
    if invalid is None: invalid = np.isnan(data)
    ind = nd.distance_transform_edt(invalid, return_distances=False, return_indices=True)
    return data[tuple(ind)]

def backproject_points_np(p, fx=None, fy=None, cx=None, cy=None, K=None):
    """
    p.shape = (nr_points,xyz)
    """
    if not K is None:
        fx = K[0,0]
        fy = K[1,1]
        cx = K[0,2]
        cy = K[1,2]
    # true_divide
    u = ((p[:, 0] / p[:, 2]) * fx) + cx
    v = ((p[:, 1] / p[:, 2]) * fy) + cy
    return np.stack([v, u]).T

reason: import numpy,import scipy,from scipy


text:
"""
==========================================
Find the intersection of two segmentations
==========================================

When segmenting an image, you may want to combine multiple alternative
segmentations. The `skimage.segmentation.join_segmentations` function
computes the join of two segmentations, in which a pixel is placed in
the same segment if and only if it is in the same segment in _both_
segmentations.

"""
import numpy as np
from scipy import ndimage as ndi
import matplotlib.pyplot as plt

from skimage.filters import sobel
from skimage.segmentation import slic, join_segmentations
from skimage.morphology import watershed
from skimage.color import label2rgb
from skimage import data, img_as_float

coins = img_as_float(data.coins())

# make segmentation using edge-detection and watershed
edges = sobel(coins)
markers = np.zeros_like(coins)
foreground, background = 1, 2
markers[coins < 30.0 / 255] = background
markers[coins > 150.0 / 255] = foreground

ws = watershed(edges, markers)
seg1 = ndi.label(ws == foreground)[0]

# make segmentation using SLIC superpixels
seg2 = slic(coins, n_segments=117, max_iter=160, sigma=1, compactness=0.75,
            multichannel=False)

# combine the two
segj = join_segmentations(seg1, seg2)

# show the segmentations
fig, axes = plt.subplots(ncols=4, figsize=(9, 2.5), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})
axes[0].imshow(coins, cmap=plt.cm.gray, interpolation='nearest')
axes[0].set_title('Image')

color1 = label2rgb(seg1, image=coins, bg_label=0)
axes[1].imshow(color1, interpolation='nearest')
axes[1].set_title('Sobel+Watershed')

color2 = label2rgb(seg2, image=coins, image_alpha=0.5)
axes[2].imshow(color2, interpolation='nearest')
axes[2].set_title('SLIC superpixels')

color3 = label2rgb(segj, image=coins, image_alpha=0.5)
axes[3].imshow(color3, interpolation='nearest')
axes[3].set_title('Join')

for ax in axes:
    ax.axis('off')
fig.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1)
plt.show()

ext: py

max_stars_repo_name: JDWarner/scikit-image

max_issues_repo_name: JDWarner/scikit-image

max_forks_repo_name: JDWarner/scikit-image

reason: import numpy,from scipy


text:
import numpy as np

def k2e(k, E0):
    """
    Convert from k-space to energy in eV

    Parameters
    ----------
    k : float
        k value
    E0 : float
        Edge energy in eV

    Returns
    -------
    out : float
        Energy value

    See Also
    --------
    :func:`isstools.conversions.xray.e2k`
    """
    return ((1000/(16.2009 ** 2)) * (k ** 2)) + E0

def e2k(E, E0):
    """
    Convert from energy in eV to k-space

    Parameters
    ----------
    E : float
        Current energy in eV
    E0 : float
        Edge energy in eV

    Returns
    -------
    out : float
        k-space value

    See Also
    --------
    :func:`isstools.conversions.xray.k2e`
    """
    return 16.2009 * (((E - E0)/1000) ** 0.5)

def encoder2energy(encoder, pulses_per_deg, offset = 0):
    """
    Convert from encoder counts to energy in eV

    Parameters
    ----------
    encoder : float or np.array()
        Encoder counts to convert
    pulses_per_deg: float
        Number of pulses per degree of the encoder
    offset : float
        Offset in degrees to adjust the conversion

    Returns
    -------
    out : float or np.array()
        Energy value or array of values

    See Also
    --------
    :func:`isstools.conversions.xray.energy2encoder`
    """
    return -12398.42 / (2 * 3.1356 * np.sin(np.deg2rad((encoder/pulses_per_deg) - float(offset))))

def energy2encoder(energy, pulses_per_deg, offset = 0):
    """
    Convert from energy in eV to encoder counts

    Parameters
    ----------
    energy : float or np.array()
        Energy in eV to convert
    offset : float
        Offset in degrees to adjust the conversion

    Returns
    -------
    out : float or np.array()
        Encoder counts value or array of values

    See Also
    --------
    :func:`isstools.conversions.xray.encoder2energy`
    """
    return pulses_per_deg * (np.degrees(np.arcsin(-12398.42 / (2 * 3.1356 * energy))) - float(offset))

def generate_energy_grid(e0, preedge_start, xanes_start, xanes_end, exafs_end, preedge_spacing,
                        xanes_spacing, exafsk_spacing, int_time_preedge = 1, int_time_xanes = 1, int_time_exafs = 1, k_power = 0):
    preedge = np.arange(e0 + preedge_start, e0 + xanes_start, preedge_spacing)
    preedge_int = np.ones(len(preedge)) * int_time_preedge

    edge = np.arange(e0 + xanes_start, e0 + xanes_end, xanes_spacing)
    edge_int = np.ones(len(edge)) * int_time_xanes

    iterator = exafsk_spacing
    kenergy = 0
    postedge = np.array([])

    energy_end = k2e(exafs_end, e0)
    exafs_int = []
    while(kenergy + e0 + xanes_end < energy_end):
        kenergy = k2e(iterator, e0) - e0

        postedge = np.append(postedge, e0 + xanes_end + kenergy)
        k_current = e2k(e0 + xanes_end + kenergy,e0)
        exafs_int.append(int_time_exafs * (k_current ** k_power))
        iterator += exafsk_spacing

    integration_times = np.append(np.append(preedge_int, edge_int), np.array(exafs_int))
    grid = np.append(np.append(preedge, edge), postedge)
    return grid, integration_times
    #return np.append(np.append(preedge, edge), postedge)

max_stars_repo_name: mrakitin/xas

max_issues_repo_name: mrakitin/xas

max_forks_repo_name: mrakitin/xas

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 07 15:23:12 2017

This script extracts the values from all sheets in a spreadsheet and
copies them in to another spreadsheet. No other information (formulas,
formatting, etc.) is copied over, unfortunately I do not know if that is
possible.

It was created to mine the hidden data from the EFT spredsheets.

@author: edward.barratt
"""

import os
import subprocess
from shutil import copyfile
import win32com.client as win32
import numpy as np

import EFT_Tools as tools

# The auto hot key paths, so that the initial warning window on the EFT can be
# closed automatically.
ahk_exepath = 'C:\Program Files\AutoHotkey\AutoHotkey.exe'
ahk_ahkpath = 'closeWarning.ahk'

InputFile = 'original\EFT2017_v8.0_original.xlsb'
OutputFile = 'output\EFT2017_v8.0_Extracted.xlsx'

# Create a temporary copy so that we don't risk changing the original.
infn, inex = os.path.splitext(InputFile)
InputFileTemp = infn + 'TEMP' + inex
copyfile(InputFile, InputFileTemp)
InputFileTemp = os.path.abspath(InputFileTemp) # Neccesary because win32 seems
OutputFile = os.path.abspath(OutputFile)       # to struggle with relative
                                               # paths.

# Open a excel application instance, and open both workbooks.
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
subprocess.Popen([ahk_exepath, ahk_ahkpath])
wb1 = excel.Workbooks.Open(InputFileTemp)
wb2 = excel.Workbooks.Add()
wb2.SaveAs(OutputFile)

shCount = wb1.Sheets.Count
print("Number of sheets: {}".format(shCount))

for shi in reversed(range(shCount)):
  sh = wb1.Sheets[shi+1]
  print('Copying the contents of sheet {}: {}'.format(shi+1, sh.Name))
  ws1 = wb1.Worksheets(sh.Name)
  ws2 = wb2.Worksheets.Add()
  ws2.Name = sh.Name
  # Check chunks that are 100 by 100 at a time, until we find the extent of the data.
  b = 100
  X1, X2 = 1, b
  Y1, Y2 = 1, b
  FoundEndX, FoundEndY = False, False
  while not FoundEndY:
    dataCheck = ws1.Range('{}{}:{}{}'.format(tools.numToLetter(X1), Y1, tools.numToLetter(X2), Y2)).Value
    dataCheck = np.array(dataCheck)
    if np.sum(np.equal(dataCheck, None)) == np.size(dataCheck):
      YMin = 1
      YMax = Y2
      FoundEndY = True
    elif Y2 > 10000:
      FoundEndY = True
    else:
      Y1 = Y2 + 1
      Y2 = Y2 + b
  while not FoundEndX:
    dataCheck = ws1.Range('{}{}:{}{}'.format(tools.numToLetter(X1), YMin, tools.numToLetter(X2), YMax)).Value
    dataCheck = np.array(dataCheck)
    if np.sum(np.equal(dataCheck, None)) == np.size(dataCheck):
      XMin = 1
      XMax = X2
      FoundEndX = True
    elif X2 > 10000:
      FoundEndX = True
    else:
      X1 = X2 + 1
      X2 = X2 + b
  # Now we've found the full extent, copy the values to the new workbook.
  RangeExtent = '{}{}:{}{}'.format(tools.numToLetter(XMin), YMin, tools.numToLetter(XMax), YMax)
  print('    Extent: {}'.format(RangeExtent))
  ws2.Range(RangeExtent).Value = ws1.Range(RangeExtent).Value

wb1.Save()
wb1.Close()
os.remove(InputFileTemp)
wb2.Save()
wb2.Close()
excel.Quit()

max_stars_repo_name: OceanMetSEPA/extractfromeft

max_issues_repo_name: OceanMetSEPA/extractfromeft

max_forks_repo_name: OceanMetSEPA/extractfromeft


text:
import numpy as np
import pytest

from sparsely import ClassificationDataGenerator, RegressionDataGenerator
from sparsely.data_generator import BaseDataGenerator

def validate_data_generator(
        data_generator: BaseDataGenerator,
        return_weights: bool,
        shuffle_features: bool,
        random_state: bool
):
    if return_weights:
        X, y, weights = data_generator.sample(
            shuffle_features=shuffle_features,
            return_weights=return_weights,
            random_state=random_state
        )

        # Check weights
        assert weights.shape[0] == (data_generator.n_features,)
        assert weights.dtype == float
        assert not np.isnan(weights).any()
        if shuffle_features:
            assert (~np.isclose(weights, 0)).sum() == data_generator.n_informative
        else:
            assert not np.isclose(weights[:data_generator.n_informative], 0).any()
            assert np.isclose(weights[data_generator.n_informative:], 0).all()

    else:
        X, y = data_generator.sample(
            shuffle_features=shuffle_features,
            return_weights=return_weights,
            random_state=random_state
        )

    # Check dataset
    assert X.shape == (data_generator.n_samples, data_generator.n_features)
    assert y.shape == (data_generator.n_samples,)
    assert X.dtype == float
    assert y.dtype == float
    assert not np.isnan(X).any()
    assert not np.isnan(y).any()

@pytest.mark.parametrize("return_weights", [True, False])
@pytest.mark.parametrize("shuffle_features", [True, False])
@pytest.mark.parametrize("random_state", range(5))
def test_regression_data_generator(
        regression_data_generator: RegressionDataGenerator,
        return_weights: bool,
        shuffle_features: bool,
        random_state: bool
):
    validate_data_generator(
        data_generator=regression_data_generator,
        return_weights=return_weights,
        shuffle_features=shuffle_features,
        random_state=random_state
    )

@pytest.mark.parametrize("return_weights", [True, False])
@pytest.mark.parametrize("shuffle_features", [True, False])
@pytest.mark.parametrize("random_state", range(5))
def test_classification_data_generator(
        classification_data_generator: ClassificationDataGenerator,
        return_weights: bool,
        shuffle_features: bool,
        random_state: bool
):
    validate_data_generator(
        data_generator=classification_data_generator,
        return_weights=return_weights,
        shuffle_features=shuffle_features,
        random_state=random_state
    )

max_stars_repo_name: jivanhoe/sparsely

max_issues_repo_name: jivanhoe/sparsely

max_forks_repo_name: jivanhoe/sparsely


text:
import pytest
import xarray as xr
import tensorflow as tf
import numpy as np
from datetime import timedelta
from cftime import DatetimeProlepticGregorian as datetime
from fv3fit.keras._models.recurrent import PureKerasModel, _BPTTTrainer as _BPTTTrainer
import tempfile
import fv3fit
import os
import subprocess

@pytest.fixture
def sample_dim_name():
    return "sample"

@pytest.fixture
def dt():
    return timedelta(seconds=1)

@pytest.fixture(params=["limiter", "no_limiter"])
def use_moisture_limiter(request):
    if request.param == "limiter":
        return True
    elif request.param == "no_limiter":
        return False
    else:
        raise NotImplementedError(request.param)

def get_train_dataset(sample_dim_name, dt):
    n_samples = 6
    n_times = 12
    n_z = 16
    t_start = datetime(2020, 1, 1)
    ds = xr.Dataset(
        data_vars={
            "air_temperature": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
                attrs={"units": "K"},
            ),
            "specific_humidity": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
                attrs={"units": "kg/kg"},
            ),
            "air_temperature_tendency_due_to_model": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
            ),
            "specific_humidity_tendency_due_to_model": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
            ),
            "air_temperature_tendency_due_to_nudging": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
            ),
            "specific_humidity_tendency_due_to_nudging": xr.DataArray(
                np.random.randn(n_samples, n_times, n_z),
                dims=[sample_dim_name, "time", "z"],
            ),
            "a": xr.DataArray(
                np.random.randn(n_samples, n_times), dims=[sample_dim_name, "time"],
            ),
            "b": xr.DataArray(
                np.random.randn(n_samples, n_times), dims=[sample_dim_name, "time"],
            ),
        },
        coords={"time": np.asarray([t_start + i * dt for i in range(n_times)])},
    )
    return ds

def get_model(sample_dim_name, use_moisture_limiter):
    return _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
        use_moisture_limiter=use_moisture_limiter,
    )

def test_predict_model_gives_tendency_of_train_model(
    sample_dim_name, dt, use_moisture_limiter
):
    np.random.seed(0)
    tf.random.set_seed(1)
    train_dataset = get_train_dataset(sample_dim_name, dt)
    model = get_model(sample_dim_name, use_moisture_limiter)
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    train_dataset["air_temperature_tendency_due_to_model"][:] = 0.0
    train_dataset["specific_humidity_tendency_due_to_model"][:] = 0.0

    air_temperature, specific_humidity = model.train_keras_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )
    dQ1, dQ2 = model.train_tendency_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )

    tendency_ds = model.predictor_model.predict(train_dataset.isel(time=0))

    # take difference of first output from initial input state
    Q1_train_tendency = (
        air_temperature[:, 0, :] - train_dataset["air_temperature"][:, 0, :]
    ) / (dt.total_seconds())
    Q2_train_tendency = (
        specific_humidity[:, 0, :] - train_dataset["specific_humidity"][:, 0, :]
    ) / (dt.total_seconds())

    np.testing.assert_allclose(dQ1[:, 0, :], tendency_ds["dQ1"].values, atol=1e-6)
    np.testing.assert_allclose(dQ2[:, 0, :], tendency_ds["dQ2"].values, atol=1e-6)
    np.testing.assert_allclose(dQ1[:, 0, :], Q1_train_tendency, atol=1e-6)
    np.testing.assert_allclose(dQ2[:, 0, :], Q2_train_tendency, atol=1e-6)

def test_fit_bptt_command_line(regtest, sample_dim_name, dt):
    config_text = """
regularizer:
  name: l2
  kwargs:
    l: 0.0001
optimizer:
  name: Adam
  kwargs:
    learning_rate: 0.001
    amsgrad: True
    clipnorm: 1.0
hyperparameters:
  n_hidden_layers: 3
  n_units: 256
  train_batch_size: 48
  use_moisture_limiter: true
  state_noise: 0.25
input_variables:
  - a
  - b
decrease_learning_rate_epoch: 1
decreased_learning_rate: 0.0001
total_epochs: 2
random_seed: 0
"""
    np.random.seed(0)
    training_dataset = get_train_dataset(sample_dim_name, dt)
    with tempfile.TemporaryDirectory() as tmpdir:
        arrays_dir = os.path.join(tmpdir, "arrays")
        os.mkdir(arrays_dir)
        for i in range(5):
            filename = os.path.join(arrays_dir, f"arrays_{i}.nc")
            training_dataset.to_netcdf(filename)
        config_filename = os.path.join(tmpdir, "config.yaml")
        with open(config_filename, "w") as f:
            f.write(config_text)
        outdir = os.path.join(tmpdir, "output")
        subprocess.check_call(
            ["python", "-m", "fv3fit.train_bptt", arrays_dir, config_filename, outdir]
        )
        # as a minimum, test that output exists
        assert os.path.isdir(outdir)
        assert len(os.listdir(outdir)) > 0

        # one model per epoch is saved
        for sample_model_dir in sorted(os.listdir(outdir)):
            loaded = fv3fit.load(os.path.join(outdir, sample_model_dir))

            first_timestep = training_dataset.isel(time=0)
            loaded_output = loaded.predict(first_timestep)

            assert isinstance(loaded_output, xr.Dataset)

def test_train_model_uses_correct_given_tendency(sample_dim_name, dt):
    train_dataset = get_train_dataset(sample_dim_name, dt)
    np.random.seed(0)
    tf.random.set_seed(1)
    model = _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
    )
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    assert not np.all(
        train_dataset["air_temperature_tendency_due_to_model"].values == 0.0
    )

    air_temperature, specific_humidity = model.train_keras_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )
    dQ1, dQ2 = model.train_tendency_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )

    # take difference of first output from initial input state
    Q1_train_tendency = (
        air_temperature[:, 0, :] - train_dataset["air_temperature"][:, 0, :]
    ) / (dt.total_seconds())
    Q2_train_tendency = (
        specific_humidity[:, 0, :] - train_dataset["specific_humidity"][:, 0, :]
    ) / (dt.total_seconds())

    np.testing.assert_allclose(
        Q1_train_tendency - dQ1[:, 0, :],
        train_dataset["air_temperature_tendency_due_to_model"][:, 0, :].values,
        atol=1e-6,
    )
    np.testing.assert_allclose(
        Q2_train_tendency - dQ2[:, 0, :],
        train_dataset["specific_humidity_tendency_due_to_model"][:, 0, :].values,
        atol=1e-6,
    )

    # check for second timestep also
    Q1_train_tendency = (air_temperature[:, 1, :] - air_temperature[:, 0, :]) / (
        dt.total_seconds()
    )
    Q2_train_tendency = (specific_humidity[:, 1, :] - specific_humidity[:, 0, :]) / (
        dt.total_seconds()
    )

    np.testing.assert_allclose(
        Q1_train_tendency - dQ1[:, 1, :],
        train_dataset["air_temperature_tendency_due_to_model"][:, 1, :].values,
        atol=1e-6,
    )
    np.testing.assert_allclose(
        Q2_train_tendency - dQ2[:, 1, :],
        train_dataset["specific_humidity_tendency_due_to_model"][:, 1, :].values,
        atol=1e-6,
    )

def test_predict_model_gives_different_tendencies(sample_dim_name, dt):
    train_dataset = get_train_dataset(sample_dim_name, dt)
    model = _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
    )
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    tendency_ds = model.predictor_model.predict(train_dataset.isel(time=0))
    assert not np.all(tendency_ds["dQ1"].values == tendency_ds["dQ2"].values)

def test_train_model_gives_different_outputs(sample_dim_name, dt):
    train_dataset = get_train_dataset(sample_dim_name, dt)
    model = _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
    )
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    air_temperature, specific_humidity = model.train_keras_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )
    assert not np.all(air_temperature == specific_humidity)

def test_integrate_stepwise(sample_dim_name, dt):
    """
    We need an integrate_stepwise routine for scripts that evaluate the model
    timeseries, since the saved model operates on a single timestep. This code
    is hard to write for a variety of reasons.

    The test here makes sure that the integration code performs an identical
    integration to the one used to train the model.

    train_keras_model does this by predicting a timeseries
    directly, while integrate_stepwise integrates the stepwise model over
    many timesteps.
    """
    train_dataset = get_train_dataset(sample_dim_name, dt)
    np.random.seed(0)
    tf.random.set_seed(1)
    model = _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
    )
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    air_temperature, specific_humidity = model.train_keras_model.predict(
        x=model.get_keras_inputs(train_dataset)
    )
    out_ds = model.predictor_model.integrate_stepwise(train_dataset)
    np.testing.assert_allclose(
        out_ds["air_temperature"].values[:, 0, :], air_temperature[:, 0, :], atol=1e-6
    )
    np.testing.assert_allclose(
        out_ds["specific_humidity"].values[:, 0, :],
        specific_humidity[:, 0, :],
        atol=1e-6,
    )
    np.testing.assert_allclose(
        out_ds["air_temperature"].values[:, 1, :], air_temperature[:, 1, :], atol=1e-6
    )
    np.testing.assert_allclose(
        out_ds["specific_humidity"].values[:, 1, :],
        specific_humidity[:, 1, :],
        atol=1e-6,
    )
    np.testing.assert_allclose(
        out_ds["air_temperature"].values, air_temperature, atol=1e-5
    )
    np.testing.assert_allclose(
        out_ds["specific_humidity"].values, specific_humidity, atol=1e-5
    )
    assert not np.all(air_temperature == specific_humidity)

def test_reloaded_model_gives_same_outputs(sample_dim_name, dt):
    train_dataset = get_train_dataset(sample_dim_name, dt)
    model = _BPTTTrainer(
        sample_dim_name,
        ["a", "b"],
        n_units=32,
        n_hidden_layers=4,
        kernel_regularizer=None,
        train_batch_size=48,
        optimizer="adam",
    )
    model.fit_statistics(train_dataset)
    model.fit(train_dataset)

    with tempfile.TemporaryDirectory() as tmpdir:
        fv3fit.dump(model.predictor_model, tmpdir)
        loaded = fv3fit.load(tmpdir)

    first_timestep = train_dataset.isel(time=0)
    reference_output = model.predictor_model.predict(first_timestep)
    # test that loaded model gives the same predictions
    loaded_output = loaded.predict(first_timestep)
    xr.testing.assert_equal(reference_output, loaded_output)

class DummyPredictor:
    def __init__(self, sample_dim, input_vars, output_vars):
        self.sample_dim_name = sample_dim
        self.input_variables = input_vars
        self.output_variables = output_vars

    def predict(self, X: np.ndarray):
        return X

zdim, ydim, xdim = 2, 3, 3
da_3d = xr.DataArray(
    [[[1.0 for x in range(xdim)] for y in range(ydim)] for z in range(zdim)],
    dims=["z", "y", "x"],
    coords={"y": range(ydim), "x": range(xdim)},
)

zdim, nsamples = 2, 9
da_stacked = xr.DataArray(
    [[1.0 for s in range(nsamples)] for z in range(zdim)],
    dims=["z", "sample"],
    coords={"sample": range(nsamples), "z": range(zdim)},
)

@pytest.mark.parametrize(
    "da,",
    [
        pytest.param(da_3d, id="unstacked_input"),
        pytest.param(da_stacked, id="stacked_input"),
    ],
)
def test_pure_keras_predict_works_on_format(da):
    input_vars, output_vars = ["input"], ["output"]
    dummy_predictor = DummyPredictor("sample", input_vars, output_vars)
    model = PureKerasModel(
        input_vars,
        output_vars,
        output_metadata=[
            {"dims": ["sample", "z"], "units": ""},
            {"dims": ["sample", "z"], "units": ""},
        ],
        model=dummy_predictor,
    )
    ds = xr.Dataset({"input": da, "output": da})
    prediction = model.predict(ds)
    for dim in prediction.dims:
        assert len(prediction[dim]) == len(ds[dim])


text:
from astropy.modeling import models, fitting
import matplotlib.pyplot as plt
from . import plots
import numpy as np
import pdb

def fit1d(xdata,zdata,degree=1,reject=0,ydata=None,plot=None,plot2d=False,xr=None,yr=None,zr=None,xt=None,yt=None,zt=None,pfit=None,log=False,colorbar=False,size=5) :
    """
    Do a 1D polynomial fit to data set and plot if requested

    Args:
        xdata : independent variable
	zdata : dependent variable to be fit

    Keyword args:
        degree: degree of polynomial to fit (default=1 for linear fit)
        reject : single iteration rejection of points that deviate from initial by more than specified value (default=0, no rejection)
	ydata : auxiliary variable for plots (default=None)
        plot  : axes to plot into (default=None)
        plot2d (bool)  : set to make a 2D plot with auxiliary variable, rather than 1D color-coded by auxiliary variable
        xr[2] : xrange for plot
        yr[2] : yrange for plot
        zr[2] : zrange for plot
        xt    : xtitle for plot
        yt    : ytitle for plot
        zt    : ztitle for plot

    Returns :
        pfit  : 1D polynomial fit
    """

    # set up fitter and do fit
    if pfit is None :
        fit_p = fitting.LinearLSQFitter()
        p_init = models.Polynomial1D(degree=degree)
        pfit = fit_p(p_init, xdata, zdata)
        # rejection of points?
        if reject > 0 :
            gd=np.where(abs(zdata-pfit(xdata)) < reject)[0]
            bd=np.where(abs(zdata-pfit(xdata)) >= reject)[0]
            print('rejected ',len(xdata)-len(gd),' of ',len(xdata),' points')
            pfit = fit_p(p_init, xdata[gd], zdata[gd])

    print('1D rms: ',(zdata-pfit(xdata)).std())

    # plot if requested
    if plot is not None :
        if xr is None : xr = [xdata.min(),xdata.max()]
        if yr is None and ydata is not None : yr = [ydata.min(),ydata.max()]
        if log :
           zplot=10.**zdata
        else :
           zplot=zdata
        if zr is None : zr = [zplot.min(),zplot.max()]
        if ydata is None :
            x = np.linspace(xr[0],xr[1],200)
            if log :
               zfit=10.**pfit(x)
            else :
               zfit=pfit(x)
            # straight 1D plot
            plots.plotp(plot,xdata,zplot,xr=xr,yr=yr,zr=zr,
                   xt=xt,yt=yt,size=size)
            plots.plotl(plot,x,zfit)
        elif plot2d :
            # 2D image plot with auxiliary variable
            y, x = np.mgrid[yr[1]:yr[0]:200j, xr[1]:xr[0]:200j]
            if log :
               zfit=10.**pfit(x)
            else :
               zfit=pfit(x)
            plots.plotc(plot,xdata,ydata,zplot,xr=xr,yr=yr,zr=zr,
                   xt=xt,yt=xt,zt=yt,colorbar=True,size=size,cmap='rainbow')
            plot.imshow(zfit,extent=[xr[1],xr[0],yr[1],yr[0]],
                aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
        else :
            # 1D plot color-coded by auxiliary variable
            x = np.linspace(xr[0],xr[1],200)
            if log :
               zfit=10.**pfit(x)
            else :
               zfit=pfit(x)
            plots.plotc(plot,xdata,zplot,ydata,xr=xr,yr=zr,zr=yr,
                   xt=xt,yt=yt,zt=zt,size=size,colorbar=colorbar)
            plots.plotl(plot,x,zfit,color='k')
    return pfit

def fit2d(xdata,ydata,zdata,degree=1,reject=0,plot=None,xr=None,yr=None,zr=None,xt=None,yt=None,zt=None,gdrange=None,pfit=None,log=False,size=5) :
    """
    Do a 2D polynomial fit to data set and plot if requested

    Args:
        xdata : independent variable
        ydata : independent variable
	zdata : dependent variable to be fit

    Keyword args:
        degree: degree of polynomial to fit (default=1 for linear fit)
        reject : single iteration rejection of points that deviate from initial by more than specified value (default=0, no rejection)
	ydata : auxiliary variable for plots (default=None)
        plot  : axes to plot into (default=None)
        xr[2] : xrange for plot
        yr[2] : yrange for plot
        zr[2] : zrange for plot
        xt    : xtitle for plot
        yt    : ytitle for plot
        zt    : ztitle for plot

    Returns :
        pfit  : 2D polynomial fit
    """

    if gdrange is not None :
        gd = np.where((zdata > gdrange[0]) & (zdata < gdrange[1]))[0]
        xfit = xdata[gd]
        yfit = ydata[gd]
        zfit = zdata[gd]
    else :
        xfit = xdata
        yfit = ydata
        zfit = zdata

    # set up fitter and do fit
    if pfit is None :
        fit_p = fitting.LinearLSQFitter()
        p_init = models.Polynomial2D(degree=degree)
        pfit = fit_p(p_init, xfit, yfit, zfit)
        # rejection of points?
        if reject > 0 :
            gd=np.where(abs(zfit-pfit(xfit,yfit)) < reject)[0]
            bd=np.where(abs(zfit-pfit(xfit,yfit)) >= reject)[0]
            print('rejected ',len(xdata)-len(gd),' of ',len(xdata),' points')
            pfit = fit_p(p_init, xfit[gd], yfit[gd], zfit[gd])

    print('2D rms: ',(zfit-pfit(xfit,yfit)).std())

    if plot is not None :
        if log :
            zfit = 10.**zfit
        if xr is None : xr = [xfit.min(),xfit.max()]
        if yr is None : yr = [yfit.min(),yfit.max()]
        if zr is None : zr = [zfit.min(),zfit.max()]
        # plot data
        plots.plotc(plot,xfit,yfit,zfit,xr=xr,yr=yr,zr=zr,
                xt=xt,yt=yt,zt=zt,colorbar=True,size=size,linewidth=1)
        # create independent variable grid for model and display
        y, x = np.mgrid[yr[1]:yr[0]:200j, xr[1]:xr[0]:200j]
        if log :
            plot.imshow(10.**pfit(x,y),extent=[xr[1],xr[0],yr[1],yr[0]],
                aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
        else :
            plot.imshow(pfit(x,y),extent=[xr[1],xr[0],yr[1],yr[0]],
                aspect='auto',vmin=zr[0],vmax=zr[1], origin='lower',cmap='rainbow')
        #plt.show()

    return pfit

def linear(data,design,err=None,reject=0) :
    '''
    Given data array (npts) and design matrix (npar, npts), return linear solution
    '''
    npar=design.shape[0]
    npts=design.shape[1]
    if err is None:
        err = np.ones([npts])

    ATA = np.dot(design, design.T / err[:, None]**2)
    soln = np.linalg.solve(ATA, np.dot(design, data / err**2))
    inv = np.linalg.inv(ATA)

    #alpha=np.zeros([npar,npar])
    #beta=np.zeros([npar])
    #for ipar in range(npar) :
    #    beta[ipar]+=(data*design[ipar,:]/err**2).sum()
    #    for jpar in range(npar) :
    #        alpha[ipar,jpar]+=(design[ipar,:]*design[jpar,:]/err**2).sum()
    #c=np.linalg.inv(alpha)
    #soln=np.dot(c,beta)
    #pdb.set_trace()

    return soln, inv

max_stars_repo_name: holtzmanjon/tools

max_issues_repo_name: sdss/apogee

max_forks_repo_name: sdss/apogee

reason: import numpy,from astropy


text:
#
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# 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.
#

import ctypes
import numpy as np
import tensorrt as trt
import pycuda.autoinit
import pycuda.driver as cuda

soFilePath = "./CCLPlugin.so"
height = 384
width = 640
np.random.seed(97)

def getCCLPlugin():
    for c in trt.get_plugin_registry().plugin_creator_list:
        if c.name == 'CCLPlugin':
            p0 = trt.PluginField("minPixelScore", np.array([0.7], dtype=np.float32), trt.PluginFieldType.FLOAT32)
            p1 = trt.PluginField("minLinkScore", np.array([0.7], dtype=np.float32), trt.PluginFieldType.FLOAT32)
            p2 = trt.PluginField("minArea", np.array([10], dtype=np.int32), trt.PluginFieldType.INT32)
            p3 = trt.PluginField("maxcomponentCount", np.array([65536], dtype=np.int32), trt.PluginFieldType.INT32)
            return c.create_plugin(c.name, trt.PluginFieldCollection([p0, p1, p2, p3]))
    return None

def buildEngine(logger):
    builder = trt.Builder(logger)
    builder.max_batch_size = 1
    builder.max_workspace_size = 3 << 30
    builder.fp16_mode = False
    network = builder.create_network()

    inputT0 = network.add_input('pixelScore', trt.float32, (height, width))
    inputT1 = network.add_input('linkScore', trt.float32, (8, height, width))
    cclLayer = network.add_plugin_v2([inputT0, inputT1], getCCLPlugin())

    network.mark_output(cclLayer.get_output(0))
    network.mark_output(cclLayer.get_output(1))
    return builder.build_cuda_engine(network)

def run():
    logger = trt.Logger(trt.Logger.ERROR)
    trt.init_libnvinfer_plugins(logger, '')
    ctypes.cdll.LoadLibrary(soFilePath)

    engine = buildEngine(logger)
    if engine == None:
        print("Failed building engine!")
        return None
    print("Succeeded building engine!")

    context = engine.create_execution_context()
    stream = cuda.Stream()
    inputH0 = np.ascontiguousarray(np.random.rand(height, width).reshape(-1))
    inputH1 = np.ascontiguousarray(np.random.rand(8, height, width).reshape(-1))
    inputD0 = cuda.mem_alloc(inputH0.nbytes)
    inputD1 = cuda.mem_alloc(inputH1.nbytes)
    outputH0 = np.empty(context.get_binding_shape(2), dtype=trt.nptype(engine.get_binding_dtype(2)))
    outputH1 = np.empty(context.get_binding_shape(3), dtype=trt.nptype(engine.get_binding_dtype(3)))
    outputD0 = cuda.mem_alloc(outputH0.nbytes)
    outputD1 = cuda.mem_alloc(outputH1.nbytes)

    cuda.memcpy_htod_async(inputD0, inputH0, stream)
    cuda.memcpy_htod_async(inputD1, inputH1, stream)
    stream.synchronize()
    context.execute_async(1, [int(inputD0), int(inputD1), int(outputD0), int(outputD1)], stream.handle)
    stream.synchronize()
    cuda.memcpy_dtoh_async(outputH0, outputD0, stream)
    cuda.memcpy_dtoh_async(outputH1, outputD1, stream)
    stream.synchronize()

    print(np.shape(outputH0), np.shape(outputH1))
    #print(outputH0)
    #print(outputH1)

if __name__ == '__main__':
    run()
    print("test finish!")

max_stars_repo_name: Jie-Fang/trt-samples-for-hackathon-cn

max_issues_repo_name: Jie-Fang/trt-samples-for-hackathon-cn

max_forks_repo_name: Jie-Fang/trt-samples-for-hackathon-cn

reason: import numpy,import pycuda


text:
import h5py
import os
import numpy as np

dim = 12288

base = '/home/yliu0/data/'
pin= os.path.join(base, 'logos.hdf5')
out = os.path.join(base, './latent/latent{}.h5'.format(dim))

num_chns = 3
img_size = 64

if __name__ == '__main__':
    # clean
    if os.path.exists(out):
        os.remove(out)

    fin = h5py.File(pin, 'r')
    din = fin['logos']

    f = h5py.File(out, 'w')
    dset = f.create_dataset('latent', (1, dim),
            chunks=(1, dim),
            maxshape=(None, dim),
            dtype='u1')

    l = din.shape[0]
    din = np.reshape(din, (l, dim))
    print din.shape
    for i, val in enumerate(din):
        dset.resize((i + 1, dim))
        dset[i] = val
        f.flush()

    f.close()
    fin.close()

max_stars_repo_name: yyyliu/latent-space-cartography

max_issues_repo_name: yyyliu/latent-space-cartography

max_forks_repo_name: yyyliu/latent-space-cartography


text:
import os

from torch.functional import norm
from options.test_options import TestOptions
from data import CreateDataLoader
from models import create_model
from util.visualizer import save_images
from util import html
from util import util
import torch
from models import utils as mutils
from data import human36m_skeleton
import math
import itertools
import torch
import torchvision
from collections import defaultdict
import numpy as np
import time
import re
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import lpips
#from skimage.measure import compares_ssim as ssim

if __name__ == '__main__':
    opt = TestOptions().parse()
    opt.num_test = 1000000
    num_save = opt.num_test_save

    # load data
    data_loader = CreateDataLoader(opt)
    dataset = data_loader.load_data()
    print('Created dataset with %d samples' % len(dataset))

    # setup model
    model = create_model(opt)
    model.setup(opt)

    # create a website
    web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.iteration))
    webpage = html.HTML(web_dir, '%s, Phase = %s, Iteration = %s' % (
        opt.name, opt.phase, opt.iteration))

    if opt.eval:
        model.eval()

    lpips_net = lpips.LPIPS(net='alex')
    lpips_net.cuda()

    ############################################################################
    def format_results_per_activity(results):
        """
        results dict {activity: result}
        """
        s = ''
        for activity, result in sorted(results.items()):
            s += '%s: %.4f ' % (activity, result)
        return s

    def format_results_per_activity2(results):
        """
        results dict {activity: result}
        """
        order_full = ['waiting', 'posing', 'greeting', 'directions', 'discussion', 'walking',
                'eating', 'phone_call', 'purchases', 'sitting', 'sitting_down', 'smoking',
                'taking_photo', 'walking_dog', 'walking_together']
        order_yutig = ['Waiting', 'Posing', 'Greeting', 'Directions', 'Discussion', 'Walking']

        if set(order_full) == set(results.keys()):
            order = order_full
        elif set(order_yutig) == set(results.keys()):
            order = order_yutig
        else:
            raise ValueError()

        numbers = ['%.4f' % results[k] for k in order]
        return '\t'.join(order), '\t'.join(numbers)

    def mean_per_activity(distances, paths, path_fn):
        activities = [path_fn(p) for p in paths]
        return mean_distance_per_activity(distances, activities)

    def human36m_path_to_activity(path):
        return path.split(os.path.sep)[-5]

    def y_human36m_path_to_activity(path):
        return re.split('\s|\.', path.split(os.path.sep)[-2])[0]

    def mean_distance_per_activity(distances, activities):
        d = defaultdict(list)
        for distance, activity in zip(distances, activities):
            d[activity].append(distance)
        means = {}
        for activity, values in d.items():
            means[activity] = np.mean(values)
        return means

    def compute_reconstruction_loss(prediction, target):
        # normalize predicted image to -1, 1
        normalized_prediction = (prediction - prediction.min())/(prediction.max() - prediction.min())
        normalized_prediction = 2*normalized_prediction - 1
        lpips_loss = lpips_net(normalized_prediction, target)
        #ssim_loss = ssim(normalized_prediction, target, channel_axis = 1, data_range = target.max() - target.min())
        return lpips_loss

    def compute_mean_distance(input, target, correspondeces=None,
                              target_correspondeces=None, used_points=None,
                              offline_prediction=None):
        if target_correspondeces is not None:
            target_swapped = mutils.swap_points(target, target_correspondeces)
        else:
            target_swapped = target.clone()
        if correspondeces is not None:
            input_swapped = mutils.swap_points(input, correspondeces)
            if offline_prediction is not None:
                offline_prediction_swapped = mutils.swap_points(offline_prediction, correspondeces)
        else:
            input_swapped = input.clone()
            if offline_prediction is not None:
                offline_prediction_swapped = offline_prediction.clone()

        if used_points is not None:
            input = input[:, used_points]
            input_swapped = input_swapped[:, used_points]
            if offline_prediction is not None:
                offline_prediction = offline_prediction[:, used_points]
            target = target[:, used_points]
            target_swapped = target_swapped[:, used_points]

        # offline
        if offline_prediction is not None:
            distance = mutils.mean_l2_distance(offline_prediction, input)
            swapped_distance = mutils.mean_l2_distance(offline_prediction, input_swapped)
            min_idx = distance > swapped_distance
            for i in range(len(min_idx)):
                if min_idx[i]:
                    input[i] = input_swapped[i]

        distance = mutils.mean_l2_distance(target, input)
        swapped_distance = mutils.mean_l2_distance(target_swapped, input)
        correct_flip = distance < swapped_distance
        min_distance = torch.min(distance, swapped_distance)

        return distance, min_distance, correct_flip

    def normalize_points(points):
        return (points + 1) / 2.0

    def points_to_original(points, height, width, height_ratio, width_ratio):
        """
        points: B x N x 2
        """
        points *= torch.tensor([[[height, width]]], dtype=torch.float32, device=points.device)
        points /= torch.stack([height_ratio, width_ratio], dim=-1, )[:, None].to(points.device)
        return points

    if opt.used_points == 'simple_links':
        used_points = set()
        for a, b in human36m_skeleton.simple_link_indices:
            used_points.add(a)
            used_points.add(b)
        used_points = list(used_points)
        used_points = sorted(used_points)
    elif opt.used_points == 'original':
        used_points = sorted(list(human36m_skeleton.official_eval_indices.values()))
    elif opt.used_points in ['all']:
        used_points = None
    else:
        raise ValueError()

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

    distances = []
    distances_min = []
    correct_flips = []
    paths = []
    lpips_losses = []

    if opt.paired_skeleton_type in ['human36m', 'human36m_simple2']:
        target_correspondeces = human36m_skeleton.get_lr_correspondences()
    else:
        target_correspondeces = None

    if opt.skeleton_type in ['human36m', 'human36m_simple2']:
        correspondeces = human36m_skeleton.get_lr_correspondences()
    else:
        correspondeces = None

    n_batches = int(math.ceil(float(len(dataset)) / opt.batch_size))

    save_frq = int(math.ceil(float(min(n_batches, opt.num_test)) / num_save))

    avg_time = []

    path_fn = human36m_path_to_activity
    if opt.dataset_mode == 'simplehuman36m':
        path_fn = y_human36m_path_to_activity

    iter_start_time = time.time()
    for i, data in enumerate(dataset):
        if i >= opt.num_test:
            break

        model.set_input(data)
        model.test()

        img_path = model.get_image_paths()

        prediction = model.regressed_points
        if hasattr(model, 'offline_regressed_points'):
            offline_prediction = model.offline_regressed_points
        else:
            offline_prediction = None
        target = model.paired_B_points

        prediction = normalize_points(prediction)
        if offline_prediction is not None:
            offline_prediction = normalize_points(offline_prediction)
        target = normalize_points(target)

        if opt.error_form == 'original':
            height_ratio = model.input['height_ratio']
            width_ratio = model.input['width_ratio']
            original_landmarks = model.input['landmarks']
            prediction = points_to_original(
                prediction, opt.fineSize, opt.fineSize, height_ratio, width_ratio)
            if offline_prediction is not None:
                offline_prediction = points_to_original(
                    offline_prediction, opt.fineSize, opt.fineSize, height_ratio, width_ratio)
            target = points_to_original(
                target, opt.fineSize, opt.fineSize, height_ratio, width_ratio)
        elif opt.error_form == 'image_size':
            pass
        else:
            raise ValueError()

        # compute distance error
        dist, dist_min, correct_flip = compute_mean_distance(
            prediction, target, correspondeces=correspondeces,
            target_correspondeces=target_correspondeces,
            used_points=used_points, offline_prediction=offline_prediction)

        # compute reconstruction loss
        lpips_loss = compute_reconstruction_loss(model.rec_A, model.real_A)

        # log results
        distances.extend(dist.cpu().numpy())
        distances_min.extend(dist_min.cpu().numpy())
        correct_flips.extend(correct_flip.cpu().numpy())
        lpips_losses.extend(lpips_loss.detach().cpu().numpy())
        paths.extend(data['A_paths'])

        t = (time.time() - iter_start_time)
        iter_start_time = time.time()
        avg_time.append(t)
        if i % opt.print_freq == 0:
            samples_frq = float(opt.batch_size) / t
            samples_frq_avg = float(opt.batch_size) / np.mean(avg_time)
            time_str = '%.1f samples/sec %.1f samples/sec (avg)' % (
                samples_frq, samples_frq_avg)

            print('processing (%d/%d)-th batch %s' % (i, n_batches, time_str))
            print(np.random.choice(img_path, 1))

            mean_distances = mean_per_activity(distances, paths, path_fn)
            mean_distance = np.mean(mean_distances.values())
            mean_min_distances = mean_per_activity(distances_min, paths, path_fn)
            mean_min_distance = np.mean(mean_min_distances.values())
            mean_correct_flips = mean_per_activity(correct_flips, paths, path_fn)
            mean_correct_flip = np.mean(mean_correct_flips.values())
            mean_lpips_losses = mean_per_activity(lpips_losses, paths, path_fn)
            mean_lpips_loss = np.mean(mean_lpips_losses.values())

            results_str = 'mean distance %.4f\n' % mean_distance
            results_str += 'mean min distance %.4f\n' % mean_min_distance
            results_str += 'mean correct flips %.4f\n' % mean_correct_flip
            results_str += 'mean lpips loss %.4f\n' % mean_lpips_loss
            results_str += '%s\n' % format_results_per_activity(mean_distances)
            results_str += '%s\n' % format_results_per_activity(mean_min_distances)
            results_str += '%s\n' % format_results_per_activity(mean_correct_flips)
            results_str += '%s\n' % format_results_per_activity(mean_lpips_losses)
            print(results_str)

        if i % save_frq == 0:
            visuals = model.get_current_visuals()
            webpage.add_text(data['A_paths'][0])
            if opt.dataset_mode == 'simplehuman36m':
                data['image_name'] = ['-'.join(x.split(os.path.sep)[-2:]) for x in img_path]
            if 'image_name' in data:
                img_names = [x.replace(os.path.sep, '-') for x in data['image_name']]
                take_basename = False
            else:
                img_names = img_path
                take_basename = True
            save_images(
                webpage, visuals, img_names, aspect_ratio=opt.aspect_ratio,
                width=opt.display_winsize, basename=take_basename)
            webpage.save()

    mean_distances = mean_per_activity(distances, paths, path_fn)
    mean_distance = np.mean(mean_distances.values())
    mean_min_distances = mean_per_activity(distances_min, paths, path_fn)
    mean_min_distance = np.mean(mean_min_distances.values())
    mean_correct_flips = mean_per_activity(correct_flips, paths, path_fn)
    mean_correct_flip = np.mean(mean_correct_flips.values())
    mean_lpips_losses = mean_per_activity(lpips_losses, paths, path_fn)
    mean_lpips_loss = np.mean(mean_lpips_losses.values())

    results_str = 'mean distance %.4f\n' % mean_distance
    results_str += 'mean min distance %.4f\n' % mean_min_distance
    results_str += 'mean correct flips %.4f\n' % mean_correct_flip
    results_str += 'mean lpips loss %.4f\n' % mean_lpips_loss
    results_str += '%s\n' % format_results_per_activity(mean_distances)
    results_str += '%s\n' % format_results_per_activity(mean_min_distances)
    results_str += '%s\n' % format_results_per_activity(mean_min_distances)
    results_str += '%s\n' % format_results_per_activity(mean_correct_flips)
    results_str += '%s\n' % format_results_per_activity(mean_lpips_losses)

    print(results_str)
    webpage.add_text(results_str)

    #results_str = format_results_per_activity2(mean_distances)
    #print(results_str[0])
    #webpage.add_text(results_str[0])
    #print(results_str[1])
    #webpage.add_text(results_str[1])

    #results_str = format_results_per_activity2(mean_min_distances)
    #print(results_str[0])
    #webpage.add_text(results_str[0])
    #print(results_str[1])
    #webpage.add_text(results_str[1])

    # save the website
    webpage.save()

max_stars_repo_name: VMReyes/keypointgan

max_issues_repo_name: VMReyes/keypointgan

max_forks_repo_name: VMReyes/keypointgan


text:
#-------------------------------------------------------------------------------
#
#  SHC format parser
#
# Author: Martin Paces <martin.paces@eox.at>
#
#-------------------------------------------------------------------------------
# Copyright (C) 2018 EOX IT Services GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies of this Software or works derived from this Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-------------------------------------------------------------------------------

from numpy import inf, array

def parse_shc_file(file_in):
    """ Parse SHC file and return a dictionary containing the parsed model data.
    """
    lines = _strip_shc_comments(file_in)
    data = _parse_shc_header(lines)
    data["nm"], data["gh"] = _parse_shc_coefficients(lines)
    return data

def parse_shc_header(file_in):
    """ Parse SHC file header and return a dictionary containing the parsed
    model info.
    """
    lines = _strip_shc_comments(file_in)
    return _parse_shc_header(lines)

def _parse_shc_header(lines):
    data = _parse_shc_header_line(next(lines))
    data["t"] = times = _parse_shc_times(next(lines))
    if "validity_start" not in data:
        data["validity_start"] = times.min() if times.size > 1 else -inf
    if "validity_end" not in data:
        data["validity_end"] = data["t"].max() if times.size > 1 else +inf
    return data

def _parse_shc_coefficients(lines):
    """ Parse SHC coefficients. """
    nm_index = []
    coefficients = []
    for line in lines:
        fields = line.split()
        nm_index.append([int(v) for v in fields[:2]])
        coefficients.append([float(v) for v in fields[2:]])
    return array(nm_index), array(coefficients)

def _parse_shc_times(line):
    """ Parse SHC times. """
    return array([float(v) for v in line.split()])

def _parse_shc_header_line(line):
    """ Parse the SHC file header. """
    fields = line.split()
    header = {
        "degree_min": int(fields[0]),
        "degree_max": int(fields[1]),
        "ntime": int(fields[2]),
        "spline_order": int(fields[3]),
        "nstep": int(fields[4]),
    }
    if fields[5:7]:
        header["validity_start"] = float(fields[5])
        header["validity_end"] = float(fields[6])
    return header

def _strip_shc_comments(file_in):
    """ Strip initial comments and empty lines from a text file stream. """
    for line in file_in:
        line = line.partition("#")[0].strip()
        if line:
            yield line
            break
    for line in file_in:
        line = line.strip()
        yield line

max_stars_repo_name: CHEN-Zhaohui/geoist

max_issues_repo_name: CHEN-Zhaohui/geoist

max_forks_repo_name: CHEN-Zhaohui/geoist

reason: from numpy


text:
import numpy as np
from typing import Tuple, List

import tensorflow as tf

import gym

# Can check some outside info: https://bytepawn.com/solving-openai-gym-classic-control-problems-with-pytorch.html
envs = ['CartPole-v0', 'MountainCar-v0', 'Acrobot-v1', 'Pendulum-v1', 'MountainCarContinuous-v0']
min_episodes = {'CartPole-v0': 100, 'MountainCar-v0': 100, 'Acrobot-v1': 100, 'Pendulum-v1': 100, 'MountainCarContinuous-v0': 109}
thresholds = {'CartPole-v0': 195, 'MountainCar-v0': -150, 'Acrobot-v1': -150, 'Pendulum-v1': -150, 'MountainCarContinuous-v0': 90}
max_steps = {'CartPole-v0': 200, 'MountainCar-v0': 200, 'Acrobot-v1': 200, 'Pendulum-v1': 200, 'MountainCarContinuous-v0': 999}
dts = {'CartPole-v0': 0.02, 'MountainCar-v0': 1, 'Acrobot-v1': 0.2, 'Pendulum-v1': 0.05, 'MountainCarContinuous-v0': 1}

class Environment:
    # Heavily based on:
    # https://github.com/tensorflow/docs/blob/master/site/en/tutorials/reinforcement_learning/actor_critic.ipynb

    def __init__(self, name, seed=0):
        if name not in envs:
            print(f'{name} not known. Choose from {str(envs)}')
            return

        self.env = gym.make(name)
        self.env.seed(seed)
        self.name = name

        self.min_episodes_val = min_episodes[name]
        self.threshold_val = thresholds[name]
        self.max_steps_val = max_steps[name]
        self.dt_val = dts[name]

    def step(self, action:np.ndarray, getInfo=False) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
        state, reward, done, info = self.env.step(action)
        return (state.astype(np.float32), np.array(reward, np.int32), np.array(done, np.int32))

    def tf_step(self, action) -> List[tf.Tensor]:
        return tf.numpy_function(self.step, [action], [tf.float32, tf.int32, tf.int32])

    def get_new_starting_state(self):
        return tf.constant(self.env.reset(), dtype=tf.float32)

    def get_num_actions(self):
        try:
            return self.env.action_space.n
        except AttributeError as e:
            return self.env.action_space._shape[0]

    def min_episodes(self):
        return self.min_episodes_val

    def threshold(self):
        return self.threshold_val

    def max_steps(self):
        return self.max_steps_val

    def dt(self):
        return self.dt_val

    def get_name(self):
        return self.name


text:
import socket
import sys
import os
import cv2
import pickle
import numpy as np
import struct
import time
import select
import thread

from constants import *
from imgcaption.im2txt.get_imgcaption import ImageCaptioner
#from .imgcaption.im2txt.configuration import ImageCaptioner

def write_caption(image_file, imagecaptioner):
	imagecaptioner.getCaption(image_file)

def on_client(conn, addr, ImageCaptionerObject, timeout):
	print("New client opened at "+str(addr[0])+':'+str(addr[1]))
	conn.settimeout(timeout)
	try:
		payload_size = struct.calcsize("Q")				# Should be 8
		while True:
			data = ""
			print("Waiting for payload data...")
			begin = time.time()
			while len(data) < payload_size:
				recv_data = conn.recv(4096)
				if recv_data:
					data += recv_data
				else:
					if time.time()-begin > timeout:
						print("Payload size data receive timeout. Likely that connection is closed. Restarting...")
						raise Exception('Data Timeout, Restarting Connection')
					else:
						time.sleep(0.1)
			print("Got payload data")

			packed_msg_size = data[:payload_size]
			data = data[payload_size:]
			msg_size = struct.unpack("Q", packed_msg_size)[0]
			print(str(addr[0])+':'+str(addr[1])+': '+str(msg_size))

			try:
				print("Waiting to get entire frame...")
				begin = time.time()
				while len(data) < msg_size:
					if time.time()-begin > timeout:
						raise Exception('Data receive timed out, clearing...')
					recv_data = conn.recv(16384)
					if (recv_data):
						data += recv_data
						begin = time.time()
					else:
						time.sleep(0.1)

			except Exception as e:
				print(e)
				data = ""
				continue

			print("Got entire frame")
			frame_data = data[:msg_size]
			data = data[msg_size:]					# Reset

			frame=pickle.loads(frame_data)
			cv2.imwrite(TEMP_COMMU_IMG_LOC,frame)

			print("Getting captions...")
			captions = ImageCaptionerObject.getCaption(TEMP_COMMU_IMG_LOC)

			# Write captions
			print("Writing captions")
			while os.path.exists(COMMU_IMG_CAPTIONS_LOCK):
				continue
			open(COMMU_IMG_CAPTIONS_LOCK, 'w').close()
			with open(COMMU_IMG_CAPTIONS, 'w') as f:
				f.write(str(time.time()))
				for c in captions:
					f.write('\n')
					f.write(' '.join([str(x) for x in c]))
			os.remove(COMMU_IMG_CAPTIONS_LOCK)

			#cv2.imshow('frame',frame)
			#if cv2.waitKey(1) == 27:
			#	break
	except KeyboardInterrupt:
		serveropen = False
	except Exception as e:
		print(e)
		pass
	finally:
		# Remove lock file for image
		if os.path.exists(COMMU_IMG_CAPTIONS_LOCK):
			os.remove(COMMU_IMG_CAPTIONS_LOCK)
		print "Closing client connection: "+str(addr[0])+':'+str(addr[1])
		conn.close()

def image_process(host=RECEIVE_IMAGE_HOST, port=RECEIVE_IMAGE_PORT, timeout=RECV_TIMEOUT, model_path=PRETRAINED_MODEL_PATH, vocab_list=PRETRAINED_WORD_COUNTS):
	IC = ImageCaptioner(model_path, vocab_list)
	IC.openSession()
	print("Image Captioner session and process opened")

	try:
		print('Starting listening port and image processing')
		s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		print 'Socket created'
		s.bind((host,port))
		print 'Socket bind complete'

		try:
			serveropen = True
			while serveropen:
				s.listen(10)
				print('Socket now listening on '+str(host)+' : '+str(port))

				conn,addr = s.accept()
				print 'Got a connection from '+str(addr[0])+' : '+str(addr[1])

				thread.start_new_thread(on_client,(conn, addr, IC, timeout,))
				#on_client(conn, addr, IC, timeout=timeout)

		except Exception as e:
			print(e)
			pass
		finally:
			print "Closing server socket"
			s.shutdown(socket.SHUT_RDWR)
			s.close()

	except Exception as e:
		print(e)
		pass
	finally:
		print("Closing Image Captioner session")
		IC.closeSession()

if __name__ == "__main__":
	image_process()

max_stars_repo_name: Reinaesaya/OUIRL-ChatBot

max_issues_repo_name: Reinaesaya/OUIRL-ChatBot

max_forks_repo_name: Reinaesaya/OUIRL-ChatBot