test_libtt.py 4.02 KB
#!/usr/bin/python3
# ====================================================
# First, compile:  python setup.py build_ext --inplace
# Second, exucute: python test_libtt.py
# ====================================================

# ====================================================
# ====================================================
# === Import the extension and manage errors
# ====================================================
# ====================================================
try:
    import libtt
except Exception as e:
    print(f"Error import libtt: {e}")
    print("\nI try to help you to find the solution:")
    import os, glob
    import subprocess
    cwd = os.getcwd()
    fics = glob.glob("libtt*.so")
    for fic in fics:
        cmd = f"ldd {fic}"
        print(f"\nExecute: {cmd}")
        argus = []
        argus.append("ldd")
        argus.append(fic)
        res = subprocess.run(argus, capture_output=True)
        outb = res.stdout
        outs = outb.decode().split("\n")
        comment = ""
        for out in outs:
            print(out)
            words = out.split()
            if len(words) > 2:
                lib = words[0]
                path = words[2]
                if path == "not":
                    comment = f"The path of the library {lib} is not found. Try:"
        if comment != "":
            print(f"{comment}")
            cmd = f"export LD_LIBRARY_PATH={cwd}:$LD_LIBRARY_PATH"
            print(f"$ {cmd}")
            print("\nIf it a success you have to record the LD_LIBRARY_PATH permanently:")
            print("$ sudo vi /etc/ld.so.conf")
            print("$ sudo ldconfig")
            print("")
    exit()

# ====================================================
# ====================================================
# === List of tests to execute
# ====================================================
# ====================================================
tests = []
#tests.append("matrix")
#tests.append("matrixio")
tests.append("pimaseries")

# ====================================================
# ====================================================
# === Code of tests
# ====================================================
# ====================================================
import time

for test in tests:

    print("="*40+"\n"+"="*10+f" Test: {test} \n"+"="*40)

    if test == "matrix":

        res = libtt.matrix(4096,4096,8.99)
        print(f"{res=}")
        res = libtt.matrix(10,5,4.56)
        print(f"{res=}")

    if test == "matrixio":

        import numpy as np
        for k in range(15):
            print("="*20 + f" libtt {k}")
            p = np.zeros((1000,1000), dtype=np.dtype(np.uint32) )
            #p = np.zeros((1000,1000), dtype=np.dtype(np.int32) )
            #p = np.zeros((4096, 4096), dtype=np.dtype(np.float32) )
            #p = np.zeros((4096, 4096), dtype=np.dtype(np.float64) )
            print(f"{p=}")
            res = libtt.matrixio(p)
            print(f"{res=}")

    if test == "pimaseries":

        import sys
        sys.path.append("..");
        import guitastro
        import numpy as np

        try:
            print("== ima")
            ima = guitastro.Ima()
            p = np.zeros((100,50), dtype=np.dtype(np.uint32) )
            #p = np.zeros((100,50), dtype=np.dtype(np.float32) )
            #p = np.zeros((100,50), dtype=np.dtype(np.float64) )
            #p = [ [1, 3, 4], [7, -6, -9] ]
            ima.array2buf(p)
            for k in range(15):
                print("="*20 + f" libtt {k}")
                print(f"{ima._array}")
                print(f"{ima._array.dtype=}")
                #print(f"{ima.cards=}")
                ima2 = libtt.pimaseries(ima, "OFFSET offset=100")
                print(f"{ima2}")
                print(f"{ima2.dtype=}")
        except BaseException as e:
            print("Error:", str(e))

        print("== END")
        del sys.modules['libtt']
        del libtt
        print("== SUPER END")