text:
import argparse
import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'

import os.path as osp
import shutil
import tempfile
import pandas as pd
import numpy as np

import mmcv
import torch
import torch.distributed as dist
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import get_dist_info, load_checkpoint

from mmdet.apis import init_dist
from mmdet.core import wrap_fp16_model
from mmdet.datasets import build_dataloader, build_dataset
from mmdet.models import build_detector

from mmdet.datasets.kaggle_pku_utils import quaternion_to_euler_angle, filter_igore_masked_using_RT, filter_output
from tqdm import tqdm
from tools.evaluations.map_calculation import map_main
from multiprocessing import Pool

# from finetune_RT_NMR import finetune_RT
from finetune_RT_NMR_img import finetune_RT

def single_gpu_test(model, data_loader, show=False):
    model.eval()
    results = []
    dataset = data_loader.dataset
    prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(return_loss=False, rescale=not show, **data)
        results.append(result)

        if show:
            model.module.show_result(data, result)

        batch_size = data['img'][0].size(0)
        for _ in range(batch_size):
            prog_bar.update()
    return results

def multi_gpu_test(model, data_loader, tmpdir=None):
    model.eval()
    results = []
    dataset = data_loader.dataset
    rank, world_size = get_dist_info()
    if rank == 0:
        prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(return_loss=False, rescale=True, **data)
        results.append(result)

        if rank == 0:
            batch_size = data['img'][0].size(0)
            for _ in range(batch_size * world_size):
                prog_bar.update()

    # collect results from all ranks
    results = collect_results(results, len(dataset), tmpdir)

    return results

def collect_results(result_part, size, tmpdir=None):
    rank, world_size = get_dist_info()
    # create a tmp dir if it is not specified
    if tmpdir is None:
        MAX_LEN = 512
        # 32 is whitespace
        dir_tensor = torch.full((MAX_LEN,),
                                32,
                                dtype=torch.uint8,
                                device='cuda')
        if rank == 0:
            tmpdir = tempfile.mkdtemp()
            tmpdir = torch.tensor(
                bytearray(tmpdir.encode()), dtype=torch.uint8, device='cuda')
            dir_tensor[:len(tmpdir)] = tmpdir
        dist.broadcast(dir_tensor, 0)
        tmpdir = dir_tensor.cpu().numpy().tobytes().decode().rstrip()
    else:
        mmcv.mkdir_or_exist(tmpdir)
    # dump the part result to the dir
    mmcv.dump(result_part, osp.join(tmpdir, 'part_{}.pkl'.format(rank)))
    dist.barrier()
    # collect all parts
    if rank != 0:
        return None
    else:
        # load results of all parts from tmp dir
        part_list = []
        for i in range(world_size):
            part_file = osp.join(tmpdir, 'part_{}.pkl'.format(i))
            part_list.append(mmcv.load(part_file))
        # sort the results
        ordered_results = []
        for res in zip(*part_list):
            ordered_results.extend(list(res))
        # the dataloader may pad some samples
        ordered_results = ordered_results[:size]
        # remove tmp dir
        shutil.rmtree(tmpdir)
        return ordered_results

def write_submission(outputs, args, dataset,
                     conf_thresh=0.15,
                     filter_mask=False,
                     horizontal_flip=False):
    img_prefix = dataset.img_prefix

    submission = args.out.replace('.pkl', '')
    submission += '_' + img_prefix.split('/')[-1]
    submission += '_conf_' + str(conf_thresh)

    if filter_mask:
        submission += '_filter_mask'
    elif horizontal_flip:
        submission += '_horizontal_flip'

    submission += '.csv'
    predictions = {}

    CAR_IDX = 2  # this is the coco car class
    for idx_img, output in tqdm(enumerate(outputs)):
        file_name = os.path.basename(output[2]["file_name"])
        ImageId = ".".join(file_name.split(".")[:-1])

        # Wudi change the conf to car prediction
        if len(output[0][CAR_IDX]):
            conf = output[0][CAR_IDX][:, -1]  # output [0] is the bbox
            idx_conf = conf > conf_thresh
            if filter_mask:
                # this filtering step will takes 2 second per iterations
                # idx_keep_mask = filter_igore_masked_images(ImageId[idx_img], output[1][CAR_IDX], img_prefix)
                idx_keep_mask = filter_igore_masked_using_RT(ImageId, output[2], img_prefix, dataset)

                # the final id should require both
                idx = idx_conf * idx_keep_mask
            else:
                idx = idx_conf
            # if 'euler_angle' in output[2].keys():
            if False:  # NMR has problem saving 'euler angle' Its
                eular_angle = output[2]['euler_angle']
            else:
                eular_angle = np.array([quaternion_to_euler_angle(x) for x in output[2]['quaternion_pred']])
            translation = output[2]['trans_pred_world']
            coords = np.hstack((eular_angle[idx], translation[idx], conf[idx, None]))

            coords_str = coords2str(coords)

            predictions[ImageId] = coords_str
        else:
            predictions[ImageId] = ""

    pred_dict = {'ImageId': [], 'PredictionString': []}
    for k, v in predictions.items():
        pred_dict['ImageId'].append(k)
        pred_dict['PredictionString'].append(v)

    df = pd.DataFrame(data=pred_dict)
    print("Writing submission csv file to: %s" % submission)
    df.to_csv(submission, index=False)
    return submission

def filter_output_pool(t):
    return filter_output(*t)

def write_submission_pool(outputs, args, dataset,
                          conf_thresh=0.1,
                          horizontal_flip=False,
                          max_workers=20):
    """
    For accelerating filter image
    :param outputs:
    :param args:
    :param dataset:
    :param conf_thresh:
    :param horizontal_flip:
    :param max_workers:
    :return:
    """
    img_prefix = dataset.img_prefix

    submission = args.out.replace('.pkl', '')
    submission += '_' + img_prefix.split('/')[-1]
    submission += '_conf_' + str(conf_thresh)
    submission += '_filter_mask.csv'
    if horizontal_flip:
        submission += '_horizontal_flip'
    submission += '.csv'
    predictions = {}

    p = Pool(processes=max_workers)
    for coords_str, ImageId in p.imap(filter_output_pool,
                                      [(i, outputs, conf_thresh, img_prefix, dataset) for i in range(len(outputs))]):
        predictions[ImageId] = coords_str

    pred_dict = {'ImageId': [], 'PredictionString': []}
    for k, v in predictions.items():
        pred_dict['ImageId'].append(k)
        pred_dict['PredictionString'].append(v)

    df = pd.DataFrame(data=pred_dict)
    print("Writing submission csv file to: %s" % submission)
    df.to_csv(submission, index=False)
    return submission

def coords2str(coords):
    s = []
    for c in coords:
        for l in c:
            s.append('%.5f' % l)
    return ' '.join(s)

def parse_args():
    parser = argparse.ArgumentParser(description='MMDet test detector')
    parser.add_argument('--config',
                        default='../configs/6dvnet/6dvnet_htc_hrnetv2p_w48_20e_kitti3d.py', help='train config file path')
    parser.add_argument('--checkpoint', default='/data/Kaggle/checkpoints/all_cwxe99_3070100flip05resumme93Dec29-16-28-48/epoch_100.pth',
                        help='checkpoint file')
    parser.add_argument('--conf', default=0.9, help='Confidence threshold for writing submission')
    parser.add_argument('--json_out', help='output result file name without extension', type=str)
    parser.add_argument('--eval', type=str, nargs='+',
                        choices=['proposal', 'proposal_fast', 'bbox', 'segm', 'keypoints', ' kaggle'],
                        help='eval types')
    parser.add_argument('--show', action='store_true', help='show results')
    parser.add_argument('--tmpdir', help='tmp dir for writing some results')
    parser.add_argument('--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher')
    parser.add_argument('--local_rank', type=int, default=0)
    parser.add_argument('--horizontal_flip', default=False, action='store_true')
    parser.add_argument('--world_size', default=8)
    args = parser.parse_args()
    if 'LOCAL_RANK' not in os.environ:
        os.environ['LOCAL_RANK'] = str(args.local_rank)
    return args

def main():
    args = parse_args()

    cfg = mmcv.Config.fromfile(args.config)
    # Wudi change the args.out directly related to the model checkpoint file data
    args.out = os.path.join(cfg.work_dir, cfg.data.test.img_prefix.split('/')[-3] + args.checkpoint.split('/')[-2] + '_' + args.checkpoint.split('/')[-1][:-4] + '.pkl')

        # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # build the dataloader
    dataset = build_dataset(cfg.data.test)
    if not os.path.exists(args.out):
        data_loader = build_dataloader(
            dataset,
            imgs_per_gpu=1,
            workers_per_gpu=cfg.data.workers_per_gpu,
            dist=distributed,
            shuffle=False)

        # build the model and load checkpoint
        model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
        fp16_cfg = cfg.get('fp16', None)
        if fp16_cfg is not None:
            wrap_fp16_model(model)
        checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
        # old versions did not save class info in checkpoints, this walkaround is
        # for backward compatibility
        if 'CLASSES' in checkpoint['meta']:
            model.CLASSES = checkpoint['meta']['CLASSES']
        else:
            model.CLASSES = dataset.CLASSES

        if not distributed:
            model = MMDataParallel(model, device_ids=[0])
            outputs = single_gpu_test(model, data_loader, args.show)
        else:
            model = MMDistributedDataParallel(model.cuda())
            outputs = multi_gpu_test(model, data_loader, args.tmpdir)
        mmcv.dump(outputs, args.out)

    else:
        outputs = mmcv.load(args.out)

    if distributed:
        rank, _ = get_dist_info()
        if rank != 0:
            return

    # Now we visualise the output here
    dataset.visualise_pred(outputs, args)

if __name__ == '__main__':
    main()


text:
import numpy as np
from numba import jit

def detect_poles(xyz, neighbourthr = 0.5, min_point_num = 3, dis_thr = 0.08, width_thr = 10, fov_up=30.67, fov_down=-10.67, proj_H = 32, proj_W = 250, lowest=0.1, highest=6, lowthr = 1.5, highthr = 0.7, totalthr = 0.6):
    range_data, proj_vertex, _ = range_projection(xyz,
                                                fov_up=fov_up,
                                                fov_down=fov_down,
                                                proj_H=proj_H,
                                                proj_W=proj_W,
                                                max_range=50,
                                                cut_z = True,
                                                low=lowest,
                                                high=highest)

    height = range_data.shape[0]
    width = range_data.shape[1]

    open_set = gen_open_set(range_data, height, width)
    open_set = np.array(open_set)

    clusters = gen_clusters(open_set, range_data, height, width, min_point_num = min_point_num, dis_thr = dis_thr)

    clusters_list = []
    for cluster in clusters:
        clusters_list.append(cluster.tolist())

    clusters_copy = list(clusters_list)
    for cluster in clusters_copy:
        cluster.sort()
        min_height = cluster[0][0]
        max_height = cluster[len(cluster)-1][0]
        cluster.sort(key=takeSecond)
        min_width = cluster[0][1]
        max_width = cluster[len(cluster)-1][1]
        ratio = (max_height - min_height + 1) / (max_width - min_width + 1)
        delate = 0
        dela = False
        for index in cluster:
            index[0] = int(index[0])
            index[1] = int(index[1])
            if (range_data[index[0],index[1]+1] != -1) and (not [index[0],index[1]+1] in cluster) and range_data[index[0]][index[1]] > range_data[index[0],index[1]+1]:
                dela = True
            if (range_data[index[0],index[1]-1] != -1) and (not [index[0],index[1]-1] in cluster) and range_data[index[0]][index[1]] > range_data[index[0],index[1]-1]:
                dela = True
            if dela:
                delate += 1
                dela = False
        if ratio < 1.0 or delate > 0.3 * len(cluster) or (max_width - min_width + 1) > width_thr:
            clusters_list.remove(cluster)

    poleparams = np.empty([0, 3])
    for cluster in clusters_list:
        x = []
        y = []
        z = []
        for index in cluster:
            index[0] = int(index[0])
            index[1] = int(index[1])
            x.append(proj_vertex[index[0]][index[1]][0])
            y.append(proj_vertex[index[0]][index[1]][1])
            z.append(proj_vertex[index[0]][index[1]][2])

        high = max(z)
        low = min(z)
        if high > highthr and low < lowthr and (high-low) > totalthr:
            if fit_circle(x,y) != None:
                average_x,average_y,R_1 = fit_circle(x,y)
                fine_thr = R_1 + 0.1
                scan_x = xyz[:, 0]
                scan_y = xyz[:, 1]
                scan_z = xyz[:, 2]

                high = min(high,3.0)

                current_vertex_fine = xyz[(scan_x > (average_x - fine_thr)) & (scan_x < (average_x + fine_thr)) & (scan_y < (average_y + fine_thr)) & (scan_y > (average_y - fine_thr)) & (scan_z < high) & (scan_z > low)]
                x = []
                y = []
                for i in range(current_vertex_fine.shape[0]):
                    x.append(current_vertex_fine[i, 0])
                    y.append(current_vertex_fine[i, 1])
                if len(x) >= 6:
                    if fit_circle(x,y) != None:
                        xc_1, yc_1, R_1 = fit_circle(x,y)
                        if R_1 > 0.02 and R_1 < 0.4:
                            neighbour = xyz[(((scan_x > (average_x - fine_thr - neighbourthr)) & (scan_x < (average_x - fine_thr))) | ((scan_x > (average_x + fine_thr)) & (scan_x < (average_x + fine_thr + neighbourthr)))) & (((scan_y > (average_y + fine_thr - neighbourthr)) & (scan_y < (average_y - fine_thr))) | ((scan_y > (average_y + fine_thr)) & (scan_y < (average_y + fine_thr + neighbourthr)))) & (scan_z < high) & (scan_z > low)]
                            if neighbour.shape[0] < 0.15 * current_vertex_fine.shape[0]:
                                poleparams = np.vstack([poleparams, [xc_1,yc_1,R_1]])

    return poleparams

@jit(nopython=True)
def gen_open_set(range_data, height, width):
    open_set = []
    for i in range(1,height-1):
        for j in range(1,width-1):
            if range_data[i][j] != -1:
                open_set.append([i, j])
    return open_set

@jit(nopython=True)
def in_array(set, index):
    for i in range(set.shape[0]):
        if set[i][0] == index[0] and set[i][1] == index[1]:
            return True
    return False

@jit(nopython=True)
def gen_clusters(open_set, range_data, height, width, min_point_num = 3, dis_thr = 0.08):
    clusters = []
    while open_set.shape[0] > 0:
        cluster = np.zeros((0, 2))
        current_index = open_set[0]
        open_set = np.delete(open_set, [0,1]).reshape((-1, 2))
        cluster = np.append(cluster, current_index).reshape((-1, 2))
        near_set = np.zeros((0, 2), dtype=np.int64)

        if (current_index[0]+1 < height) and (in_array(open_set, np.array([current_index[0]+1,current_index[1]]))) and abs(range_data[current_index[0]][current_index[1]] - range_data[current_index[0]+1][current_index[1]]) < dis_thr:
            near_set = np.append(near_set, [current_index[0]+1, current_index[1]]).reshape((-1, 2))
        if (current_index[1]+1 < width) and (in_array(open_set, np.array([current_index[0],current_index[1]+1]))) and abs(range_data[current_index[0]][current_index[1]] - range_data[current_index[0]][current_index[1]+1]) < dis_thr:
            near_set = np.append(near_set, [current_index[0], current_index[1]+1]).reshape((-1, 2))
        while len(near_set) > 0:
            near_index = near_set[0]
            near_set = np.delete(near_set, [0,1]).reshape((-1, 2))
            for i in range(open_set.shape[0]):
                if open_set[i][0] == near_index[0] and open_set[i][1] == near_index[1]:
                    open_set = np.delete(open_set, [2*i,2*i+1]).reshape((-1, 2))
                    break
            cluster = np.append(cluster, near_index).reshape((-1, 2))
            if (near_index[0]+1 < height) and (in_array(open_set, np.array([near_index[0]+1, near_index[1]]))) and (not in_array(cluster, np.array([near_index[0]+1, near_index[1]]))) and (not in_array(near_set, np.array([near_index[0]+1, near_index[1]]))) and (abs(range_data[near_index[0]][near_index[1]] - range_data[near_index[0]+1][near_index[1]]) < dis_thr):
                near_set = np.append(near_set, [near_index[0]+1, near_index[1]]).reshape((-1, 2))
            if (near_index[1]+1 < width) and (in_array(open_set, np.array([near_index[0], near_index[1]+1]))) and (not in_array(cluster, np.array([near_index[0], near_index[1]+1]))) and (not in_array(near_set, np.array([near_index[0], near_index[1]+1]))) and (abs(range_data[near_index[0]][near_index[1]] - range_data[near_index[0]][near_index[1]+1]) < dis_thr):
                near_set = np.append(near_set, [near_index[0], near_index[1]+1]).reshape((-1, 2))
            if (near_index[1]-1 >= 0) and (in_array(open_set, np.array([near_index[0], near_index[1]-1]))) and (not in_array(cluster, np.array([near_index[0], near_index[1]-1]))) and (not in_array(near_set, np.array([near_index[0], near_index[1]-1]))) and (abs(range_data[near_index[0]][near_index[1]] - range_data[near_index[0]][near_index[1]-1]) < dis_thr):
                near_set = np.append(near_set, [near_index[0], near_index[1]-1]).reshape((-1, 2))

        if cluster.shape[0] > min_point_num:
            clusters.append(cluster)
    return clusters

def takeSecond(elem):
    return elem[1]

def range_projection(current_vertex, fov_up=10.67, fov_down=-30.67, proj_H=32, proj_W=900, max_range=50, cut_z = True, low=0.1, high=6):
    """ Project a pointcloud into a spherical projection, range image.
        Args:
        current_vertex: raw point clouds
        Returns:
        proj_range: projected range image with depth, each pixel contains the corresponding depth
        proj_vertex: each pixel contains the corresponding point (x, y, z, 1)
        proj_idx: each pixel contains the corresponding index of the point in the raw point cloud
    """
    # laser parameters
    fov_up = fov_up / 180.0 * np.pi  # field of view up in radians
    fov_down = fov_down / 180.0 * np.pi  # field of view down in radians
    fov = abs(fov_down) + abs(fov_up)  # get field of view total in radians

    # get depth of all points
    depth = np.linalg.norm(current_vertex[:, :3], 2, axis=1)

    if cut_z:
        z = current_vertex[:, 2]
        current_vertex = current_vertex[(depth > 0) & (depth < max_range) & (z < high) & (z > low)]  # get rid of [0, 0, 0] points
        depth = depth[(depth > 0) & (depth < max_range) & (z < high) & (z > low)]
    else:
        current_vertex = current_vertex[(depth > 0) & (depth < max_range)]  # get rid of [0, 0, 0] points
        depth = depth[(depth > 0) & (depth < max_range)]

    # get scan components
    scan_x = current_vertex[:, 0]
    scan_y = current_vertex[:, 1]
    scan_z = current_vertex[:, 2]

    # get angles of all points
    yaw = -np.arctan2(scan_y, scan_x)
    pitch = np.arcsin(scan_z / depth)

    # get projections in image coords
    proj_x = 0.5 * (yaw / np.pi + 1.0)  # in [0.0, 1.0]
    proj_y = 1.0 - (pitch + abs(fov_down)) / fov  # in [0.0, 1.0]

    # scale to image size using angular resolution
    proj_x *= proj_W  # in [0.0, W]
    proj_y *= proj_H  # in [0.0, H]

    # round and clamp for use as index
    proj_x = np.floor(proj_x)
    proj_x = np.minimum(proj_W - 1, proj_x)
    proj_x = np.maximum(0, proj_x).astype(np.int32)  # in [0,W-1]

    proj_y = np.floor(proj_y)
    proj_y = np.minimum(proj_H - 1, proj_y)
    proj_y = np.maximum(0, proj_y).astype(np.int32)  # in [0,H-1]

    # order in decreasing depth
    order = np.argsort(depth)[::-1]
    depth = depth[order]
    proj_y = proj_y[order]
    proj_x = proj_x[order]

    scan_x = scan_x[order]
    scan_y = scan_y[order]
    scan_z = scan_z[order]

    indices = np.arange(depth.shape[0])
    indices = indices[order]

    proj_range = np.full((proj_H, proj_W), -1,
                        dtype=np.float32)  # [H,W] range (-1 is no data)
    proj_vertex = np.full((proj_H, proj_W, 4), -1,
                        dtype=np.float32)  # [H,W] index (-1 is no data)
    proj_idx = np.full((proj_H, proj_W), -1,
                        dtype=np.int32)  # [H,W] index (-1 is no data)

    proj_range[proj_y, proj_x] = depth
    proj_vertex[proj_y, proj_x] = np.array([scan_x, scan_y, scan_z, np.ones(len(scan_x))]).T
    proj_idx[proj_y, proj_x] = indices

    return proj_range, proj_vertex, proj_idx

def fit_circle(x, y):
    x_m = sum(x)/len(x)
    y_m = sum(y)/len(y)
    u = x - x_m
    v = y - y_m
    # linear system defining the center in reduced coordinates (uc, vc):
    #    Suu * uc +  Suv * vc = (Suuu + Suvv)/2
    #    Suv * uc +  Svv * vc = (Suuv + Svvv)/2
    Suv  = sum(u*v)
    Suu  = sum(u**2)
    Svv  = sum(v**2)
    Suuv = sum(u**2 * v)
    Suvv = sum(u * v**2)
    Suuu = sum(u**3)
    Svvv = sum(v**3)

    # Solving the linear system
    A = np.array([ [ Suu, Suv ], [Suv, Svv]])
    B = np.array([ Suuu + Suvv, Svvv + Suuv ])/2.0

    if np.linalg.det(A) != 0:
        uc, vc = np.linalg.solve(A, B)
        xc_1 = x_m + uc
        yc_1 = y_m + vc
        # Calculation of all distances from the center (xc_1, yc_1)
        Ri_1      = np.sqrt((x-xc_1)**2 + (y-yc_1)**2)
        R_1       = np.mean(Ri_1)
        return xc_1,yc_1,R_1

    return None

ext: py

max_stars_repo_name: collector-m/pole-localization

max_issues_repo_name: collector-m/pole-localization

max_forks_repo_name: collector-m/pole-localization

reason: import numpy,from numba


text:
import numpy as np
import matplotlib.pyplot as plt

beta = [0.36,0.75,0.5,0.4]
theta = [21308746,7733,169272,1965868]
name = ["Structures","Battery/Cell","Electrical distribution","Solar Array Operating"]

fig,ax = plt.subplots(1,1)
T = np.arange(0.,10.01,0.01)
total = 1
for b,t,n in zip(beta,theta,name):
    ax.plot(T,np.exp(-(T/t)**b),label=n)
    total = total*np.exp(-(T/t)**b)
c = 1-np.exp(-(T/400982)**0.39)
c = 1-c*c
ax.plot(T,c ,label="Communication")
total = total * 0.98*0.965*0.995*0.995*0.98*c*0.98
ax.plot(T,total,label="Mission Reliability")
ax.set_xlim(0,1)
#ax.set_ylim(0.94,1.)
ax.set_xlabel("Time [years]")
ax.set_ylabel("Reliability")
ax.set_title("One year period")
ax.grid()
ax.legend()
plt.show()

print(total[-1])

reason: import numpy


text:
import numpy as np
import theano
import theano.tensor as T
import theano.tests.unittest_tools
from gp_kernel import kernel, symbolic_kernel
from cov_vec_bprop import CovVec, PosteriorMean
from lanczos_sqrtm import lanczos
import scipy
import time

def main():
    from fast_gp import sparse_w
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = .1
    b = 10
    c = .001
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)
    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    len_u = 1024 + 1
    #len_u = 128 + 1
    #len_u = 64
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    x_test = u[1:]
    #x_test = np.linspace(x_min, x_max, 20)
    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)

    t_idx_train = T.imatrix()
    t_w_train = T.matrix()
    t_idx_test = T.imatrix()
    t_w_test = T.matrix()
    t_gp_params = T.vector()
    t_indep_noise = T.scalar()
    t_ys = T.matrix()
    t_y = T.vector()

    cov_vec = CovVec(u, kernel, symbolic_kernel)

    def linear_op(zs):
        return cov_vec(t_idx_train, t_w_train, t_idx_test, t_w_test,
                       t_gp_params, t_indep_noise, zs)

    n_lanczos_basis = 10
    batch_size = 10
    cov_zs = lanczos(linear_op, t_ys, n_lanczos_basis, batch_size)

    post_mean = PosteriorMean(u, kernel, symbolic_kernel)
    mu = post_mean(t_idx_train, t_w_train, t_idx_test, t_w_test,
                   t_gp_params, t_indep_noise, t_y)

    gp_samples = mu.dimshuffle('x', 0) + cov_zs

    gp_samples_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test,
        t_gp_params, t_indep_noise, t_y, t_ys], gp_samples)

    len_test = len(x_test)
    y_test = np.random.normal(size=(batch_size, len_test))
    gdraws = gp_samples_fn(idx_train, w_train, idx_test, w_test,
                           (a, b), c, y, y_test)
    print gdraws.shape

    t_random_proj = T.matrix()
    val = (gp_samples * t_random_proj).sum()

    val_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test,
        t_gp_params, t_indep_noise, t_y, t_ys, t_random_proj], val)

    grad_val_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test,
        t_gp_params, t_indep_noise, t_y, t_ys, t_random_proj],
        theano.grad(val,
                    wrt=[t_gp_params, t_indep_noise],
                    consider_constant=[t_idx_train, t_w_train,
                                       t_idx_test, t_w_test,
                                       t_y, t_ys, t_random_proj]))

    grad_val_fn1 = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test,
        t_gp_params, t_indep_noise, t_y, t_ys, t_random_proj],
        theano.grad(val,
                    wrt=[t_random_proj],
                    consider_constant=[t_idx_train, t_w_train,
                                       t_idx_test, t_w_test,
                                       t_y, t_ys]))

    random_proj = np.random.rand(batch_size, len_test)
    t1 = time.time()
    for _ in xrange(10):
        grad_val_fn(idx_train, w_train, idx_test, w_test,
                (a, b), c, y, y_test, random_proj)
    t2 = time.time()
    print t2 - t1
    t1 = time.time()
    for _ in xrange(10):
        grad_val_fn1(idx_train, w_train, idx_test, w_test,
                (a, b), c, y, y_test, random_proj)
    t2 = time.time()
    print t2 - t1
    return

    n_test = 10
    for _ in xrange(n_test):
        random_proj = np.random.rand(batch_size, len_test)
        print 'test grad'
        print val_fn(idx_train, w_train, idx_test, w_test,
                     (a, b), c, y, y_test, random_proj)
        print grad_val_fn(idx_train, w_train, idx_test, w_test,
                          (a, b), c, y, y_test, random_proj)

        def val_fn1(x):
            a, b, c = x
            return val_fn(idx_train, w_train, idx_test, w_test,
                          (a, b), c, y, y_test, random_proj)

        def grad_val_fn1(x):
            a, b, c = x
            [a, b], c = grad_val_fn(idx_train, w_train, idx_test, w_test,
                                    (a, b), c, y, y_test, random_proj)
            return np.array([a, b, c])

        print scipy.optimize.check_grad(val_fn1, grad_val_fn1,
                                        np.array([a, b, c]))

    return
    import pylab as pl
    pl.figure()

    for each_sample in gdraws:
        pl.plot(x_test, each_sample, '-', c='b', alpha=.5)
    pl.plot(x, y, 'o', c='r')
    pl.show()

if __name__ == '__main__':
    main()

max_stars_repo_name: steveli/gp-adapter

max_issues_repo_name: steveli/gp-adapter

max_forks_repo_name: steveli/gp-adapter

reason: import numpy,import scipy,import theano


text:
__all__ = ['flatten_layer', 'pre_process_layer_states', 'bin_time_series',
           'binarize_time_series', 'plot_state_distribution', 'pattern_plot',
           'max_binary', 'jensenshannon', 'individual_state_plots',
           'embed_time_series', 'plot_embedding', 'layer_preprocessing']

from typing import Tuple
import numpy as np
import sklearn as sk
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy.special import rel_entr

def jensenshannon(p, q, base=None):
    p = np.asarray(p)
    q = np.asarray(q)
    p = p / np.sum(p, axis=0)
    q = q / np.sum(q, axis=0)
    m = (p + q) / 2.0
    left = rel_entr(p, m)
    right = rel_entr(q, m)
    js = np.sum(left, axis=0) + np.sum(right, axis=0)
    if base is not None:
        js /= np.log(base)
    return np.sqrt(js / 2.0)

def embed_time_series(x: np.ndarray,
                      tau: int,
                      dimensions: int) -> np.ndarray:
    """
    :param x: 1-D time-series (length N)
    :param tau: number of time-steps back for delay
    :param dimensions: dimensionality of the embedding
    :return: (T - dimensions*tau) x (dimensions)
    """
    embedding = np.zeros(shape=(len(x) - dimensions * tau, dimensions))
    for dim in range(embedding.shape[1]):
        embedding[:, dim] = x[dimensions * tau - dim * tau:len(x) - dim * tau]

    return embedding

def plot_embedding(embedding: np.ndarray,
                   tau: int,
                   dimensions: Tuple[int, int],
                   prefix: str,
                   as_png=False):
    """
    makes a plot of a time-series embedding
    :param embedding: a 2D array, first dim is time, second is dim
    :param tau: the delay size (for labelling)
    :param dimensions: tuple of first dimension vs second dimension
    :param prefix: filename prefix
    :param as_png: whether to write as a png file (default: true)
    """
    plt.scatter(embedding[:, dimensions[0]],
                embedding[:, dimensions[1]],
                s=1.0)
    plt.xlabel("x(t-" + str(dimensions[0] * tau) + ")")
    plt.ylabel("x(t-" + str(dimensions[1] * tau) + ")")
    if as_png:
        plt.savefig(prefix + ".png", dpi=300)
    else:
        plt.savefig(prefix + ".pdf")
    plt.clf()
    plt.close()

# TODO: remove neurons with inactive downsteam neighbors

def max_binary(layer_state):
    # difference time-series
    differenced_states = layer_state[1:, :] - layer_state[-1:, :]
    # calc variance of each neuron
    variances = np.var(differenced_states, axis=0)
    # remove no-variance neurons
    active_indices = [i for i, v in enumerate(variances) if v > 0.00001]
    filtered_layer_state = np.take(layer_state, active_indices, axis=1)

    binary_layer_state = np.zeros((filtered_layer_state.shape[0],
                                   filtered_layer_state.shape[1]), dtype=bool)
    for t in range(filtered_layer_state.shape[0]):
        max_index = np.argmax(filtered_layer_state[t])
        binary_layer_state[t, max_index] = True

    return binary_layer_state

def flatten_layer(layer_state):
    return layer_state.reshape(layer_state.shape[0], -1)

def _differenced_preprocessing(layer_state, filter_lowest=0.2):
    # difference time-series
    differenced_states = layer_state[1:, :] - layer_state[-1:, :]

    # calc variance of each neuron
    variances = np.var(differenced_states, axis=0)
    # remove no-variance neurons
    active_indices = [i for i, v in enumerate(variances) if v > 0.00001]
    print("removing...", len(variances) - len(active_indices),
          " neurons for inactivity")
    filtered_states = np.take(differenced_states, active_indices, axis=1)
    # remove low-variance neurons
    variances = np.var(filtered_states, axis=0)
    indices_l2g = np.argsort(variances)
    chosen = indices_l2g[int(filter_lowest * len(indices_l2g)):]
    print("removing...", len(variances) - len(chosen),
          " neurons for low activity")
    filtered_states = np.take(filtered_states, chosen, axis=1)
    # standardize whole set
    if filtered_states.shape[1] >= 1:
        scaler = sk.preprocessing.StandardScaler()
        scaled_states = scaler.fit_transform(filtered_states)
    else:
        scaled_states = filtered_states
    print("\tremaining states:", scaled_states.shape[1])
    return scaled_states

def _preprocessing(layer_state, filter_lowest=0.2):
    # difference time-series
    differenced_states = layer_state[1:, :] - layer_state[-1:, :]

    # calc variance of each neuron
    variances = np.var(differenced_states, axis=0)
    # remove no-variance neurons
    active_indices = [i for i, v in enumerate(variances) if v > 0.00001]
    print("removing...", len(variances) - len(active_indices),
          " neurons for inactivity")
    filtered_states = np.take(differenced_states, active_indices, axis=1)
    filtered_layer_state = np.take(layer_state, active_indices, axis=1)
    # remove low-variance neurons
    variances = np.var(filtered_states, axis=0)
    indices_l2g = np.argsort(variances)
    chosen = indices_l2g[int(filter_lowest * len(indices_l2g)):]
    print("removing...", len(variances) - len(chosen),
          " neurons for low activity")
    filtered_states = np.take(filtered_states, chosen, axis=1)
    filtered_layer_state = np.take(filtered_layer_state, chosen, axis=1)
    # standardize whole set
    if filtered_states.shape[1] >= 1:
        scaler = sk.preprocessing.StandardScaler()
        scaled_states = scaler.fit_transform(filtered_layer_state)
    else:
        scaled_states = filtered_layer_state
    print("\tremaining states:", scaled_states.shape[1])
    return scaled_states

def pre_process_layer_states(layer_state, filter_lowest=0.2, differenced=True):
    """
    Differences the time-series, removes the neurons with low
    variance and then rescales the rest.
    :param layer_state: a TxN matrix where N is the number of neurons.
    :param filter_lowest: remove lowest X percent AFTER neurons with
    no variance are removed.
    :return: a TxA matrix where A is the remaining number of neurons
    """
    if differenced:
        return _differenced_preprocessing(layer_state, filter_lowest)
    else:
        return _preprocessing(layer_state, filter_lowest)

def layer_preprocessing(layer_state):
    # difference time-series
    differenced_states = layer_state[1:, :] - layer_state[-1:, :]

    # calc variance of each neuron
    variances = np.var(differenced_states, axis=0)
    # remove no-variance neurons
    active_indices = [i for i, v in enumerate(variances) if v > 0.00001]
    filtered_layer_state = np.take(layer_state, active_indices, axis=1)
    # if everything filtered out, then leave as is
    if filtered_layer_state.shape[1] == 0:
        filtered_layer_state = layer_state
    scaler = sk.preprocessing.StandardScaler()
    scaled_states = scaler.fit_transform(filtered_layer_state)
    return scaled_states

def bin_time_series(layer_state, window_size=5, method="avg"):
    """
    bins the activity in a non-rolling window of a given size by a chosen method
    :param layer_state: TxN matrix where N is the number of neurons.
    :param window_size: size in time-steps to bin
    :param method: default "avg" ("max" for largest value)
    :return: txN matrix where t is the size of the time-series post binning
    """
    num_bins = int(layer_state.shape[0] / window_size)
    binned_state = np.zeros((num_bins, layer_state.shape[1]))
    if method == "avg":
        for i in range(num_bins):
            binned_state[i, :] = np.mean(layer_state[i * window_size:(i+1)*window_size, :],
                                         axis=0)

    elif method == "max":
        for i in range(num_bins):
            binned_state[i, :] = np.max(layer_state[i * window_size:(i+1)*window_size, :],
                                        axis=0)
    else:
        raise NotImplementedError
    print("new time end:", binned_state.shape[0])
    return binned_state

def binarize_time_series(layer_state, threshold, absolute=False):
    """
    Given a threshold return a binary matrix of 0's or 1's based on threshold
    :param layer_state: TxN matrix where N is the number of neurons.
    :param threshold: threshold above which the value is considered 1.
    :param absolute: whether to threshold by absolute value (default: False)
    :return: binarized matrix of shape TxN
    """
    if absolute:
        return sk.preprocessing.binarize(np.abs(layer_state), threshold)
    else:
        return sk.preprocessing.binarize(layer_state, threshold)

def plot_state_distribution(layer, prefix="test", plot_min_max=True,
                            png=False):
    """
    Makes a time series plot of the max/min/median/90%/10% quartile ranges
    :param layer: a TxN matrix, where N is the number of neurons
    :param prefix: prefix for output file name
    :param plot_min_max: if True plots min/max values (default: True)
    :return: None
    """
    plt.clf()

    mins = []
    maxes = []
    tops = []
    bots = []
    centers = []
    for state in layer:

        # Max, min
        mins.append(min(state))
        maxes.append(max(state))

        # Percentile 90, 10, 50
        tops.append(np.percentile(state, 90))
        bots.append(np.percentile(state, 10))
        centers.append(np.percentile(state, 50))

    plt.plot(centers, color="black", marker="None", lw=1, label='median')
    plt.fill_between(range(len(tops)), tops, bots, color="red", alpha=0.3,
                     label='10%/90%')
    if plot_min_max:
        plt.plot(mins, ls="dotted", marker="None", color="black", lw=1,
                 label='min/max')
        plt.plot(maxes, ls="dotted", marker="None", color="black", lw=1)
    plt.xlim(0, len(tops))
    plt.legend()
    if png:
        plt.savefig(prefix + "_state_distribution.png", dpi=300)
    else:
        plt.savefig(prefix + "_state_distribution.pdf")
    plt.clf()
    plt.close()

def pattern_plot(layer, prefix="test", window=None, xlabel="time",
                 ylabel="activation", png=False):
    """
    :param layer: TxN activation matrix
    :param prefix: for file name
    :param xlabel: name for T dimension (default: time)
    :param ylabel: name for N dimension (default: activation)
    :return: None
    """
    plt.clf()
    fig, ax = plt.subplots()
    cmap = mpl.colors.ListedColormap(['w', 'k'])
    bounds = [0, 0.5, 1]
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    if window is None:
        ax.imshow(layer.T, interpolation='none', cmap=cmap, norm=norm)
    else:
        ax.imshow(layer.T[:, window[0]:window[1]],
                  interpolation='none', cmap=cmap, norm=norm)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    if png:
        plt.savefig(prefix + "_pattern.png", dpi=300)
    else:
        plt.savefig(prefix + "_pattern.pdf")
    plt.clf()
    plt.close()

def individual_state_plots(history, layer, prefix="test"):
    flat_layer = flatten_layer(history[layer])
    processed_layer = pre_process_layer_states(flat_layer, 0, False)
    for i in range(processed_layer.shape[1]):
        plt.plot(processed_layer[:, i])
        plt.savefig(str(i) + "_" + prefix + "_state_ts.png", dpi=300)
        plt.close()
        plt.clf()

max_stars_repo_name: Nathaniel-Rodriguez/tdavis

max_issues_repo_name: Nathaniel-Rodriguez/tdavis

max_forks_repo_name: Nathaniel-Rodriguez/tdavis

reason: import numpy,from scipy


text:
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import numpy
import pytest
import tensorflow
import torch
from utils import rand_im

from neuralcompression.functional import multiscale_structural_similarity
from neuralcompression.metrics import MultiscaleStructuralSimilarity

def tf_ms_ssim(x, y):
    # Converting NCHW image format to NHWC
    x = tensorflow.convert_to_tensor(x.permute(0, 2, 3, 1))
    y = tensorflow.convert_to_tensor(y.permute(0, 2, 3, 1))
    return torch.tensor(tensorflow.image.ssim_multiscale(x, y, max_val=1).numpy())

@pytest.mark.parametrize(
    "num_channels, img_size, batch_size, seed",
    [(1, 256, 1, 100), (1, 250, 4, 101), (10, 256, 1, 102)],
)
def test_ms_ssim_functional(num_channels, img_size, batch_size, seed):
    # Testing basic properties of the functional MS-SSIM API,
    # plus comparing its output to Tensorflow's implementation.
    rng = numpy.random.default_rng(seed)

    img1 = rand_im([batch_size, num_channels, img_size, img_size], rng)
    img2 = rand_im([batch_size, num_channels, img_size, img_size], rng)

    assert torch.allclose(
        multiscale_structural_similarity(img1, img1), torch.tensor(1.0)
    )

    assert multiscale_structural_similarity(
        img1, img1, reduction="none"
    ).shape == torch.Size([batch_size])
    assert torch.allclose(
        multiscale_structural_similarity(img1, img1, reduction="sum"),
        torch.tensor(batch_size).float(),
    )
    assert torch.allclose(
        multiscale_structural_similarity(img1, img1, reduction="mean"),
        torch.tensor(1.0),
    )

    score = multiscale_structural_similarity(img1, img2, reduction="none")

    reference = tf_ms_ssim(img1, img2)

    assert torch.allclose(score, reference, atol=1e-5)

@pytest.mark.parametrize(
    "num_channels, img_size, batch_sizes, seed",
    [(2, 256, [1, 5, 3], 123)],
)
def test_ms_ssim_module(num_channels, img_size, batch_sizes, seed):
    # Tests that the MS-SSIM Module API produces identical
    # results to the functional API after accumulating
    # variable-sized batches.

    rng = numpy.random.default_rng(seed)
    metric = MultiscaleStructuralSimilarity()

    imgs1 = []

    for batch_size in batch_sizes:
        imgs1.append(rand_im([batch_size, num_channels, img_size, img_size], rng))

    imgs2 = []

    for batch_size in batch_sizes:
        imgs2.append(rand_im([batch_size, num_channels, img_size, img_size], rng))

    for img1, img2 in zip(imgs1, imgs2):
        score = metric(img1, img2)
        assert torch.allclose(score, multiscale_structural_similarity(img1, img2))

    assert torch.allclose(
        metric.compute(),
        multiscale_structural_similarity(
            torch.cat(imgs1, dim=0), torch.cat(imgs2, dim=0)
        ),
    )

max_stars_repo_name: tallamjr/NeuralCompression

max_issues_repo_name: tallamjr/NeuralCompression

max_forks_repo_name: tallamjr/NeuralCompression


text:
import numpy as np
from PIL import Image
import cv2

Single_Num=100
classes=10
Num=Single_Num*classes
width, height=32,32
channel=3

SVHNArray=np.empty((Num,32,32,3))
OnedigitArray=np.empty((classes,32,32,3)) # for saving classes SVHN digits
OnedigitArrayMasks=np.empty((classes,32,32,1)) # for saving classes SVHN digit's masks

# auxiliary dataset
# imgs
"""select digits"""
temp = Image.open('onesample_VI_data/004065_0.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[0] = temp_arr
temp = Image.open('onesample_VI_data/013671_1.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[1] = temp_arr
temp = Image.open('onesample_VI_data/010555_2.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[2] = temp_arr
temp = Image.open('onesample_VI_data/007640_3.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[3] = temp_arr
temp = Image.open('onesample_VI_data/006708_4.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[4] = temp_arr
temp = Image.open('onesample_VI_data/006687_5.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[5] = temp_arr
temp = Image.open('onesample_VI_data/005710_6.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[6] = temp_arr
temp = Image.open('onesample_VI_data/005178_7.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[7] = temp_arr
temp = Image.open('onesample_VI_data/005045_8.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[8] = temp_arr
temp = Image.open('onesample_VI_data/003504_9.png')
temp_arr=np.array(temp,dtype=np.float)
OnedigitArray[9] = temp_arr

temp = Image.open('onesample_VI_data/004065_0_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[0] = temp_arr
temp = Image.open('onesample_VI_data/013671_1_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[1] = temp_arr
temp = Image.open('onesample_VI_data/010555_2_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[2] = temp_arr
temp = Image.open('onesample_VI_data/007640_3_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[3] = temp_arr
temp = Image.open('onesample_VI_data/006708_4_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[4] = temp_arr
temp = Image.open('onesample_VI_data/006687_5_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[5] = temp_arr
temp = Image.open('onesample_VI_data/005710_6_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[6] = temp_arr
temp = Image.open('onesample_VI_data/005178_7_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[7] = temp_arr
temp = Image.open('onesample_VI_data/005045_8_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[8] = temp_arr
temp = Image.open('onesample_VI_data/003504_9_mask.png')
temp_arr=np.array(temp,dtype=np.float).reshape((32,32,1))
OnedigitArrayMasks[9] = temp_arr

for imgFolderName in range(1,classes+1,1):
    for k in range(Single_Num):
        temp = Image.open('SVHNimages/{}/{:06d}.png'.format(imgFolderName,k+1))
        temp_arr=np.array(temp,dtype=np.float)
        SVHNArray[(imgFolderName-1)*Single_Num+k] = temp_arr
np.random.shuffle(SVHNArray) # shuffle

M_Array=np.empty((Num,32,32,3))
BgRndomIndexAux1=np.arange(Num)
np.random.shuffle(BgRndomIndexAux1)
# Num=1000
for i in range(classes):
    mask = np.concatenate([OnedigitArrayMasks[i], OnedigitArrayMasks[i], OnedigitArrayMasks[i]],axis=2)
    temp = OnedigitArray[i]
    mask=mask/255.
    for j in range(Single_Num):
        bg = SVHNArray[BgRndomIndexAux1[i*Single_Num+j]]
        bg = bg[0:7, 0:7, :]
        bg_img = Image.fromarray(np.uint8(bg))
        bg_img = bg_img.resize((width, height))
        bg1 = np.array(bg_img)
        # synthesis
        img=temp*mask+bg1*(1-mask)
        # cv2.imshow('tt',img/255)
        # cv2.waitKey()
        M_Array[i*Single_Num+j]=img
# exit()
SVHN_maskArray=np.empty(shape=(Num,classes)) # it is no use, but have to make for function
for i in range(Num):
    SVHN_maskArray[i]=np.zeros((classes))

M_Array=M_Array/255.

save_dir='npz_datas/'
np.savez(save_dir +'SVHN10_img_N' + str(Num) +'x' + str(width) +'x' + str(height) +'x' + str(channel) +'_testForModularity.npz', images=M_Array, masks=SVHN_maskArray)
print('done ')

max_stars_repo_name: zju-vipa/One-GORD

max_issues_repo_name: HymEric/One-GORD

max_forks_repo_name: HymEric/One-GORD


text:
from keras.models import load_model
import keras.preprocessing.text as kpt
from keras.preprocessing.sequence import pad_sequences
import sys
import os
import json
import numpy as np
from utils import ConfigurationManager, FileManager

##

global dictionary
global model

dictionaryUrl = os.path.join(FileManager.getRootUrl(), 'tmp/wordindex.json')
dictionary = json.loads(FileManager.readFile(dictionaryUrl))

modelUrl = os.path.join(FileManager.getRootUrl(), 'tmp/code_model.h5')
model = load_model(modelUrl)

def convert_text_to_index_array(text):
    # one really important thing that `text_to_word_sequence` does
    # is make all texts the same length -- in this case, the length
    # of the longest text in the set.
    wordvec = []
    for word in kpt.text_to_word_sequence(text):

        if word in dictionary:
            if dictionary[word] <= 100000:
                wordvec.append([dictionary[word]])
            else:
                wordvec.append([0])
        else:
            wordvec.append([0])

    return wordvec

##

def main():
    data = {"success": False}
    languages = ConfigurationManager.getLanguages()

    matched = 0
    totalExamples = 0

    for languageFolder in FileManager.getLanguagesFolders(FileManager.datasets['testing']['url']):
        language = str(languageFolder.name).lower()
        for exampleFolder in FileManager.getExamplesFolders(languageFolder.path):
            totalExamples += 1

            X_test = []
            originalFileContent = FileManager.readFile(FileManager.getOriginalFileUrl(exampleFolder.path))
            code_snip = originalFileContent
            # print(code_snip, file=sys.stdout)
            word_vec = convert_text_to_index_array(code_snip)
            X_test.append(word_vec)
            X_test = pad_sequences(X_test, maxlen=100)
            # print(X_test[0].reshape(1,X_test.shape[1]), file=sys.stdout)
            y_prob = model.predict(X_test[0].reshape(1, X_test.shape[1]), batch_size=1, verbose=2)[0]

            a = np.array(y_prob)
            idx = np.argmax(a)
            if str(languages[idx]) == language:
                matched += 1

            # data["predictions"] = []
            # for i in range(len(languages)):
            #     # print(languages[i], file=sys.stdout)
            #     r = {"label": languages[i], "probability": format(y_prob[i] * 100, '.2f')}
            #     data["predictions"].append(r)

    print('')
    print('')
    print('totalExamples = ' + str(totalExamples))
    print('matched = ' + str(matched))
    print('matched / totalExamples  = ' + str(matched / totalExamples))
    print('')
    print('')

##

if __name__ == "__main__":
    main()


text:
from collections import Counter
import logging
from pathlib import Path

import numpy as np
import pandas as pd

import classy.data
import classy.decision_tree

class Classifier:
    def __init__(self, path_data):
        """Create a classification pipeline directed at a preprocessed data file.

        Parameters
        ----------
        path_data : str
            Path to preprocessed data in a CSV file.
        """
        self.path_data = Path(path_data)
        self.read_and_verify_data()

    def read_and_verify_data(self):
        """Read in user data and do sanitary check."""
        classy.preprocessing.Preprocessor.read_and_verify_data(self)

    def classify(self, skip_validation=False):
        """Classify the preprocessed observations."""
        # TODO Switch from pd.DataFrame entirely to OOP approach

        # Instantiate MCFA model instance if not done yet
        model = classy.data.load("mcfa")

        # Get only the classification columns
        data_input = self.data[classy.defs.COLUMNS["all"]].values

        # Compute resonsibility matrix based on observed values only
        self.responsibility = model.predict_proba(data_input)

        # Compute latent scores
        self.data_imputed = model.impute(data_input)
        self.data_latent = model.transform(self.data_imputed)

        # Add latent scores and responsibility to input data
        for factor in range(model.n_factors):
            self.data[f"z{factor}"] = self.data_latent[:, factor]

        self.data["cluster"] = np.argmax(self.responsibility, axis=1)

        for i in range(model.n_components):
            self.data[f"cluster_{i}"] = self.responsibility[:, i]

        # Add asteroid classes based on decision tree
        self.data_classified = classy.decision_tree.assign_classes(self.data)

        # Detect features
        self.add_feature_flags()

        # Print results
        classes = Counter(self.data_classified.class_)
        results_str = ", ".join(
            [f"{count} {class_}" for class_, count in classes.most_common()]
        )
        logging.info(f"Looks like we got {results_str}")

    def plot(self):
        """Display the latent scores of the classified observations."""
        classy.plotting.plot(self.data_classified)

    def add_feature_flags(self):
        """Detect features in spectra and amend the classification."""

        for i, sample in self.data_classified.reset_index(drop=True).iterrows():
            for feature, props in classy.defs.FEATURE.items():
                if sample.class_ in props["candidates"]:

                    if sample[feature] == 1:
                        if feature == "h":
                            self.data_classified.loc[i, "class_"] = "Ch"
                            break
                        else:
                            self.data_classified.loc[i, "class_"] = (
                                self.data_classified.loc[i, "class_"] + feature
                            )

    def to_file(self):
        """Save the classified data to file."""

        if self.data_classified is None:
            raise NotClassifiedError(
                "You have to call the Classifier.classify() function first."
            )

        path_output = Path(
            self.path_data.parent
            / f"{self.path_data.stem if not self.path_data.stem.endswith('_preprocessed') else self.path_data.stem.replace( '_preprocessed', '') }_classified{self.path_data.suffix}"
        )

        self.data_classified.to_csv(path_output, index=False)
        logging.info(f"Stored classified data to {path_output.resolve()}")

class NotClassifiedError:
    pass

max_stars_repo_name: maxmahlke/classy

max_issues_repo_name: maxmahlke/classy

max_forks_repo_name: maxmahlke/classy


text:
"""
For testing intra_comp operations and 3 layers of intra_comp's forks
Visualize each comp's output with image output
"""

from comp_pixel_versions import comp_pixel_m
from intra_comp import *
from utils import imread
import cv2
import numpy as np

# -----------------------------------------------------------------------------
# Input:
IMAGE_PATH = "../images/raccoon.jpg"
# Outputs:
OUTPUT_PATH = "../images/intra_comp_a/"

# -----------------------------------------------------------------------------
# Functions

def draw_g(img_out, g_):

    for y in range(g_.shape[0]):  # loop rows, skip last row
        for x in range(g_.shape[1]):  # loop columns, skip last column
            img_out[y,x] = g_[y,x]

    return img_out.astype('uint8')

def draw_ga(img_out, g_):

    for y in range(g_.shape[0]):
        for x in range(g_.shape[1]):
            img_out[y,x] = g_[y,x]

    img_out = img_out * 180/np.pi  # convert to degrees
    img_out = (img_out /180 )*255  # scale 0 to 180 degree into 0 to 255

    return img_out.astype('uint8')

def draw_gr(img_out, g_, rng):

    for y in range(g_.shape[0]):
        for x in range(g_.shape[1]):
            # project central dert to surrounding rim derts
            img_out[(y*rng)+1:(y*rng)+1+rng,(x*rng)+1:(x*rng)+1+rng] = g_[y,x]

    return img_out.astype('uint8')

def draw_gar(img_out, g_, rng):

    for y in range(g_.shape[0]):
        for x in range(g_.shape[1]):
            # project central dert to surrounding rim derts
            img_out[(y*rng)+1:(y*rng)+3,(x*rng)+1:(x*rng)+3] = g_[y,x]

    img_out = img_out * 180/np.pi  # convert to degrees
    img_out = (img_out /180 )*255  # scale 0 to 180 degree into 0 to 255

    return img_out.astype('uint8')

def draw_m(img_out, m_):

    for y in range(m_.shape[0]):  # loop rows, skip last row
        for x in range(m_.shape[1]):  # loop columns, skip last column
            img_out[y,x] = m_[y,x]

    return img_out.astype('uint8')

def draw_ma(img_out, m_):

    for y in range(m_.shape[0]):
        for x in range(m_.shape[1]):
            img_out[y,x] = m_[y,x]

    img_out = img_out * 180/np.pi  # convert to degrees
    img_out = (img_out /180 )*255  # scale 0 to 180 degree into 0 to 255

    return img_out.astype('uint8')

def draw_mr(img_out, m_, rng):

    for y in range(m_.shape[0]):
        for x in range(m_.shape[1]):
            # project central dert to surrounding rim derts
            img_out[(y*rng)+1:(y*rng)+1+rng,(x*rng)+1:(x*rng)+1+rng] = m_[y,x]

    return img_out.astype('uint8')

def draw_mar(img_out, m_, rng):

    for y in range(m_.shape[0]):
        for x in range(m_.shape[1]):
            # project central dert to surrounding rim derts
            img_out[(y*rng)+1:(y*rng)+3,(x*rng)+1:(x*rng)+3] = m_[y,x]

    img_out = img_out * 180/np.pi  # convert to degrees
    img_out = (img_out /180 )*255  # scale 0 to 180 degree into 0 to 255

    return img_out.astype('uint8')

# -----------------------------------------------------------------------------
# Main

if __name__ == "__main__":

    print('Reading image...')
    image = imread(IMAGE_PATH)

    dert_ = comp_pixel_m(image)

    print('Processing first layer comps...')

    ga_dert_ = comp_a(dert_, fga = 0)  # if +G
    gr_dert_ = comp_r(dert_, fig = 0, root_fcr = 0)  # if -G

    print('Processing second layer comps...')
    # comp_a ->
    gaga_dert_ = comp_a(ga_dert_, fga = 1)  # if +Ga
    gg_dert_   = comp_g(ga_dert_)           # if -Ga
    # comp_r ->
    gagr_dert_ = comp_a(gr_dert_, fga = 0)                # if +Gr
    grr_dert_  = comp_r(gr_dert_, fig = 0, root_fcr = 1)  # if -Gr

    print('Processing third layer comps...')
    # comp_aga ->
    ga_gaga_dert_ = comp_a(gaga_dert_, fga = 1) # if +Gaga
    g_ga_dert_    = comp_g(gaga_dert_)          # if -Gaga
    # comp_g ->
    ga_gg_dert_ = comp_a(gg_dert_, fga = 0)                # if +Gg
    g_rg_dert_  = comp_r(gg_dert_, fig = 1, root_fcr = 0)  # if -Gg
    # comp_agr ->
    ga_gagr_dert_ = comp_a(gagr_dert_, fga = 1)  # if +Gagr
    g_gr_dert_    = comp_g(gagr_dert_)           # if -Gagr
    # comp_rr ->
    ga_grr_dert_ = comp_a(grr_dert_, fga = 0)                # if +Grr
    g_rrr_dert_  = comp_r(grr_dert_, fig = 0, root_fcr = 1)  # if -Grr：

    print('Drawing forks...')
    ini_ = np.zeros((image.shape[0], image.shape[1]))  # initialize image y, x

    # 0th layer
    g_ = draw_g(ini_.copy(), dert_[1])
    m_ = draw_g(ini_.copy(), dert_[4])
    # 1st layer
    ga_ = draw_ga(ini_.copy(), ga_dert_[4])  # angle doesn't output m
    ma_ = draw_ma(ini_.copy(), ga_dert_[9])
    gr_ = draw_gr(ini_.copy(), gr_dert_[1], rng=2)
    mr_ = draw_mr(ini_.copy(), gr_dert_[4], rng=2)
    # 2nd layer
    gaga_ = draw_ga(ini_.copy(), gaga_dert_[4])
    maga_ = draw_ma(ini_.copy(), gaga_dert_[9])
    gg_ =   draw_g(ini_.copy(),  gg_dert_[1])
    mg_ =   draw_m(ini_.copy(),  gg_dert_[4])
    gagr_ = draw_gar(ini_.copy(), gagr_dert_[4], rng=2)
    magr_ = draw_mar(ini_.copy(), gagr_dert_[9], rng=2)
    grr_ =  draw_gr(ini_.copy(),  grr_dert_[1], rng=4)
    mrr_ =  draw_mr(ini_.copy(),  grr_dert_[4], rng=4)
    # 3rd layer
    ga_gaga_ = draw_ga(ini_.copy(), ga_gaga_dert_[4])
    ma_gaga_ = draw_ma(ini_.copy(), ga_gaga_dert_[9])
    g_ga_    = draw_g(ini_.copy(),  g_ga_dert_[1])
    m_ga_    = draw_m(ini_.copy(), g_ga_dert_[4])
    ga_gg_   = draw_ga(ini_.copy(), ga_gg_dert_[4])
    ma_gg_   = draw_ma(ini_.copy(), ga_gg_dert_[9])
    g_rg_    = draw_gr(ini_.copy(), g_rg_dert_[1], rng=2)
    m_rg_    = draw_mr(ini_.copy(), g_rg_dert_[4], rng=2)
    ga_gagr_ = draw_gar(ini_.copy(), ga_gagr_dert_[4], rng=2)
    ma_gagr_ = draw_mar(ini_.copy(), ga_gagr_dert_[9], rng=2)
    g_gr_    = draw_gr(ini_.copy(),  g_gr_dert_[1], rng=2)
    m_gr_    = draw_mr(ini_.copy(),  g_gr_dert_[4], rng=2)
    ga_grr_  = draw_gar(ini_.copy(), ga_grr_dert_[4], rng=4)
    ma_grr_  = draw_mar(ini_.copy(), ga_grr_dert_[9], rng=4)
    g_rrr_   = draw_gr(ini_.copy(),  g_rrr_dert_[1], rng=8)
    m_rrr_   = draw_mr(ini_.copy(),  g_rrr_dert_[4], rng=8)

    # save to disk
    cv2.imwrite(OUTPUT_PATH+'0_g.png', g_)
    cv2.imwrite(OUTPUT_PATH+'1_m.png', m_)
    cv2.imwrite(OUTPUT_PATH+'2_ga.png', ga_)
    cv2.imwrite(OUTPUT_PATH+'3_ma.png', ma_)
    cv2.imwrite(OUTPUT_PATH+'4_gr.png', gr_)
    cv2.imwrite(OUTPUT_PATH+'5_mr.png', mr_)
    cv2.imwrite(OUTPUT_PATH+'6_gaga.png', gaga_)
    cv2.imwrite(OUTPUT_PATH+'7_maga.png', gaga_)
    cv2.imwrite(OUTPUT_PATH+'8_gg.png', gg_)
    cv2.imwrite(OUTPUT_PATH+'9_mg.png', mg_)
    cv2.imwrite(OUTPUT_PATH+'10_gagr.png', gagr_)
    cv2.imwrite(OUTPUT_PATH+'11_magr.png', magr_)
    cv2.imwrite(OUTPUT_PATH+'12_grr.png', grr_)
    cv2.imwrite(OUTPUT_PATH+'13_mrr.png', mrr_)
    cv2.imwrite(OUTPUT_PATH+'14_ga_gaga.png', ga_gaga_)
    cv2.imwrite(OUTPUT_PATH+'15_ma_gaga.png', ma_gaga_)
    cv2.imwrite(OUTPUT_PATH+'16_g_ga.png', g_ga_)
    cv2.imwrite(OUTPUT_PATH+'17_m_ga.png', m_ga_)
    cv2.imwrite(OUTPUT_PATH+'18_ga_gg.png', ga_gg_)
    cv2.imwrite(OUTPUT_PATH+'19_ma_gg.png', ma_gg_)
    cv2.imwrite(OUTPUT_PATH+'20_g_rg.png', g_rg_)
    cv2.imwrite(OUTPUT_PATH+'21_m_rg.png', m_rg_)
    cv2.imwrite(OUTPUT_PATH+'22_ga_gagr.png', ga_gagr_)
    cv2.imwrite(OUTPUT_PATH+'23_ma_gagr.png', ma_gagr_)
    cv2.imwrite(OUTPUT_PATH+'24_g_gr.png', g_gr_)
    cv2.imwrite(OUTPUT_PATH+'25_m_gr.png', m_gr_)
    cv2.imwrite(OUTPUT_PATH+'26_ga_grr.png', ga_grr_)
    cv2.imwrite(OUTPUT_PATH+'27_ma_grr.png', ma_grr_)
    cv2.imwrite(OUTPUT_PATH+'28_g_rrr.png', g_rrr_)
    cv2.imwrite(OUTPUT_PATH+'29_m_rrr.png', m_rrr_)

    print('Terminating...')

def add_colour(img_comp,size_y,size_x):
    img_colour = np.zeros((3,size_y,size_x))
    img_colour[2] = img_comp
    img_colour[2][img_colour[2]<255] = 0
    img_colour[2][img_colour[2]>0] = 205
    img_colour[1] = img_comp
    img_colour[1][img_colour[1]==255] = 0
    img_colour[1][img_colour[1]>0] = 255
    img_colour = np.rollaxis(img_colour,0,3).astype('uint8')

    return img_colour

'''
    dertm_ = np.zeros((5, dert_.shape[1], dert_.shape[2])) # add extra m channel
    dert_[0:4] = dert_[:]
'''

max_issues_repo_name: Risingabhi/CogAlg

max_forks_repo_name: Risingabhi/CogAlg


text:
import math
import numpy as np
import torch as t
import torch.nn as nn
import torch.nn.functional as F

# Import FusedLayerNorm if we have apex, otherwise use regular LayerNorm
try:
    from apex.normalization import FusedLayerNorm
    print("Using apex FusedLayerNorm")
except ImportError:
    from torch.nn import LayerNorm as FusedLayerNorm

class LayerNorm(FusedLayerNorm):
    def __init__(self, normalized_shape, eps=1e-5, elementwise_affine=True):
        super().__init__(normalized_shape, eps=eps, elementwise_affine=elementwise_affine)
        self.width = np.prod(normalized_shape)
        self.max_numel = 65535*self.width

    def forward(self, input):
        if input.numel() > self.max_numel:
            return F.layer_norm(input.float(), self.normalized_shape, self.weight, self.bias, self.eps).type_as(input)
        else:
            return super(LayerNorm, self).forward(input.float()).type_as(input)

def gelu(x):
    return 0.5 * x * (1 + t.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * t.pow(x, 3))))

def swish(x):
    return x * t.sigmoid(x)

@t.jit.script
def quick_gelu(x):
    return x * t.sigmoid(1.702 * x)

@t.jit.script
def quick_gelu_bwd(x, grad_output):
    sig = t.sigmoid(1.702 * x)
    return grad_output * sig * (1.702 * x * (1 - sig) + 1.)

class QuickGelu(t.autograd.Function):
    @staticmethod
    def forward(ctx, x):
        ctx.save_for_backward(x)
        return quick_gelu(x)

    @staticmethod
    def backward(ctx, grad_output):
        return quick_gelu_bwd(ctx.saved_tensors[0], grad_output)

def memory_efficient_quick_gelu(x):
    return QuickGelu.apply(x)

ACT_FNS = {
    'relu': t.nn.functional.relu,
    'swish': swish,
    'gelu': gelu,
    'quick_gelu': memory_efficient_quick_gelu #quick_gelu
}

def _move_to_gpu_and_convert_conv_weights_to_fp16(l):
    l.cuda()
    if isinstance(l, Conv1D):
        l.w.data = l.w.data.half()

def _convert_conv_weights_to_fp32(l):
    if isinstance(l, Conv1D):
        l.w.data = l.w.data.float()

def _convert_conv_weights_to_fp16(l):
    if isinstance(l, Conv1D):
        l.w.data = l.w.data.half()

def _convert_embedding_weights_to_fp16(l):
    if isinstance(l, t.nn.Embedding):
        l.weight.data = l.weight.data.half()

def _convert_embedding_weights_to_fp32(l):
    if isinstance(l, t.nn.Embedding):
        l.weight.data = l.weight.data.float()

class Conv1D(nn.Module):
    def __init__(self, n_in, n_out, zero_out=False, init_scale=1.0):
        super(Conv1D, self).__init__()
        self.n_in = n_in
        self.n_out = n_out
        if zero_out:
            w = t.zeros(n_in, n_out)
        else:
            w = t.empty(n_in, n_out)
            nn.init.normal_(w, std=0.02 * init_scale)
        b = t.zeros(n_out)
        self.w = nn.Parameter(w)
        self.b = nn.Parameter(b)

    def forward(self, x):
        size_out = (*x.size()[:-1], self.n_out)
        x = t.addmm(self.b.type_as(x), x.view(-1, x.size(-1)), self.w.type_as(x)) # If x if float then float else half
        x = x.view(*size_out)
        return x

# For large contexts, mask's can take up memory, so you can make a single saved mask for all layers
class Mask(nn.Module):
    def __init__(self, n_ctx):
        super().__init__()
        self.register_buffer('b', t.tril(t.ones(n_ctx, n_ctx)).view(1, 1, n_ctx, n_ctx))

    def forward(self, w):
        w = w * self.b + -1e9 * (1 - self.b)  # For fp16 do w = w.float().masked_fill(self.b, float('-inf')
        return w

def filter_logits(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
    """ Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
        Args:
            logits: logits distribution shape (vocabulary size)
            top_k >0: keep only top k tokens with highest probability (top-k filtering).
            top_p >0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
    """
    #assert logits.dim() == 2  # batch size 1 for now - could be updated for more but the code would be less clear
    logits = logits.clone()
    top_k = min(top_k, logits.size(-1))  # Safety check
    assert (top_k == 0) or (top_p == 0.0)
    if top_k > 0:
        # Remove all tokens with a probability less than the last token of the top-k
        indices_to_remove = logits < t.topk(logits, top_k, dim=-1)[0][..., -1:]
        logits[indices_to_remove] = filter_value

    if top_p > 0.0:
        sorted_logits, sorted_indices = t.sort(logits, descending=True, dim=-1)
        cumulative_probs = t.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)

        # Remove tokens with cumulative probability above the threshold
        sorted_indices_to_remove = cumulative_probs > top_p
        # Shift the indices to the right to keep also the first token above the threshold
        sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
        sorted_indices_to_remove[..., 0] = 0

        #indices_to_remove = sorted_indices[sorted_indices_to_remove]
        indices_to_remove = t.zeros_like(logits, dtype=t.uint8).scatter_(dim=-1, index=sorted_indices, src=sorted_indices_to_remove)
        logits[indices_to_remove] = filter_value
    return logits

max_stars_repo_name: peterlazzarino/jukebox

max_issues_repo_name: peterlazzarino/jukebox

max_forks_repo_name: peterlazzarino/jukebox


text:
import numpy as np
import tensorflow as tf
import tflearn
from tensorflow.contrib.layers.python.layers import batch_norm
import tensorflow.contrib as tf_contrib

weight_regularizer = tf_contrib.layers.l2_regularizer(0.0001)

# for reuse

def uniform(shape, scale=0.05, name=None):
	"""Uniform init."""
	initial = tf.random_uniform(shape, minval=-scale, maxval=scale, dtype=tf.float32)
	return tf.get_variable(name, shape=shape, initializer = tf.initializer.random_uniform(minval=-scale, maxval = scale), dtype=tf.float32)
	#return tf.Variable(initial, name=name, dtype=tf.float32)

def glorot(shape, name=None):
	"""Glorot & Bengio (AISTATS 2010) init."""
	#init_range = np.sqrt(6.0/(shape[0]+shape[1]))
	#initial = tf.random_uniform(shape, minval=-init_range, maxval=init_range, dtype=tf.float32)
	return tf.get_variable(name, shape = shape, initializer = tf.glorot_uniform_initializer(), dtype=tf.float32)
	#return tf.Variable(initial, name=name)

def zeros(shape, name=None):
	"""All zeros."""
	initial = tf.zeros(shape, dtype=tf.float32)
	return tf.get_variable(name, shape=shape, initializer=tf.constant_initializer(0.0), dtype=tf.float32)

	#return tf.Variable(initial, name=name)

def ones(shape, name=None):
	"""All ones."""
	initial = tf.ones(shape, dtype=tf.float32)
	return tf.get_variable(name, shape=shape, initializer=tf.constant_initializer(1.0), dtype=tf.float32)

	#return tf.Variable(initial, name=name)

def dot(x, y, sparse=False):
	"""Wrapper for tf.matmul (sparse vs dense)."""
	if sparse:
		res = tf.sparse_tensor_dense_matmul(x, y)
	else:
		res = tf.matmul(x, y)
	return res

def create_conv_layer(name, input_tensor, in_channels, out_channels, is_training = True, activation='relu', kx = 3, ky = 3, stride_x = 2, stride_y = 2, batchnorm=False, padding='VALID', add=None, deconv = False):
	if deconv == False:
		input_tensor = tf.pad(input_tensor, [[0, 0], [kx/2, kx/2], [kx/2, kx/2], [0, 0]], mode="CONSTANT")

	weights = tf.get_variable(name+'weights', shape=[kx, ky, in_channels, out_channels],
			initializer=tf.truncated_normal_initializer(stddev=np.sqrt(0.02 / kx / ky / in_channels)),
			regularizer=weight_regularizer,
			dtype=tf.float32
	)
	biases = tf.get_variable(name+'biases', shape=[out_channels], initializer=tf.constant_initializer(0.0),regularizer=weight_regularizer, dtype=tf.float32)

	if deconv == False:
		t = tf.nn.conv2d(input_tensor, weights, [1, stride_x, stride_y, 1], padding=padding)
		s = tf.nn.bias_add(t, biases)

	else:
		batch = tf.shape(input_tensor)[0]
		size = tf.shape(input_tensor)[1]

		#print(input_tensor)
		#print(tf.transpose(weights,perm=[0,1,3,2]))

		t = tf.nn.conv2d_transpose(input_tensor, tf.transpose(weights,perm=[0,1,3,2]),[batch, size * stride_x, size * stride_y, out_channels], [1, stride_x, stride_y, 1],
				padding='SAME', data_format = "NHWC")

		# t = tf.nn.conv2d_transpose(input_tensor, tf.transpose(weights,perm=[0,1,3,2]),tf.tensor([batch, size * stride_x, size * stride_y, out_channels]), [1, stride_x, stride_y, 1],
		# 		padding='SAME', data_format = "NHWC")

		s = tf.nn.bias_add(t, biases)

	if add is not None: # res
		s = s + add

	if batchnorm:
		#print("use batchnorm ", name)
		#n = batch_norm(s, decay = 0.99, center=True, scale=True, updates_collections=None, is_training=is_training, epsilon=0.01)
		# fused = False
		#n = batch_norm(s, decay = 0.99, center=True, scale=True, updates_collections=None, is_training=is_training, scope = name, fused = False)
		n = batch_norm(s, decay = 0.99, center=True, scale=True, updates_collections=None, is_training=is_training, fused = False)

		# remove 'scope = name' to support old checkpoints.

		#n = tf.layers.batch_normalization(s, training=is_training, renorm=True)
	else:
		n = s

	if activation == 'relu':
			return tf.nn.relu(n), weights, biases
	elif activation == 'sigmoid':
			return tf.nn.sigmoid(n), weights, biases
	elif activation == 'tanh':
			return tf.nn.tanh(n), weights, biases
	elif activation == 'linear':
			return n, weights, biases


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May  5 11:05:20 2021

@author: Marc M. E. Cormier
"""

from reader import ParseNeware
import streamlit as st
from bokeh.plotting import figure, save
import bokeh.palettes as bp
from bokeh.io import curdoc, export_png
import numpy as np
from pathlib import Path
#import shutil as sh
#import os
import matplotlib.pyplot as plt
#os.environ['MPLCONFIGDIR'] = "./.matplotlib"
import matplotlib

#cfgdir = matplotlib.get_configdir()
#st.write(cfgdir)
#cwd = os.getcwd()
#sh.copy("{}/mpl_styles/grapher.mplstyle".format(cwd),
#        "{}/stylelib/grapher.mplstyle".format(cfgdir))

# The below HACK still does not solve the failure of Streamlit when
# running on Streamlit share. Large files crash!
# HACK This only works when we've installed streamlit with pipenv, so the
# permissions during install are the same as the running process
STREAMLIT_STATIC_PATH = Path(st.__path__[0]) / 'static'
# We create a downloads directory within the streamlit static asset directory
# and we write output files to it
DOWNLOADS_PATH = (STREAMLIT_STATIC_PATH / "downloads")
if not DOWNLOADS_PATH.is_dir():
    DOWNLOADS_PATH.mkdir()

st.write("""
         # Let's plot some Neware data!
         """)

fdata = st.file_uploader("Load your Neware file here!")

@st.cache(persist=True)
def read_data(uploaded_bytes, cell_id):
    return ParseNeware(cell_id, all_lines=uploaded_bytes,
                       tmppath=DOWNLOADS_PATH)

@st.cache(persist=True)
def voltage_curves(cycnums, active_mass=None):
    cap_list = []
    volt_list = []
    for i in range(len(cycnums)):
        cap, volt = nd.get_vcurve(cycnum=cycnums[i], active_mass=active_mass)
        cap_list.append(cap)
        volt_list.append(volt)

    return cap_list, volt_list

@st.cache(persist=True)
def dQdV(cycnums, avgstride=None, active_mass=None):
    volt_list = []
    dqdv_list = []
    for i  in range(len(cycnums)):
        volt, dqdv = nd.get_dQdV(cycnum=cycnums[i], active_mass=active_mass,
                                 avgstride=smooth)
        volt_list.append(volt)
        dqdv_list.append(dqdv)

    return volt_list, dqdv_list

if fdata is not None:
    #nd = ParseNeware("Cell_ID", all_lines=fdata)
    nd = read_data(fdata, "Cell_ID")
    st.write("Found {} charge C-rates".format(nd.chg_crates))
    st.write("Found {} discharge C-rates".format(nd.dis_crates))

    st.write("""
             ### Successfully loaded Neware file.
             """)

    plot_opts = st.sidebar.selectbox("What would you like to plot?",
                                     ('Discharge capacity',
                                      #'Specific discharge capacity',
                                      'V-Q',
                                      'dQ/dV'))

    rate_type = st.sidebar.selectbox("For which cycle component do you want to select rates?",
                                     ('cycle', 'charge', 'discharge'))
    type_rates = nd.get_rates(cyctype=rate_type)
    rates = ['All'] + type_rates
    st.write("Found {} {} rates.".format(rates, rate_type))
    rate = st.sidebar.selectbox("Which C-rate would you like to see?",
                                tuple(rates))

    ncycs = nd.get_ncyc()
    if rate == 'All':
        cyc_nums = np.arange(1, ncycs+1)
    else:
        cyc_nums = np.array(nd.select_by_rate(rate, cyctype=rate_type))

    cyc_range = st.sidebar.slider("Cycle Numbers", 1, ncycs, (1, ncycs), 1)
    inds = np.where((cyc_nums <= cyc_range[1]) & (cyc_nums >= cyc_range[0]))[0]
    cycnums = cyc_nums[inds]
    num_cycs = len(cycnums)
    st.write("Plotting {0} cycles within range: ({1}, {2})".format(rate,
                                                               cyc_range[0],
                                                               cyc_range[1]))

    active_mass = st.sidebar.number_input("Active material mass (in grams):")
    if active_mass == 0.0:
        active_mass = None
    else:
        st.write("Calculating specific capacity using {} g active material".format(active_mass))
    #cycnums = np.arange(cyc_range[0], cyc_range[1])
    #st.write("Cycle numbers", cycnums)
    cmap = st.sidebar.selectbox("Color pallette",
                                ('Default', 'viridis', 'cividis'))
    if cmap == 'Default':
        avail_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
        colors = avail_colors*int(num_cycs/len(avail_colors) + 1)
    elif cmap == 'viridis':
        colors = bp.viridis(num_cycs)
    elif cmap == 'cividis':
        colors = bp.cividis(num_cycs)

    #else:
    #    colors = plt.get_cmap(cmap)(np.linspace(0,1,num_cycs))

    smooth = st.sidebar.slider("dQ/dV moving average window width", 0, 10)
    if smooth == 0:
        smooth = None

#    with plt.style.context('grapher'):
#        fig, ax = plt.subplots(figsize=(5,4))
    #curdoc().theme = 'dark_minimal' # not working
    p = figure(plot_width=800, plot_height=400)

    if plot_opts == 'V-Q':
        caps, volts = voltage_curves(cycnums, active_mass=active_mass)

        if active_mass is not None:
            p.xaxis.axis_label = 'Specific Capacity (mAh/g)'
        else:
            p.xaxis.axis_label = 'Capacity (mAh)'
        p.yaxis.axis_label = 'Voltage (V)'
        for cap, volt, color in zip(caps, volts, colors):
            p.line(cap, volt, color=color, line_width=2.0)

    elif plot_opts == 'dQ/dV':
        volts, dqdvs = dQdV(cycnums, active_mass=active_mass,
                            avgstride=smooth)

        p.xaxis.axis_label = 'Voltage (V)'
        if active_mass is not None:
            p.yaxis.axis_label = 'dQ/dV (mAh/V/g)'
        else:
            p.yaxis.axis_label = 'dQ/dV (mAh/V)'
        for volt, dqdv, color in zip(volts, dqdvs, colors):
            p.line(volt, dqdv, color=color, line_width=2.0)

    elif plot_opts == 'Discharge capacity':
        cycs, dcap = nd.get_discap(cycnums=cycnums, active_mass=active_mass)
        if active_mass is not None:
            p.yaxis.axis_label = 'Specific Capacity (mAh/g)'
        else:
            p.yaxis.axis_label = 'Capacity (mAh)'
        p.xaxis.axis_label = 'Cycle Number'
        p.circle(cycs, dcap, size=4, color="black", alpha=0.75)

    #elif plot_opts == 'Discharge capacity':
        #x, y = nd.get_discap(specific=True)
        #p.circle(x, y, size=4, color="black", alpha=0.75)
        #ax.plot(x, y, "o")
        #ax.set_xlabel('Cycle Number')
        #ax.set_ylabel('Specific Capacity (mAh/g)')

        #ax.plot(x, y)

    #st.pyplot(fig)
    st.bokeh_chart(p, use_container_width=True)
    rel_path = st.text_input("Save figure to: C://")
    savepng_button = st.button("Save figure to png!")
    savehtml_button = st.button("Save figure to html! (interactive plot)")

    home_path = Path("/home/mmemc")
    fig_path = home_path / rel_path
    if savehtml_button is True:
        save(p, filename="{}.html".format(fig_path))
    if savepng_button is True:
        export_png(p, filename="{}.png".format(fig_path))

max_stars_repo_name: mmemcormier/cell-explorer

max_issues_repo_name: mmemcormier/cell-explorer

max_forks_repo_name: mmemcormier/cell-explorer


text:
"""
Module for the parametric Dynamic Mode Decomposition.
"""
import pickle
import numpy as np

class ParametricDMD:
    """
    Implementation of the parametric Dynamic Mode Decomposition proposed in
    arXiv:2110.09155v1. Both the *monolithic* and *partitioned* approaches are
    available, see the documentation of the parameter `dmd` for more details.

    :param dmd: Instance(s) of :class:`dmdbase.DMDBase`, used by the
        paramtric DMD for the prediction of future spatial modal coefficients.
        If `dmd` is a `list` the *partitioned* approach is selected, in this
        case the number of parameters in the training set should be equal to
        the number of DMD instances provided. If `dmd` is not a list, we employ
        the monolithic approach.
    :type dmd: DMDBase
    :param spatial_pod: Instance of an object usable for the generation of a
        ROM of the given dataset (see for instance the class
        `POD <https://mathlab.github.io/EZyRB/pod.html>`_ from the Python
        library `EZyRB <https://github.com/mathLab/EZyRB>`_).
    :param approximation: An interpolator following the standard
        learning-prediction pattern (`fit()` -> `predict()`). For some
        convenient wrappers see those implemented in
        `EZyRB <https://github.com/mathLab/EZyRB>`_).
    """

    def __init__(self, dmd, spatial_pod, approximation):
        self._dmd = dmd
        self._spatial_pod = spatial_pod
        self._approximation = approximation

        self._training_parameters = None
        self._parameters = None
        self._ntrain = None
        self._time_instants = None
        self._space_dim = None

    @property
    def is_partitioned(self):
        """
        Return `True` if this instance is partitioned, `False` if it is
        monolithic.

        :type: bool
        """
        return self._dmd is not None and isinstance(self._dmd, list)

    @property
    def _reference_dmd(self):
        """
        An object used as a reference for several properties like
        :func:`dmd_time` and :func:`dmd_timesteps`. If this instance is
        monolithic the returned value is `self._dmd`, otherwise it is the first
        item of the list `self._dmd`.

        :return: The object used as a reference.
        :rtype: pydmd.DMDBase
        """
        if self.is_partitioned:
            return self._dmd[0]
        return self._dmd

    @property
    def dmd_time(self):
        """
        The time dictionary used by the reference DMD instance (see also
        :func:`_reference_dmd`). Note that when you set this attribute the
        value is set only for the reference DMD (see :func:`_reference_dmd`),
        however when :func:`_predict_modal_coefficients` is called the values
        of all DMDs become consistent.

        :getter: Return the time dictionary used by the reference DMD instance.
        :setter: Set the given time dictionary in the field `dmd_time` for all
            DMD instances.
        :type: pydmd.dmdbase.DMDTimeDict
        """
        return self._reference_dmd.dmd_time

    @dmd_time.setter
    def dmd_time(self, value):
        self._reference_dmd.dmd_time = value

    @property
    def dmd_timesteps(self):
        """
        The timesteps in the output of this instance, which coincides with the
        timesteps in the output of the reference of this instance (see
        :func:`_reference_dmd`).

        :return: The timesteps in the output of this instance.
        :rtype: list
        """
        return self._reference_dmd.dmd_timesteps

    @property
    def original_time(self):
        """
        The original time dictionary used by this instance, which coincides
        with the original dictionary used by the reference of this instance
        (see :func:`_reference_dmd`).

        :return: The original time dictionary used by this instance.
        :rtype: dict
        """
        return self._reference_dmd.original_time

    @property
    def original_timesteps(self):
        """
        The original timesteps in the input fed to this instance, which
        coincides with the original timesteps in the input fed to the reference
        of this instance (see :func:`_reference_dmd`).

        :return: The original timesteps in the input fed to this instance.
        :rtype: list
        """
        return self._reference_dmd.original_timesteps

    @property
    def training_parameters(self):
        """
        The original parameters passed when `self.fit` was called, represented
        as a 2D array (the index of the parameter vary along the first
        dimension).

        :type: numpy.ndarray
        """
        return self._training_parameters

    def _set_training_parameters(self, params):
        """
        Set the value of `self._original_parameters`, while checking that the
        value provided is a 2D array.

        :param numpy.ndarray: A 2D array which contains the original
            parameters.
        """
        if isinstance(params, list):
            params = np.array(params)
        if params.ndim == 1:
            params = params[:, None]
        if params.ndim > 2:
            raise ValueError("Parameters must be stored in 2D arrays.")

        self._training_parameters = params

    @property
    def parameters(self):
        """
        The new parameters to be used in `reconstructed_data`, represented
        as a 2D array (the index of the parameter vary along the first
        dimension).

        :getter: Return the current parameters.
        :setter: Change the current parameters.
        :type: numpy.ndarray
        """
        return self._parameters if hasattr(self, "_parameters") else None

    @parameters.setter
    def parameters(self, value):
        if isinstance(value, list):
            value = np.array(value)
        if value.ndim == 1:
            value = value[:, None]
        elif value.ndim > 2:
            raise ValueError("Parameters must be stored in 2D arrays.")

        self._parameters = value

    def _arrange_parametric_snapshots(self, X):
        """
        Arrange the given parametric snapshots (see :func:`fit` for an overview
        of the shape of `X`) into a 2D matrix in which the shape is distributed
        as follows:

        - 0: Space;
        - 1: Time/Parameter.

        Time varies faster than the parameter along the columns of the matrix.

        An overview of the shape of the resulting matrix:

         .. math::

            M = \\begin{bmatrix}
                    x_1(t_1,\\mu_1) & \dots & x_1(t_n,\\mu_1) & x_1(t_1,\\mu_1)
                        & \dots & x_1(t_{n-1},\\mu_k) & x_1(t_n,\\mu_k)\\\\
                    \\vdots & \\dots & \\vdots & \\vdots & \\dots & \\vdots
                        & \\dots\\\\
                    x_m(t_1,\\mu_1) & \dots & x_m(t_n,\\mu_1) & x_m(t_1,\\mu_1)
                        & \dots & x_m(t_{n-1},\\mu_k) & x_m(t_n,\\mu_k)
                \\end{bmatrix}

        :math:`x(t, \mu) \in \mathbb{R}^m` is the functon which represents the
        parametric system at time :math:`t` with the parameter :math:`\\mu`.

        :param X: Parametric snapshots.
        :type X: numpy.ndarray
        :return: The given parametric snapshots rearranged in a 2D matrix.
        :rtype: numpy.ndarray
        """
        return np.reshape(
            np.ravel(X, "C"),
            (self._space_dim, self._time_instants * self._ntrain),
            "F",
        )

    def _training_modal_coefficients(self, space_timemu):
        """
        Compute the POD modal coefficient from the given matrix, and put
        the resulting coefficients (along with their time evolution in matrix
        form) into a list.

        In symbols, from the given matrix :math:`X^x_{t,\mu} \in
        \mathbb{R}^{m \\times nk}` we compute the modal
        coefficients corresponding to its columns. At this point we have
        something like this:

        .. math::

            \\widetilde{X}^s_{t,\mu} = \\begin{bmatrix}
                    \\widetilde{x}_1(t_1,\\mu_1), & \dots &
                        \\widetilde{x}_1(t_n,\\mu_1), &
                        \\widetilde{x}_1(t_1,\\mu_1), & \dots &
                        \\widetilde{x}_1(t_{n-1},\\mu_k), &
                        \\widetilde{x}_1(t_n,\\mu_k)\\\\
                    \\vdots & \\dots & \\vdots & \\vdots & \\dots & \\vdots &
                        \\dots\\\\
                    \\widetilde{x}_p(t_1,\\mu_1), & \dots & x_p(t_n,\\mu_1) &
                        \\widetilde{x}_p(t_1,\\mu_1), & \dots &
                        \\widetilde{x}_p(t_{n-1},\\mu_k), &
                        \\widetilde{x}_p(t_n,\\mu_k)
                \\end{bmatrix} \in \mathbb{R}^{p \\times nk}

        Detecting the sub-matrices corresponding to the time evolution of the
        POD modal coefficients corresponding to a particular realization of the
        system for some parameter :math:`\\mu_i`, we may rewrite this matrix as
        follows:

        .. math::

            \\widetilde{X}^s_{t,\mu} = \\begin{bmatrix}
                    \\widetilde{X}_{\\mu_1}, & \dots & \\widetilde{X}_{\\mu_1}
            \\end{bmatrix}

        The returned list contains the matrices
        :math:`\\widetilde{X}_{\\mu_i} \in \\mathbb{p \\times n}`.

        :param space_timemu: A matrix containing parametric/time snapshots as
            returned by :func:`_arrange_parametric_snapshots`.
        :type space_timemu: numpy.ndarray
        :return: A list of matrices. Each matrix contain the time evolution of
            the POD modal coefficients corresponding to a parameter from the
            training set.
        :rtype: list
        """

        spatial_modal_coefficients = self._spatial_pod.fit(
            space_timemu
        ).reduce(space_timemu)
        return np.split(spatial_modal_coefficients, self._ntrain, axis=1)

    def _fit_dmd(self, training_modal_coefficients):
        """
        Train the DMD instance(s) on the given training modal coefficients.

        :param training_modal_coefficients: Matrix (or list of matrices) of
            modal coefficients. The time varies along columns.
        :type training_modal_coefficients: numpy.ndarray
        """

        if self.is_partitioned:
            # partitioned parametric DMD
            for dmd, data in zip(self._dmd, training_modal_coefficients):
                dmd.fit(data)
        else:
            spacemu_time = np.vstack(training_modal_coefficients)
            self._dmd.fit(spacemu_time)

    def fit(self, X, training_parameters):
        """
        Compute the parametric Dynamic Modes Decomposition from the input data
        stored in the array `X`. The shape of the parameter `X` must be
        used as follows:

        - 0: Parameters;
        - 1: Time;
        - 2: Space.

        Which means that along the first axis the parameter varies, along the
        second axis varies the time, and along the last axis varies the
        space (i.e. the dimensionality of a single snapshot of the dynamical
        system).

        The second parameter contains the list of training parameters
        corresponding to the given array of snapshots `X`. It is fundamental
        that `X.shape[0] == len(training_parameters)`, otherwise the number
        of parametric snapshots would be different than the number of training
        parameters, which obviously cannot happen.

        :param numpy.ndarray X: The input snapshots, in multiple time instants
            and parameters.
        :param numpy.ndarray training_parameters: The parameters used for the
            training, corresponding to the snapshots in `X`.
        """

        if X.shape[0] != len(training_parameters):
            raise ValueError(
                "Unexpected number of snapshots for the given"
                "parameters. Received {} parameters, and {} snapshots".format(
                    len(training_parameters), X.shape[0]
                )
            )

        # we store these values for faster access
        self._ntrain, self._time_instants, self._space_dim = X.shape
        if self.is_partitioned and self._ntrain != len(self._dmd):
            raise ValueError(
                "Invalid number of DMD instances provided: "
                "expected n_train={}, got {}".format(
                    self._ntrain, len(self._dmd)
                )
            )

        # store the training parameters: they will be used in
        # `reconstructed_data`
        self._set_training_parameters(training_parameters)

        # arrange the parametric snapshots in a convenient way to perform POD
        space_timemu = self._arrange_parametric_snapshots(X)

        # obtain POD modal coefficients from the training set
        training_modal_coefficients = self._training_modal_coefficients(
            space_timemu
        )

        # fit DMD(s) with POD modal coefficients
        self._fit_dmd(training_modal_coefficients)

    @property
    def reconstructed_data(self):
        """
        Get the reconstructed data, for the time instants specified in
        `dmd_time`, and the parameters stored in `parameters`.

        The shape of the returned data is distributed as follows:

        - 0: Parameters;
        - 1: Time;
        - 2: Space.

        :return: Snapshots predicted/interpolated using parametric Dynamic Mode
            Decomposition.
        :rtype: numpy.ndarray
        """
        forecasted_modal_coefficients = self._predict_modal_coefficients()
        interpolated_pod_modal_coefficients = (
            self._interpolate_missing_modal_coefficients(
                forecasted_modal_coefficients
            )
        )

        interpolated_pod_modal_coefficients = np.swapaxes(
            interpolated_pod_modal_coefficients, 0, 1
        )

        return np.apply_along_axis(
            self._spatial_pod.expand, 2, interpolated_pod_modal_coefficients
        )

    def save(self, fname):
        """
        Save the object to `fname` using the pickle module.

        :param str fname: the name of file where the reduced order model will
            be saved.

        Example:

        >>> from pydmd import ParametricDMD
        >>> pdmd = ParametricDMD(...) #  Construct here the rom
        >>> pdmd.fit(...)
        >>> pdmd.save('pydmd.pdmd')
        """
        with open(fname, "wb") as output:
            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)

    @staticmethod
    def load(fname):
        """
        Load the object from `fname` using the pickle module.

        :return: The `ReducedOrderModel` loaded

        Example:

        >>> from pydmd import ParametricDMD
        >>> pdmd = ParametricDMD.load('pydmd.pdmd')
        >>> print(pdmd.reconstructed_data)
        """
        with open(fname, "rb") as output:
            dmd = pickle.load(output)

        return dmd

    def _predict_modal_coefficients(self):
        """
        Predict future spatial modal coefficients in the time instants in
        `dmd_time`.

        :return: Predicted spatial modal coefficients.
        :rtype: numpy.ndarray
        """
        if self.is_partitioned:
            for dmd in self._dmd:
                # we want to "bound" this DMD objects "dmd_time"
                dmd.dmd_time = self._reference_dmd.dmd_time
        if self.is_partitioned:
            return np.vstack(
                list(map(lambda dmd: dmd.reconstructed_data, self._dmd))
            )
        return self._dmd.reconstructed_data

    def _interpolate_missing_modal_coefficients(
        self, forecasted_modal_coefficients
    ):
        """
        Interpolate spatial modal coefficients for the (untested) parameters
        stored in `parameters`. The interpolation uses the interpolator
        provided in the constructor of this instance.

        The returned value is a 3D tensor, its shape is used as follows:

        - 0: Time;
        - 1: Parameters;
        - 2: POD reduced space.

        :param numpy.ndarray forecasted_modal_coefficients: An array of spatial
            modal coefficients for tested parameters.
        :return: An array of (interpolated) spatial modal coefficients for
            untested parameters.
        :rtype: numpy.ndarray
        """
        if forecasted_modal_coefficients.shape[1] != len(self.dmd_timesteps):
            raise ValueError(
                "Invalid number of time instants provided: "
                "expected {}, got {}.".format(
                    forecasted_modal_coefficients.shape[1],
                    len(self.dmd_timesteps),
                )
            )

        approx = self._approximation

        forecasted_modal_coefficients = np.array(
            np.split(forecasted_modal_coefficients, self._ntrain, axis=0)
        )

        def interpolate_future_pod_coefficients(future_training_coefficients):
            approx.fit(
                self.training_parameters, future_training_coefficients.T
            )
            return approx.predict(self.parameters)

        return np.array(
            list(
                map(
                    interpolate_future_pod_coefficients,
                    forecasted_modal_coefficients.T,
                )
            )
        )

max_stars_repo_name: kathryn-garside/PyDMD-fork

max_issues_repo_name: kathryn-garside/PyDMD-fork

max_forks_repo_name: kathryn-garside/PyDMD-fork