text:
import numpy as np
import pandas as pd
from io import BytesIO

import param
import geopandas
import holoviews as hv
import geoviews as gv
import panel as pn
import pygridgen as pgg

from holoviews.plotting.bokeh.callbacks import Link, LinkCallback

class PolaritySwap(Link):
    """
    Link to be applied to a Points object with a polarity column to
    allow toggling of the polarity (beta) value client-side.
    """
    _requires_target = False

class PolarityCallback(LinkCallback):
    """
    Client side JavaScript to toggle the node polarity (beta) value of
    nodes tapped using the Tap tool
    """
    source_model = 'selected'
    on_source_changes = ['indices']
    source_handles = ['cds', 'draw_tool']
    source_code = """
    if (source_draw_tool.active) { return }
    var index = source_selected.indices[0]
    if (index == undefined)
      return
    var polarity = source_cds.get_column('color')
    if (polarity[index] == '+')
      polarity[index] = '0'
    else if (polarity[index] == '0')
      polarity[index] = '-'
    else
      polarity[index] = '+'
    source_cds.data['color'] = polarity
    source_cds.data['polarity'] = polarity
    source_selected.indices = []
    source_cds.properties.data.change.emit()
    source_cds.change.emit()
    """

# Registering Link callback
callbacks = Link._callbacks['bokeh']
callbacks[PolaritySwap] = PolarityCallback

# Dictionary of possible tile sources to make available via the background dropdown menu
TILE_SOURCES = {k:v.opts(global_extent=True)
                for k,v in gv.tile_sources.tile_sources.items()}
TILE_SOURCES['None'] = None

class GridEditor(param.Parameterized):
    """
    Interactive boundary editor for previewing and generating pygridgen grids.

    Core features:
       * Add, drag and delete nodes interactively with the PointsDraw tool
       * Toggle their polarity (beta) value with the Tap Tool
       * Insert nodes into selected edges after selecting them with the Tap Tool.
       * Set a focus function and update the grid
       * Pythonic access to the geopandas boundary DataFrame.
       * Serializable state to capture editor state between sessions
       * Customizable background tile sources or background tile elements.

    For more information please visit https://github.com/pygridgen/hologridgen
    """

    # Algorithmic parameters hidden from the GUI

    max_nodes = param.Integer(default=1000, precedence=-1,
                              doc = "Maximum number of nodes in a boundary")

    ul_idx=param.Integer(default=0, precedence=-1,
                         doc='Upper left index: parameter of grid generation')

    polarity_value = param.Dict(default={'+':1, '0':0, '-':-1}, precedence=-1, doc="""
      Beta values to associate with the three node polarities (positive
      '+', neutral '0' and negative '-'). Setting '+':4/3 for instance
      enables grid generation with only three positive nodes.""")

    # Parameters not in the GUI for Pythonic data access

    focus = param.ClassSelector(default=None, class_=pgg.Focus,
                                allow_None=True, precedence=-1, doc="""
      Parameter that can be set to a pygridgen Focus object (or None).
      When set, mesh generation will apply the specified Focus
      function.""")

    grid = param.ClassSelector(default=None, class_=pgg.grid.Gridgen,
                               allow_None=True, precedence=-1, doc="""
      Parameter that exposes the pygridgen Gridgen object that will be
      set after mesh generation """)

    ready = param.Boolean(default=False, precedence=-1, doc="""
      Boolean predicate indicating readiness for mesh generation: mesh generation
      can be executed when the sum of the polarity in the boundary is 4.""")

    # User settings hidden from the GUI, settable in the constructor (precedence= -1)

    width = param.Integer(default=600, precedence=-1, bounds=(200, 1000),
        doc="Width of the HoloViews object corresponding to the editor view area")

    height = param.Integer(default=600, precedence=-1, bounds=(200, 1000),
        doc="Height of the HoloViews object corresponding to the editor view area")

    custom_background = param.Parameter(default=None, precedence=-1, doc="""
      Custom HoloViews element to use as the background when the
      background parameter is set to 'Custom'.""")

    background = param.ObjectSelector('None', objects=TILE_SOURCES.keys(), doc="""
      Selector of available default tile sources which can also be set
      to 'None' for no background or 'Custom' in which case the
      HoloViews/GeoViews element set in the custom_background parameter
      (if any) is used as the background.""")

    # Customizable HoloViews styles (hidden from the GUI, settable in the constructor)

    node_style = param.Dict(dict(cmap={'+': 'red', '-': 'blue', '0':'black'}),
                            precedence=-1, doc="""
      Style options for nodes. Note that the size is overidden by the
      node_size param controllable in the GUI and the polarity colors
      can be changed by setting the cmap dictionary.""")

    mesh_style = param.Dict(dict(line_width=2, line_alpha=1, line_color='blue'),
                            precedence=-1, doc="Style options for displayed mesh.")

    edge_style = param.Dict(dict(line_width=2, line_alpha=1, line_color='green',
                                nonselection_color='green', nonselection_alpha=0.5),
                            precedence=-1, doc="""
      Style options for displayed boundary edges. The nonselection_*
      options set how deselected edges appear when the Tap tool is used
      to insert new nodes into edges""")

    start_indicator_style = param.Dict(
        dict(marker='triangle', angle=30, fill_alpha=0, size=30, color='black'),
        precedence=-1, doc="""
      Style of the start marker indicating the first boundary
      point. Default is a triangle that can be rotated by setting the
      'angle' keyword.""")

    # GUI controllable parameters

    node_size = param.Integer(default=10, bounds=(1,200),
                              doc="Size of nodes used to mark the boundary.")

    edge_width = param.Integer(default=2, bounds=(1,50), doc="Width of the boundary edges")

    xres = param.Integer(default=50,  bounds=(2, None), doc="""
       X resolution of the generated grid""")

    yres = param.Integer(default=50,  bounds=(2, None), doc="""
       Y resolution of the generated grid""")

    generate_mesh = param.Event(doc='Event that runs mesh generation')

    hide_mesh = param.Event(doc='Event that clears displayed mesh')

    insert_points = param.Event(
        doc='Event that inserts a new node into an edge selected with the Tap tool')

    _columns = ['color', 'polarity', 'x', 'y']

    def __init__(self, data=None, **params):
        data_params = {} if data is None else {k:v for k,v in data.items()
                                               if k not in self._columns}
        params = dict(data_params, **params)
        data = {k:[] for k in self._columns} if (data is None) else data
        super().__init__(**params)

        def install_handle(plot, element):
            "Handle needed to make the draw_tool available in the JS callback"
            plot.handles['draw_tool'] = plot.state.tools[-1]

        node_style = dict(self.node_style,
                           tools=['tap'],
                           color=hv.dim('polarity'),
                           fill_alpha=hv.dim('polarity').categorize({'0':0, '+':1, '-':1 }),
                           show_legend=False, hooks=[install_handle])

        # PointDraw Stream that enables the PointDraw Bokeh tool
        self._node_stream = hv.streams.PointDraw(data=data,
                                                 num_objects=self.max_nodes,
                                                 empty_value = '+')

        # Nodes is a DynamicMap returning hv.Points along the boundary
        self.nodes = hv.DynamicMap(self.points,
                                  streams=[self._node_stream,
                                           self.param.insert_points,
                                           self.param.node_size]).opts(**node_style)
        # DynamicMap drawing the boundary as a hv.Path element
        self.boundary_dmap = hv.DynamicMap(self._boundary,
                                         streams=[self._node_stream,
                                                  hv.streams.Selection1D()])
        # DynamicMap placing the start indicator
        self.start_marker = hv.DynamicMap(self._start_marker,
                                          streams=[self._node_stream]
                                          ).opts(**self.start_indicator_style)

        # Initial, empty mesh
        self.qmesh = hv.QuadMesh((np.zeros((2,2)), np.zeros((2,2)), np.zeros((2,2))))

        self._selected_edge_index = None

    @classmethod
    def from_geopandas(cls, df):
        """
        Classmethod that allows a GridEditor to be initialized from a
        boundary geopandas DataFrame (such as the one available from the
        .boundary property).
        """
        if len(df) == 0:
            return GridEditor()
        allowed = [el for el in cls._columns if el != 'geometry']
        color_vals = {v:k for k,v in cls.polarity_value.items()}
        data = {k:list(v) for k,v in df.to_dict(orient='list').items() if k in allowed}
        data['color'] = [color_vals[p] for p in data['polarity']]
        data['polarity'] = data['color']
        return GridEditor(data)

    @property
    def boundary(self):
        "Property returning the boundary GeoDataFrame"
        exclude = ['color', 'fill_alpha']
        data = self._node_stream.data
        polarity = [self.polarity_value[c] for c in data['color']]
        df_data = {c:[el for el in v] for c,v in data.items() if c not in exclude}
        df_data['polarity'] = polarity
        df = pd.DataFrame(df_data)
        return geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))

    @property
    def data(self, exclude=['name', 'fill_alpha', 'grid']):
        """
        Propery exposing serializable state of the editor that can be
        passed into the GridEditor constructor to restore that state
        """
        data = {k:[el for el in v] for k,v in self._node_stream.data.items()}
        param_data = {p:getattr(self, p) for p in self.param}
        data.update(param_data)
        return {k:v for k,v in data.items() if k not in exclude}

    def _ready(self):
        "Predicate method indicating current readiness for mesh generation"
        data = self._node_stream.data

        if len(data['x']) > 3:
            summed = sum(self.polarity_value[el] for el in data['color'])
            return (summed == 4)
        else:
            return False

    @pn.depends('ready', watch=True)
    def _check_readiness(self):
        "Callback used to disable generate mesh button till ready"
        for widget in self.widgets:
            if isinstance(widget, pn.widgets.button.Button) and (widget.name == 'Generate mesh'):
                button = widget
                break
        button.disabled = not self.ready

    @pn.depends('_node_stream.data')
    def _geojson(self):
        "Callback to generate GeoJSON file when download button is clicked"
        boundary = self.boundary
        bio = BytesIO()
        if len(boundary) != 0:
            boundary.to_file(bio, driver='GeoJSON')
        bio.seek(0)
        return bio

    # DynamicMap callbacks

    def points(self, data, insert_points, node_size):
        "DynamicMap callback returns Points representing boundary nodes"
        new_data = {'x': data['x'], 'y': data['y'],
                    'polarity' : np.array(data['color'], dtype='U1')}
        if insert_points and len(self._selected_edge_index)==1:
            point_index = self._selected_edge_index[0] + 1
            sx, ex = new_data['x'][point_index-1], new_data['x'][point_index]
            sy, ey = new_data['y'][point_index-1], new_data['y'][point_index]

            new_data['x'] = np.insert(new_data['x'], point_index, (sx+ex) / 2.)
            new_data['y'] = np.insert(new_data['y'], point_index, (sy+ey) / 2.)
            new_data['polarity'] = np.insert(new_data['polarity'], point_index, '+')
        return hv.Points(new_data, vdims=['polarity']).opts(size=node_size)

    def _generate_mesh(self, generate_mesh=False, hide_mesh=False):
        "Callback returning generated QuadMesh element"
        if not self.ready:
            return self.qmesh.opts(fill_alpha=0, line_alpha=0)

        elif hide_mesh or (not generate_mesh):
            return self.qmesh.opts(fill_alpha=0, line_alpha=0)

        if self.ready:
            gdf = self.boundary
            kwargs = dict(shape=(self.xres, self.yres), ul_idx=self.ul_idx)
            if self.focus is not None:
                kwargs['focus'] = self.focus
            self.grid = pgg.Gridgen(gdf.geometry.x, gdf.geometry.y, gdf.polarity, **kwargs)
            xdim, ydim = self.grid.x.shape
            zs = np.ones((xdim-1, ydim-1))
            self.qmesh = hv.QuadMesh((np.array(self.grid.x), np.array(self.grid.y), zs))
            return self.qmesh.opts(**self.mesh_style, fill_alpha=0)

    def _boundary(self, data,  index):
        "Callback drawing Path element defining boundary"
        self._selected_edge_index = index
        xs, ys = data['x'], data['y']
        lines = []
        for i in range(len(xs)-1):
            s, e = i, (i+1)
            lines.append([(xs[s], ys[s]), (xs[e], ys[e])])
        self.ready = self._ready()
        return hv.Path(lines).opts(**self.edge_style)

    def _start_marker(self, data):
        "Callback to draw the start marker"
        if len(data['x']) == 0:
            return hv.Points(None)
        return hv.Points((data['x'][0], data['y'][0]))

    def _background(self, background):
        """
        Callback that allows the background to be switched between tile
        sources, a custom background element or None for no
        background
        """
        elements = []
        if background != 'None':
            elements = [TILE_SOURCES[background].opts(global_extent=True, alpha=1)]
        elif background == 'None':
            if self.custom_background:
                elements = [self.custom_background]
            else:
                elements = [TILE_SOURCES[list(TILE_SOURCES.keys())[0]].opts(alpha=0)]
        return hv.Overlay(elements)

    def view(self):
        "Main entry point for using the GridEditor after construction"
        self.polarity_link =  PolaritySwap(self.nodes)
        param_stream = hv.streams.Params(self,
                                         ['generate_mesh', 'hide_mesh'],
                                         transient=True)

        elements = [hv.DynamicMap(self._background, streams=[self.param.background]),
                    self.boundary_dmap.apply.opts(line_width=self.param.edge_width),
                    self.start_marker,
                    self.nodes,
                    hv.DynamicMap(self._generate_mesh,
                    streams=[param_stream])]

        hvobj = hv.Overlay(elements).collate()
        self.widgets = pn.Param(self.param,
                                widgets={'edge_select_mode': pn.widgets.Toggle})
        obj = pn.Row(pn.Column(self.widgets,
                               pn.widgets.FileDownload(callback=self._geojson,
                                                       filename='boundary.geojson')),
                     hvobj.opts(width=self.width, height=self.height))
        self.param.trigger('ready')
        return obj

max_stars_repo_name: pygridgen/hologridgen

max_issues_repo_name: pygridgen/hologridgen

max_forks_repo_name: pygridgen/holo-gridgen


text:
#Conor O'Sullivan
#26 March 2019
#Functions used in data cleaning and fitting process

#imports
import numpy as np

import json
from IPython.core.display import display, HTML
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score

import nltk
from nltk import sent_tokenize
from nltk.tokenize import word_tokenize
import html2text
import ast

#Variables
LIST = ["2","3","5","6","7","8","9","10","11","13","14",
        "18","19","34","35","37","41","46",
        'P1', 'P4', 'P12', 'P7']

class clean:
    #Functions used in cleaning data process

    def getCase(self,ID, path ="/Users/conorosully/Documents/Legal-Case-Prediction/data/preclean/casetext"):
        """
        Returns a case in json format from specified path.
        Required:
        ID: String
        path: Stirng
        """

        path = "{}/{}.json".format(path,ID)
        with open(path) as read_json:
            case = json.load(read_json)
            read_json.close()

        return case

    def renderHTML(self,ID, path ="/Users/conorosully/Documents/Legal-Case-Prediction/data/preclean/casetext"):
        """
        Display case in HTML format
        ID: id of case
        path: path of case
        """

        case = self.getCase(ID)
        case_text = case[ID]

        print("CASE: {}".format(ID))
        print("="*32)
        print()

        return display(HTML(case_text))

    def cleanText(self,text):
        """
        Returns a piece of text after:
        - removed punction
        - made to lower case
        - (stopwords removed when vectorized)
        """
        h = html2text.HTML2Text()
        text_raw = h.handle(text)

        tokens = word_tokenize(text_raw)

        #tokens = [word for word in tokens if word.isalnum()] #remove punctuation
        tokens = [word for word in tokens if word.isalpha()]
        tokens = [w.lower() for w in tokens] #Lower case

        text_clean = " ".join(tokens)

        return text_clean

    #cleans case with a specific form
    def cleanCase(self,case,form):
        """Returns cleaned cases sections"""

        #Procedure
        split_text = case.split(form[0])[1]
        split_text = split_text.split(form[1])
        procedure = self.cleanText(split_text[0])
        split_text = split_text[1]

        #Circumstances
        split_text = split_text.split(form[2])[1]
        split_text = split_text.split(form[3])
        circumstances = self.cleanText(split_text[0])
        split_text = split_text[1]

        #Relevant law
        split_text = split_text.split(form[4])
        relevant = self.cleanText(split_text[0])
        split_text = split_text[1]

        #The facts
        facts = circumstances + relevant

        #The law + other
        split_text = split_text.split(form[5])
        law = self.cleanText(split_text[0])
        other = self.cleanText(split_text[1])

        #full
        full = procedure + circumstances + relevant

        structure = {
        'full':full,
        "procedure":procedure,
        "facts":facts,
        "circumstances":circumstances,
        "relevant":relevant,
        "law":law,
        "other":other}

        return structure

    #Get list of unique articles
    def getUniqueArticles(self,articles):
        """Get list of unique Articles from list of complaints"""

        if type(articles) == float:
            return {}

        articles = ast.literal_eval(articles)
        unique = set() #List of unique articles

        for a in articles:
            sub_articles = a.split("+")
            for s in sub_articles:
                article = s.split("-")[0]
                unique.add(article)

        return(unique)

    # Get Article: 1 if article present, 0 otherwise
    def articleVector(self,articles,violations,nonviolations):
        """Returns vector based on list
        0: nonviolation
        1: violation
        2: other
        -1: no complaint"""

        articles = self.getUniqueArticles(articles)
        violations = self.getUniqueArticles(violations)
        nonviolations = self.getUniqueArticles(nonviolations)

        vector = []
        for l in LIST:
            if l in violations:
                vector.append(1)
            elif l in nonviolations:
                vector.append(0)
            elif l in articles:
                vector.append(2)
            else:
                vector.append(-1)

        return vector

    #check to see if form matches any existing forms
    def getForm(self,case,forms_dict):

        form = ['none', 'none', 'none', 'none', 'none', 'none']
        keys = ['procedure', 'facts', 'circumstances', 'relevant', 'law', 'other']

        #procedure#facts#circumstances#relevant#law#other

        for i,k in enumerate(keys):
            for x in forms_dict[k]:
                #x = " ".join(x.split())
                if x in case:
                    form[i] = x

        return form

    def checkForm(self,form):
        for f in form:
            if f == 'none':
                return False
        return True

    #find the structure of a case
    def printHeadings(self,case,size):

        case_split = case.split("</{}>".format(size))
        for c in case_split:
            print('<{}>'.format(size),end="")
            try:
                heading = c.split("<{}>".format(size))[1]
                print(heading + '</{}>#'.format(size))
            except:
                print('')

    def findHeadings(self,case,size):

        headings = []
        case_split = case.split("</{}>".format(size))
        for c in case_split:
            try:
                s = '<{}>'.format(size) + c.split("<{}>".format(size))[1] + '</{}>'.format(size)
                headings.append(s)
            except:
                x = 1
        return headings

    def __init__(self):
        self.data = []

class classification:

    #Functions used to help classfication model fitting process

    def results(self, y_test, predictions):
        """
        Returns classfification prediction results

        """
        acc = accuracy_score(y_test,predictions)
        cm = confusion_matrix(y_test,predictions)
        cr = classification_report(y_test,predictions)

        print(acc)
        print(cm)
        print(cr)

        return (acc,cm,cr)

    def __init__(self):
        self.data = []

ext: py

max_stars_repo_name: conorosully/legal-case-prediction

max_issues_repo_name: conorosully/legal-case-prediction

max_forks_repo_name: conorosully/legal-case-prediction

reason: import numpy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: dataReader.py
# Author: Amir Alansary <amiralansary@gmail.com>

import warnings
warnings.simplefilter("ignore", category=ResourceWarning)

import numpy as np
import SimpleITK as sitk
from tensorpack import logger
from IPython.core.debugger import set_trace

__all__ = ['filesListBrainMRLandmark','NiftiImage']

######################################################################
## extract points from txt file
def getLandmarksFromTXTFile(file):
    '''
    Extract each landmark point line by line and return vector continaing all landmarks.
    '''
    with open(file) as fp:
        landmarks = []
        for i, line in enumerate(fp):
            landmarks.append([float(k) for k in line.split(',')])
        landmarks = np.asarray(landmarks).reshape((-1,3))
        return landmarks

###############################################################################

class filesListBrainMRLandmark(object):
    """ A class for managing train files for mri cardio data

        Attributes:
        files_list: Two or on textfiles that contain a list of all images and (landmarks)
        returnLandmarks: Return landmarks if task is train or eval (default: True)
    """
    def __init__(self, files_list=None, returnLandmarks=True):
        # check if files_list exists
        assert files_list, 'There is no directory containing files list'
        # read image filenames
        self.image_files = [line.split('\n')[0] for line in open(files_list[0].name)]
        # read landmark filenames if task is train or eval
        self.returnLandmarks = returnLandmarks
        if self.returnLandmarks:
            self.landmark_files = [line.split('\n')[0] for line in open(files_list[1].name)]
            assert len(self.image_files)== len(self.landmark_files), 'number of image files is not equal to number of landmark files'

    @property
    def num_files(self):
        return len(self.image_files)

    def sample_circular(self,shuffle=False):
        """ return a random sampled ImageRecord from the list of files
        """
        if shuffle:
            indexes = rng.choice(x,len(x),replace=False)
        else:
            indexes = np.arange(self.num_files)

        while True:
            for idx in indexes:
                sitk_image, image = NiftiImage().decode(self.image_files[idx])
                if self.returnLandmarks:
                    ## transform landmarks to image space if they are in physical space
                    landmark_file = self.landmark_files[idx]
                    all_landmarks = getLandmarksFromTXTFile(landmark_file)
                    landmark = all_landmarks[14] # landmark index is 13 for ac-point and 14 pc-point
                    # transform landmark from physical to image space if required
                    # landmark = sitk_image.TransformPhysicalPointToContinuousIndex(landmark)
                    landmark = np.round(landmark).astype('int')
                else:
                    landmark = None
                # extract filename from path
                image_filename = self.image_files[idx][:-7]
                yield image, landmark, image_filename, sitk_image.GetSpacing()

###############################################################################

class ImageRecord(object):
  '''image object to contain height,width, depth and name '''
  pass

class NiftiImage(object):
    """Helper class that provides TensorFlow image coding utilities."""
    def __init__(self):
        pass

    def _is_nifti(self,filename):
        """Determine if a file contains a nifti format image.
        Args
          filename: string, path of the image file
        Returns
          boolean indicating if the image is a nifti
        """
        extensions = ['.nii','.nii.gz','.img','.hdr']
        return any(i in filename for i in extensions)

    def decode(self, filename,label=False):
        """ decode a single nifti image
        Args
          filename: string for input images
          label: True if nifti image is label
        Returns
          image: an image container with attributes; name, data, dims
        """
        image = ImageRecord()
        image.name = filename
        assert self._is_nifti(image.name), "unknown image format for %r" % image.name

        if label:
            sitk_image = sitk.ReadImage(image.name, sitk.sitkInt8)
        else:
            sitk_image = sitk.ReadImage(image.name, sitk.sitkFloat32)
            np_image = sitk.GetArrayFromImage(sitk_image)
            # threshold image between p10 and p98 then re-scale [0-255]
            p0 = np_image.min().astype('float')
            p10 = np.percentile(np_image,10)
            p99 = np.percentile(np_image,99)
            p100 = np_image.max().astype('float')
            # logger.info('p0 {} , p5 {} , p10 {} , p90 {} , p98 {} , p100 {}'.format(p0,p5,p10,p90,p98,p100))
            sitk_image = sitk.Threshold(sitk_image,
                                        lower=p10,
                                        upper=p100,
                                        outsideValue=p10)
            sitk_image = sitk.Threshold(sitk_image,
                                        lower=p0,
                                        upper=p99,
                                        outsideValue=p99)
            sitk_image = sitk.RescaleIntensity(sitk_image,
                                               outputMinimum=0,
                                               outputMaximum=255)

        # Convert from [depth, width, height] to [width, height, depth]
        image.data = sitk.GetArrayFromImage(sitk_image).transpose(2,1,0)#.astype('uint8')
        image.dims = np.shape(image.data)

        return sitk_image, image

max_stars_repo_name: benodry/tensorpack-medical

max_issues_repo_name: Helogn/rl-medical

max_forks_repo_name: Helogn/rl-medical


text:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 30 10:27:00 2016

@author: matvii
"""
from math import *
from astropy.io import fits
from utils import *
def read_fits_date(fitsfile):
    fi=fits.open(fitsfile)
    date=0
    head=fi[0]
    try:
        date=head.header['MJD-OBS']
    except:
        date=float('nan')
    fi.close()
    return date

def calculate_rotated_frame(fitsfile,E):
    fi=fits.open(fitsfile)
    NOCD=0
    head=fi[0]
    try:
        CD11=float(head.header['CD1_1'])
    except:
        NOCD=1
    try:
        CD12=float(head.header['CD1_2'])
    except:
        NOCD=1
    try:
        CD21=float(head.header['CD2_1'])
    except:
        NOCD=1
    try:
        CD22=float(head.header['CD2_2'])
    except:
        NOCD=1
    fi.close()
    if NOCD==1:
        upr=np.array([0,0.3977,0.9175])
        angle=0;
        return upr
    sgn=np.sign(CD11*CD22-CD12*CD21)
    if sgn>0:
        print('CD sign is positive')
    angle1=atan2(sgn*CD12,CD22)
    angle2=atan2(-CD21,sgn*CD11)
    up=np.array([0,0.3977,0.9175])
    if(abs(angle1-angle2)>0.005):
        print('Cannot understand rotation angle in  '+fitsfile)
    M=Ortho_Proj(E,up)
    Rz=np.array([[cos(angle1),-sin(angle1),0],[sin(angle1),cos(angle1),0],[0,0,1]])
    Mrot=np.dot(np.dot(M.transpose(),Rz.transpose()),M)
    upr=np.dot(Mrot,up.transpose()).transpose()
    return upr

max_stars_repo_name: matvii/adam-asteroid

max_issues_repo_name: matvii/adam-asteroid

max_forks_repo_name: matvii/adam-asteroid

reason: from astropy


text:
"""
Logistic Regression for Classification
"""
import numpy as np

## TODO: Batch iterator for large datasets

class LogisticRegression(object):
  """
  from logistic_regression_np import LogisticRegression
  from utils import generate_classification_style_dataset

  X, Y = generate_classification_style_dataset()

  lr = LogisticRegression()
  lr.fit(X, Y)
  lr.predict(X)

  """
  def __init__(self, n_epochs=10, learning_rate=0.1, learning_rate_decay=0.95, l2_reg=0.05, verbose=True):
    self.n_epochs = n_epochs
    self.learning_rate = learning_rate
    self.learning_rate_decay = learning_rate_decay
    self.l2_reg = l2_reg
    self.verbose = verbose

  def activation_func(self, z):
    """ Sigmoid activation function """
    return 1.0 / (1.0 + np.exp((-1)*z))

  def categorical_cross_entropy(self, X, Y):
    """ Categorical cross-entropy """
    ## Forward pass
    z = self.activation_func(np.dot(X,self.W)+self.B)

    ## Calculate cross entropy
    cross_ent = np.mean(np.sum(Y*np.log(z) + (1-Y)*np.log(1-z),axis=1))
    return cross_ent

  def fit(self, X, Y):
    """
    Train model
    """
    ## Check Y is a numpy array. TODO: Check X is numpy array and reshape
    if isinstance(Y,list):
      Y = np.array(Y)

    ## Obtain matrix dimensions
    self.n_classes = Y.shape[1]
    self.n_rows, self.n_cols = X.shape

    ## Randomly initialize weights and bias terms
    self.W = np.random.rand(self.n_rows,self.n_classes)
    self.B = np.random.rand(self.n_classes)

    ## Loop through epochs and update parameters
    for epoch in range(self.n_epochs):
      ## Forward pass
      probs = self.activation_func(np.dot(X,self.W)+self.B)
      print(probs)
      gradients = Y - probs

      ## Weight and bias update
      self.W += self.learning_rate*np.dot(X.T, gradients) - self.learning_rate*self.l2_reg*self.W
      self.B += self.learning_rate*np.mean(gradients,axis=0)

      ## Update learning rate
      self.learning_rate *= self.learning_rate_decay

      ## Display
      if self.verbose:
        loss = self.categorical_cross_entropy(X,Y)
        print(f"Epoch: {epoch} Loss: {loss}")

  def predict(self, X):
    """
    Predict using trained model
    """
    ## Forward pass
    probs = self.activation_func(np.dot(X,self.W)+self.B)

    ## Argmax on probabilities
    np.putmask(probs, probs>=0.5,1.0)
    np.putmask(probs, probs<0.5,0.0)
    return probs


text:
# Copyright (c) Microsoft. All rights reserved.

# Licensed under the MIT license. See LICENSE.md file in the project root
# for full license information.
# ==============================================================================

"""
Unit tests for the function class.
"""

import numpy as np
import pytest
from ..functions import *
from ...trainer import *
from .. import constant, parameter, input_variable, placeholder_variable, times, plus
from .ops_test_utils import compare_lists_of_np_arrays

def test_variable_forwarding():
    op = constant(value=2, shape=(3,4)) + 1
    assert op.shape == (3,4)

def test_replace_placeholders():
    p = placeholder_variable(shape=(1,))
    i = input_variable(shape=(1,),
                       needs_gradient=True,
                       name='i')
    res = p + 3
    res.replace_placeholders({p: i})

    assert res.eval({i: [[3]]}) == [6]

    if False:
        res2 = p + 2
        from .. import plus
        func = plus(res2, 10)
        res2.replace_placeholders({p: func.output})

        assert res2.eval({i: [3]}) == [15]

def test_cloning():
    p = placeholder_variable(shape=(1,), name='p')
    i = input_variable(shape=(1,),
                       needs_gradient=True,
                       name='i')
    res = p + i

    with pytest.raises(ValueError):
        res.clone(2)

    from ..functions import CloneMethod

    # Test freeze
    cloned = res.clone(CloneMethod.freeze)
    assert cloned.inputs[0].name == 'p'
    assert cloned.inputs[0].uid != p.uid
    assert cloned.inputs[1].name == 'i'
    assert cloned.inputs[1].uid != i.uid

    cloned = res.clone('freeze')
    assert cloned.inputs[0].name == 'p'
    assert cloned.inputs[0].uid != p.uid
    assert cloned.inputs[1].name == 'i'
    assert cloned.inputs[1].uid != i.uid

def test_replace_placeholder_s():
    left_val = [[10,2]]
    right_val = [[2],[3]]

    p = placeholder_variable(shape=(1,2))
    c = constant(left_val)

    op = times(p, right_val)
    op.replace_placeholders({p:c})
    assert op.eval() == 26

    op = times(p, right_val)
    op.replace_placeholder(c)
    assert op.eval() == 26

def test_exception_for_unnamed_arguments():
    i1 = input_variable((1,2), name='i1')
    i2 = input_variable((2,1), name='i2')
    root_node = plus(i1, i2)
    input1 = [[[1,2]]]
    input2 = [[[[1],[2]]]]

    with pytest.raises(Exception):
        # not allowed, since plus has more than 1 input
        result = root_node.eval([input1, input2])

def test_output_in_intermediate_node():
    x = input_variable((2,))
    y = input_variable((2,))
    x0 = np.asarray([[2., 1.]], np.float32)
    y0 = np.asarray([[4., 6.]], np.float32)

    sum_node = x + 2
    times_node = sum_node * y

    sum_output = times_node.forward({x: x0, y: y0}, sum_node.outputs)

    assert len(sum_output[1]) == 1
    assert np.allclose(list(sum_output[1].values())[0], np.asarray(x0 + 2))

    two_nodes_output = times_node.forward({x: x0, y: y0}, times_node.outputs + sum_node.outputs)

    assert len(two_nodes_output[1]) == 2

    sum_forward = np.asarray(x0 + 2)
    expected_results = [sum_forward, sum_forward * y0]

    assert compare_lists_of_np_arrays(list(two_nodes_output[1].values()), expected_results)

def test_getting_output_from_non_existent_node():
    x = input_variable((2,))
    y = input_variable((2,))
    x0 = np.asarray([[2., 1.]])
    y0 = np.asarray([[4., 6.]])

    sum_node = x + 2

    # times_node not having sum_node
    times_node = x * y

    with pytest.raises(ValueError):
        sum_output = times_node.forward({x: x0, y: y0}, sum_node.outputs)


text:
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import numpy as np

'''
For members who did not yet install the module "matplotlib",
    python3 -mpip install -U matplotlib
installation of tkinter is a prerequisite. If you do not have it,
    sudo apt install python3.6-tk
Now you won't have problems running this python file.
'''

def plotloss(csvfile):
    '''
    Args
        csvfile: name of the csv file
    Returns
        graph_loss: trend of loss values over epoch
    '''

    # Bring in the csv file
    loss_values = pd.read_csv(csvfile)

    # Initiation
    epoch = loss_values.iloc[:, 0]
    tr_loss = loss_values.iloc[:, 1]
    tr_acc = loss_values.iloc[:, 2]
    val_loss = np.asarray(loss_values.iloc[:, 3])
    val_acc = np.asarray(loss_values.iloc[:, 4])

    # Reduce the volume of data
    epoch_skip = epoch[::5]
    tr_loss_skip = tr_loss[::5]
    tr_acc_skip = tr_acc[::5]
    val_loss_skip = val_loss[::5]
    val_acc_skip = val_acc[::5]

    fig, ax1 = plt.subplots(figsize=(8, 6))
    ax2 = ax1.twinx()

    # Label and color the axes
    ax1.set_xlabel('Epoch', fontsize=16)
    ax1.set_ylabel('Loss', fontsize=16, color='black')
    ax2.set_ylabel('Accuracy', fontsize=16, color='black')

    # Plot valid/train losses
    ax1.plot(epoch_skip, tr_loss_skip, linewidth=2,
             ls='--', color='#c92508', label='Train loss')
    ax1.plot(epoch_skip, val_loss_skip, linewidth=2,
             color='#c92508', label='Validation loss')
    ax1.spines['left'].set_color('#f23d1d')
    # Coloring the ticks
    for label in ax1.get_yticklabels():
        label.set_color('#c92508')
        label.set_size(12)

    # Plot valid/trian accuracy
    ax2.plot(epoch_skip, tr_acc_skip, linewidth=2, ls='--',
             color='#2348ff', label='Train Accuracy')
    ax2.plot(epoch_skip, val_acc_skip, linewidth=2,
             color='#2348ff', label='Validation Accuracy')
    ax2.spines['right'].set_color('#2348ff')
    # Coloring the ticks
    for label in ax2.get_yticklabels():
        label.set_color('#2348ff')
        label.set_size(12)

    # Manually setting the y-axis ticks
    yticks = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
    ax1.set_yticks(yticks)
    ax2.set_yticks(yticks)

    for label in ax1.get_xticklabels():
        label.set_size(12)

    # Modification of the overall graph
    fig.legend(ncol=4, loc=9, fontsize=12)
    plt.xlim(xmin=0)
    ax2.set_ylim(ymax=1, ymin=0)
    ax1.set_ylim(ymax=1, ymin=0)
    plt.xlabel('epochs')
    plt.title("Adam optimizer", weight="bold")
    plt.grid(True, axis='y')

    # return train_loss, valid_loss

if __name__ == '__main__':
    file = '../history/csv/Adam.csv'
    #file = '../history/SGD/history_SGD4.csv'
    plt.show(plotloss(file))

max_stars_repo_name: sangkny/pytorch-unet-segmentation

max_issues_repo_name: sangkny/pytorch-unet-segmentation

max_forks_repo_name: sangkny/pytorch-unet-segmentation


text:
import numpy as np
import cupy as cp

def _fast_expand(complex_array, factor, num_per_block=200):
    shp = complex_array.shape
    times = shp[0]
    expanded_array = np.zeros((shp[0], shp[1], shp[2] * factor))
    for k in range(0, times, num_per_block):
        the_max = min([k + num_per_block, times])
        weights = cp.tile(np.arange(0, factor) / factor, (the_max - k, shp[1], 1))
        for l in range(shp[2]):
            gpu_array = cp.array(complex_array[k:the_max, :, l])
            if l < shp[2] - 1:
                gpu_array2 = cp.array(complex_array[k:the_max, :, l + 1])
                diff_array = gpu_array2 - gpu_array
            else:
                diff_array = cp.zeros((the_max - k, shp[1]))

            rep_array = cp.transpose(
                cp.tile(gpu_array, (factor, 1, 1)), [1, 2, 0])
            diff_array = cp.transpose(
                cp.tile(diff_array, (factor, 1, 1)), [1, 2, 0])
            temp_array = diff_array * weights + rep_array
            expanded_array[k:the_max, :, factor * l:factor * (l + 1)] = temp_array.get()
    return expanded_array

def _gpu_moving_average(arr, window=8, num_per_block=200):
    shp = arr.shape
    times = shp[0]
    for k in range(0, times, num_per_block):
        the_max = min([k + num_per_block, times])
        gpu_arr = cp.array(arr[k:the_max, :, :])
        gpu_arr2 = cp.zeros_like(gpu_arr)
        for i in range(shp[2]):
            the_min2 = max([0, i - int(window / 2)])
            the_max2 = min([i + int(window / 2), shp[2]])
            gpu_arr2[:, :, i] = cp.mean(gpu_arr[:, :, the_min2:the_max2], axis=2)
        arr[k:the_max, ::] = gpu_arr2.get()
        del gpu_arr, gpu_arr2
    return arr

reason: import numpy,import cupy


text:
# coding: utf-8
# Library that depends on the pyLab library and that contains the main program/functions
# for driving the LEC devices at diferent modes.

import numpy as np
from time import sleep, time
import pyLab # This the module I created
import datetime
import os
import global_settings_ivl as gsivl
today = datetime.date.today().strftime("%Y%m%d")

def run_fix_current(current, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource = 'GPIB0::24::INSTR', term = 'FRONT',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False):
    """Here goes the documentation of the function
    Mandatory arguments:
        - current: current in mA
    Optional arguments:
        - rtime: running time of the experiment, infinit by default
        - dt: time inverval between measueements
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource: resource name for the Keithley2400, default is 'GPIB0::24::INSTR'
        - term: either FRONT (default) or REAR
        - other, to be filled

    """

    # First, we create an instance of a ktly24XX instrument class with the resource
    # passed as an argument
    ky2400 = pyLab.ktly20XX(resource)
    ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount=Ncount, aver = aver)

    # Initializing some other values and figure
    get_ipython().run_line_magic('matplotlib', 'notebook')
    figure = pyLab.figure(ylabel = 'Voltage (V)', xlabel= 'Time (s)', title = 'Voltage-Time plot')
    ptime = []
    pvolt = []

    Nsteps = 2000

    i = 0

    # Opening the file to save the data
    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    filename = os.path.join(folder,today + '_' + fname + '.txt')

    if not os.path.isfile(filename):
        f=open(filename,'a')
        f.write(('# {:^5}\t'+3*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage'))
        f.write(('# {:^5}\t'+3*'{:^12}\t' + '\n').format('i','s','mA','V'))
    else:
        f=open(filename,'a')
        f.write('# {}\n'.format(timestamp))

    ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.0)
    ky2400.outpon()
    ttime = 0.0
    start = True
    stime = time()
    while ttime < rtime:
        try:

            [voltage, current, _ , ttime, _ ] = ky2400.read()
            if not start:
                ptime.append(ttime - itime)
            else:
                itime =  ttime
                ptime.append(0.0)
                start = False
            pvolt.append(voltage)

            # This creates an updating figure (by erasing the axis) It slows down the performance but still enough
            # for this application
            figure.update_lines(ptime, [pvolt])
            figure.update_axes(0)
            figure.update_axes(1)

            f.write(('{:^5d}\t' +3*'{:^12.10f}\t' + '\n').format(i,ttime - itime, current,voltage))

            i += 1
            # Safety line in order not to plot infinit values and charge the memnory
            if i % Nsteps == 0:
                ptime = [ptime[-1]]
                pvolt = [pvolt[-1]]
                i = 0
                figure.ax.set_xlim(ptime[-1],ptime[-1]*1.5)
                figure.ax.set_ylim(pvolt[-1]*0.99,pvolt[-1]*1.01)

            sleep(dt - (time() - stime) % dt )
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print()
            break

    ky2400.outpoff()
    f.close()

def run_fix_voltage(voltage, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'current-time-data',\
                    resource = 'GPIB0::24::INSTR', term = 'FRONT',\
                    fw = False, beeper = True, cmpl = 0.05,\
                    Ncount = 1, aver = False):
    """Here goes the documentation of the function
    Mandatory arguments:
        - voltage: voltage in V
    Optional arguments:
        - rtime: running time of the experiment, infinit by default
        - dt: time inverval between measurements
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource: resource name for the Keithley2400, default is 'GPIB0::24::INSTR'
        - term: either FRONT (default) or REAR
        - other, to be filled

    """

    # First, we create an instance of a ktly24XX instrument class with the resource
    # passed as an argument
    ky2400 = pyLab.ktly20XX(resource)
    ky2400.mode_vfix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount, aver = aver)

    # Initializing some other values and figure
    get_ipython().run_line_magic('matplotlib', 'notebook')
    figure = pyLab.figure(ylabel = 'Current (mA)', xlabel= 'Time (s)', title = 'Current-Time plot')
    ptime = []
    pvalue = []

    Nsteps = 2000

    i = 0

    # Opening the file to save the data
    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    filename = os.path.join(folder,today + '_' + fname + '.txt')

    if not os.path.isfile(filename):
        f=open(filename,'a')
        f.write(('# {:^5}\t'+3*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage'))
        f.write(('# {:^5}\t'+3*'{:^12}\t' + '\n').format('i','s','mA','V'))
    else:
        f=open(filename,'a')
        f.write('# {}\n'.format(timestamp))

    ky2400.mode_vfix_setvolt(voltage)
    ky2400.outpon()
    ttime = 0.0
    start = True
    stime = time()
    itime = 0.0
    while ttime < rtime:
        try:

            [voltage, current, _ , ttime, _ ] = ky2400.read()
            if not start:
                ptime.append(ttime - itime)
            else:
                itime =  ttime
                ptime.append(0.0)
                start = False
            pvalue.append(current * 1000)

            # This creates an updating figure (by erasing the axis) It slows down the performance but still enough
            # for this application
            figure.update_lines(ptime, [pvalue])
            figure.update_axes(0)
            figure.update_axes(1)

            f.write(('{:^5d}\t' +3*'{:^12.10f}\t' + '\n').format(i,ttime - itime, current,voltage))

            i += 1
            # Safety line in order not to plot infinit values and charge the memnory
            if i % Nsteps == 0:
                ptime = [ptime[-1]]
                pvalue = [pvalue[-1]]
                i = 0
                figure.ax.set_xlim(ptime[-1],ptime[-1]*1.5)
                figure.ax.set_ylim(pvalue[-1]*0.99,pvalue[-1]*1.01)

            sleep(dt - (time() - stime) % dt )
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print()
            break

    ky2400.outpoff()
    f.close()

def dt_calc(etime):
    """Returns an interval of time that increased as the ellapsed time etime increases"""
    if etime <= 10:
        return 0.25
    elif etime <= 60:
        return 0.5
    elif etime <= 120:
        return 1.0
    elif etime <= 300:
        return 5.0
    elif etime <= 3600:
        return 10.0
    else:
        return 60.0

def ivl_setup(current, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource1 = 'GPIB0::24::INSTR', term = 'FRONT',\
                    resource2 = 'GPIB0::23::INSTR',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False, nplc = 1.0,
                    config = True, interrupt_measurement = False,
                    dt_fix = True):
    """Here goes the documentation of the function
    Mandatory arguments:
        - current: current in mA
    Optional arguments:
        - rtime: running time of the experiment, infinit by default
        - dt: time inverval between measueements.
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource1: resource name for the sourcemeter Keithley24XX, default is 'GPIB0::24::INSTR'
        - resource2: resource name for the multimeter, default is 'GPIB0::23::INSTR'
        - term: either FRONT (default) or REAR for the sourmeter
        - config: if True (default) the sourcemeter from resource 1 is assumed that has to be configured, otherwise just reads the values
        - interrupt_measurement: if True, when the program is interrupted, the sourcemeter from resource 1 will stop only measuring, but will continue on ON state. Default is False (interrupts both measurement and sourcemeter)
        - Ncount: number of samples to ask the sourcemeter/multimeter, shared parameter. Default is 1
        - aver: Average the number of samples given for Ncount. Default is False- Shared parameter between  sourcemeter/multimeter.
        - nplc: Number of pulse ligh cycles to measure. Shared between sourcemeter/multimeter and default i 1.
        - fw: Activate the four-wire measurement (for the sourcemeter).
        - beeper: Activate the beeper (for the sourcemeter).
        - cmpl: compliance fot the sourcemeter (in volts since we are asking for current)
        - config: either to config (or not) the sourcemeter and multimeter, by default is True. If set False and it is really not configured, will  just complain and not work.
        - interrupt_measurement: dflt is False, if set to True when the program is interrupted, the measurement will stop but not the sourcemeter, that will continue feeding the device.
        - dt_fix: the time interval is fixed. It set to false, then it uses the time interval from the function dt_calc()
    """

    # First, we create an instance of a ktly24XX instrument class with the resource passed as an argument and later we configure (or not) the resource considering it is a Keithley24XX family
    ky2400 = pyLab.ktly20XX(resource1)
    mult = pyLab.keysight34461A(resource2) # Creating the object for the multimeter
    if config:
        ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount,\
                                   nplc = nplc, aver = aver)

############# MULTIMETER #################################

        mult.config_volt(rang = 5.0, nplc = nplc, count = Ncount)  # Configure the multimeter

############ INDICATORS FOR THE JUPYTER NOTEBOOK ##############################
    readings, ui = pyLab.CreateIndicators(['Voltage', 'Voltage'],[0.0]*2,['LEC', 'PhD'])

############ Initializing some  values and figure ###########################
    get_ipython().run_line_magic('matplotlib', 'notebook')
    figure = pyLab.figure(ndatasets=2,ylabel = 'Voltage (V)', xlabel= 'Time (s)', title = 'Voltage-Time plot')
    ptime = []
    pvolt = []
    pdiode = []

    Nsteps = 2000 # Maxmimum number of values of the array to print

############  LOGGING THE DATA ###################################
    # Opening the file to save the data
    filename = os.path.join(folder,today + '_' + fname + '.txt')
    if not os.path.isfile(filename):
        f = open(filename,'a')
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage', 'Photocurrent_volt(V)', 'Absolute time(s)'))
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('i','s','A','V', 'V', 's'))
    else:
        f = open(filename,'a')
########## PERFORMING FIRST MEASUREMENTS ############################
    # First measruementes without running the LEC
    if config:
        ky2400.mode_ifix_setcurr(0.0 / 1000.0, curr_max=0.1, curr_min= -0.001)
        ky2400.outpon()

    ttime = 0.0
    etime= 0.0 # Ellapsed time
    itime = 0.0
    start = True

    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    f.write('# {}\n'.format(timestamp))
    stime = time()
    zero_current_time = 2.0
    flag_scurr = False # Flag to control if the current has been set or not.
    i = 0 # Step counter
    k = 0

    # Add this function to sequentally update the time interval of the measurement
    if dt_fix:
        sleeping_time = dt
    else:
        sleeping_time = dt_calc(etime)

    while etime < rtime:
        try:
            [voltage, mcurrent, _ , ttime, _ ] = ky2400.read()
            voltage_ph = mult.read().mean()
#            assert not ky2400.check_volt_compliance(), 'Compliance reached!'
            if not start:
                etime = ttime - itime
                ptime.append(etime)
            else:
                itime =  ttime
                ptime.append(0.0)
                start = False
            pvolt.append(voltage)
            pdiode.append(voltage_ph)

            # This creates an updating figure (by erasing the axis) It slows down the performance but still enough
            # for this application
            figure.update_lines(ptime, [pvolt, pdiode])
            figure.update_axes(0)
            figure.update_axes(1)

            f.write(('{:^5d}\t' +5*'{:^12.10f}\t' + '\n').format(k,etime, mcurrent,voltage, voltage_ph,ttime))

            readings[0].value = str(voltage)
            readings[1].value = str(voltage_ph)

            i += 1
            k += 1
            # Safety line in order not to plot infinit values and charge the memnory
            if i % Nsteps == 0:
                ptime = [ptime[-1]]
                pvolt = [pvolt[-1]]
                pdiode = [pdiode[-1]]
                i = 0
                figure.update_axes(0, (ptime[-1],ptime[-1] + 10*sleeping_time))
                figure.update_axes(1, (pvolt[-1]*0.95,pvolt[-1]*1.05))

            if (ttime - itime) >=  zero_current_time and config and not flag_scurr:
                ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.00)
                flag_scurr = True

            if dt_fix:
                sleeping_time = dt
            else:
                sleeping_time = dt_calc(etime)

            sleep(sleeping_time - (time() - stime) % sleeping_time )
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            if interrupt_measurement:
                print('Measurement stopped, but sourcemeter still on\n')
            else:
                ky2400.outpoff()
                f.close()
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print(e)
            ky2400.outpoff()
            f.close()
            break
    if not interrupt_measurement:
        ky2400.outpoff()
        f.close()

def ivl_setup_v2(current, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource1 = 'GPIB0::24::INSTR', term = 'FRONT',\
                    resource2 = 'GPIB0::23::INSTR',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False, nplc = 1.0,
                    config = True, interrupt_measurement = False,
                    dt_fix = True):
    """Standalone version of the ivl_setup, to be runned directly either from the terminal prompt or from a script.
    Mandatory arguments:
        - current: current in mA
    Optional arguments:
        - rtime: running time of the experiment, infinit by default
        - dt: time inverval between measueements.
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource1: resource name for the sourcemeter Keithley24XX, default is 'GPIB0::24::INSTR'
        - resource2: resource name for the multimeter, default is 'GPIB0::23::INSTR'
        - term: either FRONT (default) or REAR for the sourmeter
        - config: if True (default) the sourcemeter from resource 1 is assumed that has to be configured, otherwise just reads the values
        - interrupt_measurement: if True, when the program is interrupted, the sourcemeter from resource 1 will stop only measuring, but will continue on ON state. Default is False (interrupts both measurement and sourcemeter)
        - Ncount: number of samples to ask the sourcemeter/multimeter, shared parameter. Default is 1
        - aver: Average the number of samples given for Ncount. Default is False- Shared parameter between  sourcemeter/multimeter.
        - nplc: Number of pulse ligh cycles to measure. Shared between sourcemeter/multimeter and default i 1.
        - fw: Activate the four-wire measurement (for the sourcemeter).
        - beeper: Activate the beeper (for the sourcemeter).
        - cmpl: compliance fot the sourcemeter (in volts since we are asking for current)
        - config: either to config (or not) the sourcemeter and multimeter, by default is True. If set False and it is really not configured, will  just complain and not work.
        - interrupt_measurement: dflt is False, if set to True when the program is interrupted, the measurement will stop but not the sourcemeter, that will continue feeding the device.
        - dt_fix: the time interval is fixed. It set to false, then it uses the time interval from the function dt_calc()
    """

    # First, we create an instance of a ktly24XX instrument class with the resource passed as an argument and later we configure (or not) the resource considering it is a Keithley24XX family
    ky2400 = pyLab.ktly20XX(resource1)
    mult = pyLab.keysight34461A(resource2) # Creating the object for the multimeter
    if config:
        ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount,\
                                   nplc = nplc, aver = aver)

############# MULTIMETER #################################

        mult.config_volt(rang = 5.0, nplc = nplc, count = Ncount)  # Configure the multimeter

############ Initializing some  values and figure ###########################
    Nsteps = 200 # Maxmimum number of values of the array to print
    ptime = np.linspace(0,Nsteps,Nsteps, False)
    pvolt = np.zeros(len(ptime))
    pdiode = np.zeros(len(ptime))
    lines = None

############  LOGGING THE DATA ###################################
    # Opening the file to save the data
    filename = os.path.join(folder,today + '_' + fname + '.txt')
    if not os.path.isfile(filename):
        f = open(filename,'a')
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage', 'Photocurrent_volt(V)', 'Absolute time(s)'))
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('i','s','A','V', 'V', 's'))
    else:
        f = open(filename,'a')
########## PERFORMING FIRST MEASUREMENTS ############################
    # First measruementes without running the LEC
    if config:
        ky2400.mode_ifix_setcurr(0.0 / 1000.0, curr_max=0.1, curr_min= -0.001)
        ky2400.outpon()

    ttime = 0.0
    etime= 0.0 # Ellapsed time
    itime = 0.0
    start = True

    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    f.write('# {}\n'.format(timestamp))
    stime = time()
    zero_current_time = 2.0
    flag_scurr = False # Flag to control if the current has been set or not.
    flag_ch_cmpl = False # Just to check if the compliance has been change after x seconds
    i = 0 # Step counter
    k = 0

    # Add this function to sequentally update the time interval of the measurement
    if dt_fix:
        sleeping_time = dt
    else:
        sleeping_time = dt_calc(etime)

    while etime < rtime:
        try:
            time1 = time()
            [voltage, mcurrent, _ , ttime, _ ] = ky2400.read()
            voltage_ph = mult.read().mean()
#            assert not ky2400.check_volt_compliance(), 'Compliance reached!'
            if not start:
                etime = ttime - itime
            else:
                itime =  ttime
                start = False

            pvolt[-1] = voltage
            pdiode[-1] = voltage_ph

            f.write(('{:^5d}\t' +5*'{:^12.10f}\t' + '\n').format(k,etime, mcurrent,voltage, voltage_ph,ttime))

            i += 1
            k += 1

            lines = pyLab.live_plotter(ptime,[pvolt, pdiode],lines, title = 'IVL characterisation', ylabel = 'Voltage (V)')
            pvolt = np.append(pvolt[1:],0.0)
            pdiode = np.append(pdiode[1:],0.0)

            if not flag_ch_cmpl:
                if (time() - stime) > 300:
                    ky2400.inst.write(":SENS:VOLT:PROT 21")    # Set 21V compliance limit.
                    flag_ch_cmpl = True

            if (ttime - itime) >=  zero_current_time and config and not flag_scurr:
                ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.00)
                flag_scurr = True

            if dt_fix:
                sleeping_time = dt
            else:
                sleeping_time = dt_calc(etime)

            while (time() - time1) < sleeping_time:
                sleep(0.01)
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            if interrupt_measurement:
                print('Measurement stopped, but sourcemeter still on\n')
            else:
                ky2400.outpoff()
                f.close()
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print(e)
            ky2400.outpoff()
            f.close()
            break
    if not interrupt_measurement:
        ky2400.outpoff()
        f.close()

def ivl_setup_gui(current, window, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource1 = 'GPIB0::24::INSTR', term = 'FRONT',\
                    resource2 = 'GPIB0::23::INSTR',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False, nplc = 1.0,
                    config = True, interrupt_measurement = False,
                    dt_fix = True):
    """Standalone version of the ivl_setup, to be runned directly either from the terminal prompt or from a script.
    Mandatory arguments:
        - current: current in mA
        - window to which we are plotting, as it is the gui version
    Optional arguments:
        - rtime: running time of the experiment, infinit by default
        - dt: time inverval between measueements.
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource1: resource name for the sourcemeter Keithley24XX, default is 'GPIB0::24::INSTR'
        - resource2: resource name for the multimeter, default is 'GPIB0::23::INSTR'
        - term: either FRONT (default) or REAR for the sourmeter
        - config: if True (default) the sourcemeter from resource 1 is assumed that has to be configured, otherwise just reads the values
        - interrupt_measurement: if True, when the program is interrupted, the sourcemeter from resource 1 will stop only measuring, but will continue on ON state. Default is False (interrupts both measurement and sourcemeter)
        - Ncount: number of samples to ask the sourcemeter/multimeter, shared parameter. Default is 1
        - aver: Average the number of samples given for Ncount. Default is False- Shared parameter between  sourcemeter/multimeter.
        - nplc: Number of pulse ligh cycles to measure. Shared between sourcemeter/multimeter and default i 1.
        - fw: Activate the four-wire measurement (for the sourcemeter).
        - beeper: Activate the beeper (for the sourcemeter).
        - cmpl: compliance fot the sourcemeter (in volts since we are asking for current)
        - config: either to config (or not) the sourcemeter and multimeter, by default is True. If set False and it is really not configured, will  just complain and not work.
        - interrupt_measurement: dflt is False, if set to True when the program is interrupted, the measurement will stop but not the sourcemeter, that will continue feeding the device.
        - dt_fix: the time interval is fixed. It set to false, then it uses the time interval from the function dt_calc()
    """

    # First, we create an instance of a ktly24XX instrument class with the resource passed as an argument and later we configure (or not) the resource considering it is a Keithley24XX family
    ky2400 = pyLab.ktly20XX(resource1)
    mult = pyLab.keysight34461A(resource2) # Creating the object for the multimeter
    if config:
        ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount,\
                                   nplc = nplc, aver = aver)

############# MULTIMETER #################################

        mult.config_volt(rang = 5.0, nplc = nplc, count = Ncount)  # Configure the multimeter

############ Initializing some  values and figure ###########################
    Nsteps = 200 # Maxmimum number of values of the array to print
    ptime = np.linspace(0,Nsteps,Nsteps, False)
    pvolt = np.zeros(len(ptime))
    pdiode = np.zeros(len(ptime))
    lines = None

############  LOGGING THE DATA ###################################
    # Opening the file to save the data
    filename = os.path.join(folder,today + '_' + fname + '.txt')
    if not os.path.isfile(filename):
        f = open(filename,'a')
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage', 'Photocurrent_volt(V)', 'Absolute time(s)'))
        f.write(('# {:^5}\t'+5*'{:^12}\t' + '\n').format('i','s','A','V', 'V', 's'))
    else:
        f = open(filename,'a')
########## PERFORMING FIRST MEASUREMENTS ############################
    # First measruementes without running the LEC
    if config:
        ky2400.mode_ifix_setcurr(0.0 / 1000.0, curr_max=0.1, curr_min= -0.001)
        ky2400.outpon()

    ttime = 0.0
    etime= 0.0 # Ellapsed time
    itime = 0.0
    start = True

    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    f.write('# {}\n'.format(timestamp))
    stime = time()
    zero_current_time = 2.0
    flag_scurr = False # Flag to control if the current has been set or not.

    i = 0 # Step counter
    k = 0

    while etime < rtime:
        try:
            time1 = time()
            [voltage, mcurrent, _ , ttime, _ ] = ky2400.read()
            voltage_ph = mult.read().mean()
#            assert not ky2400.check_volt_compliance(), 'Compliance reached!'
            if not start:
                etime = ttime - itime
            else:
                itime =  ttime
                start = False

            pvolt[-1] = voltage
            pdiode[-1] = voltage_ph

            f.write(('{:^5d}\t' +5*'{:^12.10f}\t' + '\n').format(k,etime, mcurrent,voltage, voltage_ph,ttime))

            i += 1
            k += 1

            lines = pyLab.live_plotter(ptime,[pvolt, pdiode],lines, title = 'IVL characterisation', ylabel = 'Voltage (V)')
            pvolt = np.append(pvolt[1:],0.0)
            pdiode = np.append(pdiode[1:],0.0)

            if (ttime - itime) >=  zero_current_time and config and not flag_scurr:
                ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.00)
                flag_scurr = True

            if dt_fix:
                sleeping_time = dt
            else:
                sleeping_time = dt_calc(etime)

            #sleeping_time = sleeping_time - (time() - stime) % sleeping_time
            while (time() - time1) < sleeping_time:
                sleep(0.01)
                window.update()
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            if interrupt_measurement:
                print('Measurement stopped, but sourcemeter still on\n')
            else:
                ky2400.outpoff()
                f.close()
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print(e)
            ky2400.outpoff()
            f.close()
            break
    if not interrupt_measurement:
        ky2400.outpoff()
        f.close()

def ivl_setup_daq(current, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource1 = 'GPIB0::24::INSTR', term = 'FRONT',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False, nplc = 1.0,
                    config = True, interrupt_measurement = False,dt_fix = True,\
                    device = 'Dev1',\
                    adqTime = 0.05, adqFreq = 10000, list_channels  = [0],\
                    rang_channels = [10], mode = 'FiniteSamps',\
                    terminalConfig = 'SE'):
    """Same as ivl_setup_v2 but with the photodiode adquisition made by a USB daq
    Mandatory arguments:
        - current: current in mA
    Optional arguments:
        - rtime: running time of the experiment in seconds, infinit by default
        - dt: time inverval between measueements.
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource1: resource name for the sourcemeter Keithley24XX, default is 'GPIB0::24::INSTR'
        - device: resource name for the DAQ device,
        - term: either FRONT (default) or REAR for the sourmeter
        - config: if True (default) the sourcemeter from resource 1 is assumed that has to be configured, otherwise just reads the values
        - interrupt_measurement: if True, when the program is interrupted, the sourcemeter from resource 1 will stop only measuring, but will continue on ON state. Default is False (interrupts both measurement and sourcemeter)
        - Ncount: number of samples to ask the sourcemeter/multimeter, shared parameter. Default is 1
        - aver: Average the number of samples given for Ncount. Default is False- Shared parameter between  sourcemeter/multimeter.
        - nplc: Number of pulse ligh cycles to measure. Shared between sourcemeter/multimeter and default i 1.
        - fw: Activate the four-wire measurement (for the sourcemeter).
        - beeper: Activate the beeper (for the sourcemeter).
        - cmpl: compliance fot the sourcemeter (in volts since we are asking for current)
        - config: either to config (or not) the sourcemeter and multimeter, by default is True. If set False and it is really not configured, will  just complain and not work.
        - interrupt_measurement: dflt is False, if set to True when the program is interrupted, the measurement will stop but not the sourcemeter, that will continue feeding the device.
        - dt_fix: the time interval is fixed. It set to false, then it uses the time interval from the function dt_calc()
    """

    # First, we create an instance of a ktly24XX instrument class with the resource passed as an argument and later we configure (or not) the resource considering it is a Keithley24XX family
    ky2400 = pyLab.ktly20XX(resource1)

    if config:
        ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount,\
                                   nplc = nplc, aver = aver)

    usb6009 = pyLab.daq(device,adqTime,adqFreq,list_channels,rang_channels, mode = mode, terminalConfig = terminalConfig)
############ Initializing some  values and figure ###########################
    Nsteps = 200 # Maxmimum number of values of the array to print
    ptime = np.linspace(0,Nsteps,Nsteps, False)
    pvolt = np.zeros(len(ptime))
    pdiode = np.zeros(len(ptime))
    lines = None

############  LOGGING THE DATA ###################################
    # Opening the file to save the data
    filename = os.path.join(folder,today + '_' + fname + '.txt')
    if not os.path.isfile(filename):
        with open(filename,'a') as f:
            f = open(filename,'a')
            f.write(('# {:^5}\t'+6*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage', 'Photocurrent_volt', 'Absolute time', 'Temperature'))
            f.write(('# {:^5}\t'+6*'{:^12}\t' + '\n').format('i','s','A','V', 'V', 's','C'))

########## PERFORMING FIRST MEASUREMENTS ############################
    # First measruementes without running the LEC
    if config:
        ky2400.mode_ifix_setcurr(0.0 / 1000.0, curr_max=0.1, curr_min= -0.001)
        ky2400.outpon()

    ttime = 0.0
    etime= 0.0 # Ellapsed time
    itime = 0.0
    start = True

    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    with open(filename,'a') as f:
        f.write('# {}\n'.format(timestamp))
    stime = time()
    zero_current_time = 2.0
    flag_scurr = False # Flag to control if the current has been set or not.
    flag_ch_cmpl = False # Just to check if the compliance has been change after x seconds
    i = 0 # Step counter
    k = 0

    # Add this function to sequentally update the time interval of the measurement
    if dt_fix:
        sleeping_time = dt
    else:
        sleeping_time = dt_calc(etime)

    while etime < rtime:
        try:
            time1 = time()
            [voltage, mcurrent, _ , ttime, _ ] = ky2400.read()
            usb6009.start()
            voltage_ph = usb6009.read_analog().mean(axis = 0)[0]
            usb6009.stop()
#            assert not ky2400.check_volt_compliance(), 'Compliance reached!'
            if not start:
                etime = ttime - itime
            else:
                itime =  ttime
                start = False

            pvolt[-1] = voltage
            pdiode[-1] = voltage_ph*1.0
            # Snippet to read the temperature of the PID
            with open(r'..\Python Scripts\temp\temp.dat') as f1:
                temperature = float(f1.read())
            with open(filename,'a') as f:
                f.write(('{:^5d}\t' +6*'{:^12.10f}\t' + '\n').format(k,etime, mcurrent,voltage, voltage_ph,ttime, temperature))

            i += 1
            k += 1

            lines = pyLab.live_plotter(ptime,[pvolt, pdiode],lines, title = 'IVL characterisation', ylabel = 'Voltage (V)')
            pvolt = np.append(pvolt[1:],0.0)
            pdiode = np.append(pdiode[1:],0.0)

            if not flag_ch_cmpl:
                if (time() - stime) > 300:
                    ky2400.inst.write(":SENS:VOLT:PROT 21")    # Set 21V compliance limit.
                    flag_ch_cmpl = True

            if (ttime - itime) >=  zero_current_time and config and not flag_scurr:
                ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.00)
                flag_scurr = True

            if dt_fix:
                sleeping_time = dt
            else:
                sleeping_time = dt_calc(etime)

            while (time() - time1) < sleeping_time:
                sleep(0.01)
        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            usb6009.clear()
            if interrupt_measurement:
                print('Measurement stopped, but sourcemeter still on\n')
            else:
                ky2400.outpoff()
                f.close()
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print(e)
            ky2400.outpoff()
            f.close()
            usb6009.clear()
            break
    if not interrupt_measurement:
        ky2400.outpoff()
        f.close()

def ivl_setup_cc(current, rtime = np.inf, dt = 0.25,\
                    folder = '.\\', fname = 'voltage-time-data',\
                    resource1 = 'GPIB0::24::INSTR', term = 'FRONT',\
                    fw = False, beeper = True, cmpl = 21.0,\
                    Ncount = 1, aver = False, nplc = 1.0,
                    config = True, interrupt_measurement = False,dt_fix = True,\
                    device = 'Dev1',\
                    adqTime = 0.05, adqFreq = 10000, list_channels  = [0],\
                    rang_channels = [10], mode = 'FiniteSamps',\
                    terminalConfig = 'SE'):
    """Same as ivl_setup_v2 but with the photodiode adquisition made by a USB daq
    Mandatory arguments and using globals:
        - current: current in mA
    Optional arguments:
        - rtime: running time of the experiment in seconds, infinit by default
        - dt: time inverval between measueements.
        - folder: folder path where to save the file, abs o relative, current directory by default.
        - fname: filename where to save the data. Always append if the file already exists
        - resource1: resource name for the sourcemeter Keithley24XX, default is 'GPIB0::24::INSTR'
        - device: resource name for the DAQ device,
        - term: either FRONT (default) or REAR for the sourmeter
        - config: if True (default) the sourcemeter from resource 1 is assumed that has to be configured, otherwise just reads the values
        - interrupt_measurement: if True, when the program is interrupted, the sourcemeter from resource 1 will stop only measuring, but will continue on ON state. Default is False (interrupts both measurement and sourcemeter)
        - Ncount: number of samples to ask the sourcemeter/multimeter, shared parameter. Default is 1
        - aver: Average the number of samples given for Ncount. Default is False- Shared parameter between  sourcemeter/multimeter.
        - nplc: Number of pulse ligh cycles to measure. Shared between sourcemeter/multimeter and default i 1.
        - fw: Activate the four-wire measurement (for the sourcemeter).
        - beeper: Activate the beeper (for the sourcemeter).
        - cmpl: compliance fot the sourcemeter (in volts since we are asking for current)
        - config: either to config (or not) the sourcemeter and multimeter, by default is True. If set False and it is really not configured, will  just complain and not work.
        - interrupt_measurement: dflt is False, if set to True when the program is interrupted, the measurement will stop but not the sourcemeter, that will continue feeding the device.
        - dt_fix: the time interval is fixed. It set to false, then it uses the time interval from the function dt_calc()
    """
    # Configuration only done if not done before
    ky2400 = pyLab.ktly20XX(resource1)

    gsivl.SET_CURRENT = current

    if gsivl.CONFIG_STATUS:
        ky2400.mode_ifix_configure(fw = fw,beeper = beeper, term = term, cmpl = cmpl, Ncount = Ncount,\
                                   nplc = nplc, aver = aver)

    usb6009 = pyLab.daq(device,adqTime,adqFreq,list_channels,rang_channels, mode = mode, terminalConfig = terminalConfig)

    ttime = 0.0
    etime= 0.0 # Ellapsed time
    itime = 0.0
    start = True
    ############  LOGGING THE DATA ###################################
    # Opening the file to save the data
    filename = os.path.join(folder,today + '_' + fname + '.txt')
    if not os.path.isfile(filename):
        with open(filename,'a') as f:
            f.write(('# {:^5}\t'+6*'{:^12}\t' + '\n').format('Step','Time','Current','Voltage', 'Photocurrent_volt', 'Absolute time', 'Temperature'))
            f.write(('# {:^5}\t'+6*'{:^12}\t' + '\n').format('i','s','A','V', 'V', 's','C'))
    if gsivl.MEASUREMENT_ON:
        # Obtaining and saving offset for photodiode voltage
        usb6009.start()
        voltage_ph = usb6009.read_analog().mean(axis = 0)[0]
        usb6009.stop()
        timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
        with open(filename,'a') as f:
            f.write('# {}\n'.format(timestamp))
            f.write(('# Offset photodiode: {:^12.10f}' + '\n').format(voltage_ph))

    stime = time()

    flag_ch_cmpl = False # Just to check if the compliance has been change after x seconds
    i = 0 # Step counter

    if gsivl.MEASUREMENT_ON:
        ky2400.mode_ifix_setcurr(current / 1000.0,curr_max=0.1, curr_min= 0.00)
        ky2400.outpon()
    else: ky2400.outpoff()

    max_length = 500 # Max. length of the global data

    while etime < rtime and gsivl.MEASUREMENT_ON:
        try:

            time1 = time()
            [voltage, mcurrent, _ , ttime, _ ] = ky2400.read()
            usb6009.start()
            voltage_ph = usb6009.read_analog().mean(axis = 0)[0]
            usb6009.stop()
#            assert not ky2400.check_volt_compliance(), 'Compliance reached!'
            if not start:
                etime = ttime - itime
            else:
                itime =  ttime
                start = False

            with open(r'.\temp\temp.dat') as f1:
                temperature = float(f1.read())
            with open(filename,'a') as f:
                f.write(('{:^5d}\t' +6*'{:^12.10f}\t' + '\n').format(i,etime, mcurrent,voltage, voltage_ph,ttime, temperature))
            i += 1

            total_length = len(gsivl.ETIME)

            if total_length < max_length or total_length == 0:
                gsivl.ETIME.append(datetime.datetime.now())
                gsivl.CURRENT.append(mcurrent)
                gsivl.VOLTAGE.append(voltage)
                gsivl.LUMINANCE.append(voltage_ph)
            else:
                gsivl.ETIME = gsivl.ETIME[1:] + [datetime.datetime.now()]
                gsivl.CURRENT =  gsivl.CURRENT[1:] + [mcurrent]
                gsivl.VOLTAGE = gsivl.VOLTAGE[1:] + [voltage]
                gsivl.LUMINANCE =  gsivl.LUMINANCE[1:] + [voltage_ph]

            if not flag_ch_cmpl:
                if (time() - stime) > 300:
                    ky2400.inst.write(":SENS:VOLT:PROT 21")    # Set 21V compliance limit.
                    flag_ch_cmpl = True

            if gsivl.SET_CURRENT == current:
                pass
            else:
                ky2400.mode_ifix_setcurr(gsivl.SET_CURRENT / 1000.0,curr_max=0.1, curr_min= 0.00)
                current = gsivl.SET_CURRENT

            if dt_fix:
                sleeping_time = dt
            else:
                sleeping_time = dt_calc(etime)

            while (time() - time1) < sleeping_time and gsivl.MEASUREMENT_ON:
                sleep(0.01)

        except KeyboardInterrupt:
            # In case of error turn off the source anyway and stop the program
            print('Programm interrupted in a safe way\n')
            usb6009.clear()
            if interrupt_measurement:
                print('Measurement stopped, but sourcemeter still on\n')
            else:
                ky2400.outpoff()
                f.close()
            break
        except Exception as e:
            # In case of ANY error turn off the source anyway and stop the program while printing the error
            print(e)
            ky2400.outpoff()
            usb6009.clear()
            break
    if not interrupt_measurement:
        ky2400.outpoff()

max_stars_repo_name: jrafolsr/pyInstruments

max_issues_repo_name: jrafolsr/pyInstruments

max_forks_repo_name: jrafolsr/pyInstruments


text:
import glob
import logging
import os
import re
import shutil
import tempfile

import hydra
import numpy as np
import pyparsing as pp
from edalize import *
from hydra.core.hydra_config import HydraConfig
from hydra.utils import get_original_cwd
from omegaconf import DictConfig, OmegaConf

from do_blink.edalize.vivado import Vivado

# A logger for this file
log = logging.getLogger(__name__)

from pathlib import Path

def build_do_blink_designs(cfg):
    project_dir = get_original_cwd()
    verilog_common_src_dir = os.path.join(
        project_dir, f"do_blink/{cfg.do_blink.backend}_benchmark/hdl/common/"
    )
    verilog_common_out_dir = os.path.join(
        "/tmp", f"do_blink/{cfg.do_blink.backend}_benchmark/hdl/common"
    )
    verilog_src_dir = os.path.join(
        project_dir,
        f"do_blink/{cfg.do_blink.backend}_benchmark/hdl/{cfg.do_blink.num_luts}/src/",
    )
    verilog_out_dir = os.path.join(
        "/tmp",
        f"do_blink/{cfg.do_blink.backend}_benchmark/hdl/{cfg.do_blink.num_luts}/src",
    )

    overlay_dir = os.path.join(
        project_dir,
        f"do_blink/{cfg.do_blink.backend}_benchmark/overlay/{cfg.do_blink.figure}_{cfg.do_blink.sub_figure}/{cfg.do_blink.bft}/{cfg.do_blink.num_luts}/{cfg.do_blink.device}/",
    )
    overlay_out_dir = os.path.join(
        "/tmp",
        f"do_blink/{cfg.do_blink.backend}_benchmark/overlay/{cfg.do_blink.figure}_{cfg.do_blink.sub_figure}/{cfg.do_blink.bft}/{cfg.do_blink.num_luts}/{cfg.do_blink.device}",
    )

    name = "_".join(
        [
            cfg.do_blink.backend,
            cfg.do_blink.figure,
            cfg.do_blink.sub_figure,
            str(cfg.do_blink.device),
            str(cfg.do_blink.num_luts),
            cfg.do_blink.bft,
            re.sub("\D", "", os.getcwd()),
        ]
    )
    work_root = tempfile.mkdtemp(prefix=name + "_")

    tool_options = {
        "common_src_dir": verilog_common_out_dir,
        "src_dir": verilog_out_dir,
        "overlay_dir": overlay_out_dir,
        "part": cfg.do_blink.part,
        "load_vivado": cfg.do_blink.load_vivado,
        "use_abs": cfg.do_blink.get("use_abs", False),
    }
    pnr_script = "pnr_abs.tcl" if cfg.do_blink.get("use_abs", False) else "pnr.tcl"
    hooks = {
        "pre_run": [
            {"cmd": ["mkdir", "-p", verilog_common_out_dir], "name": "mkdir"},
            {"cmd": ["mkdir", "-p", verilog_out_dir], "name": "mkdir"},
            {"cmd": ["mkdir", "-p", overlay_out_dir], "name": "mkdir"},
            {
                "cmd": ["rsync", "-a", verilog_common_src_dir, verilog_common_out_dir],
                "name": "get common verilog files",
            },
            {
                "cmd": ["rsync", "-a", verilog_src_dir, verilog_out_dir],
                "name": "get verilog files",
            },
            {
                "cmd": ["rsync", "-a", overlay_dir, overlay_out_dir],
                "name": "get overlay files",
            },
            {
                "cmd": ["sh", "vivado.sh", "-mode", "batch", "-source", "synth.tcl"],
                "name": "run synth.tcl",
            },
            {
                "cmd": ["sh", "vivado.sh", "-mode", "batch", "-source", pnr_script],
                "name": "run pnr.tcl",
            },
        ]
    }
    edam = {
        "name": name,
        "tool_options": {"vivado": tool_options},
        "toplevel": "top",
        "hooks": hooks,
    }

    backend = Vivado(edam=edam, work_root=work_root)
    backend.configure()
    try:
        backend.run()
        run_synth_log = open(
            glob.glob("{}/**/run_synth.log".format(work_root), recursive=True)[0], "r"
        ).read()
        run_log_name = "run_abs" if cfg.do_blink.get("use_abs", False) else "run"
        run_log = open(
            glob.glob("{}/**/{}.log".format(work_root, run_log_name), recursive=True)[
                0
            ],
            "r",
        ).read()

        syn_time_pattern = pp.Literal("syn:") + pp.Word(pp.nums) + pp.Literal("seconds")
        rdchk_time_pattern = (
            pp.Literal("read_checkpoint:") + pp.Word(pp.nums) + pp.Literal("seconds")
        )
        opt_time_pattern = pp.Literal("opt:") + pp.Word(pp.nums) + pp.Literal("seconds")
        place_time_pattern = (
            pp.Literal("place:") + pp.Word(pp.nums) + pp.Literal("seconds")
        )
        route_time_pattern = (
            pp.Literal("route:") + pp.Word(pp.nums) + pp.Literal("seconds")
        )
        bit_gen_time_pattern = (
            pp.Literal("bit_gen:") + pp.Word(pp.nums) + pp.Literal("seconds")
        )

        syn_time = float(syn_time_pattern.searchString(run_synth_log).asList()[0][1])
        rdchk_time = float(rdchk_time_pattern.searchString(run_log).asList()[0][1])
        opt_time = float(opt_time_pattern.searchString(run_log).asList()[0][1])
        place_time = float(place_time_pattern.searchString(run_log).asList()[0][1])
        route_time = float(route_time_pattern.searchString(run_log).asList()[0][1])
        bit_gen_time = float(bit_gen_time_pattern.searchString(run_log).asList()[0][1])
        total_time = (
            syn_time + rdchk_time + opt_time + place_time + route_time + bit_gen_time
        )
        log.info(
            f"{cfg.do_blink.device},{int(syn_time)},{int(rdchk_time)},{int(opt_time)},{int(place_time)},{int(route_time)},{int(bit_gen_time)},{int(total_time)}"
        )
        return total_time
    except RuntimeError as e:
        log.info(e)
        log.info(f"Experiment failed!")
        total_time = 10000
        return total_time

@hydra.main(config_path="../../conf", config_name="config")
def parse_config(cfg: DictConfig) -> None:
    return build_do_blink_designs(cfg)

if __name__ == "__main__":
    parse_config()

max_stars_repo_name: icgrp/doblink

max_issues_repo_name: icgrp/doblink

max_forks_repo_name: icgrp/doblink


text:
"""
Load gene expression data
"""
import pandas as pd
import numpy as np
import pickle

def make_experiment_name(dataroot, Ndata, D, sd,sd0, alpha):
    savedir = make_gene_name(dataroot, Ndata, D)[1] + "/sd=%.2f_sd0=%.2f_alpha=%.2f" %(sd,sd0, alpha)
    return savedir

def make_gene_name(dataroot, Ndata, D):
    name = "%s_data_Ndata=%d_D=%d" %(dataroot, Ndata, D)
    name_as_csv = name + ".csv"
    return (name_as_csv, name)

def load_gene_data(dataroot, directory, Ndata, D):
    savename = directory + make_gene_name(dataroot, Ndata, D)[0]
    print("Will load data from %s" %savename)
    df = pd.read_csv(savename)
    # check if data has a true-label column
    if "z_true_subset" in df.columns:
        print("Unique labels in the data are")
        print(np.unique(df["z_true_subset"]))
        df.drop(df.columns[[0,1]],axis=1,inplace=True)
    else:
        df.drop(df.columns[[0]],axis=1,inplace=True)
    data = df.to_numpy()
    assert data.shape[0] == Ndata
    assert data.shape[1] == D
    return data


text:
#!/usr/bin/env python

# this script can serve as an example for post-processing voxels
# from here, you're on your own!
# note the three critical (and general) steps involved:
# 1. read voxel image to array
# 2. perform aggregation if needed and calculation of region of interest
# 3. output image with calculated metric

"""
Creates raster of transformed relative density models for declared region of interest
"""

from datetime import datetime
import argparse
from osgeo import gdal
from l2d.utils import find_lasfiles, find_classified_lasfile, create_vrt, class_params
from l2d.voxel_utils import aggregate, clip_by_site
import os, math, numpy, glob, sys
from scipy import signal, interpolate
import scipy
import gippy
from gippy import GeoVector

def main():
    dhf = argparse.ArgumentDefaultsHelpFormatter

    desc = 'Process voxels into relative density metrics; note this script will require modifications for specific calculations, users are responsible for this'
    parser = argparse.ArgumentParser(description=desc, formatter_class=dhf)
    parser.add_argument(
        'voxdir', help='Directory holding voxel lidar data')
    parser.add_argument(
	'--voxtype', help='Type of return data to use for calculations', nargs='*', default=['count'])
    parser.add_argument(
	'--metric', help='Metric name user defined, used for naming output image', default=None)
    parser.add_argument(
	'--start', help='Low height of relative density region of interest', default=['1'])
    parser.add_argument(
	'--stop', help='Top height of relative density region of interest', default=['5'])
    parser.add_argument(
	'--pixelsize', help='Output image pixel size, used to aggregate voxels in x-y dimension', default=['1'])
    parser.add_argument(
        '-s', '--site', default=None,
        help='Site shapefile name used for ')
    parser.add_argument(
	'--outdir', help='Directory to output metric rasters, directory name should specify type of metric')
    parser.add_argument(
        '-o', '--overwrite', default=False, action='store_true',
        help='Overwrite any existing output files')
    parser.add_argument(
        '-v', '--verbose', default=False, action='store_true',
        help='Print additional info')
    args = parser.parse_args()

    start0 = datetime.now()

    #variables describing region of interest and scale
    startoff = int(args.start)
    cutoff = int(args.stop)
    pixelsize = int(args.pixelsize)
    if args.metric is None:
	args.metric = 'rdm-%s_to_%s' %(startoff,cutoff)
    voxdir = args.voxdir

    # make sure outdir exists
    args.outdir = os.path.abspath(args.outdir)
    if not os.path.exists(args.outdir):
        os.makedirs(args.outdir)

    # the final filenames
    product = args.metric
    #fouts = os.path.join(args.outdir, '%s.voxel_metric.vrt' % (product))
    fouts = {p: os.path.join(args.outdir, '%s.voxel_metric.vrt' % (p)) for p in [product]}

    # run if any products are missing
    exists = all([os.path.exists(f) for f in fouts.values()])
    if exists and not args.overwrite:
        print 'Already created metric rasters in %s' % (os.path.relpath(args.outdir))
        exit(0)

    # loop through voxel rasters
    # site = glob.glob('*.%s.*.tif' %(args.voxtype))
    # open site vector
    if args.site is not None:
        try:
            site = GeoVector(args.site)
        except:
            print 'Error opening %s' % args.site
            exit(2)
    else:
        site = [None]

    pieces = []
    for feature in site:
        try:

	    # extract naming convention
	    bname = '' if feature is None else feature.Basename() + '_'
	    ftr = bname.split('_')[0]
	    bname = os.path.join(os.path.abspath(voxdir), '%s' % (bname))
	    vox_name = bname + 'voxels.%s.tif' %(args.voxtype[0])
	    out = os.path.join(args.outdir, '%s_%s.voxel_metric.tif' % (ftr,product))
	    print out

	    #open image
	    vox_img = gippy.GeoImage(vox_name)
	    proj = vox_img.Projection()
	    affine = vox_img.Affine()
	    affine[1],affine[5] = pixelsize,-pixelsize
	    vox_arr = vox_img.Read().squeeze()
	    # vox_img = gdal.Open(vox_name)
	    # vox_arr = vox_img.ReadAsArray()
	    nbands,nrows,ncols = vox_arr.shape
	    print 'voxel dimensions: %s, %s, %s' %(nbands,nrows,ncols)
	    sys.stdout.flush()

	    # calculate relative density ratio of returns -- this section of code is the section that should be modified depending on needs
	    # modifications if desired; also note, startoff and cutoff values may not be relevant if code is changed
	    data = aggregate(vox_arr,pixelsize)
	    nbands,nrows,ncols = data.shape
	    print 'aggregated dimensions: %s, %s, %s, Calculating...' %data.shape
	    sys.stdout.flush()
	    i1 = numpy.sum(data[startoff+1:cutoff+1],axis=0,dtype=float)
	    i2 = numpy.sum(data,axis=0,dtype=float)

	    ratio = numpy.zeros(i1.shape, dtype=float)
	    ratio[numpy.where(i2>0)] = i1[numpy.where(i2>0)]/i2[numpy.where(i2>0)]
	    transformed = numpy.sqrt(ratio)+0.001

	    print 'writing image'
	    sys.stdout.flush()
	    #output ratio image

	    imgout = gippy.GeoImage(out,ncols,nrows,1,gippy.GDT_Float32)
	    imgout.SetProjection(proj)
	    imgout.SetAffine(affine)
	    imgout[0].Write(transformed)
	    imgout = None
	    #
	    # modification area ends

	    print 'clipping image to feature'

	    clip_by_site(out,feature)

	    pieces.append(out)

        except Exception, e:
	    print "Error creating metric: %s" % e
	    if args.verbose:
	        import traceback
	        print traceback.format_exc()

    # combine all features into single file and align to site for chm
    create_vrt(pieces, fouts[product], site=site)

    print 'l2d_process_voxels completed (%s) in %s' % (os.path.relpath(args.outdir), datetime.now() - start0)

if __name__ == '__main__':
    main()

reason: import scipy,from scipy