use python to run oq

502 views
Skip to first unread message

Jason Zhu

unread,
Jun 14, 2022, 12:50:14 PM6/14/22
to OpenQuake Users
Hi everyone,

I encounter a problem when I try to use Python to run the oq. Here is part of the screenshot and it keeps raising errors. Would appreciate any discussion and suggestions. Thank you , sincerely


code
with logs.init('job', 'C:\Program Files\OpenQuake Engine\demos\hazard\AreaSourceClassicalPSHA.zip') as log:
calc = calculators(log.get_oqparam(), log.calc_id)
print("OQ good to go!")
calc.run()


error
Traceback (most recent call last):
  File "C:\Users\hzhu36\Desktop\pythonProject\main.py", line 55, in <module>
    with logs.init('job', 'C:\Program Files\OpenQuake Engine\demos\hazard\AreaSourceClassicalPSHA.zip') as log:
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 261, in init
    return LogContext(job_ini, calc_id, log_level, log_file, user_name, hc_id)
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 173, in __init__
    self.calc_id = dbcmd(
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 51, in dbcmd
    return res.get()
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\baselib\parallel.py", line 404, in get
    raise etype(msg)
TypeError:
  File "C:\Program Files\OpenQuake Engine\python3.6\lib\site-packages\openquake\baselib\parallel.py", line 416, in new
    val = func(*args)
TypeError: create_job() takes 2 positional arguments but 6 were given
Screenshot 2022-06-14 124924.png

Jason Zhu

unread,
Jun 14, 2022, 7:36:49 PM6/14/22
to OpenQuake Users
another follow up question:

If Im running for hazard analysis (Classical PSHA) with different Vs30 values, are there any methods to do that? 
Im thinking using python to iterative the calculations, but could be computationally tedious 
thanks!
Sincerely

Marco Pagani

unread,
Jun 15, 2022, 1:41:16 AM6/15/22
to OpenQuake Users

Please read the manual https://www.globalquakemodel.org/openquake. See section 3.4


--
You received this message because you are subscribed to the Google Groups "OpenQuake Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openquake-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openquake-users/6d047e2b-456d-4df0-a649-5fb28b228754n%40googlegroups.com.

Jason Zhu

unread,
Jun 16, 2022, 12:01:46 PM6/16/22
to OpenQuake Users
Hi Im working Canada SHM6 and I have read the Manuel. The prescribed way does not match the calculation principles. 
Also I was wondering if the code in the advanced manuel is correct. 
I used the exactly same command 
with logs.init('job','/Users/huanruzhu/Desktop/CanadaSHM6/OpenQuake_model_files/runfiles/east/CanadaSHM6-trial_EastCollapsedSimple_140.mps.ini') as log:
calc = calculators(log.get_oqparam(), log.calc_id)
print("OQ good to go!")
calc.run()

but it raises error

Traceback (most recent call last):
  File "/Users/huanruzhu/Desktop/SHM6 Python/main.py", line 63, in <module>
    with logs.init('job','/Users/huanruzhu/Desktop/CanadaSHM6/OpenQuake_model_files/runfiles/east/CanadaSHM6-trial_EastCollapsedSimple_140.mps.ini') as log:
  File "/Users/huanruzhu/PycharmProjects/OpenQuake/venv/lib/python3.10/site-packages/openquake/commonlib/logs.py", line 261, in init

    return LogContext(job_ini, calc_id, log_level, log_file, user_name, hc_id)
  File "/Users/huanruzhu/PycharmProjects/OpenQuake/venv/lib/python3.10/site-packages/openquake/commonlib/logs.py", line 173, in __init__
    self.calc_id = dbcmd(
  File "/Users/huanruzhu/PycharmProjects/OpenQuake/venv/lib/python3.10/site-packages/openquake/commonlib/logs.py", line 49, in dbcmd
    res = sock.send((action,) + args)
  File "/Users/huanruzhu/PycharmProjects/OpenQuake/venv/lib/python3.10/site-packages/openquake/baselib/zeromq.py", line 164, in send
    raise TimeoutError(
openquake.baselib.zeromq.TimeoutError: While sending ('create_job', '/Users/huanruzhu/oqdata', 'classical', 'CanadaSHM6-trial Eastern Canada Collapsed Rates Simple Vs30=140m/s ALL EASTERN CITIES', None, None); probably the DbServer is off
Screen Shot 2022-06-16 at 9.11.54 AM.png

Peter Pažák

unread,
Jun 16, 2022, 1:06:01 PM6/16/22
to OpenQuake Users
Maybe webui needs to run on the machine when trying to run the calculation programmatically - can you try when it is running?

Peter

Dátum: štvrtok 16. júna 2022, čas: 18:01:46 UTC+2, odosielateľ: zhujas...@gmail.com

Jason Zhu

unread,
Jun 16, 2022, 4:26:21 PM6/16/22
to OpenQuake Users
Hi Peter,
Thank you so much for the feedback. When I opened the webui, the raised error changed but still there. It shows the following

C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\python.exe C:/Users/hzhu36/Desktop/pythonProject/main.py

Traceback (most recent call last):
  File "C:\Users\hzhu36\Desktop\pythonProject\main.py", line 49, in <module>
    with logs.init('job', 'C:\Program Files\OpenQuake Engine\demos\hazard\AreaSourceClassicalPSHA\job.ini') as log:

  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 261, in init
    return LogContext(job_ini, calc_id, log_level, log_file, user_name, hc_id)
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 173, in __init__
    self.calc_id = dbcmd(
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\commonlib\logs.py", line 51, in dbcmd
    return res.get()
  File "C:\Users\hzhu36\AppData\Local\Programs\Python\Python310\lib\site-packages\openquake\baselib\parallel.py", line 404, in get
    raise etype(msg)
TypeError:
  File "C:\Program Files\OpenQuake Engine\python3.6\lib\site-packages\openquake\baselib\parallel.py", line 416, in new
    val = func(*args)
TypeError: create_job() takes 2 positional arguments but 6 were given

thanks again

Peter Pažák

unread,
Jun 16, 2022, 7:06:44 PM6/16/22
to OpenQuake Users
Hi,

from the above it is visible it tries to use both python 3.10 (which is not yet officially supported) and python 3.6 from older OQ (v3.11?)
installation which is maybe the webui you run form the start menu?

You should start both the OQ webui and OQ console for the same version of OQ and enter "python"+enter in the console.
In the console when I entered line by line it completed successfully and produced outputs for a demo input.
(running "python main.py" in the console it started to create a lot of failed jobs again and again, not sure why - maybe someone else can explain).

Peter 

from openquake.commonlib import logs
from openquake.calculators.base import calculators

with logs.init('job','c:/Users/ppazak/openquake/demos/hazard/AreaSourceClassicalPSHA/job.ini') as log:
  calc = calculators(log.get_oqparam(), log.calc_id)
  calc.run()

Dátum: štvrtok 16. júna 2022, čas: 22:26:21 UTC+2, odosielateľ: zhujas...@gmail.com

Jason Zhu

unread,
Jun 17, 2022, 2:13:22 PM6/17/22
to OpenQuake Users
Hi Peter,

thanks again for your time and suggestion. Indeed, running python via IDE such as Pycharm always raises error. 

I tried with typing codes in console. Again, there is no output. Just wonder if I did it correctly

thanks 
Screenshot 2022-06-17 141113.png

Peter Pažák

unread,
Jun 20, 2022, 8:37:45 AM6/20/22
to OpenQuake Users
Hi, as the python you are using in the above is 3.6, I guess the engine activated is 3.11.
However the approach (lines entered into the console are for >=3.12) so that might be the reason for the strange message you get.

Peter

Dátum: piatok 17. júna 2022, čas: 20:13:22 UTC+2, odosielateľ: zhujas...@gmail.com

Jason Zhu

unread,
Jun 26, 2022, 9:51:12 AM6/26/22
to OpenQuake Users
Hi Peter,

thanks for your reply and i just wonder if the python version has to be 3.6.6 
What if i use 3.7 and the console version is now above 3.12
thanks

Jason Zhu

unread,
Jun 26, 2022, 9:58:16 AM6/26/22
to OpenQuake Users
you also mention you try the code and it runs without issue. Could you let me know the version of oq and also python you used? thank you 

On Monday, 20 June 2022 at 08:37:45 UTC-4 peter...@gmail.com wrote:

azizur noor

unread,
Jan 21, 2023, 11:54:36 PM1/21/23
to OpenQuake Users
Dear Jason Zhu, 
 
Were you finally able to run the OQ using Python? If you have, could you please suggest how did you prepare the input files for OQ using Python?
Thanks a lot in advance for your time.
Reply all
Reply to author
Forward
0 new messages