text:
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn.init import kaiming_normal_, constant_
import math
import numpy as np

from .util import conv, predict_flow, deconv, crop_like, correlate

"Parameter count , 39,175,298 "

class FlowNetC(nn.Module):
    expansion = 1

    def __init__(self, batchNorm=True):
        super(FlowNetC, self).__init__()

        self.batchNorm = batchNorm
        self.conv1 = conv(self.batchNorm, 1, 64, kernel_size=7, stride=2)
        self.conv2 = conv(self.batchNorm, 64, 128, kernel_size=5, stride=2)
        self.conv3 = conv(self.batchNorm, 128, 256, kernel_size=5, stride=2)
        self.conv_redir = conv(self.batchNorm, 256, 32, kernel_size=1, stride=1)

        self.conv3_1 = conv(self.batchNorm, 473, 256)
        self.conv4 = conv(self.batchNorm, 256, 512, stride=2)
        self.conv4_1 = conv(self.batchNorm, 512, 512)
        self.conv5 = conv(self.batchNorm, 512, 512, stride=2)
        self.conv5_1 = conv(self.batchNorm, 512, 512)
        self.conv6 = conv(self.batchNorm, 512, 1024, stride=2)
        self.conv6_1 = conv(self.batchNorm, 1024, 1024)

        self.deconv5 = deconv(1024, 512)
        self.deconv4 = deconv(1026, 256)
        self.deconv3 = deconv(770, 128)
        self.deconv2 = deconv(386, 64)

        self.predict_flow6 = predict_flow(1024)
        self.predict_flow5 = predict_flow(1026)
        self.predict_flow4 = predict_flow(770)
        self.predict_flow3 = predict_flow(386)
        self.predict_flow2 = predict_flow(194)

        self.upsampled_flow6_to_5 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow5_to_4 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow4_to_3 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow3_to_2 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)

        for m in self.modules():
            if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
                kaiming_normal_(m.weight, 0.1)
                if m.bias is not None:
                    constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                constant_(m.weight, 1)
                constant_(m.bias, 0)

    def forward(self, x):
        x1 = x[:, :1]
        x2 = x[:, 1:]

        out_conv1a = self.conv1(x1)
        out_conv2a = self.conv2(out_conv1a)
        out_conv3a = self.conv3(out_conv2a)

        out_conv1b = self.conv1(x2)
        out_conv2b = self.conv2(out_conv1b)
        out_conv3b = self.conv3(out_conv2b)

        out_conv_redir = self.conv_redir(out_conv3a)
        out_correlation = correlate(out_conv3a, out_conv3b)

        in_conv3_1 = torch.cat([out_conv_redir, out_correlation], dim=1)

        out_conv3 = self.conv3_1(in_conv3_1)
        out_conv4 = self.conv4_1(self.conv4(out_conv3))
        out_conv5 = self.conv5_1(self.conv5(out_conv4))
        out_conv6 = self.conv6_1(self.conv6(out_conv5))

        flow6 = self.predict_flow6(out_conv6)
        flow6_up = crop_like(self.upsampled_flow6_to_5(flow6), out_conv5)
        out_deconv5 = crop_like(self.deconv5(out_conv6), out_conv5)

        concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)
        flow5 = self.predict_flow5(concat5)
        flow5_up = crop_like(self.upsampled_flow5_to_4(flow5), out_conv4)
        out_deconv4 = crop_like(self.deconv4(concat5), out_conv4)

        concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)
        flow4 = self.predict_flow4(concat4)
        flow4_up = crop_like(self.upsampled_flow4_to_3(flow4), out_conv3)
        out_deconv3 = crop_like(self.deconv3(concat4), out_conv3)

        concat3 = torch.cat((out_conv3, out_deconv3, flow4_up), 1)
        flow3 = self.predict_flow3(concat3)
        flow3_up = crop_like(self.upsampled_flow3_to_2(flow3), out_conv2a)
        out_deconv2 = crop_like(self.deconv2(concat3), out_conv2a)

        concat2 = torch.cat((out_conv2a, out_deconv2, flow3_up), 1)
        flow2 = self.predict_flow2(concat2)

        if self.training:
            return flow2, concat2, out_conv1a, flow3, flow4, flow5, flow6
        else:
            return flow2, concat2, out_conv1a, flow3, flow4, flow5, flow6

    def weight_parameters(self):
        return [param for name, param in self.named_parameters() if "weight" in name]

    def bias_parameters(self):
        return [param for name, param in self.named_parameters() if "bias" in name]

class FlowNetC2(nn.Module):
    def __init__(self):
        super().__init__()
        self.flownetc = FlowNetC()
        self.upsampled_flow2_to_1 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
        self.upsampled_flow1_to_0 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
        self.predict_flow1 = predict_flow(130)
        self.predict_flow = predict_flow(66)
        self.deconv1 = deconv(194, 64)
        self.deconv = deconv(130, 64)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        flow2, concat2, out_conv1a, flow3, flow4, flow5, flow6 = self.flownetc(x)
        flow2_up = self.upsampled_flow2_to_1(flow2)
        out_deconv1 = self.deconv1(concat2)
        concat1 = torch.cat((out_conv1a, out_deconv1, flow2_up), 1)

        flow1 = self.predict_flow1(concat1)

        flow_up = self.upsampled_flow1_to_0(flow1)
        out_deconv = self.deconv(concat1)

        concat0 = torch.cat((out_deconv, flow_up), 1)
        flow = self.predict_flow(concat0)
        # flow = self.sigmoid(flow)

        return flow, flow1, flow2, flow3, flow4, flow5, flow6

class FlowNetCA(nn.Module):
    expansion = 1

    def __init__(self, batchNorm=True):
        super().__init__()

        self.batchNorm = batchNorm
        self.conv1 = conv(self.batchNorm, 1, 64, kernel_size=7, stride=2)
        self.conv2 = conv(self.batchNorm, 64, 128, kernel_size=5, stride=2)
        self.conv3 = conv(self.batchNorm, 128, 256, kernel_size=5, stride=2)
        self.conv_redir = conv(self.batchNorm, 256, 32, kernel_size=1, stride=1)

        self.conv3_1 = conv(self.batchNorm, 473, 256)
        self.conv4 = conv(self.batchNorm, 256, 512, stride=2)
        self.conv4_1 = conv(self.batchNorm, 512, 512)
        self.conv5 = conv(self.batchNorm, 512, 512, stride=2)
        self.conv5_1 = conv(self.batchNorm, 512, 512)
        self.conv6 = conv(self.batchNorm, 512, 1024, stride=2)
        self.conv6_1 = conv(self.batchNorm, 1024, 1024)

        self.deconv5 = deconv(1024, 512)
        self.deconv4 = deconv(1026, 256)
        self.deconv3 = deconv(770, 128)
        self.deconv2 = deconv(386, 64)

        self.predict_flow6 = predict_flow(1024)
        self.predict_flow5 = predict_flow(1026)
        self.predict_flow4 = predict_flow(770)
        self.predict_flow3 = predict_flow(386)
        self.predict_flow2 = predict_flow(194)

        self.upsampled_flow6_to_5 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow5_to_4 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow4_to_3 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)
        self.upsampled_flow3_to_2 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=False)

        self.upsampled_flow2_to_1 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
        self.upsampled_flow1_to_0 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
        self.predict_flow1 = predict_flow(130)
        self.predict_flow = predict_flow(66)
        self.deconv1 = deconv(194, 64)
        self.deconv = deconv(130, 64)
        self.sigmoid = nn.Sigmoid()

        for m in self.modules():
            if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
                kaiming_normal_(m.weight, 0.1)
                if m.bias is not None:
                    constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                constant_(m.weight, 1)
                constant_(m.bias, 0)

    def forward(self, x):
        x1 = x[:, :1]
        x2 = x[:, 1:]

        out_conv1a = self.conv1(x1)
        out_conv2a = self.conv2(out_conv1a)
        out_conv3a = self.conv3(out_conv2a)

        out_conv1b = self.conv1(x2)
        out_conv2b = self.conv2(out_conv1b)
        out_conv3b = self.conv3(out_conv2b)

        out_conv1 = torch.cat([out_conv1a, out_conv1b])
        out_conv2 = torch.cat([out_conv2a, out_conv2b])
        out_conv3 = torch.cat([out_conv3a, out_conv3b])

        out_conv_redir = self.conv_redir(out_conv3a)
        out_correlation = correlate(out_conv3a, out_conv3b)

        in_conv3_1 = torch.cat([out_conv_redir, out_correlation], dim=1)

        out_conv3 = self.conv3_1(in_conv3_1)
        out_conv4 = self.conv4_1(self.conv4(out_conv3))
        out_conv5 = self.conv5_1(self.conv5(out_conv4))
        out_conv6 = self.conv6_1(self.conv6(out_conv5))

        flow6 = self.predict_flow6(out_conv6)
        flow6_up = crop_like(self.upsampled_flow6_to_5(flow6), out_conv5)
        out_deconv5 = crop_like(self.deconv5(out_conv6), out_conv5)

        concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)
        flow5 = self.predict_flow5(concat5)
        flow5_up = crop_like(self.upsampled_flow5_to_4(flow5), out_conv4)
        out_deconv4 = crop_like(self.deconv4(concat5), out_conv4)

        concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)
        flow4 = self.predict_flow4(concat4)
        flow4_up = crop_like(self.upsampled_flow4_to_3(flow4), out_conv3)
        out_deconv3 = crop_like(self.deconv3(concat4), out_conv3)

        concat3 = torch.cat((out_conv3, out_deconv3, flow4_up), 1)
        flow3 = self.predict_flow3(concat3)
        flow3_up = crop_like(self.upsampled_flow3_to_2(flow3), out_conv2a)
        out_deconv2 = crop_like(self.deconv2(concat3), out_conv2)

        concat2 = torch.cat((out_conv2, out_deconv2, flow3_up), 1)
        flow2 = self.predict_flow2(concat2)

        flow2_up = self.upsampled_flow2_to_1(flow2)
        out_deconv1 = crop_like(self.deconv1(concat2), out_conv1)
        concat1 = torch.cat((out_conv1, out_deconv1, flow2_up), 1)

        flow1 = self.predict_flow1(concat1)

        flow_up = self.upsampled_flow1_to_0(flow1)
        out_deconv = self.deconv(concat1)

        concat0 = torch.cat((out_deconv, flow_up), 1)
        flow = self.predict_flow(concat0)

        return flow, flow1, flow2, flow3, flow4, flow5, flow6

    def weight_parameters(self):
        return [param for name, param in self.named_parameters() if "weight" in name]

    def bias_parameters(self):
        return [param for name, param in self.named_parameters() if "bias" in name]

if __name__ == "__main__":
    import argparse

    args = argparse.ArgumentParser()
    args = args.parse_args()
    args.fp16 = False

    model = FlowNetC2(args)
    x = torch.randn(10, 2, 256, 256)
    pred = model(x)
# class FlowNetC(nn.Module):
#     def __init__(self, args, batchNorm=True, div_flow=20):
#         super(FlowNetC, self).__init__()

#         self.batchNorm = batchNorm
#         self.div_flow = div_flow

#         self.conv1 = conv(self.batchNorm, 1, 64, kernel_size=7, stride=2)
#         self.conv2 = conv(self.batchNorm, 64, 128, kernel_size=5, stride=2)
#         self.conv3 = conv(self.batchNorm, 128, 256, kernel_size=5, stride=2)
#         self.conv_redir = conv(self.batchNorm, 256, 32, kernel_size=1, stride=1)

#         if args.fp16:
#             self.corr = nn.Sequential(
#                 tofp32(),
#                 Correlation(
#                     pad_size=20,
#                     kernel_size=1,
#                     max_displacement=20,
#                     stride1=1,
#                     stride2=2,
#                     corr_multiply=1,
#                 ),
#                 tofp16(),
#             )
#         else:
#             self.corr = Correlation(
#                 pad_size=20,
#                 kernel_size=1,
#                 max_displacement=20,
#                 stride1=1,
#                 stride2=2,
#                 corr_multiply=1,
#             )

#         # self.corr_activation = nn.LeakyReLU(0.1, inplace=True)
#         self.corr_activation = nn.ReLU(inplace=True)
#         self.conv3_1 = conv(self.batchNorm, 473, 256)
#         self.conv4 = conv(self.batchNorm, 256, 512, stride=2)
#         self.conv4_1 = conv(self.batchNorm, 512, 512)
#         self.conv5 = conv(self.batchNorm, 512, 512, stride=2)
#         self.conv5_1 = conv(self.batchNorm, 512, 512)
#         self.conv6 = conv(self.batchNorm, 512, 1024, stride=2)
#         self.conv6_1 = conv(self.batchNorm, 1024, 1024)

#         self.deconv5 = deconv(1024, 512)
#         self.deconv4 = deconv(1026, 256)
#         self.deconv3 = deconv(770, 128)
#         self.deconv2 = deconv(386, 64)

#         self.predict_flow6 = predict_flow(1024)
#         self.predict_flow5 = predict_flow(1026)
#         self.predict_flow4 = predict_flow(770)
#         self.predict_flow3 = predict_flow(386)
#         self.predict_flow2 = predict_flow(194)

#         self.upsampled_flow6_to_5 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
#         self.upsampled_flow5_to_4 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
#         self.upsampled_flow4_to_3 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)
#         self.upsampled_flow3_to_2 = nn.ConvTranspose2d(2, 2, 4, 2, 1, bias=True)

#         for m in self.modules():
#             if isinstance(m, nn.Conv2d):
#                 if m.bias is not None:
#                     init.uniform_(m.bias)
#                 init.xavier_uniform_(m.weight)

#             if isinstance(m, nn.ConvTranspose2d):
#                 if m.bias is not None:
#                     init.uniform_(m.bias)
#                 init.xavier_uniform_(m.weight)
#                 # init_deconv_bilinear(m.weight)
#         self.upsample1 = nn.Upsample(scale_factor=4, mode="bilinear")

#     def forward(self, x):
#         x1 = x[:, 0:1, :, :]
#         x2 = x[:, 1::, :, :]

#         out_conv1a = self.conv1(x1)
#         out_conv2a = self.conv2(out_conv1a)
#         out_conv3a = self.conv3(out_conv2a)

#         # FlownetC bottom input stream
#         out_conv1b = self.conv1(x2)
#         out_conv2b = self.conv2(out_conv1b)
#         out_conv3b = self.conv3(out_conv2b)

#         # Merge streams
#         out_corr = self.corr(out_conv3a, out_conv3b)  # False
#         out_corr = self.corr_activation(out_corr)

#         # Redirect top input stream and concatenate
#         out_conv_redir = self.conv_redir(out_conv3a)

#         in_conv3_1 = torch.cat((out_conv_redir, out_corr), 1)

#         # Merged conv layers
#         out_conv3_1 = self.conv3_1(in_conv3_1)

#         out_conv4 = self.conv4_1(self.conv4(out_conv3_1))

#         out_conv5 = self.conv5_1(self.conv5(out_conv4))
#         out_conv6 = self.conv6_1(self.conv6(out_conv5))

#         flow6 = self.predict_flow6(out_conv6)
#         flow6_up = self.upsampled_flow6_to_5(flow6)
#         out_deconv5 = self.deconv5(out_conv6)

#         concat5 = torch.cat((out_conv5, out_deconv5, flow6_up), 1)

#         flow5 = self.predict_flow5(concat5)
#         flow5_up = self.upsampled_flow5_to_4(flow5)
#         out_deconv4 = self.deconv4(concat5)
#         concat4 = torch.cat((out_conv4, out_deconv4, flow5_up), 1)

#         flow4 = self.predict_flow4(concat4)
#         flow4_up = self.upsampled_flow4_to_3(flow4)
#         out_deconv3 = self.deconv3(concat4)
#         concat3 = torch.cat((out_conv3_1, out_deconv3, flow4_up), 1)

#         flow3 = self.predict_flow3(concat3)
#         flow3_up = self.upsampled_flow3_to_2(flow3)
#         out_deconv2 = self.deconv2(concat3)
#         concat2 = torch.cat((out_conv2a, out_deconv2, flow3_up), 1)

#         flow2 = self.predict_flow2(concat2)

#         return flow2, concat2, out_conv1a, flow3, flow4, flow5, flow6


text:
#!/usr/bin/python3
# preparedicts.py - Prepares translate to dictionary to use as:
# translate_to[language][ data["sources"][id] ] instead of data["sources"][id]

import numpy as np
import pandas as pd
from re import finditer
from googletrans import Translator
languages = ['english', 'spanish', 'chinese']

def get_set(dict_values):
    '''
    Creates first a set from lists of names (causes, diseases, risks) and returns a list of this set.
    e.g. get_set(data['major_causes'].values())
    '''
    s = set()
    for names_list in dict_values:
        for name in names_list:
            s.add(name)
    return list(s)

def camel_case_split(identifier):
    '''
    Source: https://stackoverflow.com/questions/29916065/how-to-do-camelcase-split-in-python
    '''
    matches = finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
    return [m.group(0) for m in matches]

def get_description(field_name):
    '''
    Returns description from JavaScript field names,
    e.g.
    get_description('mainTitle')
    returns 'Main title'
    '''
    field_name_parts = camel_case_split(field_name)
    field_name_parts = list(map(lambda x: x.lower() , field_name_parts))
    field_name_parts[0] = field_name_parts[0].capitalize()
    return ' '.join(field_name_parts)

def prepare_translations_csv():
    '''
    Creates template for translations: english_target.csv
    '''
    data = np.load('../data/data.npy', allow_pickle='TRUE').item()

    descriptions_list = [] # descriptions from keys names
    english_list = [] # list of english words and sentences

    # prepare words for translations of 'app', 'sources', 'ftrs', 'compare_with'
    all_field_names = list(data['app'].keys())
    descriptions_list += list(map(lambda x: get_description(x) , all_field_names))
    english_list += list(data['app'].values())

    all_ftrs = list(data['sources'].values())
    descriptions_list += ['Sources of Fatality Rates'] * len(all_ftrs)
    english_list += all_ftrs

    all_compare_options = list(data['compare_with'].values())
    descriptions_list += ['Option to compare with'] * len(all_compare_options)
    english_list += all_compare_options

    # preprare words for translations of causes, diseases, risk factors
    all_causes = get_set(data['major_causes'].values())
    descriptions_list += ['Cause of death, e.g. homicide'] * len(all_causes)
    english_list += all_causes

    all_diseases = get_set(data['disease_names'].values())
    descriptions_list += ['Cause of years of life lost, e.g. cancer'] * len(all_diseases)
    english_list += all_diseases

    all_risks = get_set(data['risk_factors'].values())
    descriptions_list += ['Risk factor, e.g. smoking'] * len(all_risks)
    english_list += all_risks

    english_english_app = pd.DataFrame.from_dict({
                            'english': english_list,
                            'target': english_list, # target language goes here
                            'description': descriptions_list})
    print('Saving english words for translations: ')
    print(str(english_english_app.head()))
    print('\n')
    english_english_app.to_csv('../data/translations/english_english_app.csv', index = False)

def translate():
    translator = Translator()

    for language in ['spanish', 'chinese']:
        english_target = pd.read_csv('../data/translations/english_english_app.csv')

        # rename column to target language
        english_target.rename(columns={'target': language}, inplace=True)

        # translate
        dummy_tranlations = True
        for i in range(english_target.shape[0]):
            words = english_target.iloc[0]['english']
            if dummy_tranlations: # just write english words to target language columns
                english_target.iloc[0][language] = words
            else: # use google translate api
                if language == 'chinese':
                    translation = translator.translate(words, src='en', dest='zh-CN')
                    english_target.iloc[0][language] = translation.text
                else: # spanish
                    translation = translator.translate(words, src='en', dest='es')
                    english_target.iloc[0][language] = translation.text

        english_target.to_csv('../data/translations/english_' + language + '_app.csv', index = False)

prepare_translations_csv()
translate()

ext: py

max_stars_repo_name: markolalovic/covid-calc

max_issues_repo_name: markolalovic/covid-calc

max_forks_repo_name: markolalovic/covid-calc

reason: import numpy


text:
import numpy as np
import argparse, sys
def read_csv(anno_file, conf_thresh=0.99):
    annos = dict()
    num = 0
    with open(anno_file) as f:
        for line in f.readlines():
            items = line.split(',')
            if items[0][-3:] == 'jpg':
                bboxes = []
                for bbox_item in items[1].strip().split(';'):
                    bbox = []
                    if bbox_item == '':
                        continue
                    info = bbox_item.split('_')
                    if len(info) == 5:
                        if float(info[-1]) < conf_thresh:
                            continue
                    for i in range(4):
                        bbox.append(float(info[i]))
                    bboxes.append(bbox)
                    num += 1
                annos[items[0]] = bboxes
    return annos, num

def cal_iou(bbox1, bbox2): # x,y,w,h
    if ((abs(bbox1[0] - bbox2[0]) < ((bbox1[2] + bbox2[2]) / 2.0)) and (
            abs(bbox1[1] - bbox2[1]) < ((bbox1[3] + bbox2[3]) / 2.0))):
        lu_x_inter = max((bbox1[0] - (bbox1[2] / 2.0)), (bbox2[0] - (bbox2[2] / 2.0)))
        lu_y_inter = min((bbox1[1] + (bbox1[3] / 2.0)), (bbox2[1] + (bbox2[3] / 2.0)))

        rd_x_inter = min((bbox1[0] + (bbox1[2] / 2.0)), (bbox2[0] + (bbox2[2] / 2.0)))
        rd_y_inter = max((bbox1[1] - (bbox1[3] / 2.0)), (bbox2[1] - (bbox2[3] / 2.0)))

        inter_w = abs(rd_x_inter - lu_x_inter)
        inter_h = abs(lu_y_inter - rd_y_inter)

        inter_square = inter_w * inter_h
        union_square = (bbox1[2] * bbox1[3]) + (bbox2[2] * bbox2[3]) - inter_square

        calcIOU = inter_square / union_square * 1.0
    else:
        calcIOU = 0.0

    return calcIOU

def eval_det(gt_info, det_info, iou_thresh = 0.7, if_write_res = False):
    wrong_dets = dict()
    missing_dets = dict()
    right_num = 0
    for img in det_info.keys():
        assert img in gt_info.keys(), '{} is not annotated'.format(img)
        for det_bbox in det_info[img]:
            flag_wrong = True
            for gt_bbox in gt_info[img]:
                if cal_iou(gt_bbox, det_bbox) >= iou_thresh:
                    flag_wrong = False
                    right_num += 1
                    break
            if flag_wrong:
                if img not in wrong_dets.keys():
                    wrong_dets[img] = [det_bbox]
                else:
                    wrong_dets[img].append(det_bbox)
    if if_write_res:
        for gt_bbox in gt_info[img]:
            flag_missing = True
            for det_bbox in det_info[img]:
                if cal_iou(gt_bbox, det_bbox) >= iou_thresh:
                    flag_missing = False
                    break
            if flag_missing:
                if img not in missing_dets.keys():
                    missing_dets[img] = [gt_bbox]
                else:
                    missing_dets[img].append(gt_bbox)
        with open('wrong_dets.csv', 'w') as f:
            for img in wrong_dets.keys():
                f.write('\n{},'.format(img))
                for bbox in wrong_dets[img]:
                    f.write('{}_{}_{}_{};'.format(bbox[0], bbox[1], bbox[2], bbox[3]))
        with open('missing_dets.csv', 'w') as f:
            for img in missing_dets.keys():
                f.write('\n{},'.format(img))
                for bbox in missing_dets[img]:
                    f.write('{}_{}_{}_{};'.format(bbox[0], bbox[1], bbox[2], bbox[3]))
    return right_num
def xyxy2xywh(res):
    xywh_res = dict()
    for im in res.keys():
        xywh_res[im] = []
        for bbox in res[im]:
            xywh_res[im].append([bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]])
    return  xywh_res
def parse_args():
    parser = argparse.ArgumentParser(description='End-to-end inference')
    parser.add_argument(
        '--val_file',
        dest='val_file',
        help='csv file to val',
        default=None,
        type=str
    )
    parser.add_argument(
        '--thresh',
        dest='thresh',
        help='thresh of results to eval',
        default=0.9,
        type=float
    )
    parser.add_argument(
        '--ifxyxy2xywh',
        dest='ifxyxy2xywh',
        help='if convert results from xyxy to xywh',
        default=False,
        type=bool
    )
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    anno_file = 'test_anno.csv'
    annos_info, gt_num = read_csv(anno_file)
    res_file = args.val_file
    res_info, det_num = read_csv(res_file, args.thresh)
    if args.ifxyxy2xywh:
        res_info = xyxy2xywh(res_info)
    sum = 0.0
    print 'gt_num\t', gt_num, 'det_num', det_num
    for iou_thresh in np.arange(0.5, 0.95, 0.05):
        a = eval_det(annos_info, res_info, iou_thresh, True)
        s = 2.0 * a / (gt_num + det_num)
        print iou_thresh, a, s
        sum += s
    print 'average res\t', sum / 9

max_stars_repo_name: gaohaidong/Detectron

max_issues_repo_name: gaohaidong/Detectron

max_forks_repo_name: gaohaidong/Detectron


text:
"""
Copyright (c) Facebook, Inc. and its affiliates.
"""
import os
import sys
import math
import copy
import time
import logging
from collections.abc import Iterable
from prettytable import PrettyTable
import Pyro4
import numpy as np

import cv2
from droidlet.shared_data_structs import ErrorWithResponse
from droidlet.dashboard.o3dviz import deserialize as o3d_unpickle
from droidlet.shared_data_structs import RGBDepth

from droidlet.lowlevel.robot_coordinate_utils import (
    base_canonical_coords_to_pyrobot_coords,
    xyz_pyrobot_to_canonical_coords,
)

from droidlet.lowlevel.robot_mover import MoverInterface
from droidlet.lowlevel.robot_mover_utils import get_camera_angles, angle_diff, transform_pose

from droidlet.shared_data_struct.rotation import (
    rotation_matrix_x,
    rotation_matrix_y,
    rotation_matrix_z,
)
from droidlet.lowlevel.robot_coordinate_utils import (
    xyz_pyrobot_to_canonical_coords,
    base_canonical_coords_to_pyrobot_coords,
)

from tenacity import retry, stop_after_attempt, wait_fixed
from droidlet.lowlevel.pyro_utils import safe_call
from .data_compression import *

Pyro4.config.SERIALIZER = "pickle"
Pyro4.config.SERIALIZERS_ACCEPTED.add("pickle")
Pyro4.config.PICKLE_PROTOCOL_VERSION = 2

MAX_PAN_RAD = math.pi / 4

# TODO/FIXME: state machines.  state machines everywhere

class HelloRobotMover(MoverInterface):
    """Implements methods that call the physical interfaces of the Robot.

    Arguments:
        ip (string): IP of the Robot.
    """

    def __init__(self, ip=None):
        self.bot = Pyro4.Proxy("PYRONAME:hello_robot@" + ip)
        self.bot._pyroAsync()
        self.is_moving = self.bot.is_moving()
        self.camera_transform = self.bot.get_camera_transform().value
        self.camera_height = self.camera_transform[2, 3]
        self.cam = Pyro4.Proxy("PYRONAME:hello_realsense@" + ip)
        self.slam = Pyro4.Proxy("PYRONAME:slam@" + ip)
        self.nav = Pyro4.Proxy("PYRONAME:navigation@" + ip)
        # spin once synchronously
        self.nav.is_busy()
        # put in async mode
        self.nav._pyroAsync()
        self.nav_result = self.nav.is_busy()

        self.data_logger = Pyro4.Proxy("PYRONAME:hello_data_logger@" + ip)
        self.data_logger._pyroAsync()
        _ = safe_call(self.data_logger.ready)

        intrinsic_mat = safe_call(self.cam.get_intrinsics)
        height, width = safe_call(self.cam.get_img_resolution, rotate=False)
        self.uv_one_in_cam = HelloRobotMover.compute_uvone(intrinsic_mat, height, width)

    def log_data_start(self, seconds):
        self.data_logger.save_batch(seconds)

    def log_data_stop(self):
        self.data_logger.stop()

    def bot_step(self):
        f = not self.bot.is_moving().value
        return f

    def relative_pan_tilt(self, dpan, dtilt, turn_base=True):
        """
        move the head so its new tilt is current_tilt + dtilt
        and pan is current_pan + dpan

        Args:
            dpan (float): angle in radians to turn head left-right.
                positive is right
            dtilt (float): angle in radians to turn head up-down.
                positive is up
        """
        # FIXME handle out-of-range values properly
        dtilt = dtilt or 0
        dpan = dpan or 0

        new_tilt = self.get_tilt() + dtilt

        # FIXME: make a safe_base_turn method
        if np.abs(dpan) > MAX_PAN_RAD and turn_base:
            dyaw = np.sign(dpan) * (np.abs(dpan) - MAX_PAN_RAD)
            self.turn(dyaw * 180 / math.pi)
            pan_rad = np.sign(dpan) * MAX_PAN_RAD
        else:
            pan_rad = dpan
        new_pan = self.get_pan() + pan_rad
        self.bot.set_pan_tilt(new_pan, new_tilt)
        return "finished"

    def set_look(self, pan_rad, tilt_rad, turn_base=True, world=False):
        """
        Sets the agent to look at a specified absolute pan and tilt.
        These are  "absolute" w.r.t. robot current base, if world==False
        and absolute w.r.t. world coords if world=True

        Args:
            pan_rad (float): angle in radians to turn head left-right.
                positive is right
            tilt_rad (float): angle in radians to to turn head up-down.
                positive is down.
        """
        tilt_rad = tilt_rad or self.get_tilt()
        # TODO handle out-of-range properly
        dtilt = angle_diff(self.get_tilt(), tilt_rad)
        if not world:
            pan_rad = pan_rad or self.get_pan()
            dpan = angle_diff(self.get_pan(), pan_rad)
        else:
            base_pan = self.get_base_pos_in_canonical_coords()[2]
            pan_rad = pan_rad or base_pan + self.get_pan()
            dpan = angle_diff(base_pan + self.get_pan(), pan_rad)
        return self.relative_pan_tilt(dpan, dtilt, turn_base=turn_base)

    def is_obstacle_in_front(self, return_viz=False):
        ret = safe_call(self.cam.is_obstacle_in_front, return_viz)
        if return_viz:
            obstacle, cpcd, crop, bbox, rest = ret

            cpcd = o3d_unpickle(cpcd)
            crop = o3d_unpickle(crop)
            bbox = o3d_unpickle(bbox)
            rest = o3d_unpickle(rest)
            return obstacle, cpcd, crop, bbox, rest
        else:
            obstacle = ret
            return obstacle

    def look_at(self, target, turn_base=True, face=False):
        """
        Executes "look at" by setting the pan, tilt of the camera
        or turning the base if required.
        Uses both the base state and object coordinates in
        canonical world coordinates to calculate expected yaw and pitch.
        if face == True will move body so head yaw is 0

        Args:
            target (list): object coordinates as saved in memory.
            turn_base: if False, will try to look at point only by moving camera and not base
        """
        old_pan = self.get_pan()
        old_tilt = self.get_tilt()
        pos = self.get_base_pos_in_canonical_coords()
        cam_transform = self.bot.get_camera_transform().value
        cam_pos = cam_transform[0:3, 3]
        # convert cam_pos to canonical co-ordinates
        # FIXME !  do this properly in a util, current functions don't do it bc
        #          we are mixing base pos already converted and cam height
        cam_pos = [pos[0], cam_pos[2], pos[1]]

        logging.info(f"Current base state (x, z, yaw): {pos}, camera state (x, y, z): {cam_pos}")
        logging.info(f"looking at x,y,z: {target}")

        pan_rad, tilt_rad = get_camera_angles(cam_pos, target)
        #        pan_res = angle_diff(pos[2], pan_rad)
        # For the Hello camera, negative tilt seems to be up, and positive tilt is down
        # For the locobot camera, it is the opposite
        # TODO: debug this further, and make things across robots consistent
        logging.info(f"Returned new pan and tilt angles (radians): ({pan_rad}, {tilt_rad})")
        if face:
            # TODO less blocking, make me into state machine
            dpan = angle_diff(self.get_base_pos_in_canonical_coords()[2], pan_rad)
            self.turn(dpan * 180 / math.pi)
            return self.set_look(0, tilt_rad)
        else:
            self.set_look(pan_rad, tilt_rad, turn_base=True, world=True)

    def is_busy(self):
        return self.nav.is_busy().value and self.bot.is_busy().value

    def stop(self):
        """immediately stop the robot."""
        self.nav.stop()
        return self.bot.stop()

    def unstop(self):
        """remove a runstop flag via software"""
        return self.bot.remove_runstop()

    def get_pan(self):
        """get yaw in radians."""
        return self.bot.get_pan().value

    def get_tilt(self):
        """get pitch in radians."""
        return self.bot.get_tilt().value

    def reset_camera(self):
        """reset the camera to 0 pan and tilt."""
        return self.bot.reset()

    def move_relative(self, xyt_positions, blocking=True):
        """Command to execute a relative move.

        Args:
            xzt_positions: a list of relative (x,y,yaw) positions for the bot to execute.
            x,y,yaw are in the pyrobot's coordinates.
        """
        if not isinstance(next(iter(xyt_positions)), Iterable):
            # single xyt position given
            xyt_positions = [xyt_positions]
        for xyt in xyt_positions:
            self.nav_result.wait()
            self.nav_result = safe_call(self.nav.go_to_relative, xyt)
            if blocking:
                self.nav_result.wait()

    def move_absolute(self, xzt_positions, blocking=True):
        """Command to execute a move to an absolute position.

        It receives positions in canonical world coordinates and converts them to pyrobot's coordinates
        before calling the bot APIs.

        Args:
            xzt_positions: a list of (x_c,z_c,yaw) positions for the bot to move to.
            (x_c,z_c,yaw) are in the canonical world coordinates.
        """
        if not isinstance(next(iter(xzt_positions)), Iterable):
            # single xzt position given
            xzt_positions = [xzt_positions]
        for xzt in xzt_positions:
            logging.info("Move absolute in canonical coordinates {}".format(xzt))
            self.nav_result.wait()
            robot_coords = base_canonical_coords_to_pyrobot_coords(xzt)
            self.nav_result = self.nav.go_to_absolute(robot_coords)
            if blocking:
                self.nav_result.wait()
        return "finished"

    def get_base_pos_in_canonical_coords(self):
        """get the current robot position in the canonical coordinate system

        the canonical coordinate systems:
        from the origin, at yaw=0, front is (x, y, z) = (0, 0, 1),
        its right direction is (x,y,z) = (1, 0, 0)
        its up direction is (x,y,z) = (0, 1, 0)
        yaw is + counterclockwise

        return:
        (x, z, yaw) of the robot base in canonical coordinates
        """
        future = safe_call(self.bot.get_base_state)
        x_robot, y_robot, yaw = future.value
        z_canonical = x_robot
        x_canonical = -y_robot
        return np.array([x_canonical, z_canonical, yaw])

    def get_current_pcd(self, in_cam=False, in_global=False):
        """Gets the current point cloud"""
        return self.cam.get_current_pcd()

    def get_rgb_depth(self):
        """Fetches rgb, depth and pointcloud in pyrobot world coordinates.

        Returns:
            an RGBDepth object
        """
        base_state = self.bot.get_base_state()
        rgb, depth, rot, trans = self.cam.get_pcd_data(rotate=False)
        rgb, depth = jpg_decode(rgb), blosc_decode(depth)
        depth = np.divide(depth, 1000, dtype=np.float32)  # convert from mm to metres
        base_state = self.bot.get_base_state().value
        uv_one_in_cam = self.uv_one_in_cam
        return HelloRobotMover.compute_pcd(rgb, depth, rot, trans, base_state, uv_one_in_cam)

    @staticmethod
    def compute_uvone(intrinsic_mat, height, width):
        intrinsic_mat_inv = np.linalg.inv(intrinsic_mat)
        img_pixs = np.mgrid[0:height:1, 0:width:1]
        img_pixs = img_pixs.reshape(2, -1)
        img_pixs[[0, 1], :] = img_pixs[[1, 0], :]
        uv_one = np.concatenate((img_pixs, np.ones((1, img_pixs.shape[1]))))
        uv_one_in_cam = np.dot(intrinsic_mat_inv, uv_one)
        return uv_one_in_cam

    @staticmethod
    def compute_pcd(rgb, depth, rot_cam, trans_cam, base_state, uv_one_in_cam):
        rgb = np.asarray(rgb).astype(np.uint8)
        rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)

        depth = depth.astype(np.float32)

        # the realsense pointcloud seems to produce some spurious points
        # really far away. So, limit the depth to 8 metres
        thres = 8000
        depth[depth > thres] = thres

        depth_copy = np.copy(depth)

        depth = depth.reshape(rgb.shape[0] * rgb.shape[1])

        # normalize by the camera's intrinsic matrix
        pts_in_cam = np.multiply(uv_one_in_cam, depth)
        pts = pts_in_cam.T

        # Now, the points are in camera frame.
        # In camera frame
        # z is positive into the camera
        # (larger the z, more into the camera)
        # x is positive to the right
        # (larger the x, more right of the origin)
        # y is positive to the bottom
        # (larger the y, more to the bottom of the origin)
        #                                 /
        #                                /
        #                               / z-axis
        #                              /
        #                             /_____________ x-axis (640)
        #                             |
        #                             |
        #                             | y-axis (480)
        #                             |
        #                             |

        # We now need to transform this to pyrobot frame, where
        # x is into the camera, y is positive to the left,
        # z is positive upwards
        # https://pyrobot.org/docs/navigation
        #                            |    /
        #                 z-axis     |   /
        #                            |  / x-axis
        #                            | /
        #  y-axis        ____________|/
        #
        # If you hold the first configuration in your right hand, and
        # visualize the transformations needed to get to the second
        # configuration, you'll see that
        # you have to rotate 90 degrees anti-clockwise around the y axis, and then
        # 90 degrees clockwise around the x axis.
        # This results in the final configuration
        roty90 = rotation_matrix_y(90)
        rotxn90 = rotation_matrix_x(-90)
        # next, rotate and translate pts by
        # the robot pose and location
        rot_base = rotation_matrix_z(math.degrees(base_state[2]))

        rotation_matrix = rot_base @ rot_cam @ rotxn90 @ roty90
        translation_vector = np.array(
            [trans_cam[0] + base_state[0], trans_cam[1] + base_state[1], trans_cam[2] + 0]
        ).reshape(-1)

        pts = np.dot(pts, rotation_matrix.T)
        pts = pts + translation_vector

        # now rewrite the ordering of pts so that the colors (rgb_rotated)
        # match the indices of pts
        pts = pts.reshape((rgb.shape[0], rgb.shape[1], 3))
        pts = np.rot90(pts, k=1, axes=(1, 0))
        pts = pts.reshape(rgb.shape[0] * rgb.shape[1], 3)

        depth_rotated = np.rot90(depth_copy, k=1, axes=(1, 0))
        rgb_rotated = np.rot90(rgb, k=1, axes=(1, 0))

        return RGBDepth(rgb_rotated, depth_rotated, pts)

    def turn(self, yaw):
        """turns the bot by the yaw specified.

        Args:
            yaw: the yaw to execute in degree.
        """
        turn_rad = yaw * math.pi / 180
        self.bot.rotate_by(turn_rad)

    def explore(self, goal):
        if self.nav_result.ready:
            self.nav_result = safe_call(self.nav.explore, goal)
        else:
            print("navigator executing another call right now")
        return self.nav_result

    def get_obstacles_in_canonical_coords(self):
        """
        get the positions of obtacles position in the canonical coordinate system
        instead of the Locobot's global coordinates as stated in the Locobot
        documentation: https://www.pyrobot.org/docs/navigation or
        https://github.com/facebookresearch/pyrobot/blob/master/docs/website/docs/ex_navigation.md
        the standard coordinate systems:
        Camera looks at (0, 0, 1),
        its right direction is (1, 0, 0) and
        its up-direction is (0, 1, 0)

        return:
         list[(x, z)] of the obstacle location in standard coordinates
        """
        cordinates_in_robot_frame = self.slam.get_map()
        cordinates_in_standard_frame = [
            xyz_pyrobot_to_canonical_coords(list(c) + [0.0]) for c in cordinates_in_robot_frame
        ]
        cordinates_in_standard_frame = [(c[0], c[2]) for c in cordinates_in_standard_frame]
        return cordinates_in_standard_frame

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", default="")
    opts = parser.parse_args()
    base_path = os.path.dirname(__file__)
    mover = HelloRobotMover(ip=opts.ip)

max_stars_repo_name: BearerPipelineTest/fairo

max_issues_repo_name: BearerPipelineTest/fairo

max_forks_repo_name: BearerPipelineTest/fairo


text:
"""Minimalist structure representation for comprehensible manipulation."""

import logging
import re
from collections import defaultdict
from operator import itemgetter
from typing import Dict, List, Optional, Tuple, Union

from math import gcd
from functools import reduce

import numpy as np
import pymatgen
from pymatgen.analysis.bond_valence import BVAnalyzer
from pymatgen.ext.matproj import MPRester

import smact

from . import logger
from .utilities import get_sign

class SmactStructure:
    """SMACT implementation inspired by pymatgen Structure class.

    Handles basic structural and compositional information for a compound.
    Includes a lossless POSCAR-style specification for storing structures,
    allowing structures to be stored in files or databases, or to be pulled
    from the `Materials Project <https://www.materialsproject.org>`_.

    Attributes:
        species: A list of tuples describing the composition of the structure,
            stored as (element, oxidation, stoichiometry). The list is sorted
            alphabetically based on element symbol, and identical elements
            are sorted with highest charge first.
        lattice_mat: A numpy 3x3 array containing the lattice vectors.
        sites: A dictionary of {species: coords}, where species is a string
            representation of the species and coords is a list of position
            vectors, given as lists of length 3. For example:

            >>> s = SmactStructure.from_file('tests/files/NaCl.txt')
            >>> s.sites
            {'Cl1-': [[2.323624165, 1.643050405, 4.02463512]], 'Na1+': [[0.0, 0.0, 0.0]]}

        lattice_param: The lattice parameter.

    """

    def __init__(
      self,
      species: List[Union[Tuple[str, int, int], Tuple[smact.Species, int]]],
      lattice_mat: np.ndarray,
      sites: Dict[str, List[List[float]]],
      lattice_param: Optional[float] = 1.0,
      sanitise_species: Optional[bool] = True,
    ):
        """Initialize structure with constituent species.

        Args:
            species: See :class:`~.SmactStructure`. May be supplied as either a list of
                (element, oxidation, stoichiometry) or (:class:`~smact.Species`, stoichiometry).
            lattice_mat: See :class:`~.SmactStructure`.
            sites: See :class:`~.SmactStructure`.
            lattice_param: See :class:`~.SmactStructure`.
            sanitise_species: Whether to sanitise check species. Should be `True` unless
                species have already been sanitised by a different constructor like
                :meth:`~.from_mp`.

        """
        self.species = self._sanitise_species(species) if sanitise_species else species

        self.lattice_mat = lattice_mat

        self.sites = {spec: sites[spec] for spec in self.get_spec_strs()}  # Sort sites

        self.lattice_param = lattice_param

    def __repr__(self):
        """Represent the structure as a POSCAR.

        Alias for :meth:`~.as_poscar`.

        """
        return self.as_poscar()

    def __eq__(self, other):
        """Determine equality of SmactStructures based on their attributes.

        :attr:`~.species`, :attr:`~.lattice_mat`, :attr:`~.lattice_param` and
        :attr:`~.sites` must all be equal for the comparison to be True.

        Note:
            For the SmactStructures to be equal their attributes must be
            *identical*. For example, it is insufficient that the two
            structures have the same space group or the same species;
            the site coordinates must be equal also.

        """
        if not isinstance(other, SmactStructure):
            return False
        return all([
          self.species == other.species,
          np.array_equal(self.lattice_mat, other.lattice_mat),
          self.lattice_param == other.lattice_param,
          self.sites == other.sites,
          list(self.sites.keys()) == list(other.sites.keys()),
        ])

    @staticmethod
    def _sanitise_species(
      species: List[Union[Tuple[str, int, int], Tuple[smact.Species, int]]],
    ) -> List[Tuple[str, int, int]]:
        """Sanitise and format a list of species.

        Args:
            species: See :meth:`~.__init__`.

        Returns:
            sanit_species: Sanity-checked species in the format of
            a list of (element, oxidation, stoichiometry).

        Raises:
            TypeError: species contains the wrong types.
            ValueError: species is either empty or contains tuples of
                incorrect length.

        """
        if not isinstance(species, list):
            raise TypeError(f"`species` must be a list, got {type(species)}.")
        if len(species) == 0:
            raise ValueError("`species` cannot be empty.")
        if not isinstance(species[0], tuple):
            raise TypeError(f"`species` must be a list of tuples, got list of {type(species[0])}.")

        species_error = (
          "`species` list of tuples must contain either "
          "2-tuples of Species objects and stoichiometries, "
          "or 3-tuples of elements, oxidations and stoichiometries."
        )
        if len(species[0]) not in {2, 3}:
            raise ValueError(species_error)

        if isinstance(species[0][0], str):  # String variation of instantiation
            species.sort(key=itemgetter(1), reverse=True)
            species.sort(key=itemgetter(0))
            sanit_species = species

        elif isinstance(species[0][0], smact.Species):  # Species class variation of instantiation
            species.sort(key=lambda x: (x[0].symbol, -x[0].oxidation))
            sanit_species = [(x[0].symbol, x[0].oxidation, x[1]) for x in species]

        else:
            raise TypeError(species_error)

        return sanit_species

    @staticmethod
    def __parse_py_sites(
      structure: pymatgen.core.Structure,
    ) -> Tuple[Dict[str, List[List[float]]], List[Tuple[str, int, int]]]:
        """Parse the sites of a pymatgen Structure.

        Args:
            structure: A :class:`pymatgen.core.Structure` instance.

        Returns:
            sites (dict): In which a key is a species string
                and its corresponding value is a list of the coordinates
                that species occupies in the supercell. The coordinates
                are represented by lists containing three elements: one
                for each spatial dimension.
            species (list): A list of each species in the structure,
                represented by a tuple of (element, charge, stoichiometry).

        """
        if not isinstance(structure, pymatgen.core.Structure):
            raise TypeError("structure must be a pymatgen.core.Structure instance.")

        sites = defaultdict(list)
        for site in structure.sites:
            site_type = site.species_string
            # Add charge magnitude, for cases of unit charge
            if all([
              site_type[-2] not in map(str, range(10)),
              site_type[-1] in ("+", "-"), ]):
                site_type = site_type[:-1] + '1' + site_type[-1]

            sites[site_type].append(site.coords.tolist())

        sites = dict(sites)

        # Find stoichiometry
        total_specs = [len(val) for val in sites.values()]
        hcf = reduce(gcd, total_specs)
        total_specs = [int(x / hcf) for x in total_specs]

        species = []
        for spec, stoic in zip(sites.keys(), total_specs):
            charge_match = re.search(r"\d+", spec)

            if charge_match:
                charge_loc = charge_match.start()
                symb = spec[:charge_loc]
                charge = int(spec[-1] + spec[charge_loc:-1])
            else:
                symb = spec
                charge = 0

            species.append((symb, charge, stoic))

        return sites, species

    @staticmethod
    def from_py_struct(structure: pymatgen.core.Structure):
        """Create a SmactStructure from a pymatgen Structure object.

        Args:
            structure: A pymatgen Structure.

        Returns:
            :class:`~.SmactStructure`

        """
        if not isinstance(structure, pymatgen.core.Structure):
            raise TypeError("Structure must be a pymatgen.core.Structure instance.")

        bva = BVAnalyzer()
        struct = bva.get_oxi_state_decorated_structure(structure)

        sites, species = SmactStructure.__parse_py_sites(struct)

        lattice_mat = struct.lattice.matrix

        lattice_param = 1.0

        return SmactStructure(
          species,
          lattice_mat,
          sites,
          lattice_param,
          sanitise_species=True, )

    @staticmethod
    def from_mp(
      species: List[Union[Tuple[str, int, int], Tuple[smact.Species, int]]],
      api_key: str, ):
        """Create a SmactStructure using the first Materials Project entry for a composition.

        Args:
            species: See :meth:`~.__init__`.
            api_key: A www.materialsproject.org API key.

        Returns:
            :class:`~.SmactStructure`

        """
        sanit_species = SmactStructure._sanitise_species(species)

        with MPRester(api_key) as m:
            eles = SmactStructure._get_ele_stoics(sanit_species)
            formula = "".join(f"{ele}{stoic}" for ele, stoic in eles.items())
            structs = m.query(
              criteria={"reduced_cell_formula": formula},
              properties=["structure"], )

            if len(structs) == 0:
                raise ValueError(
                  "Could not find composition in Materials Project Database, "
                  "please supply a structure."
                )

            struct = structs[0]['structure']  # Default to first found structure

        if 0 not in (spec[1] for spec in sanit_species):  # If everything's charged
            bva = BVAnalyzer()
            struct = bva.get_oxi_state_decorated_structure(struct)

        lattice_mat = struct.lattice.matrix

        lattice_param = 1.0  # TODO Use actual lattice parameter

        sites, _ = SmactStructure.__parse_py_sites(struct)

        return SmactStructure(
          sanit_species,
          lattice_mat,
          sites,
          lattice_param,
          sanitise_species=False, )

    @staticmethod
    def from_file(fname: str):
        """Create SmactStructure from a POSCAR file.

        Args:
            fname: The name of the POSCAR file.
                See :meth:`~.as_poscar` for format specification.

        Returns:
            :class:`~.SmactStructure`

        """
        with open(fname, 'r') as f:
            return SmactStructure.from_poscar(f.read())

    @staticmethod
    def from_poscar(poscar: str):
        """Create SmactStructure from a POSCAR string.

        Args:
            poscar: A SMACT-formatted POSCAR string.
                See :meth:`~.as_poscar` for format specification.

        Returns:
            :class:`~.SmactStructure`

        """
        lines = poscar.split("\n")

        # Find stoichiometry
        total_specs = [int(x) for x in lines[6].split(" ")]
        hcf = reduce(gcd, total_specs)
        total_specs = [int(x / hcf) for x in total_specs]

        species = []
        for spec_str, stoic in zip(lines[0].split(" "), total_specs):
            charge_match = re.search(r"\d+", spec_str)

            if charge_match:
                charge_loc = charge_match.start()
                symb = spec_str[:charge_loc]
                charge = int(spec_str[-1] + spec_str[charge_loc:-1])
            else:
                symb = spec_str
                charge = 0

            species.append((symb, charge, stoic))

        lattice_param = float(lines[1])

        lattice = np.array([[float(point) for point in line.split(" ")] for line in lines[2:5]])

        sites = defaultdict(list)
        for line in lines[8:]:
            if not line:  # EOF
                break

            split_line = line.split(" ")
            coords = [float(x) for x in split_line[:3]]
            spec = split_line[-1]

            sites[spec].append(coords)

        sites = dict(sites)

        return SmactStructure(species, lattice, sites, lattice_param)

    def _format_style(
      self,
      template: str,
      delim: Optional[str] = " ",
      include_ground: Optional[bool] = False, ) -> str:
        """Format a given template string with the composition.

        Formats a python template string with species information,
        with each species separated by a given delimiter.

        Args:
            template: Template string to format, using python's
                curly brackets notation. Supported keywords are
                `ele` for the elemental symbol, `stoic` for the
                stoichiometry, `charge` for the absolute value
                of oxidation state and `sign` for the
                oxidation state's sign.
            delim: The delimeter between species' templates.
            include_ground: Whether to include the charge and sign
                of neutral species.

        Returns:
            String of templates formatted for each species, separated
                by `delim`.

        Examples:
            >>> s = SmactStructure.from_file('tests/files/CaTiO3.txt')
            >>> template = '{stoic}x{ele}{charge}{sign}'
            >>> print(s._format_style(template))
            1xCa2+ 3xO2- 1xTi4+

        """
        if include_ground:
            return delim.join(
              template.format(
                ele=specie[0],
                stoic=specie[2],
                charge=abs(specie[1]),
                sign="+" if specie[1] >= 0 else "-",
              ) for specie in self.species
            )

        return delim.join(
          template.format(
            ele=specie[0],
            stoic=specie[2],
            charge=abs(specie[1]) if specie[1] != 0 else "",
            sign=get_sign(specie[1]),
          ) for specie in self.species
        )

    @staticmethod
    def _get_ele_stoics(species: List[Tuple[str, int, int]]) -> Dict[str, int]:
        """Get the number of each element type in the compound, irrespective of oxidation state.

        Args:
            species: See :meth:`~.__init__`.

        Returns:
            eles: Dictionary of {element: stoichiometry}.

        Examples:
            >>> species = [('Fe', 2, 1), ('Fe', 3, 2), ('O', -2, 4)]
            >>> print(SmactStructure._get_ele_stoics(species))
            {'Fe': 3, 'O': 4}

        """
        eles = defaultdict(int)
        for specie in species:
            eles[specie[0]] += specie[2]

        return dict(eles)

    def has_species(self, species: Tuple[str, int]) -> bool:
        """Determine whether a given species is in the structure."""
        return species in map(itemgetter(0, 1), self.species)

    def get_spec_strs(self) -> List[str]:
        """Get string representations of the constituent species.

        Returns:
            A list of strings, formatted as '{element}{charge}{sign}'.

        Examples:
            >>> s = SmactStructure.from_file('tests/files/CaTiO3.txt')
            >>> s.get_spec_strs()
            ['Ca2+', 'O2-', 'Ti4+']

        """
        return self._format_style("{ele}{charge}{sign}").split(' ')

    def composition(self) -> str:
        """Generate a key that describes the composition.

        Key format is '{element}_{stoichiometry}_{charge}{sign}'
        with no delimiter, *sans brackets*. Species are ordered as stored within
        the structure, see :class:`~.SmactStructure`.

        Returns:
            Key describing constituent species.

        Examples:
            >>> s = SmactStructure.from_file('tests/files/CaTiO3.txt')
            >>> print(s.composition())
            Ca_1_2+O_3_2-Ti_1_4+

        """
        comp_style = "{ele}_{stoic}_{charge}{sign}"
        return self._format_style(comp_style, delim="", include_ground=True)

    def as_poscar(self) -> str:
        """Represent the structure as a POSCAR file compatible with VASP5.

        The POSCAR format adopted is as follows:

        The first line contains the species' names separated by a whitespace.
        The second through fourth line, inclusive, contain the lattice
        matrix: each line contains a lattice vector, with elements
        separated by a whitespace.
        The fifth line contains the elements' names separated by a whitespace.
        If more than one oxidation state exists for an element, the element
        appears multiple times; once for each oxidation state.
        The sixth line is the string 'Cartesian'.
        The seventh line onwards are the Cartesian coordinates of each site,
        separated by a whitespace. In addition, at the end of each line is the
        species' name, separated by a whitespace.

        For examples of this format, see the text files under tests/files.

        Returns:
            str: POSCAR-style representation of the structure.

        """
        poscar = " ".join(self.get_spec_strs()) + "\n"

        poscar += f"{self.lattice_param}\n"

        poscar += "\n".join(" ".join(map(str, vec)) for vec in self.lattice_mat.tolist()) + "\n"

        spec_count = {spec: len(coords) for spec, coords in self.sites.items()}

        poscar += self._format_style("{ele}") + "\n"

        poscar += " ".join(str(spec_count[spec]) for spec in self.get_spec_strs()) + "\n"

        poscar += "Cartesian\n"
        for spec, coords in self.sites.items():
            for coord in coords:
                poscar += " ".join(map(str, coord))
                poscar += f" {spec}\n"

        return poscar

max_stars_repo_name: AntObi/SMACT

max_issues_repo_name: AntObi/SMACT

max_forks_repo_name: AntObi/SMACT


text:
from pyTsetlinMachineParallel.tm import MultiClassTsetlinMachine
import numpy as np
from time import time
import math
from operator import itemgetter

X_train = np.array([])
Y_train = np.array([])
X_test = np.array([])
Y_test = np.array([])

# base_path_start = "Data/KfoldDataStaticTransformed/"
base_path_start = "data/"

# path_train = "Data/eventrain.data"
# path_test = "Data/eventest.data"

def round_up(n, decimals=0):
    multiplier = 10 ** decimals
    return math.ceil(n * multiplier) / multiplier

def tsetlinStandardWeighted(epochs, clauses, T, s):
    print("epochs = ", epochs)
    print("clauses = ", clauses)
    print("T = ", T)
    print("s = ", s, "\n")

    train_string = base_path_start + str(0) + "train" + base_path_end
    test_string = base_path_start + str(0) + "test" + base_path_end

    print("Loading training data..")
    train_data = np.loadtxt(train_string, delimiter=",")
    # print("..using train dataset: ", _path_train)
    global X_train
    global Y_train
    X_train = train_data[:, 0:-1]
    Y_train = train_data[:, -1]

    print("Loading test data..")
    test_data = np.loadtxt(test_string, delimiter=",")
    # print("..using test dataset: ", _path_test)
    global X_test
    global Y_test
    X_test = test_data[:, 0:-1]
    Y_test = test_data[:, -1]

    return TM(clauses, T, s, epochs)

def tsetlinStandardWeightedHandler(epochs, clauses, T, s):
    TM = tsetlinStandardWeighted(epochs, clauses, T, s)
    print("TM successfully prepared")
    return TM

def LoadStates(t_machine):
    print("Loading states")
    try:
        t_states = np.load(statefile, allow_pickle=True)
    except FileNotFoundError:
        print("Could not load state, file or directory not found.")
        exit(33)
    # Make sure X_train and Y_train is the same as the one used during training
    t_machine.fit(X_train, Y_train, epochs=1, incremental=True)
    t_machine.set_state(t_states)
    print("TM states successfully loaded from file: ", statefile)
    return t_machine

def TM(_clauses, _T, _s, _epochs):
    print("Creating MultiClass Tsetlin Machine.")
    tm = MultiClassTsetlinMachine(_clauses, _T, _s, boost_true_positive_feedback=1, weighted_clauses=True)
    tm = LoadStates(tm)
    result = round(100 * (tm.predict(X_test) == Y_test).mean(), 2)
    print("Trained accuracy: ", result)
    return tm

global lossweightsblack
global winweightsblack
global drawweightsblack

global lossweightswhite
global winweightswhite
global drawweightswhite

def TMInitBlack(epochs, clauses, treshold, s):
    print("Initializing Black Tsetlin")
    global base_path_end
    base_path_end = "statickfoldblack.data"
    global statefile
    statefile = "SavedStatesBlack.npy"
    global tmblack
    tmblack = tsetlinStandardWeightedHandler(epochs, clauses, treshold, s)
    global lossweightsblack
    global winweightsblack
    global drawweightsblack
    weightsblack = tmblack.get_state()
    lossweightsblack = weightsblack[0][0]
    winweightsblack = weightsblack[1][0]
    drawweightsblack = weightsblack[2][0]

def TMInitWhite(epochs, clauses, treshold, s):
    print("Initializing White Tsetlin")
    global base_path_end
    base_path_end = "statickfoldwhite.data"
    global statefile
    statefile = "SavedStatesWhite.npy"
    global tmwhite
    tmwhite = tsetlinStandardWeightedHandler(epochs, clauses, treshold, s)
    global lossweightswhite
    global winweightswhite
    global drawweightswhite
    weightswhite = tmwhite.get_state()
    lossweightswhite = weightswhite[0][0]
    winweightswhite = weightswhite[1][0]
    drawweightswhite = weightswhite[2][0]

def toBinary(board):
    """
    1 = black pawn
    2 = black king
    3 = white pawn
    4 = white king
    """
    if (len(board) != 32):
        print("Error in length of board, not equal to 32")
    binary_board = []
    for q in range(len(board)):
        if (board[q] == 1):
            binary_board.append(1)
        else:
            binary_board.append(0)
    for q in range(len(board)):
        if (board[q] == 2):
            binary_board.append(1)
        else:
            binary_board.append(0)
    for q in range(len(board)):
        if (board[q] == 3):
            binary_board.append(1)
        else:
            binary_board.append(0)
    for q in range(len(board)):
        if (board[q] == 4):
            binary_board.append(1)
        else:
            binary_board.append(0)

    return binary_board

def getResultScore(board, result, tm, color):
    weight_data = []
    test_boards = []

    lossweights = []
    winweights = []
    drawweights = []

    if (color == "black"):
        global lossweightsblack
        global winweightsblack
        global drawweightsblack
        lossweights = lossweightsblack
        winweights = winweightsblack
        drawweights = drawweightsblack
    elif (color == "white"):
        global lossweightswhite
        global winweightswhite
        global drawweightswhite
        lossweights = lossweightswhite
        winweights = winweightswhite
        drawweights = drawweightswhite
    else:
        print("getResultScore wrong color parameter, should be black or white")
        exit(34)

    loc_board = board.copy()
    loc_board.append(result)

    #these_clauses = []
    arraytest_boards = np.array([loc_board])
    predictions = tm.transform(arraytest_boards, inverted=False)

    loss = 0
    nloss = 0
    win = 0

    nwin = 0
    draw = 0
    ndraw = 0

    split = len(predictions[0]) / 3
    splitchanger = 0

    for z in range(len(predictions[0])):
        if (z == split):
            splitchanger += 1
        elif (z == split * 2):
            splitchanger += 1

        if (splitchanger == 0):
            if (predictions[0][z] == 1):
                loss += 1 * lossweights[z]
            elif (predictions[0][z] == 0):
                nloss += 1 * lossweights[z]
            else:
                print("tmStoreOutput: Clause not loss, win or draw")
                exit(69)
        elif (splitchanger == 1):
            index = int(z - split)
            if (predictions[0][z] == 1):
                win += 1 * winweights[index]
            elif (predictions[0][z] == 0):
                nwin += 1 * winweights[index]
            else:
                print("tmStoreOutput: Clause not loss, win or draw")
                exit(69)
        else:
            index = int(z - split * 2)
            if (predictions[0][z] == 1):
                draw += 1 * drawweights[index]
            elif (predictions[0][z] == 0):
                ndraw += 1 * drawweights[index]
            else:
                print("tmStoreOutput: Clause not loss, win or draw")
                exit(69)

    # print("Board number: ", i, " Actual result: ", Y_test[i])

    #these_clauses.append(result)
    # print("Positives: Loss: ", loss, " Win: ", win, " Draw: ", draw)

    loss = round(loss)
    nloss = round(nloss)

    win = round(win)
    nwin = round(nwin)

    draw = round(draw)
    ndraw = round(ndraw)

    #these_clauses.append(loss)
    #these_clauses.append(win)
    #these_clauses.append(draw)
    # print("Negatives: Loss: ", nloss, " Win: ", nwin, " Draw: ", ndraw)

    #these_clauses.append(nloss)
    #these_clauses.append(nwin)
    #these_clauses.append(ndraw)
    loss_score = loss - nloss
    win_score = win - nwin
    draw_score = draw - ndraw
    # print("Sum of predictions: Loss: ", loss_score, " Win: ", win_score, " Draw: ", draw_score)

    #these_clauses.append(loss_score)
    #these_clauses.append(win_score)
    #these_clauses.append(draw_score)
    #outcome = 8
    #weightscore = 8
    difference_sum = 0

    if (loss_score > win_score and loss_score > draw_score):
        #outcome = 0
        #weightscore = loss_score
        difference_sum = (loss_score - win_score) + (loss_score - draw_score)
    elif (win_score > loss_score and win_score > draw_score):
        #outcome = 1
        #weightscore = win_score
        difference_sum = (win_score - loss_score) + (win_score - draw_score)
    elif (draw_score > loss_score and draw_score > win_score):
        #outcome = 2
        #weightscore = draw_score
        difference_sum = (draw_score - loss_score) + (draw_score - win_score)
    else:
        if (loss_score == win_score and loss_score == draw_score):
            print("tmStoreOutput, all weight scores are the same")
            # weightscore could be any as they are all equal, chose loss as it comes first.
            #outcome = "69"
            #weightscore = loss_score
            difference_sum = 0

        elif (loss_score == win_score and loss_score != draw_score):
            #outcome = "01"
            # weightscore could be any as they are all equal, chose loss as it comes first.
            #weightscore = loss_score
            difference_sum = loss_score - draw_score

        elif (loss_score == draw_score and loss_score != win_score):
            #outcome = "02"
            # weightscore could be any as they are all equal, chose loss as it comes first.
            #weightscore = loss_score
            difference_sum = loss_score - win_score

        elif (win_score == draw_score and win_score != loss_score):
            #outcome = "12"
            # weightscore could be any as they are all equal, chose loss as it comes first.
            #weightscore = win_score
            difference_sum = win_score - loss_score

        else:
            print(
                "Error in tmStoreOutput, differences in scores; they are not all equal, yet not just two of them are equal? Debugs:")
            print("Loss score: ", loss_score, " Win score: ", win_score, " Draw score: ", draw_score)
            exit(234)

    # print("Tsetlin thinks ", outcome, " is correct, with score ", weightscore)
    #these_clauses.append(outcome)
    #these_clauses.append(weightscore)
    #these_clauses.append(difference_sum)

    return round(difference_sum/1000, 0)

#Evaluates the board, returns loss 0, win 1 or draw 0.5
def multiEvaluateWeighted(boards, tm, color):
    #tm is either tmblack or tmwhite. tmblack evaluates the winning chance of black player. tmwhite evaluates the winning chance of white player.
    check_boards = np.asarray(boards)
    result = tm.predict(check_boards)
    #Draw returns 0.5, a value or score between loss and win.
    results = []
    #The scores are modiefied to make sure that the scores of wins are always higher than scores draws, and scores of draws always higher than those of losses.
    for i in range(len(result)):
        # Draw
        if (result[i]  == 2):
            this_score = getResultScore(boards[i], result[i], tm, color)
            this_score += 2000
            this_score = round(this_score/100)
            results.append(this_score)
        #Win
        elif (result[i] == 1):
            this_score = getResultScore(boards[i], result[i], tm, color)
            this_score += 5000
            this_score = round(this_score/100)
            results.append(this_score)
        #Loss
        elif (result[i] == 0):
            #Negated by multiplying with (-1) as thinking that the loss prediction is reliable means that it might be more likely to be a good prediction. If it is a good prediction, we really do not want to get in this situation.
            this_score = getResultScore(boards[i], result[i], tm, color) * (-1)
            this_score -= 1000
            this_score = round(this_score/100)
            results.append(this_score)
        else:
            print("Result in evaluate not 2, 1 nor 0. This should not happen")
            exit(69)
    return results

# tsetlinStandardWeightedHandler(epochs, clauses, T, s)
# tsetlinStandardWeightedHandler(500, 15000, 28000, 15)

# Make sure the settings are equal to those used when training
#TMInitBlack(500, 15000, 28000, 15)
TMInitBlack(500, 15000, 28000, 15)
print(" ")
print(" ")
#TMInitWhite(500, 15000, 28000, 15)

# Clear the Tsetlin initializing data to relieve the memory a little.
X_train = 0
Y_train = 0
X_test = 0
Y_test = 0

print(" ")

current_board = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

#print(multiEvaluate([current_board], tmblack))

global global_weight_data
global_weight_data = []

#weights = tmblack.get_state()

#lossweights = weights[0][0]
#winweights = weights[1][0]
#drawweights = weights[2][0]

#print(weights)

boards = []
boards.append(current_board)
boards.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
boards.append([1,2,1,0,0,0,0,0,1,0,1,0,2,0,2,0,2,0,0,0,0,3,0,2,0,0,0,3,0,0,0,0])
boards.append([0,0,0,0,0,0,0,0,3,0,0,4,0,0,0,4,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0])
boards.append([0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0])
boards.append([1,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3])
boards.append([4,4,1,4,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,1,0,0,0,0,0,0,0,0,0,0,3,0])
boards.append([0,0,4,0,0,0,0,0,0,0,0,0,0,3,0,0,0,3,3,3,4,0,0,3,0,3,0,3,3,0,0,0])
boards.append([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
boards.append([3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,3,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3])

"""
test_this = np.asarray([[1,0,0,0,0,0,0,0,0,0,1,1,3,3,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0]])
result = tmblack.predict(test_this)
print(result)
"""

print(multiEvaluateWeighted(boards, tmblack, "black"))
exit(22)

max_stars_repo_name: Fiskesuppen/TsetlinMachine-Checkers

max_issues_repo_name: Fiskesuppen/TsetlinMachine-Checkers

max_forks_repo_name: Fiskesuppen/TsetlinMachine-Checkers


text:
# list of useful hardcoded info for a given simulation

import numpy as np

years             = np.arange(1999,2011,1)
nyears            = years.size
timestamp         = str(years[0]) + '-' + str(years[-1])
nltime            = 34
ltime             = np.arange(1,nltime+1,1)
nens              = 4
origin            = 'rjtd'
time              = "1200"

# create hindcast date array
hdates              = []
model_version_dates = []
for m in range(1,13,1):
    if ((m == 1) or (m == 3) or (m == 5) or (m == 7) or (m == 8) or (m == 10) or (m == 12)):
        hdates_day = np.array([10,20,31])
    elif ((m == 4) or (m == 6) or (m == 9) or (m == 11)):
        hdates_day = np.array([10,20,30])
    elif m == 2:
        hdates_day = np.array([10,20,28])

    for j in range(0,hdates_day.size,1):
        hdates.append( format(m,"02") + format(hdates_day[j],"02") )
        model_version_dates.append('20170131')

nhdates = len(hdates)


text:
import pathlib
from unittest import TestCase

import cv2
import numpy as np

from kneejerk.data.transfer import load_and_pad_images
from kneejerk.data.utils import _get_max_image_dim

HERE = pathlib.Path('.').resolve()
IM_DIR = HERE.joinpath('tests/images/')

class TestSquares(TestCase):

    def test_small_to_big(self):
        result = load_and_pad_images(str(IM_DIR) + '/200_200.png', 400)
        expected = cv2.imread(str(IM_DIR) + '/200_200_target.png')

        np.testing.assert_equal(result, expected)

    def test_pad_right(self):
        result = load_and_pad_images(str(IM_DIR) + '/400_200.png', 400)
        expected = cv2.imread(str(IM_DIR) + '/400_200_target.png')

        np.testing.assert_equal(result, expected)

    def test_pad_bottom(self):
        result = load_and_pad_images(str(IM_DIR) + '/200_400.png', 400)
        expected = cv2.imread(str(IM_DIR) + '/200_400_target.png')

        np.testing.assert_equal(result, expected)

    def test_no_pad(self):
        result = load_and_pad_images(str(IM_DIR) + '/400_400.png', 400)
        expected = cv2.imread(str(IM_DIR) + '/400_400.png')

        np.testing.assert_equal(result, expected)

class TestFindDims(TestCase):
    def test_biggest_dim(self):
        max_image_dim = _get_max_image_dim(str(HERE) + '/tests/squares.csv')
        assert max_image_dim == 400

max_stars_repo_name: NapsterInBlue/kneejerk

max_issues_repo_name: NapsterInBlue/kneejerk

max_forks_repo_name: NapsterInBlue/kneejerk


text:
#  ------------------------------------------------------------------------------------------
#  Copyright (c) Microsoft Corporation. All rights reserved.
#  Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
#  ------------------------------------------------------------------------------------------

from typing import Any, List, Union

import numpy as np
import torch
from math import inf
from scipy import stats
import torch.nn.functional as F

def get_instance_noise_model(n: float, dataset: Any, labels: Union[List, torch.Tensor], num_classes: int,
                             feature_size: int, norm_std: float, seed: int) -> np.ndarray:
    """
    :param n: noise_rate
    :param dataset: cifar10 # not train_loader
    :param labels: labels (targets)
    :param num_classes: class number
    :param feature_size: the size of input images (e.g. 28*28)
    :param norm_std: default 0.1
    :param seed: random_seed
    """
    if isinstance(labels, list):
        labels = torch.FloatTensor(labels)
    P = []
    random_state = np.random.RandomState(seed)
    flip_distribution = stats.truncnorm((0 - n) / norm_std, (1 - n) / norm_std, loc=n, scale=norm_std)
    flip_rate = flip_distribution.rvs(labels.shape[0], random_state=seed)

    W = random_state.randn(num_classes, feature_size, num_classes)
    W = torch.FloatTensor(W)

    for i, (x, y) in enumerate(dataset):
        # (1 x M) * (M x 10) = (1 x 10)
        A = x.view(1, -1).mm(W[y]).squeeze(0)
        A[y] = -inf
        A = flip_rate[i] * F.softmax(A, dim=0)
        A[y] += 1 - flip_rate[i]
        P.append(A)
    P = torch.stack(P, 0).numpy()

    return P

max_issues_repo_name: wensincai/InnerEye-DeepLearning

max_forks_repo_name: wensincai/InnerEye-DeepLearning

reason: import numpy,from scipy


text:
import os
import pickle

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from task_dyva.utils import save_figure
from task_dyva.visualization import PlotModelLatents

class FigureS6():
    """Analysis methods and plotting routines to reproduce
    Figure S6 from the manuscript (example latent state trajectories).
    """

    analysis_dir = 'model_analysis'
    stats_fn = 'holdout_outputs_01SD.pkl'
    fp_fn = 'fixed_points.pkl'
    age_bins = ['ages20to29', 'ages30to39', 'ages40to49',
                'ages50to59', 'ages60to69', 'ages70to79', 'ages80to89']
    plot_age_bins = ['ages20to29', 'ages50to59', 'ages80to89']
    plot_titles = ['Ages 20 to 29', 'Ages 50 to 59', 'Ages 80 to 89']
    figsize = (9, 13)
    figdpi = 300

    def __init__(self, model_dir, save_dir, metadata):
        self.model_dir = model_dir
        self.save_dir = save_dir
        self.expts = metadata['name']
        self.age_bins = metadata['age_range']
        self.sc_status = metadata['switch_cost_type']

        # Containers for summary stats
        self.all_stats = {ab: [] for ab in self.age_bins}
        self.all_fps = {ab: [] for ab in self.age_bins}

    def make_figure(self):
        print('Making Figure S6...')
        self._run_preprocessing()
        fig = self._plot_figure()
        save_figure(fig, self.save_dir, 'FigS6')
        print('')

    def _run_preprocessing(self):
        for expt_str, ab, sc in zip(self.expts,
                                    self.age_bins,
                                    self.sc_status):
            # Skip sc- models
            if sc == 'sc-':
                continue

            # Load stats from the holdout data
            stats_path = os.path.join(self.model_dir, expt_str,
                                      self.analysis_dir, self.stats_fn)
            with open(stats_path, 'rb') as path:
                expt_stats = pickle.load(path)

            # Load fixed points
            fp_path = os.path.join(self.model_dir, expt_str,
                                   self.analysis_dir, self.fp_fn)
            with open(fp_path, 'rb') as path:
                fps = pickle.load(path)

            self.all_stats[ab].append(expt_stats)
            self.all_fps[ab].append(fps)

    def _plot_figure(self):
        fig = plt.figure(figsize=self.figsize, dpi=self.figdpi)
        nrows = 5
        t_post = 1200
        elev, azim = 30, 60
        for ab_ind, ab in enumerate(self.plot_age_bins):
            this_stats = self.all_stats[ab]
            this_fps = self.all_fps[ab]
            this_means = np.array([s.summary_stats['u_mean_rt']
                                   for s in this_stats])
            sort_inds = np.argsort(this_means)
            plot_inds = np.arange(0, len(sort_inds), 20 // nrows)

            for ax_ind, p in enumerate(plot_inds):
                subplot_ind = ax_ind * 3 + ab_ind + 1
                ax = fig.add_subplot(nrows, 3, subplot_ind, projection='3d')
                plot_stats = this_stats[sort_inds[p]]
                plot_fps = this_fps[sort_inds[p]]

                # Plot
                if ax_ind == 0 and ab_ind == 0:
                    kwargs = {'annotate': True}
                else:
                    kwargs = {'annotate': False}
                plotter = PlotModelLatents(plot_stats, post_on_dur=t_post,
                                           fixed_points=plot_fps, plot_pre_onset=False)
                ax = plotter.plot_main_conditions(ax, elev=elev, azim=azim,
                                                  **kwargs)
                if ax_ind == 0:
                    ax.set_title(self.plot_titles[ab_ind])
        return fig

max_stars_repo_name: vishrawji/Task-DyVA

max_issues_repo_name: vishrawji/Task-DyVA

max_forks_repo_name: vishrawji/Task-DyVA


text:
# coding: utf-8

# In[ ]:

import pytest
import os
import sys
from numpy import unique

from sklearn.metrics import accuracy_score, mean_absolute_error

import donn
from donn import donn_tools

from sklearn.datasets import load_digits # Multi label classification
from sklearn.datasets import load_breast_cancer # Single label classificaiton
from sklearn.datasets import load_diabetes # Regression

# In[2]:

datasets = {"load_digits":{"name":"digits",
                           "fetch_data_function":load_digits,
                           "mode":"classifier",
                           "classifier_type":"multi"},
            "load_breast_cancer":{"name":"breast_cancer",
                                  "fetch_data_function":load_breast_cancer,
                                  "mode":"classifier",
                                  "classifier_type":"single"},
            "load_diabetes":{"name":"diabetes",
                             "fetch_data_function":load_diabetes,
                             "mode":"regressor",
                             "classifier_type":None}}

# In[3]:

def test_mode(validation=True):
    """
    Tests optimizer for different types of prediction tasks such as regression, single and multi label classification.
    Test if predictions are made and if optimized model results are better than unoptimized model
    """
    for dataset in datasets.keys():
        print(datasets[dataset]['name'])
        X, Y = datasets[dataset]['fetch_data_function'](return_X_y=True) # Get data
        x_train, y_train, x_validation, y_validation, x_test, y_test, x_supertest, y_supertest = donn_tools.split_dataset(X, Y, validation=True, supertest=True) # Split data

        ## Run Optimizer
        optimizer = donn.Optimizer(name = datasets[dataset]['name'], mode = datasets[dataset]['mode'])
        if validation == True:
            optimizer.optimize(x_train, y_train, x_validation, y_validation, x_test, y_test, max_rounds=1, level=1)
        else:
            optimizer.optimize(x_train, y_train, x_test, y_test, max_rounds=1, level=1)

        y_prediction = donn.predict(x_supertest, optimizer_name = datasets[dataset]['name']) # make prediction

        assert x_supertest.shape[0] == y_prediction.shape[0] # Check if predictions are present for each datapoint

        assert len(unique(y_prediction)) > 0 # Check if predictions are different

        ## Get baseline score for comparison
        if validation == True:
            baseline_score = donn.run_base_model(x_train, y_train, x_val=x_validation, y_val=y_validation, x_test=x_supertest, y_test=y_supertest, mode=datasets[dataset]['mode'], classifier_type=datasets[dataset]['classifier_type'])
        else:
            baseline_score = donn.run_base_model(x_train, y_train, x_test=x_supertest, y_test=y_supertest, mode=datasets[dataset]['mode'], classifier_type=datasets[dataset]['classifier_type'])

        ## Check if optimized results are better than baseline results
        if datasets[dataset]['mode'] == 'classifier':
            score = accuracy_score(y_supertest, y_prediction)
            assert score >= baseline_score
        elif datasets[dataset]['mode'] == 'regressor':
            score = mean_absolute_error(y_supertest, y_prediction)
            assert score <= baseline_score
    return "Test completed"

# In[4]:

def test_custom_parameters(validation=True):
    """
    Tests optimizer for different types of prediction tasks such as regression, single and multi label classification.
    Test if predictions are made and if optimized model results are better than unoptimized model
    """
    for dataset in datasets.keys():
        print(datasets[dataset]['name'])
        X, Y = datasets[dataset]['fetch_data_function'](return_X_y=True) # Get data
        x_train, y_train, x_validation, y_validation, x_test, y_test, x_supertest, y_supertest = donn_tools.split_dataset(X, Y, validation=True, supertest=True) # Split data

        layers = ['input', 'hidden', 'activation', 'dropout', 'output']
        if datasets[dataset]['name'] == 'digits':
            max_units_for_layers = [90, 60, 1, 1, 10]
        else:
            max_units_for_layers = [90, 60, 1, 1, 1]
        params = {"max_units_for_layers":max_units_for_layers,
                  "activation_function_options": ['selu', 'tanh'],
                  "optimizer_options": ['SGD', 'Adamax'],
                  "batch_size_range": [64],
                  "max_epochs": 25,
                  "max_dropout_rate": 0.5,
                  "output_activation_function_options": ['sigmoid', 'softmax']
                 }
        param_precisions = {"precision_for_layers":[5,4,1,1,1],
                            "precision_batch_size": 32,
                            "precision_epochs": 3,
                            "precision_dropout_rate": 0.15
                           }
        ## Run Optimizer
        optimizer = donn.Optimizer(name = datasets[dataset]['name'], mode = datasets[dataset]['mode'], layers = layers, parameters = params, parameter_precisions = param_precisions)
        if validation == True:
            optimizer.optimize(x_train, y_train, x_test, y_test, x_validation, y_validation, max_rounds=1, level=1)
        else:
            optimizer.optimize(x_train, y_train, x_test, y_test, max_rounds=1, level=1)

        y_prediction = donn.predict(x_supertest, optimizer_name = datasets[dataset]['name']) # make prediction

        assert x_supertest.shape[0] == y_prediction.shape[0] # Check if predictions are present for each datapoint

        assert len(unique(y_prediction)) > 0 # Check if predictions are different
    return "Test completed"

# In[ ]:

if __name__ == '__main__':
    test_mode(validation=True)
    test_mode(validation=False)
    test_custom_parameters(validation=True)
    test_custom_parameters(validation=False)

max_stars_repo_name: sharan-amutharasu/DONN

max_issues_repo_name: sharan-amutharasu/DONN

max_forks_repo_name: sharan-amutharasu/DONN

reason: from numpy


text:
import sys
import random
import numpy as np
import pygame
from pygame.locals import (QUIT, KEYDOWN, K_ESCAPE)
import pymunk
import pymunk.pygame_util

def add_ball(space):
    mass = 1
    radius = 14
    moment = pymunk.moment_for_circle(mass, 0, radius)
    body = pymunk.Body(mass, moment)
    x = random.randint(120, 380)
    body.position = x, 550
    shape = pymunk.Circle(body, radius)
    shape.friction = 1.0
    space.add(body, shape)
    return shape

def add_arm(space, config={}):

    default_config = {
        "arm_center": (300, 300),
        "lower_arm_length": 200,
        "lower_arm_starting_angle": 15,
        "lower_arm_mass": 10,
        "brach_rest_length": 5,
        "brach_stiffness": 450,
        "brach_damping": 200,
        "tricep_rest_length": 30,
        "tricep_stiffness": 50,
        "tricep_damping": 400
    }

    # Like dict.update() if it had an overwrite=False option
    config.update({k: v for k, v in default_config.items() if k not in list(config.keys())})

    # Upper Arm
    upper_arm_length = 200
    upper_arm_body = pymunk.Body(body_type=pymunk.Body.STATIC)
    upper_arm_body.position = config["arm_center"]
    upper_arm_body.angle = np.deg2rad(-45)
    upper_arm_line = pymunk.Segment(upper_arm_body, (0, 0), (-upper_arm_length, 0), 5)
    upper_arm_line.sensor = True  # Disable collision

    space.add(upper_arm_body)
    space.add(upper_arm_line)

    # Lower Arm
    lower_arm_body = pymunk.Body(0, 0)  # Pymunk will calculate moment based on mass of attached shape
    lower_arm_body.position = config["arm_center"]
    lower_arm_body.angle = np.deg2rad(config["lower_arm_starting_angle"])
    elbow_extension_length = 20
    lower_arm_start = (-elbow_extension_length, 0)
    lower_arm_line = pymunk.Segment(
        lower_arm_body,
        lower_arm_start,
        (config["lower_arm_length"], 0),
        5
    )
    lower_arm_line.mass = config["lower_arm_mass"]
    lower_arm_line.friction = 1.0

    space.add(lower_arm_body)
    space.add(lower_arm_line)

    # Pivot (Elbow)
    elbow_body = pymunk.Body(body_type=pymunk.Body.STATIC)
    elbow_body.position = config["arm_center"]
    elbow_joint = pymunk.PivotJoint(elbow_body, lower_arm_body, config["arm_center"])
    space.add(elbow_joint)

    # Spring (Brachialis Muscle)
    brach_spring = pymunk.constraint.DampedSpring(
        upper_arm_body,
        lower_arm_body,
        (-(upper_arm_length * (1 / 2)), 0),  # Connect half way up the upper arm
        (config["lower_arm_length"] / 5, 0),  # Connect near the bottom of the lower arm
        config["brach_rest_length"],
        config["brach_stiffness"],
        config["brach_damping"]
    )
    space.add(brach_spring)

    # Spring (Tricep Muscle)
    tricep_spring = pymunk.constraint.DampedSpring(
        upper_arm_body,
        lower_arm_body,
        (-(upper_arm_length * (3 / 4)), 0),
        lower_arm_start,
        config["tricep_rest_length"],
        config["tricep_stiffness"],
        config["tricep_damping"]
    )
    space.add(tricep_spring)

    # Elbow stop (prevent under/over extension)
    elbow_stop_point = pymunk.Circle(
        upper_arm_body,
        radius=5,
        offset=(-elbow_extension_length, -3)
    )
    elbow_stop_point.friction = 1.0
    space.add(elbow_stop_point)

    return brach_spring, tricep_spring

def main():
    pygame.init()
    screen_width = screen_height = 600
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("Curl Sim")
    clock = pygame.time.Clock()

    space = pymunk.Space()
    space.gravity = (0.0, -900.0)

    config = {
        "arm_center": (screen_width / 2, screen_height / 2),
        "lower_arm_length": 170,
    }
    brach, tricep = add_arm(space, config)

    ####################### Render Loop #####################
    debug = True
    ball_rain = False
    balls = []
    draw_options = pymunk.pygame_util.DrawOptions(screen)
    draw_options.flags = 1  # Disable constraint drawing
    if debug:
        draw_options.flags = 3  # Enable constraint drawing

    ticks_to_next_ball = 10
    frames = 0
    stiffness_delta = 50
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                sys.exit(0)

        ticks_to_next_ball -= 1
        if ticks_to_next_ball <= 0 and ball_rain:
            ticks_to_next_ball = 25
            ball_shape = add_ball(space)
            balls.append(ball_shape)
            if len(balls) > 10:
                space.remove(balls.pop(0))

        # Advance the simulation
        space.step(1/50.0)

        # Redraw all objects
        screen.fill((255, 255, 255))
        space.debug_draw(draw_options)
        pygame.display.flip()

        clock.tick(50)
        frames += 1

        #####################################################
        # Sensorimotor

        # Get sensory data

        # Activate motor units

        #####################################################
        # Vary world conditions
        if frames % 50 == 0:
            if debug:
                print("Tricep spring stiffness: ", tricep.stiffness)
            tricep.stiffness += stiffness_delta

        if frames % 1000 == 0:
            stiffness_delta *= -1

if __name__ == '__main__':
    main()

max_stars_repo_name: iandanforth/muscled-agents

max_issues_repo_name: iandanforth/muscledagents

max_forks_repo_name: iandanforth/muscled-agents


text:
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

"""
Decoders to predict 1D-4D parameter tensors given node embeddings.

"""

import numpy as np
import torch.nn as nn
from .mlp import MLP
from .layers import get_activation

class ConvDecoder(nn.Module):
    def __init__(self,
                 in_features=64,
                 hid=(128, 256),
                 out_shape=None,
                 num_classes=None):
        super(ConvDecoder, self).__init__()

        assert len(hid) > 0, hid
        self.out_shape = out_shape
        self.num_classes = num_classes
        self.fc = nn.Sequential(nn.Linear(in_features,
                                          hid[0] * np.prod(out_shape[2:])),
                                nn.ReLU())

        conv = []
        for j, n_hid in enumerate(hid):
            n_out = np.prod(out_shape[:2]) if j == len(hid) - 1 else hid[j + 1]
            conv.extend([nn.Conv2d(n_hid, n_out, 1),
                         get_activation(None if j == len(hid) - 1 else 'relu')])

        self.conv = nn.Sequential(*conv)
        self.class_layer_predictor = nn.Sequential(
            nn.ReLU(),
            nn.Conv2d(out_shape[0], num_classes, 1))

    def forward(self, x, max_shape=(1,1), class_pred=False):

        N = x.shape[0]
        x = self.fc(x).view(N, -1, *self.out_shape[2:])  # N,128,11,11
        out_shape = self.out_shape
        if sum(max_shape) > 0:
            x = x[:, :, :max_shape[0], :max_shape[1]]
            out_shape = (out_shape[0], out_shape[1], max_shape[0], max_shape[1])

        x = self.conv(x).view(N, *out_shape)  # N, out, in, h, w

        if class_pred:
            x = self.class_layer_predictor(x[:, :, :, :, 0])  # N, num_classes, 64, 1
            x = x[:, :, :, 0]  # N, num_classes, 64

        return x

class MLPDecoder(nn.Module):
    def __init__(self,
                 in_features=32,
                 hid=(64,),
                 out_shape=None,
                 num_classes=None):
        super(MLPDecoder, self).__init__()

        assert len(hid) > 0, hid
        self.out_shape = out_shape
        self.num_classes = num_classes
        self.mlp = MLP(in_features=in_features,
                       hid=(*hid, np.prod(out_shape)),
                       activation='relu',
                       last_activation=None)
        self.class_layer_predictor = nn.Sequential(
            get_activation('relu'),
            nn.Linear(hid[0], num_classes * out_shape[0]))

    def forward(self, x, max_shape=(1,1), class_pred=False):
        if class_pred:
            x = list(self.mlp.fc.children())[0](x)  # shared first layer
            x = self.class_layer_predictor(x)  # N, 1000, 64, 1
            x = x.view(x.shape[0], self.num_classes, self.out_shape[1])
        else:
            x = self.mlp(x).view(-1, *self.out_shape)
            if sum(max_shape) > 0:
                x = x[:, :, :, :max_shape[0], :max_shape[1]]
        return x

max_stars_repo_name: sergiobat/ppuda

max_issues_repo_name: sergiobat/ppuda

max_forks_repo_name: sergiobat/ppuda


text:
# Copyright (c) 2019-2020, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# import cupy as cp
# import cusignal
# import numpy as np
# import pytest

# from cusignal.test.utils import array_equal, _check_rapids_pytest_benchmark
# from scipy import signal

# gpubenchmark = _check_rapids_pytest_benchmark()

# # Missing
# # rceps
# # cceps_unwrap
# # cceps

# class TestAcoustics:
#     @pytest.mark.benchmark(group="Rceps")
#     class TestRceps:
#         def cpu_version(self, cpu_sig):
#             return signal.rceps(cpu_sig)

#         @pytest.mark.cpu
#         def test_rceps_cpu(self, benchmark):
#             benchmark(self.cpu_version, cpu_sig)

#         def test_rceps_gpu(self, gpubenchmark):

#             output = gpubenchmark(cusignal.detrend, gpu_sig)

#             key = self.cpu_version(cpu_sig)
#             assert array_equal(cp.asnumpy(output), key)

#     @pytest.mark.benchmark(group="CcepsUnwrap")
#     class TestCcepsUnwrap:
#         def cpu_version(self, cpu_sig):
#             return signal.freq_shift(cpu_sig)

#         @pytest.mark.cpu
#         def test_cceps_unwrap_cpu(self, benchmark):
#             benchmark(self.cpu_version, cpu_sig)

#         def test_cceps_unwrap_gpu(self, gpubenchmark):

#             output = gpubenchmark(cusignal.detrend, gpu_sig)

#             key = self.cpu_version(cpu_sig)
#             assert array_equal(cp.asnumpy(output), key)

#     @pytest.mark.benchmark(group="Cceps")
#     class TestCceps:
#         def cpu_version(self, cpu_sig):
#             return signal.freq_shift(cpu_sig)

#         @pytest.mark.cpu
#         def test_cceps_cpu(self, benchmark):
#             benchmark(self.cpu_version, cpu_sig)

#         def test_cceps_gpu(self, gpubenchmark):

#             output = gpubenchmark(cusignal.detrend, gpu_sig)

#             key = self.cpu_version(cpu_sig)
#             assert array_equal(cp.asnumpy(output), key)

reason: import numpy,from scipy,import cupy