Best Python code snippet using locust
run_svim.py
Source:run_svim.py
1#!/usr/bin/env python2from __future__ import print_function3print(1)4import matplotlib5matplotlib.use('TkAgg')6print(2)7import time8from datetime import datetime, date, time, timedelta9from dateutil.relativedelta import relativedelta10import numpy as np11print(3)12from opendrift.readers import reader_basemap_landmask13from opendrift.readers import reader_ROMS_native14from kino.pelagicplankton import PelagicPlanktonDrift15from opendrift.readers import reader_netCDF_CF_generic16import logging17import gdal18import os19#from netCDF4 import Dataset, datetime, date2num,num2date20print(4)21from numpy.random import RandomState22import matplotlib.pyplot as plt23try:24 import ogr25 import osr26except Exception as e:27 print (e)28 raise ValueError('OGR library is needed to read shapefiles.')29print(5)30def setupSeed(hoursBetweenTimestepInROMSFiles,startTime,endTime,startSpawningTime,endSpawningTime,releaseParticles):31 ##################################################32 # Create seed variation as function of day33 ##################################################34 # Make datetime array from start to end at 3 hour interval35 difference=endTime-startTime36 hoursOfSimulation=divmod(difference.total_seconds(), 3600)37 difference=endSpawningTime-startSpawningTime38 hoursOfSpawning=divmod(difference.total_seconds(), 3600)39 timeStepsSimulation=int(int(hoursOfSimulation[0])/hoursBetweenTimestepInROMSFiles)40 41 print ("\nsvim TIME EVOLUTION:")42 print ("=>SIMULATION: Drift simulation will run for %s simulation hours" %(timeStepsSimulation))43 print ("=>SPAWNING: Simulated spawning will run for %s simulation hours\n initiated on %s and ending on %s"%(timeStepsSimulation,startSpawningTime,endSpawningTime))44 interval = timedelta(hours=24)45 hoursPerSpawning=divmod(interval.total_seconds(), 3600) #hours per spawning event46 timeStepsSpawning=int(int(hoursOfSpawning[0])/int(hoursPerSpawning[0])) #number of spawning timesteps47 spawningTimes = [startSpawningTime + interval*n for n in range(timeStepsSpawning)] #times of spawning48 # Define number of particles released per spawning day, summing to ~releaseParticles and following a gaussian curve49 mu, sigma = 1, 0.25 # mean and standard deviation of the gaussian curve 50 prng = RandomState(1) # random number generator (specify number to ensure the same sequence each time)51 s = prng.normal(mu, sigma, timeStepsSpawning) # random distribution52 num=(s*releaseParticles/timeStepsSpawning).astype(int) # number of particles released per spawning event as releaseParticles/timeStepsSpawning, weighted by random distribution53 num=np.sort(num) #sort particles in increasing order 54 num=np.concatenate((num[len(num)%2::2],num[::-2]),axis=0) #release the highest number of particles at the midpoint of the spawning period55 56 print ("SPAWNING: Simulated spawning will release %s eggs"%(np.sum(num)))57 return num, spawningTimes58print(6)59def createOutputFilenames(startTime,endTime,verticalBehavior,spawning_ground):60 startDate=''61 if startTime.day<10:62 startDate+='0%s'%(startTime.day)63 else:64 startDate+='%s'%(startTime.day)65 if startTime.month<10:66 startDate+='0%s'%(startTime.month)67 else:68 startDate+='%s'%(startTime.month)69 startDate+='%s'%(startTime.year)70 endDate=''71 if endTime.day<10:72 endDate+='0%s'%(endTime.day)73 else:74 endDate+='%s'%(endTime.day)75 if endTime.month<10:76 endDate+='0%s'%(endTime.month)77 else:78 endDate+='%s'%(endTime.month)79 endDate+='%s'%(endTime.year)80 81 # File naming82 if verticalBehavior:83 outputFilename='results_stock_recruitment/opendrift_%s_%s_to_%s_vertical.nc'%(spawning_ground,startDate,endDate)84 animationFilename='figures/animation_%s_%s_to_%s_vertical.mp4'%(spawning_ground,startDate,endDate)85 plotFilename='figures/plot_%s_%s_to_%s_vertical.png'%(spawning_ground,startDate,endDate)86 else:87 outputFilename='results_stock_recruitment/opendrift_%s_%s_to_%s_novertical.nc'%(spawning_ground,startDate,endDate)88 animationFilename='figures/animation_%s_%s_to_%s_novertical.mp4'%(spawning_ground,startDate,endDate)89 plotFilename='figures/plot_%s_%s_to_%s_novertical.png'%(spawning_ground,startDate,endDate)90 if not os.path.exists('figures'):91 os.makedirs('figures')92 if not os.path.exists('results'):93 os.makedirs('results')94 return outputFilename, animationFilename, plotFilename95print(7)96 97def createAndRunSimulation(lowDepth,highDepth,endTime,shapefile,outputFilename,animationFilename,plotFilename,releaseParticles,pattern_svim,verticalBehavior,spawning_ground):98 # Setup a new simulation99 o = PelagicPlanktonDrift(loglevel=0) # Set loglevel to 0 for debug information100 #o.max_speed = 10101 #######################102 # Preparing readers103 #######################104 reader_basemap = reader_basemap_landmask.Reader(105 llcrnrlon=-7, llcrnrlat=50,106 urcrnrlon=15, urcrnrlat=65,107 resolution='i', projection='merc')108 109 o.add_reader([reader_basemap]) #Note: Include because of issue with linearNDfast110 o.set_config('general:basemap_resolution', 'i')111 reader_svim = reader_ROMS_native.Reader(pattern_svim)#SVIM reader used to cover area outside svim reader112 o.add_reader([reader_svim]) #FORCE loaded for wind information113 114 num, spawningTimes = setupSeed(hoursBetweenTimestepInROMSFiles,startTime,endTime,startSpawningTime,endSpawningTime,releaseParticles)115 116 #######################117 #Adjusting configuration118 #######################119 if verticalBehavior:120 o.set_config('processes:turbulentmixing', True)121 else:122 o.set_config('processes:turbulentmixing', False)123 o.set_config('turbulentmixing:diffusivitymodel','windspeed_Sundby1983')124 o.set_config('turbulentmixing:timestep', 4) # seconds125 o.set_config('turbulentmixing:verticalresolution', 2) # default is 1 meter, but since we have longer timestep we justify it126 if verticalBehavior:127 o.set_config('processes:verticaladvection', False)128 else:129 o.set_config('processes:verticaladvection', False)130 o.set_config('turbulentmixing:TSprofiles', False)131 #o.set_config('turbulentmixing:max_iterations', 400) #200 used in ms version132 o.set_config('drift:scheme', 'euler')133 o.set_config('general:coastline_action', 'previous') #Prevent stranding, jump back to previous position134 135 #######################136 # IBM configuration 137 #######################138 o.set_config('biology:constantIngestion', 0.75)139 o.set_config('biology:activemetabOn', 1)140 o.set_config('biology:cod', True)141 o.set_config('biology:haddock', False)142 o.set_config('biology:attenuationCoefficient',0.18)143 if verticalBehavior:144 o.set_config('biology:fractionOfTimestepSwimming',0.15) # Pause-swim behavior145 else:146 o.set_config('biology:fractionOfTimestepSwimming',0.00) # Pause-swim behavior147 o.set_config('biology:lowerStomachLim',0.3) #Min. stomach fullness needed to actively swim down148 149 150 #######################151 # Seed particles152 #######################153 #Fixed distribution in depth:154 def eq_div(N, i):155 return [] if i <= 0 else [N / i + 1] * (N % i) + [N / i] * (i - N % i)156 z_levels=range(lowDepth,highDepth+1,10) #levels of depth distribution157 for i, nums in enumerate(num):158 if nums <= 0:159 continue160 z_dist=eq_div(nums,len(z_levels)) #number of particles per level (approx. equal)161 print ("Running i=%s num=%s for spawning ground=%s"%(i,nums,spawning_ground))162 print ("Depths ",np.repeat(z_levels,z_dist))163 o.seed_from_shapefile(shapefile, nums, layername=None,featurenum=[1], z=np.repeat(z_levels,z_dist), time=spawningTimes[i])164 print ("Elements scheduled for %s : %s"%(spawning_ground,o.elements_scheduled))165 #########################166 # Run the model167 #########################168 o.run(end_time=endTime, time_step=timedelta(hours=1),time_step_output=timedelta(hours=12), 169 outfile=outputFilename,export_variables=['lon', 'lat', 'z','sea_water_temperature','length','weight','survival','sea_floor_depth_below_sea_level']) 170 print (o)171 #o.animation(background=['x_sea_water_velocity', 'y_sea_water_velocity'],filename=animationFilename)172print(8)173#########################174# SETUP175#########################176spawning_ground='viking'177lowDepth, highDepth = -50, 0 # in negative meters178verticalBehavior=False179for year in range(2010, 2011):180 print(year)181#Spawning period and number of particles per spawning ground:182 if spawning_ground=='south':183 startTime=datetime(year-1,12,15,1,00,00) 184 endTime=datetime(year,8,15,1,00,00) 185 startSpawningTime=startTime186 endSpawningTime=datetime(year,4,15,1,00,00)187 releaseParticles=32400 # Total number of particles to release (result with be approximately this number)188 elif spawning_ground=='northwest':189 startTime=datetime(year,1,1,1,00,00) 190 endTime=datetime(year,9,29,1,00,00) 191 startSpawningTime=startTime192 endSpawningTime=datetime(year,5,1,1,00,00)193 releaseParticles=22950194 elif spawning_ground=='viking':195 startTime=datetime(year,2,1,1,00,00) 196 endTime=datetime(year,9,29,1,00,00) 197 startSpawningTime=startTime198 endSpawningTime=datetime(year,5,15,1,00,00)199 releaseParticles=27000200 else:201 print ("spawning_ground is not correctly defined")202#Find forcing files needed based on months:203 startDay = datetime(startTime.year,startTime.month,1)204 endDay = datetime(endTime.year,endTime.month,1)205 month_range = [startDay]206 while startDay<endDay:207 startDay = startDay + relativedelta(months=1)208 month_range.append(startDay)209 pattern_svim=[]210 for i in month_range:211 x='/Volumes/Untitled/ROMS_files/SVIM_compressed/'+str(i.year)+'/'+'ocean_avg_'+str(i.year)+str(i.month).zfill(2)+'01.nc4'212 pattern_svim.append(x)213 hoursBetweenTimestepInROMSFiles=24214 shapefile='/Volumes/Untitled/Sustain/Spawning_grounds/Shapefiles_Gio_KINO/'+str(spawning_ground)+'.shp'215 print ("=> Using shapefile %s"%(shapefile))216 outputFilename, animationFilename, plotFilename = createOutputFilenames(startTime,endTime,verticalBehavior,spawning_ground)217 print ("Result files will be stored as:\nnetCDF=> %s\nmp4=> %s"%(outputFilename,animationFilename))218 ...
runspawningserver.py
Source:runspawningserver.py
1#!/usr/bin/env python2# Licensed to Cloudera, Inc. under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. Cloudera, Inc. licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing, software13# distributed under the License is distributed on an "AS IS" BASIS,14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15# See the License for the specific language governing permissions and16# limitations under the License.17import desktop.lib.eventlet_util18import logging19import os20import sys21from django.core.management.base import BaseCommand22from desktop import conf23import spawning.spawning_controller24from desktop.lib.daemon_utils import drop_privileges_if_necessary25from django.utils.translation import ugettext as _26SPAWNING_SERVER_HELP = r"""27 Run Hue using the Spawning WSGI server in asynchronous mode.28"""29SPAWNING_SERVER_OPTIONS = {30 'access_log_file': os.devnull,31 'backdoor': None,32 'chuid': None,33 'coverage': None,34 'daemonize': None,35 'deadman_timeout': 1,36 'factory': 'spawning.django_factory.config_factory',37 'host': conf.HTTP_HOST.get(),38 'max_age': None,39 'max_memory': 0,40 'no_keepalive': None,41 'pidfile': None,42 'port': conf.HTTP_PORT.get(),43 'processes': 1,44 'reload': None,45 'restart_args': None,46 'server_user': conf.SERVER_USER.get(),47 'server_group': conf.SERVER_GROUP.get(),48 'ssl_certificate': conf.SSL_CERTIFICATE.get(),49 'ssl_private_key': conf.SSL_PRIVATE_KEY.get(),50 'status_host': '',51 'status_port': 0,52 'stderr': None,53 'stdout': None,54 'sysinfo': None,55 'threads': 0,56 'verbose': None,57 'watch': None58}59LOG = logging.getLogger(__name__)60class Command(BaseCommand):61 help = _("Spawning Server for Hue.")62 def handle(self, *args, **options):63 from django.conf import settings64 from django.utils import translation65 if not conf.ENABLE_SERVER.get():66 LOG.info("Hue is configured to not start its own web server.")67 sys.exit(0)68 # Activate the current language, because it won't get activated later.69 try:70 translation.activate(settings.LANGUAGE_CODE)71 except AttributeError:72 pass73 runspawningserver()74 def usage(self, subcommand):75 return SPAWNING_SERVER_HELP76def runspawningserver():77 try:78 sock = spawning.spawning_controller.bind_socket(SPAWNING_SERVER_OPTIONS)79 except Exception, ex:80 LOG.error('Could not bind port %s: %s. Exiting' % (str(SPAWNING_SERVER_OPTIONS['port']), ex,))81 return82 drop_privileges_if_necessary(SPAWNING_SERVER_OPTIONS)83 factory = SPAWNING_SERVER_OPTIONS['factory']84 pos_args = ['desktop.settings']85 argv_str_format = '--factory=%s %s --port %s -s %d -t %d'86 argv_str = argv_str_format % (SPAWNING_SERVER_OPTIONS['factory'],87 pos_args[0],88 SPAWNING_SERVER_OPTIONS['port'],89 SPAWNING_SERVER_OPTIONS['processes'],90 SPAWNING_SERVER_OPTIONS['threads'])91 factory_args = {92 'access_log_file': SPAWNING_SERVER_OPTIONS['access_log_file'],93 'args': pos_args,94 'argv_str': argv_str,95 'coverage': SPAWNING_SERVER_OPTIONS['coverage'],96 'deadman_timeout': SPAWNING_SERVER_OPTIONS['deadman_timeout'],97 'host': SPAWNING_SERVER_OPTIONS['host'],98 'max_age' : SPAWNING_SERVER_OPTIONS['max_age'],99 'no_keepalive' : SPAWNING_SERVER_OPTIONS['no_keepalive'],100 'num_processes': SPAWNING_SERVER_OPTIONS['processes'],101 'pidfile': SPAWNING_SERVER_OPTIONS['pidfile'],102 'port': SPAWNING_SERVER_OPTIONS['port'],103 'reload': SPAWNING_SERVER_OPTIONS['reload'],104 'ssl_certificate': SPAWNING_SERVER_OPTIONS['ssl_certificate'],105 'ssl_private_key': SPAWNING_SERVER_OPTIONS['ssl_private_key'],106 'status_host': SPAWNING_SERVER_OPTIONS['status_host'] or SPAWNING_SERVER_OPTIONS['host'],107 'status_port': SPAWNING_SERVER_OPTIONS['status_port'],108 'sysinfo': SPAWNING_SERVER_OPTIONS['sysinfo'],109 'threadpool_workers': SPAWNING_SERVER_OPTIONS['threads'],110 'verbose': SPAWNING_SERVER_OPTIONS['verbose'],111 'watch': SPAWNING_SERVER_OPTIONS['watch']112 }113 os.environ['HUE_SPAWNING'] = 'yes'114 spawning.spawning_controller.start_controller(sock, factory, factory_args)115if __name__ == '__main__':...
spawningTypes.py
Source:spawningTypes.py
1from AdaptivePELE.constants import blockNames2class SPAWNING_TYPES:3 sameWeight, inverselyProportional, epsilon, simulatedAnnealing, FAST, variableEpsilon, UCB, independent, REAP, null, independentMetric, ProbabilityMSMCalculator, MetastabilityMSMCalculator, UncertaintyMSMCalculator, IndependentMSMCalculator = list(range(15))4SPAWNING_TYPE_TO_STRING_DICTIONARY = {5 SPAWNING_TYPES.independent: blockNames.StringSpawningTypes.independent,6 SPAWNING_TYPES.sameWeight: blockNames.StringSpawningTypes.sameWeight,7 SPAWNING_TYPES.inverselyProportional: blockNames.StringSpawningTypes.inverselyProportional,8 SPAWNING_TYPES.epsilon: blockNames.StringSpawningTypes.epsilon,9 SPAWNING_TYPES.FAST: blockNames.StringSpawningTypes.fast,10 SPAWNING_TYPES.variableEpsilon: blockNames.StringSpawningTypes.variableEpsilon,11 SPAWNING_TYPES.simulatedAnnealing: blockNames.StringSpawningTypes.simulatedAnnealing,12 SPAWNING_TYPES.UCB: blockNames.StringSpawningTypes.UCB,13 SPAWNING_TYPES.REAP: blockNames.StringSpawningTypes.REAP,14 SPAWNING_TYPES.null: blockNames.StringSpawningTypes.null,15 SPAWNING_TYPES.independentMetric: blockNames.StringSpawningTypes.independentMetric,16 SPAWNING_TYPES.ProbabilityMSMCalculator: blockNames.StringSpawningTypes.ProbabilityMSMCalculator,17 SPAWNING_TYPES.MetastabilityMSMCalculator: blockNames.StringSpawningTypes.MetastabilityMSMCalculator,18 SPAWNING_TYPES.UncertaintyMSMCalculator: blockNames.StringSpawningTypes.UncertaintyMSMCalculator,19 SPAWNING_TYPES.IndependentMSMCalculator: blockNames.StringSpawningTypes.IndependentMSMCalculator20}21MSMSpawning = set([blockNames.StringSpawningTypes.ProbabilityMSMCalculator, blockNames.StringSpawningTypes.MetastabilityMSMCalculator, blockNames.StringSpawningTypes.UncertaintyMSMCalculator, blockNames.StringSpawningTypes.IndependentMSMCalculator])22class EPSILON_VARIATION_TYPES:23 linearVariation, contactsVariation = list(range(2))24EPSILON_VARIATION_TYPE_TO_STRING_DICTIONARY = {25 EPSILON_VARIATION_TYPES.linearVariation: blockNames.VariableEpsilonTypes.linearVariation,26 EPSILON_VARIATION_TYPES.contactsVariation: blockNames.VariableEpsilonTypes.contactsVariation,27}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!