import platform
import sys

import keras
import matplotlib
import numpy
import pandas as pd
import sklearn
import tensorflow

try:
    import torch
except:
    pass


class Cuda:

    # noinspection PyUnresolvedReferences
    @staticmethod
    def sysinfo():
        print("Devices:")
        for device in tensorflow.config.list_physical_devices():
            print(" ", device)

        # os.system("pip list | fgrep tensorflow")
        # os.system("pip list | fgrep torch")

        try:
            n = torch.cuda.device_count()
            current = torch.cuda.current_device()
        except Exception as e:
            print(e)
            current = ""
        print("CUDA:")
        try:
            print("  Available:", torch.cuda.is_available())
            print("  Cards    :", n)
            for i in range(0, n):
                if i == current:
                    flag = "*"
                else:
                    flag = " "
                name = torch.cuda.get_device_name(i)
                mem_allocated = round(
                    torch.cuda.memory_allocated(0) / 1024 ** 3, 1)
                mem_cached = round(torch.cuda.memory_cached(0) / 1024 ** 3, 1)
                print(
                    f"   {flag} Card {i}: {name} Memory allocated={mem_allocated}GB cached={mem_cached}GB")
        except:
            pass

        print()
        try:
            print("Torch      :", torch.__version__)
        except:
            pass
        print("tensorflow :", tensorflow.__version__)
        print("keras      :", keras.__version__)

        print()
        print("Python     :", sys.executable)
        print("    Version:", platform.python_version())
        print("Libraries")
        print("  pandas    :", pd.__version__)
        print("  numpy     :", numpy.__version__)
        print("  sklearn   :", sklearn.__version__)
        print("  matplotlib:", matplotlib.__version__)
