text:
import os
import copy
import numpy as np
from datetime import datetime
from netCDF4 import num2date, date2num,stringtochar,chartostring
from netcdf import NetCDF
from .s3netcdf_func import getItemNetCDF,setItemNetCDF,getDataShape,checkValue,getIndices,getPartitions,getMasterIndices,\
  getChildShape,getMasterShape,getSubIndex,parseIndex,iDim,parseIdx,isQuickSet,isQuickGet
import time
np.seterr(divide='ignore', invalid='ignore', over='ignore')

class S3NetCDFGroup(object):
  """
  A NetCDF class that handles partition files

  Parameters
  ----------
  folder : str,path
      Folder of master, a subfolder is created under here
  src_file : netCDF4.Dataset
      Master NetCDF2D
  name : str
      Group name (i.e. s,t,ss,st)

  Attributes
  ----------
  folderPath :
    Folder to save .nc file
  name : str,
    Group name (i.e. s,t,ss,st)
  ndata :
    Number of axes/dimensions on the original data
  nmaster :
    Number of axes/dimensions on the master file
  nchild :
    Number of axes/dimensions on child. Should be the same to the original data
  shape :
    Shape of orginal data (e.g. 2,2000)
  master :
    Shape of master file (e.g. 2,2,1,1000)
  child :
    Shape of child file (e.g. 1,1000)
  variables:
    Pre-create dimension and variable array for child file

  """
  def __init__(self, parent, name):
    self.parent = parent
    self.src_group = src_group = parent.nca[name]
    self.metadata=metadata=src_group.metadata
    self.folderPath = os.path.join(parent.folder, name)
    self.name = name
    self.shape = list(np.array(metadata['shape'],ndmin=1))
    self.master = list(np.array(metadata['master'],ndmin=1))
    self.child = list(np.array(metadata['child'],ndmin=1))
    self.dimensions = list(np.array(metadata['dims'],ndmin=1))
    self.ndata = len(self.shape)
    self.nmaster = len(self.master)
    self.nchild = len(self.child)
    self.childDimensions=metadata['cdims']
    self.variables = src_group.variables

  def __checkVariable(self,idx):
    """
    Checking parameters before getting and setting values:
    Needs atleast two axes, name of variable and index value(i.e ["s",:])
    """
    if not isinstance(idx,tuple):raise TypeError("Needs variable")
    idx   = list(idx)
    vname = idx.pop(0)
    idx   = tuple(idx)
    if not vname in self.variables:raise Exception("Variable {} does not exist".format(vname))
    return vname,idx

  def getPartitions(self,vname,obj,partitions_only=True):
    """

    """
    if not vname in self.variables:raise Exception("Variable does not exist in group {}".format(self.name))

    shape=self.shape
    masterShape=self.master
    variable=copy.deepcopy(self.variables[vname])
    dimensions=variable['dimensions']

    values=[parseIdx(obj.get(iDim(dimension),None)) for dimension in dimensions] # Remove n from the dimension name (e.g. ntime=>time). It will look for time in the obj
    for i in range(self.ndata):
      value=values[i]
      if isinstance(value,int):
        if value is not None and value<0:raise Exception("{} needs to be equal or above 0".format(iDim(dimensions[i])))
        if value is not None and value>self.shape[i]:raise Exception("{} needs to be below {}".format(iDim(dimensions[i]),self.shape[i]))
      elif isinstance(value,slice):
        if value.start is not None and value.start<0:raise Exception("{} needs to be equal or above 0".format(iDim(dimensions[i])))
        if value.stop is not None and value.stop<0:raise Exception("{} needs to be equal or above 0".format(iDim(dimensions[i])))
        if value.start is not None and value.start>self.shape[i]:raise Exception("{} needs to be below {}".format(iDim(dimensions[i]),self.shape[i]))
        if value.stop is not None and value.stop>self.shape[i]:raise Exception("{} needs to be below {}".format(iDim(dimensions[i]),self.shape[i]))

    idx=tuple(values)

    indices = getIndices(idx,shape)

    partitions = getPartitions(indices, shape,masterShape)

    if partitions_only: return partitions
    return partitions,self,idx,indices

  def _getFilepath(self,vname,part):
    strpart = "_".join(part.astype(str))
    return os.path.join(self.folderPath, "{}_{}_{}_{}.nc".format(self.parent.name, self.name, vname, strpart))

  def _getFile(self,vname,part):
    localOnly = self.parent.localOnly
    s3 = self.parent.s3
    filepath=self._getFilepath(vname,part)
    if not os.path.exists(filepath):
      if localOnly:raise Exception("File does not exist. No data was assigned. {}".format(filepath))
      if not s3.exists(filepath):
        print("WARNING: File does not exist on S3. Zeroes were assigned to the array. {}".format(filepath))
        return None
      s3.download(filepath)
    return filepath

  def _setFile(self,vname,part):
    localOnly = self.parent.localOnly
    s3 = self.parent.s3
    filepath=self._getFilepath(vname,part)
    if not os.path.exists(filepath):
      if localOnly or not s3.exists(filepath):NetCDF.create(filepath,dimensions=self.childDimensions,variables=copy.deepcopy(self.variables))
      else:s3.download(filepath)
    return filepath

  def __run(self,vname,indices,_data=None,_value=None):
    shape = self.shape
    masterShape = self.master
    partitions = getPartitions(indices, shape,masterShape)
    masterIndices = getMasterIndices(indices,shape,masterShape)

    for part in partitions:
      idata,ipart=getSubIndex(part,shape,masterIndices)
      # GET
      if _data is not None:
        d=_data.flatten()
        filepath=self._getFile(vname,part)
        if filepath is not None:
          with NetCDF(filepath, "r") as netcdf:d=getItemNetCDF(netcdf[vname],d,ipart,idata)
          if not self.parent.localOnly and self.parent.autoRemove:
            try:os.remove(filepath)
            except:print("Already removed")
        _data=d.reshape(_data.shape)

      # SET
      if _value is not None:
        filepath=self._setFile(vname,part)
        with NetCDF(filepath, "r+") as netcdf:setItemNetCDF(netcdf[vname],_value,ipart,idata)
        if not self.parent.localOnly:
          self.parent.s3.upload(filepath)
          if self.parent.autoRemove:os.remove(filepath)
    return _data

  def __subrun(self,vname,idx,value=None):
    shape=self.shape
    indices = getIndices(idx,shape)
    ishape=[len(arr) for arr in indices]
    tmshape,tcshape=getMasterShape(ishape,return_childshape=True,ncSize=self.parent.memorySize)

    if value is not None:
      value=value.reshape(ishape)
    else:
      dataShape = getDataShape(indices)
      ftype=self.variables[vname]['ftype']
      if ftype=='M':ftype='d'
      data = np.zeros(dataShape,dtype=ftype)
      data=data.reshape(ishape)

    matrix=[np.arange(idim) for idim in tmshape[:len(ishape)]]
    meshgrid = np.meshgrid(*matrix,indexing="ij")
    tpartitions = np.unique(np.concatenate(np.array(meshgrid).T),axis=0)
    tpartitions=tpartitions.reshape((int(tpartitions.size/len(ishape)),len(ishape)))

    for part in tpartitions:

      _local=tuple([slice(i*tcshape[idim],np.minimum((i+1)*tcshape[idim],ishape[idim])) for idim,i in enumerate(part)])
      _indices=tuple([indices[idim][np.arange(i*tcshape[idim],np.minimum(i*tcshape[idim]+tcshape[idim],ishape[idim]))] for idim,i in enumerate(part)])
      if value is not None:
        _value=value[_local]
        self.__run(vname,_indices,_value=_value.flatten())
      else:
        data[_local]=self.__run(vname,_indices,_data=data[_local])

    if value is None:
      return data.reshape(dataShape)

  def __getitem__(self, idx):
    """
    Get data based on the query
    """
    vname,idx = self.__checkVariable(idx)

    # if isQuickGet(idx,self.ndata,np.array(self.master)):
    #   data=self._quickGet(vname,list(idx)[0])
    # else:
    data=self.__subrun(vname,idx)

    ftype=self.variables[vname]['ftype']
    if ftype=='M':data=data.astype("datetime64[ms]")
    if ftype=='S1':data=chartostring(data)

    return data

  def __setitem__(self, idx,value):
    """
    Set data based on the query
    """

    vname,idx = self.__checkVariable(idx)
    if self.variables[vname]['type']=="S":
      value=stringtochar(np.array(value).astype("S{}".format(self.shape[1])))

    # if self.variables[vname]['type']!="S":
    value= checkValue(value,idx,self.shape)

    # if isQuickSet(idx,self.ndata,self.master,self.child):
      # self._quickSet(vname,self._getPartIndex(idx),value)
    # else:
    self.__subrun(vname,idx,value=value)

  def _getPartIndex(self,idx):
    return int(np.floor(list(idx)[0].start/self.child[0]))

  # def _quickSet(self,vname,partIndex,value):
  #   parts=np.zeros(self.ndata,dtype="int")
  #   parts[0]=partIndex
  #   filepath=self._setFile(vname,parts)
  #   with NetCDF(filepath, "r+") as netcdf:netcdf[vname][:]=value
  #   if not self.parent.localOnly:self.parent.s3.upload(filepath)

  # def _quickGet(self,vname,i):

  #   parts=np.zeros(self.ndata,dtype="int")
  #   parts[0]=int(np.floor(i/self.child[0]))
  #   index=int(i%self.child[0])
  #   filepath=self._getFile(vname,parts)
  #   with NetCDF(filepath, "r") as netcdf:var = netcdf[vname][index]
  #   return var

    # if isinstance(i,int):
    # elif isinstance(i,slice):
    #   parts[0]=0
    #   filepath=self._getFile(vname,parts)
    #   with NetCDF(filepath, "r") as netcdf:var = netcdf[vname][i]

    #   return var

  # TODO: Transpose
  #def transpose(self,vanme):
  # if memmap not exist -> check s3 and download (nca2mem), if not give error
  # call mem2memT
  #

  #TODO: get data to memap

  #TODO: change code to zarr

  # TODO: UploadMemmap
  #def transpose(self,vanme):
  # if memmap not exist -> check s3 and download, if not give error
  #


text:
import os
import wx
import numpy as np

from agilepy.lib_wx.modulegui import ModuleGui
from agilepy.lib_wx.ogleditor import *
from agilepy.lib_base.processes import Process
from agilepy.lib_wx.processdialog import ProcessDialog
import demand
import turnflows
import origin_to_destination_wxgui as odgui

class TripDrawings(Polygons):

    def __init__(self, facilities, parent,   **kwargs):

        Polygons.__init__(self, 'facilitydraws',  parent,
                          name='Facility drawings',
                          linewidth=1,
                          detectwidth=3.0,
                          **kwargs)

        self.delete('vertices')

        self.add(cm.AttrConf('color_facility_default', np.array([0.921875,  0.78125,  0.4375, 1.0], np.float32),
                             groupnames=['options'],
                             perm='wr',
                             metatype='color',
                             name='Default color',
                             info='Default facility color.',
                             ))

        self.set_facilities(facilities)

    def get_netelement(self):
        return self._facilities

    def get_vertices_array(self):
        return self._facilities.shapes.value[self._inds_map]

    def set_facilities(self, facilities):
        # print '\nset_facilities',facilities.get_ids()
        self._facilities = facilities
        if len(self) > 0:
            self.del_rows(self.get_ids())

        ids = self._facilities.get_ids()
        self._inds_map = self._facilities.get_inds(ids)
        self.add_rows(ids=ids)
        self.update()

    def update(self, is_update=True):
        # assumes that arrsy structure did not change
        # print 'FacilityDrawings.update'
        n = len(self)
        self.colors.value[:] = np.ones(
            (n, 4), np.float32) * self.color_facility_default.value
        self.colors_highl.value[:] = self._get_colors_highl(self.colors.value)

        landusetypes = self._facilities.get_landusetypes()
        ids_landusetype = self._facilities.ids_landusetype.value
        for id_landusetype in landusetypes.get_ids():
            #inds = np.flatnonzero(ids_landusetype == id_landusetype)
            #color = landusetypes.colors[id_landusetype]
            #self.colors.value[self._inds_map[inds]] = color
            self.colors.value[self._inds_map[np.flatnonzero(
                ids_landusetype == id_landusetype)]] = landusetypes.colors[id_landusetype]

        if is_update:
            self._update_vertexvbo()
            self._update_colorvbo()

class RouteDrawings(Polygons):

    def __init__(self, zones, parent,   **kwargs):

        Polygons.__init__(self, 'zonedraws',  parent,
                          name='Transport assignment zone drawings',
                          linewidth=4,
                          detectwidth=5.0,
                          **kwargs)

        self.delete('vertices')

        self.add(cm.AttrConf('color_default', np.array([0.3, 0.9, 0.5, 0.8], dtype=np.float32),
                             groupnames=['options'],
                             perm='wr',
                             metatype='color',
                             name='Default color',
                             info='Default facility color.',
                             ))

        self.set_netelement(zones)

    def get_netelement(self):
        return self._zones

    def get_vertices_array(self):
        if self._is_not_synched:
            self._inds_map = self._zones.get_inds(self._zones.get_ids())
            self._is_not_synched = False
        return self._zones.shapes.value[self._inds_map]

    def set_netelement(self, zones):
        # print '\nset_facilities',facilities.get_ids()
        self._zones = zones
        if len(self) > 0:
            self.del_rows(self.get_ids())

        ids = self._zones.get_ids()
        #self._inds_map = self._zones.get_inds(ids)
        self.add_rows(ids=ids)

        # plugins to keep grapgics syncronized with netelements
        zones.shapes.plugin.add_event(cm.EVTADDITEM, self.on_add_element)
        zones.plugin.add_event(cm.EVTDELITEM, self.on_del_element)

        self._id_target = -1
        self.update()

    def update(self, is_update=True):
        # assumes that arrsy structure did not change
        # print 'FacilityDrawings.update'
        n = len(self)
        self.colors.value[:] = np.ones(
            (n, 4), np.float32) * self.color_default.value
        self.colors_highl.value[:] = self._get_colors_highl(self.colors.value)

        if is_update:
            self.update_internal()

    def update_internal(self):
        # print 'update_internal'
        # print '    len(self),len(self._zones)',len(self),len(self._zones)
        self._is_not_synched = True
        self._update_vertexvbo()
        self._update_colorvbo()

    def make_zone(self, shape, color):
        # print 'make_zone shape',shape,type(shape)
        return self._zones.make(shape=shape)

    def on_add_element(self, shapes, ids):
        # print 'on_add_element',shapes.attrname,ids
        if shapes == self._zones.shapes:
            self._id_target = ids[0]
            self.add_row(_id=self._id_target,
                         colors=self.color_default.value,
                         colors_highl=self._get_colors_highl(
                             self.color_default.value)
                         )
            #self._inds_map = self._zones.get_inds(self._zones.get_ids())
            # self._update_vertexvbo()
            # self._update_colorvbo()

    def begin_animation(self, id_target):
        # print 'ZoneDrawings.begin_animation
        # zones.shapes=\n',id_target,self._id_target,
        # self._zones.shapes[id_target]
        if self._id_target == -1:
            self._id_target = id_target
        self._drawobj_anim = self.parent.get_drawobj_by_ident(
            self._ident_drawobj_anim)
        self.id_anim = self._drawobj_anim.add_drawobj(self._zones.shapes[self._id_target],
                                                      self.color_anim.value,
                                                      )
        # print 'begin_animation',self.ident,_id,self._drawobj_anim
        return True

    def end_animation(self, is_del_last_vert=False):
        # print
        # 'ZoneDrawings.end_animation',self.ident,self._id_target,self.id_anim

        # print '  verices =',self._drawobj_anim.vertices[self.id_anim]
        # print '
        # self._drawobj_anim.vertices[self.id_anim]=',self._drawobj_anim.vertices[self.id_anim]
        shape = self._drawobj_anim.vertices[self.id_anim]
        self._zones.shapes[self._id_target] = shape
        self._zones.coords[
            self._id_target] = self._zones.get_coords_from_shape(shape)

        self.del_animation()
        # print '  self.get_vertices_array()=\n',self.get_vertices_array()
        # self._drawobj_anim.del_drawobj(self.id_anim)
        self.update_internal()
        #self._inds_map = self._zones.get_inds(self._zones.get_ids())
        # self._update_vertexvbo()
        # self._update_colorvbo()
        return True

    def del_elem(self, id_zone):
        """
        Deletes an element from network and then in on canvas
        through callback on_del_element
        """
        # print 'del_elem'
        # print '    len(self),len(self._zones)',len(self),len(self._zones)

        self._zones.del_element(id_zone)

    def on_del_element(self, shapes, ids):
        """
        callback from netelement
        """
        # print 'on_del_element',shapes.attrname,ids,self._id_target
        # print '    len(self),len(self._zones)',len(self),len(self._zones)
        # print '  verices =',self._drawobj_anim.vertices[self.id_anim]
        self._is_not_synched = True
        self.del_drawobj(ids[0])
        # print '    len(self),len(self._zones)',len(self),len(self._zones)
        # wx.CallAfter(self.update_internal)
        # self.update_internal()
        # print '  after CallAfter'
        # print '    len(self),len(self._zones)',len(self),len(self._zones)
        return True

    def del_animation(self, is_del_main=False):
        # print 'end_animation',self.ident,_id,self._drawobj_anim
        self._drawobj_anim.del_drawobj(self.id_anim)
        self._drawobj_anim = None

        if is_del_main:
            # self.del_drawobj(self._id_target)
            # delete first element from net, which will
            # call back on_del_netelement where the main drawobj is deleted
            self.del_elem(self._id_target)

        self._id_target = -1
        self.id_anim = -1
        return True

class AddTripTool(AddPolygonTool):

    """
    Mixin for Selection tools for OGL canvas.
    """

    def __init__(self, parent, mainframe=None):
        self.init_common('add_zone', parent, 'Add zone tool',
                         info='Click on canvas to add a zone. Add a vertex with a single click, double click to finish, right click to aboard.',
                         is_textbutton=False,
                         )
        self.init_options()

    def init_options(self):
        pass
        # self.add(cm.AttrConf( 'color',  np.array([0.3,0.9,0.5,0.8], dtype=np.float32),
        #                                groupnames = ['options'],
        #                                perm='rw',
        #                                metatype = 'color',
        #                                name = 'Zone color',
        #                                is_save = True,
        #                                info = 'Zone outline color with format [r,g,b,a]',
        #                                ))

        # self.add(cm.AttrConf( 'color_fill',  np.array([1.0,0.0,0.0,0.8], dtype=np.float32),
        #                                groupnames = ['options'],
        #                                perm='rw',
        #                                metatype = 'color',
        #                                name = 'Fill color',
        #                                is_save = True,
        #                                info = 'Object fill color with format [r,g,b,a]',
        #                                ))

    def set_button_info(self, bsize=(32, 32)):
        # print 'set_button_info select tool'
        self._bitmap = wx.Bitmap(os.path.join(
            IMAGEDIR, 'polygon2_24px.png'), wx.BITMAP_TYPE_PNG)
        self._bitmap_sel = wx.Bitmap(os.path.join(
            IMAGEDIR, 'polygon2_24px.png'), wx.BITMAP_TYPE_PNG)

    def begin_animation(self, event):
        # print 'AddLineTool.begin_animation'
        #self.drawobj_anim, _id, self.ind_vert =  self.get_current_vertexselection()
        self.drawobj_anim = self._canvas.get_drawing().get_drawobj_by_ident(
            'zonedraws')
        self.coord_last = self._canvas.unproject(event.GetPosition())
        #vertices = [list(self.coord_last),list(self.coord_last) ]
        # attention, we need copies here!!
        vertices = [1.0 * self.coord_last, 1.0 * self.coord_last, ]
        # print '  vertices
        # ',vertices#,self.width.get_value(),self.color.get_value(),

        _id = self.drawobj_anim.make_zone(vertices,
                                          color=self.drawobj_anim.color_default.get_value(),
                                          )
        self.ind_vert = 1
        self.drawobj_anim.begin_animation(_id)

        # http://www.wxpython.org/docs/api/wx.Cursor-class.html
        self._canvas.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
        self.is_animated = True

        self.parent.refresh_optionspanel(self)
        return True  # True for redrawing

class WxGui(ModuleGui):

    """Contains functions that communicate between the widgets of the main wx gui
    and the functions of the plugin.
    """

    def __init__(self, ident):
        self._demand = None
        self._init_common(ident,  priority=1000,
                          icondirpath=os.path.join(os.path.dirname(__file__), 'images'))

    def get_module(self):
        return self._demand

    def get_scenario(self):
        return self._mainframe.get_modulegui('coremodules.scenario').get_scenario()

    def get_neteditor(self):
        return self._mainframe.get_modulegui('coremodules.network').get_neteditor()

    def init_widgets(self, mainframe):
        """
        Set mainframe and initialize widgets to various places.
        """
        self._mainframe = mainframe
        #self._neteditor = mainframe.add_view("Network", Neteditor)

        # mainframe.browse_obj(self._module)
        self.make_menu()
        self.make_toolbar()

    def refresh_widgets(self):
        """
        Check through mainframe what the state of the application is
        and reset widgets. For exampe enable/disable widgets
        dependent on the availability of data.
        """
        scenario = self.get_scenario()
        # print 'demand refresh_widgets',scenario.net
        is_refresh = False
        if self._demand != scenario.demand:
            del self._demand
            self._demand = scenario.demand
            is_refresh = True
            # self.get_neteditor().get_toolbox().add_toolclass(AddZoneTool)
            # print '  odintervals',self._demand.odintervals#,self.odintervals.times_start
            # print ' ',dir(self._demand.odintervals)

        elif self._demand.is_modified():
            is_refresh = True

        if is_refresh:
            neteditor = self.get_neteditor()
            canvas = self.get_neteditor().get_canvas()
            drawing = canvas.get_drawing()
            # drawing.del_drawobj_by_ident('facilitydraws')# TODO: could be done better
            # drawing.del_drawobj_by_ident('zonedraws')
            #facilitydrawings = FacilityDrawings(self._landuse.facilities, drawing)
            #zonedrawings = ZoneDrawings(self._landuse.zones, drawing)
            #drawing.add_drawobj(facilitydrawings, layer = 5)
            #drawing.add_drawobj(zonedrawings, layer = 100)
            # canvas.draw()

    def make_menu(self):
        # print 'make_menu'
        menubar = self._mainframe.menubar
        menubar.append_menu('demand')
#-------------------------------------------------------------------------
        menubar.append_menu('demand/vehicles',
                            bitmap=self.get_icon("vehicle_24px.png"),
                            )

        menubar.append_item('demand/vehicles/clear vehicle types',
                            self.on_clear_vtypes,
                            info='Remove all vehicle types .',
                            #bitmap = self.get_icon("route3_24px.png"),
                            )

        menubar.append_item('demand/vehicles/load defaults',
                            self.on_load_vtypes_defaults,
                            info='Load default vehicle types, removing all existing vehicle types.',
                            #bitmap = self.get_icon("route3_24px.png"),
                            )

#-------------------------------------------------------------------------

        menubar.append_menu('demand/Zone-to-zone demand',
                            bitmap=self.get_icon("icon_odm.png"),
                            )
        menubar.append_item('demand/Zone-to-zone demand/add zone-to-zone flows...',
                            self.on_add_odtrips,
                            info='Add or import trips between origin and destination zones, with a certain mode during a certain time interval.',
                            bitmap=self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/Zone-to-zone demand/generate trips from flows',
                            self.on_generate_odtrips,
                            info=self.on_generate_odtrips.__doc__.strip(),
                            #bitmap = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS,wx.ART_MENU),
                            )

        menubar.append_item('demand/Zone-to-zone demand/clear zone-to-zone flows',
                            self.on_clear_odtrips,
                            info='Clear all zone to zone trips.',
                            bitmap=wx.ArtProvider.GetBitmap(
                                wx.ART_DELETE, wx.ART_MENU),
                            )
#-------------------------------------------------------------------------
        menubar.append_menu('demand/turnflows',
                            bitmap=self.get_icon("icon_turnratio.png"),
                            )
        menubar.append_item('demand/turnflows/import turnflows...',
                            self.on_import_turnflows,
                            info='Import turnflows from file.',
                            bitmap=self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/turnflows/normalize turn-probabilities',
                            self.on_normalize_turnprobabilities,
                            #info='Makes sure that sum of turn probabilities from an edge equals 1.',
                            #bitmap = self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/turnflows/turnflows to routes',
                            self.on_turnflows_to_routes,
                            #info='Makes sure that sum of turn probabilities from an edge equals 1.',
                            #bitmap = self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/turnflows/clear all turnflows',
                            self.on_clear_turnflows,
                            info='Clear all turnflows.',
                            bitmap=wx.ArtProvider.GetBitmap(
                                wx.ART_DELETE, wx.ART_MENU),
                            )

#-------------------------------------------------------------------------

        menubar.append_menu('demand/trips and routes',
                            bitmap=self.get_icon("trip3_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/route',
                            self.on_route,
                            info='Route current trips and create SUMO route xml.',
                            bitmap=self.get_icon("route3_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/export trips to SUMO xml...',
                            self.on_export_sumotrips,
                            info='Export all trips to SUMO XML format.',
                            bitmap=self.get_icon("Document_Export_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/export routes to SUMO xml...',
                            self.on_export_sumoroutes,
                            info='Export all trips to SUMO XML format.',
                            bitmap=self.get_icon("Document_Export_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/import trips from SUMO xml...',
                            self.on_import_trips_xml,
                            info=self.on_import_trips_xml.__doc__.strip(),
                            bitmap=self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/import trips with routes from SUMO xml...',
                            self.on_import_triproutes_xml,
                            info=self.on_import_triproutes_xml.__doc__.strip(),
                            bitmap=self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/import routes to existing trips from SUMO xml...',
                            self.on_import_routes_alternative_xml,
                            info=self.on_import_routes_alternative_xml.__doc__.strip(),
                            bitmap=self.get_icon("Document_Import_24px.png"),
                            )

        menubar.append_item('demand/trips and routes/clear all trips and routes',
                            self.on_clear_trips,
                            info='Clear all trips with respective routes.',
                            bitmap=wx.ArtProvider.GetBitmap(
                                wx.ART_DELETE, wx.ART_MENU),
                            )

        menubar.append_item('demand/trips and routes/clear all routes',
                            self.on_clear_routes,
                            info='Clear all routes, trips will remain.',
                            bitmap=wx.ArtProvider.GetBitmap(
                                wx.ART_DELETE, wx.ART_MENU),
                            )

#-------------------------------------------------------------------------
        if 0:
            menubar.append_menu('demand/virtual population',
                                bitmap=self.get_icon(
                                    "icon_virtualpopulation.jpg")
                                )
            menubar.append_item('demand/virtual population/create from O-D matrix',
                                self.on_create_pop_from_ods,
                                info='Generates population from O-D matrix data.',
                                #bitmap = self.get_icon("Document_Import_24px.png"),
                                )
            menubar.append_item('demand/virtual population/create plans for privates',
                                self.on_make_plans_private,
                                info='create plans for private transport.',
                                #bitmap = self.get_icon("Document_Import_24px.png"),
                                )

            menubar.append_item('demand/virtual population/Export plans to SUMO xml...',
                                self.on_write_plans_to_sumoxml,
                                info='Export all plans to SUMO routes XML format.',
                                #bitmap = self.get_icon("Document_Import_24px.png"),
                                )

# (self._menuitem_draw_route, id_item,) = menubar.append_item(
##            'plugins/traces/draw selected route in network',
# self.on_renew_objectbrowser,
##            info='Enable visualization of routes in network tab. Double-click on trace in table.',
# check=True)
# self._menuitem_draw_route.Check(False)
##
# (self._menuitem_plot_route, id_item,) = menubar.append_item(
##            'plugins/traces/add plot selected route',
# self.on_renew_objectbrowser,
##            info='Enable adding of routes to graphics in trace tab. Double-click on trace in table.',
# check=True)
# self._menuitem_plot_route.Check(False)
##
    def on_clear_vtypes(self, event=None):
        self._demand.vtypes.clear_vtypes()
        self._mainframe.browse_obj(self._demand.vtypes)
        if event:
            event.Skip()

    def on_load_vtypes_defaults(self, event=None):
        self._demand.vtypes.clear_vtypes()
        self._demand.vtypes.add_vtypes_default()
        self._mainframe.browse_obj(self._demand.vtypes)
        if event:
            event.Skip()

    def on_clear_trips(self, event=None):
        self._demand.trips.clear_trips()
        self._mainframe.browse_obj(self._demand.trips)
        if event:
            event.Skip()

    def on_clear_routes(self, event=None):
        self._demand.trips.clear_routes()
        self._mainframe.browse_obj(self._demand.trips)
        if event:
            event.Skip()

    def on_create_pop_from_ods(self, event=None):
        """
        Generates virtual population from ODM matrix.
        """
        self._demand.virtualpop.create_pop_from_ods()
        self._mainframe.browse_obj(self._demand.virtualpop)
        event.Skip()

    def on_make_plans_private(self, event=None):
        """
        Generates virtual population from ODM matrix.
        """
        self._demand.virtualpop.make_plans_private()
        self._mainframe.browse_obj(self._demand.virtualpop)
        event.Skip()

    def on_write_plans_to_sumoxml(self, event=None):
        # print 'on_write_plans_to_sumoxml'
        dirpath = self.get_scenario().get_workdirpath()
        defaultFile = self.get_scenario().get_rootfilename() + '.rou.xml'
        wildcards_all = 'All files (*.*)|*.*|XML files (*.xml)|*.xml'
        dlg = wx.FileDialog(None, message='Write plans to SUMO rou.xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.virtualpop.export_trips_xml(filepath)

        event.Skip()

    def on_add_odtrips(self, event=None):
        """
        Opend odm wizzard
        """
        dlg = odgui.AddOdDialog(self._mainframe, self._demand.odintervals)
        dlg.Show()
        dlg.MakeModal(True)
        self._mainframe.browse_obj(self._demand.odintervals)
        # self.scenariopanel.refresh_panel(self.scenario)

    def on_generate_odtrips(self, event=None):
        """
        Generates trips from origin to destination zone from current OD matrices.
        """
        self._demand.odintervals.generate_trips()
        self._mainframe.browse_obj(self._demand.trips)
        event.Skip()

    def on_clear_odtrips(self, event=None):
        """
        Generates trips from origin to destination zone from current OD matrices.
        """
        self._demand.odintervals.clear_od_trips()
        self._mainframe.browse_obj(self._demand.odintervals)
        event.Skip()

    def on_import_turnflows(self, event=None):
        tfimporter = turnflows.TurnflowImporter(self._demand.turnflows,
                                                logger=self._mainframe.get_logger()
                                                )
        dlg = ProcessDialog(self._mainframe, tfimporter)

        dlg.CenterOnScreen()

        # this does not return until the dialog is closed.
        val = dlg.ShowModal()
        # print '  val,val == wx.ID_OK',val,wx.ID_OK,wx.ID_CANCEL,val == wx.ID_CANCEL
        # print '  status =',dlg.get_status()
        if dlg.get_status() != 'success':  # val == wx.ID_CANCEL:
            # print ">>>>>>>>>Unsuccessful\n"
            dlg.Destroy()

        if dlg.get_status() == 'success':
            # print ">>>>>>>>>successful\n"
            # apply current widget values to scenario instance
            dlg.apply()
            dlg.Destroy()
            self._mainframe.browse_obj(self._demand.turnflows)
            #del self._scenario
            #self._scenario = scenariocreator.get_scenario()
            # self._scenario.import_xml()
            # this should update all widgets for the new scenario!!
            # print 'call self._mainframe.refresh_moduleguis()'
            # self._mainframe.refresh_moduleguis()

        if event:
            event.Skip()

    def on_normalize_turnprobabilities(self, event=None):
        """
        Makes sure that sum of turn probabilities from an edge equals 1.
        """
        self._demand.turnflows.normalize_turnprobabilities()
        self._mainframe.browse_obj(self._demand.turnflows)
        if event:
            event.Skip()

    def on_clear_turnflows(self, event=None):
        """Generates routes, based on flow information and turnflow probabilities.
        This function will apply the JTROUTER for each transport mode separately.
        """
        self._demand.turnflows.clear_turnflows()
        self._mainframe.browse_obj(self._demand.turnflows)
        if event:
            event.Skip()

    def on_turnflows_to_routes(self, event=None):
        """Generates routes, based on flow information and turnflow probabilities.
        This function will apply the JTROUTER for each transport mode separately.
        """
        self._demand.turnflows.turnflows_to_routes(is_clear_trips=False)
        self._mainframe.browse_obj(self._demand.trips)
        if event:
            event.Skip()

    def on_route(self, event=None):
        """Generates routes from current trip info.
        """
        # self._demand.trips.clear_routes()
        self._demand.trips.route(is_export_net=True, is_export_trips=True)
        self._mainframe.browse_obj(self._demand.trips)
        #filepath = self._demand.trips.get_routefilepath()
        #defaultFile = os.path.basename(filepath)
        #dirpath = os.path.dirname(filepath)
        #wildcards_all = 'XML route files (*rou.xml)|*rou.xml|All files (*.*)|*.*'
        # dlg = wx.FileDialog(None, message='Write routes to SUMO xml',
        #                    defaultDir=dirpath,
        #                    defaultFile=defaultFile,
        #                    wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        # if dlg.ShowModal() == wx.ID_OK:
        #    filepath = dlg.GetPath()
        #
        # else:
        #    return
        #
        #filepath = self._demand.trips.route(routefilepath = None,is_export_net = True, is_export_trips = True)

        # if filepath is not None:
        #     print 'Successfully routed to %s.'%filepath
        #    self._demand.import_routes_xml( filepath,  is_clear_trips = False, is_add = True)

    def on_import_trips_xml(self, event=None):
        """Select xml file and import new trips.
        New trips with associated routes will generated.
        """
        filepath = self._demand.trips.get_routefilepath()
        defaultFile = os.path.dirname(filepath)
        dirpath = os.path.dirname(filepath)
        wildcards_all = 'XML trip files (*.trip.xml)|*.trip.xml|All files (*.*)|*.*'
        dlg = wx.FileDialog(None, message='Import trips and routes from SUMO xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.trips.import_trips_xml(filepath, is_clear_trips=False,
                                            is_generate_ids=True)
        self._mainframe.browse_obj(self._demand.trips)

    def on_import_triproutes_xml(self, event=None):
        """Select xml file and import trips and routes.
        New trips with associated routes will generated.
        """
        filepath = self._demand.trips.get_routefilepath()
        defaultFile = os.path.dirname(filepath)
        dirpath = os.path.dirname(filepath)
        wildcards_all = 'XML route files (*.rou.xml)|*.rou.xml|All files (*.*)|*.*'
        dlg = wx.FileDialog(None, message='Import trips and routes from SUMO xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.trips.import_routes_xml(filepath, is_clear_trips=False,
                                             is_generate_ids=True,
                                             is_add=False)
        self._mainframe.browse_obj(self._demand.trips)

    def on_import_routes_alternative_xml(self, event=None):
        """Select xml file and import routes.
        Routes will added as route alternative to existing trips.
        """
        filepath = self._demand.trips.get_routefilepath()
        defaultFile = os.path.dirname(filepath)
        dirpath = os.path.dirname(filepath)
        wildcards_all = 'XML route files (*.rou.xml)|*.rou.xml|All files (*.*)|*.*'
        dlg = wx.FileDialog(None, message='Import trips and routes from SUMO xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.OPEN | wx.CHANGE_DIR
                            )

        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.trips.import_routes_xml(filepath, is_clear_trips=False,
                                             is_generate_ids=False,
                                             is_add=True)
        self._mainframe.browse_obj(self._demand.trips)

    def on_export_sumotrips(self, event=None):
        # print 'on_export_sumotrips'
        filepath = self._demand.trips.get_tripfilepath()
        defaultFile = os.path.dirname(filepath)
        dirpath = os.path.dirname(filepath)
        wildcards_all = '*.*|XML trip files (*.trip.xml)|*.trip.xml|All files (*.*)'
        dlg = wx.FileDialog(None, message='Write trips to SUMO xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.trips.export_trips_xml(filepath)

    def on_export_sumoroutes(self, event=None):
        # print 'on_export_sumotrips'
        filepath = self._demand.trips.get_routefilepath()
        defaultFile = os.path.basename(filepath)
        dirpath = os.path.dirname(filepath)
        wildcards_all = 'XML route files (*rou.xml)|*rou.xml|All files (*.*)|*.*'
        dlg = wx.FileDialog(None, message='Write routes to SUMO xml',
                            defaultDir=dirpath,
                            defaultFile=defaultFile,
                            wildcard=wildcards_all, style=wx.SAVE | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            filepath = dlg.GetPath()

        else:
            return

        self._demand.trips.export_routes_xml(filepath)

ext: py

max_stars_repo_name: iltempe/osmosi

max_issues_repo_name: iltempe/osmosi

max_forks_repo_name: iltempe/osmosi

reason: import numpy


text:
"""
@authors: Filip Maciejewski, Oskar Słowik, Tomek Rybotycki
@contact: filip.b.maciejewski@gmail.com
"""
import numpy as np
from typing import List
import copy

def get_1q_stochastic_matrix(p10: float, p01: float) -> np.ndarray:
    """ Get single-qubit stochastic map from error probabilities.

    :param p10: probability of getting outcome "1" if input was |0>
    :param p01: probability of getting outcome "0" if input was |1>

    :return: single-qubit stochastic matrix
    """

    return np.array([[1 - p01, p10], [p01, 1 - p10]])

def get_random_stochastic_matrix_1q(p10_range: list,
                                    p01_range: list,
                                    distribution_name='uniform') -> np.ndarray:
    """ Get random single-qubit stochastic map with error probabilities chosen randomly from provided
    ranges

    :param p10_range: [min,max] value of probability of getting outcome "1" if input was |0>
    :param p01_range: [min,max] value of probability of getting outcome "0" if input was |1>
    :param distribution_name: stochasticity_type of distribution
    possible options:
    - "uniform"

    #todo FBM: add Gaussian types

    :return: single-qubit stochastic matrix
    """

    if distribution_name == 'uniform':
        p10 = np.random.uniform(*p10_range)
        p01 = np.random.uniform(*p01_range)
    else:
        raise ValueError('Wrong distribution name')

    return get_1q_stochastic_matrix(p10, p01)

def add_physical_perturbation_1q(vector: np.ndarray,
                                 perturbation_range):
    changed_p10 = False
    perturbed_vector = copy.deepcopy(vector)
    while not changed_p10:
        # try to add perturbation to probability vector that is still physical
        magnitude_p10, sign_p10 = np.random.uniform(*perturbation_range), np.random.choice([-1, 1])
        p0 = vector[0]
        p1 = 1 - p0

        if 0 <= (p0 + sign_p10 * magnitude_p10) <= 1 and 0 <= (p1 - sign_p10 * magnitude_p10) <= 1:
            perturbed_vector[0] = p0 + sign_p10 * magnitude_p10
            perturbed_vector[1] = p1 - sign_p10 * magnitude_p10
            changed_p10 = True
        elif 0 <= (p0 - sign_p10 * magnitude_p10) <= 1 and 0 <= (p1 + sign_p10 * magnitude_p10) <= 1:
            perturbed_vector[0] = p0 - sign_p10 * magnitude_p10
            perturbed_vector[1] = p1 + sign_p10 * magnitude_p10
            changed_p10 = True
        else:
            pass

    return perturbed_vector

def perturb_stochastic_matrix_1q(stochastic_matrix: np.ndarray,
                                 deviation_p10_range: List[float],
                                 deviation_p01_range: List[float]) -> np.ndarray:
    perturbed_matrix = copy.deepcopy(stochastic_matrix)

    first_column_perturbed = add_physical_perturbation_1q(perturbed_matrix[:, 0], deviation_p10_range)
    second_column_perturbed = add_physical_perturbation_1q(perturbed_matrix[:, 1], deviation_p01_range)

    perturbed_matrix[:, 0] = first_column_perturbed[:]
    perturbed_matrix[:, 1] = second_column_perturbed[:]

    return perturbed_matrix


text:
"""
Display line segments. Can be useful e.g. for visializing vector fields.
"""

import numpy as np
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx

canvas = WgpuCanvas()
renderer = gfx.WgpuRenderer(canvas)

scene = gfx.Scene()

x = np.linspace(20, 620, 200, dtype=np.float32)
y = np.sin(x / 10) * 100 + 200

positions = np.column_stack([x, y, np.zeros_like(x)])
colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)
geometry = gfx.Geometry(positions=positions, colors=colors)

# Also see LineSegmentMaterial and LineThinSegmentMaterial
material = gfx.LineSegmentMaterial(
    thickness=6.0, color=(0.0, 0.7, 0.3, 0.5), vertex_colors=True
)
line = gfx.Line(geometry, material)
scene.add(line)

camera = gfx.ScreenCoordsCamera()

if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    run()


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

"""
Basic color bar alterations

"""

__title__ = "Replace volors"
__author__ = "Arden Burrell"
__version__ = "1.0 (18.02.2018)"
__email__ = "arden.burrell@gmail.com"

# modules
import numpy as np
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.colors as mpc
import webcolors as wc

import ipdb

def _ColorConversion(color):
    """
    A better converter for getting the color into RGB
    args:
        color in hex or named color format
    returns R G B """
    try:
        if  type(color).__name__ == "tuple": # color is RGB
            cR = color[0]
            cG = color[1]
            cB = color[2]
        elif mpc.is_color_like(color):
            try:
                cR, cG, cB = wc.hex_to_rgb(color)
            except ValueError:
                try:
                    cR, cG, cB = wc.name_to_rgb(color)
                except ValueError:
                    cR, cG, cB, _ = mpc.to_rgba_array(color)[0] * 255
        return cR, cG, cB
    except UnboundLocalError:
        raise ValueError(
            "Color cannot be converted into RGB: " + color + "is not a valid color")

# def _segmented(cmap, ShiftInd, cdict):

    # pass

def ReplaceColor(cmap, color = 'w', start = 0., locpoint = 0.5, stop = 1.0,
    name = 'centered', alpha = 1.0, segmented=True):
    """ Replace the colour of a value standing anywhere in the new cmap
    (relatively to the two extremes start & stop or min & max) with whichever
    value / colour of the input cmap (by default the midpoint).
    The locpoint value cannot be the min or max (start or stop).
    Args:
        cmap An existing color map,
        color: the color to replace, can be hex, RGB or named color. defualt = white
    """

    # ========== convert the colors into RGB ==========
    cA = alpha
    cR, cG, cB = _ColorConversion(color)

    # declare a colour + transparency dictionary
    cdict={'red':[], 'green':[], 'blue':[], 'alpha':[]}

    # regular index to compute the colors
    # pdb.set_trace()
    this_zero = 1.e-9

    RegInd = np.linspace(start, stop, cmap.N)

    # shifted index to match what the data should be centered on
    ShiftInd = np.hstack([np.linspace(0., locpoint, cmap.N/2, endpoint = False),np.linspace(locpoint, 1., cmap.N/2, endpoint = True)])
    # ShiftInd = np.hstack([np.linspace(0., 0.5, cmap.N/2, endpoint = False),0.5,np.linspace(0.5+this_zero, 1., cmap.N/2, endpoint = True)])
    # norm = mpc.BoundaryNorm(ShiftInd, cmap)
    # return norm
    # pdb.set_trace()
    # associate the regular cmap's colours
    for RI, SI in zip(RegInd, ShiftInd):
        if SI == locpoint: #replace the locpoint with
            cdict['red'].append((SI, cR, cR))
            cdict['green'].append((SI, cG, cG))
            cdict['blue'].append((SI, cB, cB))
            cdict['alpha'].append((SI, cA, cA))
        else:
            # get standard indexation of red, green, blue, alpha
            r, g, b, a = cmap(RI)
            cdict['red'].append((SI, r, r))
            cdict['green'].append((SI, g, g))
            cdict['blue'].append((SI, b, b))
            cdict['alpha'].append((SI, a, a))

    # pdb.set_trace()
    return LinearSegmentedColormap(name, cdict)
    # return mpc.ListedColormap(name, cdict)

max_stars_repo_name: ArdenB/fireflies

max_issues_repo_name: ArdenB/fireflies

max_forks_repo_name: ArdenB/fireflies


text:
# Author : Ali Snedden
# Date   : 4/12/20
# License: MIT
# Purpose:
#   This code attmpts to follow https://www.youtube.com/watch?v=gxAaO2rsdIs and try to
#   mimic his plots
#
#
# Notes :
#
# References :
#   1. https://idi.osu.edu/assets/pdfs/covid_response_white_paper.pdf
#   2. https://mbi.osu.edu/events/seminar-grzegorz-rempala-mathematical-models-epidemics-tracking-coronavirus-using-dynamic
#
# Future:
#
#
#
import sys
import numpy as np
import time
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.patches import Circle
from error import exit_with_error
from scipy import optimize
import random
random.seed(42)     # Change later
from classes import AGENT

def print_help(ExitCode):
    """
    ARGS:
    RETURN:
    DESCRIPTION:
    DEBUG:
    FUTURE:
    """
    sys.stderr.write(
        "python3 ./src/simulating_agent_model_2D.py [quarantine]\n"
        "   [quarantine] : optional, puts infected in quarantine symptoms present\n"
        "      \n"
        "      \n"
        "   After running, create a movie via : \n"
        "       ffmpeg -framerate 4 -pattern_type glob -i 'tmp/*.png' -c:v libx264 out.mp4\n"
        "      \n"
        "   To Run: \n"
        "       source ~/.local/virtualenvs/python3.7/bin/activate\n")
    sys.exit(ExitCode)

def displacement(Agent1=None, Agent2=None):
    """
    ARGS:
    RETURN:
    DESCRIPTION:
        Gives displacement between two agents.
    DEBUG:
    FUTURE:
    """
    if(type(Agent1) == AGENT):
        x1=Agent1.posL[0]
        y1=Agent1.posL[1]
    elif(type(Agent1) == list):
        x1=Agent1[0]
        y1=Agent1[1]
    else:
        exit_with_error("ERROR!!! {} not a supported type\n".format(type(Agent1)))

    if(type(Agent2) == AGENT):
        x2=Agent2.posL[0]
        y2=Agent2.posL[1]
    elif(type(Agent2) == list):
        x2=Agent2[0]
        y2=Agent2[1]
    else:
        exit_with_error("ERROR!!! {} not a supported type\n".format(type(Agent2)))

    return np.sqrt( (x1-x2)**2 + (y1-y2)**2)

def move_agent(Agent=None, AgentL=None, InfectDist=None, Quarantine=None, DeltaT=None):
    """
    ARGS:
        Agent      : AGENT, The AGENT whose trajectory we are computing
        AgentL     : List of AGENTs, Used for avoiding quarantined AGENTs
        InfectDist : Float, Infectiousness distance. Used as radius around
                     quarantined, infected AGENTs
        Quarantine : Boolean, do we quarantine infected agents?
        DeltaT     : Time interval.
    RETURN:
    DESCRIPTION:
        Moves agent. Applies implied boundary conditions [0,0,0] -> [1,1,1]
        If quarantine option used, I do an approximation of reality.

        Reasons :
            1. Trying to avoid multiple infected individuals gets logically complicated
               to code.
            2. Avoiding the bounds [0,0,0] -> [1,1,1] and trying to avoid an infected
               individual adds yet another level of logical complexity.

        Solution :
            1. I only avoid the first infected individual that I encounter and then I test
               the bounds.

    DEBUG:
    FUTURE:
    """
    xi= Agent.posL[0]
    yi= Agent.posL[1]
    vx= Agent.vL[0]
    vy= Agent.vL[1]
    v = np.sqrt(vx**2 + vy**2)
    xf= Agent.vL[0] * DeltaT + Agent.posL[0]
    yf= Agent.vL[1] * DeltaT + Agent.posL[1]
    r = InfectDist      # Radius about quarantined individual

    # Check if quarantined agent nearby.
    #   1. Only consider 1st quarantine encountered b/c it could be potentially very
    #      challenging to solve for preventing a susceptible from completely avoiding _all_
    #      quarantined agents.
    #   2.
    #   3.

    # Quarantined agents can't move.
    if(Agent.quarantine == True and Agent.immune == True):
        return

    if(Quarantine == True and Agent.infected == False):
        # displacement, Agent final - initial
        dfi = np.sqrt((xf-xi)**2 + (yf-yi)**2)
        # Get line function,     y = mx + b
        m   = (yf-yi)/(xf-xi)  # Slope of line
        b   = yf - m*xf        # Pick a point on the line, solve for intercept

        for agent in AgentL:
            # Must be quarantined to avoid
            if(agent.quarantine == False):
                continue
            xc = agent.posL[0]
            yc = agent.posL[1]
            # displ, quarntined - Agent final
            dfq = np.sqrt((xc-xf)**2 + (yc-yf)**2)
            # displ, quarntined - Agent initial
            diq = np.sqrt((xc-xi)**2 + (yc-yi)**2)
            # There might be a collision -
            #   It is possible that both dfq and diq are
            #   outside of radius, yet the trajectory passes through it

            #### Maybe easier if we just say if dfi < 3*r and dfq < 3*r
            if(dfq <= r or diq <= r):       # This is gross and imprecise.
                # Get circle of exclusion line, recall
                #   0 = (x - xc)^2 + (y - yc)^2 - r^2
                #   xc,yc = x,yposition of center of circle
                def f(x):
                    y = m*x+b
                    return( (x-xc)**2 + (y-yc)**2 - r**2)
                ### With many root solvers, it requires that f(a)*f(b) < 0. However,
                ### fsolve doesn't care.  It just needs bounds to look
                xroots = optimize.fsolve(f, [xc-r, xc+r])
                # If there are two roots, which do i pick? Pick closest to Agent
                if(len(xroots) == 2):
                    x1=xroots[0]
                    y1= m * x1 + b
                    d1=displacement(Agent,[x1,y1])
                    x2=xroots[1]
                    y2= m * x2 + b
                    d2=displacement(Agent,[x2,y2])
                    # Use 1st root b/c it is closer
                    if(d1<d2):
                        x=x1
                        y=y1
                    else:
                        x=x2
                        y=y2

                elif(len(xroots) == 1):
                    x = xroot
                    y = m * xroot + b
                else:
                    exit_with_error("ERROR!!! I don't understand how there can "
                                    "be more than 2 roots!\n")
                rx = x-xc
                ry = y-yc
                rvect = [rx, ry]
                # Find line perpendicular to rvect, i.e. tangent to the circle, call it 't'
                # Let :
                #       t     = a  \hat(i) + b  \hat(j)
                #       rvect = rx \hat(i) + ry \hat(j)
                # Solve equation :
                #       t \dot rvect = 0
                #       (a \hat(i) + b \hat(j))  \dot (rx \hat(i) + ry \hat(j))  = 0
                #       a * rx + b * ry = 0
                #       a = -(b * ry) / rx
                ### Verticle line
                if(rx == 0):
                    a = 0
                    b = 1
                    alpha = np.pi / 2   # 90deg, Angle between tangent and horizontal
                ### Horizontal line
                elif(ry == 0):
                    a = 1
                    b = 0
                    alpha = 0           # 0deg, Angle between tangent and horizontal
                ### Exerything else
                else:
                    b = 1
                    a = -b * ry / rx
                    alpha = np.arctan(b/a) # 0deg, Angle between tangent and horizontal
                    if(np.isnan(alpha)):
                        exit_with_error("ERROR!!! np.arctan({}/{}) == nan\n".format(b/a))

                #if(np.isclose(np.sqrt(rvect[0]*rvect[0]+rvect[1]*rvect[1]), r) == False):
                #    exit_with_error("ERROR!!! I don't know how |rvect| != |r|\n")

                # Now get angle between rvector and velocity vector
                theta = np.arccos( (vx*rvect[0] + vy*rvect[1]) /
                                   np.sqrt((vx**2 + vy**2)*(rvect[0]**2 + rvect[1]**2)))
                # Angle of reflection w/r/t to the tangent line on circle
                phi = theta - np.pi / 2.0
                #phi = theta
                vx = v * np.sin(phi) * np.cos(alpha)
                vy = v * np.cos(phi * np.sin(alpha))
                #print("{:<.5f} {:<.5f} {:<.5f}".format(vx,vy,phi))
                xf = vx * DeltaT + xi
                yf = vy * DeltaT + yi
                Agent.vL[0] = vx
                Agent.vL[1] = vy
                break
                #else:
                #    continue

    # Check bounds
    if(xf < 0):
        xf = -1.0 * xf
        Agent.vL[0] = -1.0 * Agent.vL[0]
    if(yf < 0):
        yf = -1.0 * yf
        Agent.vL[1] = -1.0 * Agent.vL[1]
    if(xf > 1.0):
        d = xf - 1.0
        xf = xf - d
        Agent.vL[0] = -1.0 * Agent.vL[0]
    if(yf > 1.0):
        d = yf - 1.0
        yf= yf - d
        Agent.vL[1] = -1.0 * Agent.vL[1]
    # Adjust Position
    Agent.posL[0] = xf
    Agent.posL[1] = yf
    # Adjust velocity
    dvx = random.uniform(-1,1)/100.0 # Want crossing time to be about 25 steps
    dvy = random.uniform(-1,1)/100.0
    Agent.vL[0] += dvx
    Agent.vL[1] += dvy

def main():
    """
    ARGS:
    RETURN:
        1. Creates images. Turn into moving using ffmpeg, e.g.
           ffmpeg -framerate 4 -pattern_type glob -i 'output/*.png' -c:v libx264 out.mp4
    DESCRIPTION:
    DEBUG:
    FUTURE:
        1. Add option to fit only a specific section of data.
        2. Make main loop NOT O(N^2). Maybe organize by position on a grid.
    """
    # Check Python version
    nArg = len(sys.argv)
    # Use python 3
    if(sys.version_info[0] != 3):
        exit_with_error("ERROR!!! Use Python 3\n")
    # Get options
    if(len(sys.argv) > 1 and "-h" in sys.argv[1]):
        print_help(0)
    elif(nArg != 1 and nArg != 2):
        print_help(1)
    elif(nArg == 1):
        quarantine = False
    elif(nArg == 2 and sys.argv[1] == "quarantine"):
        quarantine = True

    startTime = time.time()
    print("{} \n".format(sys.argv),flush=True)
    print("   Start Time : {}".format(time.strftime("%a, %d %b %Y %H:%M:%S ",
                                       time.localtime())),flush=True)
    ### Parameters to Change
    N     = 200             # Number of Agents
    nDays = 100             # number of days in simulation
    dt    = 0.25            # number of steps in a day, total steps = nDays / dt
    nStep = int(nDays / dt)
    infectTime = 14 / dt    # Infection time in units of steps
    asymptomaticTime = 5 / dt    # Infection time in units of steps
    prob  = 0.125           # Probability of infecting agent within infectDist
    infectDist = 0.05       # Distance person must be within to get infected
    critMass = 20           # Number of people before instituting a quarantine
    nDayAsymptAndInfec = 2  # Number days asymptomatic AND infectious
    agentL = []
    nSuscL = []             # Number of susceptible per step
    nInfL  = []             # Number of infected per step
    nRmL  = []              # Number of removed per step
    startQuarantine = False
    print("Parameters : \n"
          "     N = {}\n"
          "     prob = {}\n"
          "     nDays = {}\n"
          "     nStep= {}\n"
          "     infectDist= {}\n"
          "     critMass= {}\n"
          "     nDayAsymptAndInfec = {}\n"
          "     quarantine= {}\n".format(N,prob,nDays,nStep,infectDist,critMass,
          nDayAsymptAndInfec,quarantine))

    # Initialize agents
    for n in range(N):
        agent = AGENT(n)
        agentL.append(agent)

    # Infect 1 agent
    agentL[0].infected=True
    agentL[0].start=0

    # Simulation - O(N**2)
    for step in range(nStep):
        # Use plotting
        sxL = []    # Susceptible xL
        syL = []
        ixL = []    # Infected xL
        iyL = []
        rxL = []    # Removed xL
        ryL = []
        ### Only if quarentining infected individuals
        if(quarantine == True):
            qxL = []    # quarantine
            qyL = []

        for i in range(len(agentL)):
            agent = agentL[i]
            # Generate for plot
            #xL.append(agent.posL[0])
            #yL.append(agent.posL[1])
            # Susceptible
            if(agent.infected == False and agent.immune == False):
                sxL.append(agent.posL[0])
                syL.append(agent.posL[1])
            # Infected
            if(agent.infected == True):
                ixL.append(agent.posL[0])
                iyL.append(agent.posL[1])
            # Removed
            if(agent.immune == True):
                rxL.append(agent.posL[0])
                ryL.append(agent.posL[1])

            ### Only if quarentining infected individuals
            if(quarantine == True):
                if(len(ixL) == critMass):      # Critical mass to quarantine
                    startQuarantine = True
                # Quarentine after 2 days of infeciousness
                if((agent.infected == True and
                    step - agent.start - asymptomaticTime >= nDayAsymptAndInfec/dt) and
                    startQuarantine == True
                ):
                    agent.quarantine = True
                    agent.vL[0] = 0
                    agent.vL[1] = 0
                if(agent.quarantine == True):
                    qxL.append(agent.posL[0])
                    qyL.append(agent.posL[1])
                    # continue

            # Move
            move_agent(agent,agentL,infectDist,quarantine,dt)

            # 'Removed' Group. If survived, adjust time.
            if(step - agent.start > infectTime and agent.infected == True):
                agent.infected = False
                agent.immune = True
                if(quarantine == True):
                    agent.quarantine = False

            # Susceptible Group - Check if infected
            if(agent.infected == False or (step - agent.start < asymptomaticTime) or
               agent.quarantine == True
            ):
                continue

            # Infectious Group - Try to infect someone
            for j in range(len(agentL)):
                # Skip self
                if(i==j or agentL[j].immune == True or agentL[j].infected == True or
                   agentL[j].quarantine == True
                ):
                    continue
                d = displacement(agent, agentL[j])
                if(d <= infectDist):
                    rng = random.uniform(0,1)
                    if(rng < prob):
                        agentL[j].infected=True
                        agentL[j].start = step
                        agent.nInfect += 1

        # Plot
        fig, ax = plt.subplots()
        ax.scatter(sxL, syL, c="black", marker=".", label="Susceptible")
        ax.scatter(ixL, iyL, c="red",   marker="^", label="Infected")
        # Add infection radius
        for i in range(len(ixL)):
            circle = Circle((ixL[i], iyL[i]), radius=infectDist)
            circle.set_edgecolor("red")
            circle.set_facecolor("none")
            ax.add_artist(circle)
        ax.scatter(rxL, ryL, c="blue",  marker="s", label="Removed")
        ax.grid(True)
        #ax.legend([".", "^", "s"], ["Removed","Infected","Susceptible"], loc="best")
        ax.legend(loc=1)
        ax.set_xlim((0,1))
        ax.set_ylim((0,1))
        # State R0
        RL = np.asarray([a.nInfect for a in agentL if(a.immune == True)])
        if(np.sum(RL) > 0):
            R = np.mean(RL)
        else:
            R = 0
        ax.set_title("Critical Fraction : {:<.1f}%\n{:<.2f} days, R = {:<.2f}".format(critMass/N*
100,step*dt,R))
        #plt.show()
        plt.savefig("tmp/{:04d}.png".format(step))
        plt.close('all')

        # Record number susceptible, infected and removed
        nSuscL.append(len(sxL))
        nInfL.append(len(ixL))
        nRmL.append(len(rxL))

    # Generate plot of SIR vs. Time
    fig, ax = plt.subplots()
    ax.plot(range(nStep), nSuscL, c="black", label="Susceptible")
    ax.plot(range(nStep), nInfL, c="red", label="Infected")
    ax.plot(range(nStep), nRmL, c="blue", label="Removed")
    ax.legend(loc=1)
    ax.xaxis.set_ticks([d*1.0/dt for d in range(nDays) if(d%10 == 0)])
    ax.set_xticklabels([d for d in range(nDays) if(d%10 == 0)])
    ax.set_title("Critical Fraction : {:<.1f}%\nSusceptible-Infected-Removed vs. Time".format(critMass/N*100,step*dt))
    ax.set_xlabel("Time (days)")
    ax.set_ylabel("Number of Agents")
    plt.savefig("tmp/SIR_vs_time.png")
    plt.close('all')

    print("Ended : %s"%(time.strftime("%D:%H:%M:%S")))
    print("Run Time : {:.4f} h".format((time.time() - startTime)/3600.0))
    sys.exit(0)

if __name__ == "__main__":
    main()

reason: import numpy,from scipy


text:
from parcels import FieldSet, Field, ParticleSet, JITParticle, ErrorCode, AdvectionRK4, BrownianMotion2D, Variable
import datetime
from datetime import timedelta as delta
from glob import glob
import numpy as np

def set_fields(hycomfiles, stokesfiles):
    dimensions = {'lat': 'Latitude', 'lon': 'Longitude', 'time': 'MT', 'depth': 'Depth'}
    bvelfile = '/Users/erik/Codes/ParcelsRuns/Hydrodynamics_AuxiliaryFiles/Hycom/boundary_velocities.nc'
    MaskUvel = Field.from_netcdf(bvelfile, 'MaskUvel', dimensions)
    MaskVvel = Field.from_netcdf(bvelfile, 'MaskVvel', dimensions)

    uhycom = Field.from_netcdf(hycomfiles, 'u', dimensions, fieldtype='U')
    vhycom = Field.from_netcdf(hycomfiles, 'v', dimensions, fieldtype='V', grid=uhycom.grid, timeFiles=uhycom.timeFiles)

    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    uuss = Field.from_netcdf(stokesfiles, 'uuss', dimensions, fieldtype='U')
    vuss = Field.from_netcdf(stokesfiles, 'vuss', dimensions, fieldtype='V', grid=uuss.grid, timeFiles=uuss.timeFiles)

    fieldset = FieldSet(U=uhycom+uuss, V=vhycom+vuss)
    fieldset.add_field(MaskUvel)
    fieldset.add_field(MaskVvel)
    fieldset.MaskUvel.units = fieldset.U[0].units
    fieldset.MaskVvel.units = fieldset.V[0].units

    fieldset.add_periodic_halo(zonal=True, meridional=False, halosize=5)
    return fieldset

def WrapLon(particle, fieldset, time, dt):
    if particle.lon > 360.:
        particle.lon = particle.lon - 360.
    if particle.lon < 0.:
        particle.lon = particle.lon + 360.

def BoundaryVels(particle, fieldset, time, dt):
    bvu = fieldset.MaskUvel[0, particle.lon, particle.lat, particle.depth]
    bvv = fieldset.MaskVvel[0, particle.lon, particle.lat, particle.depth]
    particle.lon += bvu * dt  # taking Euler step only
    particle.lat += bvv * dt

def OutOfBounds(particle, fieldset, time, dt):
    particle.delete()

ddir = '/Volumes/data01/HYCOMdata/GLBa0.08_expt90_surf/hycom_GLBu0.08_912_'
# ddir = '/Users/erik/Desktop/HycomAntarctic/hycom_GLBu0.08_912_'

datestr = '201*'
stokesfiles = sorted(glob('/Volumes/data01/WaveWatch3data/WW3-GLOB-30M_%s' % datestr))
allfiles = sorted(glob(ddir + datestr + '00_t000.nc'))
rundays = 365 * 7

fset = set_fields(allfiles, stokesfiles)

size2D = (fset.U[0].grid.ydim, fset.U[0].grid.xdim)
fset.add_field(Field('Kh_zonal', data=10*np.ones(size2D), lon=fset.U[0].grid.lon, lat=fset.U[0].grid.lat, mesh='spherical', allow_time_extrapolation=True))
fset.add_field(Field('Kh_meridional', data=10*np.ones(size2D), lon=fset.U[0].grid.lon, lat=fset.U[0].grid.lat, mesh='spherical', allow_time_extrapolation=True))

lats = [-64.989, -64.619, -64.767, -64.528, -63.907, -63.395, -63.156, -63.452, -62.636, -62.298, -61.417, -62.249]
lons = [-63.383, -63.172, -62.658, -64.329, -64.138, -61.633, -60.631, -59.440, -58.998, -57.524, -55.251, -57.036]
lons = [l + 360 for l in lons]
locs = [9, 8, 7, 10, 11, 12, 6, 5, 4, 3, 1, 2]
days = [14, 16, 16, 18, 18, 19, 19, 19, 21, 23, 25, 26]  # all in Feb 2017

dates = [datetime.datetime(2017, 2, d) for d in days]
nperloc = 100

class AntarcticParticle(JITParticle):
    loc = Variable('loc', dtype=np.float32, initial=0.)

pset = ParticleSet(fieldset=fset, pclass=AntarcticParticle, lon=np.repeat(lons, [nperloc]),
                   lat=np.repeat(lats, [nperloc]), time=np.repeat(dates, [nperloc]),
                   loc=np.repeat(locs, [nperloc]))

ofile = pset.ParticleFile(name='antarcticplastic_wstokes_7yr.nc', outputdt=delta(days=1))

kernels = pset.Kernel(AdvectionRK4) + BrownianMotion2D + BoundaryVels + WrapLon
pset.execute(kernels, runtime=delta(days=rundays), dt=-delta(hours=1),
             output_file=ofile, recovery={ErrorCode.ErrorOutOfBounds: OutOfBounds})

max_stars_repo_name: OceanParcels/AntarcticPeninsulaPlastic

max_issues_repo_name: OceanParcels/AntarcticPeninsulaPlastic

max_forks_repo_name: OceanParcels/AntarcticPeninsulaPlastic


text:
import torch
import numpy as np
from torch.utils.data import DataLoader

class bAbIDataloader(DataLoader):

    def __init__(self, *args, **kwargs):
        super(bAbIDataloader, self).__init__(
            *args, **kwargs, collate_fn=bAbIDataloader.collate_fn)

    @staticmethod
    def collate_fn(data):
        offset = 0
        b_am = None
        b_annotation = None
        b_target = []
        for i, d in enumerate(data):
            am, annotation, target = d
            if b_am is None:
                b_am = [[] for _ in range(len(am))]

            if b_annotation is None:
                b_annotation = np.zeros(annotation[0].shape)

            b_target.append(target)

            b_annotation = np.vstack(
                (b_annotation, annotation))

            for i, edge_list in enumerate(am):
                for src, dest in edge_list:
                    b_am[i].append([src+offset, dest+offset])

            offset += len(annotation)

        max_length = 0
        for edge_list in b_am:
            max_length = max(max_length, len(edge_list))

        for edge_list in b_am:
            for i in range(0, max_length - len(edge_list)):
                edge_list.append([0, 0])
        b_am = torch.Tensor(b_am).long()
        b_annotation = torch.from_numpy(b_annotation).float()
        b_target = torch.Tensor(b_target).long()
        return b_am, b_annotation, b_target


text:
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import collections
import time
import argparse
import sys
import pickle
import numpy as np
import time
import models

parser = argparse.ArgumentParser(description='PyTorch Grafting Training')
# basic setting
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--batch-size', default=128, type=int)
parser.add_argument('--epochs', default=200, type=int)
parser.add_argument('--r', default=None, type=int)
parser.add_argument('--device', default='cuda', type=str)
parser.add_argument('--s', default='1', type=str)
parser.add_argument('--model', default='resnet32', type=str)
parser.add_argument('--cifar', default=10, type=int)
parser.add_argument('--print_frequence', default=100, type=int)
# Grafting setting
parser.add_argument('--a', default=0.4, type=float)
parser.add_argument('--c', default=5, type=int)
parser.add_argument('--num', default=1, type=int)
parser.add_argument('--i', default=1, type=int)
# Increase models diversity
parser.add_argument('--cos', action="store_true", default=False)
parser.add_argument('--difflr', action="store_true", default=False)
args = parser.parse_args()
print(args)
print('Session:%s\tModel:%d\tPID:%d' % (args.s, args.i, os.getpid()))
args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
net=models.__dict__[args.model](args.cifar).to(args.device)
start_epoch = 0
best_acc = 0
if args.difflr:
    loc = (1 + np.cos(np.pi * ((args.num - args.i) / args.num))) / 2
else:
    loc = 1
print('The initial learning rate is:', args.lr * loc)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args.lr * loc, momentum=0.9, weight_decay=5e-4)
# Data
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
if args.cifar == 10:
    trainset = torchvision.datasets.CIFAR10(root='../../data', train=True, download=True, transform=transform_train)
    testset = torchvision.datasets.CIFAR10(root='../../data', train=False, download=True, transform=transform_test)
elif args.cifar == 100:
    trainset = torchvision.datasets.CIFAR100(root='../../data', train=True, download=True, transform=transform_train)
    testset = torchvision.datasets.CIFAR100(root='../../data', train=False, download=True, transform=transform_test)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
if args.cos == True:
    lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epochs * trainloader.__len__())
else:
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=60, gamma=0.1)

def entropy(x, n=10):
    x = x.reshape(-1)
    scale = (x.max() - x.min()) / n
    entropy = 0
    for i in range(n):
        p = torch.sum((x >= x.min() + i * scale) * (x < x.min() + (i + 1) * scale), dtype=torch.float) / len(x)
        if p != 0:
            entropy -= p * torch.log(p)
    return float(entropy.cpu())

def grafting(net, epoch):
    while True:
        try:
            checkpoint = torch.load('%s/ckpt%d_%d.t7' % (args.s, args.i - 1, epoch))['net']
            break
        except:
            time.sleep(10)
    model = collections.OrderedDict()
    for i, (key, u) in enumerate(net.state_dict().items()):
        if 'conv' in key:
            w = round(args.a / np.pi * np.arctan(args.c * (entropy(u) - entropy(checkpoint[key]))) + 0.5, 2)
        model[key] = u * w + checkpoint[key] * (1 - w)
    net.load_state_dict(model)

def test(epoch):
    global best_acc
    net.eval()
    test_loss = 0
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(testloader):
            inputs, targets = inputs.to(args.device), targets.to(args.device)
            outputs = net(inputs)
            loss = criterion(outputs, targets)
            test_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()
    acc = 100. * correct / total
    if acc > best_acc:
        best_acc = acc
        state = {
            'net': net.state_dict(),
            'acc': acc
        }
        torch.save(state, '%s/best_%d.t7' % (args.s, epoch))
    print('Network:%d    epoch:%d    accuracy:%.3f    best:%.3f' % (args.i, epoch, acc, best_acc))

def train(epoch):
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        inputs, targets = inputs.to(args.device), targets.to(args.device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()
        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()
        if batch_idx % args.print_frequence == args.print_frequence - 1 or args.print_frequence == trainloader.__len__() - 1:
            print('Loss: %.3f | Acc: %.3f%% (%d/%d)' % (
                train_loss / (batch_idx + 1), 100. * correct / total, correct, total))
        if args.cos == True:
            lr_scheduler.step()
    if args.cos == False:
        lr_scheduler.step()

if __name__ == '__main__':
    for epoch in range(start_epoch, args.epochs):
        train(epoch)
        test(epoch)
        state = {
            'net': net.state_dict(),
        }
        torch.save(state, '%s/ckpt%d_%d.t7' % (args.s, args.i % args.num, epoch))
        if args.num!=1:
            grafting(net, epoch)

max_stars_repo_name: scott-mao/Ensemble-Grafting

max_issues_repo_name: TencentYoutuResearch/Ensemble-Grafting

max_forks_repo_name: TencentYoutuResearch/Ensemble-Grafting


text:
from flask import Flask, request, jsonify
from flask_cors import CORS

from scipy.misc import imsave, imread, imresize
import numpy as np
import keras.models

import sys, os
sys.path.append(os.path.abspath('./model'))

import load as load_model
global model, graph
model, graph = load_model.init()

app = Flask(__name__, static_url_path='', static_folder='deep-shrooms-frontend/build')
cors = CORS(app, resources={r"*": {"origins": "*"}})

def generate_input_image(img_array):
	img_resized = imresize(img_array, (480, 480))
	imsave('resized.jpg', img_resized)
	X = img_resized.reshape(1, 480, 480, 3)
	X = X/255.0
	return X

@app.route('/', methods=['GET'])
def frontpage():
	return app.send_static_file('index.html')

@app.route('/api/predict', methods=['POST'])
def predict():
	if 'Content-Type' not in request.headers or 'multipart/form-data' not in request.headers['Content-Type']:
		return "Content-Type wasn't 'multipart/form-data'", 400
	try:
		formFile = request.files['file']
	except:
		return "FormData didn't include a file", 400
	try:
		img = imread(formFile)
	except:
		return 'Unable to read the image file', 400

	print(img.shape)
	X = generate_input_image(img)
	with graph.as_default():
		prediction = model.predict(X)
		print(prediction)
		pred_value = prediction.flatten().tolist()[0]
		return jsonify({ "prediction": pred_value })

if __name__ == "__main__":
	app.run(host='localhost', port=os.environ.get('PORT', 9000))

max_stars_repo_name: rupshabagchi/shrimp

max_issues_repo_name: rupshabagchi/shrimp

max_forks_repo_name: rupshabagchi/shrimp


text:
import logging
import operator
import os
from functools import partial, reduce
from xml.etree.ElementTree import Element, ElementTree, SubElement

import h5py
import numpy as np
from humanfriendly import format_size

from ..base import (
    TILE_INDEX_STR,
    DenseDataset,
    MultiChannelDataset,
    MultiChannelDatasetIterator,
    MultiViewDataset,
    MultiViewDatasetIterator,
    SessionDataset,
    TiledDataset,
    TiledDatasetIterator,
)
from .error import InvalidChunkSizeError

__all__ = ["BigDataViewerDataset"]

logger = logging.getLogger("utoolbox.io.dataset")

class BigDataViewerXML(object):
    """
    Args:
        path (str): path to the dataset
    """

    class View(object):
        attributes = dict()

        def __init__(
            self, vid, shape, name="untitled", voxel_size=(1, 1, 1), **attributes
        ):
            self.vid = vid
            self.shape = shape
            self.name = name
            self.voxel_size = voxel_size
            self.attributes = {
                key: BigDataViewerXML.View.archive_attribute(key, value)
                for key, value in attributes.items()
            }

            self.reset_transform()

        def add_transform(self, name, matrix):
            self.transforms.append((name, matrix))

        def reset_transform(self):
            self.transforms = []

            voxel_size = self.voxel_size[::-1]
            # upsample low-res axes
            min_voxel_size = min(voxel_size)
            voxel_size = tuple(s / min_voxel_size for s in voxel_size)

            matrix = np.zeros((3, 4))
            matrix[range(3), range(3)] = voxel_size
            self.add_transform("calibration", matrix)

        def serialize(self):
            # abstract definitions
            setup = Element("ViewSetup")
            SubElement(setup, "id").text = str(self.vid)
            SubElement(setup, "name").text = self.name
            SubElement(setup, "size").text = " ".join(
                str(s) for s in reversed(self.shape)
            )

            # attach attributes
            attributes = SubElement(setup, "attributes")
            for key, index in self.attributes.items():
                SubElement(attributes, key).text = str(index)

            # spatial calibrations
            voxel = SubElement(setup, "voxelSize")
            SubElement(voxel, "unit").text = "micron"
            SubElement(voxel, "size").text = " ".join(
                str(s) for s in reversed(self.voxel_size)
            )

            transforms = Element("ViewRegistration")
            transforms.set("timepoint", str(0))
            transforms.set("setup", str(self.vid))
            for name, matrix in reversed(self.transforms):
                transform = SubElement(transforms, "ViewTransform")
                transform.set("type", "affine")
                SubElement(transform, "Name").text = name
                SubElement(transform, "affine").text = " ".join(
                    "{:.4f}".format(v) for v in matrix.ravel()
                )

            return setup, transforms

        ##

        @classmethod
        def archive_attribute(cls, key, value):
            """
            Archive an attribute and returns its respective ID.
            """
            # XML can only accept strings
            value = str(value)
            try:
                attribute = cls.attributes[key]
                try:
                    return attribute.index(value)
                except ValueError:
                    # new value
                    attribute.append(value)
                    return len(attribute) - 1
            except KeyError:
                # new attribute
                cls.attributes[key] = [value]
                return 0

    ##

    def __init__(self, path):
        path = os.path.realpath(path)
        self._init_tree(path)

        # XML will place next to the dataset
        fname, _ = os.path.splitext(path)
        self._path = f"{fname}.xml"

        self._views = []

    ##

    @property
    def path(self):
        return self._path

    @property
    def root(self):
        return self._root

    @property
    def views(self):
        return self._views

    ##

    def add_view(self, data, name="untitled", voxel_size=(1, 1, 1), **kwargs):
        """
        Add a new view and return its stored view ID.
        """
        vid = len(self._views)
        if "tile" not in kwargs:
            kwargs["tile"] = vid
        view = BigDataViewerXML.View(
            vid, data, name=name, voxel_size=voxel_size, **kwargs
        )
        self._views.append(view)
        return vid

    def serialize(self):
        for view in self._views:
            setup, transforms = view.serialize()
            self._setups.append(setup)
            self._registrations.append(transforms)

        for key, values in BigDataViewerXML.View.attributes.items():
            attribute = SubElement(self._setups, "Attributes")
            attribute.set("name", key)
            for i, value in enumerate(values):
                variants = SubElement(attribute, key.capitalize())
                SubElement(variants, "id").text = str(i)
                SubElement(variants, "name").text = str(value)

        tree = ElementTree(self.root)
        tree.write(self.path)
        logger.info(f'XML saved to "{self.path}"')

    ##

    def _init_tree(self, h5_path):
        # init XML
        root = Element("SpimData")
        root.set("version", "0.2")

        # using relative path
        base_path = SubElement(root, "BasePath")
        base_path.set("type", "relative")
        base_path.text = "."

        sequence = SubElement(root, "SequenceDescription")

        # a HDF data source
        loader = SubElement(sequence, "ImageLoader")
        loader.set("format", "bdv.hdf5")
        loader_path = SubElement(loader, "hdf5")
        loader_path.set("type", "relative")
        loader_path.text = os.path.basename(h5_path)

        # populate default fields
        setups = SubElement(sequence, "ViewSetups")
        timepoints = SubElement(sequence, "Timepoints")
        timepoints.set("type", "pattern")
        registrations = SubElement(root, "ViewRegistrations")

        # TODO no timeseries
        SubElement(timepoints, "integerpattern").text = str(0)

        # save internal arguments
        self._root = root
        self._setups, self._registrations = setups, registrations

class BigDataViewerHDF5(object):
    def __init__(
        self, path, mode, cache_chunk_size=(64, 64, 64), cache_n_chunks=(1, 4, 4)
    ):
        self._path = path

        nslots = reduce(operator.mul, cache_n_chunks, 1)
        nbytes = reduce(operator.mul, cache_chunk_size, 1) * nslots * 2
        logger.info(f"cache size: {format_size(nbytes, binary=True)}")

        self._func = partial(
            h5py.File, self.path, mode, rdcc_nbytes=nbytes, rdcc_nslots=nslots
        )

    def __enter__(self):
        self.open()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    ##

    @property
    def handle(self):
        return self._handle

    @property
    def path(self):
        return self._path

    ##

    def add_view(self, ss, data, pyramid, chunks, compression):
        sname = f"s{ss:02d}"
        info = self.handle.create_group(sname)
        self._write_matrix(info, "resolutions", pyramid)
        if isinstance(chunks[0], int):
            # reuse same chunk size for each level
            chunks = (chunks,)
            chunks *= len(pyramid)
        self._write_matrix(info, "subdivisions", chunks)

        # NOTE BDV reads int16 instead of uint16 internally
        data = data.astype(np.int16)

        for i, (bins, chunk) in enumerate(zip(pyramid, chunks)):
            # new dataset
            path = f"t{0:05d}/s{ss:02d}/{i}"
            logger.debug(f".. > {path}, bin:{bins}, chunk:{chunk}")

            # downsample range
            ranges = tuple(slice(None, None, step) for step in bins)
            sdata = data[ranges]

            # kick start
            sdata = sdata.compute()

            group = self.handle.create_group(path)
            try:
                group.create_dataset(
                    "cells", data=sdata, chunks=chunk, compression=compression
                )
            except ValueError as err:
                err_str = str(err)
                raise InvalidChunkSizeError(err_str)
            finally:
                self.handle.flush()

    def close(self):
        self.handle.close()
        self._handle = None

    def open(self):
        self._handle = self._func()

    ##

    @classmethod
    def _write_matrix(cls, handle, name, matrix, can_append=True):
        matrix = np.array(matrix)
        matrix = np.fliplr(matrix)  # HDF5 use different order
        if can_append:
            shape = (None,) + matrix.shape[1:]
        handle.create_dataset(name, data=matrix, maxshape=shape)

class DummyBigDataViewerHDF5(BigDataViewerHDF5):
    def add_view(self, *args, **kwargs):
        pass

    def open(self):
        logger.info("BigDataViewerDataset DRY RUN dump()")

    def close(self):
        pass

class BigDataViewerDataset(
    SessionDataset, DenseDataset, MultiChannelDataset, MultiViewDataset, TiledDataset
):
    """
    Using HDF5-backend BigDataViewer format.

    Args:
        TBD
    """

    def __init__(self, root_dir: str):
        super().__init__(store=root_dir, path="/")  # BDV does not allow arbitrary root

        self._xml_handle, self._h5_handle = None, None

    ##

    @property
    def h5(self) -> BigDataViewerHDF5:
        return self._h5_handle

    @property
    def read_func(self):
        pass

    @property
    def root_dir(self):
        return self._root_dir

    @property
    def xml(self) -> BigDataViewerXML:
        return self._xml_handle

    ##

    @classmethod
    def dump(
        cls,
        dst_dir: str,
        dataset,
        pyramid=[(1, 1, 1), (2, 4, 4)],
        chunks=(64, 128, 128),
        compression="gzip",
        dry_run=False,
    ):
        try:
            os.makedirs(dst_dir)
        except FileExistsError:
            logger.warning(f'folder "{dst_dir}" already exists')
        h5_path = os.path.join(dst_dir, f"dataset.h5")

        xml = BigDataViewerXML(h5_path)
        if not isinstance(dataset, DenseDataset):
            raise TypeError("dataset is not a DenseDataset")
        voxel_size = dataset.voxel_size

        shape, _ = dataset._load_array_info()

        if dry_run:
            klass = DummyBigDataViewerHDF5
        else:
            klass = BigDataViewerHDF5

        with klass(h5_path, "w") as h:
            for channel, c_selected in MultiChannelDatasetIterator(dataset):
                for view, v_selected in MultiViewDatasetIterator(c_selected):
                    kwargs = {"channel": channel, "illumination": view}

                    for (index, coord), selected in TiledDatasetIterator(
                        v_selected, axes="xyz", return_key=True, return_format="both"
                    ):
                        # find tile linear index
                        coords = {
                            k: v for k, v in zip(sorted(TILE_INDEX_STR), index)
                        }  # in XYZ order
                        result = dataset.tile_coords.xs(
                            list(coords.values()),
                            axis="index",
                            level=list(coords.keys()),
                        )
                        row_index = result.iloc[0].name
                        linear_index = dataset.tile_coords.index.get_loc(row_index)

                        uuid = selected.inventory.values[0]

                        ss = xml.add_view(
                            shape,
                            name=uuid,
                            voxel_size=voxel_size,
                            tile=linear_index,
                            **kwargs,
                        )
                        logger.info(f" [{ss}] {uuid}")

                        # anisotropic factor
                        min_voxel_size = min(voxel_size)
                        factor = tuple(s / min_voxel_size for s in voxel_size)

                        # anisotropic factor
                        min_voxel_size = min(voxel_size)
                        factor = tuple(s / min_voxel_size for s in voxel_size)

                        # 3d transformation, pad info if missing
                        if len(coord) == 2:
                            coord = coord + (0,)

                        # transformation
                        matrix = np.zeros((3, 4))
                        matrix[range(3), range(3)] = 1
                        matrix[range(3), -1] = [
                            c / v * s * f
                            for c, v, s, f in zip(
                                coord,
                                reversed(voxel_size),
                                (-1, -1, -1),
                                reversed(factor),
                            )
                        ]
                        xml.views[ss].add_transform(
                            "Translation to Regular Grid", matrix
                        )

                        # write data
                        h.add_view(ss, dataset[selected], pyramid, chunks, compression)

            xml.serialize()

    ##

    def _open_session(self):
        # TODO use HDF5/XML instance object instead of handle
        pass

    def _close_session(self):
        pass

    def _can_read(self):
        pass

    def _enumerate_files(self):
        pass

    def _load_array_info(self):
        pass

    def _load_channel_info(self):
        pass

    def _retrieve_file_list(self, coord_dict):
        pass

    def _load_metadata(self):
        pass

    def _load_coordinates(self):
        pass

    def _load_tiling_info(self):
        pass

    def _load_view_info(self):
        pass

max_issues_repo_name: liuyenting/utoolbox-core

max_forks_repo_name: liuyenting/utoolbox-core


text:
import json
import argparse
import math
import numpy as np

def draw_line(pos, line_length, w, laser_d, speed):
    line_gcode = [];
    gcode_move = "G0 X{:4.4f} Y{:4.4f} Z{:4.4f} F{:4.4f}\n"
    count_lines = math.ceil(w/laser_d);
    lines_step = (w-laser_d)/max((count_lines-1),1)
    overlap = 1-lines_step/laser_d

    line_gcode.append(gcode_move.format(pos[0]+laser_d/2, pos[1], pos[2], 1800))
    y = [pos[1]+line_length, pos[1]]
    for line_num in range(count_lines):
        line_gcode.append(gcode_move.format(
            *[pos[0]+laser_d/2+line_num*lines_step, y[line_num%2], pos[2], speed]))
        line_gcode.append(gcode_move.format(
            *[pos[0]+laser_d/2+(line_num+1)*lines_step, y[line_num%2], pos[2], speed]))
    return line_gcode[:-1], overlap

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="G-code generator of laser calibration for photoresist.")
    parser.add_argument('-c', '--config_file')
    args = parser.parse_args()
    if args.config_file is None:
        raise RuntimeError("Config file not selected")
    config = json.load(open(args.config_file))

    num_series = (config["surface_mm"][1]-config["offset_mm"][1])/(config["line_mm"][1]+config["eps_mm"][1])
    values_in_series = (config["surface_mm"][0]-config["offset_mm"][0])/(config["line_mm"][0]+config["eps_mm"][0])

    tests_name = ["focus_mm",
                  "width_mm",
                  "speed_mm_s"]
    count_grid_params = [config[test] for test in tests_name]

    count_unfilled = np.sum([0 if "count" in a or "values" in a else 1
                             for a in count_grid_params])
    count_reserved = np.prod([a.get("count", len(a.get("values", [1])))
                              for a in count_grid_params])

    count_grid_values = num_series*values_in_series

    count_values = math.floor(np.power(count_grid_values/count_reserved,
                                             1/max(1, count_unfilled)))
    count_grid = [a.get("count",len(a.get("values", range(count_values))))
                  for a in count_grid_params]

    print("Num testing values per dimension:\n", count_grid)

    grid_values = [a["values"] if "values" in a else
                   [a.get("values",a["start"]+i*(a["finish"]-a["start"])/(b-1))
                    for i in range(b)]
                   for a, b in zip(count_grid_params, count_grid)]
    for axle, name in zip(grid_values, tests_name):
        print(name, " values:\n",["{:.2f}".format(v) for v in axle])

    grid = [[x, y, z*60]
        for x in grid_values[0]
        for y in grid_values[1]
        for z in grid_values[2]]

    lines = [draw_line([config["offset_mm"][0]/2+(i%values_in_series)*
                (config["eps_mm"][0]+config["line_mm"][0]),
                config["offset_mm"][1]/2+math.floor(i/values_in_series)*
                (config["eps_mm"][1]+config["line_mm"][1]),
                values[0]],
               config["line_mm"][1],
               config["line_mm"][0],
               values[1],
               values[2]) for values, i in zip(grid, range(len(grid)))]

    result_file = open(config["output_path"], "w")
    #settings
    pre_steps = "G28\nG21\nG0 X{:4.4f} Y{:4.4f} Z{:4.4f}\nM400\nG92 X0 Y0\nG90\n"
    result_file.write(pre_steps.format(*[config["left_bottom"][0], config["left_bottom"][1], grid_values[0][0]]))
    #draw lines
    disable_laser = "M107 \nG4 P{:4.4f}\n"
    enable_laser = "M106 S255.0\nG4 P{:4.4f}\n"
    wait_move_finished = "M400\n"
    for line in lines:
        result_file.write(wait_move_finished)
        result_file.write(disable_laser.format(config["pause_off_ms"]))
        #move to line start
        result_file.write(line[0][0])
        result_file.write(enable_laser.format(config["pause_on_ms"]))
        result_file.write("".join(line[0][1:]))
    result_file.write(wait_move_finished)
    result_file.write(disable_laser.format(config["pause_off_ms"]))

max_stars_repo_name: catscury/gcalas

max_issues_repo_name: catscury/gcalas

max_forks_repo_name: catscury/gcalas