text:
from functools import partial
import multiprocessing as mp
import numpy as np
import os
import sys

from dowel import logger, tabular
from garage.sampler.on_policy_vectorized_sampler import OnPolicyVectorizedSampler

from ast_toolbox.rewards import ExampleAVReward
from ast_toolbox.simulators import ExampleAVSimulator

os.sched_setaffinity(0, {i for i in range(mp.cpu_count())})

def myexcepthook(exctype, value, traceback):
    for p in mp.active_children():
       p.terminate()

def slice_dict(in_dict, slice_idx):
    """Helper function to recursively parse through a dictionary of dictionaries and arrays to slice \
    the arrays at a certain index.

    Parameters
    ----------
    in_dict : dict
        Dictionary where the values are arrays or other dictionaries that follow this stipulation.
    slice_idx : int
        Index to slice each array at.

    Returns
    -------
    dict
        Dictionary where arrays at every level are sliced.

    """
    for key, value in in_dict.items():
        # pdb.set_trace()
        if isinstance(value, dict):
            in_dict[key] = slice_dict(value, slice_idx)
        else:
            in_dict[key][slice_idx + 1:, ...] = np.zeros_like(value[slice_idx + 1:, ...])

    return in_dict

def simulate_single_path_worker(sim_func, slice_func, reward_func, reward_info_func, sim_from_s0, path):
    if sim_from_s0:
        s_0 = path["observations"][0]
    else:
        s_0 = None
    actions = path['env_infos']['actions']

    end_idx, info = sim_func(actions=actions, s_0=s_0)

    if end_idx >= 0:
        slice_func(path, end_idx)

    rewards = reward_func(
        action=actions[end_idx],
        info=reward_info_func()
    )

    path["rewards"][end_idx] = rewards
    path['env_infos']['cache'] = np.zeros_like(path["rewards"])
    for i in range(len(info)):
        path['env_infos']['state'][i,:-2] = info[i]['state']
    return path

class ASTVectorizedSampler(OnPolicyVectorizedSampler):
    """A vectorized sampler for AST to handle open-loop simulators.

    Garage usually genearates samples in a closed-loop process. This version of the vectorized sampler instead
    grabs dummy data until the full rollout specification is generated, then goes back and runs the `simulate`
    function to actually obtain results. Rewards are then calculated and the path data is corrected.

    Parameters
    ----------
    algo : :py:class:`garage.np.algos.base.RLAlgorithm`
        The algorithm.
    env : :py:class:`ast_toolbox.envs.ASTEnv`
        The environment.
    n_envs : int
        Number of parallel environments to run.
    open_loop : bool
        True if the simulation is open-loop, meaning that AST must generate all actions ahead of time, instead
        of being able to output an action in sync with the simulator, getting an observation back before
        the next action is generated. False to get interactive control, which requires that `blackbox_sim_state`
        is also False.
    sim : :py:class:`ast_toolbox.simulators.ASTSimulator`
        The simulator wrapper, inheriting from `ast_toolbox.simulators.ASTSimulator`.
    reward_function : :py:class:`ast_toolbox.rewards.ASTReward`
        The reward function, inheriting from `ast_toolbox.rewards.ASTReward`.

    """

    def __init__(self, algo, env, n_envs=1, open_loop=True,  batch_simulate=False,
                 sim=ExampleAVSimulator(), reward_function=ExampleAVReward(),
                 sim_from_s0=False):

        # pdb.set_trace()
        self.open_loop = open_loop
        self.batch_simulate = batch_simulate
        self.sim = sim
        self.reward_function = reward_function
        self.sim_from_s0 = sim_from_s0
        super().__init__(algo, env, n_envs)

    def obtain_samples(self, itr, batch_size=None, whole_paths=False):
        """Sample the policy for new trajectories.

        Parameters
        ----------
        itr : int
            Iteration number.
        batch_size : int
            Number of samples to be collected. If None,
            it will be default [algo.max_path_length * n_envs].
        whole_paths : bool
            Whether return all the paths or not. True
            by default. It's possible for the paths to have total actual
            sample size larger than batch_size, and will be truncated if
            this flag is true.

        Returns
        -------
        : list[dict]
            A list of sampled rollout paths.
            Each rollout path is a dictionary with the following keys:
                - observations (numpy.ndarray)
                - actions (numpy.ndarray)
                - rewards (numpy.ndarray)
                - agent_infos (dict)
                - env_infos (dict)

        """
        # pdb.set_trace()
        paths = super().obtain_samples(itr, batch_size)
        # pdb.set_trace()
        if self.open_loop:
            # mp.log_to_stderr(logging.DEBUG)
            if self.batch_simulate:
                # import pdb; pdb.set_trace()
                def store_process_return_value(func, queue, **kwargs):
                    queue.put(func(**kwargs))

                queue = mp.Queue()
                process_kwargs = {'func':self.sim.batch_simulate_paths,
                                  'queue': queue,
                                  'paths': paths,
                                  'reward_function': self.reward_function}

                p = mp.Process(target=store_process_return_value, kwargs=process_kwargs)
                p.start()
                p.join()

                if p.exitcode == 0:
                    paths = queue.get()
                    p.terminate()

                paths = self.sim.batch_simulate_paths(paths=paths, reward_function=self.reward_function)
            else:
                if self.n_envs > 1:
                    print("Opening Pool with ", self.n_envs, ' processes.')
                    sys.excepthook = myexcepthook

                    simulate_single_path_worker_partial = partial(simulate_single_path_worker,
                                                                  self.sim.simulate,
                                                                  slice_dict,
                                                                  self.reward_function.give_reward,
                                                                  self.sim.get_reward_info,
                                                                  self.sim_from_s0)

                    # pool = mp.Pool(processes=self.n_envs)
                    # paths = pool.map(simulate_single_path_worker_partial, paths)
                    # pool.close()
                    # print('Pool Closed')

                    from pebble import ProcessPool
                    from concurrent.futures import TimeoutError
                    with ProcessPool() as pool:
                        future = pool.map(simulate_single_path_worker_partial, paths, timeout=300)

                        iterator = future.result()
                        paths = []

                        while True:
                            try:
                                paths.append(next(iterator))
                            except StopIteration:
                                break
                            except TimeoutError as error:
                                print("THREAD TIMEOUT: function took longer than %d seconds" % error.args[1])
                            except Exception as error:
                                print("Simulation process raised %s" % error)
                                print(error.traceback)  # Python's traceback of remote process

                else:
                    new_paths = []
                    init_state = self.sim.clone_state().copy()
                    for path in paths:
                        # import pdb
                        # pdb.set_trace()
                        self.sim.restore_state(init_state)
                        new_path = simulate_single_path_worker(sim_func=self.sim.simulate,
                                                    slice_func=slice_dict,
                                                    reward_func=self.reward_function.give_reward,
                                                    reward_info_func=self.sim.get_reward_info,
                                                    sim_from_s0=self.sim_from_s0,
                                                    path=path)
                        new_paths.append(new_path)
                        # print(new_path['env_infos']['state'][:,9:13])

                    paths = new_paths

            # for path in paths:
            #     s_0 = path["observations"][0]
            #
            #     # actions = path['env_infos']['info']['actions']
            #     actions = path['actions']
            #     # pdb.set_trace()
            #     end_idx, info = self.sim.simulate(actions=actions, s_0=s_0)
            #     # print('----- Back from simulate: ', end_idx)
            #     if end_idx >= 0:
            #         # pdb.set_trace()
            #         self.slice_dict(path, end_idx)
            #     rewards = self.reward_function.give_reward(
            #         action=actions[end_idx],
            #         info=self.sim.get_reward_info()
            #     )
            #     # print('----- Back from rewards: ', rewards)
            #     # pdb.set_trace()
            #     path["rewards"][end_idx] = rewards
            #     # info[:, -1] = path["rewards"][:info.shape[0]]
            #     # path['env_infos']['sim_info'] = info
            #     path['env_infos']['sim_info'] = np.zeros_like(path["rewards"])
            #     # import pdb; pdb.set_trace()

        total_steps = 0
        for path in paths:
            total_steps += path['actions'].shape[0]

        tabular.record('TotalSteps', total_steps)

        return paths

    def slice_dict(self, in_dict, slice_idx):
        """Helper function to recursively parse through a dictionary of dictionaries and arrays to slice \
        the arrays at a certain index.

        Parameters
        ----------
        in_dict : dict
            Dictionary where the values are arrays or other dictionaries that follow this stipulation.
        slice_idx : int
            Index to slice each array at.

        Returns
        -------
        dict
            Dictionary where arrays at every level are sliced.

        """
        for key, value in in_dict.items():
            # pdb.set_trace()
            if isinstance(value, dict):
                in_dict[key] = self.slice_dict(value, slice_idx)
            else:
                in_dict[key][slice_idx + 1:, ...] = np.zeros_like(value[slice_idx + 1:, ...])

        return in_dict

max_stars_repo_name: mark-koren/AdaptiveStressTestingToolbox

max_issues_repo_name: mark-koren/AdaptiveStressTestingToolbox

max_forks_repo_name: mark-koren/AdaptiveStressTestingToolbox


text:
import os
import sys
import ctypes
import tifffile as tf
import numpy as np
import warnings
from contextlib import contextmanager

PLAT = sys.platform
if PLAT == 'linux2':
    PLAT = 'linux'
elif PLAT == 'cygwin':
    PLAT = 'win32'

def imread(fpath, **kwargs):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        return tf.imread(fpath, **kwargs)

def array_is_otf(arr):
    """Check whether a numpy array is likely an OTF file

    tifffile reads the otf file as a real-valued float32, instead
    of complex valued... so the second column is almost always 0
    Does this always work?
    """
    if arr.dtype != 'float32':
        return False
    if arr.shape[0] > arr.shape[1]:
        return False

    # the first pixel of an OTF will always be 1.0 and the second column 0s
    if arr[0, 0] == 1 and (not arr[:, 1].any()):
        return True

def path_is_otf(fpath):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        with tf.TiffFile(fpath) as tif:
            if (not len(tif.series)) or (tif.series[0].ndim != 2):
                return False
            return array_is_otf(tif.series[0].asarray())

def is_otf(arr_or_fpath):
    """
    accepts either a numpy array or a string representing a filepath
    and returns True if the array or path represents an OTF file
    """
    if isinstance(arr_or_fpath, str):
        # assume it's a filepath
        if os.path.isfile(arr_or_fpath):
            return path_is_otf(arr_or_fpath)
        else:
            raise FileNotFoundError('file path does not exist: {}'  # noqa
                                    .format(arr_or_fpath))
    elif isinstance(arr_or_fpath, np.ndarray):
        return array_is_otf(arr_or_fpath)
    return False

def getAbsoluteResourcePath(relativePath):
    """ Load relative path, in an environment agnostic way"""

    try:
        # PyInstaller stores data files in a tmp folder refered to as _MEIPASS
        basePath = sys._MEIPASS
    except Exception:
        # If not running as a PyInstaller created binary, try to find the data file as
        # an installed Python egg
        try:
            basePath = os.path.dirname(sys.modules['llspy'].__file__)
        except Exception:
            basePath = ''

        # If the egg path does not exist, assume we're running as non-packaged
        if not os.path.exists(os.path.join(basePath, relativePath)):
            basePath = 'llspy'

    path = os.path.join(basePath, relativePath)
    # If the path still doesn't exist, this function won't help you
    if not os.path.exists(path):
        return None

    return path

def load_lib(libname):
    """load shared library, searching a number of likely paths
    """
    # first just try to find it on the search path

    searchlist = [os.path.join(os.environ.get('CONDA_PREFIX', '.'), 'Library', 'bin'),
                  os.path.join(os.environ.get('CONDA_PREFIX', '.'), 'lib'),
                  './lib',
                  '.']

    ext = {'linux': '.so',
           'win32': '.dll',
           'darwin': '.dylib'}

    if not libname.endswith(('.so', '.dll', '.dylib')):
        libname += ext[PLAT]

    for f in searchlist:
        try:
            d = getAbsoluteResourcePath(f)
            return ctypes.CDLL(os.path.abspath(os.path.join(d, libname)))
        except Exception:
            continue

    #last resort, chdir into each dir
    curdir = os.path.abspath(os.curdir)
    for f in searchlist:
        try:
            d = os.path.abspath(getAbsoluteResourcePath(f))
            if os.path.isdir(d):
                os.chdir(d)
                lib = ctypes.CDLL(libname)
                os.chdir(curdir)
                return lib
            raise Exception('didn\'t find it')
        except Exception:
            continue

# https://stackoverflow.com/questions/5081657/how-do-i-prevent-a-c-shared-library-to-print-on-stdout-in-python/17954769#17954769
@contextmanager
def stdout_redirected(to=os.devnull):
    '''
    import os

    with stdout_redirected(to=filename):
        print("from Python")
        os.system("echo non-Python applications are also supported")
    '''
    fd = sys.stdout.fileno()

    # assert that Python and C stdio write using the same file descriptor
    # assert libc.fileno(ctypes.c_void_p.in_dll(libc, "stdout")) == fd == 1

    def _redirect_stdout(to):
        sys.stdout.close() # + implicit flush()
        os.dup2(to.fileno(), fd) # fd writes to 'to' file
        sys.stdout = os.fdopen(fd, 'w') # Python writes to fd

    with os.fdopen(os.dup(fd), 'w') as old_stdout:
        with open(to, 'w') as file:
            _redirect_stdout(to=file)
        try:
            yield # allow code to be run with the redirected stdout
        finally:
            _redirect_stdout(to=old_stdout) # restore stdout.
                                            # buffering and flags such as
                                            # CLOEXEC may be different

ext: py

max_stars_repo_name: Jun-NIBS/pycudadecon

max_issues_repo_name: Jun-NIBS/pycudadecon

max_forks_repo_name: Jun-NIBS/pycudadecon

reason: import numpy


text:
import importlib
import pytest
import json
import sys

import numpy as np

from delphi.GrFN.networks import GroundedFunctionNetwork
from delphi.GrFN.linking import make_link_tables
from delphi.translators.GrFN2WiringDiagram.translate import to_wiring_diagram

data_dir = "tests/data/GrFN/"
sys.path.insert(0, "tests/data/program_analysis")

@pytest.fixture
def crop_yield_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/crop_yield.f")

@pytest.fixture
def petpt_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/PETPT.for")

@pytest.fixture
def petasce_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/PETASCE_simple.for")

@pytest.fixture
def sir_simple_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/SIR-simple.f")

@pytest.fixture
def sir_gillespie_inline_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/SIR-Gillespie-SD_inline.f")

@pytest.fixture
def sir_gillespie_ms_grfn():
    return GroundedFunctionNetwork.from_fortran_file("tests/data/program_analysis/SIR-Gillespie-MS.f")

def test_petpt_creation_and_execution(petpt_grfn):
    A = petpt_grfn.to_agraph()
    A.draw("PETPT--GrFN.pdf", prog="dot")
    CAG = petpt_grfn.to_CAG_agraph()
    CAG.draw('PETPT--CAG.pdf', prog='dot')
    assert isinstance(petpt_grfn, GroundedFunctionNetwork)
    assert len(petpt_grfn.inputs) == 5
    assert len(petpt_grfn.outputs) == 1

    values = {name: 1.0 for name in petpt_grfn.inputs}
    res = petpt_grfn.run(values)
    assert res[0] == np.float32(0.029983712)

def test_petasce_creation(petasce_grfn):
    A = petasce_grfn.to_agraph()
    CAG = petasce_grfn.to_CAG_agraph()
    CG = petasce_grfn.to_call_agraph()
    A.draw('PETASCE--GrFN.pdf', prog='dot')
    CAG.draw('PETASCE--CAG.pdf', prog='dot')

    values = {
        "PETASCE_simple::@global::petasce::0::doy::-1": 20.0,
        "PETASCE_simple::@global::petasce::0::meevp::-1": "A",
        "PETASCE_simple::@global::petasce::0::msalb::-1": 0.5,
        "PETASCE_simple::@global::petasce::0::srad::-1": 15.0,
        "PETASCE_simple::@global::petasce::0::tmax::-1": 10.0,
        "PETASCE_simple::@global::petasce::0::tmin::-1": -10.0,
        "PETASCE_simple::@global::petasce::0::xhlai::-1": 10.0,
        "PETASCE_simple::@global::petasce::0::tdew::-1": 20.0,
        "PETASCE_simple::@global::petasce::0::windht::-1": 5.0,
        "PETASCE_simple::@global::petasce::0::windrun::-1": 450.0,
        "PETASCE_simple::@global::petasce::0::xlat::-1": 45.0,
        "PETASCE_simple::@global::petasce::0::xelev::-1": 3000.0,
        "PETASCE_simple::@global::petasce::0::canht::-1": 2.0,
    }

    res = petasce_grfn.run(values)
    assert res[0] == np.float32(0.00012496980836348878)

def test_crop_yield_creation(crop_yield_grfn):
    assert isinstance(crop_yield_grfn, GroundedFunctionNetwork)
    G = crop_yield_grfn.to_agraph()
    G.draw('crop_yield--GrFN.pdf', prog='dot')
    CAG = crop_yield_grfn.to_CAG_agraph()
    CAG.draw('crop_yield--CAG.pdf', prog='dot')

def test_sir_simple_creation(sir_simple_grfn):
    assert isinstance(sir_simple_grfn, GroundedFunctionNetwork)
    G = sir_simple_grfn.to_agraph()
    G.draw('SIR-simple--GrFN.pdf', prog='dot')
    CAG = sir_simple_grfn.to_CAG_agraph()
    CAG.draw('SIR-simple--CAG.pdf', prog='dot')
    lambdas = importlib.__import__(f"SIR-simple_lambdas")
    (D, I, S, F) = to_wiring_diagram(sir_simple_grfn, lambdas)
    assert len(D) == 3
    assert len(I) == 3
    assert len(S) == 9
    assert len(F) == 5

def test_sir_gillespie_inline_creation(sir_gillespie_inline_grfn):
    assert isinstance(sir_gillespie_inline_grfn, GroundedFunctionNetwork)
    G = sir_gillespie_inline_grfn.to_agraph()
    G.draw('SIR-Gillespie_inline--GrFN.pdf', prog='dot')
    CAG = sir_gillespie_inline_grfn.to_CAG_agraph()
    CAG.draw('SIR-Gillespie_inline--CAG.pdf', prog='dot')

def test_sir_gillespie_ms_creation(sir_gillespie_ms_grfn):
    assert isinstance(sir_gillespie_ms_grfn, GroundedFunctionNetwork)
    G = sir_gillespie_ms_grfn.to_agraph()
    G.draw('SIR-Gillespie_ms--GrFN.pdf', prog='dot')
    CAG = sir_gillespie_ms_grfn.to_CAG_agraph()
    CAG.draw('SIR-Gillespie_ms--CAG.pdf', prog='dot')

def test_linking_graph():
    grfn = json.load(open("tests/data/program_analysis/SIR-Gillespie-SD_GrFN_with_groundings.json", "r"))
    tables = make_link_tables(grfn)
    assert len(tables.keys()) == 207

@pytest.mark.skip("Need to update to latest JSON")
def test_petasce_torch_execution():
    lambdas = importlib.__import__("PETASCE_simple_torch_lambdas")
    pgm = json.load(open(data_dir + "PETASCE_simple_torch.json", "r"))
    G = GroundedFunctionNetwork.from_dict(pgm, lambdas)

    N = 100
    samples = {
        "petasce::doy_0": np.random.randint(1, 100, N),
        "petasce::meevp_0": np.where(np.random.rand(N) >= 0.5, 'A', 'W'),
        "petasce::msalb_0": np.random.uniform(0, 1, N),
        "petasce::srad_0": np.random.uniform(1, 30, N),
        "petasce::tmax_0": np.random.uniform(-30, 60, N),
        "petasce::tmin_0": np.random.uniform(-30, 60, N),
        "petasce::xhlai_0": np.random.uniform(0, 20, N),
        "petasce::tdew_0": np.random.uniform(-30, 60, N),
        "petasce::windht_0": np.random.uniform(0, 10, N),
        "petasce::windrun_0": np.random.uniform(0, 900, N),
        "petasce::xlat_0": np.random.uniform(0, 90, N),
        "petasce::xelev_0": np.random.uniform(0, 6000, N),
        "petasce::canht_0": np.random.uniform(0.001, 3, N),
    }

    values = {
        k: torch.tensor(v, dtype=torch.double) if v.dtype != "<U1" else v
        for k, v in samples.items()
    }

    res = G.run(values, torch_size=N)
    assert res.size()[0] == N

max_stars_repo_name: mwdchang/delphi

max_issues_repo_name: mwdchang/delphi

max_forks_repo_name: mwdchang/delphi


text:
import glob, os, sys
import numpy as np
import scipy.integrate as integrate
import scipy.ndimage
import matplotlib.pylab as plt
from scipy.interpolate import griddata

def integra(x,y,method='simps'):
        fullHyst = x[-1] == x[0]
        if fullHyst:
           middle=int(np.round(x.size/2))
           top=int(np.round(x.size))
           if method=='simps':
               branchup=integrate.simps(y[0:middle],x[0:middle])
               branchdown=integrate.simps(y[middle:top],x[middle:top])
           else:
               branchup=integrate.trapz(y[0:middle],x[0:middle])
               branchdown=integrate.trapz(y[middle:top],x[middle:top])
           result=(-branchdown-branchup)/2
        else:
            if method=='simps':
                branchdown=integrate.simps(y,x)
                branchup=integrate.simps(-np.flipud(y),-np.flipud(x))
            else:
                branchdown=integrate.trapz(y,x)
                branchup=integrate.trapz(-np.flipud(y),-np.flipud(x))
            result=(-branchdown+branchup)/2
        return result

class Dist:
    """
    This class load the data given a filename
    and gives the possibility to generate a plot with the uploaded data
    """
    def __init__(self, filename, is_avoid_zeros=True):
        # It is better to make general x,y arrays

        if not os.path.isfile(filename):
            filename='%s %s' %(filename.split(".dat",1)[0],".DAT")
            if not os.path.isfile(filename):
                print("%s file do not exists" % (filename))
                self.x, self.y=[0,0]
            else:
                self.x, self.y = np.loadtxt(filename, comments="#", unpack=True, usecols=(0,1))
                if is_avoid_zeros:
                    s_len = len(self.x)
                    self.x, self.y = self.avoid_zeros()
                    print("%i lines deleted" % (s_len - len(self.x)))
                    self.x, self.y = self.avoid_rep()
        else:
            self.x, self.y = np.loadtxt(filename, comments="#", unpack=True, usecols=(0,1))
            if is_avoid_zeros:
                s_len = len(self.x)
                self.x, self.y = self.avoid_zeros()
                print("%i lines deleted" % (s_len - len(self.x)))
                self.x, self.y = self.avoid_rep()

    def avoid_zeros(self):
        is_not_zero = self.y != 0
        x = self.x[is_not_zero]
        y = self.y[is_not_zero]
        return x, y

    def plot(self, loglog=True):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        if loglog:
            ax.loglog(self.x, self.y, 'o')
        else:
            ax.plot(self.x, self.y, 'o')

    def avoid_rep(self):
        j=1
        x=self.x
        y=self.y
        for i in range (2,len(self.x)):
            if (self.x[i]!=self.x[i-1]):
                x[j] = self.x[i]
                y[j] = self.y[i]
                j=j+1
        return x[1:j], y[1:j]

class Integral:
    """
    Standalone version of integrate class
    This class load the data given a filename and integrates the curve
    """
    def __init__(self, filename, mainDir, is_avoid_zeros=True):
        self._mainDir = mainDir
        fname = os.path.join(self._mainDir, filename)
        self.dist=Dist(fname,is_avoid_zeros)
        # It is better to make general x,y arrays
        value=integra( self.dist.x,  self.dist.y)
        self.energy=2*4*np.pi*1.e-7*value

if __name__ == "__main__":
    #mainDir = "W:\\Micro\\Riccardo\\3D\\Mumax_dot_pillars"
    mainDir = "W:\\Micro\\Riccardo\\3D\\dot\\150\\Angles_new"
    #mainDir = "W:\\Micro\\2d3d\\dot680\\Hysteresis"
    #mainDir = "W:\\Micro\\Riccardo\\cfr2d3d_3d_random\\3d\\completed\\media"
    #mainDir = "W:\\Micro\\2d3d\\dot150\\n54"
    #filename = "ring_Hyst_150w03t30.txt"
    #mainDir = "S:\\Alessandra\\2d3d"
    filename = "dot150_s25_a00_hyst.dat"
    integ=Integral(filename,mainDir)
    dati=np.array([])
    dati=np.append(dati,(int(150),int(00),integ.energy))
    dati=np.reshape(dati,(-1,3))
    print(dati)
    outputfile=filename.split(".", 1)[0]+".dat"
    outputfile="Energy_"+outputfile
    np.savetxt(os.path.join(mainDir, outputfile),dati,fmt='%4d %4d  %12.8e',header='diametro spessore energia')
    print(integ.energy)

reason: import numpy,import scipy,from scipy


text:
"""The ReLU function is a common activation function used in artifical neural netoworks.

```
relu(x) = max(0, x)
```

The Leaky ReLU function is a variant of the ReLU function designed to avoid
having nodes "die". Its definition is as follows.

```
leaky_relu(x) = max(0.1x, x)
```

This problem attempts to synthesize a program that computes the output of both
the ReLU and LeakyReLU functions.

"""
import numpy as np
import random

from pyshgp.gp.estimators import PushEstimator
from pyshgp.gp.genome import GeneSpawner
from pyshgp.gp.selection import Lexicase
from pyshgp.push.instruction_set import InstructionSet

def target_function(x: float) -> (float, float):
    """Generate a training data point."""
    return max(0.0, x), max(0.1 * x, x)

X = np.arange(-1.0, 1.0, 0.05).reshape([-1, 1])
y = np.array([target_function(x[0]) for x in X])

spawner = GeneSpawner(
    n_inputs=1,
    instruction_set=InstructionSet().register_core_by_stack({"float", "bool"}),
    literals=[0.1, 0.0],
    erc_generators=[
        lambda: random.randint(0, 10),
    ]
)

ep_lex_sel = Lexicase(epsilon=True)

if __name__ == "__main__":
    est = PushEstimator(
        population_size=300,
        max_generations=50,
        simplification_steps=500,
        spawner=spawner,
        selector=ep_lex_sel,
        verbose=2
    )

    est.fit(X=X, y=y)

max_stars_repo_name: RedBeansAndRice/pyshgp

max_issues_repo_name: RedBeansAndRice/pyshgp

max_forks_repo_name: RedBeansAndRice/pyshgp


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# author : Santosh Kesiraju
# e-mail : kcraj2[AT]gmail[DOT]com, kesiraju[AT]fit[DOT]vutbr[DOT]cz
# Date created : 08 Jun 2021
# Last modified : 08 Jun 2021

"""
Get unique words from the input transcription file
"""

import os
import sys
import argparse
import numpy as np
from auto_utils import arrange_into_freq_bins, get_wfreq

def get_word_count(fname):

    wcount = {}
    with open(fname, "r", encoding="utf-8") as fpr:
        for line in fpr:
            parts = line.strip().split()
            for word in parts[1:]:
                try:
                    wcount[word] += 1
                except KeyError:
                    wcount[word] = 1
    return wcount

def main():
    """ main method """

    args = parse_arguments()

    wcount = get_word_count(args.trans_file)
    print("uniq words:", len(wcount))

    with open(args.out_file, 'w', encoding='utf-8') as fpw:
        i = 1
        for w in wcount:
            fpw.write(str(i) + " " + w + "\n")
            i += 1
    print(args.out_file, 'saved.')

    counts = get_wfreq(wcount)
    arrange_into_freq_bins(counts, 25)

    if args.wc:
        np.savetxt(args.wc, counts, fmt="%d")

def parse_arguments():
    """ parse command line arguments """

    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("trans_file", help="path to transcription file")
    parser.add_argument("out_file", help="out file to save words")
    parser.add_argument("-wc", help="save word counts to this file")
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    main()

max_stars_repo_name: skesiraju/indic-kws

max_issues_repo_name: skesiraju/indic-kws

max_forks_repo_name: skesiraju/indic-kws


text:
import numpy as np

class MockGeometry(object):

    def __init__(self, pixel_centres=None, pixel_neighbors=np.array([1]), pixel_neighbors_size=np.array([1])):

        self.pixel_scales = (1.0, 1.0)
        self.origin = (0.0, 0.0)
        self.pixel_centres = pixel_centres

        self.pixel_neighbors = pixel_neighbors.astype('int')
        self.pixel_neighbors_size = pixel_neighbors_size.astype('int')

class MockPixelization(object):

    def __init__(self, value):
        self.value = value

    # noinspection PyUnusedLocal,PyShadowingNames
    def mapper_from_grid_stack_and_border(self, grid_stack, border):
        return self.value

    # noinspection PyUnusedLocal,PyShadowingNames
    def mapper_from_grid_stack(self, grid_stack):
        return self.value

class MockRegularization(object):

    def __init__(self, value):
        self.value = value

class MockMapper(object):

    def __init__(self):
        self.mapping_matrix = np.ones((1, 1))
        self.regularization_matrix = np.ones((1, 1))
        self.geometry = MockGeometry()

class MockConvolver(object):

    def __init__(self, matrix_shape):
        self.shape = matrix_shape

    def convolve_mapping_matrix(self, mapping_matrix):
        return np.ones(self.shape)

class MockInversion(object):

    def __init__(self):
        self.blurred_mapping_matrix = np.zeros((1, 1))
        self.regularization_matrix = np.zeros((1, 1))
        self.curvature_matrix = np.zeros((1, 1))
        self.curvature_reg_matrix = np.zeros((1, 1))
        self.solution_vector = np.zeros((1))

    @property
    def reconstructed_image(self):
        return np.zeros((1, 1))

max_stars_repo_name: AshKelly/PyAutoLens

max_issues_repo_name: AshKelly/PyAutoLens

max_forks_repo_name: AshKelly/PyAutoLens


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

from collections.abc import Container, KeysView, Mapping, MutableMapping
from numbers import Number
from pprint import PrettyPrinter

import numpy as np

from dimod.sampleset import as_samples
from dimod.vartypes import as_vartype, Vartype

from dimod.bqm.utils import cylinear_min, cylinear_max, cylinear_sum
from dimod.bqm.utils import cyquadratic_min, cyquadratic_max, cyquadratic_sum
from dimod.bqm.utils import cyneighborhood_max, cyneighborhood_min, cyneighborhood_sum

__all__ = ['BQM', 'ShapeableBQM']

class BQMView(Mapping):
    __slots__ = ['_bqm']

    def __init__(self, bqm):
        self._bqm = bqm

    # support python2 pickle
    def __getstate__(self):
        return {'_bqm': self._bqm}

    # support python2 pickle
    def __setstate__(self, state):
        self._bqm = state['_bqm']

    def __repr__(self):
        # let's just print the whole (potentially massive) thing for now, in
        # the future we'd like to do something a bit more clever (like hook into
        # dimod's Formatter)
        stream = io.StringIO()
        stream.write('{')
        last = len(self) - 1
        for i, (key, value) in enumerate(self.items()):
            stream.write('{!s}: {!s}'.format(key, value))
            if i != last:
                stream.write(', ')
        stream.write('}')
        return stream.getvalue()

class Adjacency(BQMView):
    """Quadratic biases as a nested dict of dicts.

    Accessed like a dict of dicts, where the keys of the outer dict are all
    of the model's variables (e.g. `v`) and the values are the neighborhood of
    `v`. Each neighborhood if a dict where the keys are the neighbors of `v`
    and the values are their associated quadratic biases.
    """
    def __getitem__(self, v):
        if not self._bqm.has_variable(v):
            raise KeyError('{} is not a variable'.format(v))
        return Neighborhood(self._bqm, v)

    def __iter__(self):
        return self._bqm.iter_variables()

    def __len__(self):
        return self._bqm.num_variables

class ShapeableAdjacency(Adjacency):
    def __getitem__(self, v):
        if not self._bqm.has_variable(v):
            raise KeyError('{} is not a variable'.format(v))
        return ShapeableNeighborhood(self._bqm, v)

class Neighborhood(BQMView):
    __slots__ = ['_var']

    def __init__(self, bqm, v):
        super().__init__(bqm)
        self._var = v

    def __getitem__(self, v):
        try:
            return self._bqm.get_quadratic(self._var, v)
        except ValueError as e:
            raise KeyError(*e.args)

    def __iter__(self):
        return self._bqm.iter_neighbors(self._var)

    def __len__(self):
        return self._bqm.degree(self._var)

    def __setitem__(self, v, bias):
        self._bqm.set_quadratic(self._var, v, bias)

    def max(self, default=None):
        """The maximum quadratic bias in the neighborhood."""
        try:
            return cyneighborhood_max(self._bqm, self._var, default)
        except TypeError:
            pass

        generator = (b for _, _, b in self._bqm.iter_quadratic(self._var))

        if default is None:
            return max(generator)
        else:
            return max(generator, default=default)

    def min(self, default=None):
        """The minimum quadratic bias in the neighborhood."""
        try:
            return cyneighborhood_min(self._bqm, self._var, default)
        except TypeError:
            pass

        generator = (b for _, _, b in self._bqm.iter_quadratic(self._var))

        if default is None:
            return min(generator)
        else:
            return min(generator, default=default)

    def sum(self, start=0):
        """The sum of the quadratic biases in the neighborhood."""
        try:
            return cyneighborhood_sum(self._bqm, self._var, start)
        except TypeError:
            pass

        return sum((b for _, _, b in self._bqm.iter_quadratic(self._var)),
                   start)

class ShapeableNeighborhood(Neighborhood, MutableMapping):
    def __delitem__(self, v):
        self._bqm.remove_interaction(self._var, v)

class Linear(BQMView):
    """Linear biases as a mapping.

    Accessed like a dict, where keys are the variables of the binary quadratic
    model and values are the linear biases.
    """
    __slots__ = ['_bqm']

    def __init__(self, bqm):
        self._bqm = bqm

    def __getitem__(self, v):
        try:
            return self._bqm.get_linear(v)
        except ValueError as e:
            raise KeyError(*e.args)

    def __iter__(self):
        return self._bqm.iter_variables()

    def __len__(self):
        return len(self._bqm)

    def __setitem__(self, v, bias):
        # inherits its ability to reshape the bqm from the `.set_linear` method
        self._bqm.set_linear(v, bias)

    def min(self, default=None):
        """Returns the minimum linear bias."""
        try:
            return cylinear_min(self._bqm, default=default)
        except TypeError:
            pass

        if default is None:
            return min(self.values())
        else:
            return min(self.values(), default=default)

    def max(self, default=None):
        """Returns the maximum linear bias."""
        try:
            return cylinear_max(self._bqm, default=default)
        except TypeError:
            pass

        if default is None:
            return max(self.values())
        else:
            return max(self.values(), default=default)

    def sum(self, start=0):
        """Return the sum of the linear biases."""
        try:
            return cylinear_sum(self._bqm, start=start)
        except TypeError:
            pass

        return sum(self.values(), start)

class ShapeableLinear(Linear, MutableMapping):
    def __delitem__(self, v):
        try:
            self._bqm.remove_variable(v)
        except ValueError:
            raise KeyError(repr(v))

class Quadratic(BQMView):
    """Quadratic biases as a flat mapping.

    Accessed like a dict, where keys are 2-tuples of varables, which represent
    an interaction and values are the quadratic biases.
    """
    def __getitem__(self, uv):
        try:
            return self._bqm.get_quadratic(*uv)
        except ValueError as e:
            raise KeyError(*e.args)

    def __iter__(self):
        return self._bqm.iter_interactions()

    def __len__(self):
        return self._bqm.num_interactions

    def __setitem__(self, uv, bias):
        # inherits its ability to reshape the bqm from the `.set_linear` method
        u, v = uv
        self._bqm.set_quadratic(u, v, bias)

    def min(self, default=None):
        """Returns the minimum quadratic bias."""
        try:
            return cyquadratic_min(self._bqm, default=default)
        except TypeError:
            pass

        if default is None:
            return min(self.values())
        else:
            return min(self.values(), default=default)

    def max(self, default=None):
        """Returns the maximum quadratic bias."""
        try:
            return cyquadratic_max(self._bqm, default=default)
        except TypeError:
            pass

        if default is None:
            return max(self.values())
        else:
            return max(self.values(), default=default)

    def sum(self, start=0):
        """Return the sum of the quadratic biases."""
        try:
            return cyquadratic_sum(self._bqm, start=start)
        except TypeError:
            pass

        return sum(self.values(), start)

class ShapeableQuadratic(Quadratic, MutableMapping):
    def __delitem__(self, uv):
        try:
            self._bqm.remove_interaction(*uv)
        except ValueError:
            raise KeyError(repr(uv))

class BQM(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def __init__(self, obj):
        pass

    def __eq__(self, other):
        try:
            return (self.vartype == other.vartype
                    and self.shape == other.shape  # not necessary but fast to check
                    and self.offset == other.offset
                    and self.linear == other.linear
                    and self.adj == other.adj)
        except AttributeError:
            return False

    def __ne__(self, other):
        return not (self == other)

    @abc.abstractproperty
    def num_interactions(self):
        """int: The number of interactions in the model."""
        pass

    @abc.abstractproperty
    def num_variables(self):
        """int: The number of variables in the model."""
        pass

    @abc.abstractproperty
    def offset(self):
        """The constant energy offset associated with the model."""
        pass

    @abc.abstractproperty
    def vartype(self):
        """The variable type, :class:`.Vartype.SPIN` or
        :class:`.Vartype.BINARY`.
        """
        pass

    @abc.abstractmethod
    def change_vartype(self, vartype, inplace=True):
        """Return a binary quadratic model with the specified vartype."""
        pass

    @abc.abstractmethod
    def degree(self, v):
        pass

    @abc.abstractmethod
    def get_linear(self, v):
        pass

    @abc.abstractmethod
    def get_quadratic(self, u, v):
        pass

    @abc.abstractmethod
    def iter_linear(self):
        pass

    @abc.abstractmethod
    def iter_quadratic(self, variables=None):
        pass

    @abc.abstractmethod
    def relabel_variables(self, mapping, inplace=True):
        pass

    @abc.abstractmethod
    def set_linear(self, u, v):
        pass

    @abc.abstractmethod
    def set_quadratic(self, u, v, bias):
        pass

    # mixins

    def __len__(self):
        """The number of variables in the binary quadratic model."""
        return self.num_variables

    def __repr__(self):
        return "{!s}({!s}, {!s}, {!r}, {!r})".format(type(self).__name__,
                                                     self.linear,
                                                     self.quadratic,
                                                     self.offset,
                                                     self.vartype.name)

    @property
    def adj(self):
        return Adjacency(self)
    adj.__doc__ = Adjacency.__doc__

    @property
    def base(self):
        """The base binary quadratic model, itself if not a view."""
        return self

    @property
    def binary(self):
        """The binary-valued version of the binary quadratic model.

        If the binary quadratic model is binary-valued, this references itself,
        otherwise it is a :class:`.BinaryView`.
        """
        if self.vartype is Vartype.BINARY:
            return self

        try:
            return self._binary
        except AttributeError:
            pass

        # this may be kept around even if self.vartype is changed, but that's
        # covered by the above check
        self._binary = binary = BinaryView(self)
        return binary

    @property
    def linear(self):
        return Linear(self)
    linear.__doc__ = Linear.__doc__

    @property
    def quadratic(self):
        return Quadratic(self)
    quadratic.__doc__ = Quadratic.__doc__

    @property
    def shape(self):
        """A 2-tuple, the :attr:`num_variables` and :attr:`num_interactions`."""
        return self.num_variables, self.num_interactions

    @property
    def spin(self):
        """The spin-valued version of the binary quadratic model.

        If the binary quadratic model is spin-valued, this references itself,
        otherwise it is a :class:`.SpinView`.
        """
        if self.vartype is Vartype.SPIN:
            return self

        try:
            return self._spin
        except AttributeError:
            pass

        # this may be kept around even if self.vartype is changed, but that's
        # covered by the above check
        self._spin = spin = SpinView(self)
        return spin

    @property
    def variables(self):
        """The variables of the binary quadratic model."""
        return KeysView(self.linear)

    def add_offset(self, offset):
        """Add specified value to the offset of a binary quadratic model."""
        self.offset += offset

    def remove_offset(self):
        """Set the binary quadratic model's offset to zero."""
        # maintain type
        self.offset -= self.offset

    # This may be overwritten for speed
    def copy(self, deep=False):
        """Return a copy."""
        return copy.deepcopy(self) if deep else copy.copy(self)

    def degrees(self, array=False, dtype=np.int):
        if array:
            return np.fromiter((self.degree(v) for v in self.iter_variables()),
                               count=len(self), dtype=dtype)
        return {v: self.degree(v) for v in self.iter_variables()}

    @classmethod
    def empty(cls, vartype):
        """Create a new empty binary quadratic model."""
        return cls(vartype)

    def energies(self, samples_like, dtype=None):
        """Determine the energies of the given samples.

        Args:
            samples_like (samples_like):
                A collection of raw samples. `samples_like` is an extension of
                NumPy's array_like structure. See :func:`.as_samples`.

            dtype (:class:`numpy.dtype`, optional):
                The data type of the returned energies.

        Returns:
            :obj:`numpy.ndarray`: The energies.

        """
        samples, labels = as_samples(samples_like)

        ldata, (irow, icol, qdata), offset \
            = self.to_numpy_vectors(variable_order=labels, dtype=dtype)

        energies = samples.dot(ldata) + (samples[:, irow]*samples[:, icol]).dot(qdata) + offset
        return np.asarray(energies, dtype=dtype)  # handle any type promotions

    def energy(self, sample, dtype=None):
        energy, = self.energies(sample, dtype=dtype)
        return energy

    def flip_variable(self, v):
        """Flip variable v in a binary quadratic model.

        Args:
            v (variable):
                Variable in the binary quadratic model.

        """
        for u in self.adj[v]:
            self.spin.adj[v][u] *= -1
        self.spin.linear[v] *= -1

    @classmethod
    def from_coo(cls, obj, vartype=None):
        """Deserialize a binary quadratic model from a COOrdinate format encoding.

        COOrdinate_ is a sparse encoding for binary quadratic models.

        .. _COOrdinate: https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)

        Args:
            obj: (str/file):
                Either a string or a `.read()`-supporting `file object`_ that represents
                linear and quadratic biases for a binary quadratic model. This data
                is stored as a list of 3-tuples, (i, j, bias), where :math:`i=j`
                for linear biases.

            vartype (:class:`.Vartype`/str/set, optional):
                Variable type for the binary quadratic model. Accepted input values:

                * :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
                * :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``

                If not provided, the vartype must be specified with a header in the
                file.

        .. _file object: https://docs.python.org/3/glossary.html#term-file-object

        .. note:: Variables must use index lables (numeric lables). Binary quadratic
            models created from COOrdinate format encoding have offsets set to
            zero.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :func:`~dimod.serialization.coo.load` or
            :func:`~dimod.serialization.coo.loads` directly.

        """
        import dimod.serialization.coo as coo

        if isinstance(obj, str):
            return coo.loads(obj, cls=cls, vartype=vartype)

        return coo.load(obj, cls=cls, vartype=vartype)

    @classmethod
    def from_ising(cls, h, J, offset=0):
        """Create a binary quadratic model from an Ising problem.

        Args:
            h (dict/list):
                Linear biases of the Ising problem. If a dict, should be of the
                form `{v: bias, ...}` where v is a spin-valued variable and `bias`
                is its associated bias. If a list, it is treated as a list of
                biases where the indices are the variable labels.

            J (dict[(variable, variable), bias]):
                Quadratic biases of the Ising problem.

            offset (optional, default=0.0):
                Constant offset applied to the model.

        Returns:
            A spin-valued binary quadratic model.

        """
        return cls(h, J, offset, Vartype.SPIN)

    @classmethod
    def from_networkx_graph(cls, G, vartype=None, node_attribute_name='bias',
                            edge_attribute_name='bias'):
        """Create a binary quadratic model from a NetworkX graph.

        Args:
            G (:obj:`networkx.Graph`):
                A NetworkX graph with biases stored as node/edge attributes.

            vartype (:class:`.Vartype`/str/set, optional):
                Variable type for the binary quadratic model. Accepted input
                values:

                * :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
                * :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``

                If not provided, the `G` should have a vartype attribute. If
                `vartype` is provided and `G.vartype` exists then the argument
                overrides the property.

            node_attribute_name (hashable, optional, default='bias'):
                Attribute name for linear biases. If the node does not have a
                matching attribute then the bias defaults to 0.

            edge_attribute_name (hashable, optional, default='bias'):
                Attribute name for quadratic biases. If the edge does not have a
                matching attribute then the bias defaults to 0.

        Returns:
            Binary quadratic model

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use the :func:`.from_networkx_graph` function.

        """
        from dimod.converters import from_networkx_graph  # avoid circular import
        return from_networkx_graph(G, vartype, node_attribute_name,
                                   edge_attribute_name, cls=cls)

    @classmethod
    def from_numpy_matrix(cls, mat, variable_order=None, offset=0.0,
                          interactions=None):
        """Create a binary quadratic model from a NumPy array.

        Args:
            mat (:class:`numpy.ndarray`):
                Coefficients of a quadratic unconstrained binary optimization
                (QUBO) model formatted as a square NumPy 2D array.

            variable_order (list, optional):
                If provided, labels the QUBO variables; otherwise, row/column
                indices are used. If `variable_order` is longer than the array,
                extra values are ignored.

            offset (optional, default=0.0):
                Constant offset for the binary quadratic model.

            interactions (iterable, optional, default=[]):
                Any additional 0.0-bias interactions to be added to the binary
                quadratic model. Only works for shapeable binary quadratic
                models.

        Returns:
            Binary quadratic model with vartype set to :class:`.Vartype.BINARY`.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use the constructor directly.

        """
        bqm = cls(mat, Vartype.BINARY)
        bqm.offset = offset

        if variable_order is not None:
            bqm.relabel_variables(dict(enumerate(variable_order)))

        if interactions is not None:
            for u, v in interactions:
                bqm.add_interaction(u, v, 0.0)

        return bqm

    @classmethod
    def from_numpy_vectors(cls, linear, quadratic, offset, vartype, variable_order=None):
        """Create a binary quadratic model from vectors.

        Args:
            linear (array_like):
                A 1D array-like iterable of linear biases.

            quadratic (tuple[array_like, array_like, array_like]):
                A 3-tuple of 1D array_like vectors of the form (row, col, bias).

            offset (numeric, optional):
                Constant offset for the binary quadratic model.

            vartype (:class:`.Vartype`/str/set):
                Variable type for the binary quadratic model. Accepted input values:

                * :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
                * :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``

            variable_order (iterable, optional):
                If provided, labels the variables; otherwise, indices are used.

        Returns:
            A binary quadratic model

        """

        try:
            heads, tails, values = quadratic
        except ValueError:
            raise ValueError("quadratic should be a 3-tuple")

        if not len(heads) == len(tails) == len(values):
            raise ValueError("row, col, and bias should be of equal length")

        if variable_order is None:
            variable_order = list(range(len(linear)))

        linear = {v: float(bias) for v, bias in zip(variable_order, linear)}
        quadratic = {(variable_order[u], variable_order[v]): float(bias)
                     for u, v, bias in zip(heads, tails, values)}

        return cls(linear, quadratic, offset, vartype)

    @classmethod
    def from_qubo(cls, Q, offset=0):
        """Create a binary quadratic model from a QUBO problem.

        Args:
            Q (dict):
                Coefficients of a quadratic unconstrained binary optimization
                (QUBO) problem. Should be a dict of the form `{(u, v): bias, ...}`
                where `u`, `v`, are binary-valued variables and `bias` is their
                associated coefficient.

            offset (optional, default=0.0):
                Constant offset applied to the model.

        Returns:
            A binary-valued binary quadratic model.

        """
        return cls({}, Q, offset, Vartype.BINARY)

    def has_variable(self, v):
        """Return True if v is a variable in the binary quadratic model."""
        try:
            self.get_linear(v)
        except (ValueError, TypeError):
            return False
        return True

    def iter_variables(self):
        """Iterate over the variables of the binary quadratic model.

        Yields:
            hashable: A variable in the binary quadratic model.

        """
        for v, _ in self.iter_linear():
            yield v

    def iter_interactions(self):
        """Iterate over the interactions of the binary quadratic model.

        Yields:
            interaction: An interaction in the binary quadratic model.

        """
        for u, v, _ in self.iter_quadratic():
            yield u, v

    def iter_neighbors(self, u):
        """Iterate over neighbors of a variable in the binary quadratic model.

        Yields:
            variable: The neighbors of `v`.

        """
        for _, v, _ in self.iter_quadratic(u):
            yield v

    def normalize(self, bias_range=1, quadratic_range=None,
                  ignored_variables=None, ignored_interactions=None,
                  ignore_offset=False):
        """Normalizes the biases of the binary quadratic model such that they
        fall in the provided range(s), and adjusts the offset appropriately.

        If `quadratic_range` is provided, then `bias_range` will be treated as
        the range for the linear biases and `quadratic_range` will be used for
        the range of the quadratic biases.

        Args:
            bias_range (number/pair):
                Value/range that the biases of the BQM will be scaled to fit
                within. If `quadratic_range` is provided, this range is
                used to fit the linear biases.

            quadratic_range (number/pair):
                The BQM will be scaled so that the quadratic biases fit within
                this range.

            ignored_variables (iterable, optional):
                Biases associated with these variables are not scaled.

            ignored_interactions (iterable[tuple], optional):
                As an iterable of 2-tuples. Biases associated with these
                interactions are not scaled.

            ignore_offset (bool, default=False):
                If True, the offset is not scaled.

        """

        def parse_range(r):
            if isinstance(r, Number):
                return -abs(r), abs(r)
            return r

        def min_and_max(iterable):
            if not iterable:
                return 0, 0
            return min(iterable), max(iterable)

        if ignored_variables is None:
            ignored_variables = set()
        elif not isinstance(ignored_variables, Container):
            ignored_variables = set(ignored_variables)

        if ignored_interactions is None:
            ignored_interactions = set()
        elif not isinstance(ignored_interactions, Container):
            ignored_interactions = set(ignored_interactions)

        if quadratic_range is None:
            linear_range, quadratic_range = bias_range, bias_range
        else:
            linear_range = bias_range

        lin_range, quad_range = map(parse_range, (linear_range,
                                                  quadratic_range))

        lin_min, lin_max = min_and_max([v for k, v in self.linear.items()
                                        if k not in ignored_variables])
        quad_min, quad_max = min_and_max([v for (a, b), v in self.quadratic.items()
                                          if ((a, b) not in ignored_interactions
                                              and (b, a) not in
                                              ignored_interactions)])

        inv_scalar = max(lin_min / lin_range[0], lin_max / lin_range[1],
                         quad_min / quad_range[0], quad_max / quad_range[1])

        if inv_scalar != 0:
            self.scale(1 / inv_scalar, ignored_variables=ignored_variables,
                       ignored_interactions=ignored_interactions,
                       ignore_offset=ignore_offset)

            return 1.0 / inv_scalar
        else:
            return 1.0

    def relabel_variables_as_integers(self, inplace=True):
        """Relabel the variables of the BQM to integers.

        Args:
            inplace (bool, optional, default=True):
                If True, the binary quadratic model is updated in-place;
                otherwise, a new binary quadratic model is returned.

        Returns:
            tuple: A 2-tuple containing:

                A binary quadratic model with the variables relabeled. If
                `inplace` is set to True, returns itself.

                dict: The mapping that will restore the original labels.

        """
        if not inplace:
            return self.copy().relabel_variables_as_integers(inplace=True)

        mapping = dict((v, i) for i, v in enumerate(self.variables) if i != v)
        return (self.relabel_variables(mapping, inplace=True),
                dict((i, v) for v, i in mapping.items()))

    def scale(self, scalar, ignored_variables=None, ignored_interactions=None,
              ignore_offset=False):
        """Multiply all the biases by the specified scalar.

        Args:
            scalar (number):
                Value by which to scale the energy range of the binary
                quadratic model.

            ignored_variables (iterable, optional):
                Biases associated with these variables are not scaled.

            ignored_interactions (iterable[tuple], optional):
                As an iterable of 2-tuples. Biases associated with these
                interactions are not scaled.

            ignore_offset (bool, default=False):
                If True, the offset is not scaled.

        """

        if ignored_variables is None:
            ignored_variables = set()
        elif not isinstance(ignored_variables, Container):
            ignored_variables = set(ignored_variables)

        if ignored_interactions is None:
            ignored_interactions = set()
        elif not isinstance(ignored_interactions, Container):
            ignored_interactions = set(ignored_interactions)

        linear = self.linear
        for v in linear:
            if v in ignored_variables:
                continue
            linear[v] *= scalar

        quadratic = self.quadratic
        for u, v in quadratic:
            if (u, v) in ignored_interactions or (v, u) in ignored_interactions:
                continue
            quadratic[(u, v)] *= scalar

        if not ignore_offset:
            self.offset *= scalar

    @classmethod
    def shapeable(cls):
        return issubclass(cls, ShapeableBQM)

    def to_coo(self, fp=None, vartype_header=False):
        """Serialize the binary quadratic model to a COOrdinate format encoding.

        COOrdinate_ is a sparse encoding for binary quadratic models.

        .. _COOrdinate: https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)

        Args:
            fp (file, optional):
                `.write()`-supporting `file object`_ to save the linear and quadratic biases
                of a binary quadratic model to. The model is stored as a list of 3-tuples,
                (i, j, bias), where :math:`i=j` for linear biases. If not provided,
                returns a string.

            vartype_header (bool, optional, default=False):
                If true, the binary quadratic model's variable type as prepended to the
                string or file as a header.

        .. _file object: https://docs.python.org/3/glossary.html#term-file-object

        .. note:: Variables must use index lables (numeric lables). Binary quadratic
            models saved to COOrdinate format encoding do not preserve offsets.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :func:`~dimod.serialization.coo.dump` or
            :func:`~dimod.serialization.coo.dumps` directly.

        """
        import dimod.serialization.coo as coo

        if fp is None:
            return coo.dumps(self, vartype_header)
        else:
            coo.dump(self, fp, vartype_header)

    def to_ising(self):
        """Converts a binary quadratic model to Ising format.

        If the binary quadratic model's vartype is not :class:`.Vartype.SPIN`,
        values are converted.

        Returns:
            tuple: 3-tuple of form (`linear`, `quadratic`, `offset`), where
            `linear` is a dict of linear biases, `quadratic` is a dict of
            quadratic biases, and `offset` is a number that represents the
            constant offset of the binary quadratic model.

        """
        bqm = self.spin
        return dict(bqm.linear), dict(bqm.quadratic), bqm.offset

    def to_networkx_graph(self, node_attribute_name='bias',
                          edge_attribute_name='bias'):
        """Convert a binary quadratic model to NetworkX graph format.

        Args:
            node_attribute_name (hashable, optional, default='bias'):
                Attribute name for linear biases.

            edge_attribute_name (hashable, optional, default='bias'):
                Attribute name for quadratic biases.

        Returns:
            :class:`networkx.Graph`: A NetworkX graph with biases stored as
            node/edge attributes.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :func:`.to_networkx_graph`.

        """
        from dimod.converters import to_networkx_graph  # avoid circular import
        return to_networkx_graph(self, node_attribute_name, edge_attribute_name)

    def to_numpy_matrix(self, variable_order=None):
        """Convert a binary quadratic model to NumPy 2D array.

        Args:
            variable_order (list, optional):
                If provided, indexes the rows/columns of the NumPy array. If
                `variable_order` includes any variables not in the binary
                quadratic model, these are added to the NumPy array.

        Returns:
            :class:`numpy.ndarray`: The binary quadratic model as a NumPy 2D
            array. Note that the binary quadratic model is converted to
            :class:`~.Vartype.BINARY` vartype.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :meth:`.to_dense`.

        """
        num_variables = self.num_variables
        M = np.zeros((num_variables, num_variables), dtype=self.base.dtype)

        if variable_order is None:
            variable_order = range(num_variables)
        elif len(variable_order) != num_variables:
            raise ValueError("variable_order does not include all variables")

        label_to_idx = {v: i for i, v in enumerate(self.variables)}

        # do the dense thing
        for ui, u in enumerate(variable_order):
            try:
                M[ui, ui] = self.binary.linear[u]
            except KeyError:
                raise ValueError(("if 'variable_order' is not provided, binary "
                                  "quadratic model must be "
                                  "index labeled [0, ..., N-1]"))

            for vi, v in enumerate(variable_order[ui+1:], start=ui+1):
                M[ui, vi] = self.binary.quadratic.get((u, v), 0.0)

        return M

    def to_numpy_vectors(self, variable_order=None,
                         dtype=np.float, index_dtype=np.intc,
                         sort_indices=False, sort_labels=True,
                         return_labels=False):
        """The BQM as 4 numpy vectors, the offset and a list of variables."""
        num_variables = self.num_variables
        num_interactions = self.num_interactions

        irow = np.empty(num_interactions, dtype=index_dtype)
        icol = np.empty(num_interactions, dtype=index_dtype)
        qdata = np.empty(num_interactions, dtype=dtype)

        if variable_order is None:
            variable_order = list(self.iter_variables())

            if sort_labels:
                try:
                    variable_order.sort()
                except TypeError:
                    # can't sort unlike types in py3
                    pass

        if len(variable_order) != num_variables:
            raise ValueError("variable_order does not match the number of "
                             "variables")

        try:
            ldata = np.fromiter((self.linear[v] for v in variable_order),
                                count=num_variables, dtype=dtype)
        except KeyError:
            msg = "provided 'variable_order' does not match binary quadratic model"
            raise ValueError(msg)

        label_to_idx = {v: idx for idx, v in enumerate(variable_order)}

        # we could speed this up a lot with cython
        for idx, ((u, v), bias) in enumerate(self.quadratic.items()):
            irow[idx] = label_to_idx[u]
            icol[idx] = label_to_idx[v]
            qdata[idx] = bias

        if sort_indices:
            # row index should be less than col index, this handles
            # upper-triangular vs lower-triangular
            swaps = irow > icol
            if swaps.any():
                # in-place
                irow[swaps], icol[swaps] = icol[swaps], irow[swaps]

            # sort lexigraphically
            order = np.lexsort((irow, icol))
            if not (order == range(len(order))).all():
                # copy
                irow = irow[order]
                icol = icol[order]
                qdata = qdata[order]

        ret = [ldata, (irow, icol, qdata), ldata.dtype.type(self.offset)]

        if return_labels:
            ret.append(variable_order)

        return tuple(ret)

    def to_qubo(self):
        """Convert a binary quadratic model to QUBO format.

        If the binary quadratic model's vartype is not :class:`.Vartype.BINARY`,
        values are converted.

        Returns:
            tuple: 2-tuple of form (`biases`, `offset`), where `biases` is a
            dict in which keys are pairs of variables and values are the
            associated linear or quadratic bias and `offset` is a number that
            represents the constant offset of the binary quadratic model.

        """
        qubo = dict(self.binary.quadratic)
        qubo.update(((v, v), bias) for v, bias in self.binary.linear.items())
        return qubo, self.binary.offset

class ShapeableBQM(BQM):
    @abc.abstractmethod
    def add_variable(self, v=None, bias=0):
        """Add a variable to the binary quadratic model.

        Args:
            v (hashable, optional):
                A label for the variable. Defaults to the length of the binary
                quadratic model, if that label is available. Otherwise defaults
                to the lowest available positive integer label.

            bias (numeric, optional, default=0):
                The initial bias value for the added variable. If `v` is already
                a variable, then `bias` (if any) is adding to its existing
                linear bias.

        Returns:
            hashable: The label of the added variable.

        Raises:
            TypeError: If the label is not hashable.

        """
        pass

    @abc.abstractmethod
    def remove_interaction(self, u, v):
        pass

    @abc.abstractmethod
    def remove_variable(self):
        pass

    # mixins

    @property
    def adj(self):
        return ShapeableAdjacency(self)

    @property
    def linear(self):
        return ShapeableLinear(self)

    @property
    def quadratic(self):
        return ShapeableQuadratic(self)

    def add_variables_from(self, linear):
        """Add variables and/or linear biases to a binary quadratic model.

        Args:
            linear (dict/iterable):
                A collection of variables in their associated linear biases.
                If a dict, should be of the form `{v: bias, ...}` where `v` is
                a variable and `bias` is its associated linear bias. Otherwise
                should be an iterable of `(v, bias)` pairs.

        """
        if isinstance(linear, Mapping):
            for v, bias in linear.items():
                self.add_variable(v, bias)
        else:
            try:
                for v, bias in linear:
                    self.add_variable(v, bias)
            except TypeError:
                raise TypeError("expected 'linear' to be a dict or an iterable"
                                " of 2-tuples.")

    def add_interaction(self, u, v, bias):
        """Add an interaction and/or quadratic bias to a binary quadratic model.

        Args:
            u (variable):
                One of the pair of variables to add to the model. Can be any
                python object that is a valid dict key.

            v (variable):
                One of the pair of variables to add to the model. Can be any
                python object that is a valid dict key.

            bias (bias):
                Quadratic bias associated with u, v. If u, v is already in the
                model, this value is added to the current quadratic bias.

        """
        self.set_quadratic(u, v, bias + self.get_quadratic(u, v, default=0))

    def add_interactions_from(self, quadratic):
        """Add interactions and/or quadratic biases to a binary quadratic model.

        Args:
            quadratic (dict/iterable):
                A collection of interactions and their associated quadratic
                bias. If a dict, should be of the form `{(u, v): bias, ...}`
                where `u` and `v` are variables in the model and `bias` is
                there associated quadratic bias. Otherwise, whould be an
                iterable of `(u, v, bias)` triplets.

        """
        if isinstance(quadratic, Mapping):
            for (u, v), bias in quadratic.items():
                self.add_interaction(u, v, bias)
        else:
            try:
                for u, v, bias in quadratic:
                    self.add_interaction(u, v, bias)
            except TypeError:
                raise TypeError("expected 'quadratic' to be a dict or an "
                                "iterable of 3-tuples.")

    def contract_variables(self, u, v):
        """Enforce u, v being the same variable in a binary quadratic model.

        The resulting variable is labeled 'u'. Values of interactions between
        `v` and variables that `u` interacts with are added to the
        corresponding interactions of `u`.

        Args:
            u (variable):
                Variable in the binary quadratic model.

            v (variable):
                Variable in the binary quadratic model.

        """
        if not self.has_variable(u):
            msg = "{} is not a variable in the binary quadratic model"
            raise ValueError(msg.format(u))
        if not self.has_variable(v):
            msg = "{} is not a variable in the binary quadratic model"
            raise ValueError(msg.format(v))

        if self.vartype is Vartype.BINARY:
            # the quadratic bias becomes linear
            self.set_linear(u, (self.get_linear(u) + self.get_linear(v)
                                + self.get_quadratic(u, v, default=0)))
        elif self.vartype is Vartype.SPIN:
            # the quadratic bias becomes an offset
            self.set_linear(u, self.get_linear(u) + self.get_linear(v))
            self.offset += self.get_quadratic(u, v)

        self.remove_interaction(u, v)

        # add all of v's interactions to u's
        for _, w, b in self.iter_quadratic(v):
            self.set_quadratic(u, w, self.get_quadratic(u, w, default=0) + b)

        # finally remove v
        self.remove_variable(v)

    def fix_variable(self, v, value):
        """Remove a variable by fixing its value.

        Args:
            v (variable):
                Variable in the binary quadratic model to be fixed.

            value (int):
                Value assigned to the variable. Values must match the
                :class:`.Vartype` of the binary quadratic model.

        """

        if value not in self.vartype.value:
            raise ValueError("expected value to be in {}, received {} "
                             "instead".format(self.vartype.value, value))

        try:
            for u, bias in self.adj[v].items():
                self.linear[u] += bias*value
        except KeyError:
            raise ValueError('{} is not a variable'.format(v))

        self.offset += value*self.linear[v]
        self.remove_variable(v)

    def fix_variables(self, fixed):
        """Fix the value of the variables and remove them.

        Args:
            fixed (dict/iterable):
                A dictionary or an iterable of 2-tuples of variable assignments.

        """
        if isinstance(fixed, Mapping):
            fixed = fixed.items()
        for v, val in fixed:
            self.fix_variable(v, val)

    def remove_variables_from(self, variables):
        """Remove the given variables from the binary quadratic model."""
        for v in variables:
            self.remove_variable(v)

    def remove_interactions_from(self, interactions):
        """Remove the given interactions from the binary quadratic model."""
        for u, v in interactions:
            self.remove_interaction(u, v)

    def update(self, other):
        """Update the binary quadratic model, adding biases from another."""

        if self.vartype is Vartype.SPIN:
            bqm = other.spin
        elif self.vartype is Vartype.BINARY:
            bqm = other.binary
        else:
            raise ValueError("unknown vartype")

        self.add_variables_from(bqm.linear)
        self.add_interactions_from(bqm.quadratic)
        self.add_offset(bqm.offset)

class VartypeView(BQM):

    def __init__(self, bqm):
        self._bqm = bqm

    @property
    def base(self):
        return self._bqm

    @property
    def num_interactions(self):
        return self._bqm.num_interactions

    @property
    def num_variables(self):
        return self._bqm.num_variables

    def change_vartype(self, *args, **kwargs):
        msg = '{} can only be {}-valued'.format(type(self).__name__,
                                                self.vartype.name)
        raise NotImplementedError(msg)

    def degree(self, *args, **kwargs):
        return self._bqm.degree(*args, **kwargs)

    def iter_linear(self):
        for v, _ in self._bqm.iter_linear():
            yield v, self.get_linear(v)

    def iter_quadratic(self, variables=None):
        for u, v, _ in self._bqm.iter_quadratic(variables):
            yield u, v, self.get_quadratic(u, v)

class BinaryView(VartypeView):
    @property
    def binary(self):
        return self

    @property
    def offset(self):
        bqm = self._bqm
        return (bqm.offset
                - sum(b for _, b in bqm.iter_linear())
                + sum(b for _, _, b in bqm.iter_quadratic()))

    @offset.setter
    def offset(self, bias):
        # just add the difference
        self._bqm.offset += bias - self.offset

    @property
    def spin(self):
        return self._bqm.spin

    @property
    def vartype(self):
        return Vartype.BINARY

    def copy(self):
        return self._bqm.change_vartype(Vartype.BINARY, inplace=False)

    def relabel_variables(self, *args, **kwargs):
        return self._bqm.relabel_variables(*args, **kwargs).binary

    def get_linear(self, v):
        bqm = self._bqm
        return 2 * bqm.get_linear(v) - 2 * sum(b for _, _, b in bqm.iter_quadratic(v))

    def get_quadratic(self, u, v, default=None):
        try:
            return 4 * self._bqm.get_quadratic(u, v)
        except ValueError as err:
            if default is None:
                raise err
        return default

    def set_linear(self, v, bias):
        bqm = self._bqm

        # need the difference
        delta = bias - self.get_linear(v)

        bqm.set_linear(v, bqm.get_linear(v) + delta / 2)
        bqm.offset += delta / 2

    def set_quadratic(self, u, v, bias):
        bqm = self._bqm

        # need the difference
        delta = bias - self.get_quadratic(u, v, default=0)

        # if it doesn't exist and BQM is not shapeable, this will fail
        bqm.set_quadratic(u, v, bias / 4)  # this one is easy

        # the other values get the delta
        bqm.set_linear(u, bqm.get_linear(u) + delta / 4)
        bqm.set_linear(v, bqm.get_linear(v) + delta / 4)

        bqm.offset += delta / 4

class SpinView(VartypeView):
    @property
    def binary(self):
        return self._bqm.binary

    @property
    def offset(self):
        bqm = self._bqm
        return (bqm.offset
                + sum(b for _, b in bqm.iter_linear()) / 2
                + sum(b for _, _, b in bqm.iter_quadratic()) / 4)

    @offset.setter
    def offset(self, bias):
        # just add the difference
        self._bqm.offset += bias - self.offset

    @property
    def spin(self):
        return self

    @property
    def vartype(self):
        return Vartype.SPIN

    def copy(self):
        return self._bqm.change_vartype(Vartype.SPIN, inplace=False)

    def relabel_variables(self, *args, **kwargs):
        return self._bqm.relabel_variables(*args, **kwargs).spin

    def get_linear(self, v):
        bqm = self._bqm
        return (bqm.get_linear(v) / 2
                + sum(b for _, _, b in bqm.iter_quadratic(v)) / 4)

    def get_quadratic(self, u, v, default=None):
        try:
            return self._bqm.get_quadratic(u, v) / 4
        except ValueError as err:
            if default is None:
                raise err
        return default

    def set_linear(self, v, bias):
        bqm = self._bqm

        # need the difference
        delta = bias - self.get_linear(v)

        bqm.set_linear(v, bqm.get_linear(v) + 2 * delta)
        bqm.offset -= delta

    def set_quadratic(self, u, v, bias):
        bqm = self._bqm

        # need the difference
        delta = bias - self.get_quadratic(u, v, default=0)

        # if it doesn't exist and BQM is not shapeable, this will fail
        bqm.set_quadratic(u, v, 4 * bias)  # this one is easy

        # the other values get the delta
        bqm.set_linear(u, bqm.get_linear(u) - 2 * delta)
        bqm.set_linear(v, bqm.get_linear(v) - 2 * delta)

        bqm.offset += delta

# register the various objects with prettyprint
def _pprint_bqm(printer, bqm, stream, indent, *args, **kwargs):
    clsname = type(bqm).__name__
    stream.write(clsname)
    indent += len(clsname)
    bqmtup = (bqm.linear, bqm.quadratic, bqm.offset, bqm.vartype.name)
    printer._pprint_tuple(bqmtup, stream, indent, *args, **kwargs)

try:
    PrettyPrinter._dispatch[BQMView.__repr__] = PrettyPrinter._pprint_dict
    PrettyPrinter._dispatch[BQM.__repr__] = _pprint_bqm
except AttributeError:
    # we're using some internal stuff in PrettyPrinter so let's silently fail
    # for that
    pass

max_stars_repo_name: amahmudwave/dimod

max_issues_repo_name: amahmudwave/dimod

max_forks_repo_name: amahmudwave/dimod


text:
import os
import numpy as np
import cv2
from tqdm import tqdm as tq
from os.path import splitext

class Ram_Patch_Utils:
    def __init__(self) -> None:
        pass

    def ram_patch_img(self,img_path:str="",total_number:int=16,output_dir:str="output_patched_image")-> None:
        """[Create multiple images in patch from a single images,
         patch is created from left to right with file name starting from 0 eg: 0.png, 1.png .....
         Works best with square sized image]

        Args:
            img_path (str): [Image path ]
            total_number (int): [Number of patch images to be created]
            output_dir (str): [Output directory to save patch images]
        """

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        img =cv2.imread(img_path,)
        h,w,c =img.shape
        crop_height = h/total_number
        crop_width = w/total_number

        count = 0
        for i in range(total_number):
            for j in range(total_number):
                img1 = img[int(i*crop_width):int((i+1)*crop_width),int(j*crop_height):int((j+1)*crop_height)]
                cv2.imwrite(os.path.join(output_dir,f"{count}.png"),img1)
                count+=1

    def ram_patch_merge_img(self,input_dir:str="output_patched_image",output_dir:str="output_patch_merge_img",patch_height:int=1024,patch_width:int=1024):

        # if not os.path.exists(output_dir):
        #     os.makedirs(output_dir)
        # print(input_dir)
        img_list = os.listdir(input_dir)
        # file_extension = img_list[0].split('.')[-1]
        # print(input_dir)
        # print(img_list)
        # print(len(img_list))
        img1 = cv2.imread(os.path.join(input_dir,img_list[0]))
        h,w,c =img1.shape
        vert_merge_no =int(patch_height/h)
        hori_merge_no =int(patch_width/w)

        count=0
        for i in range(vert_merge_no):
            for j in range(hori_merge_no):
                img = cv2.imread(os.path.join(input_dir,f"{count}.png"))
                hor_concat = img if j == 0 else cv2.hconcat([hor_concat,img])
                count+=1
            vert_concat = hor_concat if i == 0 else cv2.vconcat([vert_concat,hor_concat])

        # vert_concat = cv2.cvtColor(vert_concat,cv2.COLOR_BGR2RGB)
        # cv2.imshow("preview",vert_concat)
        # cv2.imwrite(f"{output_dir}/example.{file_extension}",vert_concat)
        cv2.imwrite(output_dir,vert_concat)
        # cv2.waitKey(2000)

def create_dir(dir_name):
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

#use case
patch_img = Ram_Patch_Utils()
# patch_img.ram_patch_img("/home/nabu/workspace/ram_utils/ram_utils/drop-original.png",16)
# input_img_dir = "/home/nabu/workspace/pytorch_env/deepNN_py/carpet_script/bw_convert_train/path_calcualtion/patch_inference_img/color/000"
# input_img_dir = "/home/nabu/workspace/pytorch_env/deepNN_py/carpet_script/bw_convert_train/path_calcualtion/patch_inference_img/"
# output_dir = "/home/nabu/workspace/pytorch_env/deepNN_py/carpet_script/bw_convert_train/path_calcualtion/inference_img_patched"

input_img_dir = "/home/nabu/workspace/pytorch_env/deepNN_py/carpet_script/bw_convert_train/path_calcualtion/patch_img_differences"
output_dir="/home/nabu/workspace/pytorch_env/deepNN_py/carpet_script/bw_convert_train/path_calcualtion/difference_img_patched"

input_img_dir = "/home/pasonatech/workplace_new/ram_utils/ram_utils/autoencoder_stuff/merge_infer_dropout_section"
output_dir = "/home/pasonatech/workplace_new/ram_utils/ram_utils/autoencoder_stuff/patch_infer_diff_back_256"

list_datatype_directory = sorted(os.listdir(input_img_dir))
for dir_type in list_datatype_directory:
    # print(dir_type) #datatype folder name
    input_dir_type = os.path.join(input_img_dir,dir_type)

    #create output folder
    datawise_output_dir = os.path.join(output_dir,dir_type)
    # print(datawise_output_dir)
    create_dir(datawise_output_dir)

    #list patch images folder
    list_dir_type_number = sorted(os.listdir(input_dir_type))

    for each_patch_dir in list_dir_type_number:
        #input patch folder
        each_dir = os.path.join(input_dir_type,each_patch_dir)
        #output file name
        output_filename = f"{each_patch_dir}.png"
        output_save_dir = os.path.join(datawise_output_dir,output_filename)

        # print(output_save_dir)
        # exit()
        # print(each_dir)
        patch_img.ram_patch_merge_img(input_dir=each_dir,output_dir=output_save_dir,patch_height=256,patch_width=256)
    # print(output_save_dir)


text:
from sklearn.model_selection import train_test_split
import numpy as np

class Model:
    def __init__(self, df):
        self.df = df

    def X_features_drop(self, list_columns):
        return self.df.drop(columns = list_columns,axis=1)

    def y_target(self, column):
        return self.df[column]

    def split(self, X, y, test_size, random_state):
        return train_test_split(X,y, test_size=test_size, random_state=random_state)

    def prediction(self, model, pclass, sex, age, sibsp, parch, fare, embarked):
        input_data_as_numpy_array = np.asarray((pclass, sex, age, sibsp, parch, fare, embarked))
        input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
        prediction = model.predict(input_data_reshaped)
        if prediction[0]==0:
            return "Dead"
        if prediction[0]==1:
            return "Alive"

# input_data = (3,0,35,0,0,8.05,0)  # Note that these datas exclude the Survived data, as it is to be determined from the model itself

# input_data_as_numpy_array = np.asarray(input_data)

# input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)

# prediction = model.predict(input_data_reshaped)
# #print(prediction)
# if prediction[0]==0:
#     print("Dead")
# if prediction[0]==1:
#     print("Alive")

max_stars_repo_name: Adriengith/titanicazertyu

max_issues_repo_name: Adriengith/titanicazertyu

max_forks_repo_name: Adriengith/titanicazertyu


text:
# -*- coding: utf-8 -*-

import pywph as pw
import matplotlib.pyplot as plt
import numpy as np

M, N = 512, 512

j = 6
theta = np.pi / 4
k0 = 0.85 * np.pi

w = pw.BumpSteerableWavelet(M, N, j, theta, k0=k0)
g = pw.GaussianFilter(M, N, j)

# Bump-steerable filter plot (the wavelet is centered)
fig, axs = plt.subplots(1, 2, figsize=(6, 3), sharex=True, sharey=True)
axs[0].imshow(np.fft.fftshift(w.data.real), origin='lower')
axs[1].imshow(np.fft.fftshift(w.data.imag), origin='lower')
fig.subplots_adjust(top=0.95, bottom=0.091, left=0.056, right=0.97, hspace=0.19, wspace=0.046)
fig.show()

# Gaussian filter plot (the filter is centered)
fig, axs = plt.subplots(1, 1, figsize=(3.5, 3))
axs.imshow(np.fft.fftshift(g.data), origin='lower')
fig.subplots_adjust(top=0.949, bottom=0.129, left=0.089, right=0.957, hspace=0.2, wspace=0.2)
fig.show()


text:
# coding: utf-8

import os
import numpy as np
from numpy import random
import cv2
import keras
import json
from keras import backend as K
import tensorflow as tf
import globals

from keras.layers import Input, concatenate, Reshape, BatchNormalization, Activation, Lambda
from keras.layers.convolutional import Conv2D, MaxPooling2D, Conv2DTranspose, ZeroPadding2D, Cropping2D
from keras.models import Model
from keras.utils.np_utils import to_categorical
from keras.optimizers import Adam

# Disabling both USE_W1 and USE_W2 should result in typical categorical_cross_entropy loss
USE_W1 = True
USE_W2 = True

def custom_weighted_loss(input_shape, use_regression, weight_bb, obj_to_bkg_ratio=0.00016, avg_obj_size=1000, loss_scaler=1000):

    def custom_loss(y_true, y_pred):
        # the code here is only executed once, since these should all be graph operations. Do not expect Numpy
        # calculations or the like to work here, only Keras backend and tensor flow nodes.

        max_pixels = input_shape[0] * input_shape[1]
        #y_pred = tf.Print(y_pred, ["y_pred", tf.shape(y_pred)])

        if use_regression:
            y_true_obj, y_true_bb = tf.split(y_true, [globals.NUM_CLASSES, globals.NUM_REGRESSION_OUTPUTS], 2)
            y_pred_obj, y_pred_bb = tf.split(y_pred, [globals.NUM_CLASSES, globals.NUM_REGRESSION_OUTPUTS], 2)
        else:
            y_true_obj = y_true
            y_pred_obj = y_pred

        log_softmax = tf.log(y_pred_obj, name="logsoftmax")
        neglog_softmax = tf.scalar_mul(-1., log_softmax)

        pixel_loss = tf.multiply(y_true_obj, neglog_softmax, name="pixel_loss")

        labels_bkg, labels_frg = tf.split(y_true_obj, 2, 2, name="split_2")
        bkg_frg_areas = tf.reduce_sum(y_true_obj, 1)
        bkg_area, frg_area = tf.split(bkg_frg_areas, 2, 1, name="split_1")

        # The branches here configure the graph differently. You can imagine these branches working as if the path
        # that was disabled didn't exist at all in the code. Each path should work independently.

        if USE_W1:
            w1_bkg_weights = tf.scalar_mul(obj_to_bkg_ratio, labels_bkg)
        else:
            w1_bkg_weights = labels_bkg

        frg_area_tiled = tf.tile(frg_area, tf.stack([1, max_pixels]))

        # prevent divide by zero, max is number of pixels
        frg_area_tiled = K.clip(frg_area_tiled, K.epsilon(), max_pixels)
        inv_frg_area = tf.div(tf.ones_like(frg_area_tiled), frg_area_tiled)

        w2_weights = tf.scalar_mul(avg_obj_size, inv_frg_area)
        w2_frg_weights = tf.multiply(labels_frg, tf.expand_dims(w2_weights, axis=2))

        w1_times_w2 = tf.add(w1_bkg_weights, w2_frg_weights, name="w1_times_w2")
        weighted_loss = tf.multiply(w1_times_w2, pixel_loss, name="weighted_loss")
        weighted_loss = tf.scalar_mul(loss_scaler, weighted_loss)

        loss = loss_obj = tf.reduce_sum(weighted_loss, -1, name="loss")

        # weighted loss for regression branch
        if use_regression:
            diff_bb = tf.subtract(y_true_bb, y_pred_bb)
            l2_norm = tf.norm(diff_bb)

            weighted_loss_bb = tf.multiply(w2_frg_weights, l2_norm, name="weighted_l2_loss")
            loss_bb = tf.reduce_sum(weighted_loss_bb, -1, name="loss_bb")
            loss_bb = tf.scalar_mul(weight_bb, loss_bb)
            #loss_obj = tf.Print(loss_obj, ["loss_obj max", tf.reduce_max(loss_obj), " mean:", tf.reduce_mean(loss_obj)])
            #loss_bb = tf.Print(loss_bb, ["loss_bb max:", tf.reduce_max(loss_bb), " mean:", tf.reduce_mean(loss_bb)])

            loss = tf.add(loss_obj, loss_bb, name="loss")

        #loss = tf.Print(loss, ["loss", tf.shape(loss), loss])
        return loss

    return custom_loss

def build_model(input_shape, num_classes, data_source,
                use_regression=False,
                obj_to_bkg_ratio=0.00016,
                avg_obj_size=1000,
                weight_bb=0.01,
                metrics=None,
                trainable=True):

    # set channels last format
    K.set_image_data_format('channels_last')

    # vertical strides
    vs = 2
    if data_source == "lidar":
        vs = globals.LIDAR_CONV_VERTICAL_STRIDE

    post_normalized = inputs = Input(shape=input_shape, name='input')
    if globals.USE_SAMPLE_WISE_BATCH_NORMALIZATION:
        flatten_input = Reshape((-1, input_shape[2]), name='flatten_input')(inputs)
        normalized = BatchNormalization(name='normalize', axis=1)(flatten_input)
        post_normalized = Reshape((input_shape[0], input_shape[1], input_shape[2]), name='unflatten_input')(normalized)
    if globals.USE_FEATURE_WISE_BATCH_NORMALIZATION:
        post_normalized = BatchNormalization(name='normalize', axis=-1)(post_normalized)
    inputs_padded = ZeroPadding2D(padding=((0, 0), (0, 3)))(post_normalized)
    conv1 = Conv2D(4, 5, strides=(vs, 4), activation='relu', name='conv1', padding='same',
                   kernel_initializer='random_uniform', bias_initializer='zeros')(inputs_padded)
    conv2 = Conv2D(6, 5, strides=(vs, 2), activation='relu', name='conv2', padding='same',
                   kernel_initializer='random_uniform', bias_initializer='zeros')(conv1)
    conv3 = Conv2D(12, 5, strides=(vs, 2), activation='relu', name='conv3', padding='same',
                   kernel_initializer='random_uniform', bias_initializer='zeros')(conv2)
    deconv4 = Conv2DTranspose(16, 5, strides=(vs, 2), activation='relu', name='deconv4', padding='same',
                              kernel_initializer='random_uniform', bias_initializer='zeros')(conv3)

    concat_deconv4 = concatenate([conv2, deconv4], name='concat_deconv4')

    # classification task
    deconv5a = Conv2DTranspose(8, 5, strides=(vs, 2), activation='relu', name='deconv5a', padding='same',
                               kernel_initializer='random_uniform', bias_initializer='zeros')(concat_deconv4)

    deconv5a_padded = Cropping2D(cropping=((0, 0), (1, 0)))(deconv5a)

    concat_deconv5a = concatenate([conv1, deconv5a_padded], name='concat_deconv5a')
    deconv6a = Conv2DTranspose(2, 5, strides=(vs, 4), name='deconv6a', padding='same',
                               kernel_initializer='random_uniform', bias_initializer='zeros')(concat_deconv5a)

    if data_source == "lidar":
        deconv6a_crop = Cropping2D(cropping=((0, 0), (0, 3)))(deconv6a)
    elif data_source == "camera":
        deconv6a_crop = Cropping2D(cropping=((0, 0), (0, 4)))(deconv6a)
    else:
        print "invalid data source"
        exit(1)

    deconv6a_flatten = Reshape((-1, num_classes), name='deconv6a_flatten')(deconv6a_crop)
    softmax = Activation('softmax',name='softmax')(deconv6a_flatten)
    output = classification_output = Lambda(lambda x: K.clip(x, K.epsilon(), 1), name='classification_output')(softmax)

    # regression task
    if use_regression:
        deconv5b = Conv2DTranspose(globals.NUM_REGRESSION_OUTPUTS, 5, strides=(vs,2), activation='relu',
                                   name='deconv5b', padding='same',
                                   kernel_initializer='random_uniform', bias_initializer='zeros')(concat_deconv4)

        if data_source == "camera":
            deconv5b_padded = ZeroPadding2D(padding=((1, 0), (0, 0)))(deconv5b)
        elif data_source == "lidar":
            deconv5b_padded = Cropping2D(cropping=((0, 0), (1, 0)))(deconv5b)
        else:
            print "invalid data source"
            exit(1)

        concat_deconv5b = concatenate([conv1, deconv5b_padded], name='concat_deconv5b')

        deconv6b = Conv2DTranspose(globals.NUM_REGRESSION_OUTPUTS, 5, strides=(vs,4), activation='relu',
                                   name='deconv6b', padding='same',
                                   kernel_initializer='random_uniform', bias_initializer='zeros')(concat_deconv5b)

        if data_source == "camera":
            deconv6b_crop = Cropping2D(cropping=((3, 0), (0, 4)))(deconv6b)
        elif data_source == "lidar":
            deconv6b_crop = Cropping2D(cropping=((0, 0), (0, 3)))(deconv6b)
        else:
            print "invalid data source"
            exit(1)

        regression_output = Reshape((-1, globals.NUM_REGRESSION_OUTPUTS), name='regression_output')(deconv6b_crop)

        # concatenate two outputs into one so that we can have one loss function
        output = concatenate([classification_output, regression_output], name='outputs')

    model = Model(inputs=inputs, outputs=output)
    model.compile(optimizer=Adam(lr=globals.LEARNING_RATE),
                  loss=custom_weighted_loss(input_shape, use_regression, weight_bb, obj_to_bkg_ratio, avg_obj_size),
                  metrics=metrics)

    print(model.summary())

    return model

def load_model(model_file, weights_file, input_shape, num_classes, use_regression,
               obj_to_bkg_ratio=0.00016,
               avg_obj_size=1000,
               weight_bb=0.01,
               metrics=None):
    with open(model_file, 'r') as jfile:
        print('Loading weights file {}'.format(weights_file))
        print("reading existing model and weights")
        model = keras.models.model_from_json(json.loads(jfile.read()))
        model.load_weights(weights_file)
        model.compile(optimizer=Adam(lr=globals.LEARNING_RATE),
                      loss=custom_weighted_loss(input_shape, use_regression, weight_bb, obj_to_bkg_ratio, avg_obj_size),
                      metrics=metrics)

    return model

def test(model):
    # please change path if needed
    path = '../../dataset1/10/lidar_360/1490991699437114271_'
    if os.path.exists(path + 'height.png'):
        print('image found')
    height_img = cv2.imread(path + 'height.png')
    height_gray = cv2.cvtColor(height_img, cv2.COLOR_RGB2GRAY)
    distance_img = cv2.imread(path + 'distance.png')
    distance_gray = cv2.cvtColor(distance_img, cv2.COLOR_RGB2GRAY)
    intensity_img = cv2.imread(path + 'intensity.png')
    intensity_gray = cv2.cvtColor(intensity_img, cv2.COLOR_RGB2GRAY)
    x = np.zeros(globals.INPUT_SHAPE)
    x[:, :, 0] = height_gray
    x[:, :, 1] = distance_gray
    x[:, :, 2] = intensity_gray

    label = np.zeros(globals.INPUT_SHAPE[:2])
    label[8:, 1242:1581] = 1  #bounding box of the obstacle vehicle
    y = to_categorical(label, num_classes=2) #1st dimension: off-vehicle, 2nd dimension: on-vehicle
    #print(np.nonzero(y[:,1])[0].shape[0])

    #place holder
    regression_label = np.zeros((1, globals.IMG_WIDTH*globals.IMG_HEIGHT, 24))
    outputs = np.concatenate((np.asarray([y]), regression_label), axis=2)
    print(outputs.shape)
    model.fit(np.asarray([x]), outputs, batch_size=globals.BATCH_SIZE, epochs=globals.EPOCHS, verbose=1)

def main():
    model = build_model(globals.INPUT_SHAPE, 2, use_regression=True)
    test(model)

if __name__ == '__main__':
    main()

reason: import numpy,from numpy


text:
import cv2
import numpy as np

img = cv2.imread('base.png', 1)
rate = float(img.shape[0])/float(img.shape[1])
if img.shape[1] != 100 :
	height = int(100*rate)
	img = cv2.resize(img, (100, height), interpolation=cv2.INTER_CUBIC)

def custom_blur_demo(image):
    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
    dst = cv2.filter2D(image, -1, kernel=kernel)
    return dst

#img = custom_blur_demo(img)
cv2.imwrite('1.png', img)