text:
#!/usr/bin/env python
"""
model.py for Blow
version: 1
"""
from __future__ import absolute_import
from __future__ import print_function

import os
import sys
import time
import numpy as np

import torch
import torch.nn as torch_nn
import torch.nn.functional as torch_nn_func

import core_scripts.other_tools.debug as nii_debug
import core_scripts.other_tools.display as nii_warn
import sandbox.block_nn as nii_nn
import sandbox.block_blow as nii_blow

__author__ = "Xin Wang"
__email__ = "wangxin@nii.ac.jp"
__copyright__ = "Copyright 2021, Xin Wang"

########
class Model(torch_nn.Module):
    """ Model definition
    """
    def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None):
        super(Model, self).__init__()

        #################
        ## must-have
        #################
        # mean std of input and output
        in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\
                                                         args, mean_std)
        self.input_mean = torch_nn.Parameter(in_m, requires_grad=False)
        self.input_std = torch_nn.Parameter(in_s, requires_grad=False)
        self.output_mean = torch_nn.Parameter(out_m, requires_grad=False)
        self.output_std = torch_nn.Parameter(out_s, requires_grad=False)
        self.input_dim = in_dim
        self.output_dim = out_dim

        # a flag for debugging (by default False)
        self.model_debug = False

        #################
        ## model config
        #################
        # waveform sampling rate
        self.sample_rate = prj_conf.wav_samp_rate

        # load speaker map
        self.speaker_map = prj_conf.options['speaker_map']
        self.speaker_num = self.speaker_map.num()
        if 'conversion_map' in prj_conf.options:
            self.conversion_map = prj_conf.options['conversion_map']
        else:
            self.conversion_map = None

        self.cond_dim = 128
        self.num_block = 8
        self.num_flow_steps_perblock = 12
        self.num_conv_channel_size = 512
        self.num_conv_conv_kernel = 3

        self.m_spk_emd = torch.nn.Embedding(self.speaker_num, self.cond_dim)

        self.m_blow = nii_blow.Blow(
            self.cond_dim, self.num_block,
            self.num_flow_steps_perblock,
            self.num_conv_channel_size,
            self.num_conv_conv_kernel)

        # only used for synthesis
        self.m_overlap = nii_blow.OverlapAdder(4096, 4096//4, False)
        # done
        return

    def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None):
        """
        """
        if data_mean_std is not None:
            in_m = torch.from_numpy(data_mean_std[0])
            in_s = torch.from_numpy(data_mean_std[1])
            out_m = torch.from_numpy(data_mean_std[2])
            out_s = torch.from_numpy(data_mean_std[3])
            if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim:
                print("Input dim: {:d}".format(in_dim))
                print("Mean dim: {:d}".format(in_m.shape[0]))
                print("Std dim: {:d}".format(in_s.shape[0]))
                print("Input dimension incompatible")
                sys.exit(1)
            if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim:
                print("Output dim: {:d}".format(out_dim))
                print("Mean dim: {:d}".format(out_m.shape[0]))
                print("Std dim: {:d}".format(out_s.shape[0]))
                print("Output dimension incompatible")
                sys.exit(1)
        else:
            in_m = torch.zeros([in_dim])
            in_s = torch.ones([in_dim])
            out_m = torch.zeros([out_dim])
            out_s = torch.ones([out_dim])

        return in_m, in_s, out_m, out_s

    def normalize_input(self, x):
        """ normalizing the input data
        """
        return (x - self.input_mean) / self.input_std

    def normalize_target(self, y):
        """ normalizing the target data
        """
        return (y - self.output_mean) / self.output_std

    def denormalize_output(self, y):
        """ denormalizing the generated output from network
        """
        return y * self.output_std + self.output_mean

    def forward(self, wav, fileinfo):
        """loss = forward(self, input_feat, wav)

        input
        -----
          wav: tensor, target waveform (batchsize, length2, 1)
               it should be raw waveform, flot valued, between (-1, 1)
               the code will do mu-law conversion

          fileinfo: list, file information for each data in the batch

        output
        ------
          loss: tensor / scalar,

        Note: returned loss can be directly used as the loss value
        no need to write Loss()
        """
        # prepare speaker IDs
        # (batch, )
        speaker_ids = torch.tensor(
            [self.speaker_map.parse(x) for x in fileinfo],
            dtype=torch.long, device=wav.device)
        # convert to embeddings
        # (batch, 1, cond_dim)
        speaker_emd = self.m_spk_emd(speaker_ids).unsqueeze(1)

        # normalize conditiona feature
        #input_feat = self.normalize_input(input_feat)
        # compute
        z, neg_logp, logp_z, log_detjac = self.m_blow(wav, speaker_emd)
        return [[-logp_z, -log_detjac], [True, True]]

    def inference(self, wav, fileinfo):
        """wav = inference(mels)

        input
        -----
          wav: tensor, target waveform (batchsize, length2, 1)

        output
        ------
          wav_new: tensor, same shape
        """
        # framing the input waveform into frames
        #
        # framed_wav (batchsize, frame_num, frame_length)
        framed_wav = self.m_overlap(wav)
        batch, frame_num, frame_len = framed_wav.shape
        # framed_Wav (batchsize * frame_num, frame_length, 1)
        framed_wav = framed_wav.view(-1, frame_len).unsqueeze(-1)

        # get the speaker IDs
        # (batch, )
        speaker_ids = torch.tensor(
            [self.speaker_map.parse(x) for x in fileinfo],
            dtype=torch.long, device=wav.device)
        # (batch * frame_num)
        speaker_ids = speaker_ids.repeat_interleave(frame_num)

        # if conversion map is defined, change the speaker identity following
        #  the conversion map. Otherwise, specify target speaker ID
        #  through environment variable TEMP_TARGET_SPEAKER
        if self.conversion_map:
            target_speaker = torch.tensor(
                [self.speaker_map.get_idx(
                    self.conversion_map[self.speaker_map.parse(x, False)]) \
                    for x in fileinfo],
                device=wav.device, dtype=torch.long)
            target_speaker = target_speaker.repeat_interleave(frame_num)
        else:
            # target speaker ID
            target_speaker = torch.tensor(
                [int(os.getenv('TEMP_TARGET_SPEAKER'))] * speaker_ids.shape[0],
                device=wav.device, dtype=torch.long)

        # if it is intended to swap the source / target speaker ID
        #  this can be used to recover the original waveform given converted
        #  speech
        flag_reverse = os.getenv('TEMP_FLAG_REVERSE')
        if flag_reverse and int(flag_reverse):
            # if this is for reverse conversion
            # swap the IDs
            print("revert IDs")
            tmp = speaker_ids
            speaker_ids = target_speaker
            target_speaker = tmp

        # print some information
        for idx in range(len(speaker_ids)):
            if idx % frame_num == 0:
                print("From ID {:3d} to {:3d}, ".format(
                    speaker_ids[idx], target_speaker[idx]), end=' ')

        # get embeddings (batch * frame_num, 1, cond_dim)
        speaker_emd = self.m_spk_emd(speaker_ids).unsqueeze(1)
        target_speaker_emb = self.m_spk_emd(target_speaker).unsqueeze(1)

        # compute
        z, neg_logp, logp_z, log_detjac = self.m_blow(framed_wav, speaker_emd)

        # output_framed (batch * frame, frame_length, 1)
        output_framed = self.m_blow.reverse(z, target_speaker_emb)

        # overlap and add
        # view -> (batch, frame_num, frame_length)
        return self.m_overlap.reverse(output_framed.view(batch, -1, frame_len),
                                      True)

    def convert(self, wav, src_id, tar_id):
        """wav = inference(mels)

        input
        -----
          wav: tensor, target waveform (batchsize, length2, 1)
          src_id: int, ID of source speaker
          tar_id: int, ID of target speaker

        output
        ------
          wav_new: tensor, same shape
        """
        # framing the input waveform into frames
        #   m_overlap.forward does framing
        # framed_wav (batchsize, frame_num, frame_length)
        framed_wav = self.m_overlap(wav)
        batch, frame_num, frame_len = framed_wav.shape

        # change frames into batch
        # framed_Wav (batchsize * frame_num, frame_length, 1)
        framed_wav = framed_wav.view(-1, frame_len).unsqueeze(-1)

        # source speaker IDs
        # (batch, )
        speaker_ids = torch.tensor([src_id for x in wav],
                                   dtype=torch.long, device=wav.device)
        # (batch * frame_num)
        speaker_ids = speaker_ids.repeat_interleave(frame_num)
        # get embeddings (batch * frame_num, 1, cond_dim)
        speaker_emd = self.m_spk_emd(speaker_ids).unsqueeze(1)

        # target speaker IDs
        tar_speaker_ids = torch.tensor([tar_id for x in wav],
                                   dtype=torch.long, device=wav.device)
        # (batch * frame_num)
        tar_speaker_ids = tar_speaker_ids.repeat_interleave(frame_num)
        target_speaker_emb = self.m_spk_emd(tar_speaker_ids).unsqueeze(1)

        # analysis
        z, _, _, _ = self.m_blow(framed_wav, speaker_emd)

        # synthesis
        # output_framed (batch * frame, frame_length, 1)
        output_framed = self.m_blow.reverse(z, target_speaker_emb)

        # overlap and add
        # view -> (batch, frame_num, frame_length)
        return self.m_overlap.reverse(
            output_framed.view(batch, -1, frame_len), True)

# Loss is returned by model.forward(), no need to specify
# just a place holder so that the output of model.forward() can be
# sent to the optimizer
class Loss():
    def __init__(self, args):
        return

    def compute(self, output, target):
        return output

if __name__ == "__main__":
    print("Definition of model")

max_stars_repo_name: Nijta/project-NN-Pytorch-scripts

max_issues_repo_name: Nijta/project-NN-Pytorch-scripts

max_forks_repo_name: Nijta/project-NN-Pytorch-scripts


text:
import numpy as np
x, y = map(int, input().split())
a, b = (np.array([input().split() for _ in range(x)], dtype=int) for _ in range(2))
print(a+b, a-b, a*b, a//b, a%b, a**b, sep='\n')

ext: py

max_stars_repo_path: Array Mathematics.py

max_issues_repo_path: Array Mathematics.py

max_forks_repo_path: Array Mathematics.py

reason: import numpy


text:
import os
import logging
import json

from nnattack.variables import auto_var, get_file_name

from params import (
    compare_attacks,
    compare_defense,

    #compare_nns,
    nn_k1_robustness,
    nn_k3_robustness,
    nn_k1_approx_robustness_figs,

    dt_robustness_figs,
    rf_robustness_figs,
    nn_k1_robustness_figs,
    nn_k3_robustness_figs,

    dt_robustness,
    rf_robustness,
    mlp_ap_robustness,
    mlp_at_robustness,
    lr_ap_robustness,
    lr_at_robustness,

    nn1_def,
    nn3_def,
    dt_def,
    rf_def,
    lr_def,
    mlp_def,
)
from main import eps_accuracy

logging.basicConfig(level=logging.DEBUG)

DEBUG = True if os.environ.get('DEBUG', False) else False

def main():
    experiments = [
        compare_attacks(),
        compare_defense(),

        #nn_k1_robustness_figs(),
        #nn_k3_robustness_figs(),
        #rf_robustness_figs(),
        #dt_robustness_figs(),

        dt_robustness(),
        rf_robustness(),
        nn_k3_robustness(),
        nn_k1_robustness(),
        #mlp_ap_robustness(),
        #mlp_at_robustness(),
        #lr_ap_robustness(),
        #lr_at_robustness(),

        #nn1_def(),
        #nn3_def(),
        #dt_def(),
        #rf_def(),
        #lr_def(),
        #mlp_def(),
    ]
    grid_params = []
    for exp in experiments:
        exp_fn, _, grid_param, run_param = exp()
        if isinstance(grid_param, list):
            grid_params.extend(grid_param)
        else:
            grid_params.append(grid_param)

    if DEBUG:
        run_param['n_jobs'] = 1
        run_param['allow_failure'] = False
    else:
        run_param['n_jobs'] = 4
        run_param['allow_failure'] = True

    auto_var.run_grid_params(exp_fn, grid_params, **run_param)
    #auto_var.run_grid_params(delete_file, grid_params, n_jobs=1,
    #                          with_hook=False, allow_failure=False)
    #auto_var.run_grid_params(celery_run, grid_params, n_jobs=1,
    #                         allow_failure=False)
    #auto_var.run_grid_params(temp_fix, grid_params, n_jobs=6,
    #                         allow_failure=False, with_hook=False)

def delete_file(auto_var):
    os.unlink(get_file_name(auto_var) + '.json')

def celery_run(auto_var):
    run_exp.delay(auto_var.var_value)

from main import set_random_seed
import numpy as np
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler

def temp_fix(auto_var):
    file_name = get_file_name(auto_var)
    print(file_name)
    if os.path.exists("%s.json" % file_name):
        with open("%s.json" % file_name, "r") as f:
            ret = json.load(f)
        if "tst_score" in ret:
            return
    else:
        return

    random_state = set_random_seed(auto_var)
    ord = auto_var.get_var("ord")

    X, y, eps_list = auto_var.get_var("dataset")
    idxs = np.arange(len(X))
    random_state.shuffle(idxs)
    trnX, tstX, trny, tsty = X[idxs[:-200]], X[idxs[-200:]], y[idxs[:-200]], y[idxs[-200:]]

    scaler = MinMaxScaler()
    trnX = scaler.fit_transform(trnX)
    tstX = scaler.transform(tstX)

    lbl_enc = OneHotEncoder(categories=[np.sort(np.unique(y))], sparse=False)
    #lbl_enc = OneHotEncoder(sparse=False)
    lbl_enc.fit(trny.reshape(-1, 1))

    auto_var.set_intermidiate_variable("lbl_enc", lbl_enc)

    results = []

    auto_var.set_intermidiate_variable("trnX", trnX)
    auto_var.set_intermidiate_variable("trny", trny)

    model_name = auto_var.get_variable_value("model")
    attack_name = auto_var.get_variable_value("attack")
    if 'adv_rf' in model_name:
        pre_model = auto_var.get_var_with_argument('model', model_name[4:])
        pre_model.fit(trnX, trny)
        if 'blackbox' in attack_name:
            auto_var.set_intermidiate_variable("model", pre_model)
    elif 'adv_nn' in model_name and 'blackbox' in attack_name:
        pre_model = auto_var.get_var_with_argument('model', model_name[4:])
        pre_model.fit(trnX, trny)
        auto_var.set_intermidiate_variable("model", pre_model)

    model = auto_var.get_var("model")
    auto_var.set_intermidiate_variable("model", model)
    model.fit(trnX, trny)

    pred = model.predict(tstX)
    ori_tstX, ori_tsty = tstX, tsty # len = 200
    idxs = np.where(pred == tsty)[0]
    random_state.shuffle(idxs)

    augX = None
    if ('adv' in model_name) or ('advPruning' in model_name) or ('robustv2' in model_name):
        assert hasattr(model, 'augX')
        auto_var.set_intermidiate_variable("trnX", model.augX)
        auto_var.set_intermidiate_variable("trny", model.augy)
        augX, augy = model.augX, model.augy

    ret['tst_score'] = (model.predict(ori_tstX) == ori_tsty).mean()
    with open("%s.json" % file_name, "w") as f:
        json.dump(ret, f)

if __name__ == "__main__":
    main()

max_stars_repo_name: wagner-group/geoadex

max_issues_repo_name: wagner-group/geoadex

max_forks_repo_name: wagner-group/geoadex


text:
import scipy.io.wavfile as siow
import scipy.signal as ssr
import matplotlib.pyplot as plt
import math

def groupNumpy(to_group_array, interval):
  '''
  Breaks numpy array into an array of arrays.  Where each array is <interval> long.
  Inputs:
    to_group_array - array we wish to break down into sub intervals
    interval - interval length we wish to break down into
  Outputs:
    2D array of size [len/interval, interval]
  '''
  print("array lenth: ", len(to_group_array))
  n = math.ceil( len(to_group_array)/(interval*1.0) )
  print("N: ", n)
  return 0

filename = "hello.wav"

audio_tuple = siow.read(filename)
audio_array = audio_tuple[1]
print("Sample rate: ", audio_tuple[0])
#Down sample from 44.1 khz to 16 khz
print("Original Sample len: ", len(audio_array))
audio_new = ssr.resample(audio_array, 16000)

groupNumpy(audio_new, 320)
'''
#Plots look about the same (probably are), so assuming down sampling works for now
plt.plot( audio_new, color="orange" )
plt.ylabel('sound wave')
plt.show()
'''

reason: import scipy


text:
import cv2
import numpy as np
#load image
img = cv2.imread("Camerica.jpg" , 0)

height , width= img.shape
#count number of colors
color = []

for i in range(height):
    for j in range(width):
        if(img[i,j] not in color):
            color.append(img[i,j])

color.sort()
print color
print len(color)

replacement=[[29, 41, 81],[ 0, 49, 82] , [14, 77 ,146] , [16 ,52 ,166] , [0,128 ,255], [15 ,82 ,186], [ 76, 81, 109],[114 ,133 ,165]]

replacement.sort()
dict = {}
j=0;k=0
for i in range(len(color)):
    dict[color[i]] = replacement[j]
    if not (i%2):
        j= j +1
    if j >= len(replacement):
        j = j - 1;
print dict

coloredImg = cv2.imread("Camerica.jpg")
for i in range(height):
    for j in range(width):
        coloredImg[i,j]= dict[img[i,j]]

print coloredImg[0,9]
cv2.imshow("final" , coloredImg)
cv2.waitKey(0)
cv2.destroyAllWindows()


text:
#python ob_foreground.py images/eagle.png
from __future__ import division
import cv2
#to show the image
from matplotlib import pyplot as plt
import numpy as np
from math import cos, sin
import argparse

if __name__=="__main__":
	argparser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
	argparser.add_argument('i',help = '')
	args = argparser.parse_args()
	i = args.i
	#read the image
	image = cv2.imread(i)
	img = image

	mask = np.zeros(img.shape[:2],np.uint8)

	bgdModel = np.zeros((1,65),np.float64)
	fgdModel = np.zeros((1,65),np.float64)

	rect = (161,79,150,150)
	cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
	mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
	img = img*mask2[:,:,np.newaxis]
	# show the images
	cv2.imshow("Result", np.hstack([img, image]))
	cv2.waitKey(0)

max_stars_repo_name: udelblue/cv

max_issues_repo_name: udelblue/cv

max_forks_repo_name: udelblue/cv


text:
import argparse
import io
import os
from typing import Callable, List, Tuple, Optional

import PIL.Image
import PIL.ImageDraw2
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
from matplotlib.patches import Patch

from model import produce_model

model = None
metadata = None

def setup_model(initial_weight: str) -> None:
    global model
    if model is None:
        model = produce_model(initial_weight)
    return

def classify_image(img: PIL.Image.Image,
                   topk: int = 1,
                   feature_threshold: float = 0.33,
                   pred_list: Optional[List[int]] = None) -> Tuple[List[Tuple[int, float]], List[np.ndarray], np.ndarray]:
    global model
    transform_tensor = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    x: torch.Tensor = transform_tensor(img)
    x = x.unsqueeze(0)
    with torch.no_grad():
        features = model(x)[0]
        output = F.adaptive_avg_pool2d(features, (1, 1)).view(-1)
        act = F.softmax(output, dim=0)
        if pred_list is None:
            _, pred_list = output.topk(topk)
            pred_list = list(pred_list.numpy())
        softmax_map, object_map = F.softmax(features, dim=0).max(dim=0)

    # prepare image classification results and respective activation map
    r = []
    activation_maps = []
    for pred in pred_list:
        r.append((pred, act[pred].item()))
        activation_maps.append(features[pred].numpy())

    # prepare object detection
    threshold_map = softmax_map < feature_threshold
    object_map[threshold_map] = -1
    object_map = object_map.numpy()
    return r, activation_maps, object_map

def idx_to_label(idx: int) -> str:
    global metadata
    if metadata is None:
        metadata = {}
        classes = torch.load('data/classes.bin')
        meta = torch.load('data/meta.bin')[0]
        metadata['classes'] = classes
        metadata['labels'] = {}
        for cls in classes:
            metadata['labels'][cls] = meta[cls][0]
    return metadata['labels'][metadata['classes'][idx]]

def wnid_to_idx(wnid: str) -> int:
    global metadata
    return metadata[1].index(wnid)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate Class Activation Map for images')
    parser.add_argument('-i', '--input', type=str, metavar='INPUT_FILE', required=True,
                        help="one input image for producing CAM")
    parser.add_argument('-o', '--output', type=str, metavar='OUTPUT_DIR', required=True,
                        help="output image directory")
    parser.add_argument('-w', '--initial-weight', type=str, metavar='INITIAL_CKPT', required=True,
                        help="path to initial weights")
    parser.add_argument('-k', '--topk', type=int, metavar='TOP_K', default=1,
                        help="produce TOP_K number of heat-maps")
    parser.add_argument('-t', '--threshold', type=float, default=0.33, metavar='DET_THRESHOLD',
                        help="threshold for object detection")
    parser.add_argument('--font', type=str, default=os.path.expanduser("~/Library/Fonts/OpenSans-Bold.ttf"),
                        help="font for rendering text")
    parser.add_argument('--activation', action='store_true',
                        help="produce activation map")
    parser.add_argument('--detection', action='store_true',
                        help="produce object detection map")
    parser.add_argument('--no-softmax', action='store_true',
                        help="do not output softmax value")
    parser.add_argument('--no-label', action='store_true',
                        help="do not output object label")
    parser.add_argument('--full-resolution', action='store_true')
    parser.add_argument('--normalize-per-category', action='store_true',
                        help="self explanatory")
    parser.add_argument('-c', '--categories', type=int, nargs='+', metavar='CATEGORY_INDICES', required=False,
                        help="indices of categories to render")

    args = parser.parse_args()

    setup_model(args.initial_weight)

    transform_image = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224)
    ])

    # initialize data
    image = PIL.Image.open(args.input)
    if args.full_resolution:
        pass
    else:
        image: PIL.Image.Image = transform_image(image)

    if args.categories is None:
        pred_list, activation_maps, object_map = classify_image(image, topk=args.topk, feature_threshold=args.threshold)
    else:
        pred_list, activation_maps, object_map = classify_image(image,
                                                                feature_threshold=args.threshold,
                                                                pred_list=args.categories)
    output_name = os.path.basename(args.output)
    os.makedirs(args.output, exist_ok=True)

    image.save(os.path.join(args.output, 'orig.png'))

    if args.activation:
        colormap: Callable = plt.cm.gnuplot2
        if args.normalize_per_category:
            for i, activation_map in enumerate(activation_maps):
                activation_map = (activation_map - activation_map.min()) / activation_map.max()
                activation_maps[i] = activation_map
        else:
            # unitize using same min/max
            activation_maps = np.asarray(activation_maps)
            activation_maps = (activation_maps - activation_maps.min()) / activation_maps.max()
        for pred, activation_map in zip(pred_list, activation_maps):
            pred_idx, confidence = pred
            # produce activation map
            activation_map = colormap(activation_map)
            activation_map[:, :, 3] = 0.75  # alpha channel
            activation_map *= 255
            activation_map = np.uint8(activation_map.clip(0, 255))
            activation_map_image = PIL.Image.fromarray(activation_map).resize(image.size, resample=PIL.Image.BILINEAR)
            # obtain the image with activation map overlay
            final_cam_image = PIL.Image.alpha_composite(image.convert('RGBA'), activation_map_image)
            # produce text label
            label = idx_to_label(pred_idx)
            draw = PIL.ImageDraw2.Draw(final_cam_image)
            color = 'white'
            font = PIL.ImageDraw2.Font(color, args.font, size=14)
            text = ""
            if args.no_label:
                pass
            else:
                text += f"{label}"
            if args.no_softmax:
                pass
            else:
                text += f" {confidence:.4f}"
            draw.text((5, 5), text, font)

            final_cam_image.save(os.path.join(args.output, f'cam_{pred_idx}.png'))

    if args.detection:
        color_list = np.asarray(plt.cm.Set3.colors)
        color_list = np.concatenate((color_list, np.ones((12, 1))), axis=1)
        # set alpha channel
        color_list[:, 3] = 0.75
        size = object_map.shape
        size = (*size, 4)
        rgb_overlay = np.zeros(size)
        # render detection map
        labels = []
        for color_idx, cat_idx in enumerate(np.unique(object_map)):
            if cat_idx == -1:
                continue
            # will simply crash when more object types are present
            rgb_overlay[object_map == cat_idx] = color_list[color_idx]
            labels.append((color_list[color_idx], idx_to_label(cat_idx)))
        # produce image
        # TODO: use matplotlib to generate images with legend/labels
        rgb_overlay = np.uint8(np.clip(rgb_overlay * 255, 0, 255))
        rgb_overlay_img = PIL.Image.fromarray(rgb_overlay)
        rgb_overlay_img = rgb_overlay_img.resize(image.size, resample=PIL.Image.BILINEAR)

        final_detection_map = PIL.Image.alpha_composite(image.convert('RGBA'), rgb_overlay_img)
        final_detection_map.save(os.path.join(args.output, f"det_thr_{args.threshold:.2f}.png"))
        # eh? https://stackoverflow.com/questions/4534480
        patches = [
            Patch(color=color, label=label) for (color, label) in labels
        ]
        fig = plt.figure(figsize=(4, 3), dpi=600)
        fig.legend(patches, [label for (_, label) in labels], loc='center', frameon=False)
        buf = io.BytesIO()
        plt.savefig(buf, bbox_inches='tight')
        legend = PIL.Image.open(buf)
        legend.save(os.path.join(args.output, f"det_legend_thr_{args.threshold:.2f}.png"))

max_stars_repo_name: zcqian/pth-search-hyperparams

max_issues_repo_name: zcqian/pth-search-hyperparams

max_forks_repo_name: zcqian/pth-search-hyperparams


text:
# necessary basic libraries
import numpy as np
import cv2
import json
import rasterio
import geopandas
from geopandas.tools import sjoin
from rasterio.mask import mask
from rasterio.io import DatasetReader
from shapely.geometry import box
import geopandas as gpd
from fiona.crs import from_epsg

# class img_data(DatasetReader):
#    """
#    Class for image data to be processed for tiling
#    """
#
#    def __init__(self):
#        self.x_origin = self.bounds[0]
#        self.y_origin = self.bounds[3]
#        self.pixelSizeX = self.affine[0]
#        self.pixelSizeY = -self.affine[4]
#

def getFeatures(gdf):
    """
    Function to parse features from GeoDataFrame in such a manner that rasterio wants them
    """
    return [json.loads(gdf.to_json())["features"][0]["geometry"]]

def tile_data(data, out_dir, buffer=30, tile_width=200, tile_height=200, crowns=None):
    """
    Function to tile up image and (if included) corresponding crowns
    """
    for minx in np.arange(data.bounds[0], data.bounds[2] - tile_width, tile_width, int):
        # print("minx:", minx)
        for miny in np.arange(
            data.bounds[1], data.bounds[3] - tile_height, tile_height, int
        ):
            # print("miny:", miny)

            # new tiling bbox including the buffer
            bbox = box(
                minx - buffer,
                miny - buffer,
                minx + tile_width + buffer,
                miny + tile_height + buffer,
            )
            # define the bounding box of the tile, excluding the buffer (hence selecting just the central part of the tile)
            bbox_central = box(minx, miny, minx + tile_width, miny + tile_height)

            # turn the bounding boxes into geopandas DataFrames
            geo = gpd.GeoDataFrame({"geometry": bbox}, index=[0], crs=from_epsg(4326))
            geo_central = gpd.GeoDataFrame(
                {"geometry": bbox_central}, index=[0], crs=from_epsg(4326)
            )  # 3182

            # here we are cropping the tiff to the bounding box of the tile we want
            coords = getFeatures(geo)
            # print("Coords:", coords)

            # define the tile as a mask of the whole tiff with just the bounding box
            out_img, out_transform = mask(data, shapes=coords, crop=True)
            # print('out transform:', out_transform)

            # This can be useful when reprojecting later as know the crs format to put it into
            # epsg_code = int(data.crs.data["init"][5:])
            # print(epsg_code)

            # copy the metadata then update it, the "nodata" and "dtype" where important as made larger
            # tifs have outputted tiles which were not just black
            out_meta = data.meta.copy()
            out_meta.update(
                {
                    "driver": "GTiff",
                    "height": out_img.shape[1],
                    "width": out_img.shape[2],
                    "transform": out_transform,
                    "nodata": None,
                    "dtype": "uint8",
                }
            )
            # print('Out Meta:',out_meta)

            # Saving the tile as a new tiff, named by the origin of the tile. If tile appears blank in folder can show the image here and may
            # need to fix RGB data or the dtype
            # show(out_img)
            out_tif = out_dir + "tile_" + str(minx) + "_" + str(miny) + ".tif"
            with rasterio.open(out_tif, "w", **out_meta) as dest:
                dest.write(out_img)

            # read in the tile we have just saved
            clipped = rasterio.open(
                out_dir + "/tile_" + str(minx) + "_" + str(miny) + ".tif"
            )
            # read it as an array
            # show(clipped)
            arr = clipped.read()

            # each band of the tiled tiff is a colour!
            R = arr[0]
            G = arr[1]
            B = arr[2]

            # stack up the bands in an order appropriate for saving with cv2, then rescale to the correct 0-255 range for cv2

            rgb = np.dstack((B, G, R))  # BGR for cv2
            rgb_rescaled = rgb  # scale to image
            # print('rgb rescaled', rgb_rescaled)

            # save this as jpg or png...we are going for png...again, named with the origin of the specific tile
            # here as a naughty method
            cv2.imwrite(
                out_dir + "tile_" + str(minx) + "_" + str(miny) + ".png",
                rgb_rescaled,
            )

            # img = cv2.imread(
            #    "gdrive/MyDrive/JamesHirst/NY/LargeArea_images/naip_cayuga/naip_cayuga_tiled_by_me/tile_"
            #    + str(minx)
            #    + "_"
            #    + str(miny)
            #    + ".png"
            # )
            # print('png shape:', img.shape)
            if crowns is not None:
                # select the crowns that intersect the non-buffered central
                # section of the tile using the inner join
                # JB : a better solution would be to clip crowns to tile extent
                overlapping_crowns = sjoin(crowns, geo_central, how="inner")
                overlapping_crowns = overlapping_crowns.explode(index_parts=True)
                # print("Overlapping crowns:", overlapping_crowns)

                # translate to 0,0 to overlay on png
                # this now works as a universal approach.
                if minx == data.bounds[0] and miny == data.bounds[1]:
                    # print("We are in the bottom left!")
                    moved = overlapping_crowns.translate(-minx, -miny)
                elif miny == data.bounds[1]:
                    # print("We are on the bottom, but not bottom left")
                    moved = overlapping_crowns.translate(-minx + buffer, -miny)
                elif minx == data.bounds[0]:
                    # print("We are along the left hand side, but not bottom left!")
                    moved = overlapping_crowns.translate(-minx, -miny + buffer)
                else:
                    # print("We are in the middle!")
                    moved = overlapping_crowns.translate(-minx + buffer, -miny + buffer)
                # print("Moved coords:", moved)

                # scale to deal with the resolution
                scalingx = 1 / (data.transform[0])
                scalingy = -1 / (data.transform[4])
                moved_scaled = moved.scale(scalingx, scalingy, origin=(0, 0))
                # print(moved_scaled)
                impath = {
                    "imagePath": (
                        out_dir + "tile_" + str(minx) + "_" + str(miny) + ".png"
                    )
                }

                # save as a geojson, a format compatible with detectron2, again named by the origin of the tile.
                # If the box selected from the image is outside of the mapped region due to the image being on a slant
                # then the shp file will have no info on the crowns and hence will create an empty gpd Dataframe.
                # this causes an error so skip creating geojson. The training code will also ignore png so no problem.
                try:
                    filename = "./tile_" + str(minx) + "_" + str(miny) + ".geojson"
                    moved_scaled.to_file(
                        driver="GeoJSON",
                        filename=filename,
                    )
                    with open(filename, "r") as f:
                        shp = json.load(f)
                        shp.update(impath)
                    with open(filename, "w") as f:
                        json.dump(shp, f)
                except:
                    print("ValueError: Cannot write empty DataFrame to file.")
                    continue

def tile_data_reduced(
    data, out_dir, buffer=30, tile_width=200, tile_height=200, crowns=None, threshold=0
):
    """
    Function to tile up image and (if included) corresponding crowns.
    Only outputs tiles with crowns in.
    """
    for minx in np.arange(data.bounds[0], data.bounds[2] - tile_width, tile_width, int):
        # print("minx:", minx)
        for miny in np.arange(
            data.bounds[1], data.bounds[3] - tile_height, tile_height, int
        ):
            # print("miny:", miny)

            # new tiling bbox including the buffer
            bbox = box(
                minx - buffer,
                miny - buffer,
                minx + tile_width + buffer,
                miny + tile_height + buffer,
            )
            # define the bounding box of the tile, excluding the buffer (hence selecting just the central part of the tile)
            bbox_central = box(minx, miny, minx + tile_width, miny + tile_height)

            # turn the bounding boxes into geopandas DataFrames
            geo = gpd.GeoDataFrame({"geometry": bbox}, index=[0], crs=from_epsg(4326))
            geo_central = gpd.GeoDataFrame(
                {"geometry": bbox_central}, index=[0], crs=from_epsg(4326)
            )  # 3182
            overlapping_crowns = sjoin(crowns, geo_central, how="inner")

            # skip firward if there are no crowns in a tile
            if crowns is not None:
                if overlapping_crowns.empty:
                    continue
                if len(overlapping_crowns) < threshold:
                    continue
            # here we are cropping the tiff to the bounding box of the tile we want
            coords = getFeatures(geo)
            # print("Coords:", coords)

            # define the tile as a mask of the whole tiff with just the bounding box
            out_img, out_transform = mask(data, shapes=coords, crop=True)

            # Or to really narrow down the crop onto the crown area
            # newbox = overlapping_crowns.total_bounds
            # newbox = gpd.GeoDataFrame(
            #    {"geometry": box(newbox[0], newbox[1], newbox[2], newbox[3])},
            #    index=[0],
            #    crs=from_epsg(4326),
            # )
            # newbox = getFeatures(newbox)

            # out_img, out_transform = mask(data, shapes=newbox, crop=True)

            # print('out transform:', out_transform)

            # This can be useful when reprojecting later as know the crs format to put it into
            # epsg_code = int(data.crs.data["init"][5:])
            # print(epsg_code)

            # copy the metadata then update it, the "nodata" and "dtype" where important as made larger
            # tifs have outputted tiles which were not just black
            out_meta = data.meta.copy()
            out_meta.update(
                {
                    "driver": "GTiff",
                    "height": out_img.shape[1],
                    "width": out_img.shape[2],
                    "transform": out_transform,
                    "nodata": None,
                    "dtype": "uint8",
                }
            )
            # print('Out Meta:',out_meta)

            # Saving the tile as a new tiff, named by the origin of the tile. If tile appears blank in folder can show the image here and may
            # need to fix RGB data or the dtype
            # show(out_img)
            out_tif = out_dir + "tile_" + str(minx) + "_" + str(miny) + ".tif"
            with rasterio.open(out_tif, "w", **out_meta) as dest:
                dest.write(out_img)

            # read in the tile we have just saved
            clipped = rasterio.open(
                out_dir + "/tile_" + str(minx) + "_" + str(miny) + ".tif"
            )
            # read it as an array
            # show(clipped)
            arr = clipped.read()

            # each band of the tiled tiff is a colour!
            R = arr[0]
            G = arr[1]
            B = arr[2]

            # stack up the bands in an order appropriate for saving with cv2, then rescale to the correct 0-255 range for cv2

            rgb = np.dstack((B, G, R))  # BGR for cv2
            rgb_rescaled = rgb  # scale to image
            # print('rgb rescaled', rgb_rescaled)

            # save this as jpg or png...we are going for png...again, named with the origin of the specific tile
            # here as a naughty method
            cv2.imwrite(
                out_dir + "tile_" + str(minx) + "_" + str(miny) + ".png",
                rgb_rescaled,
            )

            # img = cv2.imread(
            #    "gdrive/MyDrive/JamesHirst/NY/LargeArea_images/naip_cayuga/naip_cayuga_tiled_by_me/tile_"
            #    + str(minx)
            #    + "_"
            #    + str(miny)
            #    + ".png"
            # )
            # print('png shape:', img.shape)
            if crowns is not None:
                # select the crowns that intersect the non-buffered central
                # section of the tile using the inner join
                # JB : a better solution would be to clip crowns to tile extent
                overlapping_crowns = sjoin(crowns, geo_central, how="inner")
                overlapping_crowns = overlapping_crowns.explode(index_parts=True)
                # print("Overlapping crowns:", overlapping_crowns)

                # translate to 0,0 to overlay on png
                # this now works as a universal approach.
                if minx == data.bounds[0] and miny == data.bounds[1]:
                    # print("We are in the bottom left!")
                    moved = overlapping_crowns.translate(-minx, -miny)
                elif miny == data.bounds[1]:
                    # print("We are on the bottom, but not bottom left")
                    moved = overlapping_crowns.translate(-minx + buffer, -miny)
                elif minx == data.bounds[0]:
                    # print("We are along the left hand side, but not bottom left!")
                    moved = overlapping_crowns.translate(-minx, -miny + buffer)
                else:
                    # print("We are in the middle!")
                    moved = overlapping_crowns.translate(-minx + buffer, -miny + buffer)
                # print("Moved coords:", moved)

                # scale to deal with the resolution
                scalingx = 1 / (data.transform[0])
                scalingy = -1 / (data.transform[4])
                moved_scaled = moved.scale(scalingx, scalingy, origin=(0, 0))
                # print(moved_scaled)

                impath = {
                    "imagePath": (
                        out_dir + "tile_" + str(minx) + "_" + str(miny) + ".png"
                    )
                }

                # save as a geojson, a format compatible with detectron2, again named by the origin of the tile.
                # If the box selected from the image is outside of the mapped region due to the image being on a slant
                # then the shp file will have no info on the crowns and hence will create an empty gpd Dataframe.
                # this causes an error so skip creating geojson. The training code will also ignore png so no problem.
                try:
                    filename = "./tile_" + str(minx) + "_" + str(miny) + ".geojson"
                    moved_scaled.to_file(
                        driver="GeoJSON",
                        filename=filename,
                    )
                    with open(filename, "r") as f:
                        shp = json.load(f)
                        shp.update(impath)
                    with open(filename, "w") as f:
                        json.dump(shp, f)
                except:
                    print("ValueError: Cannot write empty DataFrame to file.")
                    continue

if __name__ == "__main__":
    # Right let's test this first with Sepilok 10cm resolution, then I need to try it with 50cm resolution.
    img_path = "/content/drive/Shareddrives/detectreeRGB/benchmark/Ortho2015_benchmark/P4_Ortho_2015.tif"
    crown_path = "gdrive/MyDrive/JamesHirst/NY/Buffalo/Buffalo_raw_data/all_crowns.shp"
    out_dir = "./"
    # Read in the tiff file
    # data = img_data.open(img_path)
    # Read in crowns
    crowns = geopandas.read_file(crown_path)
    print(
        "shape =",
        data.shape,
        ",",
        data.bounds,
        "and number of bands =",
        data.count,
        ", crs =",
        data.crs,
    )

    buffer = 20
    tile_width = 200
    tile_height = 200
    # resolution = 0.6 # in metres per pixel - @James Ball can you get this from the tiff?

    tile_data(data, buffer, tile_width, tile_height, out_dir, crowns)


text:
from ast import Str
from django.shortcuts import redirect, render
from django.views.generic import View,TemplateView
from django.http import JsonResponse, HttpResponse
import pytesseract
from numpy import fromstring,uint8
import cv2
from pytesseract.pytesseract import Output
from InvoiceParser.settings import tesseract_location
import json
from .models import Invoice,InvoiceLabel
import base64,uuid
import io
from PIL import Image
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.contrib import messages
from django.shortcuts import get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin
from .utils import *
from openpyxl import Workbook
from io import BytesIO

pytesseract.pytesseract.tesseract_cmd=tesseract_location

class ParseDataView(LoginRequiredMixin,View):

    def post(self,*args, **kwargs):
        if self.request.method=='POST':
           request=self.request
           coordinates=json.loads(request.POST['json_coord'])
           invoice_file=request.POST['img64']
           format,invoice=invoice_file.split(';base64,')
           ext=format.split('/')[1]
           invoice=base64.b64decode(invoice)

           invoice_cv2=cv2.imdecode(fromstring(invoice,uint8),cv2.IMREAD_UNCHANGED)

           buf = io.BytesIO(invoice)
           img = Image.open(buf)
           img=img.convert('RGB')

           img_io = io.BytesIO()
           img.save(img_io, format='JPEG')

           model=Invoice(User=self.request.user)
           model.invoice_image=InMemoryUploadedFile(img_io, field_name=None, name="{}.{}".format(uuid.uuid4(),ext), content_type='image/jpeg', size=img_io.tell, charset=None)
           model.save()

           for coord in coordinates:
               key=coord['label']
               x,y,w,h=int(coord['x']),int(coord['y']),int(coord['w']),int(coord['h'])
               value=""
               if key.lower()!="table":
                    ROI=invoice_cv2[y:y+h,x:x+w]
                    value=pytesseract.image_to_string(ROI)
                    value=value.strip()
               print(key,value)
               label=InvoiceLabel(invoice_model=model,key=key,value=value,x_axis=x,y_axis=y,width=w,height=h)
               label.save()
           messages.success(request,"Congrats! {} Model was succesfully Created..".format(model.model_number))

        return JsonResponse({'Model_id':model.model_number})

    def get(self,*args, **kwargs):
        return render(self.request,'newmodel.html')

class ProcessInvoices(LoginRequiredMixin,View):

    def get(self,*args, **kwargs):
        obj=get_object_or_404(Invoice,model_number=self.kwargs['model_number'])
        return render(self.request,'uploadimage.html',{'model_no':self.kwargs['model_number']})

    def post(self,*args, **kwargs):
        request=self.request
        obj=get_object_or_404(Invoice,model_number=self.kwargs['model_number'])
        invoice_list=list()
        invoice_labels=obj.invoicelabel_set.all()

        for key,image in request.FILES.items():
            obj=cv2.imdecode(fromstring(image.read(),uint8),cv2.IMREAD_UNCHANGED)
            invoice_list.append((key,obj))

        result_set=dict()

        for key,invoice in invoice_list:
            key=key.split('_')[1]
            result_set[key]=dict()
            temp_invoice=invoice
            for label in invoice_labels:
                result_set[key][label.key]=''
                x,y,w,h=label.x_axis,label.y_axis,label.width,label.height
                invoice=invoice[y:y+h,x:x+w]
                if label.key=='table':
                    invoice=cv2.cvtColor(invoice, cv2.COLOR_BGR2GRAY)
                    invoice_img_bin=inverted_thresholding(invoice)
                    _,h_mask=horizontal_mask(invoice,invoice_img_bin)
                    _,v_mask=vertical_mask(invoice,invoice_img_bin)
                    table_mask = cv2.bitwise_or(h_mask,v_mask)
                    invoice_img_bin[np.where(table_mask==255)]=0
                    dilated_image=dilate_image(invoice_img_bin)

                    contours, hierarchy = cv2.findContours(
                            dilated_image, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE)

                    bounding_box_list=[ cv2.boundingRect(cnt) for cnt in contours ]
                    bounding_box_list=sorted(bounding_box_list,key=lambda x: (x[1],x[0]))

                    final_table=evaluate(invoice,bounding_box_list)
                    result_set[key][label.key]=final_table
                else:
                    value=pytesseract.image_to_string(invoice)
                    value=value.strip()
                    value=value.replace('\n'," ")
                    result_set[key][label.key]=value
                # print(result_set)
                invoice=temp_invoice

        for key in result_set.keys():
            value=result_set[key]
            result_set[key]=list([request.POST[key],value])

        return render(self.request,'display.html',{'data':json.dumps(result_set),'model_number':self.kwargs['model_number']})

def download_csv(request):

    obj=json.loads(request.body)
    obj=obj['write_data']

    csv=Workbook()
    flag=True
    sheet=None
    for key in obj.keys():
        if flag:
            sheet=csv.active
            flag=False
        else:
            sheet=csv.create_sheet()

        for item,item_value in obj[key][1].items():
            if item!='table':
                sheet.append([item,item_value])
            else:
                for row in item_value:
                    sheet.append(row)

    response = HttpResponse(content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    response['Content-Disposition'] = 'attachment; filename=Invoice_data.xlsx'

    csv.save(response)
    return response

def download_json(request):
    obj=json.loads(request.body)
    obj=obj['write_data']
    response = HttpResponse(json.dumps(obj),content_type="application/json")
    response['Content-Disposition'] = 'attachment; filename=Invoice_data.json'
    return response

max_stars_repo_name: ArsalanJamali/InvoiceParser-POC

max_issues_repo_name: ArsalanJamali/InvoiceParser-POC

max_forks_repo_name: ArsalanJamali/InvoiceParser-POC

reason: from numpy


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import numpy as np

from .check_random_state import check_random_state

__all__ = ['random_cmap']

def random_cmap(ncolors=256, background_color='black', random_state=None):
    """
    Generate a matplotlib colormap consisting of random (muted) colors.

    A random colormap is very useful for plotting segmentation images.

    Parameters
    ----------
    ncolors : int, optional
        The number of colors in the colormap.  For use with segmentation
        images, ``ncolors`` should be set to the number of labels.  The
        default is 256.

    background_color : str, optional
        The name of the background (first) color in the colormap.  Valid
        colors names are defined by ``matplotlib.colors.cnames``.  The
        default is ``'black'``.

    random_state : int or `~numpy.random.RandomState`, optional
        The pseudo-random number generator state used for random
        sampling.  Separate function calls with the same
        ``random_state`` will generate the same colormap.

    Returns
    -------
    cmap : `matplotlib.colors.Colormap`
        The matplotlib colormap with random colors.
    """

    from matplotlib import colors

    prng = check_random_state(random_state)
    h = prng.uniform(low=0.0, high=1.0, size=ncolors)
    s = prng.uniform(low=0.2, high=0.7, size=ncolors)
    v = prng.uniform(low=0.5, high=1.0, size=ncolors)
    hsv = np.dstack((h, s, v))
    rgb = np.squeeze(colors.hsv_to_rgb(hsv))

    if background_color is not None:
        if background_color not in colors.cnames:
            raise ValueError('"{0}" is not a valid background color '
                             'name'.format(background_color))
        rgb[0] = colors.hex2color(colors.cnames[background_color])

    return colors.ListedColormap(rgb)

max_stars_repo_name: barentsen/photutils

max_issues_repo_name: barentsen/photutils

max_forks_repo_name: barentsen/photutils


text:
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

from typing import List, Optional, Union, Any

import numpy as np
import torch
from detectron2.data.transforms.augmentation import AugmentationList, Augmentation
from detectron2.structures import Boxes
from fvcore.transforms.transform import Transform, TransformList

class AugInput:
    """
    Same as AugInput in vision/fair/detectron2/detectron2/data/transforms/augmentation.py
    but allows torch.Tensor as input
    """

    def __init__(
        self,
        image: Union[np.ndarray, torch.Tensor],
        *,
        boxes: Optional[Union[np.ndarray, torch.Tensor, Boxes]] = None,
        sem_seg: Optional[Union[np.ndarray, torch.Tensor]] = None,
    ):
        """
        Args:
            image (ndarray/torch.Tensor): (H,W) or (H,W,C) ndarray of type uint8 in range [0, 255], or
                floating point in range [0, 1] or [0, 255]. (C, H, W) for tensor.
            boxes (ndarray or None): Nx4 float32 boxes in XYXY_ABS mode
            sem_seg (ndarray or None): HxW uint8 semantic segmentation mask. Each element
                is an integer label of pixel.
        """
        self.image = image
        self.boxes = boxes
        self.sem_seg = sem_seg

    def transform(self, tfm: Transform) -> None:
        """
        In-place transform all attributes of this class.

        By "in-place", it means after calling this method, accessing an attribute such
        as ``self.image`` will return transformed data.
        """
        self.image = tfm.apply_image(self.image)
        if self.boxes is not None:
            self.boxes = tfm.apply_box(self.boxes)
        if self.sem_seg is not None:
            self.sem_seg = tfm.apply_segmentation(self.sem_seg)

    def apply_augmentations(
        self, augmentations: List[Union[Augmentation, Transform]]
    ) -> AugmentationList:
        """
        Equivalent of ``AugmentationList(augmentations)(self)``
        """
        return AugmentationList(augmentations)(self)

class Tensor2Array(Transform):
    """Convert image tensor (CHW) to np array (HWC)"""

    def __init__(self):
        super().__init__()

    def apply_image(self, img: torch.Tensor) -> np.ndarray:
        # CHW -> HWC
        assert isinstance(img, torch.Tensor)
        assert len(img.shape) == 3, img.shape
        return img.cpu().numpy().transpose(1, 2, 0)

    def apply_coords(self, coords: Any) -> Any:
        return coords

    def apply_segmentation(self, segmentation: torch.Tensor) -> np.ndarray:
        assert len(segmentation.shape) == 2, segmentation.shape
        return segmentation.cpu().numpy()

    def inverse(self) -> Transform:
        return Array2Tensor()

class Array2Tensor(Transform):
    """Convert image np array (HWC) to torch tensor (CHW)"""

    def __init__(self):
        super().__init__()

    def apply_image(self, img: np.ndarray) -> torch.Tensor:
        # HWC -> CHW
        assert isinstance(img, np.ndarray)
        assert len(img.shape) == 3, img.shape
        return torch.from_numpy(img.transpose(2, 0, 1).astype("float32"))

    def apply_coords(self, coords: Any) -> Any:
        return coords

    def apply_segmentation(self, segmentation: np.ndarray) -> torch.Tensor:
        assert len(segmentation.shape) == 2, segmentation.shape
        return torch.from_numpy(segmentation.astype("long"))

    def inverse(self) -> Transform:
        return Tensor2Array()


text:
"""
Demonstrates chaco performance with large datasets.

There are 10 plots with 100,000 points each.  Right-click and drag to
create a range selection region.  The region can be moved around and
resized (drag the edges).  These interactions are very fast because
of the backbuffering built into chaco.

Zooming with the mousewheel and the zoombox (as described in simple_line.py)
is also available, but panning is not.
"""

# Major library imports
from scipy.special import jn
from numpy import arange

from chaco.example_support import COLOR_PALETTE
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import Bool, HasTraits, Instance
from traitsui.api import Group, Item, UItem, View

# Chaco imports
from chaco.api import OverlayPlotContainer, create_line_plot, add_default_axes, \
                                 add_default_grids
from chaco.tools.api import RangeSelection, RangeSelectionOverlay, ZoomTool

#===============================================================================
# # Create the Chaco plot.
#===============================================================================

# Do the plots use downsampling?
use_downsampling = True

def _create_plot_component(use_downsampling=True):

    container = OverlayPlotContainer(
        padding=40,
        bgcolor="lightgray",
        use_backbuffer=True,
        border_visible=True,
        fill_padding=True)

    numpoints = 100000
    low = -5
    high = 15.0
    x = arange(low, high + 0.001, (high - low) / numpoints)

    # Plot some bessel functionsless ../en
    value_mapper = None
    index_mapper = None
    for i in range(10):
        y = jn(i, x)
        plot = create_line_plot(
            (x, y), color=tuple(COLOR_PALETTE[i]), width=2.0)
        plot.use_downsampling = use_downsampling

        if value_mapper is None:
            index_mapper = plot.index_mapper
            value_mapper = plot.value_mapper
            add_default_grids(plot)
            add_default_axes(plot)
        else:
            plot.value_mapper = value_mapper
            value_mapper.range.add(plot.value)
            plot.index_mapper = index_mapper
            index_mapper.range.add(plot.index)
        if i % 2 == 1:
            plot.line_style = "dash"
        plot.bgcolor = "white"
        container.add(plot)

    selection_overlay = RangeSelectionOverlay(component=plot)
    plot.tools.append(RangeSelection(plot))
    zoom = ZoomTool(plot, tool_mode="box", always_on=False)
    plot.overlays.append(selection_overlay)
    plot.overlays.append(zoom)

    return container

#===============================================================================
# Attributes to use for the plot view.
size = (600, 500)
title = "Million Point Plot"

#===============================================================================
# # Demo class that is used by the demo.py application.
#===============================================================================
class Demo(HasTraits):
    plot = Instance(Component)

    use_downsampling = Bool(True)

    traits_view = View(
        UItem(
            'plot', editor=ComponentEditor()),
        Group(Item('use_downsampling')),
        width=size[0],
        height=size[1],
        resizable=True,
        title=title)

    def _plot_default(self):
        return _create_plot_component(self.use_downsampling)

    def _use_downsampling_changed(self):
        self.plot = _create_plot_component(self.use_downsampling)

demo = Demo()

if __name__ == "__main__":
    demo.configure_traits()

#--EOF---

reason: from numpy,from scipy


text:
# -*- coding: utf-8 -*-
# pylint: disable=E1101,W0104
"""
Tests for NRWAL equation directory handler objects
"""
import numpy as np
import os
import pytest

from NRWAL.handlers.equations import Equation
from NRWAL.handlers.groups import EquationGroup
from NRWAL.handlers.directories import EquationDirectory

TEST_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_DATA_DIR = os.path.join(TEST_DIR, 'data/')
MODULE_DIR = os.path.dirname(TEST_DIR)
EQNS_DIR = os.path.join(MODULE_DIR, 'NRWAL/')

GOOD_DIR = os.path.join(TEST_DATA_DIR, 'test_eqns_dir/')

BAD_DIR = os.path.join(TEST_DATA_DIR, 'bad_eqn_dir/')
BAD_FILE_TYPE = os.path.join(BAD_DIR, 'bad_file_type.txt')
BAD_EQN = os.path.join(BAD_DIR, 'bad_list_eqn.yaml')

COST_REDUCTIONS_DIR = os.path.join(TEST_DATA_DIR, 'test_cost_reductions/')

def test_bad_eqn_dir():
    """Test that EquationDirectory raises a RuntimeError when passed a
    directory with bad equation files."""
    with pytest.raises(RuntimeError):
        EquationDirectory(BAD_DIR)

def test_eqn_dir_parsing():
    """Test the equation directory parsing logic and recursion"""
    obj = EquationDirectory(GOOD_DIR)
    assert isinstance(obj, EquationDirectory)
    assert isinstance(obj['jacket'], EquationGroup)
    assert isinstance(obj['jacket.yaml'], EquationGroup)

    assert isinstance(obj['subdir'], EquationDirectory)
    assert isinstance(obj['subdir::jacket'], EquationGroup)
    assert isinstance(obj['subdir::jacket::lattice'], Equation)

    assert '.ignore' not in obj.keys()
    assert '__ignore' not in obj.keys()
    assert '.ignore' not in obj['subdir'].keys()
    assert '__ignore' not in obj['subdir'].keys()
    assert 'ignore' not in str(EquationDirectory)
    assert 'empty' not in obj.keys()

    with pytest.raises(KeyError):
        obj['bad']  # pylint: disable=W0104

    with pytest.raises(KeyError):
        obj['jacket::bad']  # pylint: disable=W0104

def test_print_eqn_dir():
    """Test the pretty printing of the EquationDirectory heirarchy"""
    obj = EquationDirectory(GOOD_DIR)
    assert len(str(obj).split('\n')) == 34

def test_variable_setting():
    """Test the presence of a variables.yaml file in an EquationDirectory"""
    obj = EquationDirectory(GOOD_DIR)
    assert not obj.default_variables

    with pytest.raises(RuntimeError):
        obj['jacket::outfitting_8MW'].evaluate(depth=10)

    eqn = obj['subdir::jacket::outfitting_8MW']
    assert eqn.evaluate(depth=10) == 624.0
    assert eqn.evaluate(depth=10, outfitting_cost=20) == 1248.0
    assert eqn.evaluate(depth=10, outfitting_cost=10) == eqn.evaluate(depth=10)
    assert eqn.evaluate(depth=10, outfitting_cost=20) != eqn.evaluate(depth=10)

    with pytest.raises(RuntimeError):
        eqn.evaluate()

    assert obj['subdir'].default_variables['lattice_cost'] == 100
    sjacket = obj['subdir::jacket']
    assert sjacket.default_variables['lattice_cost'] == 100
    assert sjacket['outfitting_8MW'].default_variables['lattice_cost'] == 100
    assert sjacket['lattice'].default_variables['lattice_cost'] == 100
    subsub = obj['subdir::subsubdir']
    ssjacket = subsub['jacket']
    assert subsub.default_variables['lattice_cost'] == 50
    assert ssjacket.default_variables['lattice_cost'] == 50
    assert ssjacket['lattice'].default_variables['lattice_cost'] == 50
    x = ssjacket['subgroup3::eqn123'].default_variables['lattice_cost']
    assert x == 50

    eqn = obj['subdir::subsubdir::jacket::subgroup3::eqn123']
    assert eqn.default_variables['lattice_cost'] == 50
    assert eqn.default_variables['outfitting_cost'] == 10
    assert eqn.evaluate() == 95

def test_nearest():
    """Test the lookup of power-based equations and the nearest-power
    calculation from a dir object"""
    dir_obj = EquationDirectory(GOOD_DIR, interp_extrap_power=False,
                                use_nearest_power=True)
    eqn = dir_obj['jacket::outfitting_11MW']
    truth = dir_obj['jacket::outfitting_10MW']
    assert eqn == truth

def test_interp_extrap_power():
    """Test interp and extrap functionality of power-based equations
    from __getitem__ on a dir object"""
    dir_obj = EquationDirectory(GOOD_DIR, interp_extrap_power=True,
                                use_nearest_power=True)
    eqn = dir_obj['jacket::outfitting_11MW']
    truth = ('((((outfitting_8MW(depth, outfitting_cost) '
             '- outfitting_10MW(depth, outfitting_cost)) * 1.0) / -2.0) '
             '+ outfitting_10MW(depth, outfitting_cost))')
    assert str(eqn) == truth

def test_eqn_dir_add():
    """Test the addition / merging of two EquationDirectory objects"""
    dir_obj = EquationDirectory(GOOD_DIR)
    dir1 = dir_obj['subdir']
    dir2 = dir_obj['subdir::subsubdir']
    dir3 = dir1 + dir2
    assert 'lattice_cost=50' in str(dir3['jacket::lattice'])
    assert 'outfitting_cost=10' in str(dir3['jacket::outfitting_8MW'])

def test_dir_math_retrieval():
    """Test the group and directory __getitem__ method with embedded math"""
    obj = EquationDirectory(GOOD_DIR, interp_extrap_power=False,
                            use_nearest_power=False)
    key1 = 'jacket::lattice'
    key2 = 'jacket::outfitting_8MW'
    key3 = 'jacket::transition_piece'
    key4 = '0.6'
    eqn1 = obj[key1]
    eqn2 = obj[key2]
    eqn3 = obj[key3]
    eqn4 = obj[key4]
    y1 = eqn1.eval(**{k: 2 for k in eqn1.variables})
    y2 = eqn2.eval(**{k: 2 for k in eqn2.variables})
    y3 = eqn3.eval(**{k: 2 for k in eqn3.variables})
    y4 = eqn4.eval()

    key_math = ''.join([key1, ' - ', key2, '+', key3])
    eqn_math = obj[key_math]
    y_math = eqn_math.eval(**{k: 2 for k in eqn_math.variables})
    assert y1 - y2 + y3 == y_math

    key_math = ''.join([key1, ' - ', key2, ' * ', key3])
    eqn_math = obj[key_math]
    y_math = eqn_math.eval(**{k: 2 for k in eqn_math.variables})
    assert y1 - y2 * y3 == y_math

    key_math = ''.join([key1, ' / ', key2, ' +', key3])
    eqn_math = obj[key_math]
    y_math = eqn_math.eval(**{k: 2 for k in eqn_math.variables})
    assert y1 / y2 + y3 == y_math

    key_math = ''.join([key1, ' / ', key2, ' ** ', key4])
    eqn_math = obj[key_math]
    y_math = eqn_math.eval(**{k: 2 for k in eqn_math.variables})
    assert y1 / y2 ** y4 == y_math

    key_math = ''.join([key1, ' * ', key2, ' ** ', key4])
    eqn_math = obj[key_math]
    y_math = eqn_math.eval(**{k: 2 for k in eqn_math.variables})
    assert y1 * y2 ** y4 == y_math

def test_bad_math_retrieval():
    """Test that attempting math in __getitem__ between an EquationGroup and
    an Equation raises a TypeError"""
    obj = EquationDirectory(GOOD_DIR, interp_extrap_power=False,
                            use_nearest_power=False)
    key1 = 'jacket'
    key2 = 'jacket::outfitting_8MW'

    key_math = ''.join([key1, ' - ', key2])
    with pytest.raises(TypeError):
        obj[key_math]

def test_dir_parenthesis_retrieval():
    """Test parenthetical math expression retrieval from directory object"""
    obj = EquationDirectory(GOOD_DIR, interp_extrap_power=False,
                            use_nearest_power=False)
    key1 = 'jacket::lattice'
    key2 = 'jacket::outfitting_8MW'
    key = '2 * ({} + {})'.format(key1, key2)
    eqn = obj[key]
    truth = ('(2 * (lattice(depth, lattice_cost, turbine_capacity) '
             '+ outfitting_8MW(depth, outfitting_cost)))')
    assert str(eqn) == truth
    truth = ('2 * (((np.exp(3.7136 + 0.00176 * turbine_capacity '
             '** 2.5 + 0.645 * np.log(depth))) * lattice_cost) '
             '+ ((40 + (0.8 * (18 + depth))) * outfitting_cost))')
    assert eqn.full == truth
    inputs = {k: np.ones(3) for k in eqn.variables}
    out = eqn.eval(**inputs)
    assert isinstance(out, np.ndarray)
    assert np.allclose(out, 192.54674167 * np.ones(3))
    eqn1 = obj[key1]
    eqn2 = obj[key2]
    out1 = eqn1.evaluate(**inputs)
    out2 = eqn2.evaluate(**inputs)
    assert np.allclose(out, 2 * (out1 + out2))
    assert ~np.allclose(out, 2 * out1 + out2)

def test_cost_reductions_directory():
    """Test interp/extrap/nearest on cost reduction year from the
    directory object."""

    obj = EquationDirectory(COST_REDUCTIONS_DIR, use_nearest_year=True)
    eqn1 = obj['cost_reductions::fixed::turbine_install_2030']
    eqn2 = obj['cost_reductions::fixed::turbine_install_2025']
    assert eqn1 == eqn2

    obj = EquationDirectory(COST_REDUCTIONS_DIR, interp_extrap_year=True,
                            use_nearest_year=True)
    eqn1 = obj['cost_reductions::fixed::turbine_install_2030']
    eqn2 = obj['cost_reductions::fixed::turbine_install_2020']
    eqn3 = obj['cost_reductions::fixed::turbine_install_2025']
    assert (eqn3.eval() - eqn2.eval()) + eqn3.eval() == eqn1.eval()

    eqn1 = obj['cost_reductions::fixed::turbine_install_2023']
    eqn2 = obj['cost_reductions::fixed::turbine_install_2020']
    eqn3 = obj['cost_reductions::fixed::turbine_install_2025']
    assert (3 / 5) * (eqn3.eval() - eqn2.eval()) + eqn2.eval() == eqn1.eval()

max_stars_repo_name: NREL/NRWAL

max_issues_repo_name: NREL/NRWAL

max_forks_repo_name: NREL/NRWAL


text:
import numpy as np
from def_get_mags import get_zdistmod, get_kcorrect2, aper_and_comov, abs2lum, lumdensity, abs_mag
import math
from def2clump import meanlum2, medlum2, get_error
import matplotlib.pyplot as plt
from def_mymath import halflight2

def get_ind_lums(newdata, bands, aperture):
	import numpy as np
	from def_get_mags import get_zdistmod, get_kcorrect2, aper_and_comov, abs2lum, lumdensity, abs_mag
	import math
	from defclump import meanlum2
	from my_def_plots import halflight_plot, scatter_fit
	from scipy import interpolate
	import matplotlib.pyplot as plt
	from def_mymath import halflight

	Naps=len(aperture)
	Ndat=len(newdata)
	try:
		redshifts=newdata['Z']
		DM= get_zdistmod(newdata, 'Z')
	except:
		redshifts=newdata['Z_2']
		DM= get_zdistmod(newdata, 'Z_2')
	kcorrect=get_kcorrect2(newdata,'mag_forced_cmodel', '_err', bands, '','hsc_filters.dat',redshifts)
	fig=plt.figure()
	bigLI=[]
	bigrad=[]
	bigden=[]
	bigLI10=[]
	bigrad10=[]
	bigden10=[]
	for n in range(0, Ndat):

		LI=[]
		LI10=[]
		LI2=[]
		LI210=[]
		lumdi=[]
		lumdi10=[]

		string=str(n)
		radkpc=aper_and_comov(aperture, redshifts[n])
		#print('redshifts is ', redshifts[n])

		for a in range(0, Naps):	#this goes through every aperture
			ns=str(a)
			#print('aperture0',ns)

			absg, absr, absi, absz, absy= abs_mag(newdata[n], 'mag_aperture0', kcorrect, DM[n], bands, ns, n)

			Lumg, Lumr, Lumi, Lumz, Lumy=abs2lum(absg, absr, absi, absz, absy)

			Lg, Lr, Li, Lz, Ly=lumdensity(Lumg, Lumr, Lumi, Lumz, Lumy, radkpc[a])

			logLumi=math.log10(Lumi)
			logLi=math.log10(Li)
			LI10.append(logLumi)
			lumdi10.append(logLi)

			LI.append(Lumi)
			lumdi.append(Li)
		#print('LI for ',n,' galaxy is ', LI)
		bigLI.append(LI)
		bigden.append(lumdi)
		bigLI10.append(LI10)
		bigden.append(lumdi10)

		lograd=[math.log10(radkpc[n]) for n in range(len(radkpc))]
		bigrad10.append(lograd)

		bigrad.append(radkpc)

	bigLIs=np.array(bigLI)
	bigLIs10=np.array(bigLI10)

	bigrads=np.array(bigrad)
	bigrads10=np.array(bigrad10)

	lumdensi=np.array(bigden)
	lumdensi10=np.array(bigden10)
	return bigLIs, bigrads, lumdensi,bigLIs10, bigrads10, lumdensi10

def get_avg_lums(bigLIs, bigrads, lumdensi, type=''):
	bigLIs.flatten()	#luminosity
	bigrads.flatten()	#radii
	lumdensi.flatten()	#luminosity density
	Naps=0.0

	if type=='mean':
		meanlum, radavg, bb=meanlum2(bigLIs, bigrads,Naps,scale='log')
		meandens, radavg, bb=meanlum2(lumdensi, bigrads,Naps,scale='log')

		err='bootstrap_stdv'
		lumdenerr=get_error(lumdensi, bigrads, bb, error=err)

		print('Mean Luminosity= ', meanlum)
		print('Mean LumDensity=', meandens)
		print('Binned Radii= ', radavg)
		print('Standard Deviation= ', lumdenerr)

		return meanlum, meandens, radavg, lumdenerr

	if type== 'median':
		medlum, radavg, bb=medlum2(bigLIs, bigrads)
		medens, radavg, bb=medlum2(lumdensi, bigrads)
		err='bootstrap_stdv'
		lumdenerr=get_error(lumdensi, bigrads, bb, error=err)

		print('Median Luminosity= ', medlum)
		print('Median LumDensity=', medens)
		print('Binned Radii= ', radavg)
		print('Standard Deviation= ', lumdenerr)

		return medlum, medens, radavg, lumdenerr

def get_halflight(bigLIs, bigrads):
	print('Getting Halfs')
	N=np.ndim(bigrads)
	if N == 2:
		print('Array is 2D')
		halfrad=[]
		for x in range(len(bigrads)):
			#print('Galaxy #', str(x))
			#print('Luminosity= ', bigLIs[x])
			#print('Radii= ', bigrads[x])
			rad=bigrads[x]
			lum=bigLIs[x]
			half=math.log10(10**np.max(lum)/2.0)
			fhalf=halflight(rad,lum)
			halfrad.append(fhalf)
		halfrads=np.array(halfrad)
	else:
		halfrads=halflight(bigrads,bigLIs)
	return halfrads

def get_slopes(lum, hx, x, y, error=None, names=None, smax=False):
	import scipy.stats as stats
	from def_mymath import my_linregress3
	from my_def_plots import scatter_fit, simple_hist

	mult=4
	Ndim=np.ndim(x)
	N=len(x)

	if error is None:
		print('No error was given')
		error=np.ones((N, len(x[0])))

	if names is None:
		print('No names given')
	if Ndim==2:
		xcut=[]
		ycut=[]
		errcut=[]
		for i in range(len(x)):
			xrow=x[i]
			yrow=y[i]
			errow=error[i]
			hhx=hx[i]

			#merr=errow[xrow>=hhx]
			#mx=xrow[xrow>=hhx]
			#my=yrow[xrow>=hhx]

			if smax== True:
				hhx10=10**hhx
				hhx2s=mult*hhx10
				hhx2=math.log10(hhx2s)

				bad=0
				#print(hhx2, np.max(xrow))
				if np.max(xrow) >= hhx2:
					mx=xrow[(xrow>=hhx)&(xrow<=hhx2)]
					my=yrow[(xrow>=hhx)&(xrow<=hhx2)]
					merr=errow[(xrow>=hhx)&(xrow<=hhx2)]
					if len(mx) >=4:
						xcut.append(mx)
						ycut.append(my)
						errcut.append(merr)
				else:
					print('Upper Cut is Out of the Radius Range')
					bad += 1
			else:
				merr=errow[xrow>=hhx]
				mx=xrow[xrow>=hhx]
				my=yrow[xrow>=hhx]
				if len(mx) >=4:
					xcut.append(mx)
					ycut.append(my)
					errcut.append(merr)
		slopes=[]
		intercepts=[]
		errs=[]
		for n in range(len(xcut)):
			#print(len(x[n]), len(xcut[n]))
			#slope, int, r_value, p_value, std_err = stats.linregress(xcut[n],ycut[n])
			slope, int, std_err=my_linregress3(xcut[n], ycut[n], errcut[n])
			slopes.append(slope)
			intercepts.append(int)
			errs.append(std_err)
		return slopes, intercepts, errs
	else: #for arrays of 1D *aka* the stacked profile
		x=np.array(x)
		print('r1/2 limit is ', hx)
		print('xrange for stacked is ', x)
		if error is None:
			error=np.ones(N)
		if smax== True:
			hx10=10**hx
			hx2s=mult*hx10
			hx2=math.log10(hx2s)
			print('upper limit is ', hx2)
			if np.max(x) <= hx2:
				print('Upper cut is out of the Radius range')
			else:
				xcut=x[(x>=hx)&(x<=hx2)]
				ycut=y[(x>=hx)&(x<=hx2)]
				errcut=error[(x>=hx)&(x<=hx2)]
		else:
			xcut=x[x>=hx]
			ycut=y[x>=hx]
			errcut=error[x>=hx]
		print('Radii are= ', xcut)
		print('R1/2 is= ', hx)

		sl3, C3, std_err3=my_linregress3(xcut, ycut, errcut)

		return sl3, C3, xcut, ycut, std_err3, errcut

def get_slopes1(data, lum, hx, x, y, error=None, names=None, smax=False):
	import scipy.stats as stats
	from def_mymath import my_linregress3
	from my_def_plots import scatter_fit, simple_hist
	get_vir_r= lambda M, ro, delta_c: (M/(4./3.*math.pi*rho*delta_c))**(1./3.)
	mult=5
	Ndim=np.ndim(x)
	N=len(x)
	if error is None:
		print('Is this going through?')
		error=np.ones((N, len(x[0])))
	if names is None:
		print('No names given')
	if Ndim==2:
		xcut=[]
		ycut=[]
		errcut=[]
		bad=0
		good=0
		for i in range(len(x)):
			xrow=x[i]
			yrow=y[i]
			Lum=lum[i]
			errow=error[i]
			hhx=hx[i]

			if smax is True:
				from astropy.cosmology import FlatLambdaCDM
				from astropy import units as u
				cosmo = FlatLambdaCDM(H0=100, Om0=0.3)
				redshift=data['Z']
				rhos=cosmo.critical_density(redshift[i]) #in g/cm^3
				solMkpc3= u.solMass / u.kpc**3
				rho=rhos.to(solMkpc3)
				print('Critical Density= ', rho)
				rho=rho.value
				M2L=10

				Lmax=np.max(Lum)

				Mvir=M2L*10**Lmax
				#Mvir=math.log10(Mvir)
				#Mvir=M2L*Lmax
				print('Virial Mass= ', Mvir)

				delta_c=500

				r_vir=get_vir_r(Mvir, rho, delta_c)
				r_vir=math.log10(r_vir)
				print('Radii= ', str(xrow))
				print('Virial Radius is ', str(r_vir))
				if np.max(xrow) >= r_vir:
					mx=xrow[(xrow>=hhx)&(xrow<=r_vir)]
					my=yrow[(xrow>=hhx)&(xrow<=r_vir)]
					merr=errow[(xrow>=hhx)&(xrow<=r_vir)]
					if len(mx) >=4:
						xcut.append(mx)
						ycut.append(my)
						errcut.append(merr)
						good +=1
					else:
						print('Not enough data')
				else:
					print('Upper Cut is Out of the Virial Radius Range')
					#bad=sum(bad+1)
					bad +=1
			elif smax is False:
				merr=errow[xrow>=hhx]
				mx=xrow[xrow>=hhx]
				my=yrow[xrow>=hhx]
				if len(mx) >=4:
					xcut.append(mx)
					ycut.append(my)
					errcut.append(merr)
				else:
					print('Not enough data')
		print('Number of good profiles: ', good)
		print('Number of bad profiles: ', bad)
		print(len(x), len(xcut))
		hi=hi
		slopes=[]
		intercepts=[]
		errs=[]
		for n in range(len(xcut)):
			slope, int, std_err=my_linregress3(xcut[n], ycut[n], errcut[n])
			slopes.append(slope)
			intercepts.append(int)
			errs.append(std_err)
		return slopes, intercepts, errs
	else:
		x=np.array(x)
		if error is None:
			error=np.ones(N)
		if smax is True:
			hx10=10**hx
			hx2s=mult*hx10
			hx2=math.log10(hx2s)

			if np.max(x) <= hx2:
				print('Upper cut is out of the Radius range')
			else:
				xcut=x[(x>=hx)&(x<=hx2)]
				ycut=y[(x>=hx)&(x<=hx2)]
				errcut=error[(x>=hx)&(x<=hx2)]
		else:
			xcut=x[x>=hx]
			ycut=y[x>=hx]
			errcut=error[x>=hx]

		sl3, C3, std_err3=my_linregress3(xcut, ycut, errcut)
		return sl3, C3, xcut, ycut, std_err3, errcut

max_stars_repo_path: not currently in use/def2clean.py

max_issues_repo_path: not currently in use/def2clean.py

max_forks_repo_path: not currently in use/def2clean.py

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


text:
import pickle
import utility as util
import itertools
from scipy.spatial.distance import cosine
import numpy as np
from scipy.special import betainc
from scipy.special import loggamma

def evaluate_model_selection(True_J, best_j, min_J, max_J, sample_names, result_path):
    out_all = open(result_path + "/tables/summary/model_selection.tsv", "w")
    out_all.write("Sample_name\tPredicted_J\n")
    summary_table = [0 for j in range(max_J-min_J+1)]
    for s,sample_name in enumerate(sample_names):
        out_all.write(sample_name + "\t" + str(best_j[s]) + "\n")
        summary_table[best_j[s]-min_J] += 1

    out = open(result_path + "/tables/summary/model_selection_summary.tsv", "w")
    out.write("Method")
    for j in range(min_J, max_J+1):
        out.write("\tJ=" + str(j))
    out.write("\nSigTracer")
    for j in range(max_J-min_J+1):
        out.write("\t" + str(summary_table[j]))
    out.write("\n")

def evaluate_accuracy(ABS_path, exp_name, J, best_r, ref_names, sample_names, purity, result_path):
    # Evaluate CCF accuracy
    gt_CCF = pickle.load(open(ABS_path + "data/" + exp_name + "/ground_truth.pkl", "rb"))["CCF"]
    out = open(result_path + "/tables/summary/delta_CCF.tsv", "w")
    out.write("Sample_name\tΔCCF\n")
    for s,sample_name in enumerate(sample_names):
        rho, CCF = util.load_BB(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        temp_delta_CCF = calc_delta_CCF(CCF, gt_CCF[s])
        out.write(sample_name + "\t" + str(temp_delta_CCF) + "\n")

    # Evaluate activity accuracy
    gt_activity = pickle.load(open(ABS_path + "data/" + exp_name + "/ground_truth.pkl",
                                   "rb"))["activity"]
    out = open(result_path + "/tables/summary/cos_activity.tsv", "w")
    out.write("Sample_name\tCosine_distance_of_activity\n")
    for s,sample_name in enumerate(sample_names):
        activity = util.load_activity(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        temp_cos = calc_cos_activity(activity, gt_activity[s])
        out.write(sample_name + "\t" + str(temp_cos) + "\n")

    # Evaluate Reconstruction-Rate for mutation-type
    out = open(result_path + "/tables/summary/RR_mut.tsv", "w")
    out.write("Sample_name\tRR_mutaiotn-type\n")
    for s, sample_name in enumerate(sample_names):
        qz = util.load_qz(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        mutations = util.load_mut_catalog(ABS_path, exp_name, sample_name)["trinucleotide"]
        signature = util.load_signature(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        temp_RR = calc_RR_mut(mutations, qz, signature, J)
        out.write(sample_name + "\t" + str(temp_RR) + "\n")

    # Evaluate Reconstruction-Rate for VAF
    out = open(result_path + "/tables/summary/RR_VAF.tsv", "w")
    out.write("Sample_name\tRR_VAF\n")
    for s, sample_name in enumerate(sample_names):
        mut_catalog = util.load_mut_catalog(ABS_path, exp_name, sample_name)
        pi = util.load_pi(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        rho,CCF = util.load_BB(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        qUC = util.load_qUC(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        qU = util.load_qU(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        temp_RR = calc_RR_VAF(mut_catalog["var_counts"], mut_catalog["ref_counts"],\
                              mut_catalog["total_cn"], purity[s], pi, rho, CCF, qUC,\
                              J, qU)
        out.write(sample_name + "\t" + str(temp_RR) + "\n")

    # Evaluate log-likelihood
    out = open(result_path + "/tables/summary/log_likelihood.tsv", "w")
    out.write("Sample_name\tlog_likelihood\n")
    for s, sample_name in enumerate(sample_names):
        mut_catalog = util.load_mut_catalog(ABS_path, exp_name, sample_name)
        qU = util.load_qU(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        qzU = util.load_qzU(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        qUz = convert_qzU_to_qUz(qzU)
        qUC = util.load_qUC(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        rho,CCF = util.load_BB(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        signature = util.load_signature(ABS_path, exp_name, sample_name, J, best_r[s], "pe")
        temp_LL = calc_log_likelihood(mut_catalog["var_counts"], mut_catalog["ref_counts"],\
                                      mut_catalog["total_cn"], mut_catalog["trinucleotide"],\
                                      mut_catalog["major_cn"], signature, purity[s], rho, CCF, qU, qUz, qUC, J)
        out.write(sample_name + "\t" + str(temp_LL) + "\n")

def calc_delta_CCF(predicted, correct):
    J = len(predicted)
    j_candidates = list(itertools.permutations(list(range(J)), J))
    total_distance = [0.0 for l in range(len(j_candidates))]
    for l, j_candidate in enumerate(j_candidates):
        for j, sorted_j in enumerate(j_candidate):
            total_distance[l] += abs(correct[j] - predicted[sorted_j]) / J
    min_distance = min(total_distance)
    return min_distance

def calc_cos_activity(predicted, correct):
    J = len(predicted)
    j_candidates = list(itertools.permutations(list(range(J)), J))
    total_distance = [0.0 for l in range(len(j_candidates))]
    for l, j_candidate in enumerate(j_candidates):
        for j, sorted_j in enumerate(j_candidate):
            total_distance[l] += cosine(correct[j], predicted[sorted_j]) / J
    min_distance = min(total_distance)
    return min_distance

def calc_RR_mut(mutations, qz, signature, J):
    N = len(mutations); K = len(signature); V = len(signature[0])
    mut = [0 for v in range(V)]
    for x in mutations: mut[x] += 1
    rec = [0 for v in range(V)]
    for n in range(N):
        temp = np.dot(qz[n], signature)
        for v in range(V): rec[v] += temp[v]
    RR = 0.0
    for v in range(V): RR += min(mut[v], rec[v])
    RR /= N

    return RR

def calc_RR_VAF(B, D, CN_tumor, purity, pi, rho, CCF, qUC, J, qU, div_num=30):
    N = len(B)
    M = [[[] for j in range(J)] for n in range(N)]
    for n in range(N):
        for j in range(J):
            for c in range(len(qUC[n][j])):
                M[n][j].append((1+c) * qUC[n][j][c])
    eta = [[[]for j in range(J)] for n in range(N)]
    for n in range(N):
        for j in range(J):
            for c in range(len(M[n][j])):
                eta[n][j].append((purity * M[n][j][c]) / ((purity * CN_tumor[n]) + ((1-purity) * 2.0)))

    left = [0 for div in range(div_num+1)]
    for div in range(1, div_num+1):
        left[div] = div * 1.0/div_num

    rec = [0.0 for div in range(div_num)]
    for n in range(N):
        for div in range(div_num):
            for j in range(J):
                temp = 0.0
                for c in range(len(eta[n][j])):
                    if(eta[n][j][c] != 0.0):
                        temp += qUC[n][j][c] * (betainc(rho[j]*CCF[j]*eta[n][j][c],rho[j]*(1.0-CCF[j]*eta[n][j][c]),left[div+1])\
                                - betainc(rho[j]*CCF[j]*eta[n][j][c], rho[j]*(1.0-CCF[j]*eta[n][j][c]), left[div]))
                rec[div] += temp

    mut = [0 for div in range(div_num)]
    for n in range(N):
        temp_VAF = float(B[n])/float(B[n] + D[n])
        temp_div = int(temp_VAF/(1.0/div_num))
        if(temp_div == div_num): temp_div = div_num-1
        mut[temp_div] += 1

    RR = 0.0
    for div in range(div_num): RR += min(mut[div], rec[div])
    RR /= N
    return RR

def convert_qzU_to_qUz(qzU):
    N = len(qzU); K = len(qzU[0]); J = len(qzU[0][0])
    qUz = [[[0.0 for k in range(K)] for j in range(J)] for n in range(N)]
    for n in range(N):
        for j in range(J):
            for k in range(K):
                qUz[n][j][k] = qzU[n][k][j]
    return qUz

def calc_log_likelihood(B, D, CN_tumor, mutations, CN_major, signature, purity, rho, CCF, qU, qUz, qUC, J):
    LL = 0.0
    N = len(B); K = len(signature)
    for n in range(N):
        for j in range(J):
            temp_sum = sum(qUz[n][j])
            if(temp_sum != 0.0):
                for k in range(K): qUz[n][j][k] /= temp_sum
            else:
                for k in range(K): qUz[n][j][k] = 1.0/K
            temp_sum = sum(qUC[n][j])
            if(temp_sum != 0.0):
                for c in range(len(qUC[n][j])): qUC[n][j][c] /= temp_sum
            else:
                for c in range(len(qUC[n][j])): qUC[n][j][c] = 1.0/len(qUC[n][j])

    for n in range(N):
        temp = 0.0
        for j in range(J):
            for k in range(K):
                if(signature[k][mutations[n]] != 0.0): temp += qU[n][j] * qUz[n][j][k] * np.log(signature[k][mutations[n]])
        LL += temp

    eta = [[[]for j in range(J)] for n in range(N)]
    for n in range(N):
        for j in range(J):
            for c in range(CN_major[n]):
                eta[n][j].append((purity * (1+c)) / ((purity * CN_tumor[n]) + ((1-purity) * 2.0)))

    for n in range(N):
        temp = 0.0
        for j in range(J):
            for c in range(len(eta[n][j])):
                x = BetaBinomial(B[n], B[n]+D[n], rho[j], CCF[j] * eta[n][j][c])
                temp += qU[n][j] * qUC[n][j][c] * x
        LL += temp
    return LL

def BetaBinomial(B, D, M, mu):
    log_likelihood = loggamma(D+1) - loggamma(B+1) - loggamma(D-B+1)
    log_likelihood += loggamma(M) - loggamma(D+M) + loggamma(B+M*mu) + loggamma(D-B+M*(1-mu))
    log_likelihood -= loggamma(M*mu) + loggamma(M*(1-mu))
    return log_likelihood

max_stars_repo_name: qkirikigaku/SigTracer

max_issues_repo_name: qkirikigaku/SigTracer

max_forks_repo_name: qkirikigaku/SigTracer

reason: import numpy,from scipy


text:
import tensorflow as tf
from model import Generator, Discriminator, ClassEmbedding
from dataset import CalligraphyDataset
from config import config
import os
import time
import matplotlib.pyplot as plt
import numpy as np

ds = CalligraphyDataset(data_dir='./data/character-1000',
                        character_csv='./data/label_character.csv',
                        batch_size=config.batch_size,
                        shuffle=True,
                        repeat=False
                        )

generator = Generator()
discriminator = Discriminator()
class_embed = ClassEmbedding()

loss_obj = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real, generated):
    real_loss = loss_obj(tf.ones_like(real), real)

    generated_loss = loss_obj(tf.zeros_like(generated), generated)

    total_disc_loss = real_loss + generated_loss

    return total_disc_loss * 0.5

def generator_loss(generated):
    return loss_obj(tf.ones_like(generated), generated)

discriminator_optimizer = tf.keras.optimizers.RMSprop(config.learning_rate_D)
generator_optimizer = tf.keras.optimizers.Adam(config.learning_rate_G, beta_1=0.5)
class_embed_optimizer = tf.keras.optimizers.Adam(config.learning_rate_G, beta_1=0.5)

checkpoint_prefix = os.path.join(config.checkpoint_dir, 'train')
checkpoint = tf.train.Checkpoint(
    discriminator_optimizer=discriminator_optimizer,
    generator_optimizer=generator_optimizer,
    class_embed_optimizer=class_embed_optimizer,
    generator=generator,
    discriminator=discriminator,
    class_embed=class_embed
)
manager = tf.train.CheckpointManager(checkpoint, config.checkpoint_dir, max_to_keep=10)

def train_step(images, labels):
    noise = tf.random.uniform([len(labels), 1, 1, config.noise_dim], minval=-1., maxval=1.)

    with tf.GradientTape(persistent=True) as tape:
        labels_embed = class_embed(labels)
        generated_images = generator(noise, labels_embed, training=True)

        real_logits = discriminator(images, labels_embed, training=True)
        fake_logits = discriminator(generated_images, labels_embed, training=True)

        gen_loss = generator_loss(fake_logits)
        disc_loss = discriminator_loss(real_logits, fake_logits)

    gen_gradients = tape.gradient(gen_loss, generator.trainable_variables)
    dis_gradients = tape.gradient(disc_loss, discriminator.trainable_variables)
    embed_gradients = tape.gradient(disc_loss, class_embed.trainable_variables)

    generator_optimizer.apply_gradients(zip(gen_gradients, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(dis_gradients, discriminator.trainable_variables))
    class_embed_optimizer.apply_gradients(zip(embed_gradients, class_embed.trainable_variables))

    return gen_loss, disc_loss

# keeping the random vector constant for generation (prediction) so
# it will be easier to see the improvement of the gan.
# To visualize progress in the animated GIF
const_random_vector_for_saving = tf.random.uniform([config.num_examples_to_generate, 1, 1, config.noise_dim],
                                                   minval=-1., maxval=1.)

def print_or_save_sample_images(sample_images, max_print_size=config.num_examples_to_generate,
                                is_square=False, is_save=False, epoch=None,
                                example_dir=None):
    available_print_size = list(range(1, 101))
    assert max_print_size in available_print_size
    if len(sample_images.shape) == 2:
        size = int(np.sqrt(sample_images.shape[1]))
    elif len(sample_images.shape) > 2:
        size = sample_images.shape[1]
        channel = sample_images.shape[3]
    else:
        ValueError('Not valid a shape of sample_images')

    if not is_square:
        print_images = sample_images[:max_print_size, ...]
        print_images = print_images.reshape([max_print_size, size, size, channel])
        print_images = print_images.swapaxes(0, 1)
        print_images = print_images.reshape([size, max_print_size * size, channel])
        if channel == 1:
            print_images = np.squeeze(print_images, axis=-1)

        fig = plt.figure(figsize=(max_print_size, 1))
        plt.imshow(print_images * 0.5 + 0.5)  # , cmap='gray')
        plt.axis('off')

    else:
        num_columns = int(np.sqrt(max_print_size))
        max_print_size = int(num_columns ** 2)
        print_images = sample_images[:max_print_size, ...]
        print_images = print_images.reshape([max_print_size, size, size, channel])
        print_images = print_images.swapaxes(0, 1)
        print_images = print_images.reshape([size, max_print_size * size, channel])
        print_images = [print_images[:, i * size * num_columns:(i + 1) * size * num_columns] for i in
                        range(num_columns)]
        print_images = np.concatenate(tuple(print_images), axis=0)
        if channel == 1:
            print_images = np.squeeze(print_images, axis=-1)

        fig = plt.figure(figsize=(num_columns, num_columns))
        plt.subplots_adjust(left=0.0, right=1.0, top=1.0, bottom=0.0)
        plt.imshow(print_images * 0.5 + 0.5, cmap='gray')
        plt.axis('off')

    if is_save and epoch is not None:
        filepath = os.path.join(example_dir, 'image_at_epoch_{:04d}.png'.format(epoch))
        plt.savefig(filepath, dpi=300)
    else:
        plt.show()

def train():
    global_step = tf.Variable(0, trainable=False)

    sample_condition = tf.eye(config.class_dim)
    sample_condition = tf.reshape(sample_condition, [-1, 1, 1, config.class_dim])
    sample_condition = sample_condition[:config.num_examples_to_generate]

    for epoch in range(config.max_epochs):
        start_time = time.time()
        for step, (images, labels) in enumerate(ds.dataset):
            labels = np.array([ds.characters[item.numpy().decode('utf-8')] for item in labels])
            labels = tf.one_hot(labels, depth=config.class_dim)
            labels = tf.reshape(labels, shape=[-1, 1, 1, config.class_dim])
            labels = tf.cast(labels, dtype=tf.float32)

            gen_loss, disc_loss = train_step(images, labels)
            global_step.assign_add(1)

            if global_step.numpy() & config.print_steps == 0:
                duration = time.time() - start_time
                examples_per_sec = config.batch_size / float(duration)

                print(
                    "Epochs: {:.2f} global_step: {} loss_D: {:.3g} loss_G: {:.3g} ({:.2f} examples/sec; {:.3f} sec/batch)".format(
                        epoch, global_step.numpy(), disc_loss, gen_loss, examples_per_sec, duration))

        if (epoch + 1) % config.save_images_epochs == 0:
            condition_embed = class_embed(sample_condition, training=False)
            sample_images = generator(const_random_vector_for_saving, condition_embed, training=False)
            print_or_save_sample_images(sample_images.numpy(), config.num_examples_to_generate,
                                        is_square=True, is_save=True, epoch=epoch + 1,
                                        example_dir=config.example_dir)
            print("This images are saved at {} epoch".format(epoch + 1))

        if (epoch + 1) % config.save_model_epochs == 0:
            ckpt_save_path = manager.save()
            print('Saving checkpoint for epoch {} at {}'.format(epoch + 1, ckpt_save_path))

        print('Time taken for epoch {} is {} sec\n'.format(epoch + 1, time.time() - start_time))

train()

max_stars_repo_name: zhuojg/calligraphy-gan

max_issues_repo_name: zhuojg/calligraphy-gan

max_forks_repo_name: zhuojg/calligraphy-gan


text:
import wave
from math import ceil
import numpy as np

byte_depth_to_dtype = {1: np.uint8, 2: np.uint16, 4: np.uint32, 8: np.uint64}

sound = wave.open('cosmin.rusu@epfl.ch.wav', "r")

num_channels = sound.getnchannels()
sample_width = sound.getsampwidth()
num_frames = sound.getnframes()
sound_frames = sound.readframes(num_frames)

bytes_to_recover = 2500000

def roundup(x, base=1):
    return int(ceil(x / base)) * base

def lsb_deinterleave_bytes(carrier, num_bits, num_lsb, byte_depth=1):
    plen = roundup(num_bits / num_lsb)
    carrier_dtype = byte_depth_to_dtype[byte_depth]
    payload_bits = np.unpackbits(
        np.frombuffer(carrier, dtype=carrier_dtype, count=plen).view(np.uint8)
    ).reshape(plen, 8 * byte_depth)[:, 8 - num_lsb:8]
    return np.packbits(payload_bits).tobytes()[:num_bits // 8]

data = lsb_deinterleave_bytes(sound_frames, 8 * bytes_to_recover, 1,
                              byte_depth=sample_width)

data = bytes(data)
i = data.find(bytes("COM402", 'utf-8'))

print(data[i:i+52])


text:
from ltlf2dfa.parser.ltlf import LTLfParser
from ltlf2dfa.base import MonaProgram
from ltlf2dfa.ltlf2dfa import invoke_mona, createMonafile

import networkx as nx

import re

# Dump a value from a file based on a regex passed in.
def get_value(text, regex, value_type=str):
    pattern = re.compile(regex, re.MULTILINE)
    results = pattern.search(text)
    if results:
        return value_type(results.group(1))
    else:
        print("Could not find the value {}, in the text provided".format(regex))
        return value_type(0.0)

class LTLf2nxParser:

    def __init__(self):
        self.parser = LTLfParser()
        self.formula = None

    def parse_formula(self, formula_str):
        self.formula = self.parser(formula_str)

    def to_dot(self):
        if self.formula is None:
            print('<LTLf2ndParser.to_dot()> No formula parsed. Please parse a formula first using parse_formula(str)!')
            return None

        return self.formula.to_dfa()

    def to_mona_output(self):
        if self.formula is None:
            print('<LTLf2ndParser.to_dot()> No formula parsed. Please parse a formula first using parse_formula(str)!')
            return None

        mona_p_string = MonaProgram(self.formula).mona_program()
        createMonafile(mona_p_string)
        mona = invoke_mona()
        return mona

    def to_nxgraph(self, name= 'MONA_DFA'):
        if self.formula is None:
            print('<LTLf2ndParser.to_dot()> No formula parsed. Please parse a formula first using parse_formula(str)!')
            return None

        mona_output = self.to_mona_output()
        g = nx.DiGraph()
        g.graph['name'] = name

        # if formula is unsatisfiable
        if mona_output == "Formula is unsatisfiable":
            print('Formula is unsatisfiable, DFA not constructed!')
            return None

        # atomic propositions
        variables = get_value(mona_output, r'.*DFA for formula with free variables:[\s]*(.*?)\n.*', str)
        g.graph['ap'] = variables.split()

        # accepting states
        accepting_states = get_value(mona_output, r".*Accepting states:[\s]*(.*?)\n.*", str)
        accepting_states = [
            str(x.strip()) for x in accepting_states.split() if len(x.strip()) > 0
        ]

        g.graph['acc'] = accepting_states

        # edges and states
        for line in mona_output.splitlines():
            if line.startswith("State "):

                orig_state = get_value(line, r".*State[\s]*(\d+):\s.*", str)
                dest_state = get_value(line, r".*state[\s]*(\d+)[\s]*.*", str)
                guard = get_value(line, r".*:[\s](.*?)[\s]->.*", str)
                if g.has_edge(orig_state, dest_state):
                    g.edges[orig_state, dest_state]['guard'].append(guard)
                else:
                    g.add_edge(orig_state, dest_state, guard=[guard])

        # remove the don't care state 0
        assert g.has_edge('0', '1')
        assert len(list(g.successors('0'))) == 1
        assert len(g.edges['0', '1']['guard']) == 1
        assert all(c == 'X' for c in g.edges['0', '1']['guard'][0])
        g.remove_node('0')

        # initial state
        g.graph['init'] = '1'

        return g

reason: import networkx