text:
import tkinter as tk
from PIL import ImageTk, Image, ImageDraw
import ntpath
import glob2 as glob
from collections import OrderedDict
import datetime
import numpy as np
from scipy.spatial import distance

def about(header=False):
    """
    Provides a header and front-end interface for new users and pipeline workflows.

    Parameters
    ----------
    header : boolean, default: False
        Determines whether to display a header or a front-end interface. By default, this is set
        to ``False``, meaning that it automatically generates a front-end interface if passed.

    Notes
    -----
    This is the most frontend aspect about **mednoise**. Beyond this, **mednoise** is
    a series of scripts to be included in a terminal or pipeline workflow.

    Examples
    --------
    >>> md.about()
        #############################################################################################
                                             8I
                                             8I
                                             8I                              gg
                                             8I                              ""
          ,ggg,,ggg,,ggg,    ,ggg,     ,gggg,8I   ,ggg,,ggg,     ,ggggg,     gg     ,g,      ,ggg,
         ,8" "8P" "8P" "8,  i8" "8i   dP"  "Y8I  ,8" "8P" "8,   dP"  "Y8ggg  88    ,8'8,    i8" "8i
         I8   8I   8I   8I  I8, ,8I  i8'    ,8I  I8   8I   8I  i8'    ,8I    88   ,8'  Yb   I8, ,8I
        ,dP   8I   8I   Yb, `YbadP' ,d8,   ,d8b,,dP   8I   Yb,,d8,   ,d8'  _,88,_,8'_   8)  `YbadP'
        8P'   8I   8I   `Y8888P"Y888P"Y8888P"`Y88P'   8I   `Y8P"Y8888P"    8P""Y8P' "YY8P8P888P"Y888
        #############################################################################################

    >>> md.about(header=True)
        #############################################################################################
                                             8I
                                             8I
                                             8I                              gg
                                             8I                              ""
          ,ggg,,ggg,,ggg,    ,ggg,     ,gggg,8I   ,ggg,,ggg,     ,ggggg,     gg     ,g,      ,ggg,
         ,8" "8P" "8P" "8,  i8" "8i   dP"  "Y8I  ,8" "8P" "8,   dP"  "Y8ggg  88    ,8'8,    i8" "8i
         I8   8I   8I   8I  I8, ,8I  i8'    ,8I  I8   8I   8I  i8'    ,8I    88   ,8'  Yb   I8, ,8I
        ,dP   8I   8I   Yb, `YbadP' ,d8,   ,d8b,,dP   8I   Yb,,d8,   ,d8'  _,88,_,8'_   8)  `YbadP'
        8P'   8I   8I   `Y8888P"Y888P"Y8888P"`Y88P'   8I   `Y8P"Y8888P"    8P""Y8P' "YY8P8P888P"Y888
        #############################################################################################
        Copyright 2021 Ravi Bandaru
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this package except in compliance with the License.
        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.
        #############################################################################################
        Welcome to mednoise, a python package that contains algorithms to handle and pre-process
        large amounts of image-based metadata to remove noise and enhance the accuracy of machine
        learning and deep learning models for scientific research.
        #############################################################################################
        You can bring up the help menu (h) or exit (e).
    """
    if header==True:
        logo = """
        #############################################################################################
                                             8I
                                             8I
                                             8I                              gg
                                             8I                              ""
          ,ggg,,ggg,,ggg,    ,ggg,     ,gggg,8I   ,ggg,,ggg,     ,ggggg,     gg     ,g,      ,ggg,
         ,8" "8P" "8P" "8,  i8" "8i   dP"  "Y8I  ,8" "8P" "8,   dP"  "Y8ggg  88    ,8'8,    i8" "8i
         I8   8I   8I   8I  I8, ,8I  i8'    ,8I  I8   8I   8I  i8'    ,8I    88   ,8'  Yb   I8, ,8I
        ,dP   8I   8I   Yb, `YbadP' ,d8,   ,d8b,,dP   8I   Yb,,d8,   ,d8'  _,88,_,8'_   8)  `YbadP'
        8P'   8I   8I   `Y8888P"Y888P"Y8888P"`Y88P'   8I   `Y8P"Y8888P"    8P""Y8P' "YY8P8P888P"Y888
        #############################################################################################
        """
        print(logo)
        global storeddictionary
        global analyzedval
        storeddictionary = 1
        analyzedval = 1

    if header==False:
        logo = """
        #############################################################################################
                                             8I
                                             8I
                                             8I                              gg
                                             8I                              ""
          ,ggg,,ggg,,ggg,    ,ggg,     ,gggg,8I   ,ggg,,ggg,     ,ggggg,     gg     ,g,      ,ggg,
         ,8" "8P" "8P" "8,  i8" "8i   dP"  "Y8I  ,8" "8P" "8,   dP"  "Y8ggg  88    ,8'8,    i8" "8i
         I8   8I   8I   8I  I8, ,8I  i8'    ,8I  I8   8I   8I  i8'    ,8I    88   ,8'  Yb   I8, ,8I
        ,dP   8I   8I   Yb, `YbadP' ,d8,   ,d8b,,dP   8I   Yb,,d8,   ,d8'  _,88,_,8'_   8)  `YbadP'
        8P'   8I   8I   `Y8888P"Y888P"Y8888P"`Y88P'   8I   `Y8P"Y8888P"    8P""Y8P' "YY8P8P888P"Y888
        #############################################################################################
        Copyright 2021 Ravi Bandaru
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this package except in compliance with the License.
        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.
        #############################################################################################
        Welcome to mednoise, a python package that contains algorithms to handle and pre-process
        large amounts of image-based metadata to remove noise and enhance the accuracy of machine
        learning and deep learning models for scientific research.
        #############################################################################################
        You can bring up the help menu (h) or exit (e).
        """
        print(logo)
        response = input("    ")
        print("    #############################################################################################")
        print("")
        if response == "e":
            print("    exiting.")
        if response == "h":
            print("    documentation can be accessed at https://mednoise.github.io/documentation.")
        print("")
        print("    #############################################################################################")
    if header != True and header != False:
        raise ValueError('header argument was incorrectly specified. note that it is a boolean attribute.')

about(True)

def manual_merge(filepath, find = (0,0,0), replace = (255,255,255)):
    """
    Combines multiple input images of the same size to yield one binary image that allows for
    common feature detection.

    Parameters
    ----------
    filepath : string
        A filepath for images to be selected from. Since **mednoise** uses ``glob``,
        it can take any argument that ``glob`` can parse through.
    find : RGB tuple, default: (0,0,0)
        A value that indicates silenced noise. Usually is considered the
        background color of the input image, often ``(0,0,0)``.
    replace : RGB tuple, default: (255,255,255)
        A value that indicates complete noise. Usually is considered the
        complement of the background color of the input image, often ``(255,255,255)``.

    Notes
    -----
    This allows users to find common features and then pass them through their own package scripts,
    or predeveloped scripts like ``md.manual_find`` and ``md.manual_edit``.

    Examples
    --------
    >>> md.manual_merge("/example/directory/*, (0,0,0), (255, 0, 0)) #for 4 images, yielding the below image
    md.manual_merge - Image 1 Importing:0:00:01
    md.manual_merge - Image 2 Importing:0:00:01md.manual_merge - Image 1 Pixel Cleaning:0:00:00
    md.manual_merge - Image 2 Pixel Cleaning:0:00:00
    md.manual_merge - Image 1 and 2 Pixel Merge:0:00:50
    md.manual_merge - Image 3  Pixel Merge:0:00:59
    md.manual_merge - Image 4  Pixel Merge:0:00:51
    md.manual_merge - Final Merge and Conversion:0:00:50
    md.manual_merge - Image Save:0:00:01

    .. figure:: combined_image.png
       :scale: 50 %
       :align: center

       ``md.manual_merge`` output image
    """
    files = glob.glob(filepath)
    original = []

    startTime = datetime.datetime.now().replace(microsecond=0)
    image = Image.open(files[0])
    rgb1 = image.convert('RGB')
    width, height = image.size
    pixel_values1 = list(rgb1.getdata())
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_merge - Image 1 Importing:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    image2 = Image.open(files[1])
    rgb2 = image2.convert('RGB')
    pixel_values2 = list(rgb2.getdata())
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_merge - Image 2 Importing:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    for index, item in enumerate(pixel_values1):
        if item != find:
            pixel_values1[index] = 2
        else:
            pixel_values1[index] = 1
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_merge - Image 1 Pixel Cleaning:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    for index, item in enumerate(pixel_values2):
        if item != find:
            pixel_values2[index] = 2
        else:
            pixel_values2[index] = 1
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_merge - Image 2 Pixel Cleaning:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    for index, item in enumerate(pixel_values1) and enumerate(pixel_values2):
        print(round((100*index)/(width*height),1), end = "\r")
        if pixel_values1[index] == 1 and pixel_values2[index]== 1:
            original.append(1)
        else:
            original.append(2)
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_merge - Image 1 and 2 Pixel Merge:" + str(durationTime))

    i=1
    for index,item in enumerate(files):
        startTime = datetime.datetime.now().replace(microsecond=0)
        image = Image.open(files[index])
        rgb1 = image.convert('RGB')
        pixel_values1 = list(rgb1.getdata())
        width, height = rgb1.size
        for index, item in enumerate(pixel_values1):
            if item != find:
                pixel_values1[index] = 2
            else:
                pixel_values1[index] = 1
        for index, item in enumerate(pixel_values1) and enumerate(original):
            print(round((100*index)/(width*height),1), end = "\r")
            if pixel_values1[index] == 1 and original[index]== 1:
                original[index] = 1
            else:
                original[index] = 2
        endTime = datetime.datetime.now().replace(microsecond=0)
        durationTime = endTime - startTime
        print ("md.manual_merge - Image", i, " Pixel Merge:" + str(durationTime))
        i+=1

    startTime = datetime.datetime.now().replace(microsecond=0)
    for index, item in enumerate(original):
            print(round((100*index)/(width*height),1), end = "\r")
            if original[index]== 1:
                original[index] = find
            else:
                original[index] = replace
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print("md.manual_merge - Final Merge and Conversion:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    image_out = Image.new("RGB",(width,height))
    image_out.putdata(original)
    image_out.save('combined_image.png')
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print("md.manual_merge - Image Save:" + str(durationTime))

def manual_find(filepath):
    """
    Offers an interface through tkinter to identify pixel coordinates and create tuple-lists that can be passed through a filter.

    Parameters
    ----------
    filepath : string
        A filepath for images to be selected from. Must be a path to a file, not a directory or other ``glob`` parseable structure.

    Notes
    -----
    This allows users to find polygon coordinates and then pass them through their own package scripts,
    or predeveloped scripts like ``md.manual_edit``.

    Examples
    --------
    >>> md.manual_find("/example/directory/file.png") #after four clicks on the tkinter interface
    (51,78),
    (51,275),
    (7,261),
    (8,78),
    """
    window = tk.Tk()
    window.title("Pixel Finder")
    window.geometry("960x720")
    window.configure(background='grey')
    img = ImageTk.PhotoImage(Image.open(filepath))
    panel = tk.Label(window, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    def pressed1(event):
        print("(" + str(event.x) + "," + str(event.y) + ")" + ",")
    window.bind('<Button-1>', pressed1)
    window.mainloop()

def manual_edit(filepath, xy, find = (0,0,0)):
    """
    Offers a manual method through which sections of input images can be silenced.

    Parameters
    ----------
    filepath : string
        A filepath for images to be selected from. Must be a path to a file, not a directory or other ``glob`` parseable structure.

    xy : tuple
        A tuple of restraint tuples for the polygon to be silenced. This can be either generated
        by setting the output of ``md.manual_find`` to a list or developing your own algorithm.

    find : RGB tuple, default: (0,0,0)
        A value that indicates silenced noise. Usually is considered the
        background color of the input image, often ``(0,0,0)``.

    Notes
    -----
    This allows users to silence polygon coordinates after then pass them through their own package scripts,
    or predeveloped scripts like ``md.manual_merge`` or ``md.manual_find``.

    Examples
    --------
    >>> restraints = [(473,91),(214,601),(764,626)]
    >>> md.manual_edit("/example/directory/file.png", xy = restraints) #removing a triangle from input image
    md.manual_edit - Image 1 Save:0:00:01

    .. figure:: edited.png
       :scale: 50 %
       :align: center

       ``md.manual_edit`` output image
    """
    files = glob.glob(filepath)
    restraints = xy
    for index,item in enumerate(files):
        with Image.open(files[index]) as im:
            startTime = datetime.datetime.now().replace(microsecond=0)
            name = ntpath.basename(files[index])
            size = len(name)
            mod_string = name[:size - 4]
            print(mod_string)
            draw = ImageDraw.Draw(im)
            draw.polygon(restraints, fill=find, outline=find)
            im.save(mod_string + "_clean" + ".PNG")
            endTime = datetime.datetime.now().replace(microsecond=0)
            durationTime = endTime - startTime
            print("md.manual_edit - Image " + str(index+1) + " Save:" + str(durationTime))

def manual_primer(filepath, find = (0,0,0)):
    """
    Creates one binary image from an imput image that allows for common feature detection.

    Parameters
    ----------
    filepath : string
         A filepath for images to be selected from. Must be a path to a file, not a directory or other ``glob`` parseable structure.
    find : RGB tuple, default: (0,0,0)
        A value that indicates silenced noise. Usually is considered the
        background color of the input image, often ``(0,0,0)``.

    Notes
    -----
    This function is almost entirely useless without an outside algorithm that a user develops. **mednoise**
    is already optimized to not require primed images, so this function instead serves as a tool for user
    developed algorithms that have not been optimized.

    Examples
    --------
    >>> md.manual_primer("/example/directory/*")
    md.manual_primer - Importing Images:0:00:00
    md.manual_primer - Image 1 Importing:0:00:01
    md.manual_primer - Image 1 Cleaning:0:00:00
    md.manual_primer - Image 1 Conversion:0:00:47
    md.manual_primer - Image 1 Image Save:0:00:01

    .. figure:: primed.png
       :scale: 50 %
       :align: center

       ``md.manual_primer`` output image
    """

    replace = (255,255,255)

    startTime = datetime.datetime.now().replace(microsecond=0)
    files = glob.glob(filepath)
    original = []
    endTime = datetime.datetime.now().replace(microsecond=0)
    durationTime = endTime - startTime
    print ("md.manual_primer - Importing Images:" + str(durationTime))

    startTime = datetime.datetime.now().replace(microsecond=0)
    for indexor,item in enumerate(files):
        name = ntpath.basename(files[indexor])
        size = len(name)
        mod_string = name[:size - 4]
        image = Image.open(files[indexor])
        rgb1 = image.convert('RGB')
        pixel_values1 = list(rgb1.getdata())
        width, height = image.size
        pixel_values1 = list(rgb1.getdata())
        endTime = datetime.datetime.now().replace(microsecond=0)
        durationTime = endTime - startTime
        print ("md.manual_primer - Image" + " " + str(indexor+1) + " Importing:" + str(durationTime))

        startTime = datetime.datetime.now().replace(microsecond=0)
        for index, item in enumerate(pixel_values1):
            if item != find:
                pixel_values1[index] = 2
            else:
                pixel_values1[index] = 1
        endTime = datetime.datetime.now().replace(microsecond=0)
        durationTime = endTime - startTime
        startTime = datetime.datetime.now().replace(microsecond=0)
        print ("md.manual_primer - Image" + " " + str(indexor+1) +" Cleaning:" + str(durationTime))

        startTime = datetime.datetime.now().replace(microsecond=0)
        const = (width*height)/100
        for index, item in enumerate(pixel_values1):
                print(str(round((index)/(const),1)) + "%" , end = "\r")
                if pixel_values1[index] == 1:
                    original.append(find)
                else:
                    original.append(replace)
        endTime = datetime.datetime.now().replace(microsecond=0)
        durationTime = endTime - startTime
        print ("md.manual_primer - Image" + " " + str(indexor+1) +" Conversion:" + str(durationTime))

        startTime = datetime.datetime.now().replace(microsecond=0)
        image_out = Image.new("RGB",(width,height))
        image_out.putdata(original)
        image_out.save(mod_string + "_primed" + ".PNG")
        endTime = datetime.datetime.now().replace(microsecond=0)
        durationTime = endTime - startTime
        print ("md.manual_primer - Image" + " " + str(indexor+1) + " Image Save:" + str(durationTime))

ext: py

max_stars_repo_name: arfon/mednoise

max_issues_repo_name: arfon/mednoise

max_forks_repo_name: arfon/mednoise

reason: import numpy,from scipy


text:
"""
This file is part of PUQ
Copyright (c) 2013 PUQ Authors
See LICENSE file for terms.
"""

import copy
import time, os, re, h5py, sys
import numpy as np
from puq.testprogram import TestProgram
from numpy import ndarray
from puq.hdf import get_output_names
from logging import debug
from puq.util import vprint
from puq.options import options
from puq.jpickle import pickle, unpickle
from socket import gethostname
from puq.parameter import get_psamples
from puq.calibrate import calibrate

_vcache = {}
_dcache = {}

class Sweep(object):
    """
    Creates an object that contains all the information about
    a parameter sweep.

    Args:
      psweep: Parameter Sweep object. See :class:`PSweep`.
      host: Host object.  See :class:`Host`.
      prog: TestProgram object: See :class:`TestProgram`.
      caldata(array: Experimental data for calibration. Optional.
      calerr(float): Measurement error in the experimental data.
      description(string): Optional description of this run.
      sweepid: an optional string that uniquely identifies this sweep.
      This string is passed to the TestProgram by the Host object
      using the jobinfo parameter.
    """

    def __init__(self, psweep, host, prog, caldata=None, calerr=None, description='',sweepid=''):
        self.host = host
        self.host.sweepid=sweepid

        self._reinit = False

        if isinstance(prog, TestProgram):
            self.prog = prog
        else:
            self.prog = TestProgram(prog)

        if description == '':
            description = self.prog.desc
        self.description = description

        # optional calibration data
        self.caldata = caldata
        self.err = calerr

        # trying to get 10Hz resolution, 1 year clock
        secperyear = 365*24*60*60
        self.fname = 'sweep_%s' % int((time.time() % secperyear) * 10)
        self.psweep = psweep
        self.host.prog = copy.copy(self.prog) #else jsonpickle can't unpickle without error
        self.input_script = os.path.abspath(sys.argv[0])

    def _save_hdf5(self):
        debug('')
        h5 = h5py.File(self.fname + '.hdf5')

        # write HDF5 header information, once only
        if not 'version' in h5.attrs:
            h5.attrs['MEMOSA_UQ'] = 'MEMOSA'
            h5.attrs['version'] = 201
            #h5.attrs['id'] = self.id
            h5.attrs['date'] = time.strftime("%b %d %H:%M %Z %Y", time.localtime())
            h5.attrs['hostname'] = gethostname()
            h5.attrs['username'] = "puq user" #FR
            h5.attrs['UQtype'] = self.psweep.__class__.__name__.lower()
            h5.attrs['description'] = self.description

        # overwrite previous
        if 'input' in h5:
            del h5['input']
        if 'private' in h5:
            del h5['private']

        hp = h5.require_group('private')
        hp['sweep'] = pickle(self,max_depth=6)

        # in /input write the input params in json and regular arrays
        h = h5.require_group('input')

        # basic parameter table for non-python reading of the hdf5 file
        h.create_dataset('param_array',data=np.column_stack([p.values for p in self.psweep.params]),
                         compression='gzip',compression_opts=9)
        h['param_array'].attrs['name'] = [str(p.name) for p in self.psweep.params]
        h['param_array'].attrs['description'] = [str(p.description) for p in self.psweep.params]

        # json-pickled parameters
        h = h.require_group('params')
        for p in self.psweep.params:
            h[p.name] = pickle(p)
            if p.attrs!=None and len(p.attrs)>0:
                for attrtuple in p.attrs:
                    h[p.name].attrs[attrtuple[0]]=attrtuple[1]

        # input script
        if hasattr(self, 'input_script'):
            h5['input/scriptname'] = str(self.input_script)
            try:
                h5['input/script'] = open(self.input_script).read()
            except:
                h5['input/script'] = "Source was unavailable."
            h5.close()

    def _save_and_run(self,dryrun=False):
        self._save_hdf5()
        res = self.host.run(dryrun)
        if res:
            self._save_hdf5()
        return res

    def run(self, fn=None, overwrite=False, dryrun=False):
        """
        Calls PSweep.run() to run all the jobs in the Sweep.  Collect the data
        from the outputs and call the PSweep analyze method.  If the PSweep method
        has an iterative callback defined, call it, otherwise return.

        Args:
          fn(string): HDF5 filename for output. '.hdf5' will be
            appended to the filename if necessary. If fn is None,
            a filename will be generated starting with "sweep\_"
            followed by a timestamp.
          overwrite(boolean): If True and fn is not None, will
            silently overwrite any previous files of the same name.
          dryrun(boolean): Simulates what would happen if UQ is run, without
            actually running the TestProgram.
        Returns:
          True on success.
        """

        if fn is not None:
            self.fname = os.path.splitext(fn)[0]
            fn = self.fname + '.hdf5'
            if os.path.exists(fn):
                if not overwrite:
                    done = False
                    while 1:
                        ans = raw_input('%s already exists.  Replace (Y/N):' % fn)
                        try:
                            if ans.upper() == 'N':
                                done = True
                                break
                            elif ans.upper() == 'Y':
                                break
                        except:
                            pass
                        print "Please answer with 'Y' or 'N'\n"
                    if done:
                        sys.exit(-1)
                os.remove(fn)
        vprint(1, 'Saving run to %s.hdf5' % self.fname)
        return self.psweep.run(self,dryrun)

    def extend(self, num=None):
        return self.psweep.extend(num)

    def collect_data(self, hf=None):
        """ Collects data from captured stdout files and puts it in arrays
        in 'output/data'. Returns True on success.
        """

        need_to_close = False
        if hf is None:
            hf = h5py.File(self.fname + '.hdf5')
            need_to_close = True

        finished_jobs = self.host.collect(hf)
        self._extract_hdf5(hf, finished_jobs)

        has_data = 'data' in hf['output']
        if has_data:
            outd = hf['output/data']
            data = dict([(x, outd[x].value) for x in outd])
            params = dict([(p.name, p.values) for p in self.psweep.params])

        if need_to_close:
            hf.close()

        if not has_data and not self._reinit:
            print "WARNING: There is no data in the output section!"
            print "--Check that your runs completed successfully."
            return False
        return params, data

    def analyze_errors(self, hf):
        p = re.compile('Command exited with non-zero status \d+')
        for job in hf['output/jobs']:
            if job == 'time':
                continue
            err = hf['output/jobs/%s/stderr' % job].value
            res = p.findall(err)
            if res:
                print "Job %s: %s" % (job, res[0])
                for line in err.split('\n'):
                    if line != res[0] and not line.startswith('HDF5:{'):
                        print line
            elif len(err) == 0:
                print "Job %s never completed. Walltime exceeded?" % job

            results = False
            out = hf['output/jobs/%s/stdout' % job].value
            for line in out.split('\n'):
                if line.startswith('HDF5:{'):
                    results = True
                    break
            if not results:
                print "ERROR: Job %s has no output data in stdout." % job

    def analyze(self, verbose=False):
        """
        If not done already, collects the output from all the jobs into an HDF5 file.
        Parses any tagged data in the output and puts it in
        the output/data group in the HDF5 file.

        This function is called from the puq command line interface script automatically.
        """

        debug('')
        hf = h5py.File(self.fname + '.hdf5')
        if not self.host.status(quiet=1)[1]:
            print "Cannot collect data or perform analysis until all jobs are completed."
            print "You should do 'puq resume' to resume jobs."
            sys.exit(-1)

        # collect the data if it has not already been collected.
        has_data = 'output' in hf and 'data' in hf['output']
        if not has_data:
            print('No data found. Attempting to collect data')
            try:
                if not self.collect_data(hf):
                    raise Exception()
                self.psweep.analyze(hf)
            except:
                print 'Warning: analysis failed.'
                errors = 1

        # quick error check
        if 'data' in hf['output']:
            errors = 0
            try:
                options[self.psweep.__class__.__name__]['verbose'] = verbose
            except KeyError:
                options[self.psweep.__class__.__name__] = {'verbose': verbose}

            for var in hf['output/data']:
                if not isinstance(hf['output/data/%s' % var], h5py.Group):
                    tlen = len(hf['output/data/%s' % var].value)
                    num_jobs = len(hf['output/jobs'])-1
                    if tlen != num_jobs:
                        errors += 1
                        print "Expected %s data points for variable %s, but got %s." % (num_jobs, var, tlen)
                        self.analyze_errors(hf)
                        return errors

        if not 'psamples' in hf:
            s = get_psamples(self.psweep.params)
            if s is not None:
                hf['psamples'] = s

        # FIXME check for correlation if multiple outputs

        # calibrate
        if hasattr(self, 'caldata') and self.caldata is not None:
            self._calibrate(hf)

        hf.close()
        self._save_hdf5()
        return errors

    # Bayesian Calibration
    def _calibrate(self, hf):
        ovar = get_output_names(hf)[0]
        rs = unpickle(hf["/smolyak/%s/response" % ovar].value)
        self.psweep.params = calibrate(self.psweep.params, self.caldata, self.err, rs.eval)

    def _dump_hdf5_cache(self, hf, d):
        global _vcache, _dcache
        if len(_vcache):
            if d:
                dgrp = hf.require_group('output/data')
            else:
                dgrp = hf.require_group('output/jobs')
            for n in _vcache:
                if n in dgrp:
                    del dgrp[n]
                adata = _vcache[n]
                if d and len(adata.shape) > 1:
                    # Data is a multidimensional array and we want to do analysis
                    # on each array element individually.  So we write them
                    # individually to /output/data
                    numvals = np.prod(adata.shape[1:])
                    for i, index in enumerate(np.ndindex(adata.shape[1:])):
                        name = '%s%s' % (n, [ind for ind in index])
                        data = adata.flatten()[i::numvals]
                        ds = dgrp.create_dataset(name, data=data)
                        ds.attrs["description"] = _dcache[n]
                else:
                    ds = dgrp.create_dataset(n, data=adata)
                    ds.attrs["description"] = str(_dcache[n])
            _vcache = {}
            _dcache = {}

    def _dump_hdf5(self, grp, line, job, mjob):
        debug("Dump %s : %s" % (job, line))
        #print "Dump %s : %s" % (job, line)
        global _vcache, _dcache

        # old format used single quotes.
        if line.startswith("{'"):
            #sometimes nans can still slip through. if so set it to null
            #which gets converted to None when loading the json
            if "'value': nan" in line:
                line=line.replace("'value': nan","'value': null")
                print('warning: output value for job {} was nan'.format(job))

            line = line.replace("'", '"')

        x = unpickle(line)
        v = x['value']
        n = x['name']

        if not n in _vcache:
            if isinstance(v, ndarray):
                _vcache[n] = np.empty([mjob] + list(v.shape))
            else:
                _vcache[n] = np.empty((mjob))
            _vcache[n].fill(np.nan)
            _dcache[n] = x['desc']

        _vcache[n][job] = v

    # Extract tagged data to hdf5
    def _extract_hdf5(self, hf, jobs):
        debug("Extract")
        mjob = np.max(jobs) + 1
        run_grp = hf.require_group('output/jobs')
        for ext in ['out', 'err']:
            for j in jobs:
                grp = run_grp.require_group(str(j))
                if not 'std%s' % ext in grp:
                    continue
                f = grp['std%s' % ext].value
                cont = False
                for line in f.splitlines():
                    if cont:
                        line = line.strip()
                        cline += line
                        if line.endswith(':5FDH'):
                            cont = False
                            cline = cline[:-5]
                            self._dump_hdf5(grp, cline, j, mjob)
                    elif line.startswith('HDF5:'):
                        line = line[5:].strip()
                        if line.endswith(':5FDH'):
                            line = line[:-5]
                            self._dump_hdf5(grp, line, j, mjob)
                        else:
                            cont = True
                            cline = line
                    elif ext == 'err':
                        print 'STDERR[job %d]: %s' % (j, line)
            self._dump_hdf5_cache(hf, ext == 'out')

    def resume(self):
        if hasattr(self.host, 'jobs'):
            self.host.run()
            self._save_hdf5()
            self.analyze()
        else:
            print "All jobs finished."

max_stars_repo_name: zoidy/puq

max_issues_repo_name: zoidy/puq

max_forks_repo_name: zoidy/puq

reason: import numpy,from numpy


text:
# Copyright 2020 - 2021 MONAI Consortium
# 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.

"""Transforms using a smooth spatial field generated by interpolating from smaller randomized fields."""

from typing import Any, Optional, Sequence, Union

import numpy as np

import monai
from monai.transforms.spatial.array import Resize
from monai.transforms.transform import Randomizable, RandomizableTransform, Transform
from monai.transforms.utils import rescale_array
from monai.utils import InterpolateMode, ensure_tuple
from monai.utils.enums import TransformBackends
from monai.utils.type_conversion import convert_to_dst_type

__all__ = ["SmoothField", "RandSmoothFieldAdjustContrast", "RandSmoothFieldAdjustIntensity"]

class SmoothField(Randomizable):
    """
    Generate a smooth field array by defining a smaller randomized field and then resizing to the desired size. This
    exploits interpolation to create a smoothly varying field used for other applications.

    Args:
        spatial_size: final output size of the array
        rand_size: size of the randomized field to start from
        padder: optional transform to add padding to the randomized field
        mode: interpolation mode to use when upsampling
        align_corners: if True align the corners when upsampling field
        low: low value for randomized field
        high: high value for randomized field
        channels: number of channels of final output
    """

    def __init__(
        self,
        spatial_size: Union[Sequence[int], int],
        rand_size: Union[Sequence[int], int],
        padder: Optional[Transform] = None,
        mode: Union[InterpolateMode, str] = InterpolateMode.AREA,
        align_corners: Optional[bool] = None,
        low: float = -1.0,
        high: float = 1.0,
        channels: int = 1,
    ):
        self.resizer: Transform = Resize(spatial_size, mode=mode, align_corners=align_corners)
        self.rand_size: tuple = ensure_tuple(rand_size)
        self.padder: Optional[Transform] = padder
        self.field: Optional[np.ndarray] = None
        self.low: float = low
        self.high: float = high
        self.channels: int = channels

    def randomize(self, data: Optional[Any] = None) -> None:
        self.field = self.R.uniform(self.low, self.high, (self.channels,) + self.rand_size)  # type: ignore
        if self.padder is not None:
            self.field = self.padder(self.field)

    def __call__(self):
        resized_field = self.resizer(self.field)

        return rescale_array(resized_field, self.field.min(), self.field.max())

class RandSmoothFieldAdjustContrast(RandomizableTransform):
    """
    Randomly adjust the contrast of input images by calculating a randomized smooth field for each invocation. This
    uses SmoothFieldAdjustContrast and SmoothField internally.

    Args:
        spatial_size: size of input array's spatial dimensions
        rand_size: size of the randomized field to start from
        padder: optional transform to add padding to the randomized field
        mode: interpolation mode to use when upsampling
        align_corners: if True align the corners when upsampling field
        prob: probability transform is applied
        gamma: (min, max) range for exponential field
    """

    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]

    def __init__(
        self,
        spatial_size: Union[Sequence[int], int],
        rand_size: Union[Sequence[int], int],
        padder: Optional[Transform] = None,
        mode: Union[InterpolateMode, str] = InterpolateMode.AREA,
        align_corners: Optional[bool] = None,
        prob: float = 0.1,
        gamma: Union[Sequence[float], float] = (0.5, 4.5),
    ):
        super().__init__(prob)

        if isinstance(gamma, (int, float)):
            self.gamma = (0.5, gamma)
        else:
            if len(gamma) != 2:
                raise ValueError("Argument `gamma` should be a number or pair of numbers.")

            self.gamma = (min(gamma), max(gamma))

        self.sfield = SmoothField(spatial_size, rand_size, padder, mode, align_corners, self.gamma[0], self.gamma[1])

    def set_random_state(
        self, seed: Optional[int] = None, state: Optional[np.random.RandomState] = None
    ) -> "RandSmoothFieldAdjustContrast":
        super().set_random_state(seed, state)
        self.sfield.set_random_state(seed, state)
        return self

    def randomize(self, data: Optional[Any] = None) -> None:
        super().randomize(None)

        if self._do_transform:
            self.sfield.randomize()

    def __call__(self, img: np.ndarray, randomize: bool = True):
        """
        Apply the transform to `img`, if `randomize` randomizing the smooth field otherwise reusing the previous.
        """
        if randomize:
            self.randomize()

        if not self._do_transform:
            return img

        img_min = img.min()
        img_max = img.max()
        img_rng = img_max - img_min

        field = self.sfield()
        field, *_ = convert_to_dst_type(field, img)

        img = (img - img_min) / max(img_rng, 1e-10)  # rescale to unit values
        img = img ** field  # contrast is changed by raising image data to a power, in this case the field

        out = (img * img_rng) + img_min  # rescale back to the original image value range

        out, *_ = convert_to_dst_type(out, img, img.dtype)

        return out

class RandSmoothFieldAdjustIntensity(RandomizableTransform):
    """
    Randomly adjust the intensity of input images by calculating a randomized smooth field for each invocation. This
    uses SmoothField internally.

    Args:
        spatial_size: size of input array
        rand_size: size of the randomized field to start from
        padder: optional transform to add padding to the randomized field
        mode: interpolation mode to use when upsampling
        align_corners: if True align the corners when upsampling field
        prob: probability transform is applied
        gamma: (min, max) range of intensity multipliers
    """

    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]

    def __init__(
        self,
        spatial_size: Union[Sequence[int], int],
        rand_size: Union[Sequence[int], int],
        padder: Optional[Transform] = None,
        mode: Union[monai.utils.InterpolateMode, str] = monai.utils.InterpolateMode.AREA,
        align_corners: Optional[bool] = None,
        prob: float = 0.1,
        gamma: Union[Sequence[float], float] = (0.1, 1.0),
    ):
        super().__init__(prob)

        if isinstance(gamma, (int, float)):
            self.gamma = (0.5, gamma)
        else:
            if len(gamma) != 2:
                raise ValueError("Argument `gamma` should be a number or pair of numbers.")

            self.gamma = (min(gamma), max(gamma))

        self.sfield = SmoothField(spatial_size, rand_size, padder, mode, align_corners, self.gamma[0], self.gamma[1])

    def set_random_state(
        self, seed: Optional[int] = None, state: Optional[np.random.RandomState] = None
    ) -> "RandSmoothFieldAdjustIntensity":
        super().set_random_state(seed, state)
        self.sfield.set_random_state(seed, state)
        return self

    def randomize(self, data: Optional[Any] = None) -> None:
        super().randomize(None)

        if self._do_transform:
            self.sfield.randomize()

    def __call__(self, img: np.ndarray, randomize: bool = True):
        """
        Apply the transform to `img`, if `randomize` randomizing the smooth field otherwise reusing the previous.
        """

        if randomize:
            self.randomize()

        if not self._do_transform:
            return img

        img_min = img.min()
        img_max = img.max()

        field = self.sfield()
        rfield, *_ = convert_to_dst_type(field, img)

        out = img * rfield
        out, *_ = convert_to_dst_type(out, img, img.dtype)

        return out

reason: import numpy


text:
#!/usr/bin/python

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])

# Elementwise sum; both produce the array
print(x + y)
print(np.add(x, y))

print("**********************")

# Elementwise difference
print(x - y)
print(np.subtract(x, y))

print("**********************")

# Elementwise product
print(x * y)
print(np.multiply(x, y))

print("**********************")

# Elementwise division
print(x / y)
print(np.divide(x, y))

print("**********************")

# Elementwise square root
print(np.sqrt(x))

max_stars_repo_name: janbodnar/Python-Course

max_issues_repo_name: janbodnar/Python-Course

max_forks_repo_name: janbodnar/Python-Course


text:
import numpy as np
import cv2
import torch.nn as nn
import torch
import torch.nn.functional as F

from multiscaleloss import charbonnier_loss, warp

def celex_compute_photometric_loss(prev_images_temp, next_images_temp, event_images, output, weights=None,
                                   loss_area=-1):
    # size is
    # prev_images_tmp: 8*256*256
    # next_images_tmp: 8*256*256
    # event_images:    8*4*256*256
    # output:          tuple(8*2*256*256,8*2*128*128,8*2*64*64,8*2*32*32)

    prev_images = np.array(prev_images_temp)
    next_images = np.array(next_images_temp)

    total_photometric_loss = 0.
    loss_weight_sum = 0.

    # 256 128 64 32 四个尺寸都计算一遍
    for i in range(len(output)):
        flow = output[i]

        m_batch = flow.size(0)  # 8
        height = flow.size(2)  # 256
        width = flow.size(3)  # 256

        prev_images_resize = torch.zeros(m_batch, 1, height, width)
        next_images_resize = torch.zeros(m_batch, 1, height, width)

        for p in range(m_batch):
            # 这里有必要resize吗，两个应该是相同大小的啊。不同！！！，这里的宽高是flow的宽高，不一定是256*256
            prev_images_resize[p, 0, :, :] = torch.from_numpy(
                cv2.resize(prev_images[p, :, :], (height, width), interpolation=cv2.INTER_LINEAR))
            next_images_resize[p, 0, :, :] = torch.from_numpy(
                cv2.resize(next_images[p, :, :], (height, width), interpolation=cv2.INTER_LINEAR))

        # 根据光流，将第二张图反推，得到第一张图，然后和原始的第一张图做比较
        next_images_warped = warp(next_images_resize.cuda(), flow.cuda())
        # 得到灰度值差，如果为了适应celex这里的差值似乎应该做一些修改
        if loss_area == -1:
            error_temp = next_images_warped - prev_images_resize.cuda()
        else:
            # 这里考虑边界部分不进行loss计算
            for row in range(loss_area // 2, height - loss_area // 2):
                for col in range(loss_area // 2, width - loss_area // 2):
                    error_temp[row][col] = np.mean(next_images_warped[row - loss_area // 2:row + loss_area // 2,
                                                  col - loss_area // 2:col + loss_area // 2] -
                                                  prev_images_resize[row - loss_area // 2:row + loss_area // 2,
                                                  col - loss_area // 2:col + loss_area // 2])

        # charbonnier损失
        photometric_loss = charbonnier_loss(error_temp)

        # 对不同的尺寸赋予不同的权重，但是这里都是的weights都是1
        total_photometric_loss += weights[len(weights) - i - 1] * photometric_loss
        loss_weight_sum += 1.
    # 最后再求个平均
    total_photometric_loss = total_photometric_loss / loss_weight_sum

    return total_photometric_loss


text:
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved
#
# 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.
"""
This file aims to use multiprocessing to do following process.
    `
    for data in reader():
        yield func(data)
    `
"""
#encoding=utf8
import numpy as np
import multiprocessing as mp
import traceback
from pgl.utils.logger import log

def mp_reader_mapper(reader, func, num_works=4):
    """
    This function aims to use multiprocessing to do following process.
    `
    for data in reader():
        yield func(data)
    `
    The data in_stream is the `reader`, the mapper is map the in_stream to
    an out_stream.
    Please ensure the `func` have specific return value, not `None`!
    :param reader: the data iterator
    :param func: the map func
    :param num_works: number of works
    :return: an new iterator
    """

    def _read_into_pipe(func, conn):
        """
        read into pipe, and use the `func` to get final data.
        """
        while True:
            data = conn.recv()
            if data is None:
                conn.send(None)
                conn.close()
                break
            conn.send(func(data))

    def pipe_reader():
        """pipe_reader"""
        conns = []
        all_process = []
        for w in range(num_works):
            parent_conn, child_conn = mp.Pipe()
            conns.append(parent_conn)
            p = mp.Process(target=_read_into_pipe, args=(func, child_conn))
            p.start()
            all_process.append(p)

        data_iter = reader()
        if not hasattr(data_iter, "__next__"):
            __next__ = data_iter.next
        else:
            __next__ = data_iter.__next__

        def next_data():
            """next_data"""
            _next = None
            try:
                _next = __next__()
            except StopIteration:
                # log.debug(traceback.format_exc())
                pass
            except Exception:
                log.debug(traceback.format_exc())
            return _next

        for i in range(num_works):
            conns[i].send(next_data())

        finish_num = 0
        finish_flag = np.zeros(len(conns), dtype="int32")
        while finish_num < num_works:
            for conn_id, conn in enumerate(conns):
                if finish_flag[conn_id] > 0:
                    continue
                sample = conn.recv()
                if sample is None:
                    finish_num += 1
                    conn.close()
                    finish_flag[conn_id] = 1
                else:
                    yield sample
                    conns[conn_id].send(next_data())

    return pipe_reader

max_stars_repo_name: zbmain/PGL

max_issues_repo_name: zbmain/PGL

max_forks_repo_name: zbmain/PGL


text:
import torch
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as spio
import os
import time

def imprepare(img):

    if type(img) == np.ndarray:
        # make sure data is numpy
        pass

    else:
        img = img.cpu() # put from gpu to cpu
        img = img.detach() # in case img is a torch tensor
        npimg = img.numpy()

    img = img / 2 + 0.5     # unnormalize
    img = torch.clamp(torch.from_numpy(img), min=0.0, max=1.0) #clamp outliers
    npimg = img.numpy()

    return npimg

def cudaLoader(model, train_set, val_set):
    """
    This function takes the model, the train set and the validation set as
    inputs and sends them to cuda devices if available.
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    # device = ('cpu') # comment/uncomment to use GPU
    print("device:\t", device)

    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
        model = nn.DataParallel(model)

    model.to(dtype = torch.double) #typecasting
    train_set = train_set.to(device)
    val_set = val_set.to(device)
    model = model.to(device);
    return model, train_set, val_set

#target_dir can be generated with timestamp !!
def imsave(recon, target_dir, s, t):
    outputPath = ('C:/Users/littl/Documents/PythonScripts/reconproject/ \
            cmriRecon/data/' + target_dir)
    spio.savemat(outputPath + 'SliceatT{}'.format(t), \
            {'SliceatT{}'.format(t): recon[0,0,...].numpy()}) #dict w/ np value
    print("Result images saved at:\n", outputPath )

# Save files to matlab folder
def savetomatlab(target_dir, datatype, datatypestr, sample_number, overwrite = False): # e.g. savetomatlab(foo_dir, input)

    if overwrite == False:
        print("Set overwrite = True to save the results !")

#   print avoids to mistakenly overwrite previous results.
    if overwrite == True:
        # Check computer to save .mat files in right path
        if COMPUTER == "Dell":
            output_path = ('C:/Users/littl/Documents/PythonScripts/reconproject/cmriRecon/results/' + target_dir)
        elif COMPUTER == "O2":
            output_path = ('/home/nw92/reconproject/cmriRecon/results/' + target_dir)

        # Check if folder already exists
        if not os.path.exists(output_path):
            os.makedirs(output_path)

        spio.savemat(output_path + '/' + datatypestr + '_sample_' + str(sample_number),
        {datatypestr + '_sample_' + str(sample_number)
        : datatype[sample_number,...]
        }) #dict w/ np value

        print("Result images saved at:\n", output_path )

#not written yet
def myDataLoader():
    pass

#not written yet
def myTrainer():
    pass

max_stars_repo_name: ygCoconut/MRIreconUNET

max_issues_repo_name: ygCoconut/MRIreconUNET

max_forks_repo_name: ygCoconut/MRIreconUNET

reason: import numpy,import scipy


text:
import tensorflow as tf
import numpy as np

def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)

loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50:
        print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))


text:
import numpy as np
import pandas as pd
from sklearn import svm
import sys
import argparse

from sklearn.linear_model import Perceptron
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import VotingRegressor
from ml_models.conv_net import OneDConvNet

def parse_args():
    parser = argparse.ArgumentParser(description = "Run regression on tab file")
    required = parser.add_argument_group("Required arguments to run")
    required.add_argument("input", type=argparse.FileType('r'))
    args = parser.parse_args()
    return args

def get_input_file():
    try:
        ASO_file = pd.read_csv(args.input.name, delimiter="\t")
        return(ASO_file)
    except Exception as err:
        print("Error opening file.")
        print(err)
        sys.exit(1)

def split(word):
    return list(word)

def one_hot(nuc_list):
    one_hot = []
    for nucelotide in nuc_list:
        if nucelotide == 'A':
            one_hot.append([1,0,0,0])
        elif nucelotide == 'C':
            one_hot.append([0,1,0,0])
        elif nucelotide == 'G':
            one_hot.append([0,0,1,0])
        elif nucelotide == 'T':
            one_hot.append([0,0,0,1])
        else:
            one_hot.append([0,0,0,0])
    return one_hot

def main():

    ASO_score_data = get_input_file()

    # Split ASO string into a list.
    ASO_score_data["ASO"] = ASO_score_data["ASO"].apply(lambda x: one_hot(split(x)))
    ASO_score_data["ASO"] = ASO_score_data["ASO"].apply(lambda l: [item for sublist in l for item in sublist])

    X = pd.DataFrame(ASO_score_data["ASO"].to_list())
    X = X.replace(np.nan, 0)
    Y = ASO_score_data["score"]

    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
    X_train = X_train.to_numpy()
    X_test = X_test.to_numpy()
    Y_train = Y_train.to_numpy()
    Y_test = Y_test.to_numpy()

    # Conv net sample code
    one_d_convnet = OneDConvNet()
    trained_model = one_d_convnet.fit(X_train, X_test, Y_train, Y_test, epochs=1, batch_size=32)

    clf = svm.SVR()
    clf.fit(X_train, Y_train)
    Y_predicted = clf.predict(X_test)

    mse = np.mean(np.square(Y_predicted - Y_test))

    for i in range(len(Y_predicted)):
        print("Predicted Value: " + str(Y_predicted[i]))
        print("Actual Value: " + str(Y_test[i]))
        print("-------------------------------------------------")

    print("Mean Squared Error: " + str(mse))

if __name__ == '__main__':
    args = parse_args()
    if args.input:
        main()
    else:
        print("Enter input file")

max_stars_repo_name: lackeylela/openASO

max_issues_repo_name: lackeylela/openASO

max_forks_repo_name: lackeylela/openASO


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 10 10:53:28 2021
Unassign parcels that are not included for all subjects (0 sources).
Note that the IDs are changed by the reduction! Removed IDs are saved.
@author: rouhinen
"""

import os
import glob
import numpy as np
import time

subjectsFolder = 'K:\\palva\\fidelityWeighting\\fwSubjects_p\\'
sourceIdPattern = '\\*parc2018yeo7_600.npy'
oldPattern = 'parc2018yeo7_600'
newPattern = 'parc2018yeo7_600_reduced'
reducedPattern = 'parc2018yeo7_600_reducedParcels'

## Search folders in main folder.
subjects = next(os.walk(subjectsFolder))[1]
if any('_Population' in s for s in subjects):
    subjects.remove('_Population')
maxID = 0
sourceIdentities = []
fileSourceIDNew = []
times = [time.perf_counter()]

## Loop over folders containing subjects. Get maximum parcel number. Populate source identities.
for i, subject in enumerate(subjects):
    subjectFolder = os.path.join(subjectsFolder, subject)

    # Load original source identities
    fileSourceIdentities = glob.glob(subjectFolder + sourceIdPattern)[0]

    identities = np.load(fileSourceIdentities)         # Source length vector. Expected ids for parcels are 0 to n-1, where n is number of parcels, and -1 for sources that do not belong to any parcel.

    """ Get maximum ID. Continuous IDs are not assumed for each subject,
    but are at the population level. """
    maxID = max([maxID, max(identities)])

    sourceIdentities.append(identities)
    fileSourceIDNew.append(fileSourceIdentities.replace(oldPattern, newPattern))

expectedIDs = list(range(maxID+1))
missingIDs = []
## Loop over subjects. Check which parcels have 0 parcels on at least one subject.
for i, identities in enumerate(sourceIdentities):
    """ Get unique parcel IDs, non-negative. """
    idSet = set(identities)                         # Get unique IDs
    idSet = [item for item in idSet if item >= 0]   # Remove negative values (should have only -1 if any)

    # If idSet is smaller than idSet, save missing values.
    if len(expectedIDs) > len(idSet):
        missing = list(range(maxID+1))
        for ii, ID in enumerate(idSet):
            missing.remove(ID)

        # Concatenate to missingIDs
        missingIDs += missing

# Sorted small to large list of parcels with some subjects having 0 sources.
missingIDs = np.sort(list(set(missingIDs)))

## Loop over subjects. Set missing ID to -1.
for i, identities in enumerate(sourceIdentities):
    # Loop over missing IDs.
    for ii, missing in enumerate(missingIDs):
        identities = [ID if ID !=missing else -1 for ID in identities]

    ## Loop over missing IDs, large to small.
    for ii, missing in enumerate(missingIDs[::-1]):
        identities = [ID-1 if ID > missing else ID for ID in identities]

    np.save(fileSourceIDNew[i], identities)
    reducedFile = fileSourceIDNew[i].replace(newPattern, reducedPattern)
    np.save(reducedFile, list(missingIDs))

print('Parcel IDs set to -1: ' + str(np.sort(list(missingIDs))))

max_stars_repo_name: sanrou/fidelityWeighting

max_issues_repo_name: sanrou/fidelityWeighting

max_forks_repo_name: sanrou/fidelityWeighting


text:
"""
This submodule taken from: https://github.com/bmorris3/irtf_templates
"""

import os

import astropy.units as u
import h5py
import numpy as np
from astropy.modeling.blackbody import blackbody_lambda

from .spectrum import Spectrum1D
from .spectral_types import spt_to_teff

__all__ = ['IRTFTemplate']

hdf5_archive_path = os.path.join(os.path.dirname(__file__), os.pardir, 'data',
                                 'irtf_templates.hdf5')

class TemplateArchive(object):
    def __init__(self):
        self._hdf5 = None

    @property
    def hdf5(self):
        return h5py.File(hdf5_archive_path, 'r+')

archive = TemplateArchive()

class IRTFTemplate(Spectrum1D):
    def __init__(self, sptype, fill_gaps=True):
        """
        Parameters
        ----------
        sptype : str
            Spectral type of target.
        """
        with archive.hdf5 as f:
            data = f['templates'][sptype.upper()][:]
            header = {k: v for k, v in
                      f['templates'][sptype.upper()].attrs.items()}

        self.wavelength = data[:, 0] * u.um
        self.flux = data[:, 1] * u.W * u.m**-2 * u.um**-1
        self.error = data[:, 2]
        self.header = header

        try:
            self.t_eff = spt_to_teff(sptype)
        except KeyError:
            self.t_eff = None

        if fill_gaps:
            self.fill_gaps()

    def fill_gaps(self):
        """
        Fill gaps in template spectra at strong telluric absorption bands.

        Will do a quadratic fit to the nearby continuum and fill the gap by
        extrapolating the smooth quadratic across the gap.
        """
        normed_template = self.flux# / self.flux.max()
        diffs = np.diff(self.wavelength)
        indices = np.where(diffs > 100*np.median(diffs))[0]
        gap_fillers = []

        for i in indices:
            wl_min, wl_max = self.wavelength[i], self.wavelength[i+1]
            delta_lambda = self.wavelength[i] - self.wavelength[i-1]

            p = np.polyfit(self.wavelength[i-500:i+500],
                           normed_template[i-500:i+500], 2)
            gap_wavelengths = np.arange(wl_min.value+delta_lambda.value,
                                        wl_max.value-delta_lambda.value,
                                        delta_lambda.value) * u.um
            fit = np.polyval(p, gap_wavelengths)
            gap_fillers.append([gap_wavelengths, fit])

        # Extend to 5.3 um
        if self.wavelength.max() < 5.3 * u.um:
            p = np.polyfit(self.wavelength[-1000:], normed_template[-1000:], 1)
            gap_wavelengths = np.arange(self.wavelength.max().value, 5.3,
                                        delta_lambda.value) * u.um
            fit = np.polyval(p, gap_wavelengths)
            gap_fillers.append([gap_wavelengths, fit])

        gap_wls = [i[0] for i in gap_fillers]
        gap_fluxes = [i[1] for i in gap_fillers]
        wl_unit = self.wavelength.unit
        fl_unit = self.flux.unit
        self.wavelength = np.concatenate([self.wavelength] + gap_wls)
        self.flux = np.concatenate([self.flux] + gap_fluxes)

        sort = np.argsort(self.wavelength)
        self.wavelength = u.Quantity(self.wavelength[sort].value, wl_unit)
        self.flux = u.Quantity(self.flux[sort].value, fl_unit)

    def scale_temperature(self, delta_teff, plot=False):
        """
        Scale up or down the flux according to the ratio of blackbodies between
        the effective temperature of the IRTF template spectrum and the
        new effective temperature ``t_eff + delta_teff``.

        Parameters
        ----------
        delta_teff : float
            Change in effective temperature to approximate

        Returns
        -------
        spec : `~libra.IRTFTemplate`
            Scaled spectral template to ``t_eff + delta_teff``
        """

        new_t_eff = self.t_eff + delta_teff
        old_bb = blackbody_lambda(self.wavelength, self.t_eff)
        new_bb = blackbody_lambda(self.wavelength, new_t_eff)

        ratio_bbs = (new_bb / old_bb).value

        if plot:
            import matplotlib.pyplot as plt
            fig, ax = plt.subplots(figsize=(10, 8))
            ax.plot(self.wavelength, self.flux, label='init')
            ax.plot(self.wavelength,
                    old_bb * np.max(self.flux) / np.max(old_bb), label='old bb')
            ax.plot(self.wavelength,
                    new_bb * np.max(self.flux) / np.max(old_bb), label='new bb', ls='--')
            ax.plot(self.wavelength, ratio_bbs * self.flux, label='scaled', ls=':')
            ax.legend()

        return Spectrum1D(self.wavelength, ratio_bbs * self.flux,
                          header=self.header, t_eff=new_t_eff)

max_stars_repo_name: jlustigy/libra

max_issues_repo_name: jlustigy/libra

max_forks_repo_name: jlustigy/libra

reason: import numpy,import astropy,from astropy


text:
"""Submodule for a class extending ``ase.Atoms`` with bonds."""

import sys
import itertools

import ase
import numpy as np

from .lammps_reader import (
  read_box, read_bonds, read_atoms_from_data, read_atoms_from_molecule)
from .lammps_units import lmp_units

def create_atoms_from_data(
  path, atom_style, pbc=False, lammps_unit="real"):
  """Create a BondedAtoms instance from Lammps' data file.

  Parameters:

  path: str
    File path to Lammps' data file.

  atom_style: str
    Specifies an *atom style* used in Lammps.

  pbc: array-like object of bool
    For each axis in the unit cell decides whether the positions
    will be moved along this axis.

  lammps_unit: str
    Specifies a *unit* used in Lammps.

  """
  if isinstance(pbc, bool):
    pbc = (pbc,) * 3

  atoms = read_atoms_from_data(path, atom_style)
  bonds = read_bonds(path)

  obj = create_atoms_from_json(atoms, bonds, lammps_unit)

  dunit2ase = lmp_units[lammps_unit]["_2si"]["distance"] * ase.units.m
  obj.set_cell([dunit2ase * l for l in read_box(path)])

  obj.set_pbc(pbc)
  obj.adjust_pbc_bonds()

  return obj

def create_atoms_from_molecule(path, lammps_unit="real"):
  """Create a BondedAtoms instance from Lammps' molecule file.

  Parameters:

  path: str
    File path to Lammps' molecule file.

  lammps_unit: str
    Specifies a *unit* used in Lammps.

  """
  atoms = read_atoms_from_molecule(path)
  bonds = read_bonds(path)

  return create_atoms_from_json(atoms, bonds, lammps_unit)

def create_atoms_from_json(atoms, bonds=[], lammps_unit="real"):
  """Create a BondedAtoms instance from JSON object(s).

  Parameters:

  atoms: list of dict
    JSON describing atoms.
    All the dictionary contained in the list must have the same keys.
    The following keys are required (some are optional):

    * ``xu`` (float) : In Lammps' unit.
    * ``yu`` (float) : In Lammps' unit.
    * ``zu`` (float) : In Lammps' unit.
    * ``type`` (int) : Optional.
    * ``mass`` (float) : In Lammps' unit. Optional.
    * ``vx`` (float) : In Lammps' unit. Optional.
    * ``vy`` (float) : In Lammps' unit. Optional.
    * ``vz`` (float) : In Lammps' unit. Optional.
    * ``id`` (int) : Required when ``bonds`` is supplied.

  bonds: list of dict
    JSON describing bonds.
    All the dictionary contained in the list must have the same keys.
    The following keys are required:

    * ``atom1-id`` (int) : The first atom id.
    * ``atom2-id`` (int) : The second atom id.

  lammps_unit: str
    Specifies a *unit* used in Lammps.

  """
  lmp_unit = lmp_units[lammps_unit]
  dunit2ase = lmp_unit["_2si"]["distance"] * ase.units.m  # distance
  vunit2ase = dunit2ase / lmp_unit["_2si"]["time"] / ase.units.s  # velocity
  munit2ase = lmp_unit["_2si"]["mass"] * ase.units.kg  # mass

  obj = BondedAtoms(positions=[
      dunit2ase * np.array([atom["xu"], atom["yu"], atom["zu"]])
      for atom in atoms if all(k in atom for k in ["xu", "yu", "zu"])
    ])

  types = [atom["type"] for atom in atoms if "type" in atom]
  if len(types) == len(obj):
    obj.set_types(types)

  masses = [atom["mass"] for atom in atoms if "mass" in atom]
  if len(masses) == len(obj):
    obj.set_masses(munit2ase * np.array(masses))

  velocities = [
    vunit2ase * np.array([atom["vx"], atom["vy"], atom["vz"]])
    for atom in atoms if all(k in atom for k in ["vx", "vy", "vz"])
  ]
  if len(velocities) == len(obj):
    obj.set_velocities(velocities)

  if len(bonds) > 0:

    bondarray = np.array([
        [bond["atom1-id"], bond["atom2-id"]]
        for bond in bonds if "atom1-id" in bond and "atom2-id" in bond
      ])

    _, counts = np.unique(bondarray, return_counts=True)

    max_bonds_per_atom = max(counts)

    if 4 < max_bonds_per_atom:
      obj.change_max_bonds(max_bonds_per_atom)

    id2idx = {
      atom["id"]: i for i, atom in enumerate(atoms) if "id" in atom
    }

    for id1, id2 in bondarray:
      obj.add_bond(id2idx[id1], id2idx[id2])

  return obj

def _compute_image_flags(vec, cell, pbc):
  """Compute image flags for bond data.

  Parameters:

  vec: numpy.ndarray of number
      Vector from an atom owning a bond to the other atom
      connected by the bond.

  cell: numpy.ndarray of number
      Unit cell vectors.

  pbc: array-like object of bool
      For each axis in the unit cell decides whether the positions
      will be moved along this axis.

  """
  coeffs = np.linalg.solve(cell.T, vec) + 0.5
  coeffs[np.logical_not(pbc)] = 0.0
  coeffs //= 1.0

  return -coeffs.astype(int)

#=======================================================================

class BondedAtoms(ase.Atoms):
  """Inheriting ``ase.Atoms`` class, and has functionalities
  to handle (covalent) bonds connecting a pair of atoms.

  An instance of this class has bond data as a ``numpy.ndarray``
  as with other atomic properties like positions and velocities.
  Shape of this array for bond data is (*N*, *M*, 4), where *N* is
  the number of atoms and *M* is the maximum number of bonds per atom.
  The first axis of the array corresponds to atoms, and the second axis
  corresponds to bonds connected to each atom.
  Four integers in the third axis describes each bond:
  the first integer is a relative index for the other atom,
  the second to fourth integers are relative image flags
  (used for resolving a periodic boundary condition)
  for the other atom in the *x*, *y* and *z* direction, respectively.

  In addition, an instance of this class has type of atoms
  as a one-dimensional ``numpy.ndarray``. Length of this array is
  equal to the number of atoms. Atom's type is typically required
  for making Lammps' data file and molecule file.

  Basically, you can use methods of ``ase.Atoms`` in the same manner.

  """

  def __init__(self, *args, **kwargs):
    """
    Parameters:

    args:
      A variable number of arguments passed to ``super().__init__()``.

    kwargs:
      A variable number of named arguments passed to ``super().__init__()``.

    """
    super().__init__(*args, **kwargs)

    # maximum number of bonds per atom.
    self._max_bonds = 4

    if "bonds" not in self.arrays:
      self.new_array(
        "bonds", np.zeros((len(self), self._max_bonds, 4)), int)

    if "types" not in self.arrays:
      self.new_array(
        "types", np.ones(len(self)), int)

  def add_bond(self, atom1, atom2, img1=(0,0,0), img2=(0,0,0)):
    """Add a bond connecting two atoms.

    Parameters:

    atom1: int
      Index for the first atom.

    atom2: int
      Index for the second atom.

    img1: tuple/list of int
      Three-element tuple or list of integers indicating which image of
      a periodic simulation box the first atom is in.

    img2: tuple/list of int
      Three-element tuple or list of integers indicating which image of
      a periodic simulation box the second atom is in.

    .. note::

      * Bond data stored in the first atom:
        ``[atom2-atom1, img2[0]-img1[0], img2[1]-img1[1], img2[2]-img1[2]]``.
      * Bond data stored in the second atom:
        ``[atom1-atom2, img1[0]-img2[0], img1[1]-img2[1], img1[2]-img2[2]]``.

    """
    if not isinstance(img1, (tuple, list)) or len(img1) != 3:
      raise RuntimeError(
        "Image flag 'img1' should be 3-membered tuple/list")
    elif not isinstance(img2, (tuple, list)) or len(img2) != 3:
      raise RuntimeError(
        "Image flag 'img2' should be 3-membered tuple/list")

    relative_data = np.asarray([  # not `np.array` due to pylint alarm
        atom2-atom1, img2[0]-img1[0], img2[1]-img1[1], img2[2]-img1[2]
      ], int)

    idx1 = self._get_available_bond_index(
      atom1, relative_data)
    idx2 = self._get_available_bond_index(
      atom2, -relative_data, 1 if atom1 == atom2 else 0)

    self.arrays["bonds"][atom1][idx1] = relative_data
    self.arrays["bonds"][atom2][idx2] = -relative_data

  def change_max_bonds(self, n=4):
    """Change the maximum number of bonds per atom.

    Like original properties of ``ase.Atoms``,
    bond data is also stored as a ``numpy.ndarray``,
    which is a homogeneous array of fixed-size items.
    This means that all the atoms have the same capacity for bond data,
    and the capacity is fixed until manually changed.
    This method changes the capacity, namely,
    the maximum number of bonds per atom.
    Note that existing data stay unchanged after changing the capacity.

    Parameters:

    n: int
      New maximum number of bonds per atom.

    """
    if n < self._max_bonds:
      self.arrays["bonds"] = np.array(
        [bs[:n] for bs in self.arrays["bonds"]])
    elif self._max_bonds < n:
      zeros = np.zeros((n - self._max_bonds, 4), int)
      self.arrays["bonds"] = np.array(
        [np.append(bs, zeros, axis=0) for bs in self.arrays["bonds"]])

    self._max_bonds = n

  def extend(self, other):
    """Extend this instance by appending the given instance.

    This method returns this extended instance,
    which is the same behavior with ``ase.Atoms.extend()``.

    Parameters:

    other: instance of ase.Atom/ase.Atoms/BondedAtoms
      For ase.Atom or ase.Atoms instance,
      the instance will be down-casted to BondedAtoms before extension.

    """
    if type(other) == ase.Atom:
      other = self.__class__([other])

    if type(other) == ase.Atoms:
      other = self.__class__(other)

    return super().extend(other)

  def get_bonds(self):
    """Return an array of integers containing bond data.

    An instance of ``ase.Atoms`` stores data in ``self.arrays``;
    for example, atoms' positions are accessed by key 'positions'.
    For BondedAtoms instance, bond data is also stored
    in ``self.arrays`` with key of 'bonds'.
    This method returns a copy of ``self.arrays["bonds"]``.

    """
    return self.arrays["bonds"].copy()

  def get_bonds_per_atom(self):
    """Return a nested list of bond data.

    The returned list is two-dimensional:
    each element of the first list corresponds to each atom,
    and the second list consists of *absolute* indices for atoms
    bonded with the each atom.

    .. note::

      This method is different from ``self.get_bonds()`` in terms of
      the following two points:

      * No data about image flags.
      * Contains data of *existing* bonds only.

    """
    return [
      [i + b[0] for b in bs if b[0] != 0]
      for i, bs in enumerate(self.arrays["bonds"])
    ]

  def get_types(self):
    """Return an array of integers containing atom's type.

    An instance of ``ase.Atoms`` stores data in ``self.arrays``;
    for example, atoms' positions are accessed by key 'positions'.
    For BondedAtoms instance, atom's type data is also stored
    in ``self.arrays`` with key of 'types'.
    This method returns a copy of ``self.arrays["types"]``.

    """
    return self.arrays["types"].copy()

  def get_bonded_bonds(self):
    """Return a two-dimensional ``numpy.ndarray`` describing all bonds.

    Shape of the returned array is (*N_b*, 2),
    where *N_b* is the number of bonds.
    Each row (the first axis) corresponds to each bond,
    and each column (the second axis) consists of *absolute* index
    of the first and second atom connected by the each bond.

    """
    bonds = set()

    bonds_per_atom = self.get_bonds_per_atom()

    for i, bs in enumerate(bonds_per_atom):
      bonds |= set((i, j) for j in bs if (j, i) not in bonds)

    return np.array(sorted(list(bonds)), int)

  def get_bonded_angles(self):
    """Return a two-dimensional ``numpy.ndarray``
    describing all angles defined by two consecutive bonds.

    Shape of the returned array is (*N_a*, 3),
    where *N_a* is the number of angles
    defined by two consecutive bonds.
    Each row (the first axis) corresponds to each angle,
    and each column (the second axis) consists of *absolute* index
    of the first, second and third atom in the each angle.

    """
    bonds_per_atom = self.get_bonds_per_atom()

    return np.array([
        (i, j, k)
        for j, bs in enumerate(bonds_per_atom)
        for i, k in itertools.combinations(bs, 2)
      ], int)

  def get_bonded_dihedrals(self):
    """Return a two-dimensional ``numpy.ndarray``
    describing all dihedrals defined by three consecutive bonds.

    Shape of the returned array is (*N_d*, 4),
    where *N_d* is the number of dihedrals
    defined by three consecutive bonds.
    Each row (the first axis) corresponds to each dihedral,
    and each column (the second axis) consists of *absolute* index
    of the first, second, third and fourth atom in the each dihedral.

    """
    bonds = set()
    dihedrals = []

    bonds_per_atom = self.get_bonds_per_atom()

    for j, bs in enumerate(bonds_per_atom):
      ks = [k for k in bs if (k, j) not in bonds]
      for k in ks:
        bonds.add((j, k))
        dihedrals.extend(
          (i, j, k, l) if n%2 else (l, k, j, i)  # equally distribute dihedrals to the second atom
          for n, (i, l) in enumerate(itertools.product(
            set(bs)-{k}, set(bonds_per_atom[k])-{j})))

    dihedrals.sort(key=lambda x: x[1])

    return np.array(dihedrals, int)

  def get_bonded_impropers(self):
    """Return a two-dimensional ``numpy.ndarray``
    describing all impropers defined by three bonds
    connected to the same atom.

    Shape of the returned array is (*N_i*, 4),
    where *N_i* is the number of impropers
    defined by three bonds connected to the same atom.
    Each row (the first axis) corresponds to each improper,
    and each column (the second axis) consists of *absolute* index
    of the first, second, third and fourth atom in the each improper;
    the first atom is the center one.

    """
    bonds_per_atom = self.get_bonds_per_atom()

    return np.array([
        (i,) + t
        for i, bs in enumerate(bonds_per_atom)
        for t in itertools.combinations(bs, 3)
      ], int)

  def remove_bond(self, atom1, atom2, img1=(0,0,0), img2=(0,0,0)):
    """Remove a bond connecting the given two atoms.

    Parameters:

    atom1: int
      Index for the first atom.

    atom2: int
      Index for the second atom.

    img1: tuple/list of int
      Three-element tuple or list of integers indicating which image of
      a periodic simulation box the first atom is in.

    img2: tuple/list of int
      Three-element tuple or list of integers indicating which image of
      a periodic simulation box the second atom is in.

    """
    if not isinstance(img1, (tuple, list)) or len(img1) != 3:
      raise RuntimeError(
        "Image flag 'img1' should be 3-membered tuple/list")
    elif not isinstance(img2, (tuple, list)) or len(img2) != 3:
      raise RuntimeError(
        "Image flag 'img2' should be 3-membered tuple/list")

    relative_data = np.asarray([  # not `np.array` due to pylint alarm
        atom2-atom1, img2[0]-img1[0], img2[1]-img1[1], img2[2]-img1[2]
      ], int)

    self._remove_bond(
      atom1, self._get_matched_bond_index(atom1, relative_data))
    self._remove_bond(
      atom2, self._get_matched_bond_index(atom2, -relative_data))

  def set_bonds(self, bonds):
    """Store the given bond data.

    This method sets bond data to ``self.array``
    using ``self.set_array()``.

    Parameters:

    bonds: array-like object of int
      Array describing bonds. Shape of the array must be (*N*, *M*, 4),
      where *N* is the number of atoms and *M* is the maximum number
      of bonds per atom. Four integers of innermost-axis are as follows:
      the first integer is a relative index for the other atom,
      the second to fourth integers are relative image flags
      for the other atom in the *x*, *y* and *z* direction, respectively.

    """
    self.set_array("bonds", bonds, int, ())

  def set_types(self, types):
    """Store the given type of atoms.

    This method sets type of atoms to ``self.array``
    using ``self.set_array()``.

    Parameters:

    types: array-like object of int
      Per-atom array containing atom's type.
      Length of the array must be equal to the number of atoms.

    """
    self.set_array("types", types, int, ())

  def sort_bonds(self):
    """Sort bonds in each atom.

    Note that the order of atoms stays unchanged.

    """
    def key_for_sorting_bonds(x):
      return (
        0 if x[0] > 0 else 1 if x[0] < 0 else 2,
        0 if all(i == 0 for i in x[1:]) else 1) + tuple(x)

    self.set_array("bonds", [
        sorted(bs, key=key_for_sorting_bonds)
        for bs in self.arrays["bonds"]
      ], int, ())

  def adjust_pbc_bonds(self):
    """Adjust bonds under periodic boundary conditions.

    This method corrects image flags of each bond if distance
    between two atoms connected with the bond is longer than
    a half length of the unit cell side.

    .. note::

      If the *i* th atom connects to one of images of the *j* th atom,
      it **must not** connect to another image of the the *j* th atom.
      In addition, all the bond lengths must be less than
      a half length of the unit cell side.

    """
    cell = self.get_cell()
    pbc = self.get_pbc()

    if pbc.any() and np.asarray(cell)[pbc].any(axis=1).all() and (cell, pbc):

      positions = self.get_positions()

      for i, bs in enumerate(self.arrays["bonds"]):
        for b in bs:
          if not np.all(b == 0):
            b[1:] = _compute_image_flags(
              positions[i+b[0]] - positions[i], cell, pbc)

  def wrap(self, center=(0.5, 0.5, 0.5), pbc=None, eps=1e-7):
      """Wrap positions to unit cell.

      Parameters:

      center: array-like object of float
          The positons in fractional coordinates that the new positions
          will be nearest possible to.

      pbc: bool or list/tuple of bool
        For each axis in the unit cell decides whether the positions
        will be moved along this axis.  By default, the boundary
        conditions of the Atoms object will be used.

      eps: float
        Small number to prevent slightly negative coordinates from being
        wrapped.

      """
      super().wrap(center, pbc, eps)
      self.adjust_pbc_bonds()

  def __delitem__(self, idx):
    """Delete a selected atom.

    This method overrides ``ase.Atoms.__delitem__``.
    Before calling the parent method to remove a selected atom,
    this method deletes bond data associated with the atom.

    Parameters:

    idx: int or list/tuple of int
      Index for an atom to be deleted.
      A list or tuple of integers is also acceptable for multiple atoms.

    Examples:

    >>> from ease4lmp import BondedAtoms
    >>> atoms = BondedAtoms('CO', positions=[(0, 0, 0), (0, 0, 1.1)])
    >>> atoms.set_bonds([
    ...   [[1, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]],
    ...   [[-1, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]]
    ... ])
    >>> atoms.get_bonds()
    array([[[ 1,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]],
           [[-1,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]]])
    >>> del atoms[0]
    >>> atoms.get_bonds()
    array([[[0, 0, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0]]])

    """
    if isinstance(idx, int):
      idx = np.array([idx])
    elif isinstance(idx, (tuple, list)) and len(idx) > 0:
      idx = np.array(idx)

    mask = np.ones(len(self), bool)
    mask[idx] = False

    for i, bs in enumerate(self.arrays["bonds"]):

      if not mask[i]:
        continue

      ib = 0
      while ib < self._max_bonds:
        j = i + bs[ib][0]
        if not mask[j]:
          self._remove_bond(i, ib)
        else:
          bs[ib][0] += (-np.sum(mask[i:j] == False)
            if i < j else np.sum(mask[j:i] == False))
          ib += 1

    super().__delitem__(idx)

  def __imul__(self, m):
    """Create a super cell and returns it.

    This method overrides ``ase.Atoms.__imul__``.
    After calling the parent method to expand the simulation box,
    this method recalculates image flags over the expanded bond data.
    Assuming that two atoms connected with a bond are
    in different images of the periodic simulation box before expansion.
    If the two atoms are in the same image after expansion,
    their image falgs for the opponent must be updated to (0, 0, 0).

    Parameters:

    m: int or list/tuple of int
      Three-element tuple or list defining how many times
      a periodic simulation box is repeated in each direction.
      The first, second and third element corresponds to the *x*, *y*
      and *z* direction, respectively.
      If an integer is given, a tuple ``(m, m, m)`` is used.

    Examples:

    >>> from ease4lmp import BondedAtoms
    >>> wire = BondedAtoms(
    ...   'Au',
    ...   positions=[[0.0, 5.0, 5.0]],
    ...   cell=[2.9, 5.0, 5.0],
    ...   pbc=[1, 0, 0])
    >>> wire.add_bond(0, 0, img2=(1, 0, 0))
    >>> wire.get_bonds()
    array([[[ 0,  1,  0,  0],
            [ 0, -1,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]]])
    >>> wire *= (3,1,1)
    >>> wire.get_bonds()
    array([[[ 1,  0,  0,  0],
            [ 2, -1,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]],
           [[ 1,  0,  0,  0],
            [-1,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]],
           [[-2,  1,  0,  0],
            [-1,  0,  0,  0],
            [ 0,  0,  0,  0],
            [ 0,  0,  0,  0]]])

    """
    if isinstance(m, int):
      m = (m, m, m)

    n = len(self)

    super().__imul__(m)

    bonds = self.arrays["bonds"]
    i0 = 0

    for m0 in range(m[0]):
      for m1 in range(m[1]):
        for m2 in range(m[2]):
          i1 = i0 + n
          for i in range(i0, i1):
            bs = [b for b in bonds[i] if np.any(b[1:] != 0)]
            for b in bs:
              img_old = b[1:]
              img_new = np.array([
                (m0 + img_old[0]) // m[0],
                (m1 + img_old[1]) // m[1],
                (m2 + img_old[2]) // m[2]], int)
              tmp = img_old - img_new * m  # m will be converted to <numpy.ndarray>
              b[0] += n * (tmp[0]*m[1]*m[2] + tmp[1]*m[2] + tmp[2])
              b[1:] = img_new
          i0 = i1

    return self

  def _get_available_bond_index(self, atom, relative_data, offset=0):
    """Return the minimum index available for bond data.

    Parameters:

    atom: int
      Index for an atom which a new bond is connected to.

    relative_data: numpy.ndarray of int
      Four-element one-dimensional ``numpy.ndarray``
      describing a new bond data to be added the given atom.

    offset: int
      Returned index is offset by this value.

    """
    for i, b in enumerate(self.arrays["bonds"][atom]):
      if (b == relative_data).all():
        raise RuntimeError(
          "Bond between atom '{}' and '{}' already exists"
          .format(atom, atom+relative_data[0]))
      elif np.all(b == 0):
        if offset == 0:
          return i
        else:
          offset -= 1

    raise RuntimeError(
      "Too many bonds attache to atom '{}'".format(atom))

  def _get_matched_bond_index(self, atom, relative_data):
    """Return an index for bond in the given atom.

    Parameters:

    atom: int
      Index for an atom.

    relative_data: numpy.ndarray of int
      Four-element one-dimensional ``numpy.ndarray``.
      This method returns an index for a bond described by this parameter.

    """
    for i, b in enumerate(self.arrays["bonds"][atom]):
      if (b == relative_data).all():
        return i

    raise RuntimeError(
      "No such a bond attaches to atom '{}'".format(atom))

  def _remove_bond(self, atom, bond_idx):
    """Remove a specified bond from the given atom.

    Parameters:

    atom: int
      Index for an atom.

    bond_idx: int
      Index for a bond (in the given atom) to be removed.

    """
    bonds = self.arrays["bonds"][atom]
    bonds[bond_idx:-1] = bonds[bond_idx+1:]
    bonds[-1] = np.zeros(4, int)


text:
import tensorflow as tf

import IPython.display as display

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12,12)
mpl.rcParams['axes.grid'] = False

import numpy as np
import PIL.Image
import time
import functools

def tensor_to_image(tensor):
  tensor = tensor*255
  tensor = np.array(tensor, dtype=np.uint8)
  if np.ndim(tensor)>3:
    assert tensor.shape[0] == 1
    tensor = tensor[0]
  return PIL.Image.fromarray(tensor)

#content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg', 'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg')
#content_path = tf.keras.utils.get_file('gorilla.jpg', 'https://wallpapermemory.com/uploads/427/gorilla-wallpaper-hd-1920x1080-145603.jpg')
#content_path = tf.keras.utils.get_file('baloo.jpg', 'https://scontent-mad1-1.cdninstagram.com/v/t51.2885-15/e35/14562053_1069598309828588_4501745636017700864_n.jpg?_nc_ht=scontent-mad1-1.cdninstagram.com&_nc_cat=108&_nc_ohc=wpMpw7G-3FEAX_WYwla&oh=d6be6a42f6cf5a48dcc9f7855780df81&oe=5EBE1987')
content_path = tf.keras.utils.get_file('park.jpg', 'https://es.parisinfo.com/var/otcp/sites/images/media/1.-photos/01.-ambiance-630-x-405/parc-des-buttes-chaumont-630x405-c-otcp-david-lefranc-158-32/23920-1-fre-FR/Parc-des-Buttes-Chaumont-630x405-C-OTCP-David-Lefranc-158-32.jpg')

# https://commons.wikimedia.org/wiki/File:Vassily_Kandinsky,_1913_-_Composition_7.jpg
#style_path = tf.keras.utils.get_file('kandinsky5.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')
#style_path = tf.keras.utils.get_file('hopper_corner.jpg','https://www.nga.gov/content/dam/ngaweb/features/slideshows/edward-hopper/hopper_corner.jpg')
#style_path = tf.keras.utils.get_file('vgrhone.jpg','https://i.etsystatic.com/9089439/r/il/2befad/1671747429/il_1588xN.1671747429_m2m9.jpg')
#style_path = tf.keras.utils.get_file('vgcorn.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Vincent_Van_Gogh_-_Wheatfield_with_Crows.jpg/2880px-Vincent_Van_Gogh_-_Wheatfield_with_Crows.jpg')
style_path = tf.keras.utils.get_file('vgthunder.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Vincent_van_Gogh_-_Wheatfield_Under_Thunderclouds_-_VGM_F778.jpg/2880px-Vincent_van_Gogh_-_Wheatfield_Under_Thunderclouds_-_VGM_F778.jpg')

def load_img(path_to_img, max_dim = 256):
  img = tf.io.read_file(path_to_img)
  img = tf.image.decode_image(img, channels=3)
  img = tf.image.convert_image_dtype(img, tf.float32)

  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
  long_dim = max(shape)
  scale = max_dim / long_dim

  new_shape = tf.cast(shape * scale, tf.int32)

  img = tf.image.resize(img, new_shape)
  img = img[tf.newaxis, :]
  return img

def imshow(image, title=None):
  if len(image.shape) > 3:
    image = tf.squeeze(image, axis=0)

  plt.imshow(image)
  if title:
    plt.title(title)

content_image = load_img(content_path, 1024)
style_image = load_img(style_path, 512)

#content_image = load_img('./audrey.jpg');
#style_image = load_img('./La maja desnuda.jpg');

plt.subplot(3, 1, 1)
imshow(content_image, 'Content Image')

plt.subplot(3, 1, 2)
imshow(style_image, 'Style Image')

import tensorflow_hub as hub
hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
stylized_image = hub_module(tf.constant(content_image), tf.constant(style_image))[0]

plt.subplot(3, 1, 3)
stylized = tensor_to_image(stylized_image)
plt.imshow(np.asarray(stylized))
plt.title('Stylized Image')

stylized.save('baloo-kandynsky.jpg')

plt.waitforbuttonpress()

max_stars_repo_name: Yondbee/fart

max_issues_repo_name: Yondbee/fart

max_forks_repo_name: Yondbee/fart


text:
from numpy import add, around, divide, multiply, ones, subtract, zeros
from pandas import DataFrame

'''
 <Decription here>
 Copyright (C) 2018  Robert Pilstål

 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.
'''

def align_dataframes(framea, frameb, fill_value = 0.0):
    """Use pandas DataFrame structure to align two-dimensional data

    :param framea: First pandas dataframe to align
    :param frameb: Other pandas dataframe to align
    :param fill_value: default fill value (0.0 float)
    return: tuple of aligned frames
    """
    zeroframe = frameb.copy()
    zeroframe[:] = fill_value
    aligneda = framea.add(zeroframe, fill_value = fill_value)
    zeroframe = framea.copy()
    zeroframe[:] = fill_value
    alignedb = frameb.add(zeroframe, fill_value = fill_value)
    return aligneda, alignedb

def dataframe_from_dictionary(data, indexmap = None, fillna = None):
    """Create a pandas dataframe from dictionary of dicts

    :param data: dictionary of dicts
    :param indexmap: a dictionary for mapping indices to new names,
                     if None, no mapping will be done (default)
    :param fillna: value to fill missing values with. If None, no
                   filling will be done (default)
    :return: pandas dataframe
    """
    # Convert dictionary to DataFrame
    df = DataFrame.from_dict(data,orient='index')
    if indexmap is not None:
        df = df.rename(index = indexmap, columns = indexmap)
    if fillna is not None:
        df = df.fillna(value = fillna)
    return df

def matrix_from_interactions(interactions, mapping, default=0.0):
    """Generate numpy matrices from interaction dictionary

    :param interactions: dictionary read from WORDOM avgpsn
    :param mapping: residuemap, preserves residue names
    :param default: default interaction
    :return: tuple of interaction strength and frequency numpy array
    """
    # Expecting symmetric dictionary
    size = len(mapping)
    strength = zeros((size, size)) + default
    frequency = zeros((size, size)) + default
    # Populera strength och frequency via mappings från interactions
    for resa, inter in interactions.items():
        for resb, (strng, freq) in inter.items():
            a = mapping[resa] - 1
            b = mapping[resb] - 1
            strength[a][b] = strength[b][a] = strng
            frequency[a][b] = frequency[b][a] = freq
    return strength, frequency

def matrix_from_pandas_dataframe(pddframe):
    """matrix_from_pandas_dataframe returns the .values member

    :param pddframe: pandas dataframe
    """
    return pddframe.values

def matrix_to_colorarray(matrix, hue_from = (1.0, 1.0, 1.0), hue_to = (1.0, 0.0, 0.0), channel_max = 255.0):
    """Converts a matrix into a color channel array

    :param matrix: matrix to convert
    :param hue_from: tuple of hue-values, starting color for null
                     strength. Default three with first channel
                    1.0 and the rest 0.
    :param hue_to: tuple with ending hue-values, for max strength.
                   Coloring will be interpolated between hue_from
                   and hue_to.
    :param channel_max: Maximum level of channel, default 255.0
    :return: array with first dimension being number of channels,
             providing one matrix per channel over the other
             dimensions
    """
    rgb_matrix = zeros((len(hue_to), matrix.shape[0], matrix.shape[1]))

    # Find max, min and span
    element_min = matrix.min()
    element_max = matrix.max()
    element_range = element_max - element_min

    print("Minimum = {}, Maximum = {}, span = {}".format(element_min, element_max, element_range))

    # Normalize matrix between 0.0 and 1.0
    normed_matrix = divide(subtract(matrix, element_min), element_range)

    # Calculate rgb channels using specified "hue", rounding off
    for i in range(len(hue_to)):
        rgb_matrix[i,:,:] = around(multiply(add(multiply(normed_matrix, hue_to[i] - hue_from[i]), multiply(ones((matrix.shape[0], matrix.shape[1])), hue_from[i])), channel_max))
        #rgb_matrix[i,:,:] = around(multiply(normed_matrix, hue[i] * channel_max))

    return rgb_matrix

max_stars_repo_name: ropil/allostery-wordom

max_issues_repo_name: ropil/allostery-wordom

max_forks_repo_name: ropil/allostery-wordom

reason: from numpy