text:
                                        # UNET++ for better resoulation  against ground trouth

import torch
import torch.nn as nn
import torch.nn.functional as F
class conv_block_nested(nn.Module):

    def __init__(self, in_ch, mid_ch, out_ch):
        super(conv_block_nested, self).__init__()
        self.activation = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv2d(in_ch, mid_ch, kernel_size=3, padding=1, bias=True)
        self.bn1 = nn.BatchNorm2d(mid_ch)
        self.conv2 = nn.Conv2d(mid_ch, out_ch, kernel_size=3, padding=1, bias=True)
        self.bn2 = nn.BatchNorm2d(out_ch)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.activation(x)

        x = self.conv2(x)
        x = self.bn2(x)
        output = self.activation(x)

        return output

class Nested_UNet(nn.Module):

    def __init__(self, in_ch=3, out_ch=3):
        super(Nested_UNet, self).__init__()

        n1 = 64
        filters = [n1, n1 * 2, n1 * 4]

        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.Up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)

        self.conv0_0 = conv_block_nested(in_ch, filters[0], filters[0])
        self.conv1_0 = conv_block_nested(filters[0], filters[1], filters[1])

        self.conv0_1 = conv_block_nested(filters[0] + filters[1], filters[0], filters[0])
        self.conv1_1 = conv_block_nested(filters[1] + filters[2], filters[1], filters[1])

        self.conv0_2 = conv_block_nested(filters[0]*2 + filters[1], filters[0], filters[0])
        self.conv1_2 = conv_block_nested(filters[1]*2 + filters[2], filters[1], filters[1])

        self.conv0_3 = conv_block_nested(filters[0]*3 + filters[1], filters[0], filters[0])
        self.conv1_3 = conv_block_nested(filters[1]*3 + filters[2], filters[1], filters[1])

        self.final = nn.Conv2d(filters[0], out_ch, kernel_size=1)

    def forward(self, x, out_shape=None):

        x0_0 = self.conv0_0(x)
        x1_0 = self.conv1_0(self.pool(x0_0))
        x0_1 = self.conv0_1(torch.cat([x0_0, self.Up(x1_0)], 1))

        output = self.final(x0_1)
        if out_shape is not None:
            return F.interpolate(output, size=out_shape, mode='bilinear', align_corners=True)
        else:
            return output.permute(0, 2, 3, 1)

                                        # UNET
        import torch
        import torch.nn as nn
        import torch.nn.functional as F

        def double_conv(in_channels, out_channels):
            return nn.Sequential(
                nn.Conv2d(in_channels, out_channels, 3, padding=1),
                nn.ReLU(inplace=True),
                nn.Conv2d(out_channels, out_channels, 3, padding=1),
                nn.ReLU(inplace=True)
            )

        class UNet(nn.Module):

            def __init__(self, n_class=10):
                super().__init__()

                self.dconv_down1 = double_conv(3, 64)
                self.dconv_down2 = double_conv(64, 128)
                self.dconv_down3 = double_conv(128, 256)
                self.dconv_down4 = double_conv(256, 512)
                self.dconv_down5 = double_conv(512, 1024)

                self.maxpool = nn.MaxPool2d(2)
                self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)

                self.dconv_up4 = double_conv(512 + 1024, 512)
                self.dconv_up3 = double_conv(256 + 512, 256)
                self.dconv_up2 = double_conv(128 + 256, 128)
                self.dconv_up1 = double_conv(128 + 64, 64)

                self.conv_last = nn.Conv2d(64, n_class, 1)

            def forward(self, x):
                conv1 = self.dconv_down1(x)
                x = self.maxpool(conv1)

                conv2 = self.dconv_down2(x)
                x = self.maxpool(conv2)

                conv3 = self.dconv_down3(x)
                x = self.maxpool(conv3)

                conv4 = self.dconv_down4(x)
                x = self.maxpool(conv4)

                x = self.dconv_down5(x)

                x = self.upsample(x)
                x = torch.cat([x, conv3], dim=1)

                x = self.dconv_up4(x)
                x = self.upsample(x)
                x = torch.cat([x, conv3], dim=1)

                x = self.dconv_up3(x)
                x = self.upsample(x)
                x = torch.cat([x, conv2], dim=1)

                x = self.dconv_up2(x)
                x = self.upsample(x)
                x = torch.cat([x, conv1], dim=1)

                x = self.dconv_up1(x)

                out = self.conv_last(x)

                return out

              ############3UNET

                class down(nn.Module):
                    def __init__(self, in_ch, out_ch):
                        super(down, self).__init__()
                        self.mpconv = nn.Sequential(
                            nn.MaxPool2d(2),
                            double_conv(in_ch, out_ch)
                        )

                    def forward(self, x):
                        x = self.mpconv(x)
                        return x

                class up(nn.Module):
                    def __init__(self, in_ch, out_ch, bilinear=True):
                        super(up, self).__init__()
                        self.bilinear = bilinear

                        self.up = nn.ConvTranspose2d(in_ch // 2, in_ch // 2, 2, stride=2)

                        self.conv = double_conv(in_ch, out_ch)

                    def forward(self, x1, x2):
                        if self.bilinear:
                            x1 = F.interpolate(x1, scale_factor=2, mode='bilinear', align_corners=True)
                        else:
                            x1 = self.up(x1)

                        # input is CHW
                        diffY = x2.size()[2] - x1.size()[2]
                        diffX = x2.size()[3] - x1.size()[3]

                        x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
                                        diffY // 2, diffY - diffY // 2])

                        # for padding issues, see
                        # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
                        # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd

                        x = torch.cat([x2, x1], dim=1)
                        x = self.conv(x)
                        return x

                class outconv(nn.Module):
                    def __init__(self, in_ch, out_ch):
                        super(outconv, self).__init__()
                        self.conv = nn.Conv2d(in_ch, out_ch, 1)

                    def forward(self, x):
                        x = self.conv(x)
                        return x

                class double_conv(nn.Module):
                    '''(conv => BN => ReLU) * 2'''

                    def __init__(self, in_ch, out_ch):
                        super(double_conv, self).__init__()
                        self.conv = nn.Sequential(
                            nn.Conv2d(in_ch, out_ch, 3, padding=1),
                            nn.BatchNorm2d(out_ch),
                            nn.ReLU(inplace=True),
                            nn.Conv2d(out_ch, out_ch, 3, padding=1),
                            nn.BatchNorm2d(out_ch),
                            nn.ReLU(inplace=True)
                        )

                    def forward(self, x):
                        x = self.conv(x)
                        return x

                class inconv(nn.Module):
                    def __init__(self, in_ch, out_ch):
                        super(inconv, self).__init__()
                        self.conv = double_conv(in_ch, out_ch)

                    def forward(self, x):
                        x = self.conv(x)
                        return x

                class UNet(nn.Module):
                    def __init__(self, classes=12):
                        super(UNet, self).__init__()
                        self.inc = inconv(3, 64)
                        self.down1 = down(64, 128)
                        self.down2 = down(128, 256)
                        self.down3 = down(256, 512)
                        self.down4 = down(512, 1024)

                        self.up1 = up(512 + 1024, 512)
                        self.up2 = up(256 + 512, 256)
                        self.up3 = up(128 + 256, 128)
                        self.up4 = up(128 + 64, 3)

                        self.outc = outconv(3, classes)

                    def forward(self, x):
                        x1 = self.inc(x)
                        x2 = self.down1(x1)
                        x3 = self.down2(x2)
                        x4 = self.down3(x3)
                        x5 = self.down4(x4)
                        x = self.up1(x5, x4)
                        x = self.up2(x, x3)
                        x = self.up3(x, x2)
                        x = self.up4(x, x1)
                        x = self.outc(x)
                        # return F.sigmoid(x)

                        return x

                    ###44444

import torch
import torch.nn as nn
import torch.nn.functional as F

class DoubleConv(nn.Module):
    """(convolution => [BN] => ReLU) * 2"""

    def __init__(self, in_channels, out_channels, mid_channels=None):
        super().__init__()
        if not mid_channels:
            mid_channels = out_channels
        self.double_conv = nn.Sequential(
            nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
            nn.BatchNorm2d(mid_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        return self.double_conv(x)

class Down(nn.Module):
    """Downscaling with maxpool then double conv"""

    def __init__(self, in_channels, out_channels):
        super().__init__()
        self.maxpool_conv = nn.Sequential(
            nn.MaxPool2d(2),
            DoubleConv(in_channels, out_channels)
        )

    def forward(self, x):
        return self.maxpool_conv(x)

class Up(nn.Module):
    """Upscaling then double conv"""

    def __init__(self, in_channels, out_channels, bilinear=True):
        super().__init__()

        # if bilinear, use the normal convolutions to reduce the number of channels
        if bilinear:
            self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
            self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
        else:
            self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
            self.conv = DoubleConv(in_channels, out_channels)

    def forward(self, x1, x2):
        x1 = self.up(x1)
        # input is CHW
        diffY = x2.size()[2] - x1.size()[2]
        diffX = x2.size()[3] - x1.size()[3]

        x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
                        diffY // 2, diffY - diffY // 2])
        # if you have padding issues, see
        # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
        # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
        x = torch.cat([x2, x1], dim=1)
        return self.conv(x)

class OutConv(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(OutConv, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)

    def forward(self, x):
        return self.conv(x)

class UNet(nn.Module):
    def __init__(self,
                 n_channels=1,
                 n_classes=10,
                 bilinear=True):
        super(UNet, self).__init__()
        self.n_channels = n_channels
        self.n_classes = n_classes
        self.bilinear = bilinear

        self.inc = DoubleConv(n_channels, 64)
        self.down1 = Down(64, 128)
        self.down2 = Down(128, 256)
        self.down3 = Down(256, 512)
        factor = 2 if bilinear else 1
        self.down4 = Down(512, 1024 // factor)
        self.up1 = Up(1024, 512 // factor, bilinear)
        self.up2 = Up(512, 256 // factor, bilinear)
        self.up3 = Up(256, 128 // factor, bilinear)
        self.up4 = Up(128, 64, bilinear)
        self.outc = OutConv(64, n_classes)

    def forward(self, x, out_shape=None):
        #if out_shape is not None:
         #   return x.permute(0, 3, 1, 2)
        x1 = self.inc(x)
        x2 = self.down1(x1)
        x3 = self.down2(x2)
        x4 = self.down3(x3)
        x5 = self.down4(x4)
        x = self.up1(x5, x4)
        x = self.up2(x, x3)
        x = self.up3(x, x2)
        x = self.up4(x, x1)
        logits = self.outc(x)
        return logits

    #### Working model unet
    import torch
    import torch.nn as nn
    import torch.nn.functional as F

    class inconv(nn.Module):
        def __init__(self, in_ch, out_ch):
            super(inconv, self).__init__()
            self.conv = double_conv(in_ch, out_ch)

        def forward(self, x):
            x = self.conv(x)
            return x

    class double_conv(nn.Module):
        '''(conv => BN => ReLU) * 2'''

        def __init__(self, in_ch, out_ch):
            super(double_conv, self).__init__()
            self.conv = nn.Sequential(
                nn.Conv2d(in_ch, out_ch, 3, padding=1),
                nn.BatchNorm2d(out_ch),
                nn.ReLU(inplace=True),
                nn.Conv2d(out_ch, out_ch, 3, padding=1),
                nn.BatchNorm2d(out_ch),
                nn.ReLU(inplace=True)
            )

        def forward(self, x):
            x = self.conv(x)
            return x

    class down(nn.Module):
        def __init__(self, in_ch, out_ch):
            super(down, self).__init__()
            self.mpconv = nn.Sequential(
                nn.MaxPool2d(2),
                double_conv(in_ch, out_ch)
            )

        def forward(self, x):
            x = self.mpconv(x)
            return x

    # Upsampling/ Unpooling/ Deconvolution/ Transpose Convolution, followed by two conv layers
    class up(nn.Module):
        def __init__(self, in_ch, out_ch, bilinear=True):
            super(up, self).__init__()

            #  would be a nice idea if the upsampling could be learned too,
            #  but my machine do not have enough memory to handle all those weights
            if bilinear:
                self.up = nn.Upsample(
                    scale_factor=2, mode='bilinear', align_corners=True)
            else:
                self.up = nn.ConvTranspose2d(in_ch // 2, in_ch // 2, 2, stride=2)

            self.conv = double_conv(in_ch, out_ch)

        def forward(self, x1, x2):
            x1 = self.up(x1)

            # input is CHW
            diffY = x2.size()[2] - x1.size()[2]
            diffX = x2.size()[3] - x1.size()[3]

            x1 = F.pad(x1, (diffX // 2, diffX - diffX // 2,
                            diffY // 2, diffY - diffY // 2))

            # for padding issues, see
            # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
            # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd

            x = torch.cat([x2, x1], dim=1)
            x = self.conv(x)
            return x

    class outconv(nn.Module):
        def __init__(self, in_ch, out_ch):
            super(outconv, self).__init__()
            self.conv = nn.Conv2d(in_ch, out_ch, 1)

        def forward(self, x):
            x = self.conv(x)
            return x

    class UNet(nn.Module):
        def __init__(self, n_channels=3, n_classes=3):
            super(UNet, self).__init__()
            self.inc = inconv(n_channels, 64)  # First step of Contracting
            self.down1 = down(64, 128)  # Second step of Contracting
            self.down2 = down(128, 256)  # Third step of Contracting
            self.down3 = down(256, 512)  # Fourth step of Contracting
            self.down4 = down(512, 512)  # Bottleneck of U-Net
            self.up1 = up(1024, 256)  # First step of Expanding
            self.up2 = up(512, 128)  # Second step of Expanding
            self.up3 = up(256, 64)  # Third step of Expanding
            self.up4 = up(128, 64)  # Fourth step of Expanding
            # Output Conv layer with 1*1 filter
            self.outc = outconv(64, n_classes)

        def forward(self, x, out_shape=None):
            x1 = self.inc(x)
            x2 = self.down1(x1)
            x3 = self.down2(x2)
            x4 = self.down3(x3)
            x5 = self.down4(x4)
            x = self.up1(x5, x4)
            x = self.up2(x, x3)
            x = self.up3(x2, x)
            x = self.up4(x, x1)
            x = self.outc(x)
            # return F.sigmoid(x)
            if out_shape is not None:
                return F.interpolate(output, size=out_shape, mode='bicubic', align_corners=True)
            else:
                return x.permute(0, 2, 3, 1)

            import torch
            import torch.nn as nn
            import torch.nn.functional as F

            class Conv3x3GNReLU(nn.Module):
                def __init__(self, in_channels, out_channels, upsample=False):
                    super().__init__()
                    self.upsample = upsample
                    self.block = nn.Sequential(
                        nn.Conv2d(
                            in_channels, out_channels, (3, 3), stride=1, padding=1, bias=False
                        ),
                        nn.GroupNorm(32, out_channels),
                        nn.ReLU(inplace=True),
                    )

                def forward(self, x):
                    x = self.block(x)
                    if self.upsample:
                        x = F.interpolate(x, scale_factor=2, mode="bilinear", align_corners=True)
                    return x

            class FPNBlock(nn.Module):
                def __init__(self, pyramid_channels, skip_channels):
                    super().__init__()
                    self.skip_conv = nn.Conv2d(skip_channels, pyramid_channels, kernel_size=1)

                def forward(self, x, skip=None):
                    x = F.interpolate(x, scale_factor=2, mode="nearest")
                    skip = self.skip_conv(skip)
                    x = x + skip
                    return x

            class SegmentationBlock(nn.Module):
                def __init__(self, in_channels, out_channels, n_upsamples=0):
                    super().__init__()

                    blocks = [Conv3x3GNReLU(in_channels, out_channels, upsample=bool(n_upsamples))]

                    if n_upsamples > 1:
                        for _ in range(1, n_upsamples):
                            blocks.append(Conv3x3GNReLU(out_channels, out_channels, upsample=True))

                    self.block = nn.Sequential(*blocks)

                def forward(self, x):
                    return self.block(x)

            class MergeBlock(nn.Module):
                def __init__(self, policy):
                    super().__init__()
                    if policy not in ["add", "cat"]:
                        raise ValueError(
                            "`merge_policy` must be one of: ['add', 'cat'], got {}".format(
                                policy
                            )
                        )
                    self.policy = policy

                def forward(self, x):
                    if self.policy == 'add':
                        return sum(x)
                    elif self.policy == 'cat':
                        return torch.cat(x, dim=1)
                    else:
                        raise ValueError(
                            "`merge_policy` must be one of: ['add', 'cat'], got {}".format(self.policy)
                        )

            class FPNDecoder(nn.Module):
                def __init__(
                        self,
                        encoder_channels=3,
                        encoder_depth=5,
                        pyramid_channels=256,
                        segmentation_channels=128,
                        dropout=0.2,
                        merge_policy="add",
                ):
                    super().__init__()

                    self.out_channels = segmentation_channels if merge_policy == "add" else segmentation_channels * 4
                    if encoder_depth < 3:
                        raise ValueError(
                            "Encoder depth for FPN decoder cannot be less than 3, got {}.".format(encoder_depth))

                    encoder_channels = encoder_channels
                    # encoder_channels = encoder_channels

                    self.p5 = nn.Conv2d(encoder_channels, pyramid_channels, kernel_size=1)
                    self.p4 = FPNBlock(pyramid_channels, encoder_channels)
                    self.p3 = FPNBlock(pyramid_channels, encoder_channels)
                    self.p2 = FPNBlock(pyramid_channels, encoder_channels)

                    self.seg_blocks = nn.ModuleList([
                        SegmentationBlock(pyramid_channels, segmentation_channels, n_upsamples=n_upsamples)
                        for n_upsamples in [3, 2, 1, 0]
                    ])

                    self.merge = MergeBlock(merge_policy)
                    self.dropout = nn.Dropout2d(p=dropout, inplace=True)

                def forward(self, features):
                    c2, c3, c4, c5 = features

                    p5 = self.p5(c5)
                    p4 = self.p4(p5, c4)
                    p3 = self.p3(p4, c3)
                    p2 = self.p2(p3, c2)

                    feature_pyramid = [seg_block(p) for seg_block, p in zip(self.seg_blocks, [p5, p4, p3, p2])]
                    x = self.merge(feature_pyramid)
                    x = self.dropout(x)

                    return x.permute(3, 2, 1, 0)

            #####

import torch
import torch.nn as nn
from collagen.core import Module
from collagen.modelzoo.segmentation import backbones
from collagen.modelzoo.segmentation import constants
from collagen.modelzoo.segmentation import decoders

class EncoderDecoder(Module):
    def __init__(self, n_outputs, backbone: str or Module, decoder: str or Module,
                 decoder_normalization='BN', spatial_dropout=None, bayesian_dropout=None,
                 unet_activation='relu', unet_width=32):
        super(EncoderDecoder, self).__init__()
        if isinstance(backbone, str):
            if backbone in constants.allowed_encoders:
                if 'resnet' in backbone:
                    backbone = backbones.ResNetBackbone(backbone, dropout=bayesian_dropout)
                else:
                    ValueError('Cannot find the implementation of the backbone!')
            else:
                raise ValueError('This backbone name is not in the list of allowed backbones!')

        if isinstance(decoder, str):
            if decoder in constants.allowed_decoders:
                if decoder == 'FPN':
                    decoder = decoders.FPNDecoder(encoder_channels=backbone.output_shapes,
                                                  pyramid_channels=256, segmentation_channels=128,
                                                  final_channels=n_outputs, spatial_dropout=spatial_dropout,
                                                  normalization=decoder_normalization,
                                                  bayesian_dropout=bayesian_dropout)
                elif decoder == 'UNet':
                    decoder = decoders.UNetDecoder(encoder_channels=backbone.output_shapes,
                                                   width=unet_width, activation=unet_activation,
                                                   final_channels=n_outputs,
                                                   spatial_dropout=spatial_dropout,
                                                   normalization=decoder_normalization)

        decoder.initialize()

        self.backbone = backbone
        self.decoder = decoder

    def forward(self, x):
        features = self.backbone(x)
        return self.decoder(features)

    def switch_dropout(self):
        """
        Has effect only if the model supports monte-carlo dropout inference.

        """
        self.backbone.switch_dropout()
        self.decoder.switch_dropout()

    def get_features(self):
        pass

    def get_features_by_name(self, name: str):
        pass

    class SimpleNet(nn.Module):
        def __init__(self,
                     num_classes=10,
                     final_channels=3):
            super(SimpleNet, self).__init__()

            self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=3, stride=1, padding=1)
            self.relu1 = nn.ReLU()

            self.conv2 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3, stride=1, padding=1)
            self.relu2 = nn.ReLU()

            # self.pool = nn.MaxPool2d(kernel_size=2)

            self.conv3 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=3, stride=1, padding=1)
            self.relu3 = nn.ReLU()

            self.conv4 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=3, stride=1, padding=1)
            self.relu4 = nn.ReLU()

            self.final_layer = nn.Conv2d(in_channels=24, out_channels=final_channels, kernel_size=3, stride=1,
                                         padding=1)

            self.fc = nn.Linear(in_features=16 * 16 * 24, out_features=num_classes)

        def forward(self, input, out_shape=1024):
            output = self.conv1(input)
            output = self.relu1(output)

            output = self.conv2(output)
            output = self.relu2(output)

            #    output = self.pool(output)  # Max pooling

            output = self.conv3(output)
            output = self.relu3(output)

            output = self.conv4(output)
            output = self.relu4(output)

            output = F.interpolate(output, scale_factor=2, mode='bilinear', align_corners=True)  # Could be 'bilinear'

            output = self.final_layer(output)

            # output = output.view(-1, 16 * 16 * 24)

            # output = self.fc(output)
            if out_shape is not None:
                return F.interpolate(output, size=out_shape, mode='bilinear', align_corners=True)
            # else:
            # return output
            x = (10, 20, 30, 40, 50)
            for var in x:
                print("index " + str(x.index(var)) + ":", var)
fname_01=(1,3,2,2,4,5,5,9)
mask_fname_01=(1,3,2,2,4,5,5,9, 3, 4, 6)
df_frame = {'fname': [], 'mask_fname': []}
for i in range(fname_01):
    df_frame[i]= fname_01[i]

    images_repeat = []
    for i in range(len(images)):
       hmds_masks = glob(str(masks_loc) + '/*' + str(images[i])[:-13] + '**/*.png', recursive=True)
       images_repeat.extend(list(np.repeat(images[i], len(hmds_masks) // 60 + len(hmds_masks) % 60)))
       '''
       images_repeat = []
        for i in range(len(images)):
           hmds_masks = glob(str(masks_loc) + '/*' + str(images[i])[:-13] + '**/*.png', recursive=True)
           #if '6068-4L_00000039.png' in str(images[i]):
            #   images_repeat.extend(list(np.repeat(images[i], len(hmds_masks))))
        #   if '6069-4L' in str(images[i]):
         #      images_repeat.extend(list(np.repeat(images[i], len(hmds_masks))))
           if i in str(images[i]):
               images_repeat.extend(list(np.repeat(images[i], len(hmds_masks)*19)))
          # elif'.png' in str(images[i]):
           #     images_repeat.append(list(np.repeat(images[i], len(hmds_masks)//2 + len(hmds_masks) % 2)))

           # elif 'a.mat.png' in str(masks[i]):
            #    masks_repeat.extend(list(np.repeat(masks[i], len(hmds_images) // 2)))
            #elif '5928-4L_00000040.png' in str(masks[i]):
             #   masks_repeat.extend(list(np.repeat(images[i], len(masks))))
         '''

       def parse_grayscale(root, entry, transform, data_key, target_key, mean=False):
           # TODO make sure that this is working
           # Image and mask generation
           img = cv2.imread(str(entry.fname))
           img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
           # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
           # img = np.expand_dims(img, 1)
           img = np.transpose(img, (1, 0, 2))

           mask = cv2.imread(str(entry.mask_fname))
           # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
           # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

           try:
               mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
               mask[1, :, :] = mask[0, :, :]
               mask[2, :, :] = mask[0, :, :]
               mask = np.transpose(mask, (1, 0, 2))
           except Exception:
               print(str(entry.mask_fname))
           # mask = np.expand_dims(mask, 0)

           if img.shape[0] != mask.shape[0]:
               img = cv2.resize(img, (mask.shape[1], mask.shape[0]))
           elif img.shape[1] != mask.shape[1]:
               mask = mask[:, :img.shape[1]]

           # img = np.expand_dims(img, 1)
           # img=np.transpose(img,(0,2,1))
           img, mask = transform((img, mask))
           img = img.permute(2, 0, 1)  # / 255.  # img.shape[0] is the color channel after permute
           mask = mask.permute(2, 0, 1)
           # mask = ndimage.zoom(mask, 1.0)
           # img = ndimage.zoom(img, 1.0)

           # plt.imshow(mask[0,:])
           # plt.show()

           # plt.imshow(img[0,:])
           # plt.show()

           # Debugging
           # plt.imshow(np.asarray(img).transpose((1, 2, 0)))
           # plt.imshow(np.asarray(mask).squeeze(), alpha=0.3)
           # plt.show()

           # Calculate mean profile
           if mean:
               masks_repeat = []
               for i in range(len(masks)):
                   hmds_images = glob(str(images_loc) + '/*' + str(masks[i])[:-9] + '/*.png', recursive=True)
                   if '6061-12La.mat.png' in str(masks[i]):
                       masks_repeat.extend(list(np.repeat(masks[i], len(hmds_images))))
                   elif 'a.mat.png' in str(masks[i]):
                       masks_repeat.extend(list(np.repeat(masks[i], len(hmds_images) // 2)))
                   elif 'b.mat.png' in str(masks[i]):
                       masks_repeat.extend(list(np.repeat(masks[i], len(hmds_images) // 2 + len(hmds_images) % 2)))
                   else:
                       raise Exception('Wrong file name for the PLM image')

                   fig = plt.figure()
                   plot = fig.add_subplot(1, 3, 1)
                   plot.set_title('Initial Weights')
                   imgplot = plt.imshow(init_weights.view(-1, 280, 280).cpu()[0], cmap='gray')

                   plot = fig.add_subplot(1, 3, 2)
                   plot.set_title('Trained Weights')
                   imgplot = plt.imshow(trained_weights.view(-1, 280, 280).cpu()[0], cmap='gray')

                   plot = fig.add_subplot(1, 3, 3)
                   plot.set_title('Difference Weights')

import cv2
import numpy as np
import gc
import os
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from pathlib import Path
import argparse
import dill
#from torch2trt import torch2trt
import torch
import torch.nn as nn
import yaml
from time import sleep, time
from tqdm import tqdm
from glob import glob
#from collagen.modelzoo.segmentation import EncoderDecoder
#from hmdscollagen.training.models import SimpleNet
from hmdscollagen.training.SimpleConvNet import SimpleConvNet

from collagen.core.utils import auto_detect_device

from hmdscollagen.data.utilities import load, save, print_orthogonal
from hmdscollagen.data.visualizations import render_volume

from pytorch_toolbelt.inference.tiles import ImageSlicer, CudaTileMerger
from pytorch_toolbelt.utils.torch_utils import tensor_from_rgb_image, to_numpy

cv2.ocl.setUseOpenCL(False)
cv2.setNumThreads(0)

class InferenceModel(nn.Module):
    def __init__(self, models_list):
        super(InferenceModel, self).__init__()
        self.n_folds = len(models_list)
        modules = {}
        for idx, m in enumerate(models_list):
            modules[f'fold_{idx}'] = m

        self.__dict__['_modules'] = modules

    def forward(self, x):
        res = 0
        for idx in range(self.n_folds):
            fold = self.__dict__['_modules'][f'fold_{idx}']
            #res += torch2trt(fold, [x]).sigmoid()
            res += fold(x).sigmoid()

        return res / self.n_folds

def inference(inference_model, img_full, device='cuda'):
    x, y, ch = img_full.shape

    input_x = config['training']['crop_size'][0]
    input_y = config['training']['crop_size'][1]

    # Cut large image into overlapping tiles
    tiler = ImageSlicer(img_full.shape, tile_size=(input_x, input_y),
                        tile_step=(input_x // 2, input_y // 2), weight=args.weight)

    # HCW -> CHW. Optionally, do normalization here
    tiles = [tensor_from_rgb_image(tile) for tile in tiler.split(img_full)]

    # Allocate a CUDA buffer for holding entire mask
    merger = CudaTileMerger(tiler.target_shape, channels=1, weight=tiler.weight)

    # Run predictions for tiles and accumulate them
    for tiles_batch, coords_batch in DataLoader(list(zip(tiles, tiler.crops)), batch_size=args.bs, pin_memory=True):
        # Move tile to GPU
        tiles_batch = (tiles_batch.float() / 255.).to(device)
        # Predict and move back to CPU
        pred_batch = inference_model(tiles_batch)

        # Merge on GPU
        merger.integrate_batch(pred_batch, coords_batch)

        # Plot
        if args.plot:
            for i in range(args.bs):
                if args.bs != 1:
                    plt.imshow(pred_batch.cpu().detach().numpy().astype('float32').squeeze()[i, :, :])
                else:
                    plt.imshow(pred_batch.cpu().detach().numpy().astype('float32').squeeze())
                plt.show()

    # Normalize accumulated mask and convert back to numpy
    merged_mask = np.moveaxis(to_numpy(merger.merge()), 0, -1).astype('float32')
    merged_mask = tiler.crop_to_orignal_size(merged_mask)
    # Plot
    if args.plot:
        for i in range(args.bs):
            if args.bs != 1:
                plt.imshow(merged_mask)
            else:
                plt.imshow(merged_mask.squeeze())
            plt.show()

    torch.cuda.empty_cache()
    gc.collect()

    return merged_mask.squeeze()

if __name__ == "__main__":
    start = time()

    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset_root', type=Path, default='/data/Repositories/HMDS_orientation/Data/test/hmds_test_flip/')
    #parser.add_argument('--dataset_root', type=Path, default='/media/santeri/Transcend1/Full samples/')
    parser.add_argument('--save_dir', type=Path, default='/data/Repositories/HMDS_collagen/workdir/')
    parser.add_argument('--subdir', type=Path, choices=['NN_prediction', ''], default='')
    #parser.add_argument('--dataset_root', type=Path, default='../../../Data/µCT/images')
    #parser.add_argument('--save_dir', type=Path, default='/data/Repositories/HMDS_collagen/workdir/')
    parser.add_argument('--bs', type=int, default=300)
    parser.add_argument('--plot', type=bool, default=False)
    parser.add_argument('--weight', type=str, choices=['pyramid', 'mean'], default='mean')
    parser.add_argument('--completed', type=int, default=13)
    parser.add_argument('--snapshot', type=Path,
                        default='/data/Repositories/HMDS_collagen/workdir/snapshots/mipt-stud-dl-b_2020_06_03_07_41_22/')
    parser.add_argument('--dtype', type=str, choices=['.bmp', '.png', '.tif'], default='.png')
    args = parser.parse_args()
    subdir = ''  # 'NN_prediction'

    # Load snapshot configuration
    with open(args.snapshot / 'config.yml', 'r') as f:
        config = yaml.load(f, Loader=yaml.Loader)

    with open(args.snapshot / 'args.dill', 'rb') as f:
        args_experiment = dill.load(f)

   # with open(args.snapshot / 'split_config.dill', 'rb') as f:
    #    split_config = dill.load(f)
    args.save_dir.mkdir(exist_ok=True)

    # Load models
    models = glob(str(args.snapshot) + '/*.pth')
    #models = glob(str(args.snapshot) + '/*fold_3_*.pth')
    models.sort()
    #device = auto_detect_device()
    device = 'cuda'  # Use the second GPU for inference

    # List the models
    model_list = []
    for fold in range(len(models)):
        #model = EncoderDecoder(**config['model'])
        model = SimpleConvNet().to(device)
        model.load_state_dict(torch.load(models[fold]))
        model_list.append(model)

    model = InferenceModel(model_list).to(device)
    #if torch.cuda.device_count() > 1:  # Multi-GPU
    #    model = nn.DataParallel(model).to(device)
    model.eval()

    threshold = 0.5 if config['training']['log_jaccard'] is False else 0.3  # Set probability threshold
    print(f'Found {len(model_list)} models.')

    # Load samples
    # samples = [os.path.basename(x) for x in glob(str(args.dataset_root / '*XZ'))]  # Load with specific name
    samples = os.listdir(args.dataset_root)
    samples.sort()
    # samples = [samples[id] for id in [7, 11]]  # Get intended samples from list

    # Skip the completed samples
   # if args.completed > 0:
    #    samples = samples[args.completed:]
    for idx, sample in enumerate(samples):
        #try:
        print(f'==> Processing sample {idx + 1} of {len(samples)}: {sample}')

        # Load image stacks
        data_xz, files = load(str(args.dataset_root / sample), rgb=True)
        data_xz = np.transpose(data_xz, (0, 1, 2,3)) # X-Z-Y-Ch
        data_yz = np.transpose(data_xz, (1, 2, 0, 3))  # Y-Z-X-Ch

        data_xz = data_xz[:, :, :, 0]
        data_xz = np.expand_dims(data_xz, axis=3)

        data_yz = data_yz[:, :, :, 0]
        data_yz = np.expand_dims(data_yz, 3)

        mask_xz = np.zeros(data_xz.shape)
        mask_yz = np.zeros(data_yz.shape)
        #mask_lz=(np.transpose(mask_yz, (0, 2, 1, 3))/2)

        # Loop for image slices
        # 1st orientation
        with torch.no_grad():  # Do not update gradients
            for slice_idx in tqdm(range(data_xz.shape[2]), desc='Running inference, XZ'):
                mask_xz[:, :, slice_idx, 0] = inference(model, data_xz[:, :, slice_idx, :])

                #plt.imshow(mask_xz[:, :,:,:])
                #plt.show()

            # 2nd orientation
           # for slice_idx in tqdm(range(data_yz.shape[2]), desc='Running inference, YZ'):
            #    mask_yz[:, :, slice_idx, 0] = inference(model, data_yz[:, :, slice_idx, :])

        # Average probability maps
        #mask_final = ((mask_xz + np.transpose(mask_yz, (0, 2, 1))) / 2) >= threshold
       # mask_final = ((mask_xz + np.transpose(mask_yz, (0, 2, 1, 3))) / 2)*255
        #k=np.transpose(mask_yz, (1,0, 2, 3))
        #mask_final = (mask_xz + np.transpose(mask_yz, (1,0, 2, 3)) / 2) * 255
       # mask_xz = list()
        #mask_yz = list()
        #data_xz = list()

        # Convert to original orientation
        #mask_final = np.transpose(mask_final, (0, 1, 2)).astype('uint8')#*255
        #mask_final = np.transpose(mask_final, (0, 2, 1))

        # Save predicted full mask
        if str(args.subdir) != '.':  # Save in original location
            save(str(args.dataset_root / sample / subdir), files, (mask_xz * 255).astype(np.uint16), dtype=args.dtype)
        else:  # Save in new location
            save(str(args.save_dir / sample), files, (mask_xz * 255).astype(np.uint16), dtype=args.dtype)

       # render_volume(data_yz[:, :, 0] * mask_final,
        #              savepath=str(args.save_dir / 'visualizations' / (sample + '_render' + args.dtype)),
         #             white=True, use_outline=False)

        print_orthogonal((mask_xz.squeeze() * 255).astype(np.uint16), invert=False, res=3.2, title=None, cbar=True,
                         savepath=str(args.save_dir / 'visualizations' / (sample + '_prediction.png')),
                         scale_factor=1000)
        #except Exception as e:
        #    print(f'Sample {sample} failed due to error:\n\n {e}\n\n.')
        #    continue
    dur = time() - start
    print(f'Inference completed in {dur // 3600} hours, {(dur % 3600) // 60} minutes, {dur % 60} seconds.')


text:
"""
    Unit test for Models
"""

# ---------------------------IMPORTS------------------------------------------

# Internals
from symupy.func import dynamic_2nd, dynamic_3rd, VehDynamic, Vehicle
from symupy.func import VehParameter, SimParameter

# Utils
import unittest
import numpy as np
from numpy.testing import assert_almost_equal

# ---------------------------CONSTANTS----------------------------------------

# Create a simulation timings
T_STP = 0.01
T_HOR = 0.5
T_SIM = 60

sim_par = SimParameter(T_STP, T_HOR, T_SIM)

# Create a vehicle model / Provided originally by the simulator
U_FFS = 25.0
K_X = 0.16
W_CGT = 6.25
L_VEH = 4.0

S0 = 10.0

veh_par = VehParameter.VehParameterSym(U_FFS, K_X, W_CGT, L_VEH)

# ---------------------------TESTS-------------------------------------------

class TestModel(unittest.TestCase):
    def test_constructor(self):
        """
        Test Vehicle class generation
        """
        # veh_par = VehParameter()
        veh_1 = Vehicle(sim_par, veh_par, dynamic_2nd)
        self.assertTrue(isinstance(veh_1, Vehicle))
        self.assertEqual(veh_1.n_veh, 1)

    def test_consistancy(self):
        """
        consistancy VehDynamic i.e. #inputs = #outputs
        """
        self.assertTrue(True)

    def test_dynamics2nd(self):
        """
        Check outcomes from 2nd order dynamics
        """
        veh_par = VehParameter()
        sim_par = SimParameter()
        self.assertEqual(sim_par.t_stp, 0.01)

        # Matrix dynamics
        A = np.array([[1, 0, sim_par.t_stp], [0, 1, 0], [0, 0, 1]])
        B1 = np.array([[0], [sim_par.t_stp], [-sim_par.t_stp]])
        B2 = np.array([[0], [0], [sim_par.t_stp]])

        # invariant
        veh_cst = np.array([0.0, 25.0, 0])
        veh_nif = np.array([0])
        veh_ctr = np.array([0])
        veh_ust = dynamic_2nd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

        # change
        veh_ctr = np.array([0.01])
        veh_ust = dynamic_2nd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

        # propagation from veh_ust
        veh_cst = veh_ust
        veh_ust = dynamic_2nd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

    def test_dynamics3rd(self):
        """
        Check outcomes from 3rd order dynamics
        """
        veh_par = VehParameter()
        sim_par = SimParameter()
        self.assertEqual(sim_par.t_stp, 0.01)

        # Matrix dynamics
        A = np.array(
            [
                [1, 0, sim_par.t_stp, 0],
                [0, 1, 0, sim_par.t_stp],
                [0, 0, 1, -sim_par.t_stp],
                [0, 0, 0, (1 - sim_par.t_stp / veh_par.v_lag)],
            ]
        )
        B1 = np.array([[0], [0], [0], [sim_par.t_stp / veh_par.v_lag]])
        B2 = np.array([[0], [0], [0], [-sim_par.t_stp / veh_par.v_lag]])

        # invariant
        veh_cst = np.array([0.0, 25.0, 0, 0])
        veh_nif = np.array([0])
        veh_ctr = np.array([0])
        veh_ust = dynamic_3rd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

        # change
        veh_ctr = np.array([0.01])
        veh_ust = dynamic_3rd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

        # propagation
        veh_cst = veh_ust
        veh_ust = dynamic_3rd(veh_cst, veh_nif, veh_ctr, veh_par, sim_par)
        val_ust = A @ veh_cst + B1 @ veh_ctr + B2 @ veh_nif
        assert_almost_equal(veh_ust, val_ust)

if __name__ == "__main__":
    unittest.main()

ext: py

max_stars_repo_name: symuvia/symupy

max_issues_repo_name: licit-lab/symupy

max_forks_repo_name: licit-lab/symupy

reason: import numpy,from numpy


text:
import pprint
import zulip
import sys
import re
import json
import httplib2
import os
import random
import numpy as np
import pickle
import json
import nltk
from tensorflow.keras.models import load_model
from nltk.stem import WordNetLemmatizer

p = pprint.PrettyPrinter()
BOT_MAIL = "covid-vaccine-bot@chirag-jain.zulipchat.com"

lemmatizer = WordNetLemmatizer()
nltk.download("punkt")
nltk.download("wordnet")

nlpModel = load_model("chatbot_model.h5")
intents = json.loads(open("intents.json").read())
words = pickle.load(open("words.pkl", "rb"))
classes = pickle.load(open("classes.pkl", "rb"))

def clean_up_sentence(sentence):
    sentence_words = nltk.word_tokenize(sentence)
    sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
    return sentence_words

# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
    # tokenize the pattern
    sentence_words = clean_up_sentence(sentence)
    # bag of words - matrix of N words, vocabulary matrix
    bag = [0] * len(words)
    for s in sentence_words:
        for i, w in enumerate(words):
            if w == s:
                # assign 1 if current word is in the vocabulary position
                bag[i] = 1
                if show_details:
                    print("found in bag: %s" % w)
    return np.array(bag)

def predict_class(sentence, model):
    # filter out predictions below a threshold
    p = bow(sentence, words, show_details=False)
    res = model.predict(np.array([p]))[0]
    ERROR_THRESHOLD = 0.25
    results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []
    for r in results:
        return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
    return return_list

def getResponse(ints, intents_json):
    tag = ints[0]["intent"]
    list_of_intents = intents_json["intents"]
    for i in list_of_intents:
        if i["tag"] == tag:
            result = random.choice(i["responses"])
            break
    return result

def get_bot_response(msg):
	# checks is a user has given a name, in order to give a personalized feedback
	if msg.startswith('my name is'):
		name = msg[11:]
		ints = predict_class(msg, nlpModel)
		res1 = getResponse(ints, intents)
		res =res1.replace("{n}",name)
	elif msg.startswith('hi my name is'):
		name = msg[14:]
		ints = predict_class(msg, nlpModel)
		res1 = getResponse(ints, intents)
		res =res1.replace("{n}",name)
	#if no name is passed execute normally
	else:
		ints = predict_class(msg, nlpModel)
		res = getResponse(ints, intents)
	return res

class ZulipBot(object):
	def __init__(self):
		self.client = zulip.Client(site="https://chirag-jain.zulipchat.com/api/")
		self.subscribe_all()

	def subscribe_all(self):
		json = self.client.get_streams()["streams"]
		streams = [{"name": stream["name"]} for stream in json]
		self.client.add_subscriptions(streams)

	def process(self, msg):
		content = msg["content"].split()
		sender_email = msg["sender_email"]
		ttype = msg["type"]
		stream_name = msg['display_recipient']
		stream_topic = msg['subject']

		print(content)

		if sender_email == BOT_MAIL:
			return

		print("Sucessfully heard.")

		if content[0].lower() == "covidbot" or content[0] == "@**CovidBot**":
			ip = content[1:]
			ip = " ".join(ip)
			message = get_bot_response(ip)
			print(message)
			self.client.send_message({
				"type": "stream",
				"subject": msg["subject"],
				"to": msg["display_recipient"],
				"content": message
			})

		elif "CovidBot" in content and content[0] != "CovidBot":
			self.client.send_message({
				"type": "stream",
				"subject": msg["subject"],
				"to": msg["display_recipient"],
				"content": "Hey there! :blush:"
				})
		else:
			return

def main():
	bot = ZulipBot()
	bot.client.call_on_each_message(bot.process)

if __name__ == "__main__":
	try:
		main()
	except KeyboardInterrupt:
		print("Thanks for using Covid Bot. Bye!")
		sys.exit(0)

reason: import numpy


text:
import heapq
import itertools
# from numba import jitclass, types
class PriorityQueue:
    def __init__(self):
        self.elements = []
        self.entry_finder = {}
        self.REMOVED = (9999,9999)
        self.counter = itertools.count()
    def empty(self):
        return len(self.elements) == 0
    def inQueue(self, item):
        return item in self.entry_finder
    def add(self, item, priority=0):
        if item in self.entry_finder:
            self.remove(item)
        count = next(self.counter)
        entry = [priority, count, item]
        self.entry_finder[item] = entry
        heapq.heappush(self.elements, entry)
    def remove(self, item):
        entry = self.entry_finder.pop(item)
        entry[-1] = self.REMOVED
    def pop(self):
        priority, count, item = heapq.heappop(self.elements)
        if item is not self.REMOVED:
            del self.entry_finder[item]
            return item
        raise KeyError('pop from an empty priority queue')
        # return heapq.heappop(self.elements)[1]

max_stars_repo_name: LunaDilangalen/gpu-pathfinding

max_issues_repo_name: LunaDilangalen/gpu-pathfinding

max_forks_repo_name: LunaDilangalen/gpu-pathfinding

reason: from numba


text:
import numpy as np
import random
import bisect

class traffic:
    def __init__(self, n=100,max_v=5,density=0.2,slow_down_random_probability=0.1,generate_car=1):
        self.n=n
        self.cars=[]
        self.cars.append({})
        self.cars.append({})
        self.nextcars={}
        self.max_v=max_v
        self.density=density
        self.slow_down_random_probability=slow_down_random_probability
        all_generate_car={1:self.generate_car1,2:self.generate_car2}
        all_generate_car[generate_car]()
        self.printroad()

    def generate_car2(self):
        allcar=range(self.n*2)
        random.shuffle(allcar)
        for i in allcar[:int(self.n*2*self.density)]:
            self.cars[i/100][i%100]=random.randint(0,self.max_v)
        self.current_car_position=[sorted([j for j in self.cars[i]])for i in range(2)]

    def generate_car1(self):
        for i in range(self.n):
            if(i%(self.n/(self.n*self.density))==0):
                self.cars[1][i]=random.randint(0,self.max_v)
        self.current_car_position=[sorted([j for j in self.cars[i]])for i in range(2)]

    # def generate_car(self):
    #     for i in range(self.n):
    #         if(random.random()<self.density):
    #             self.cars[0][i]=random.randint(0,self.max_v)
    #         if(random.random()<self.density):
    #             self.cars[1][i]=random.randint(0,self.max_v)
    #     self.current_car_position=sorted(self.cars.keys())

    def encodecar(self,lane,carposition):
        if carposition<10:
            return str(0)+str(carposition)+','+str(lane)
        else:
            return str(carposition)+','+str(lane)

    def decodecar(self,text):
        info=text.split(',')
        return(int(info[1]),int(info[0]))

    def count_car(self):
        return(sum([len(cars) for cars in self.cars]))

    def flow(self):
        num_site_pass=sum([self.cars[lane][cars] for lane in range(len(self.cars)) for cars in self.cars[lane]])
        if num_site_pass==0:
            return 0
        else:
            return num_site_pass/float(self.n)

    def iteration(self):
        self.current_car_position=[sorted([j for j in self.cars[i]])for i in range(2)]
        encode_car_position=sorted([self.encodecar(i,j) for i in range(2) for j in self.cars[i]])
        for i in encode_car_position[::-1]:
            self.changelane(i)

        encode_car_position=sorted([self.encodecar(i,j) for i in range(2) for j in self.cars[i]])
        for i in encode_car_position[::-1]:
            self.acceleration(i)

        encode_car_position=sorted([self.encodecar(i,j) for i in range(2) for j in self.cars[i]])
        for i in encode_car_position[::-1]:
            self.slowing_down(i)

        encode_car_position=sorted([self.encodecar(i,j) for i in range(2) for j in self.cars[i]])
        for i in encode_car_position[::-1]:
            self.randomization(i)
        copyofroad=self.getroad()

        encode_car_position=sorted([self.encodecar(i,j) for i in range(2) for j in self.cars[i]])

        self.nextcars=[{},{}]
        for i in encode_car_position[::-1]:
            self.car_motion(i)
        self.cars=self.nextcars

        return copyofroad

    def changelane(self,car):
        (lane,carposition)=self.decodecar(car)
        index=self.current_car_position[lane].index(carposition)
        current=self.current_car_position[lane][index%len(self.current_car_position[lane])]
        before=self.current_car_position[lane][(index+1)%len(self.current_car_position[lane])]
        if self.changelane_condition(lane,current,before):
            self.cars[self.otherlane(lane)][current]=self.cars[lane][current]
            self.cars[lane].pop(current)
            self.current_car_position[lane].remove(carposition)
            bisect.insort(self.current_car_position[self.otherlane(lane)],carposition)

    def changelane_condition(self,lane,current,before):
        condition1=(self.cars[lane][current]>self.cars[lane][before])
        condition2=(not current in self.cars[self.otherlane(lane)])
        condition3=(before-current)%self.n<=self.max_v/2
        return (condition1 and condition2 and condition3)

    def otherlane(self,lane):
        return int(not lane)

    def acceleration(self,car):
        (lane,carposition)=self.decodecar(car)
        if(self.cars[lane][carposition]<self.max_v):
            self.cars[lane][carposition]+=1

    def slowing_down(self,car):
        (lane,carposition)=self.decodecar(car)
        index=self.current_car_position[lane].index(carposition)
        current=self.current_car_position[lane][index%len(self.current_car_position[lane])]
        before=self.current_car_position[lane][(index+1)%len(self.current_car_position[lane])]
        if((before-current)%self.n<=self.cars[lane][current] and before!=current):
            self.cars[lane][current]=(before-current)%self.n-1

    def randomization(self,car):
        (lane,carposition)=self.decodecar(car)
        if(random.random()<self.slow_down_random_probability):
            if(not self.cars[lane][carposition]==0):
                self.cars[lane][carposition]-=1

    def car_motion(self,car):
        (lane,carposition)=self.decodecar(car)
        velocity=self.cars[lane][carposition]
        nextposition=(velocity+carposition)%self.n
        self.nextcars[lane][nextposition]=velocity

    def get_car(self):
        return self.cars

    def printroad(self):
        board0=['~' for i in range(self.n)]
        board1=['~' for i in range(self.n)]
        for i in self.cars[0]:
            board0[i]=int(self.cars[0][i])
        for i in self.cars[1]:
            board1[i]=int(self.cars[1][i])
        string=''
        for i in board0:
            string+=str(i)
        string+='\n'
        for i in board1:
            string+=str(i)
        print(string)

    def getroad(self):
        board0=['~' for i in range(self.n)]
        board1=['~' for i in range(self.n)]
        for i in self.cars[0]:
            board0[i]=int(self.cars[0][i])
        for i in self.cars[1]:
            board1[i]=int(self.cars[1][i])
        string=''
        for i in board0:
            string+=str(i)
        string+='\n'
        for i in board1:
            string+=str(i)
        return(string)


text:
import numpy as np

import matplotlib.pyplot as plt

dt = 0.0005
t = np.arange(0.0, 20.0, dt)
s1 = np.sin(2 * np.pi * 100 * t)
s2 = 2 * np.sin(2 * np.pi * 400 * t)

# create a transient "chirp"
mask = np.where(np.logical_and(t > 10, t < 12), 1.0, 0.0)
s2 = s2 * mask

# add some noise into the mix
nse = 0.01 * np.random.random(size=len(t))

x = s1 + s2 + nse  # the signal
NFFT = 1024  # the length of the windowing segments
Fs = int(1.0 / dt)  # the sampling frequency

# Pxx is the segments x freqs array of instantaneous power, freqs is
# the frequency vector, bins are the centers of the time bins in which
# the power is computed, and im is the matplotlib.image.AxesImage
# instance

ax1 = plt.subplot(211)
plt.plot(t, x)
plt.subplot(212, sharex=ax1)
Pxx, freqs, bins, im = plt.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
                                    cmap=plt.cm.gist_heat)
plt.show()

max_stars_repo_name: alanland/ml-pratice

max_issues_repo_name: alanland/ml-pratice

max_forks_repo_name: alanland/ml-pratice


text:
import numpy as np
import tensorflow as tf

from avod.core import box_list
from avod.core import box_list_ops
from avod.builders.dataset_builder import DatasetBuilder

class MiniBatchUtilsTest(tf.test.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.dataset = DatasetBuilder.build_kitti_dataset(
            DatasetBuilder.KITTI_UNITTEST)

        cls.mb_utils = cls.dataset.kitti_utils.mini_batch_utils

    def test_get_anchors_info(self):

        # Take the first non empty sample
        sample = self.dataset.sample_names[1]

        # Check the anchors info for first class type
        anchors_info = self.mb_utils.get_anchors_info(
            self.dataset.classes_name,
            self.dataset.kitti_utils.anchor_strides,
            sample)

        anchor_indices = anchors_info[0]
        anchor_ious = anchors_info[1]
        anchor_offsets = anchors_info[2]
        anchor_classes = anchors_info[3]

        # Lengths should all be the same
        self.assertTrue(len(anchor_indices), len(anchor_ious))
        self.assertTrue(len(anchor_indices), len(anchor_offsets))
        self.assertTrue(len(anchor_indices), len(anchor_classes))

        # Indices, IOUs, and classes values should all be >= 0
        self.assertTrue((anchor_indices >= 0).all())
        self.assertTrue((anchor_ious >= 0).all())
        self.assertTrue((anchor_classes >= 0).all())

        # Offsets should be (N, 6)
        self.assertTrue(len(anchor_offsets.shape) == 2)
        self.assertTrue(anchor_offsets.shape[1] == 6)

    def test_iou_mask_ops(self):
        # corners are in [y1, x1, y2, x2] format
        corners_pred = tf.constant(
            [[4.0, 3.0, 7.0, 5.0],
             [14.0, 14.0, 16.0, 16.0],
             [0.0, 0.0, 21.0, 19.0],
             [3.0, 4.0, 5.0, 7.0]])
        corners_gt = tf.constant(
            [[4.0, 3.0, 7.0, 6.0],
             [14.0, 14.0, 15.0, 15.0],
             [0.0, 0.0, 20.0, 20.0]])
        # 3 classes
        class_indices = tf.constant([1., 2., 3.])

        exp_ious = [[0.66666669, 0., 0.02255639, 0.15384616],
                    [0., 0.25, 0.00250627, 0.],
                    [0.015, 0.01, 0.90692127, 0.015]]

        exp_max_ious = np.array([0.66666669, 0.25, 0.90692127, 0.15384616])
        exp_max_indices = np.array([0, 1, 2, 0])

        exp_pos_mask = np.array([True, False, True, False])

        exp_class_and_background_indices = np.array([1, 0, 3, 0])

        # Convert to box_list format
        boxes_pred = box_list.BoxList(corners_pred)
        boxes_gt = box_list.BoxList(corners_gt)
        # Calculate IoU
        iou = box_list_ops.iou(boxes_gt,
                               boxes_pred)

        # Get max IoU, the dimension should match the anchors we are
        # evaluating
        max_ious = tf.reduce_max(iou, axis=0)
        max_iou_indices = tf.argmax(iou, axis=0)

        # Sample a mini-batch from anchors with highest IoU match
        mini_batch_size = 4

        # Custom positive/negative iou ranges
        neg_2d_iou_range = [0.0, 0.3]
        pos_2d_iou_range = [0.6, 0.7]

        mb_mask, mb_pos_mask = \
            self.mb_utils.sample_mini_batch(max_ious,
                                            mini_batch_size,
                                            neg_2d_iou_range,
                                            pos_2d_iou_range)

        mb_class_indices = self.mb_utils.mask_class_label_indices(
            mb_pos_mask, mb_mask, max_iou_indices, class_indices)

        with self.test_session() as sess:
            iou_out = sess.run(iou)
            max_ious_out, max_iou_indices_out = sess.run([max_ious,
                                                          max_iou_indices])
            mb_mask_out, mb_pos_mask_out = sess.run([mb_mask,
                                                     mb_pos_mask])
            class_indices_out = sess.run(mb_class_indices)

            self.assertAllClose(iou_out, exp_ious)
            self.assertAllClose(max_ious_out, exp_max_ious)
            self.assertAllEqual(max_iou_indices_out, exp_max_indices)
            self.assertAllEqual(exp_pos_mask, mb_pos_mask_out)
            self.assertAllEqual(class_indices_out,
                                exp_class_and_background_indices)

if __name__ == '__main__':
    tf.test.main()

max_stars_repo_name: Liaoqing-up/avod

max_issues_repo_name: zhiranyan/AVOD-BottleneckLstm

max_forks_repo_name: zhiranyan/AVOD-BottleneckLstm


text:
import os
import json
import re
from functools import partial
from fnmatch import fnmatch

import networkx as nx
from networkx.readwrite import json_graph

import blackwidow.config as config
from blackwidow.node import Node
from blackwidow.viz.server import serve
from blackwidow.imports import import_path

class Web(object):
    """
    The full project web.
    Contains the package root and list of nodes.
    """

    def __init__(self, package_root, exclude=None):
        self.files = find_files(package_root, exclude=exclude)
        self.package_root = package_root
        self.module = import_path(package_root, os.path.dirname(package_root))
        self.nodes = [
            Node(file, package_root=self.package_root) for file in self.files
        ]

        package_import_path = partial(import_path, package_root=os.path.dirname(package_root))
        self.package_modules = set(map(package_import_path, self.files))

        self.graph = nx.Graph()

        for node in self.nodes:
            if node.module not in self.package_modules:
                continue
            self.graph.add_node(node.module)
            self.graph.node[node.module]['name'] = node.module

            for module in node.imports:
                module = self._package_module(module)
                if module in self.package_modules:
                    self.graph.add_edge(node.module, module)

    def _package_module(self, module):
        if not module.startswith(self.module):
            module = ".".join([self.module, module])
        return module

    def visualize(self, port=None):
        """
        Dumps graph data to a dictionary and renders using d3's force-directed graph
        """
        json_data = json_graph.node_link_data(self.graph)
        json_data['package_name'] = self.module
        serve(data=json_data, port=port)

def find_files(dir, exclude=None):
    """
    Given a root directory, compiles a list of python files to analyze.
    If a discovered python file is provided in the list of excluded, it will be ignored
    """
    if not exclude:
        exclude = []

    matches = []
    for root, dirs, files in os.walk(dir):
        for file in files:
            filepath = os.path.abspath(os.path.join(root, file))
            if any(fnmatch(filepath, pattern) for pattern in exclude):
                continue

            if filepath.endswith(".py"):
                matches.append(filepath)

    return matches

if __name__ == "__main__":
    import os, sys, argparse

    parser = argparse.ArgumentParser(description='Visualize Python Project Imports')
    parser.add_argument('package', help='name of python package to analyze')
    parser.add_argument(
        '--exclude', nargs='+', metavar="pattern",
        help='list of files patterns to exclude'
    )
    args = parser.parse_args()

    package_name = args.package
    package = __import__(package_name)
    if "__init__" not in package.__file__:
        raise ValueError('Not a package: %s' % package_name)
    project_path = os.path.dirname(package.__file__)

    web = Web(project_path, exclude=args.exclude)
    web.visualize()

max_stars_repo_name: stitch/BlackWidow

max_issues_repo_name: stitch/BlackWidow

max_forks_repo_name: madisonmay/BlackWidow

reason: import networkx,from networkx


text:
"""Provides the wrapper class for a model node."""
__license__ = 'MIT'

########################################
# Dependencies                         #
########################################
from numpy import array, ndarray       # numerical array
from jpype import JBoolean             # Java boolean
from jpype import JInt                 # Java integer
from jpype import JDouble              # Java float
from jpype import JString              # Java string
from jpype import JArray               # Java array
from pathlib import Path               # file-system path
from re import split                   # string splitting
from json import load as json_load     # JSON decoder
from difflib import get_close_matches  # fuzzy matching
from functools import lru_cache        # function cache
from logging import getLogger          # event logging

########################################
# Globals                              #
########################################
logger = getLogger(__package__)        # event logger

########################################
# Node                                #
########################################
class Node:
    """
    Represents a model mode.

    Nodes work similarly to `pathlib.Path` objects from Python's
    standard library. They support string concatenation to the right
    with the division operator  in order to reference child nodes:
    ```python
    >>> node = model/'functions'
    >>> node
    Node('functions')
    >>> node/'step'
    Node('functions/step')
    ```

    Note how the `model` object also supports the division operator in
    order to generate node references. As mere references, nodes must
    must not necessarily exist in the model tree:
    ```python
    >>> (node/'new function').exists()
    False
    ```

    In rare cases, the node name itself might contain a forward slash,
    such as the dataset `sweep/solution` that happens to exist in the
    demo model from the Tutorial. These literal forward slashes can be
    escaped by doubling the character:
    ```python
    >>> node = model/'datasets/sweep//solution'
    >>> node.name()
    'sweep//solution'
    >>> node.parent()
    Node('datasets')
    ```

    This class allows inspecting a node, such as its properties and
    child nodes, as well as manipulating it to some extent, like
    toggling it on/off, creating child nodes, or "running" it. Not all
    actions made available by Comsol are exposed here. Those missing can
    however be triggered in the Java layer, which is accessible via the
    `.java` property.
    """

    ####################################
    # Internal                         #
    ####################################

    def __init__(self, model, path=None):
        if path is None:
            self.path = ('',)
        elif isinstance(path, str):
            self.path = parse(path)
        elif isinstance(path, Node):
            self.path = path.path
        else:
            error = f'Node path {path!r} is not a string or Node instance.'
            logger.error(error)
            raise TypeError(error)
        self.alias = {
            'parameter':  'parameters',
            'function':   'functions',
            'component':  'components',
            'geometry':   'geometries',
            'view':       'views',
            'selection':  'selections',
            'variable':   'variables',
            'coupling':   'couplings',
            'material':   'materials',
            'mesh':       'meshes',
            'study':      'studies',
            'solution':   'solutions',
            'batch':      'batches',
            'dataset':    'datasets',
            'evaluation': 'evaluations',
            'table':      'tables',
            'plot':       'plots',
            'result':     'plots',
            'results':    'plots',
            'export':     'exports',
        }
        if self.path[0] in self.alias:
            self.path = (self.alias[self.path[0]],) + self.path[1:]
        self.groups = {
            'parameters':   'self.model.java.param().group()',
            'functions':    'self.model.java.func()',
            'components':   'self.model.java.component()',
            'geometries':   'self.model.java.geom()',
            'views':        'self.model.java.view()',
            'selections':   'self.model.java.selection()',
            'coordinates':  'self.model.java.coordSystem()',
            'variables':    'self.model.java.variable()',
            'couplings':    'self.model.java.cpl()',
            'physics':      'self.model.java.physics()',
            'multiphysics': 'self.model.java.multiphysics()',
            'materials':    'self.model.java.material()',
            'meshes':       'self.model.java.mesh()',
            'studies':      'self.model.java.study()',
            'solutions':    'self.model.java.sol()',
            'batches':      'self.model.java.batch()',
            'datasets':     'self.model.java.result().dataset()',
            'evaluations':  'self.model.java.result().numerical()',
            'tables':       'self.model.java.result().table()',
            'plots':        'self.model.java.result()',
            'exports':      'self.model.java.result().export()',
        }
        self.model = model

    def __str__(self):
        return join(self.path)

    def __repr__(self):
        return f"{self.__class__.__name__}('{self}')"

    def __eq__(self, other):
        return (self.path == other.path and self.model == other.model)

    def __truediv__(self, other):
        if isinstance(other, str):
            other = other.lstrip('/')
            return Node(self.model, join(parse(f'{self}/{other}')))
        return NotImplemented

    def __contains__(self, node):
        if isinstance(node, str):
            if (self/node).exists():
                return True
        elif isinstance(node, Node):
            if node.parent() == self and node.exists():
                return True
        return False

    def __iter__(self):
        yield from self.children()

    @property
    def java(self):
        """
        Returns the Java object this node maps to.

        Note that this is a property, not an attribute. Internally,
        it is a function that performs a top-down search of the model
        tree in order to resolve the node reference. So it introduces
        a certain overhead every time it is accessed.
        """
        if self.is_root():
            return self.model.java
        name = self.name()
        if self.is_group():
            return eval(self.groups.get(name))
        parent = self.parent()
        java = parent.java
        if not java:
            return None
        container = java if parent.is_group() else java.feature()
        for tag in container.tags():
            member = container.get(tag)
            if name == escape(member.name()):
                return member

    ####################################
    # Navigation                       #
    ####################################

    def name(self):
        """Returns the node's name."""
        return f'{self.model}' if self.is_root() else escape(self.path[-1])

    def tag(self):
        """Returns the node's tag."""
        java = self.java
        return str(java.tag()) if java else None

    def type(self):
        """
        Returns the node's feature type.

        This a something like `'Block'` for "a right-angled solid or
        surface block in 3D". Refer to the Comsol documentation for
        details. Feature types are displayed in the Comsol GUI at the
        top of the `Settings` tab.
        """
        java = self.java
        return str(java.getType()) if hasattr(java, 'getType') else None

    def parent(self):
        """Returns the parent node."""
        if self.is_root():
            return None
        else:
            return Node(self.model, join(self.path[:-1]))

    def children(self):
        """Returns all child nodes."""
        java = self.java
        if self.is_root():
            return [Node(self.model, group) for group in self.groups]
        elif self.is_group():
            return [self/escape(java.get(tag).name()) for tag in java.tags()]
        elif hasattr(java, 'feature'):
            return [self/escape(java.feature(tag).name())
                    for tag in java.feature().tags()]
        else:
            return []

    def is_root(self):
        """Checks if the node is the model's root node."""
        return bool(len(self.path) == 1 and not self.path[0])

    def is_group(self):
        """Checks if the node refers to a built-in group."""
        return bool(len(self.path) == 1 and self.path[0])

    def exists(self):
        """Checks if the node exists in the model tree."""
        return (self.java is not None)

    ####################################
    # Interaction                      #
    ####################################

    def rename(self, name):
        """Renames the node."""
        if self.is_root():
            error = 'Cannot rename the root node.'
            logger.error(error)
            raise PermissionError(error)
        if self.is_group():
            error = 'Cannot rename a built-in group.'
            logger.error(error)
            raise PermissionError(error)
        java = self.java
        if java:
            java.name(name)
        self.path = self.path[:-1] + (name,)

    def retag(self, tag):
        """Assigns a new tag to the node."""
        if self.is_root():
            error = 'Cannot change tag of root node.'
            logger.error(error)
            raise PermissionError(error)
        if self.is_group():
            error = 'Cannot change tag of built-in group.'
            logger.error(error)
            raise PermissionError(error)
        java = self.java
        if not java:
            error = f'Node "{self}" does not exist in model tree.'
            logger.error(error)
            raise LookupError(error)
        java.tag(tag)

    def property(self, name, value=None):
        """
        Returns or changes the value of the named property.

        If no `value` is given, returns the value of property `name`.
        Otherwise sets the property to the given value.
        """
        if value is None:
            return get(self.java, name)
        else:
            self.java.set(name, cast(value))

    def properties(self):
        """Returns names and values of all node properties as a dictionary."""
        java = self.java
        if not hasattr(java, 'properties'):
            return {}
        names = sorted(str(name) for name in java.properties())
        return {name: get(java, name) for name in names}

    def toggle(self, action='flip'):
        """
        Enables or disables the node.

        If `action` is `'flip'` (the default), it enables the feature
        in the model tree if it is currently disabled or disables it
        if enabled. Pass `'enable'` or `'on'` to enable the feature
        regardless of its current state. Pass `'disable'` or `'off'`
        to disable it.
        """
        java = self.java
        if not java:
            error = f'Node "{self}" does not exist in model tree.'
            logger.error(error)
            raise LookupError(error)
        if action == 'flip':
            java.active(not java.isActive())
        elif action in ('enable', 'on', 'activate'):
            java.active(True)
        elif action in ('disable', 'off', 'deactivate'):
            java.active(False)

    def run(self):
        """Performs the "run" action if the node implements it."""
        java = self.java
        if not java:
            error = f'Node "{self}" does not exist in model tree.'
            logger.error(error)
            raise LookupError(error)
        if not hasattr(java, 'run'):
            error = f'Node "{self}" does not implement "run" operation.'
            logger.error(error)
            raise RuntimeError(error)
        java.run()

    def create(self, *arguments, name=None):
        """
        Creates a new child node.

        Refer to the Comsol documentation for the values of valid
        `arguments`. It is often just the feature type of the child
        node to be created, given as a string such as `'Block'`, but
        may also require different or more arguments.

        If `name` is not given, a unique name/label will be assigned
        automatically.
        """
        if self.is_root():
            error = 'Cannot create nodes at root of model tree.'
            logger.error(error)
            raise PermissionError(error)
        java = self.java
        container = None
        if self.is_group():
            if not hasattr(java, 'uniquetag') and hasattr(java, 'feature'):
                container = java.feature()
            elif hasattr(java, 'uniquetag') and hasattr(java, 'create'):
                container = java
        elif hasattr(java, 'feature'):
            container = java.feature()
        if not hasattr(container, 'uniquetag'):
            error = f'Node {self} does not support feature creation.'
            logger.error(error)
            raise RuntimeError(error)
        for argument in arguments:
            if isinstance(argument, str):
                type = argument
                break
        else:
            type = '?'
        pattern = tag_pattern(feature_path(self) + [type])
        if pattern.endswith('*'):
            tag = container.uniquetag(pattern[:-1])
        elif pattern in container.tags():
            tag = container.uniquetag(pattern)
        else:
            tag = pattern
        if not arguments:
            container.create(tag)
        else:
            container.create(tag, *[cast(argument) for argument in arguments])
        if name:
            container.get(tag).name(unescape(name))
        else:
            name = escape(container.get(tag).name())
        child = self/name
        check = tag_pattern(feature_path(child))
        if pattern != check:
            pattern = check
            if pattern.endswith('*'):
                tag = container.uniquetag(pattern[:-1])
            elif pattern in container.tags():
                tag = container.uniquetag(pattern)
            else:
                tag = pattern
            logger.debug(f'Retagging "{child}": "{child.tag()}" → "{tag}".')
            child.retag(tag)
        return child

    def remove(self):
        """Removes the node from the model tree."""
        if self.is_root():
            error = 'Cannot remove the root node.'
            logger.error(error)
            raise PermissionError(error)
        if self.is_group():
            error = 'Cannot remove a built-in group.'
            logger.error(error)
            raise PermissionError(error)
        if not self.exists():
            error = f'Node "{self}" does not exist in model tree.'
            logger.error(error)
            raise LookupError(error)
        parent = self.parent()
        container = parent.java if parent.is_group() else parent.java.feature()
        container.remove(self.java.tag())

########################################
# Name parsing                         #
########################################

def parse(string):
    """Parses a node path given as string to a tuple."""
    # Force-cast str subclasses to str, just like `pathlib` does.
    # See bugs.python.org/issue21127 for the rationale.
    string = str(string)
    # Remove all leading and trailing forward slashes.
    string = string.lstrip('/').rstrip('/')
    # Split at forward slashes, but not double forward slashes.
    path = tuple(unescape(name) for name in split(r'(?<!/)/(?!/)', string))
    return path

def join(path):
    """Joins a node path given as tuple into a string."""
    return '/'.join(escape(name) for name in path)

def escape(name):
    """Escapes forward slashes in a node name."""
    # Also accept Java strings, but always return Python string.
    name = str(name)
    return name.replace('/', '//')

def unescape(name):
    """Reverses escaping of forward slashes in a node name."""
    return name.replace('//', '/')

########################################
# Tag patterns                         #
########################################

@lru_cache(maxsize=1)
def load_patterns():
    """Loads the look-up table for tag patterns indexed by feature path."""
    file = Path(__file__).parent/'tags.json'
    with file.open(encoding='UTF-8-sig') as stream:
        patterns = json_load(stream)
    return patterns

def feature_path(node):
    """Returns the feature path of a node."""
    if node.is_group():
        return [node.name()]
    type = node.type()
    if not type:
        type = '?'
    return feature_path(node.parent()) + [type]

def tag_pattern(feature_path):
    """Looks up the tag pattern for the best match to given feature path."""
    (group, type) = (feature_path[0], feature_path[-1])
    patterns = load_patterns()
    selected = [key for key in patterns
                if key.startswith(group) and key.endswith(type)]
    matches = get_close_matches(' → '.join(feature_path), selected)
    if matches:
        return patterns[matches[0]]
    elif type != '?':
        return type.lower()[:3] + '*'
    else:
        return 'tag*'

########################################
# Type casting                         #
########################################

def cast(value):
    """Casts a value from its Python data type to a suitable Java data type."""
    if isinstance(value, Node):
        return JString(value.tag())
    elif isinstance(value, bool):
        return JBoolean(value)
    elif isinstance(value, int):
        return JInt(value)
    elif isinstance(value, float):
        return JDouble(value)
    elif isinstance(value, str):
        return JString(value)
    elif isinstance(value, Path):
        return JString(str(value))
    elif isinstance(value, (list, tuple)):
        dimension = 0
        item = value
        while isinstance(item, (list, tuple)):
            dimension += 1
            if not len(item):
                datatype = JString
                value = []
                break
            item = item[0]
        else:
            datatype = cast(item).__class__
        value = [cast(item) for item in value]
        return JArray(datatype, dimension)(value)
    elif isinstance(value, ndarray):
        if value.dtype.kind == 'b':
            return JArray(JBoolean, value.ndim)(value)
        elif value.dtype.kind == 'f':
            return JArray(JDouble, value.ndim)(value)
        elif value.dtype.kind == 'i':
            return JArray(JInt, value.ndim)(value)
        elif value.dtype.kind == 'O':
            if value.ndim > 2:
                error = 'Cannot cast object arrays of dimension higher than 2.'
                logger.error(error)
                raise TypeError(error)
            if len(value) > 2:
                error = 'Will not cast object arrays with more than two rows.'
                logger.error(error)
                raise TypeError(error)
            rows = [row.astype(float) for row in value]
            return JArray(JDouble, 2)(rows)
        else:
            error = f'Cannot cast arrays of data type "{value.dtype}".'
            logger.error(error)
            raise TypeError(error)
    else:
        error = f'Cannot cast values of data type "{type(value).__name__}".'
        logger.error(error)
        raise TypeError(error)

def get(java, name):
    """Returns the value of a Java node property as a Python data type."""
    datatype = java.getValueType(name)
    if datatype == 'Boolean':
        return java.getBoolean(name)
    elif datatype == 'BooleanArray':
        return array(java.getBooleanArray(name))
    elif datatype == 'BooleanMatrix':
        return array([line for line in java.getBooleanMatrix(name)])
    elif datatype == 'Double':
        return java.getDouble(name)
    elif datatype == 'DoubleArray':
        return array(java.getDoubleArray(name))
    elif datatype == 'DoubleMatrix':
        return array([line for line in java.getDoubleMatrix(name)])
    elif datatype == 'DoubleRowMatrix':
        value = java.getDoubleMatrix(name)
        if len(value) == 0:
            rows = []
        elif len(value) == 1:
            rows = [array(value[0])]
        elif len(value) == 2:
            rows = [array(value[0]), array(value[1])]
        else:
            error = 'Cannot convert double-row matrix with more than two rows.'
            logger.error(error)
            raise TypeError(error)
        return array(rows, dtype=object)
    elif datatype == 'File':
        return Path(str(java.getString(name)))
    elif datatype == 'Int':
        return int(java.getInt(name))
    elif datatype == 'IntArray':
        return array(java.getIntArray(name))
    elif datatype == 'IntMatrix':
        return array([line for line in java.getIntMatrix(name)])
    elif datatype == 'None':
        return None
    elif datatype == 'Selection':
        return [str(string) for string in java.getEntryKeys(name)]
    elif datatype == 'String':
        value = java.getString(name)
        return str(value) if value else None
    elif datatype == 'StringArray':
        return [str(string) for string in java.getStringArray(name)]
    elif datatype == 'StringMatrix':
        value = java.getStringMatrix(name)
        if value:
            return [[str(string) for string in line] for line in value]
        else:
            return [[]]
    else:
        error = f'Cannot convert Java data type "{datatype}".'
        logger.error(error)
        raise TypeError(error)

########################################
# Inspection                           #
########################################

def inspect(java):
    """
    Inspects a Java node object.

    This is basically a "pretty-fied" version of the output from the
    built-in `dir` command. It displays (prints to the console) the
    methods of a model node (as given by the Comsol API) as well as
    the node's properties (if any are defined).

    The node's name, tag, and documentation reference marker are
    listed first. These access methods and a few others, which are
    common to all objects, are suppressed in the method list further
    down, for the sake of clarity.
    """

    # Also accept Node and Model instances.
    if hasattr(java, 'java'):
        java = java.java

    # Display general information about the feature.
    print(f'name:    {java.name()}')
    print(f'tag:     {java.tag()}')
    if hasattr(java, 'getType'):
        print(f'type:    {java.getType()}')
    print(f'display: {java.getDisplayString()}')
    print(f'doc:     {java.docMarker()}')

    # Display comments and notify if feature is deactivated or has warnings.
    comments = str(java.comments())
    if comments:
        print(f'comment: {comments}')
    if not java.isActive():
        print('This feature is currently deactivated.')
    if hasattr(java, 'hasWarning') and java.hasWarning():
        print('This feature has warnings.')

    # Introspect the feature's attributes.
    attributes = [attribute for attribute in dir(java)]

    # Display properties if any are defined.
    if 'properties' in attributes:
        print('properties:')
        names = [str(name) for name in java.properties()]
        for name in names:
            value = get(java, name)
            print(f'  {name}: {value}')

    # Define a list of common methods to be suppressed in the output.
    suppress = ['name', 'label', 'tag', 'getType', 'getDisplayString',
                'docMarker', 'help', 'comments', 'toString', 'icon',
                'properties', 'hasProperty', 'set',
                'getEntryKeys', 'getEntryKeyIndex', 'getValueType',
                'getInt',     'getIntArray',     'getIntMatrix',
                'getBoolean', 'getBooleanArray', 'getBooleanMatrix',
                'getDouble',  'getDoubleArray',  'getDoubleMatrix',
                'getString',  'getStringArray',  'getStringMatrix',
                'version', 'author', 'resetAuthor', 'lastModifiedBy',
                'dateCreated', 'dateModified', 'timeCreated', 'timeModified',
                'active', 'isActive', 'isactive', 'hasWarning',
                'class_', 'getClass', 'hashCode',
                'notify', 'notifyAll', 'wait']

    # Display the feature's methods.
    print('methods:')
    for name in attributes:
        if name.startswith('_') or name in suppress:
            continue
        print(f'  {name}')

def tree(node, levels=[], max_depth=None):
    """
    Displays the model tree.

    This function displays a representation of the model tree in the
    console. `node` would typically be the model's root node. `levels`
    is used internally when traversing the model tree recursively.
    Specify `max_depth` to possibly limit the number of lower branches.

    Note that this function performs poorly in client–server mode, the
    default on Linux and macOS, especially for complex models. The
    client–server communication introduces inefficiencies that do not
    occur in stand-alone mode, the default on Windows, where the model
    tree, i.e. the hierarchy of related Java objects, can be traversed
    reasonably fast.
    """
    if not isinstance(node, Node):
        # Support passing the model directly instead of a node.
        node = node/None
    if max_depth and len(levels) > max_depth:
        return
    markers = ''.join('   ' if last else '│  ' for last in levels[:-1])
    markers += '' if not levels else '└─ ' if levels[-1] else '├─ '
    print(f'{markers}{node.name()}')
    children = node.children()
    last = len(children) - 1
    for (index, child) in enumerate(children):
        tree(child, levels + [index == last], max_depth)

reason: from numpy