def release_live_import(live_import):
    """
    Cleanup and exit live import module.
    """
    if live_import is not None:
        live_import.ReleaseLiveImport()
        FreeLibrary(live_import._handle)
Exemple #2
0
    def test_1703286_A(self):
        from _ctypes import LoadLibrary, FreeLibrary
        # On winXP 64-bit, advapi32 loads at an address that does
        # NOT fit into a 32-bit integer.  FreeLibrary must be able
        # to accept this address.

        # These are tests for http://www.python.org/sf/1703286
        handle = LoadLibrary("advapi32")
        FreeLibrary(handle)
Exemple #3
0
def load_shared_lib(shared_lib_path: str) -> ctypes.CDLL:
    """
    Load a shared library using ctypes freeing the resource at end.
    """
    plugin_dll = ctypes.cdll.LoadLibrary(shared_lib_path)
    try:
        yield plugin_dll
    finally:
        if sys.platform == 'win32':
            from _ctypes import FreeLibrary
            FreeLibrary(plugin_dll._handle)
        else:
            from _ctypes import dlclose
            dlclose(plugin_dll._handle)
Exemple #4
0
 def UnloadCamDLL(self):
     del self.dll
     self.dll = None
     FreeLibrary(self.libHandle)
Exemple #5
0
 def test_1703286_A(self):
     from _ctypes import LoadLibrary, FreeLibrary
     handle = LoadLibrary('advapi32')
     FreeLibrary(handle)
Exemple #6
0
 def __del__(self):
     FreeLibrary(self.dll._handle)
Exemple #7
0
 def deleteLib(daLib):
     from _ctypes import FreeLibrary
     hlib = daLib._handle
     del daLib
     FreeLibrary(hlib)
Exemple #8
0
def rungap50(country, data, adjfact, vintage_name, changey, path, tipo,
             logfile):
    import ctypes as ct
    import numpy as np
    import pandas as pd
    from _ctypes import FreeLibrary
    import f90nml

    projpath = path + '/'
    path = path + '$'  # add to find end of dtring
    dllversion = "GAP50DLL20190503.dll"

    with open(logfile, 'a') as f:
        f.write('\n---computing ' + tipo.upper() + ' for ' + country.upper() +
                ' via GAP50.DLL v.' + dllversion)
    lib = ct.CDLL(projpath + "lib/" + dllversion)

    gap50 = getattr(lib, "pytogap")

    # Read Namelist
    nml = f90nml.read(projpath + 'priors/' + tipo.upper() + '_DLL_' +
                      country.upper() + '_' +
                      vintage_name.replace('final', '') + '.nml')
    nmlstr = str(nml)
    nt = len(nml['prior']['lab'])  # number of parameters
    ny = len(nml['ssm']['endogenous'])  # number of endogenous series
    inter = nml['GAP'][
        'Inter']  # INTER: 1=(Back,PC),2=(Back,Cycle),3=(Forw,PC),4=(Forw,Cycle)
    hor = max(0, nml['GAP']['Anchor'][1])  # horizon for anchored estimates
    starty = nml['GAP']['Startyear']
    stri = str(nml['ssm']['exogenous'])
    pos = list(find_all(stri, ','))

    if len(stri) > 0:
        nz = len(pos) + 1  # number of exogenous series
    else:
        nz = 0

    name_vars = list()

    if tipo == 'nawru':
        name_vars.append('LUR')
        nf = hor
    else:
        name_vars.append('SR')
        nf = 10
    if tipo == 'nawru':
        if inter < 3:
            name_vars.append('DWINF')
        else:
            name_vars.append('DRULC')
    # TODO : DEBUG
    # else:
    #   name_vars.append('CU')
    elif ny == 2:
        name_vars.append('CU')
    if nz == 1:
        name_vars.append(stri.strip())
    if nz > 1:
        name_vars.append(stri[0:pos[0]].strip())
        for i in range(0, len(pos) - 1):
            name_vars.append(stri[pos[i] + 1:pos[i + 1]].strip())
        name_vars.append(stri[pos[len(pos) - 1] + 1:].strip())

    nmax = changey - starty + 1
    # Select variables
    (inds, indn, xn) = selectdata(data, changey, name_vars[0:ny], nmax)
    ismax1 = np.amax(inds)  # maximum missing position
    ismax2 = 0
    if nz > 1:  #number of exog series
        (indse, indne, xne) = selectdata(data, changey, name_vars[ny + 1:],
                                         nmax)
        ismax2 = np.amax(indse)  # maximum missing position
    ismax = max(ismax1, ismax2)
    nobs = nmax - ismax  # number of in-sample observations
    yk = np.arange(0, ny + nz, dtype=float) * 0  # setup the nobs+nf - long
    for i in range(0, nobs + max(nf, hor) -
                   1):  # concatenate columns until it is nrow x ncol
        yk = np.c_[yk, np.arange(0, ny + nz, dtype=float) * 0]
    yk[0:ny, 0:nobs] = xn[0:ny, ismax:nobs + ismax]
    yk[0:ny, nobs:] = -99999.0
    if nz > 0:
        yk[ny, :] = 1.0  # set the constant series
    if nz > 1:
        yk[ny + 1:, 0:nobs] = xne[0:, ismax:nobs + ismax]

        for j in range(nobs, nobs + max(nf, hor)):

            yk[ny + 1:, j] = 0
#                    yk[ny+1:,j]  = xne[0:,nobs+ismax-1]

# Set other dll Input
    b_string0 = nmlstr.encode('utf-8')
    b_string1 = path.encode('utf-8')
    arr = (ct.c_char * 5000 * 2)()
    arr[0].value = b_string0
    arr[1].value = b_string1
    nstring = ct.pointer(ct.c_int(2))
    nobsp = ct.pointer(ct.c_int(nobs))  # setup the pointer
    nyp = ct.pointer(ct.c_int(ny))
    nzp = ct.pointer(ct.c_int(nz))
    nfp = ct.pointer(ct.c_int(nf))
    horp = ct.pointer(ct.c_int(hor))
    ntp = ct.pointer(ct.c_int(nt))

    # Set dll Output
    # Both ML and Bayes
    unobs = np.arange(0, 26, dtype=float) * 0  # setup the nobs+nf - long
    for i in range(
            1, nobs +
            max(nf, hor)):  # concatenate columns until it is nrow x ncol
        unobs = np.c_[unobs, np.arange(0, 26, dtype=float) * 0]


# Bayes
    ac = np.arange(0, 4 * ny, dtype=float) * 0  # setup the nobs+nf - long
    for i in range(1, 5):  # concatenate columns until it is nrow x ncol
        ac = np.c_[ac, np.arange(0, 4 * ny, dtype=float) * 0]
    param = np.arange(0, nt, dtype=float) * 0  # setup the nobs+nf - long
    for i in range(1, 409):  # concatenate columns until it is nrow x ncol
        param = np.c_[param, np.arange(0, nt, dtype=float) * 0]
    marginal = np.arange(0, nobs + nf,
                         dtype=float) * 0  # setup the nobs+nf - long
    for i in range(1, 1200):  # concatenate columns until it is nrow x ncol
        marginal = np.c_[marginal, np.arange(0, nobs + nf, dtype=float) * 0]
    margl = np.arange(0, 13, dtype=float) * 0  # setup the nobs+nf - long
    for i in range(1, 2):  # concatenate columns until it is nrow x ncol
        margl = np.c_[margl, np.arange(0, 13, dtype=float) * 0]

    with open(logfile, 'a') as f:
        f.write('\n-----Variable list : ' + str(name_vars))

    gap50(nstring, arr, nobsp, nyp, nzp, nfp, horp, ntp,
          np.ctypeslib.as_ctypes(yk), np.ctypeslib.as_ctypes(unobs),
          np.ctypeslib.as_ctypes(ac), np.ctypeslib.as_ctypes(param),
          np.ctypeslib.as_ctypes(marginal), np.ctypeslib.as_ctypes(margl))

    handle = lib._handle
    FreeLibrary(handle)

    # --------------------------------------------------------
    # UNOBSERVABLES (nob+nf,26)  Maximum Likelihood estimates
    # 1 series + forecasts, 2 rmse, 3 akrmse,
    # 4 smoothed trend + forecast, 5 rmse, 6 akrmse
    # 7 smoothed cycle + forecast, 8 rmse, 9 akrmse
    # 10 filtered trend, 11 filtered cycle, 12 rmse
    # 13 1st series innovations
    # 14 PC  series + forecast
    # 15 rmse
    # 16 akrmse
    # 17 PC innovations
    # 18 PC idiosync smoothed - empty!
    # 19 PC idiosync filtered - empty!
    # 20 PC Smoothed component  - mu + beta'*c(t|T)
    # 21 PC filtered component  - mu + beta'*c(t|t)"
    # 22 PC fitted values - mu + beta'*c(t|T)+gam'*z
    # 23 Anchored trend
    # 24 Anchored cycle
    # ---------------------------------------------------------

    if tipo == 'nawru':

        if nml['GAP']['Anchor'][1] < 0:
            nawru_series = pd.Series(unobs[3, 0:nmax - ismax + nf] +
                                     float(adjfact[country.lower()]),
                                     index=range(changey + 1 - nmax + ismax,
                                                 changey + nf +
                                                 1)).rename('NAWRU')
        else:
            nawru_series = pd.Series(unobs[22, 0:nmax - ismax + nf] +
                                     float(adjfact[country.lower()]),
                                     index=range(changey + 1 - nmax + ismax,
                                                 changey + nf +
                                                 1)).rename('NAWRU')

        with open(logfile, 'a') as f:
            f.write('->PASSED')
        return nawru_series

    else:
        # ------------------------------------------------------------------------
        # UNOBSERVABLES, Bayesian estimates
        # by cols: ser1,  p2.5, p5, p95, p97.5, trend, p2.5, p5, p95, p97.5,
        #          cycle, p2.5, p5, p95, p97.5, ser2,  p2.5, p5, p95, p97.5, inn
        #          slope, p2.5, p5, p95, p97.5
        # ------------------------------------------------------------------------

        with open(logfile, 'a') as f:
            f.write('->PASSED')
        return pd.Series(unobs[5, 0:nmax + nf],
                         index=range(changey + 1 - nmax + ismax,
                                     changey + ismax + nf + 1)).rename('SRKF')