text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import itertools
import re

import botocore
import numpy as np
import os
import shutil
import tensorflow as tf
import cv2
import tqdm

import tensorpack.utils.viz as tpviz
from dataset.data_config import DataConfig
from dataset.data_configs_dict import data_conf_tooth_only, data_conf_gingivitis_only
from tensorpack.predict import MultiTowerOfflinePredictor, OfflinePredictor, PredictConfig
from tensorpack.tfutils import SmartInit, get_tf_version_tuple
from tensorpack.tfutils.export import ModelExporter
from tensorpack.utils import fs, logger

from dataset import DatasetRegistry, register_coco, register_balloon
from config import config as cfg
from config import finalize_configs
from data import get_eval_dataflow, get_train_dataflow
from eval import DetectionResult, multithread_predict_dataflow, predict_image, run_resize_image, predict_resized_image
from modeling.generalized_rcnn import ResNetC4Model, ResNetFPNModel
from train_job_setup import set_config_v1
from viz import (
    draw_annotation, draw_final_outputs, draw_predictions,
    draw_proposal_recall, draw_final_outputs_blackwhite)

from data_prepare.coco_format import COCOFormatDataLoader
from dataset import register_coco, register_balloon, register_coco_format

import sagemaker
import time
import boto3
from sagemaker.tensorflow.model import TensorFlowModel

def do_visualize(model, model_path, nr_visualize=100, output_dir='output'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
    """
    df = get_train_dataflow()
    df.reset_state()

    pred = OfflinePredictor(PredictConfig(
        model=model,
        session_init=SmartInit(model_path),
        input_names=['image', 'gt_boxes', 'gt_labels'],
        output_names=[
            'generate_{}_proposals/boxes'.format('fpn' if cfg.MODE_FPN else 'rpn'),
            'generate_{}_proposals/scores'.format('fpn' if cfg.MODE_FPN else 'rpn'),
            'fastrcnn_all_scores',
            'output/boxes',
            'output/scores',
            'output/labels',
        ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img, gt_boxes, gt_labels = dp['image'], dp['gt_boxes'], dp['gt_labels']

            rpn_boxes, rpn_scores, all_scores, \
            final_boxes, final_scores, final_labels = pred(img, gt_boxes, gt_labels)

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind], all_scores[good_proposals_ind])

            results = [DetectionResult(*args) for args in
                       zip(final_boxes, final_scores, final_labels,
                           [None] * len(final_labels))]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches([
                gt_viz, proposal_viz,
                score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()

def do_evaluate(pred_config, output_file):
    num_tower = max(cfg.TRAIN.NUM_GPUS, 1)
    graph_funcs = MultiTowerOfflinePredictor(
        pred_config, list(range(num_tower))).get_predictors()

    for dataset in cfg.DATA.VAL:
        logger.info("Evaluating {} ...".format(dataset))
        dataflows = [
            get_eval_dataflow(dataset, shard=k, num_shards=num_tower)
            for k in range(num_tower)]
        all_results = multithread_predict_dataflow(dataflows, graph_funcs)
        output = output_file + '-' + dataset
        DatasetRegistry.get(dataset).eval_inference_results(all_results, output)

# from pycocotools.coco import annToMask
def convert_box_mode_xywh_2_xyxy(box):
    return box[0], box[1], box[0] + box[2], box[1] + box[3]

def do_sanity_check(pred_func, output_dir='/root/dentalpoc/logs/xxxxx', font_rs=10, thickness_rs=10):
    # num_tower = max(cfg.TRAIN.NUM_GPUS, 1)
    # graph_funcs = MultiTowerOfflinePredictor(
    #     pred_config, list(range(num_tower))).get_predictors()
    os.makedirs(output_dir, exist_ok=True)

    for dataset in cfg.DATA.VAL:
        logger.info("sanity checking {} ...".format(dataset))
        # dataflows = [
        #     get_eval_dataflow(dataset, shard=k, num_shards=num_tower, add_gt=True)
        #     for k in range(num_tower)]
        # all_results = multithread_predict_dataflow(dataflows, graph_funcs)
        coco_format_detection = DatasetRegistry.get(dataset)
        coco_object = coco_format_detection.coco
        for _im_id, _img_dic in list(coco_object.imgs.items())[1:]:
            _img_path = _img_dic['path']
            _img_seg_polygons = coco_object.imgToAnns[_im_id]
            detection_ground_truths = list(
                map(lambda x: DetectionResult(box=convert_box_mode_xywh_2_xyxy(x['bbox']),
                                              score=1.0,
                                              class_id=x['category_id'],
                                              mask=coco_object.annToMask(x)),
                    _img_seg_polygons))

            print("S======check")
            _predict_with_gt(pred_func=pred_func,
                             input_file=_img_path,
                             ground_truths=detection_ground_truths,
                             output_dir=output_dir,
                             font_rs=font_rs,
                             thickness_rs=thickness_rs)

        xxx = 0
        # output = output_file + '-' + dataset
        # DatasetRegistry.get(dataset).eval_inference_results(all_results, output)

def _predict_with_gt(pred_func, input_file, ground_truths, output_dir=None, font_rs=10, thickness_rs=10):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)

    # resized_img, orig_shape, scale = run_resize_image(img)
    #  TODO: predict_image already contains resize
    results = predict_image(img, pred_func)
    results = list(filter(lambda x: x.score > 0.7, results))
    font_scale = np.sqrt(min(img.shape[:2])) / font_rs
    thickness = thickness_rs
    print('font_scale:', font_scale)

    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    if cfg.MODE_MASK:
        final = draw_final_outputs_blackwhite(img, results, font_scale=font_scale, thickness=thickness)
    else:
        final = draw_final_outputs(img, results, font_scale=font_scale, thickness=thickness)

    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    image_with_gt = draw_final_outputs(img, ground_truths, font_scale=font_scale, thickness=thickness)
    viz = np.concatenate((image_with_gt, final), axis=1)
    out_path = os.path.join(output_dir, re.sub('/', '-', input_file) + '.out.png')
    cv2.imwrite(out_path, viz)
    logger.info("Inference output for {} written to\n {}".format(input_file, out_path))
    # tpviz.interactive_imshow(viz)

def do_predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = predict_image(img, pred_func)
    if cfg.MODE_MASK:
        final = draw_final_outputs_blackwhite(img, results)
    else:
        final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    cv2.imwrite("output.png", viz)
    logger.info("Inference output for {} written to output.png".format(input_file))
    tpviz.interactive_imshow(viz)

# def set_config_A():
#     cfg.DATA.BASEDIR = os.path.abspath('../../../data/toooth')
#     cfg.MODE_FPN = True
#     cfg.DATA.VAL = ('coco_formated_eval',)
#     cfg.DATA.TRAIN = ('coco_formated_train',)
#     cfg.TRAIN.BASE_LR = 1e-3
#     cfg.TRAIN.EVAL_PERIOD = 1
#     cfg.TRAIN.LR_SCHEDULE = [1000]
#     cfg.PREPROC.TRAIN_SHORT_EDGE_SIZE = [600, 1200]
#     cfg.TRAIN.CHECKPOINT_PERIOD = 1
#     cfg.DATA.NUM_WORKERS = 1
#     cfg.TRAIN.CHECKPOINT_PERIOD = 1

def predict_from_endpoint(img):
    endpoint_name = 'tensorflow-inference-2020-01-28-22-47-19-125'

    SYDNEY = "ap-southeast-2"
    boto_session = boto3.Session(region_name=SYDNEY)
    sagemaker_session = sagemaker.Session(boto_session=boto_session)
    predictor = sagemaker.tensorflow.model.TensorFlowPredictor(endpoint_name, sagemaker_session)

    try:
        t0 = time.time()
        # print(t0)
        _outs = predictor.predict(img)
        preds = _outs['predictions']
        t1 = time.time()
        print('time cost:', t1 - t0)
    except:
        return np.array([]), np.array([]), np.array([]), np.array([])

    boxes = np.array([p['output/boxes:0'] for p in preds])
    probs = np.array([p['output/scores:0'] for p in preds])
    labels = np.array([p['output/labels:0'] for p in preds])
    masks = np.array([p['output/masks:0'] for p in preds])

    return boxes, probs, labels, masks

"""
./predict.py --predict input1.jpg input2.jpg --load /path/to/Trained-Model-Checkpoint --config SAME-AS-TRAINING
"""

if __name__ == '__main__':
    # set_config_A()
    data_config = DataConfig(image_data_basedir=None)
    data_config.pop_from_dict(data_conf_gingivitis_only) #data_conf_tooth_only
    register_coco_format(data_config=data_config)
    set_config_v1(data_config=data_config)

    cfg.freeze(False)
    cfg.PREPROC.TEST_SHORT_EDGE_SIZE = 400
    cfg.freeze(True)

    parser = argparse.ArgumentParser()
    parser.add_argument('--load', help='load a model for evaluation.', required=False)
    parser.add_argument('--visualize', action='store_true', help='visualize intermediate results')
    parser.add_argument('--evaluate', help="Run evaluation. "
                                           "This argument is the path to the output json evaluation file")
    parser.add_argument('--predict', help="Run prediction on a given image. "
                                          "This argument is the path to the input image file", nargs='+')

    parser.add_argument('--sanity_check', help="Run prediction on a given image vs ground truth", action='store_true')
    parser.add_argument('--endpoint_check', help="Run prediction on a given image vs ground truth", action='store_true')
    parser.add_argument('--benchmark', action='store_true', help="Benchmark the speed of the model + postprocessing")
    parser.add_argument('--config', help="A list of KEY=VALUE to overwrite those defined in config.py",
                        nargs='+')
    parser.add_argument('--output-pb', help='Save a model to .pb')
    parser.add_argument('--output-serving', help='Save a model to serving file')

    args = parser.parse_args()

    # TODO: cheat
    args.load = "/root/dentalpoc/logs/ging_tune_004/checkpoint"
    output_dir = '/root/dentalpoc/out_ging_tune_004_EPP400'
    # args.output_pb = 'decay_01.pb'
    # args.output_serving = '01211'

    if args.config:
        cfg.update_config_from_args(args.config)
    # register_coco(cfg.DATA.BASEDIR)  # add COCO datasets to the registry
    # register_balloon(cfg.DATA.BASEDIR)

    import os

    MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model()

    if not tf.test.is_gpu_available():
        from tensorflow.python.framework import test_util

        assert get_tf_version_tuple() >= (1, 7) and test_util.IsMklEnabled(), \
            "Inference requires either GPU support or MKL support!"
    assert args.load
    finalize_configs(is_training=False)

    if args.predict or args.visualize:
        cc = list(args.predict)
        cfg.TEST.RESULT_SCORE_THRESH = cfg.TEST.RESULT_SCORE_THRESH_VIS

    if args.visualize:
        do_visualize(MODEL, args.load)
    else:
        predcfg = PredictConfig(
            model=MODEL,
            session_init=SmartInit(args.load),
            input_names=MODEL.get_inference_tensor_names()[0],
            output_names=MODEL.get_inference_tensor_names()[1])

        if args.output_pb:
            output_pb_path = os.path.join(os.path.dirname(args.load), args.output_pb)
            ModelExporter(predcfg).export_compact(output_pb_path, optimize=False)
        elif args.output_serving:
            output_serving_path = os.path.join(os.path.dirname(args.load), 'export/Servo', args.output_serving)
            ModelExporter(predcfg).export_serving(output_serving_path, signature_name='serving_default') #, optimize=False
        if args.predict:
            predictor = OfflinePredictor(predcfg)
            for image_file in args.predict:
                do_predict(predictor, image_file)
        elif args.sanity_check:
            predictor = OfflinePredictor(predcfg)
            do_sanity_check(pred_func=predictor, output_dir=output_dir, font_rs=40, thickness_rs=1)
        elif args.endpoint_check:
            predictor = predict_from_endpoint
            do_sanity_check(pred_func=predictor, output_dir=output_dir, font_rs=40, thickness_rs=1)
        elif args.evaluate:
            assert args.evaluate.endswith('.json'), args.evaluate
            do_evaluate(predcfg, args.evaluate)
        elif args.benchmark:
            df = get_eval_dataflow(cfg.DATA.VAL[0])
            df.reset_state()
            predictor = OfflinePredictor(predcfg)
            for _, img in enumerate(tqdm.tqdm(df, total=len(df), smoothing=0.5)):
                # This includes post-processing time, which is done on CPU and not optimized
                # To exclude it, modify `predict_image`.
                predict_image(img[0], predictor)

max_stars_repo_name: ruodingt/tensorpack

max_issues_repo_name: ruodingt/tensorpack

max_forks_repo_name: ruodingt/tensorpack

reason: import numpy,import sage,from sage


text:
import numpy as np
from utils.data_loader import load
import utils.ensemble_dt as edt
import utils.ensemble_bag_mlp as ebm
import utils.svm_mlp_dt_combo as smdt
import utils.ensemble_rf as erf
from sklearn import preprocessing
from tabulate import tabulate

train_plus, train_negative, test_plus, test_negative = load("/home/toorajtaraz/Downloads/project/")
train_data_count = train_plus.shape[0]
test_data_count = test_plus.shape[0]

train_plus = train_plus.reshape((train_data_count, -1))
train_negative = train_negative.reshape((train_data_count, -1))
test_plus = test_plus.reshape((test_data_count, -1))
test_negative = test_negative.reshape((test_data_count, -1))

train_data = []
train_label = []

test_data = []
test_label = []

for x in train_plus:
    train_data.append(x)
    train_label.append(1)

for x in train_negative:
    train_data.append(x)
    train_label.append(0)

for x in test_plus:
    test_data.append(x)
    test_label.append(1)

for x in test_negative:
    test_data.append(x)
    test_label.append(0)

test_data = np.array(test_data)
train_data = np.array(train_data)
test_label = np.array(test_label)
train_label = np.array(train_label)

scaler = preprocessing.StandardScaler().fit(train_data)
train_data = scaler.transform(train_data)

scaler = preprocessing.StandardScaler().fit(test_data)
test_data = scaler.transform(test_data)

edt.init_vars(train_data, train_label, test_data, test_label)
adaboosted_dt_acc = edt.test_accuracy(edt.adaboosted_dt())

ebm.init_vars(train_data, train_label, test_data, test_label)
bagged_mlp_acc = ebm.test_accuracy(ebm.bagged_mlp())

smdt.init_vars(train_data, train_label, test_data, test_label)
svm_mlp_dt_combo_acc = smdt.test_accuracy(smdt.pipeline(debug=True))

erf.init_vars(train_data, train_label, test_data, test_label)
random_forrest_acc = erf.test_accuracy(erf.handle_random_forrest())

result = [
    ["Adaboosted Decision Tree", adaboosted_dt_acc[0], adaboosted_dt_acc[1]],
    ["Bagged MLP", bagged_mlp_acc[0], bagged_mlp_acc[1]],
    ["SVM + MLP + DT", svm_mlp_dt_combo_acc[0], svm_mlp_dt_combo_acc[1]],
    ["Random Forest", random_forrest_acc[0], random_forrest_acc[1]]
]
final_table = tabulate(result, headers=['TYPE', "TEST_P", "TRAIN_P"])
print(final_table)

ext: py

max_stars_repo_name: toorajtaraz/sameornotthesame

max_issues_repo_name: toorajtaraz/sameornotthesame

max_forks_repo_name: toorajtaraz/sameornotthesame

reason: import numpy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2018/12/13

import re
import os
import sys
import glob
import numpy as np
import shutil

def split_lc(rawfile_path,rawfile_name,opfolder):
    """Split the LC file and save into .txt, return the splitted filename list
    LC raw ファイルを開いて、splitして、別々のtxtファイルに保存
    """
    print("Splitting txt file...")

    splitted_files = []    # empty list to record the new file names
    for fpath,fname in zip(rawfile_path,rawfile_name):    # open the file, store in "content"
        try:
            with open(fpath, 'r',encoding='mac_roman') as f:
                state = 'stop'   # もう write/stop 使ってないので、state いらないかも
                i = 0
                content = f.readlines() # ここにいれなくても、for line in f でいいのでは
                for line in content:
                    if re.match(r'^\[(.+)',line):
                        state = 'write'
                        i+=1
                        newsplitfname = opfolder+"/files/{}/{}-{}-{}.txt".format(fname,fname,i,line[1:-2])
                        splitted_files.append(newsplitfname)
                        f2 = open(newsplitfname,'w')
                        f2.write(line)
                    elif state == 'write':
                        f2.write(line)
                    #else:
        except:
            with open(fpath, 'r',encoding='UTF-8') as f:
                state = 'stop'
                i = 0
                content = f.readlines()
                for line in content:
                    if re.match(r'^\[(.+)',line):
                        state = 'write'
                        i+=1
                        newsplitfname = opfolder+"/files/{}/{}-{}-{}.txt".format(fname,fname,i,line[1:-2])
                        splitted_files.append(newsplitfname)
                        f2 = open(newsplitfname,'w')
                        f2.write(line)
                    elif state == 'write':
                        f2.write(line)
                    #else:
        #f2.close()
        f.closed    # maybe I should write f.closed since I'm using the with statement?
    print("Done")
    return splitted_files # but I don't use this anywhere

def rewrite_MS_filename(rawfile_name):
    """Rename the MS file name from "MS Chromatogram.txt" into "(m/z).txt" and put it into mz folder
    """
    print("Rewriting mz filename...")

    # rename MS filename by its m/z.
    # open file, read the 2nd row (where m/z is written) and make that into the filename
    newfname = []
    replacename = ''
    for folder in rawfile_name:
        msfiles = [os.path.basename(path) for path in sorted(glob.glob(opfolder+"/files/"+folder+"/*-MS Chromatogram.txt"))]
        for fname in msfiles:    # MS chromatogram files のファイル名を mzに書き換える。そのファイル名リストをnewfnameに入れる
            filepath = opfolder+"/files/{}/{}".format(folder,fname)
            with open(filepath, 'r') as f:
                for i, line in enumerate(f):
                    if i == 2: break
                    if i == 1:    # read 2nd row, where the m/z are written
                        if isfloat(line[-8:-2].strip()):
                            replacename = '{}-{}'.format(folder,line[-8:-2].strip())
                            newfname.append("{}/{}.txt".format(folder,replacename))
                        elif re.search(r'1-1MS',line) :
                            replacename = '{}-{}-1'.format(folder,line[-5:].strip())
                            newfname.append("{}/{}.txt".format(folder,replacename))
                        elif re.search(r'1-2MS',line) :
                            replacename = '{}-{}-2'.format(folder,line[-5:].strip())
                            newfname.append("{}/{}.txt".format(folder,replacename))
            f.close()
            os.rename(filepath,opfolder+"/mz/{}/{}.txt".format(folder,replacename))
    print("Done")
    return newfname

def get_abs_rel_intensity(mz_files):
    """Absolute intensity & Relative intensity わける
    """
    print("Separating abs/rel itensity...")
    for fname in set(mz_files):
        data = np.loadtxt(opfolder+'/mz/'+fname,skiprows=7)
        data = data.astype(float)
        data_abs = np.delete(data,2,1)
        data_rel = np.delete(data,1,1)

        with open(opfolder+'/mz_abs/'+fname,'w') as f_abs:
            for lines in data_abs:
                lines = np.array_str(lines)
                f_abs.write(lines[1:-1]+'\n')
        f_abs.close()
        with open(opfolder+'/mz_rel/'+fname,'w') as f_rel:
            for lines in data_rel:
                lines = np.array_str(lines)
                f_rel.write(lines[1:-1]+'\n')
        f_rel.close()
    #    try:
    #    except IndexError:
    #        pass
    print("Done")

def cut_MS_data(mz_files,num):
    """ms data point が多すぎるので、与えた数字でカット。
    """
    print("Cutting mz data by {}...".format(num))

    for type in ('abs','rel'):
        for fname in mz_files:
            filepath = opfolder+"/mz_{}/{}".format(type,fname)
            data = np.loadtxt(filepath,skiprows=0)

            f2 = open(filepath,"w")
            count = 0
            for lines in data:
                if count % num == 0:
                    # print(lines)
                    f2.write(np.array_str(lines)[1:-1]+'\n')
                count+=1
            f2.close()
    print("Done")

def delete_mz_folder():
    """mzフォルダごと消す。
    """
    try:
        shutil.rmtree(opfolder+"/mz")
        #print("Deleted mz folder...")
    except OSError as e:
        print ("Error: {} - {}.".format(e.filename, e.strerror))

def create_folder(folders_to_create=[]):
    """Make new folders if they don't exist
    """
    for f in folders_to_create:
        if not os.path.exists(f):
            os.makedirs(f)

def obtain_filename(folder):
    """Obtain the filename in the given folder and return in list
    """
    fname_list = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
    if '.DS_Store' in fname_list: fname_list.remove('.DS_Store')
    fname_list.sort()
    return fname_list

def obtain_dirname(folder):
    """Obtain the folder name in the given folder and return in list
    """
    dirname_list = [f for f in os.listdir(folder) if os.path.isdir(os.path.join(folder, f))]
    fname_list.sort()
    return dirname_list

def isfloat(string):
    try:
        float(string)
        return True
    except:
        return False

### Ongoing
# Want to make "selected-mz folder". pick the selected mz files.

if __name__ == "__main__":

    # obtain filename in the ipfolder
    ipfolder = sys.argv[1]
    rawfile_path = sorted(glob.glob(ipfolder+"/*.txt"))
    rawfile_name = [os.path.basename(path)[:-4] for path in rawfile_path]

    # create empty folders to store files (mz, pda, etc...)
    opfolder = ipfolder + '-output'
    folders1 = [opfolder,opfolder+'/files',opfolder+'/mz']
    folders2 = [opfolder+"/files/"+fname for fname in rawfile_name]
    folders3 = [opfolder+"/mz/"+fname for fname in rawfile_name]
    folders4 = [opfolder+'/mz_abs',opfolder+'/mz_rel'] + [opfolder+'/mz_abs/'+ f for f in rawfile_name] + [opfolder+'/mz_rel/'+ f for f in rawfile_name]
    create_folder(folders1+folders2+folders3+folders4)

    # Split HPLC file and store in ./split folder
    split_lc(rawfile_path,rawfile_name,opfolder)

    # Organize MS Chromatogram file
    mz_files = rewrite_MS_filename(rawfile_name)    # rename the MS Chromatogram filename to m/z, store in ./mz/ folder and return the filename
    get_abs_rel_intensity(mz_files)    # get only abs intensity
    cut_MS_data(mz_files,3)    # cut down MS Chromatogram data
    delete_mz_folder()    # delete mz folder

    print("Finished")

max_stars_repo_name: maikof/Shimadzu-HPLC-raw-data-splitter

max_issues_repo_name: maikof/Shimadzu-HPLC-raw-data-splitter

max_forks_repo_name: maikof/Shimadzu-HPLC-raw-data-splitter


text:
#!/usr/bin/env python
"""
Functions for loading and processing dyPolyChord dynamic nested sampling runs.
"""
import os
import warnings
import numpy as np
import nestcheck.ns_run_utils
import nestcheck.data_processing
import nestcheck.io_utils as iou

def settings_root(likelihood_name, prior_name, ndim, **kwargs):
    """
    Returns a standard string containing information about settings.

    Parameters
    ----------
    likelihood_name: str
    prior_name: str
    ndim: int
    prior_scale: float or int
    dynamic_goal: float, int or None
    nlive_const: int
    nrepeats: int
    nint: int, optional
    init_step: int, optional

    Returns
    -------
    root: str
    """
    prior_scale = kwargs.pop('prior_scale')
    dynamic_goal = kwargs.pop('dynamic_goal')
    nlive_const = kwargs.pop('nlive_const')
    nrepeats = kwargs.pop('nrepeats')
    ninit = kwargs.pop('ninit', None)
    init_step = kwargs.pop('init_step', None)
    if kwargs:
        raise TypeError('Unexpected **kwargs: {0}'.format(kwargs))
    root = '{}_{}_{}_dg{}'.format(likelihood_name, prior_name, prior_scale,
                                  dynamic_goal)
    if dynamic_goal is not None:
        assert ninit is not None
        root += '_{}init'.format(ninit)
        if dynamic_goal != 0:
            assert init_step is not None
            root += '_{}is'.format(init_step)
    root += '_{}d_{}nlive_{}nrepeats'.format(ndim, nlive_const, nrepeats)
    return root.replace('.', '_')

def process_dypolychord_run(file_root, base_dir, **kwargs):
    """
    Load the output files of a dynamic run and process them to the nestcheck
    format.

    Parameters
    ----------
    file_root: str
    base_dir: str
    dynamic_goal: float
    dup_assert: bool, optional
        Whether to throw an AssertionError if there are duplicate point
        loglikelihood values.
    dup_warn: bool, optional
        Whether to give a UserWarning if there are duplicate point
        loglikelihood values.

    Returns
    -------
    run: dict
        Nested sampling run in nestcheck format (see
        http://nestcheck.readthedocs.io/en/latest/api.html for more
        information).
    """
    dynamic_goal = kwargs.pop('dynamic_goal')
    dup_assert = kwargs.pop('dup_assert', False)
    dup_warn = kwargs.pop('dup_warn', False)
    if kwargs:
        raise TypeError('Unexpected **kwargs: {0}'.format(kwargs))
    init = nestcheck.data_processing.process_polychord_run(
        file_root + '_init', base_dir, dup_assert=dup_assert,
        dup_warn=dup_warn)
    assert np.all(init['thread_min_max'][:, 0] == -np.inf), (
        'Initial run contains threads not starting at -inf.\n'
        'thread_min_max=' + str(init['thread_min_max']))
    dyn = nestcheck.data_processing.process_polychord_run(
        file_root + '_dyn', base_dir, dup_assert=dup_assert,
        dup_warn=dup_warn)
    dyn_info = iou.pickle_load(os.path.join(
        base_dir, file_root + '_dyn_info'))
    if dynamic_goal == 0:
        # If dynamic_goal == 0 then nlive should only decrease, so check all
        # threads start by sampling
        assert np.all(dyn['thread_min_max'][:, 0] == -np.inf), (
            str(dyn['thread_min_max']))
    # Get info to run
    run_output = {'file_root': file_root,
                  'base_dir': base_dir}
    if 'resume_ndead' not in dyn_info:
        # The dynamic run was not resumed part way through the initial run:
        # hence there are no samples repeated in both runs' files and we can
        # simply combine dyn and init using standard nestcheck functions.
        run = nestcheck.ns_run_utils.combine_ns_runs([init, dyn])
        try:
            run_output['nlike'] = (
                init['output']['nlike'] + dyn['output']['nlike'])
        except KeyError:
            pass # protect from error reading nlike from .stats file
    else:
        # The dynamic run started by resuming the initial run part way
        # through, and we need to remove duplicate points from the combined run
        run = combine_resumed_dyn_run(init, dyn, dyn_info['resume_ndead'])
        try:
            run_output['nlike'] = (
                init['output']['nlike'] + dyn['output']['nlike']
                - dyn_info['resume_nlike'])
        except KeyError:
            pass # protect from error reading nlike from .stats file
    run['output'] = run_output
    # check the nested sampling run has the expected properties
    nestcheck.ns_run_utils.check_ns_run(
        run, dup_assert=dup_assert, dup_warn=dup_warn)
    return run

def combine_resumed_dyn_run(init, dyn, resume_ndead):
    """
    Merge initial run and dynamic run which was resumed from it, including
    removing duplicate points present in both runs.

    Parameters
    ----------
    init: dict
        Initial exploratory run in nestcheck format (see
        http://nestcheck.readthedocs.io/en/latest/api.html for more
        information).
    dyn: dict
        Dynamic run in nestcheck format.
    resume_ndead: int
        The number of dead points present when dyn was resumed from init.

    Returns
    -------
    run: dict
        Combined run in nestcheck format.
    """
    assert np.array_equal(
        init['logl'][:resume_ndead], dyn['logl'][:resume_ndead]), (
            'The first {0} points should be the same'.format(resume_ndead))
    init['theta'] = init['theta'][resume_ndead:, :]
    for key in ['nlive_array', 'logl', 'thread_labels']:
        init[key] = init[key][resume_ndead:]
    # We also need to remove the points that were live when the resume file was
    # written, as these show up as samples in both dyn and init
    live_inds = []
    empty_thread_inds = []
    for i, th_lab in enumerate(np.unique(init['thread_labels'])):
        th_inds = np.where(init['thread_labels'] == th_lab)[0]
        live_logl = init['logl'][th_inds[0]]
        init['thread_min_max'][i, 0] = live_logl
        if np.where(dyn['logl'] == live_logl)[0].shape[0] > 0:
            live_inds.append(th_inds[0])
            if th_inds.shape[0] == 1:
                empty_thread_inds.append(i)
        else:
            warnings.warn(
                ('Expected live point at resume should be present in dynamic '
                 'run. If there are no further errors, this warning can be '
                 'ignored.\nlogl={}, th_lab={}, inds={}, init samples (after '
                 'removing first resume_ndead)={}, unique threads in init={}, '
                 'dyn samples={}, resume_ndead={}.').format(
                     str(live_logl), th_lab,
                     np.where(dyn['logl'] == live_logl),
                     init['logl'].shape[0],
                     np.unique(init['thread_labels']).shape[0],
                     dyn['logl'].shape[0], resume_ndead), UserWarning)
    # Remove the live points at resume from init
    init['theta'] = np.delete(init['theta'], live_inds, axis=0)
    for key in ['nlive_array', 'logl', 'thread_labels']:
        init[key] = np.delete(init[key], live_inds)
    # Deal with the case that one of the threads is now empty
    if empty_thread_inds:
        # remove any empty threads from logl_min_max
        init['thread_min_max'] = np.delete(
            init['thread_min_max'], empty_thread_inds, axis=0)
        # Now we need to reorder the thread labels to avoid gaps
        thread_labels_new = np.full(init['thread_labels'].shape, np.nan)
        for i, th_lab in enumerate(np.unique(init['thread_labels'])):
            inds = np.where(init['thread_labels'] == th_lab)[0]
            thread_labels_new[inds] = i
            # Check the newly relabelled thread label matches thread_min_max
            assert init['thread_min_max'][i, 0] <= init['logl'][inds[0]]
            assert init['thread_min_max'][i, 1] == init['logl'][inds[-1]]
        assert np.all(~np.isnan(thread_labels_new))
        init['thread_labels'] = thread_labels_new.astype(int)
    # Add the init threads to dyn with new labels that continue on from the dyn
    # labels
    init['thread_labels'] += dyn['thread_min_max'].shape[0]
    run = nestcheck.ns_run_utils.combine_threads(
        nestcheck.ns_run_utils.get_run_threads(dyn) +
        nestcheck.ns_run_utils.get_run_threads(init),
        assert_birth_point=False)
    return run

def clean_extra_output(root_name):
    """Clean the additional output files made by dyPolyChord, leaving only
    output files for the combined run in PolyChord format.

    Parameters
    ----------
    root_name: str
        File root. Equivalent to os.path.join(base_dir, file_root).
    """
    os.remove(root_name + '_dyn_info.pkl')
    for extra in ['init', 'dyn']:
        os.remove(root_name + '_{0}.stats'.format(extra))
        os.remove(root_name + '_{0}_dead-birth.txt'.format(extra))
        os.remove(root_name + '_{0}_dead.txt'.format(extra))
        # tidy up remaining .resume files (if the function has reach this
        # point, both the initial and dynamic runs have finished so we
        # shouldn't need to resume)
        try:
            os.remove(root_name + '_{0}.resume'.format(extra))
        except OSError:
            pass

max_stars_repo_name: ejhigson/dyPolyChord

max_issues_repo_name: ejhigson/dyPolyChord

max_forks_repo_name: ejhigson/dyPolyChord


text:
import torch
import numpy as np
import warnings
import functools

HANDLEdFUNCTIONS = {}

def implements(torch_function):
    """Register a torch function override for ScalarTensor"""

    @functools.wraps(torch_function)
    def decorator(func):
        HANDLEdFUNCTIONS[torch_function] = func
        return func

    return decorator

# ----------------------------------- getters ---------------------------------------------

@implements(torch.Tensor.shape.__get__)
def shape(self):
    """
    Gets standard shape
    """
    return torch.Tensor.shape.__get__(self.q)

@implements(torch.Tensor.get_device)
def get_device(self):
    return self.torch().get_device()
@implements(torch.Tensor.is_complex)
def is_complex(self):
    return self.torch().is_complex()

@implements(torch.Tensor.size)
def size(self, *args, **kwargs):
    """
    Gets standard size
    """
    return torch.Tensor.size(self.q, *args, **kwargs)

@implements(torch.Tensor.T.__get__)
def T(self):
    return self.q.t()

@implements(torch.Tensor.requires_grad.__get__)
def get_requires_grad(self):
    return self.q.requires_grad

@implements(torch.Tensor.grad.__get__)
def get_grad(self):
    return self.q.grad

@implements(torch.Tensor.__getitem__)
def get_item(self, idx):
    return torch.Tensor.__getitem__(self.q, idx)

# ----------------------------------- setters -------------------------------------------------

@implements(torch.Tensor.requires_grad.__set__)
def set_requires_grad(self, requires_grad):
    self.q.requires_grad = requires_grad

@implements(torch.Tensor.grad.__set__)
def set_grad(self, grad):
    self.q.grad = grad

# ----------------------------------- general ------------------------------------------------

@implements(torch.Tensor.dim)
def dim(self):
    return self.q.dim()

@implements(torch.Tensor.t)
def t(self):
    return self.q.t()

@implements(torch.chunk)
def chunk(input, *args, **kwargs):
    return torch.chunk(input.q, *args, **kwargs)

@implements(torch.Tensor.chunk)
def chunk(input, *args, **kwargs):
    return torch.chunk(input.q, chunks=4, *args, **kwargs)

@implements(torch.Tensor.cpu)
def cpu(self):
    return self.q.cpu()

@implements(torch.Tensor.cuda)
def cuda(self):
    return self.q.cuda()

@implements(torch.Tensor.reshape)
def reshape(self, *args, **kwargs):
    return self.q.reshape(*args, **kwargs)

@implements(torch.Tensor.view)
def view(self, *args, **kwargs):
    return self.q.view(*args, **kwargs)

@implements(torch.allclose)
def allclose(input1, input2, *args, **kwargs):
    if all(isinstance(i, QuaternionTensor) for i in [input1, input2]):
        return torch.allclose(input1.q, input2.q)
    else:
        return torch.allclose(input1.q, input2)

@implements(torch.cat)
def cat(input, *args, **kwargs):
    check_list = [isinstance(i, QuaternionTensor) for i in input]
    if all(check_list):
        return torch.cat([x.q for x in input], *args, **kwargs)
    else:
        return torch.cat([input[i].q if check_list[i] else input[i] for i in range(len(check_list))], *args, **kwargs)

@implements(torch.stack)
def stack(input, *args, **kwargs):
    check_list = [isinstance(i, QuaternionTensor) for i in input]
    if all(check_list):
        return torch.stack([x.q for x in input], *args, **kwargs)
    else:
        return torch.stack([input[i].q if check_list[i] else input[i] for i in range(len(check_list))], *args, **kwargs)

@implements(torch.Tensor.neg)
def neg(input):
    return input.__class__(-input.q)

@implements(torch.nn.functional._pad)
def pad(input, *args, **kwargs):
    return torch.nn.functional._pad(input.q)

@implements(torch.Tensor.squeeze)
def squeeze(self, *args, **kwargs):
    return self.q.squeeze(*args, **kwargs)

@implements(torch.Tensor.unsqueeze)
def unsqueeze(self, *args, **kwargs):
    return self.q.unsqueeze(*args, **kwargs)

# -----------------------------activation functions ------------------------------------------

@implements(torch.nn.functional.relu)
def relu(input, *args, **kwargs):
    return torch.nn.functional.relu(input.q, *args, **kwargs)

@implements(torch.nn.functional.leaky_relu)
def leaky_relu(input, *args, **kwargs):
    return torch.nn.functional.leaky_relu(input.q, *args, **kwargs)

# ----------------------------------- conj ---------------------------------------------------

@implements(torch.Tensor.conj)
def conj(self):
    """
    Quaternion conjugate
    """
    if len(self.shape) > 1:
        con = torch.cat([self.a, -self.b, -self.c, -self.d], 1)
    else:
        con = [self.a, -self.b, -self.c, -self.d]

    return self.__class__(con, False)

@implements(torch.conj)
def conj(input):
    return input.conj()

# ----------------------------------- inverse ------------------------------------------------

@implements(torch.Tensor.inverse)
def inverse(self):
    """
    Quaternion inverse
    """
    if len(self.shape) > 1:
        inverse = self.conj() / self.sq_norm()
    else:
        inverse = self.conj() / self.sq_norm()
    return inverse

@implements(torch.inverse)
def inverse(input):
    return input.inverse()

# ----------------------------------- min ------------------------------------------------

@implements(torch.Tensor.min)
def min(self):
    return torch.min(self.q)

@implements(torch.min)
def min(input):
    return input.min()

# ----------------------------------- max ------------------------------------------------

@implements(torch.Tensor.max)
def max(self):
    return torch.max(self.q)

@implements(torch.max)
def max(input):
    return input.max()

# ----------------------------------- sum ------------------------------------------------

@implements(torch.Tensor.sum)
def sum(self, *args, **kwargs):
    return torch.sum(self.q, *args, **kwargs)

@implements(torch.sum)
def sum(input, *args, **kwargs):
    return input.sum(*args, **kwargs)

# ----------------------------------- mean ------------------------------------------------

@implements(torch.Tensor.mean)
def mean(self, *args, **kwargs):
    return torch.mean(self.q, *args, **kwargs)

@implements(torch.mean)
def mean(input, *args, **kwargs):
    return input.mean(*args, **kwargs)

# ----------------------------------- norm ------------------------------------------------

@implements(torch.Tensor.norm)
def norm(self, *args, **kwargs):
    """
    Quaternion (non-squared) norm.
    """
    return torch.sqrt(self.a ** 2 + self.b ** 2 + self.c ** 2 + self.d ** 2)

@implements(torch.linalg.norm)
def norm(input, *args, **kwargs):
    return input.norm(*args, **kwargs)

# ----------------------------------- add ------------------------------------------------

@implements(torch.Tensor.add)
def add(self, other):
    """
    Standard addition but only adds the other tensor
    to the real part if it has 1/4 of the channels.
    """
    if np.prod(self.shape) != 4:
        if isinstance(other, torch.Tensor):
            if other.__class__.__name__ == "QuaternionTensor":
                other = other.q
            if other.dim() > 1 and self.dim() > 1:
                if other.shape[1] * 4 == self.shape[1]:
                    out = torch.cat([self.a + other, self.b, self.c, self.d], 1)
                    if self.real_tensor:
                        out = real_repr(out)
                else:
                    out = self.q + other
            elif other.dim() <= 1 and self.dim() > 1:
                out = torch.cat([self.a + other.unsqueeze(1), self.b, self.c, self.d], 1)
                if self.real_tensor:
                    out = real_repr(out)
        else:
            out = self.q + other

    else:
        print(self.shape)
        if other.__class__.__name__ == "QuaternionTensor":
            out = self.q + other.q
        elif isinstance(other, int):
            out = [self.a + other, self.b, self.c, self.d]
        elif len(other) == 1:
            out = [self.a + other, self.b, self.c, self.d]
        else:
            out = self.q + other

    return self.__class__(out)

@implements(torch.add)
def add(input1, input2):
    return input1.add(input2)

# ----------------------------------- mul ------------------------------------------------

@implements(torch.Tensor.mul)
def mul(self, other):
    """
    Product of two quaternions, called "Hamilton product".
    Using the basis product's rules and the distributive rule
    for two quaternions q1 = a1 + b1*i + c1*j + d1*k and
    q2 = a2 + b2*i + c2*j + d2*k we get:

    anew = a1 * a2 - b1 * b2 - c1 * c2 - d1 * d2
    bnew = (a1 * b2 + b1 * a2 + c1 * d2 - d1 * c2) i
    cnew = (a1 * c2 - b1 * d2 + c1 * a2 + d1 * b2) j
    dnew = (a1 * d2 + b1 * c2 - c1 * b2 + d1 * a2) k

    It broadcastes the other C/4 tensor to C channels
    to compute the standard Kronecker product.
    """

    if isinstance(other, QuaternionTensor):
        a2, b2, c2, d2 = other.chunk()
        r = self.a * a2 - self.b * b2 - self.c * c2 - self.d * d2
        i = self.a * b2 + self.b * a2 + self.c * d2 - self.d * c2
        j = self.a * c2 - self.b * d2 + self.c * a2 + self.d * b2
        k = self.a * d2 + self.b * c2 - self.c * b2 + self.d * a2

        if self.dim() > 1 or other.dim() > 1:
            out = torch.cat([r, i, j, k], 1)
        else:
            out = [r, i, j, k]

    elif isinstance(other, torch.Tensor):
        if other.dim() > 1 and other.dim() > 1:
            if other.shape[1] * 4 == self.shape[1]:
                out = self.q * torch.cat([other] * 4, 1)
            else:
                out = self.q * other
        else:
            if other.dim() == 1 and other.shape[0] == self.shape[0]:
                out = self.q * torch.stack([other] * self.shape[1], 1)
            else:
                out = self.q * other
    else:
        out = self.q * other

    return self.__class__(out)

@implements(torch.mul)
def mul(input1, input2):
    return input1.mul(input2)

# ----------------------------------- matmul ---------------------------------------------

@implements(torch.Tensor.matmul)
def matmul(self, other):
    return torch.matmul(self.q, other)

@implements(torch.matmul)
def matmul(input1, input2):
    return input1.matmul(input2)

# ----------------------------------- div ------------------------------------------------

@implements(torch.Tensor.div)
def true_div(self, other):
    """
    Quaternion division q1 * (q2)^-1.
    It broadcastes the other C/4 tensor to C channels
    to compute the standard elementwise division.
    """
    real_tensor = False

    if isinstance(other, QuaternionTensor):
        out = self * other.inverse()

    elif isinstance(other, torch.Tensor):
        if other.dim() > 1 and self.dim() > 1:
            if other.shape[1] * 4 == self.shape[1]:
                out = self.q / torch.cat([other] * 4, 1)
            else:
                out = self.q / other
        else:
            if other.dim() == 1 and other.shape[0] == self.shape[0]:
                out = self.q / torch.stack([other] * self.shape[1], 1)
            else:
                out = self.q / other
    else:
        out = self.q / other

    return self.__class__(out)

@implements(torch.div)
def div(input1, input2):
    return input1.div(input2)

# ----------------------------------- pow ------------------------------------------------

@implements(torch.Tensor.pow)
def pow(self, n):
    """
    Quaternion power.
    """
    n = float(n)
    if self.quat_ops:
        v = self.v
        if self.dim() > 1:
            out = v / v.norm() * torch.sin(n * self.theta())
            out += torch.cos(n * self.theta())
            out *= (self.norm() ** n)

        else:
            out = v / v.norm() * torch.sin(n * self.theta())
            out += torch.cos(n * self.theta())
            out *= (self.norm() ** n)

        return self.__class__(out)
    else:
        return torch.pow(self.q, n)

@implements(torch.pow)
def div(input1, input2):
    return input1.pow(input2)

# ----------------------------------- exp ------------------------------------------------

@implements(torch.Tensor.exp)
def exp(self):
    """
    Quaternion exponential.
    """
    if self.quat_ops:
        v = self.v
        a = self.a
        v_norm = v.norm()
        exp = torch.exp(a)
        real = exp * torch.cos(v_norm)
        if self.dim() > 1:
            vector = exp * (v / v_norm) * torch.sin(v_norm)
            out = real + vector
        else:
            vector = exp * (v / v_norm) * torch.sin(v_norm)
            out = torch.cat([
                real, vector[1].unsqueeze(0), vector[2].unsqueeze(0), vector[3].unsqueeze(0)
            ], 0)

        return self.__class__(out)

    else:
        return torch.exp(self.q)

@implements(torch.exp)
def exp(input):
    return input.exp()

# ----------------------------------- log ------------------------------------------------

@implements(torch.Tensor.log)
def log(self):
    """
    Quaternion logarithm.
    """
    if self.quat_ops:
        v = self.v
        a = self.a
        v_norm = v.norm()
        q_norm = self.q.norm()

        real = torch.log(q_norm)

        if self.dim() > 1:
            vector = (v / v_norm) * self.theta()
            out = real + vector
        else:
            vector = (v / v_norm) * self.theta()
            out = [real.unsqueeze(0), vector[1].unsqueeze(0),
                   vector[2].unsqueeze(0), vector[3].unsqueeze(0)]

        return self.__class__(out)
    else:
        return torch.log(self.q)

@implements(torch.log)
def log(input):
    return input.log()

# ----------------------------------- layers ------------------------------------------------

@implements(torch.nn.functional.interpolate)
def interpolate(input, *args, **kwargs):
    return torch.nn.functional.interpolate(input.q, *args, **kwargs)

@implements(torch.nn.functional.max_pool2d)
def max_pool2d(input, *args, **kwargs):
    return torch.nn.functional.max_pool2d(input.q, *args, **kwargs)

@implements(torch.conv1d)
def conv1d(input, *args, **kwargs):
    return torch.conv1d(input.q, *args, **kwargs)

@implements(torch.conv2d)
def conv2d(input, *args, **kwargs):
    return torch.conv2d(input.q, *args, **kwargs)

@implements(torch.conv3d)
def conv3d(input, *args, **kwargs):
    return torch.conv3d(input.q, *args, **kwargs)

@implements(torch.conv_transpose1d)
def conv_transpose1d(input, *args, **kwargs):
    return torch.conv_transpose1d(input.q, *args, **kwargs)

@implements(torch.conv_transpose2d)
def conv_transpose2d(input, *args, **kwargs):
    return torch.conv_transpose2d(input.q, *args, **kwargs)

@implements(torch.conv_transpose3d)
def conv_transpose3d(input, *args, **kwargs):
    return torch.conv_transpose3d(input.q, *args, **kwargs)

@implements(torch.nn.functional.batch_norm)
def bn(input, *args, **kwargs):
    return torch.nn.functional.batch_norm(input.q, *args, **kwargs)

@implements(torch.nn.functional.dropout)
def dropout(input, *args, **kwargs):
    return torch.nn.functional.dropout(input.q, *args, **kwargs)

@implements(torch.nn.Dropout)
def dropout(input, *args, **kwargs):
    return torch.nn.functional.dropout(input.q, *args, **kwargs)

@implements(torch.nn.functional.layer_norm)
def layer_norm(input, *args, **kwargs):
    return torch.nn.functional.layer_norm(input.q, *args, **kwargs)

# ----------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------
# ---------------------------- QuaternionTensor ------------------------------------------
# ----------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

def get_parts(q):
    """
    Divides input tensor in real and imaginary parts.

    @type q: torch.Tensor
    @type dim: int
    """

    a, b, c, d = torch.chunk(q, 4, 1)

    return a.transpose(1, 0), \
           b.transpose(1, 0), \
           c.transpose(1, 0), \
           d.transpose(1, 0)

def check_q_type(q):
    """
    Readies the tensor for the QuaternionTensor class

    @type q: torch.Tensor/list/tuple
    """
    if isinstance(q, (tuple, list)):

        if all(isinstance(i, torch.Tensor) for i in q) == True and len(q) != 0:
            assert len(q) == 4, "Quaternion must have 4 elements."
            if all(i.dim() == 1 for i in q):
                q = torch.cat(q, 0)
            else:
                q = torch.stack(q, 1)
        else:
            q = torch.Tensor(q)
    return q

def real_repr(q):
    """
    Gets the real representation of the tensor.

    @type q: torch.Tensor
    """
    a, b, c, d = get_parts(q)

    if all((i.dim() == 1) for i in [a, b, c, d]) == True:
        a = a.view(1, 1)
        b = b.view(1, 1)
        c = c.view(1, 1)
        d = d.view(1, 1)

    return torch.cat([torch.cat([a, -b, -c, -d], dim=1),
                      torch.cat([b, a, -d, c], dim=1),
                      torch.cat([c, d, a, -b], dim=1),
                      torch.cat([d, -c, b, a], dim=1)], dim=0)

def real_rot_repr(q):
    """
    Gets the real rotation representation of the tensor.

    @type q: torch.Tensor
    """
    a, b, c, d = get_parts(q)

    if all((i.dim() == 1) for i in [a, b, c, d]) == True:
        a = a.view(1, 1)
        b = b.view(1, 1)
        c = c.view(1, 1)
        d = d.view(1, 1)

    row1 = torch.cat([torch.zeros_like(b)] * 4, dim=1)
    row2 = torch.cat([torch.zeros_like(b),
                      1 - 2 * (c ** 2 + d ** 2),
                      2 * (b * c - d * a),
                      2 * (b * d + c * a)], dim=1)
    row3 = torch.cat([torch.zeros_like(b),
                      2 * (b * c + d * a),
                      1 - 2 * (b ** 2 + d ** 2),
                      2 * (c * d - b * a)], dim=1)
    row4 = torch.cat([torch.zeros_like(b),
                      2 * (b * d - c * a),
                      2 * (c * d + b * a),
                      1 - 2 * (b ** 2 + c ** 2)], dim=1)

    return torch.cat([row1, row2, row3, row4], dim=0)

class QuaternionTensor(torch.Tensor):
    """
    The class contains the common quaternion operations suited
    for the Pytorch framework subclassing its
    "torch.Tensor". Thus it can be fed in any
    Pytorch function that accepts a torch.Tensor or can be
    used independently for other, more general, applications.
    """

    @staticmethod
    def __new__(cls, q=[], real_tensor=False, quat_ops=True, *args, **kwargs):

        q = check_q_type(q)
        if real_tensor:
            q = real_repr(q)
        cls.q = q
        cls.device = q.device
        return super().__new__(cls, q.cpu(), *args, **kwargs)

    def __init__(self, q=[], real_tensor=False, quat_ops=True):
        super().__init__()
        """
        Init accepts incoming quaternion and immediately
        transforms it into its real represention when
        "real_tensor" is set to True.

        @type q: torch.Tensor/list/tuple
        @type real_tensor: bool
        """

        self.real_tensor = real_tensor
        self.quat_ops = quat_ops
        q = check_q_type(q)

        if len(q) != 0:
            if real_tensor:
                q = real_repr(q)
            self.q = q
            self.q.grad = None

    @property
    def a(self):
        if self.dim() == 1:
            out = self.q[:self.shape[0] // 4]
        else:
            out = self.q[:, :self.shape[1] // 4]
        out.quat_ops = False
        return out

    @property
    def b(self):
        if self.dim() == 1:
            step = self.shape[0] // 4
            out = self.q[step:step * 2]
        else:
            step = self.shape[1] // 4
            out = self.q[:, step:step * 2]

        out.quat_ops = False
        return out

    @property
    def c(self):
        if self.dim() == 1:
            step = self.shape[0] // 4
            out = self.q[step * 2:step * 3]
        else:
            step = self.shape[1] // 4
            out = self.q[:, step * 2:step * 3]

        out.quat_ops = False
        return out

    @property
    def d(self):
        if self.dim() == 1:
            step = self.shape[0] // 4
            out = self.q[step * 3:]
        else:
            step = self.shape[1] // 4
            out = self.q[:, step * 3:]

        out.quat_ops = False
        return out

    # overrides pytorch operations
    def __torch_function__(self, func, types, args=(), kwargs=None):
        if kwargs is None:
            kwargs = {}
        if func not in HANDLEdFUNCTIONS or not all(
                issubclass(t, QuaternionTensor)
                for t in types
        ):
            return NotImplemented
        return HANDLEdFUNCTIONS[func](*args, **kwargs)

    def torch(self):
        """
        Casts to standard pytorch
        """
        return self.q

    def rand(self, size):
        """
        Creates random QuaternionTensor
        in the interval [0,1)
        """
        return self.__class__(torch.rand(size))

    def sq_norm(self):
        """
        Quaternion squared norm.
        """
        return self.q.norm() ** 2

    @property
    def _real_repr(self):
        """
        Real representation of the quaternion.
        """
        return real_repr(self.q)

    @property
    def _real_rot_repr(self):
        """
        Real rotation representation.
        """
        return real_rot_repr(self.q)

    @property
    def v(self):
        """
        Vector part of the quaternion: 0 + b*i + c*j + d*k.
        """
        if self.dim() > 1:
            out = torch.cat([torch.zeros_like(self.b), self.b, self.c, self.d], 1)
        else:
            out = [torch.zeros_like(self.b), self.b, self.c, self.d]

        return self.__class__(out)

    def theta(self):
        """
        Angle of the quaternion.
        """
        return torch.acos(self.a / self.norm())

    @property
    def qshape(self):
        """
        Quaternion shape
        """
        if self.real_tensor:
            return self.a.shape
        else:
            return self.shape

    def clone(self):
        """
        General Pytorch cloning.
        """
        return self.__class__(self.q.clone())

    def to(self, device):
        """
        Sends tensor to CPU or GPU.
        """
        new_obj = QuaternionTensor(self.q)
        tempTensor = super().to(device)
        new_obj.data = tempTensor.data
        new_obj.device = device
        new_obj.requires_grad = tempTensor.requires_grad

        return new_obj

    def chunk(self):
        return self.a, self.b, self.c, self.d

    def __add__(self, other):
        return torch.add(self, other)

    def __radd__(self, other):
        return self.__add__(other)

    def __iadd_(self, other):
        return self.__class__(self + other)

    def __sub__(self, other):
        return torch.add(self, -other)

    def __rsub__(self, other):
        return -self.__add__(-other)

    def __isub__(self, other):
        return self.__class__(self - other)

    def __mul__(self, other):
        return torch.mul(self, other)

    def __rmul__(self, other):

        if isinstance(other, torch.Tensor):
            if other.dim() > 1 and self.dim() > 1:
                if other.shape[1] * 4 == self.shape[1]:
                    out = torch.cat([other] * 4, 1) * self.q
                else:
                    out = other * self.q
            else:
                if other.dim() == 1 and other.shape[0] == self.shape[0]:
                    out = other * self.q
                else:
                    out = other * self.q
        else:
            out = other * self.q

        return self.__class__(out)

    def __imul__(self, other):
        return self.__class__(self * other)

    def __matmul__(self, other):
        return torch.matmul(self, other)

    def __rmatmul__(self, other):
        return self.__matmul__(other)

    def __truediv__(self, other):
        return torch.div(self, other)

    def __rtruediv__(self, other):

        if isinstance(other, torch.Tensor):
            if other.dim() > 1 and self.dim() > 1:
                if other.shape[1] * 4 == self.shape[1]:
                    out = torch.cat([other] * 4, 1) / self.q
                else:
                    out = other / self.q
            else:
                if other.dim() == 1 and other.shape[0] == self.shape[0]:
                    out = torch.stack([other] * self.shape[1], 1) / self.q
                else:
                    out = other / self.q
        else:
            out = other / self.q

        return self.__class__(out)

    def __itruediv__(self, other):
        return self.__class__(self / other)

    def __pow__(self, n):
        return torch.pow(self, n)

    def __len__(self):
        return len(self.q)

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return f"real part: {self.a}\n" + \
               f"imaginary part (i): {self.b}\n" + \
               f"imaginary part (j): {self.c}\n" + \
               f"imaginary part (k): {self.d}"


text:
import pytest
import numpy as np

from whatlies import Embedding, EmbeddingSet

@pytest.fixture
def emb():
    x = Embedding("x", [0.0, 1.0])
    y = Embedding("y", [1.0, 0.0])
    z = Embedding("z", [0.5, 0.5])
    return EmbeddingSet(x, y, z)

def test_emb_dist(emb):
    assert np.isclose(emb["x"].distance(emb["x"]), 0.0)
    assert np.isclose(emb["x"].distance(emb["y"], metric="euclidean"), np.sqrt(2))
    assert np.isclose(emb["x"].distance(emb["z"], metric="euclidean"), np.sqrt(2) / 2)

def test_emb_norm(emb):
    assert np.isclose(emb["x"].norm, 1.0)
    assert np.isclose(emb["y"].norm, 1.0)
    assert np.isclose(emb["z"].norm, np.sqrt(0.5))

def test_emb_add(emb):
    new_emb = emb["x"] + emb["y"]
    assert np.isclose(new_emb.vector, np.array([1.0, 1.0])).all()

def test_emb_subtract(emb):
    new_emb = emb["x"] - emb["y"]
    assert np.isclose(new_emb.vector, np.array([-1.0, 1.0])).all()

def test_emb_proj_unto1(emb):
    new_emb = emb["z"] >> emb["y"]
    assert np.isclose(new_emb.vector, np.array([0.5, 0.0])).all()

def test_emb_proj_unto2(emb):
    new_emb = emb["z"] >> emb["x"]
    assert np.isclose(new_emb.vector, np.array([0.0, 0.5])).all()

def test_emb_proj_away1(emb):
    new_emb = emb["z"] | emb["y"]
    assert np.isclose(new_emb.vector, np.array([0.0, 0.5])).all()

def test_emb_proj_away2(emb):
    new_emb = emb["z"] | emb["x"]
    assert np.isclose(new_emb.vector, np.array([0.5, 0.0])).all()

def test_emb_gt(emb):
    assert (emb["z"] > emb["x"]) == pytest.approx(0.5)
    assert (emb["x"] > emb["z"]) == pytest.approx(1.0)

def test_emb_plot_no_err_2d(emb):
    emb["x"].plot(kind="arrow").plot(kind="text")
    emb["y"].plot(kind="arrow").plot(kind="text")
    emb["z"].plot(kind="arrow").plot(kind="text")

def test_emb_plot_no_err_3d():
    x = Embedding("x", [0.0, 1.0, 1.0])
    y = Embedding("y", [1.0, 0.0, 1.0])
    z = Embedding("z", [0.5, 0.5, 1.0])
    for item in [x, y, z]:
        item.plot("scatter", x_axis=x, y_axis=y)

def test_emb_str_method(emb):
    for char in "xyz":
        assert str(emb[char]) == char

def test_emb_ndim():
    foo = Embedding("foo", [0, 1, 0.2])
    assert foo.ndim == 3

def test_negation():
    foo = Embedding("foo", [0.1, 0.3])
    assert np.allclose((-foo).vector, -np.array([0.1, 0.3]))

max_stars_repo_name: koaning/whatlies

max_issues_repo_name: koaning/whatlies

max_forks_repo_name: koaning/whatlies


text:
import datetime
import tempfile
from os import path

import numpy as np

from pynwb import NWBHDF5IO
from pynwb.file import NWBFile, Subject
from pynwb.testing import TestCase, remove_test_file
from hdmf.common import DynamicTableRegion

from ndx_nirs import (
    NIRSDevice,
    NIRSSeries,
    NIRSSourcesTable,
    NIRSDetectorsTable,
    NIRSChannelsTable,
)

def setup_nwbfile():
    nwbfile = NWBFile(
        session_description="A test NWB NIRS file",
        identifier="nirs_test",
        session_start_time=datetime.datetime(
            2021, 4, 1, 15, 0, 0, tzinfo=datetime.timezone.utc
        ),
        subject=Subject(subject_id="X"),
    )

    nirs_device = create_fake_nirs_device()
    nwbfile.add_device(nirs_device)
    nwbfile.add_acquisition(create_fake_nirs_series(nirs_device))
    return nwbfile

def create_fake_sources_table():
    table = NIRSSourcesTable()
    table.add_row({"label": "S1", "x": 0.0, "y": -1.0})
    table.add_row({"label": "S2", "x": 0.0, "y": 1.0})
    return table

def create_fake_detectors_table():
    table = NIRSDetectorsTable()
    table.add_row({"label": "D1", "x": 0.0, "y": -2.0})
    table.add_row({"label": "D2", "x": 0.0, "y": 0.0})
    table.add_row({"label": "D3", "x": 0.0, "y": 2.0})
    return table

def create_fake_channels_table():
    sources = create_fake_sources_table()
    detectors = create_fake_detectors_table()
    source_detector_pairs = [(0, 0), (0, 1), (1, 1), (1, 2)]
    table = NIRSChannelsTable(sources, detectors)
    ch_id = 0
    for source_idx, detector_idx in source_detector_pairs:
        for wavelength in [690.0, 830.0]:
            table.add_row(
                label=f"CH{ch_id}",
                source=source_idx,
                detector=detector_idx,
                source_wavelength=wavelength,
            )
            ch_id += 1
    return table

def create_fake_nirs_device():
    channels = create_fake_channels_table()

    return NIRSDevice(
        name="device",
        description="An fNIRS device",
        manufacturer="XYZ",
        nirs_mode="time-domain",
        channels=channels,
        sources=channels.source.table,
        detectors=channels.detector.table,
        time_delay=4.2,
        time_delay_width=0.5,
        additional_parameters="flux_capacitor_gain = 9000; speaker_volume = 11;",
    )

def create_fake_nirs_series(device):
    channels = device.channels
    fake_timeseries = np.arange(0, 100, 0.05)  # in seconds
    fake_data = np.random.rand(len(fake_timeseries), len(channels))

    return NIRSSeries(
        name="nirs_data",
        description="The raw NIRS channel data",
        timestamps=fake_timeseries,
        channels=DynamicTableRegion(
            name="channels",
            description="an ordered map to the channels in this NIRS series",
            table=channels,
            data=channels.id[:],
        ),
        data=fake_data,
        unit="V",
    )

class NIRSIntegrationTests(TestCase):
    """Integration tests for the ndx-nirs extension classes"""

    def setUp(self):
        self.nwb = setup_nwbfile()
        self.path = path.join(tempfile.gettempdir(), "test.nwb")

    def tearDown(self):
        remove_test_file(self.path)

    def test_roundtrip(self):
        """Verify that an NWBFile with NIRS data saved to disk and read again is
        identical to the original NWBFile
        """

        with NWBHDF5IO(self.path, "w") as io:
            io.write(self.nwb)

        with NWBHDF5IO(self.path, "r") as io:
            read_nwb = io.read()
            self.assertContainerEqual(self.nwb, read_nwb)
            device = read_nwb.devices["device"]
            self.assertIs(device.channels.source.table, device.sources)
            self.assertIs(device.channels.detector.table, device.detectors)

max_stars_repo_name: agencyenterprise/ndx-nirs

max_issues_repo_name: agencyenterprise/ndx-nirs

max_forks_repo_name: agencyenterprise/ndx-nirs


text:
#Matplotlib Plotting
#Autor: Javier Arturo Hernández Sosa
#Fecha: 30/Sep/2017
#Descripcion: Ejecicios Matplotlib Plotting - Alexandre Devert - (17)
#Gráfico de Barras Apilado
import numpy as np
import matplotlib.pyplot as plt

data = np.array([[5., 30., 45., 22.],
                 [5., 25., 50., 20.],
                 [1., 2., 1., 1.]])

color_list=['b', 'g', 'r']
#La función .shape() regresa las filas y las columnas
#  .shape[0] número de filas .shape[1] número de columnas
X=np.arange(data.shape[1]) #Esto es igual a X=np.arange(4)

for i in range(data.shape[0]): #Esto es igual a range(3)
    plt.bar(X, data[i],
            # np.sum() suma los arrays dados en el eje x-> axis=0 (filas), eje y ->y axis=1 (columnas)
            bottom=np.sum(data[:i], axis=0),
            color=color_list[i%len(color_list)])

plt.show()