def samefile(path, path2): """Test whether two pathnames reference the same actual file""" path = _tostr(path, "samefile") path2 = _tostr(path2, "samefile") f = File(path) f2 = File(path2) return f.getCanonicalPath() == f2.getCanonicalPath()
def downloadRemoteScanFile(Framework): remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) # download scanner log file before downloading scan file downloadScanLogFile(Framework) if remoteScanFileLocation is None: logger.debug("No scan file to downloaded from current execution") return None logger.debug("About to download scan file from current execution:", remoteScanFileLocation) extension = InventoryUtils.getFileExtension(remoteScanFileLocation) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) # folder for scan files localScanFileFolderPath = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME ) downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() # this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) # get file to the local machine logger.debug("Scan file to be downloaded to location:", targetScanFile.getCanonicalPath()) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, targetScanFile.getCanonicalPath()): return None return targetScanFile
def removeLocalScanLog(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) localScanLogFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME + CollectorsParameters.FILE_SEPARATOR localScanLogFile = File(localScanLogFolderPath, localScanLogName) try: # if the local scan log exists, delete it before next steps if localScanLogFile.exists(): logger.debug("local scan log file found, just delete it: ", localScanLogFile.getCanonicalPath()) if not localScanLogFile.delete(): logger.warn("delete scan log file failed, ensure the there's permission and it's not locked:", localScanLogFile.getCanonicalPath()) except: logger.warn("delete scan log file failed: ", localScanLogFile.getCanonicalPath()) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def useTempScanFile(Framework): tempScanFilePath = Framework.getProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE) if tempScanFilePath is None: logger.debug("No scan file found from previous scanner execution") return None logger.debug("Using scan file from previous execution:", tempScanFilePath) extension = InventoryUtils.getFileExtension(tempScanFilePath) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) # folder for scan files localScanFileFolderPath = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME ) downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() # this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) logger.debug("Scan file from previous execution will be moved to ", targetScanFile.getCanonicalPath()) tempScanFile = File(tempScanFilePath) if not tempScanFile.renameTo(targetScanFile): return None return targetScanFile
def get_path_with_real_case(filename): from java.io import File # noqa f = File(filename) ret = f.getCanonicalPath() if IS_PY2 and not isinstance(ret, str): return ret.encode(getfilesystemencoding()) return ret
def test_image_example_dir_iteration(self): f = File(str(inspect.getfile( inspect.currentframe() ))) example_dir = File(File(f.getParentFile().getParentFile().getParentFile(),"character_examples"),"A") image_example_dir = ImageExampleDir(example_dir.getCanonicalPath()) for label, image in image_example_dir: if label != "A": raise "The label of the examples in this dir should be A"
def lstat(path): """lstat(path) -> stat result Like stat(path), but do not follow symbolic links. """ abs_path = sys.getPath(path) try: return stat_result.from_jnastat(_posix.lstat(abs_path)) except NotImplementedError: pass except: raise f = File(sys.getPath(path)) # XXX: jna-posix implements similar link detection in # JavaFileStat.calculateSymlink, fallback to that instead when not # native abs_parent = f.getAbsoluteFile().getParentFile() if not abs_parent: # root isn't a link return stat(path) can_parent = abs_parent.getCanonicalFile() if can_parent.getAbsolutePath() == abs_parent.getAbsolutePath(): # The parent directory's absolute path is canonical.. if f.getAbsolutePath() != f.getCanonicalPath(): # but the file's absolute and canonical paths differ (a # link) return stat_result((_stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0)) # The parent directory's path is not canonical (one of the parent # directories is a symlink). Build a new path with the parent's # canonical path and compare the files f = File(_path.join(can_parent.getAbsolutePath(), f.getName())) if f.getAbsolutePath() != f.getCanonicalPath(): return stat_result((_stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0)) # Not a link, only now can we determine if it exists (because # File.exists() returns False for dead links) if not f.exists(): raise OSError(errno.ENOENT, strerror(errno.ENOENT), path) return stat(path)
def loaddata(): filepath = File(pyplugin.dataFolder, "config") months = {0: 0, 1: 1, 2: 2, 3: 2, 4: 3, 5: 0, 6: 0} if filepath.exists(): for line in open(filepath.getCanonicalPath()): data = line.strip().split(':') try: if int(data[0]) in months: months[int(data[0])] = int(data[1]) except: logs.info("error in configfile") return months
def checkScanFileExistance(Framework): # this step is always finished with success since we DON'T require scan file from previous execution, just nice to have Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) DownloadScanFileBeforeExecution = Boolean.parseBoolean(Framework.getParameter('DownloadScanFileBeforeExecution')) if DownloadScanFileBeforeExecution: try: client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) previousExecutionStarted = options.get(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED) if (previousExecutionStarted is None) or (len(previousExecutionStarted.strip()) == 0): logger.debug('Previous execution timestamp no found, continuing with workflow') return remoteScanFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug('No scan file path found on remote machine, continuing with workflow') return lastSuccessExecuton = Framework.getState().getJobLastSuccessfulRun() logger.debug('Last success execution ' + str(lastSuccessExecuton)) logger.debug('Remote scan file execution ' + str(previousExecutionStarted)) if long(lastSuccessExecuton) > long(previousExecutionStarted): logger.debug('Scan file on probe side is newer than on remote machine, skipping downloading') return logger.debug('Last success execution ' + str(lastSuccessExecuton) + ' older than scan file on remote machine ' + str(remoteScanFileLocation) + '. Going to download scan file:' + str(remoteScanFileLocation)) tempScanFileFolder = CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId() + CollectorsParameters.FILE_SEPARATOR File(tempScanFileFolder).mkdirs() extension = InventoryUtils.getFileExtension(remoteScanFileLocation) tempScanFileName = InventoryUtils.generateScanFileName(Framework, extension) tempScanFile = File(tempScanFileFolder, tempScanFileName) tempScanFilePath = tempScanFile.getCanonicalPath() logger.debug('Try to download scan file to the:', tempScanFilePath) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, tempScanFilePath, 0): logger.debug('Failed to download scan file before current execution') Framework.setProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE, tempScanFilePath) except: reason = str(sys.exc_info()[1]) logger.debug('Failed to check/download scan file from previous execution. Reason:', reason) else: logger.debug('Even not checking whether scan file exists on remote machine or not.')
def getLocalScanLogFileLocation(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) # folder for scan files localScanLogFolder = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME ) downloadedScanLogDir = File(localScanLogFolder) downloadedScanLogDir.mkdirs() # this scan log file will be created after downloading from remote machine targetScanFile = File(downloadedScanLogDir, localScanLogName) localScanLogLocation = targetScanFile.getCanonicalPath() return localScanLogLocation
def RunConfigs(projString,simConfigs,simDt,argv=None): projFile = File(os.getcwd(), projString) if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) simManager.runMultipleSims(simConfigs = simConfigs, simDt = simDt, simulators = simulators, runInBackground = runInBackground, varTimestepNeuron = varTimestepNeuron, varTimestepTolerance = varTimestepTolerance, mpiConfig = mpiConfig, suggestedRemoteRunTime = suggestedRemoteRunTime)
def downloadLogsIfNeeded(Framework): platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) logger.debug('Checking if to print install/uninstall logs') downloadMigrateLog = Framework.getProperty(AgentUtils.DOWNLOAD_MIGRATE_LOG_FILE) downloadInstallLog = Framework.getProperty(AgentUtils.DOWNLOAD_INSTALL_LOG_FILE) downloadUnInstallLog = Framework.getProperty(AgentUtils.DOWNLOAD_UNINSTALL_LOG_FILE) if not downloadMigrateLog and not downloadInstallLog and not downloadUnInstallLog: logger.debug('Migrate/Install/UnInstall log should not be downloaded') return try: logger.debug('Releasing old connection') InventoryUtils.releaseConnection(Framework) logger.debug('Preparing framework for new connection') AgentUtils.prepareFrameworkForShellOrAgentConnect(Framework) except: errorMessage = str(sys.exc_info()[1]) logger.debugException('Failed to initialize connection for downloading agent log files' + errorMessage) return if downloadMigrateLog: # If need to download migrate log, we need to connect to DDMi agent as well Framework.setProperty(InventoryUtils.STATE_PROPERTY_IS_MIGRATE, str('true')) if not InventoryUtils.ensureConnection(Framework): logger.debug('Failed to connect to the remote machine, no logs available') else: ip_address = Framework.getTriggerCIData('ip_address') localInstallFile = None localUnInstallFile = None try: try: agentsConfigFile = Framework.getConfigFile(CollectorsConstants.AGENTSSBYPLATFORM_FILE_NAME) BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) architecture = Framework.getProperty(InventoryUtils.STATE_PROPERTY_ARCHITECTURE) agentPlatformConfig = agentsConfigFile.getPlatformConfiguration(platform, architecture) ip_address_str = str(ip_address) if (ip_address_str.find(':') <> -1): ip_address_str = ip_address_str.replace(':','-') if downloadMigrateLog: logger.debug('Download the migrate log') installLogFile = agentPlatformConfig.getUpgradeLogFile() localInstallFile = File(AGENTS_LOGS_TEMP_DIR_FILE, ip_address_str + '-' + installLogFile) getLogFileContent(Framework, localInstallFile, str(BASEDIR) + installLogFile) if downloadInstallLog: logger.debug('Download the install/update log') if AgentUtils.isUpgradeByUDAgent(Framework): installLogFile = agentPlatformConfig.getUpgradeLogFile() else: installLogFile = agentPlatformConfig.getInstallLogFile() localInstallFile = File(AGENTS_LOGS_TEMP_DIR_FILE, ip_address_str + '-' + installLogFile) getLogFileContent(Framework, localInstallFile, str(BASEDIR) + installLogFile) if downloadUnInstallLog: logger.debug('Download the uninstall log') unInstallLogFile = agentPlatformConfig.getUnInstallLogFile() localUnInstallFile = File(AGENTS_LOGS_TEMP_DIR_FILE, ip_address_str + '-' + unInstallLogFile) getLogFileContent(Framework, localUnInstallFile, str(BASEDIR) + unInstallLogFile) except: errorMessage = str(sys.exc_info()[1]) logger.debugException(errorMessage) Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_FAILED_EXECUTE_STEP, ['FinalizeAndReleaseResources', errorMessage]) finally: try: if localInstallFile and not localInstallFile.delete(): logger.debug('File was not deleted:' + localInstallFile.getCanonicalPath()) except: logger.debugException('Failed to delete ' + localInstallFile.getCanonicalPath()) try: logger.debug('Going to delete file ' + localInstallFile.getCanonicalPath()) if localUnInstallFile and not localUnInstallFile.delete(): logger.debug('File was not deleted:' + localUnInstallFile.getCanonicalPath()) except: logger.debugException('Failed to delete ' + localUnInstallFile.getCanonicalPath())
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) ### Change num in each cell group densityGrC = 1.90e6 densityMF = 6.6e5 densityGoC = 4607 totDens = densityGrC + densityMF + densityGoC numMF = int(targetNum * densityMF/totDens) numGoC = int(targetNum * densityGoC/totDens) numGrC = targetNum - numMF - numGoC vol = targetNum/(totDens*1e-9) height = 150 side = math.sqrt(vol/height) info = "Number in %fx%fx%i box (vol: %f mm^3): GrC: %i, MF: %i, GoC: %i, Total: %i"%(side, side, height, vol*1e-9,numGrC, numMF,numGoC, (numGrC +numMF +numGoC)) print info SimulationsInfo.addExtraSimProperty("summary", info) region3D = simManager.project.regionsInfo.getRegionObject("TestGranCellVolume") region3D.setParameter(region3D.WIDTH_PARAM, side) region3D.setParameter(region3D.HEIGHT_PARAM, height) region3D.setParameter(region3D.DEPTH_PARAM, side) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGrC").setMaxNumberCells(numGrC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGoC").setMaxNumberCells(numGoC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingMF").setMaxNumberCells(numMF) ######simManager.project.cellGroupsInfo.getCellPackingAdapter("lg2").setMaxNumberCells(numCells2) allSims = simManager.runMultipleSims(simConfigs = simConfigs, simDt = simDt, simDuration = simDuration, simulators = simulators, runInBackground = runInBackground, varTimestepNeuron = varTimestepNeuron, mpiConfigs = mpiConfigs, suggestedRemoteRunTime = suggestedRemoteRunTime, simRefGlobalPrefix = simAllPrefix, runSims = runSims, maxElecLens = multipleRuns, saveAsHdf5 = saveAsHdf5, saveOnlySpikes = saveOnlySpikes, runMode = runMode) while (len(simManager.allRunningSims)>0): print "Waiting for the following sims to finish: "+str(simManager.allRunningSims) time.sleep(5) # wait a while... simManager.updateSimsRunning() times = [] procNums = [] for sim in allSims: simDir = File(projFile.getParentFile(), "/simulations/"+sim) try: simData = SimulationData(simDir) simData.initialise() simTime = simData.getSimulationProperties().getProperty("RealSimulationTime") print "Simulation: %s took %s seconds"%(sim, simTime) times.append(float(simTime)) paraConfig = simData.getSimulationProperties().getProperty("Parallel configuration") print paraConfig numProc = int(paraConfig[max(paraConfig.find(" host, ")+7, paraConfig.find(" hosts, ")+8):paraConfig.find(" processor")]) procNums.append(numProc) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info() print times print procNums ''' import matplotlib.pyplot as plt lines = plt.loglog(times, procNums, 'r:') plt.ylabel('Simulation time') plt.xlabel('Number of processors') plt.show()''' from ucl.physiol.neuroconstruct.gui.plotter import PlotManager from ucl.physiol.neuroconstruct.gui.plotter import PlotCanvas from ucl.physiol.neuroconstruct.dataset import DataSet plotFrame = PlotManager.getPlotterFrame("Time for simulation run on different numbers of processors", 0, 1) plotFrame.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "Simulation times for: "+str(procNums) dataSet = DataSet(info, info, "#", "s", "Number of processors", "Simulation time") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) for i in range(len(times)): dataSet.addPoint(procNums[i],times[i]*procNums[i]) #dataSet.addPoint(procNums[i],times[i]) plotFrame.addDataSet(dataSet)
saveDataAsHdf5 = False varTimestepNeuron = False verbose = True runInBackground= False suggestedRemoteRunTime = 120 ####################################### ### Load neuroConstruct project print "Loading project from "+ projFile.getCanonicalPath() pm = ProjectManager() project = pm.loadProject(projFile) ### Set duration & timestep & simulation configuration project.simulationParameters.setDt(simDt) simConfig = project.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) if saveDataAsHdf5: project.neuronSettings.setDataSaveFormat(NeuronSettings.DataSaveFormat.HDF5_NC)
def samefile(path, path2): """Test whether two pathnames reference the same actual file""" f = File(path) f2 = File(path2) return f.getCanonicalPath() == f2.getCanonicalPath()
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) ### Change num in each cell group simManager.project.cellGroupsInfo.getCellPackingAdapter("lg1").setMaxNumberCells(numCells1) simManager.project.cellGroupsInfo.getCellPackingAdapter("lg2").setMaxNumberCells(numCells2) allSims = simManager.runMultipleSims(simConfigs = simConfigs, simDt = simDt, simDuration = simDuration, simulators = simulators, runInBackground = runInBackground, varTimestepNeuron = varTimestepNeuron, mpiConfigs = mpiConfigs, suggestedRemoteRunTime = suggestedRemoteRunTime, simRefGlobalPrefix = simAllPrefix, runSims = runSims, maxElecLens = multipleRuns, saveAsHdf5 = saveAsHdf5, saveOnlySpikes = saveOnlySpikes, runMode = runMode) while (len(simManager.allRunningSims)>0): print "Waiting for the following sims to finish: "+str(simManager.allRunningSims) time.sleep(5) # wait a while... simManager.updateSimsRunning() times = [] procNums = [] for sim in allSims: simDir = File(projFile.getParentFile(), "/simulations/"+sim) try: simData = SimulationData(simDir) simData.initialise() simTime = simData.getSimulationProperties().getProperty("RealSimulationTime") print "Simulation: %s took %s seconds"%(sim, simTime) times.append(float(simTime)) paraConfig = simData.getSimulationProperties().getProperty("Parallel configuration") print paraConfig numProc = int(paraConfig[max(paraConfig.find(" host, ")+7, paraConfig.find(" hosts, ")+8):paraConfig.find(" processor")]) procNums.append(numProc) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info() print times print procNums ''' import matplotlib.pyplot as plt lines = plt.loglog(times, procNums, 'r:') plt.ylabel('Simulation time') plt.xlabel('Number of processors') plt.show()''' from ucl.physiol.neuroconstruct.gui.plotter import PlotManager from ucl.physiol.neuroconstruct.gui.plotter import PlotCanvas from ucl.physiol.neuroconstruct.dataset import DataSet plotFrame = PlotManager.getPlotterFrame("Time for simulation run on different numbers of processors", 0, 1) plotFrame.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "Simulation times for: "+str(procNums) dataSet = DataSet(info, info, "#", "s", "Number of processors", "Simulation time") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) for i in range(len(times)): #dataSet.addPoint(procNums[i],times[i]*procNums[i]) dataSet.addPoint(procNums[i],times[i]) plotFrame.addDataSet(dataSet)
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) ### Change num in each cell group simManager.project.cellGroupsInfo.getCellPackingAdapter("ExcCells").setMaxNumberCells(numExcCells) simManager.project.cellGroupsInfo.getCellPackingAdapter("INCells").setMaxNumberCells(numINCells) ### Change weights in synapses/gap junctions simConfig = simManager.project.simConfigInfo.getSimConfig(simConfigName) for netConnName in simConfig.getNetConns(): print "Changing synaptic delay in %s to %f"%(netConnName, defaultSynapticDelay) delayGen = NumberGenerator(defaultSynapticDelay) for synProps in simManager.project.morphNetworkConnectionsInfo.getSynapseList(netConnName): synProps.setDelayGenerator(delayGen) # defaultSynapticDelay will be recorded in simulation.props and listed in SimulationBrowser GUI SimulationsInfo.addExtraSimProperty("defaultSynapticDelay", str(defaultSynapticDelay)) allSims = simManager.runMultipleSims(simConfigs = [simConfigName], simDt = simDt, simDuration = simDuration, simulators = simulators, runInBackground = runInBackground, varTimestepNeuron = varTimestepNeuron, mpiConfigs = mpiConfigs, suggestedRemoteRunTime = suggestedRemoteRunTime, simRefGlobalPrefix = simAllPrefix, runSims = runSims, neuroConstructSeed = neuroConstructSeed, simulatorSeed = simulatorSeed) while (len(simManager.allRunningSims)>0): print "Waiting for the following sims to finish: "+str(simManager.allRunningSims) time.sleep(5) # wait a while... simManager.updateSimsRunning() for sim in allSims: simDir = File(projFile.getParentFile(), "/simulations/"+sim) try: simData = SimulationData(simDir) simData.initialise() simTime = simData.getSimulationProperties().getProperty("RealSimulationTime") print "Simulation: %s took %s seconds"%(sim, simTime) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() if mpiConfigs == [MpiSettings.LOCAL_SERIAL] or mpiConfigs == [MpiSettings.MATLEM_1PROC]: simManager.reloadSims(plotVoltageOnly = True, plotSims = plotSims, analyseSims = analyseSims)
def generateF_ICurve(self, projFile, simulator, simConfig, preStimAmp, preStimDel, preStimDur, stimAmpLow, stimAmpInc, stimAmpHigh, stimDel, stimDur, simDuration, analyseStartTime, analyseStopTime, analyseThreshold, maxNumSimultaneousSims = 4): # Load neuroConstruct project print "Loading project from file: " + projFile.getAbsolutePath()+", exists: "+ str(projFile.exists()) pm = ProjectManager() self.myProject = pm.loadProject(projFile) self.simulator = simulator simConfig = self.myProject.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) pm.doGenerate(simConfig.getName(), self.neuroConstructSeed) while pm.isGenerating(): print "Waiting for the project to be generated with Simulation Configuration: "+str(simConfig) time.sleep(2) numGenerated = self.myProject.generatedCellPositions.getNumberInAllCellGroups() print "Number of cells generated: " + str(numGenerated) simReferences = {} if numGenerated > 0: print "Generating scripts for simulator: %s..."%simulator if simulator == 'NEURON': self.myProject.neuronFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.neuronSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.neuronSettings.setGraphicsMode(0) # 0 hides graphs during execution if simulator == 'GENESIS': self.myProject.genesisFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.genesisSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.genesisSettings.setGraphicsMode(0) # 0 hides graphs during execution stimAmp = stimAmpLow while stimAmp <= stimAmpHigh: while (len(self.simsRunning)>=maxNumSimultaneousSims): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(3) # wait a while... self.updateSimsRunning() simRef = "PySim_"+str(float(stimAmp)) print "Going to run simulation: "+simRef ######## Adjusting the amplitude of the current clamp ############### # preStim = self.myProject.elecInputInfo.getStim(simConfig.getInputs().get(0)) preStim = self.myProject.elecInputInfo.getStim("Input_3") preStim.setAmp(NumberGenerator(preStimAmp)) preStim.setDel(NumberGenerator(preStimDel)) preStim.setDur(NumberGenerator(preStimDur)) self.myProject.elecInputInfo.updateStim(preStim) # stim = self.myProject.elecInputInfo.getStim(simConfig.getInputs().get(1)) stim = self.myProject.elecInputInfo.getStim("Input_4") stim.setAmp(NumberGenerator(stimAmp)) stim.setDel(NumberGenerator(stimDel)) stim.setDur(NumberGenerator(stimDur)) self.myProject.elecInputInfo.updateStim(stim) print "Next stim: "+ str(stim) self.myProject.simulationParameters.setReference(simRef) if simulator == "NEURON": self.myProject.neuronFileManager.generateTheNeuronFiles(simConfig, None, NeuronFileManager.RUN_HOC, self.simulatorSeed) print "Generated NEURON files for: "+simRef compileProcess = ProcessManager(self.myProject.neuronFileManager.getMainHocFile()) compileSuccess = compileProcess.compileFileWithNeuron(0,0) print "Compiled NEURON files for: "+simRef if compileSuccess: pm.doRunNeuron(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) if simulator == "GENESIS": compartmentalisation = GenesisCompartmentalisation() self.myProject.genesisFileManager.generateTheGenesisFiles(simConfig, None, compartmentalisation, self.simulatorSeed) print "Generated GENESIS files for: "+simRef pm.doRunGenesis(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) time.sleep(1) # Wait for sim to be kicked off simReferences[simRef] = stimAmp stimAmp = stimAmp +stimAmpInc print print "Finished running "+str(len(simReferences))+" simulations for project "+ projFile.getAbsolutePath() print "These can be loaded and replayed in the previous simulation browser in the GUI" print while (len(self.simsRunning)>0): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(4) # wait a while... self.updateSimsRunning() #simReferences = {'PySim_0.3':0.3,'PySim_0.4':0.4,'PySim_0.5':0.5} plotFrameFI = PlotManager.getPlotterFrame("F-I curve from project: "+str(self.myProject.getProjectFile())+" on "+simulator , 1, 1) plotFrameVolts = PlotManager.getPlotterFrame("VoltageTraces from project: "+str(self.myProject.getProjectFile())+" on "+simulator , 1, 1) plotFrameFI.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "F-I curve for Simulation Configuration: "+str(simConfig) dataSet = DataSet(info, info, "nA", "Hz", "Current injected", "Firing frequency") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) simList = simReferences.keys() simList.sort() for sim in simList: simDir = File(projFile.getParentFile(), "/neuroConstruct/models/Dan_GranCell/simulations/"+sim) print print "--- Reloading data from simulation in directory: %s"%simDir.getCanonicalPath() try: simData = SimulationData(simDir) simData.initialise() print "Data loaded: " print simData.getAllLoadedDataStores() times = simData.getAllTimes() cellSegmentRef = simConfig.getCellGroups().get(0)+"_0" volts = simData.getVoltageAtAllTimes(cellSegmentRef) traceInfo = "Voltage at: %s in simulation: %s"%(cellSegmentRef, sim) dataSetV = DataSet(traceInfo, traceInfo, "mV", "ms", "Membrane potential", "Time") for i in range(len(times)): dataSetV.addPoint(times[i], volts[i]) plotFrameVolts.addDataSet(dataSetV) spikeTimes = SpikeAnalyser.getSpikeTimes(volts, times, analyseThreshold, analyseStartTime, analyseStopTime) stimAmp = simReferences[sim] print "Number of spikes at %f nA in sim %s: %i"%(stimAmp, sim, len(spikeTimes)) avgFreq = 0 if len(spikeTimes)>1: avgFreq = len(spikeTimes)/ ((analyseStopTime - analyseStartTime)/1000.0) dataSet.addPoint(stimAmp,avgFreq) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info()[0] plotFrameFI.addDataSet(dataSet)
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) ### Change num in each cell group densityGrC = 1.90e6 densityMF = 6.6e5 densityGoC = 4607 totDens = densityGrC + densityMF + densityGoC numMF = int(targetNum * densityMF/totDens) numGoC = int(targetNum * densityGoC/totDens) numGrC = targetNum - numMF - numGoC vol = targetNum/(totDens*1e-9) height = 150 side = math.sqrt(vol/height) info = "Number in %fx%fx%i box (vol: %f mm^3): GrC: %i, MF: %i, GoC: %i, Total: %i"%(side, side, height, vol*1e-9,numGrC, numMF,numGoC, (numGrC +numMF +numGoC)) print info SimulationsInfo.addExtraSimProperty("summary", info) region3D = simManager.project.regionsInfo.getRegionObject("TestGranCellVolume") region3D.setParameter(region3D.WIDTH_PARAM, side) region3D.setParameter(region3D.HEIGHT_PARAM, height) region3D.setParameter(region3D.DEPTH_PARAM, side) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGrC").setMaxNumberCells(numGrC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGoC").setMaxNumberCells(numGoC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingMF").setMaxNumberCells(numMF) ######simManager.project.cellGroupsInfo.getCellPackingAdapter("lg2").setMaxNumberCells(numCells2) pm = simManager.projectManager project = simManager.project pm.doGenerate(simConfigs[0], neuroConstructSeed) while pm.isGenerating(): print "Waiting for the project to be generated with Simulation Configuration: "+str(simConfigs[0]) sleep(2) print "Generated %i cells in %i cell groups" % (project.generatedCellPositions.getNumberInAllCellGroups(), project.generatedCellPositions.getNumberNonEmptyCellGroups()) print "Generated %i instances in %i network connections" % (project.generatedNetworkConnections.getNumAllSynConns(), project.generatedNetworkConnections.getNumNonEmptyNetConns()) print "Generated %i instances in %i elect inputs" % (project.generatedElecInputs.getNumberSingleInputs(), project.generatedElecInputs.getNonEmptyInputRefs().size()) fileX = File( "%s/savedNetworks/Net_%s.nml"%(project.getProjectMainDirectory().getCanonicalPath(), targetNum)) print "Saving XML net to %s"%fileX.getCanonicalPath() pm.saveNetworkStructureXML(project, fileX, 0, 0, simConfigs[0], "Physiological Units") fileH = File( "%s/savedNetworks/Net_%s.h5"%(project.getProjectMainDirectory().getCanonicalPath(), targetNum)) print "Saving HDF5 net to %s"%fileH.getCanonicalPath() pm.saveNetworkStructureHDF5(project, fileH, simConfigs[0], "Physiological Units") print "Finished running all sims, shutting down..."
numTCR = 100 # full model: 100 numnRT = 100 # full model: 100 ####################################### ### Load neuroConstruct project import datetime start = datetime.datetime.now() print "Loading project %s from at %s " % (projFile.getCanonicalPath(),start.strftime("%Y-%m-%d %H:%M")) pm = ProjectManager() project = pm.loadProject(projFile) ### Set duration & timestep & simulation configuration project.simulationParameters.setDt(simDt) simConfig = project.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) ### Set simulation reference index = 0
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from "+ projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims = numConcurrentSims, verbose = verbose) ### Change num in each cell group densityGrC = 1.90e6 densityMF = 6.6e5 densityGoC = 4607 totDens = densityGrC + densityMF + densityGoC for conf in confs.keys(): numb = conf mpiConfigs = [confs[conf]] perProc = 5000 targetNum = perProc*numb simAllPrefix = "NZ_%i_%i_"%(perProc, numb) # Adds a prefix to simulation reference print "Simulation pref: "+simAllPrefix numMF = int(targetNum * densityMF/totDens) numGoC = int(targetNum * densityGoC/totDens) numGrC = targetNum - numMF - numGoC vol = targetNum/(totDens*1e-9) height = 150 side = math.sqrt(vol/height) info = "Number in %fx%fx%i box (vol: %f mm^3): GrC: %i, MF: %i, GoC: %i, Total: %i"%(side, side, height, vol*1e-9,numGrC, numMF,numGoC, (numGrC +numMF +numGoC)) print info SimulationsInfo.addExtraSimProperty("summary", info) region3D = simManager.project.regionsInfo.getRegionObject("TestGranCellVolume") region3D.setParameter(region3D.WIDTH_PARAM, side) region3D.setParameter(region3D.HEIGHT_PARAM, height) region3D.setParameter(region3D.DEPTH_PARAM, side) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGrC").setMaxNumberCells(numGrC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingGoC").setMaxNumberCells(numGoC) simManager.project.cellGroupsInfo.getCellPackingAdapter("TestScalingMF").setMaxNumberCells(numMF) ######simManager.project.cellGroupsInfo.getCellPackingAdapter("lg2").setMaxNumberCells(numCells2) allSims = simManager.runMultipleSims(simConfigs = simConfigs, simDt = simDt, simDuration = simDuration, simulators = simulators, runInBackground = runInBackground, varTimestepNeuron = varTimestepNeuron, mpiConfigs = mpiConfigs, suggestedRemoteRunTime = suggestedRemoteRunTime, simRefGlobalPrefix = simAllPrefix, runSims = runSims, maxElecLens = multipleRuns, saveAsHdf5 = saveAsHdf5, saveOnlySpikes = saveOnlySpikes, runMode = runMode) if wait: while (len(simManager.allRunningSims)>0): print "Waiting for the following sims to finish: "+str(simManager.allRunningSims) time.sleep(30) # wait a while... simManager.updateSimsRunning() times = [] procNums = [] for sim in allSims: simDir = File(projFile.getParentFile(), "/simulations/"+sim) try: simData = SimulationData(simDir) simData.initialise() simTime = simData.getSimulationProperties().getProperty("RealSimulationTime") print "Simulation: %s took %s seconds"%(sim, simTime) times.append(float(simTime)) paraConfig = simData.getSimulationProperties().getProperty("Parallel configuration") print paraConfig numProc = int(paraConfig[max(paraConfig.find(" host, ")+7, paraConfig.find(" hosts, ")+8):paraConfig.find(" processor")]) procNums.append(numProc) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info()
# Load an existing neuroConstruct project projFile = File("TestPython/TestPython.neuro.xml") print "Loading project from file: " + projFile.getAbsolutePath()+", exists: "+ str(projFile.exists()) pm = ProjectManager() myProject = pm.loadProject(projFile) print "Loaded project: " + myProject.getProjectName() morphDir = File("../nCexamples/Ex3_Morphology/importedMorphologies/") morphmlFile = File(morphDir, "SimplePurkinjeCell.morph.xml") print "Going to load morphology from: " + morphmlFile.getCanonicalPath() converter = MorphMLConverter() cell = converter.loadFromMorphologyFile(morphmlFile, "NewCell") print "Loaded cell: " + cell.getInstanceName() + " with " + str(cell.getAllSegments().size()) +" segments" myProject.cellManager.addCellType(cell) # Actually add it to the project myProject.cellGroupsInfo.setCellType("SampleCellGroup", cell.getInstanceName()) # Set the type of an existing cell group to this # Now the project can be generated as in Ex5_MultiSimGenerate.py # * OR *
varTimestepNeuron = False verbose = True runInBackground= False suggestedRemoteRunTime = 120 ####################################### ### Load neuroConstruct project print "Loading project from "+ projFile.getCanonicalPath() pm = ProjectManager() project = pm.loadProject(projFile) ### Set duration & timestep & simulation configuration project.simulationParameters.setDt(simDt) simConfig = project.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) if saveDataAsHdf5: project.neuronSettings.setDataSaveFormat(NeuronSettings.DataSaveFormat.HDF5_NC)
numTCR = 100 # full model: 100 numnRT = 100 # full model: 100 ####################################### ### Load neuroConstruct project import datetime start = datetime.datetime.now() print "Loading project %s from at %s " % (projFile.getCanonicalPath(),start.strftime("%Y-%m-%d %H:%M")) pm = ProjectManager() project = pm.loadProject(projFile) ### Set duration & timestep & simulation configuration project.simulationParameters.setDt(simDt) simConfig = project.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) ### Set simulation reference index = 0
plotFrameFI.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "F-I curve for Simulation Configuration: " + str(simConfig) dataSet = DataSet(info, info, "nA", "Hz", "Current injected", "Firing frequency") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) simList = simReferences.keys() simList.sort() for sim in simList: simDir = File(projFile.getParentFile(), "/simulations/" + sim) print print "--- Reloading data from simulation in directory: %s" % simDir.getCanonicalPath( ) try: simData = SimulationData(simDir) simData.initialise() print "Data loaded: " print simData.getAllLoadedDataStores() times = simData.getAllTimes() cellSegmentRef = simConfig.getCellGroups().get(0) + "_0" volts = simData.getVoltageAtAllTimes(cellSegmentRef) print "Got " + str( len(volts)) + " data points on cell seg ref: " + cellSegmentRef analyseStartTime = 100 # So it's firing at a steady rate... analyseStopTime = simConfig.getSimDuration() analyseThreshold = -20 # mV
numDeepAxAx = 100 # full model: 100 numDeepLTS = 100 # full model: 100 numL6NonTuftRS = 500 # full model: 500 numTCR = 100 # full model: 100 numnRT = 100 # full model: 100 ####################################### ### Load neuroConstruct project import datetime start = datetime.datetime.now() print "Loading project %s from at %s " % (projFile.getCanonicalPath(), start.strftime("%Y-%m-%d %H:%M")) pm = ProjectManager() project = pm.loadProject(projFile) ### Set duration & timestep & simulation configuration project.simulationParameters.setDt(simDt) simConfig = project.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) ### Set simulation reference index = 0 simRef = "%s%i" % (simRefPrefix, index)
maxElecLens = [0.025, 0.01, 0.0075, 0.005, 0.0025, 0.001, 0.00075, 0.0005, 0.00025, 0.0001] maxElecLens = [0.025, 0.01, 0.0075, 0.005, 0.0025] #maxElecLens = [.025, 0.01, 0.005, 0.0025] #maxElecLens = [0.01, 0.005, 0.0025, 0.001] #simAllPrefix = "" simAllPrefix = "Lodt_" simAllPrefix = "Looodt_" numSpikes = 3 verbose = True ############################################# print "Loading project from "+ projFile.getCanonicalPath() pm = ProjectManager() project = pm.loadProject(projFile) allFinishedSims = [] plotVoltageOnly = True plotSomaOnly = True plotSims = False allSpikeTimeDataSets = {} for simConfigName in simConfigs:
def get_path_with_real_case(filename): from java.io import File # noqa f = File(filename) ret = f.getCanonicalPath() return ret
simDuration = 200 # ms simDt = 0.01 # ms neuroConstructSeed = 1234 simulatorSeed = 1111 simRefPrefix = "SB_" defaultSynapticDelay = 0.2 # ms runInBackground = True ####################################### # Load neuroConstruct project print "Loading project from "+ projFile.getCanonicalPath() pm = ProjectManager() myProject = pm.loadProject(projFile) myProject.simulationParameters.setDt(simDt) index = 0 while File( "%s/simulations/%s%i"%(myProject.getProjectMainDirectory().getCanonicalPath(), simRefPrefix,index)).exists(): index = index+1 simRef = "%s%i"%(simRefPrefix,index) myProject.simulationParameters.setReference(simRef)
def reloadSims(waitForSimsToFinish): print "Trying to reload sims: "+str(allFinishedSims) plottedSims = [] for simRef in allFinishedSims: simDir = File(projFile.getParentFile(), "/simulations/"+simRef) timeFile = File(simDir, "time.dat") timeFile2 = File(simDir,"time.txt") # for PSICS... if timeFile.exists() or timeFile2.exists(): if verbose: print "--- Reloading data from simulation in directory: %s"%simDir.getCanonicalPath() time.sleep(1) # wait a while... try: simData = SimulationData(simDir) simData.initialise() times = simData.getAllTimes() if analyseSims: ''' volts = simData.getVoltageAtAllTimes(cellSegmentRef) if verbose: print "Got "+str(len(volts))+" data points on cell seg ref: "+cellSegmentRef analyseStartTime = 0 analyseStopTime = simConfig.getSimDuration() analyseThreshold = -20 # mV spikeTimes = SpikeAnalyser.getSpikeTimes(volts, times, analyseThreshold, analyseStartTime, analyseStopTime) print "Spike times in %s for sim %s: %s"%(cellSegmentRef, simRef, str(spikeTimes)) ''' if plotSims: simConfigName = simData.getSimulationProperties().getProperty("Sim Config") if simConfigName.find('(')>=0: simConfigName = simConfigName[0:simConfigName.find('(')] for dataStore in simData.getAllLoadedDataStores(): ds = simData.getDataSet(dataStore.getCellSegRef(), dataStore.getVariable(), False) if not plotVoltageOnly or dataStore.getVariable() == SimPlot.VOLTAGE: plotFrame = PlotManager.getPlotterFrame("Behaviour of "+dataStore.getVariable() \ +" on: %s for sim config: %s"%(str(simulators), simConfigName)) plotFrame.addDataSet(ds) plottedSims.append(simRef) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info() for simRef in plottedSims: allFinishedSims.remove(simRef) if waitForSimsToFinish and len(allRunningSims)>0: if verbose: print "Waiting for sims: %s to finish..."%str(allRunningSims) time.sleep(2) # wait a while... updateSimsRunning() reloadSims(True)
def generateV_ICurve(self, projFile, simulator, simConfig, stimAmpLow, stimAmpInc, stimAmpHigh, stimDur, simDuration, maxNumSimultaneousSims = 4): # Load neuroConstruct project print "Loading project from file: " + projFile.getAbsolutePath()+", exists: "+ str(projFile.exists()) pm = ProjectManager() self.myProject = pm.loadProject(projFile) self.simulator = simulator simConfig = self.myProject.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) pm.doGenerate(simConfig.getName(), self.neuroConstructSeed) while pm.isGenerating(): print "Waiting for the project to be generated with Simulation Configuration: "+str(simConfig) time.sleep(2) numGenerated = self.myProject.generatedCellPositions.getNumberInAllCellGroups() print "Number of cells generated: " + str(numGenerated) simReferences = {} if numGenerated > 0: print "Generating scripts for simulator: %s..."%simulator if simulator == 'NEURON': self.myProject.neuronFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.neuronSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.neuronSettings.setGraphicsMode(0) # 0 hides graphs during execution if simulator == 'GENESIS': self.myProject.genesisFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.genesisSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.genesisSettings.setGraphicsMode(0) # 0 hides graphs during execution stimAmp = stimAmpLow ADD_TO_BEFORE_INIT = self.myProject.neuronSettings.getNativeBlock(NativeCodeLocation.BEFORE_INITIAL) ADD_TO_RECORD_I = self.myProject.neuronSettings.getNativeBlock(NativeCodeLocation.AFTER_SIMULATION) ADD_TO_START_FINITIALIZE = self.myProject.neuronSettings.getNativeBlock(NativeCodeLocation.START_FINITIALIZE) while stimAmp <= stimAmpHigh: while (len(self.simsRunning)>=maxNumSimultaneousSims): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(3) # wait a while... self.updateSimsRunning() simRef = "PySim_"+str(float(stimAmp)) print "Going to run simulation: "+simRef ######## Adjusting the amplitude of the Voltage clamp ############### TEXT_BEFORE_CREATION = "objref clampobj" + "\n" + "objref record_current" + "\n" + "objref data_save" + "\n" # TEXT_BEFORE_INIT = "GranuleCell_mod_tonic[0].Soma {\n" + "clampobj = new SEClamp(0.5)\n" + "clampobj.dur1 = 150.0" + "\n" + "clampobj.amp1 = -100.0" + "\n" + "clampobj.dur2 = " + str(stimDur) + "\n" + "clampobj.amp2 = " + str(stimAmp) + "\n" + "clampobj.dur3 = 150.0" + "\n" + "clampobj.amp3 = -100.0" + "\n" + "clampobj.rs = 0.00001\n " + "}\n" # This should do the trick # TEXT_BEFORE_INIT = TEXT_BEFORE_INIT + ADD_TO_BEFORE_INIT TEXT_START_FINITIALIZE = "GranuleCell_mod_tonic[0].Soma {\n" + "clampobj = new SEClamp(0.5)\n" + "clampobj.dur1 = 150.0" + "\n" + "clampobj.amp1 = -100.0" + "\n" + "clampobj.dur2 = " + str(stimDur) + "\n" + "clampobj.amp2 = " + str(stimAmp) + "\n" + "clampobj.dur3 = 150.0" + "\n" + "clampobj.amp3 = -40.0" + "\n" + "clampobj.rs = 0.00001\n" + "record_current = new Vector()" + "\n" + "record_current.record(&clampobj.i)" + "\n" + "}\n" #This should do the trick TEXT_START_FINITIALIZE = TEXT_START_FINITIALIZE + ADD_TO_START_FINITIALIZE TEXT_TO_RECORD_I = """data_save = new File() \n""" + """strdef filename \n""" + """sprint(filename, """" + self.myProject.getProjectMainDirectory().getAbsolutePath() + """/simulations/current%.3f.txt", clampobj.amp2) \n""" + """data_save.wopen(filename) \n""" + """record_current.printf(data_save) \n""" + """data_save.close() \n""" TEXT_TO_RECORD_I = TEXT_TO_RECORD_I + ADD_TO_RECORD_I self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.BEFORE_CELL_CREATION, TEXT_BEFORE_CREATION) # self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.BEFORE_INITIAL, TEXT_BEFORE_INIT) self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.START_FINITIALIZE, TEXT_START_FINITIALIZE) self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.AFTER_SIMULATION, TEXT_TO_RECORD_I) print "Next stim: "+ str(stimAmp) self.myProject.simulationParameters.setReference(simRef) if simulator == "NEURON": self.myProject.neuronFileManager.generateTheNeuronFiles(simConfig, None, NeuronFileManager.RUN_HOC, self.simulatorSeed) print "Generated NEURON files for: "+simRef compileProcess = ProcessManager(self.myProject.neuronFileManager.getMainHocFile()) compileSuccess = compileProcess.compileFileWithNeuron(0,0) print "Compiled NEURON files for: "+simRef if compileSuccess: pm.doRunNeuron(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) if simulator == "GENESIS": compartmentalisation = GenesisCompartmentalisation() self.myProject.genesisFileManager.generateTheGenesisFiles(simConfig, None, compartmentalisation, self.simulatorSeed) print "Generated GENESIS files for: "+simRef pm.doRunGenesis(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) time.sleep(1) # Wait for sim to be kicked off simReferences[simRef] = stimAmp stimAmp = stimAmp +stimAmpInc print print "Finished running "+str(len(simReferences))+" simulations for project "+ projFile.getAbsolutePath() print "These can be loaded and replayed in the previous simulation browser in the GUI" print while (len(self.simsRunning)>0): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(4) # wait a while... self.updateSimsRunning() #simReferences = {'PySim_0.3':0.3,'PySim_0.4':0.4,'PySim_0.5':0.5} plotFrameVI = PlotManager.getPlotterFrame("V-I curve from project: "+str(self.myProject.getProjectFile())+" on "+simulator , 1, 1) plotFrameVI.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "V-I curve for Simulation Configuration: "+str(simConfig) dataSet = DataSet(info, info, "mV", "nA", "Voltage", "Current") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) simList = simReferences.keys() simList.sort() for sim in simList: simDir = File(projFile.getParentFile(), "/neuroConstruct/models/Dan_GranCell/simulations/"+sim) print print "--- Reloading data from simulation in directory: %s"%simDir.getCanonicalPath() try: simData = SimulationData(simDir) simData.initialise() print "Data loaded: " print simData.getAllLoadedDataStores() times = simData.getAllTimes() cellSegmentRef = simConfig.getCellGroups().get(0)+"_0" cfilename = "E:/neuroConstruct/models/Dan_GranCell/simulations/" + sim + "/current.txt" trace = "E:/neuroConstruct/models/Dan_GranCell/simulations/" + sim + ".txt" # cFILE = open(cfilename, "r") strom = 0.0 volts = simData.getVoltageAtAllTimes(cellSegmentRef) traceInfo = "Voltage at: %s in simulation: %s"%(cellSegmentRef, sim) stimAmp = simReferences[sim] print "Current at %f mV in sim %s: %f"%(stimAmp, sim, float(strom)) dataSet.addPoint(stimAmp,float(strom)) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info()[0] plotFrameVI.addDataSet(dataSet)
def make_fF_Curve(self, projFile, simulator, simConfig, nTrains, simDuration, analyseStartTime, analyseStopTime, analyseThreshold, maxNumSimultaneousSims = 4): # Load neuroConstruct project print "Loading project from file: " + projFile.getAbsolutePath()+", exists: "+ str(projFile.exists()) pm = ProjectManager() self.myProject = pm.loadProject(projFile) self.simulator = simulator simConfig = self.myProject.simConfigInfo.getSimConfig(simConfig) simConfig.setSimDuration(simDuration) pm.doGenerate(simConfig.getName(), self.neuroConstructSeed) while pm.isGenerating(): print "Waiting for the project to be generated with Simulation Configuration: "+str(simConfig) time.sleep(2) numGenerated = self.myProject.generatedCellPositions.getNumberInAllCellGroups() print "Number of cells generated: " + str(numGenerated) simReferences = {} if numGenerated > 0: print "Generating scripts for simulator: %s..."%simulator if simulator == 'NEURON': self.myProject.neuronFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.neuronSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.neuronSettings.setGraphicsMode(0) # 0 hides graphs during execution if simulator == 'GENESIS': self.myProject.genesisFileManager.setQuitAfterRun(1) # Remove this line to leave the NEURON sim windows open after finishing self.myProject.genesisSettings.setCopySimFiles(1) # 1 copies hoc/mod files to PySim_0 etc. and will allow multiple sims to run at once self.myProject.genesisSettings.setGraphicsMode(0) # 0 hides graphs during execution currentTrain = 0 ADD_TO_START_FINITIALIZE = self.myProject.neuronSettings.getNativeBlock(NativeCodeLocation.START_FINITIALIZE) ADD_TO_RECORD_I = self.myProject.neuronSettings.getNativeBlock(NativeCodeLocation.AFTER_SIMULATION) while currentTrain <= nTrains: while (len(self.simsRunning)>=maxNumSimultaneousSims): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(3) # wait a while... self.updateSimsRunning() simRef = "PySim_"+str(currentTrain) print "Going to run simulation: "+simRef ######## Adjusting the amplitude of the Voltage clamp ############### TEXT_BEFORE_CREATION = """objref vector, gTrainFile\n""" + "objectvar clamp\n" ### TEXT_BEFORE_INIT = "GranuleCell_mod_tonic[0].Soma {\n" + "clampobj = new VClamp(0.5)\n" + "clampobj.dur[0] = " + str(stimDur) + "\n" + "clampobj.amp[0] = " + str(stimAmp) + "\n" + "}\n" # This should do the trick TEXT_START_FINITIALIZE = "\n" + "gTrainFile = new File() \n" + "vector = new Vector(100000) \n" + "access GranuleCell_mod_tonic[0].Soma \n" + "clamp = new SEClamp(0.5) \n" + "clamp.amp1 = 0 \n" + "clamp.dur1 = 1e9 \n" + """gTrainFile.ropen("E:/neuroConstruct/models/Dan_GranCell/gTrains_nS/gAMPA_""" + str(currentTrain) + """.txt") \n""" + "vector.scanf(gTrainFile) \n" + "gTrainFile.close \n" + "for i = 0, vector.size() -1 { \n" + " if (vector.x[i] <= 0) { \n" + " vector.x[i] = 1e-20 \n" + " } \n" + " } \n" + "for i = 0, vector.size() -1 { \n" + " vector.x[i] = ( 1 / vector.x[i] ) * 1000 \n" + " } \n" + "vector.play(&clamp.rs, 0.03) \n" # This TEXT_START_FINITIALIZE = TEXT_START_FINITIALIZE + ADD_TO_START_FINITIALIZE TEXT_TO_RECORD_I = """// currently not used \n""" TEXT_TO_RECORD_I = TEXT_TO_RECORD_I + ADD_TO_RECORD_I self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.BEFORE_CELL_CREATION, TEXT_BEFORE_CREATION) self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.START_FINITIALIZE, TEXT_START_FINITIALIZE) self.myProject.neuronSettings.setNativeBlock(NativeCodeLocation.AFTER_SIMULATION, TEXT_TO_RECORD_I) print "Next Train: "+ str(currentTrain) self.myProject.simulationParameters.setReference(simRef) if simulator == "NEURON": self.myProject.neuronFileManager.generateTheNeuronFiles(simConfig, None, NeuronFileManager.RUN_HOC, self.simulatorSeed) print "Generated NEURON files for: "+simRef compileProcess = ProcessManager(self.myProject.neuronFileManager.getMainHocFile()) compileSuccess = compileProcess.compileFileWithNeuron(0,0) print "Compiled NEURON files for: "+simRef if compileSuccess: pm.doRunNeuron(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) if simulator == "GENESIS": compartmentalisation = GenesisCompartmentalisation() self.myProject.genesisFileManager.generateTheGenesisFiles(simConfig, None, compartmentalisation, self.simulatorSeed) print "Generated GENESIS files for: "+simRef pm.doRunGenesis(simConfig) print "Set running simulation: "+simRef self.simsRunning.append(simRef) time.sleep(1) # Wait for sim to be kicked off simReferences[simRef] = currentTrain currentTrain = currentTrain + 1 print print "Finished running "+str(len(simReferences))+" simulations for project "+ projFile.getAbsolutePath() print "These can be loaded and replayed in the previous simulation browser in the GUI" print while (len(self.simsRunning)>0): print "Sims currently running: "+str(self.simsRunning) print "Waiting..." time.sleep(4) # wait a while... self.updateSimsRunning() #simReferences = {'PySim_0.3':0.3,'PySim_0.4':0.4,'PySim_0.5':0.5} plotFrameFf = PlotManager.getPlotterFrame("F-f curve from project: "+str(self.myProject.getProjectFile())+" on "+simulator , 1, 1) plotFrameVolts = PlotManager.getPlotterFrame("VoltageTraces from project: "+str(self.myProject.getProjectFile())+" on "+simulator , 1, 1) plotFrameFf.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "F-f curve for Simulation Configuration: "+str(simConfig) dataSet = DataSet(info, info, "Hz", "Hz", "Input_Freq", "Output_Freq") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) simList = simReferences.keys() simList.sort() traininfos = open("E:/neuroConstruct/models/Dan_GranCell/gAMPA_traininfo.txt") train_info_list = traininfos.readlines() currentTrain = 0 for sim in simList: simDir = File(projFile.getParentFile(), "E:/neuroConstruct/models/Dan_GranCell/simulations/"+sim) print print "--- Reloading data from simulation in directory: %s"%simDir.getCanonicalPath() try: simData = SimulationData(simDir) simData.initialise() print "Data loaded: " print simData.getAllLoadedDataStores() times = simData.getAllTimes() cellSegmentRef = simConfig.getCellGroups().get(0)+"_0" volts = simData.getVoltageAtAllTimes(cellSegmentRef) time.sleep(2) traceInfo = "Voltage at: %s in simulation: %s"%(cellSegmentRef, sim) dataSetV = DataSet(traceInfo, traceInfo, "ms", "mV", "Time", "Membrane Potential") Spike_List = [] Spike_List.append(0.00) for i in range(len(times)): dataSetV.addPoint(times[i], volts[i]) for i in range(len(times)): if (volts[i] > 0.0): if (times[i] - Spike_List[len(Spike_List)-1] > 0.50): Spike_List.append(times[i]) Spike_List.remove(0.00) plotFrameVolts.addDataSet(dataSetV) spikeTimes = SpikeAnalyser.getSpikeTimes(volts, times, analyseThreshold, analyseStartTime, analyseStopTime) currentTrain_Freq = float(train_info_list[currentTrain-1]) print "Number of spikes in sim %s: %i"%(sim, len(spikeTimes)) avgFreq = 0 if len(Spike_List)>0: avgFreq = 1000.0 / ((Spike_List[len(Spike_List)-1] - Spike_List[0]) / (len(Spike_List) -1)) dataSet.addPoint(currentTrain_Freq,avgFreq) currentTrain = currentTrain + 1 except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print sys.exc_info()[0] plotFrameFf.addDataSet(dataSet)
simRef = "Sim_2296" ################################################# from ucl.physiol.neuroconstruct.project import ProjectManager print 'Loading project file: ', projFile.getAbsolutePath() pm = ProjectManager() project = pm.loadProject(projFile) print 'Successfully loaded project: ', project.getProjectName() simDir = File(projFile.getParentFile(), "/simulations/"+simRef) print print "--- Reloading data from simulation in directory: %s"%simDir.getCanonicalPath() plotFrame = PlotManager.getPlotterFrame("All voltage traces from simulation: "+simRef, False) try: simData = SimulationData(simDir) simData.initialise() volt_traces = simData.getCellSegRefs(True) for trace in volt_traces: ds = simData.getDataSet(trace, SimPlot.VOLTAGE, False) plotFrame.addDataSet(ds) except: print "Error analysing simulation data from: %s"%simDir.getCanonicalPath() print exc_info()
def get_example_image(self): f = File(str(inspect.getfile( inspect.currentframe() ))) example_dir = File(File(f.getParentFile().getParentFile().getParentFile(),"character_examples"),"A") image_example_dir = ImageExampleDir(example_dir.getCanonicalPath()) label, image = image_example_dir.__iter__().next() return image
def testAll(argv=None): if argv is None: argv = sys.argv print "Loading project from " + projFile.getCanonicalPath() simManager = nc.SimulationManager(projFile, numConcurrentSims=numConcurrentSims, verbose=verbose) ### Change num in each cell group simManager.project.cellGroupsInfo.getCellPackingAdapter( "lg1").setMaxNumberCells(numCells1) simManager.project.cellGroupsInfo.getCellPackingAdapter( "lg2").setMaxNumberCells(numCells2) allSims = simManager.runMultipleSims( simConfigs=simConfigs, simDt=simDt, simDuration=simDuration, simulators=simulators, runInBackground=runInBackground, varTimestepNeuron=varTimestepNeuron, mpiConfigs=mpiConfigs, suggestedRemoteRunTime=suggestedRemoteRunTime, simRefGlobalPrefix=simAllPrefix, runSims=runSims, maxElecLens=multipleRuns, saveAsHdf5=saveAsHdf5, saveOnlySpikes=saveOnlySpikes, runMode=runMode) while (len(simManager.allRunningSims) > 0): print "Waiting for the following sims to finish: " + str( simManager.allRunningSims) time.sleep(5) # wait a while... simManager.updateSimsRunning() times = [] procNums = [] for sim in allSims: simDir = File(projFile.getParentFile(), "/simulations/" + sim) try: simData = SimulationData(simDir) simData.initialise() simTime = simData.getSimulationProperties().getProperty( "RealSimulationTime") print "Simulation: %s took %s seconds" % (sim, simTime) times.append(float(simTime)) paraConfig = simData.getSimulationProperties().getProperty( "Parallel configuration") print paraConfig numProc = int(paraConfig[max( paraConfig.find(" host, ") + 7, paraConfig.find(" hosts, ") + 8):paraConfig.find(" processor")]) procNums.append(numProc) except: print "Error analysing simulation data from: %s" % simDir.getCanonicalPath( ) print sys.exc_info() print times print procNums ''' import matplotlib.pyplot as plt lines = plt.loglog(times, procNums, 'r:') plt.ylabel('Simulation time') plt.xlabel('Number of processors') plt.show()''' from ucl.physiol.neuroconstruct.gui.plotter import PlotManager from ucl.physiol.neuroconstruct.gui.plotter import PlotCanvas from ucl.physiol.neuroconstruct.dataset import DataSet plotFrame = PlotManager.getPlotterFrame( "Time for simulation run on different numbers of processors", 0, 1) plotFrame.setViewMode(PlotCanvas.INCLUDE_ORIGIN_VIEW) info = "Simulation times for: " + str(procNums) dataSet = DataSet(info, info, "#", "s", "Number of processors", "Simulation time") dataSet.setGraphFormat(PlotCanvas.USE_CIRCLES_FOR_PLOT) for i in range(len(times)): #dataSet.addPoint(procNums[i],times[i]*procNums[i]) dataSet.addPoint(procNums[i], times[i]) plotFrame.addDataSet(dataSet)