I need to integrate two files, one in python the other in julia. More precisely, I need to call a julia function, snapshot_sphere(b,h,data,m,r,d), from a python file. Moreover,I need to provide the parameters via the command line. I had difficulties in using PyJulia. For the moment an error message is persisting:
ModuleNotFoundError: No module named '_ctypes'
I found some dependencies related to libffi-dev. Installing it did not change anything. What should I do to call the julia function from the python file ?
Here is the code of the python file:
import argparse
from julia import Main
from PIL import Image, ImageShow
parser = argparse.ArgumentParser(description="Projection of the picture")
parser.add_argument(
"-f",
"--filename",
type=str,
action="store",
dest="fname",
help="Name of the picture"
)
parser.add_argument(
"-d",
"--density",
type=int,
action="store",
dest="density",
help="Number samples per Pixel"
)
parser.add_argument(
"-r",
"--radius",
type=float,
action="store",
dest="radius",
help="Radius of the sphere"
)
parser.add_argument(
"-x",
type=float,
action="store",
dest="x",
help="X coordinate of the center"
)
parser.add_argument(
"-y",
type=float,
action="store",
dest="y",
help="Y coordinate of the center"
)
parser.add_argument(
"-z",
type=float,
action="store",
dest="z",
help="Z coordinate of the center"
)
args = parser.parse_args()
print(args)
m = (args.x, args.y, args.z)
print(m)
im = Image.open(args.fname)
b, h = im.size
data = list(im.getdata())
Main.b = b
Main.h = h
Main.m = m
Main.r = args.radius
Main.d = args.density
Main.data = data
Main.include("projekt1.jl")
result = Main.eval("snapshot_sphere(b,h,data,m,r,d)")
Related
I am not familiar with Python, trying to build some DNN. So when I tried to parse some arguments I got this error in main.
usage: main.py [-h] [-j N] [--resume PATH] [--epochs N] [--start-epoch N] [-b N] [--lr LR]
[--weight-decay W] [-e] [--print-freq N]
DIR
main.py: error: the following arguments are required: DIR
Here is some part of the code:
# Parse arguments and prepare program
parser = argparse.ArgumentParser(description='Training and Using ColorNet')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('-j', '--workers', default=0, type=int, metavar='N', help='number of data loading workers (default: 0)')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to .pth file checkpoint (default: none)')
parser.add_argument('--epochs', default=50, type=int, metavar='N', help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N', help='manual epoch number (overridden if loading from checkpoint)')
parser.add_argument('-b', '--batch-size', default=16, type=int, metavar='N', help='size of mini-batch (default: 16)')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float, metavar='LR', help='learning rate at start of training')
parser.add_argument('--weight-decay', '--wd', default=1e-10, type=float, metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true', help='use this flag to validate without training')
parser.add_argument('--print-freq', '-p', default=10, type=int, metavar='N', help='print frequency (default: 10)')
# Current best losses
best_losses = 1000.0
use_gpu = torch.cuda.is_available()
def main():
global args, best_losses, use_gpu
args = parser.parse_args()
print('Arguments: {}'.format(args))
I read some comments to change
parser.parse_args()
to
parser.parse_args(args)
but it didn't work :)
As DIR doesn't have a default value, you need to supply one when running the program. The easiest way to do this is via a command line interface. Consult the documentation of the library you are using for further hints on that.
when i am trying to install that particular module utils.logger ,it is showing
ERROR: Could not find a version that satisfies the requirement utils.logger (from versions: none)
ERROR: No matching distribution found for utils.logger.Sorry if my way of interpretation of question is not in an appropriate manner as i am a novice.
This is the code
import logging
import functools
import argparse
import os
from datetime import datetime
from utils.logger import setlogger
from utils.train_utils import train_utils
args = None
def parse_args():
parser = argparse.ArgumentParser(description='Train')
# basic parameters
parser.add_argument('--model_name', type=str, default='cnn_2d', help='the name of the model')
parser.add_argument('--data_name', type=str, default='CWRUSlice', help='the name of the data')
parser.add_argument('--data_dir', type=str, default= "E:\Data\西储大学轴承数据中心网站", help='the directory of the data')
parser.add_argument('--normlizetype', type=str, choices=['0-1', '1-1', 'mean-std'], default='0-1', help='data normalization methods')
parser.add_argument('--processing_type', type=str, choices=['R_A', 'R_NA', 'O_A'], default='R_A',
help='R_A: random split with data augmentation, R_NA: random split without data augmentation, O_A: order split with data augmentation')
parser.add_argument('--cuda_device', type=str, default='0', help='assign device')
parser.add_argument('--checkpoint_dir', type=str, default='./checkpoint', help='the directory to save the model')
parser.add_argument("--pretrained", type=bool, default=True, help='whether to load the pretrained model')
parser.add_argument('--batch_size', type=int, default=64, help='batchsize of the training process')
parser.add_argument('--num_workers', type=int, default=0, help='the number of training process')
# optimization information
parser.add_argument('--opt', type=str, choices=['sgd', 'adam'], default='adam', help='the optimizer')
parser.add_argument('--lr', type=float, default=0.001, help='the initial learning rate')
parser.add_argument('--momentum', type=float, default=0.9, help='the momentum for sgd')
parser.add_argument('--weight_decay', type=float, default=1e-5, help='the weight decay')
parser.add_argument('--lr_scheduler', type=str, choices=['step', 'exp', 'stepLR', 'fix'], default='fix', help='the learning rate schedule')
parser.add_argument('--gamma', type=float, default=0.1, help='learning rate scheduler parameter for step and exp')
parser.add_argument('--steps', type=str, default='9', help='the learning rate decay for step and stepLR')
# save, load and display information
parser.add_argument('--max_epoch', type=int, default=100, help='max number of epoch')
parser.add_argument('--print_step', type=int, default=100, help='the interval of log training information')
args = parser.parse_args()
return args
if name == 'main':
args = parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda_device.strip()
# Prepare the saving path for the model
sub_dir = args.model_name+'_'+args.data_name + '_' + datetime.strftime(datetime.now(), '%m%d-%H%M%S')
save_dir = os.path.join(args.checkpoint_dir, sub_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# set the logger
setlogger(os.path.join(save_dir, 'training.log'))
# save the args
for k, v in args.__dict__.items():
logging.info("{}: {}".format(k, v))
trainer = train_utils(args, save_dir)
trainer.setup()
trainer.train()
my code is a below:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--episode', type=int, default=2000,
help='number of episode to run')
parser.add_argument('-b', '--batch_size', type=int, default=32,
help='batch size for experience replay')
parser.add_argument('-i', '--initial_invest', type=int, default=20000,
help='initial investment amount')
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
parser.add_argument('-w', '--weights', type=str, help='a trained model weights')
args = parser.parse_args()
maybe_make_dir('weights')
maybe_make_dir('portfolio_val')
Error:-
usage: [-h] [-e EPISODE] [-b BATCH_SIZE] [-i INITIAL_INVEST] -m MODE
[-w WEIGHTS]
: error: the following arguments are required: -m/--mode
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
So in the above error i am not able to understand what is the mistake i have also imported all the libraries correctly.
try changing this:
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
to this: (notice nargs and const)
parser.add_argument('-m', '--mode',nargs='?', const='' , type=str, required=True,
help='either "train" or "test"')
then you can add this to your code:
if len(args.mode) == 0:
print('-m either "train" or "test"')
import sys
sys.exit() #closes python script
as explained here: https://stackoverflow.com/a/58396626/13093413
main.py --mode a [--environment] [--db_server]
main.py --mode b [--environment] [--time] [--file] [--save]
main.py --mode c [--environment] [--render] [--file]
In each different modes, I need a different menu with some common arguments. The help must be specified for each different modes. Is possible to do this with argparse or I need a different module? It must first parse the mode and then decide what menu will choose from the existing 3 different menus (with some common arguments).
Thanks.
The solution is subparsers.
import argparse
if __name__ == "__main__":
# create the top-level parser
my_parser = argparse.ArgumentParser(
prog="PROG",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# create sub-parser
sub_parsers = my_parser.add_subparsers(
title="Operating modes",
description="Select the operating mode",
dest="mode",
required=True,
)
# create the parser for the "agent" sub-command
parser_agent = sub_parsers.add_parser("agent", help="Agent mode")
parser_agent.add_argument(
"--db_server", type=str, help="DB server name", default="localhost"
)
parser_agent.add_argument(
"--update_interval",
type=int,
help="Interval of updating policy parameters",
default=64,
)
# create the parse for the "learner" sub-command
parser_learner = sub_parsers.add_parser("learner", help="Learner mode")
parser_learner.add_argument(
"-e",
"--environment",
type=str,
help="Only OpenAI Gym/PyBullet environments are available!",
required=True,
)
parser_learner.add_argument(
"-t",
"--max_steps",
type=int,
help="Number of agent's steps",
default=int(1e6),
)
# create the parse for the "tester" sub-command
parser_tester = sub_parsers.add_parser("tester", help="Tester mode")
parser_tester.add_argument(
"-t",
"--max_steps",
type=int,
help="Number of agent's steps",
default=int(1e6),
)
parser_tester.add_argument(
"--render", action="store_true", help="Render the environment"
)
parser_tester.add_argument(
"-f", "--model_path", type=str, help="Path to saved model"
)
args = my_parser.parse_args()
print(args)
if args.mode == "agent":
print('mode 1')
elif args.mode == "learner":
print('mode 2')
elif args.mode == "tester":
print('mode 3')
Given an argument parser with n arguments, where I change the default value of only a small subset every run from the command line, is there a clean way of extracting a dict/namespace of all the non-default k,v arguments?
parser = argparse.ArgumentParser()
parser.add_argument("--a",type=str,default='a')
parser.add_argument("--b",type=str,default='b')
parser.add_argument("--c",type=str,default='c')
parser.add_argument("--d",type=str,default='d')
And
python run.py --a "e"
I would like to have
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--a",type=str,default='a')
parser.add_argument("--b",type=str,default='b')
parser.add_argument("--c",type=str,default='c')
parser.add_argument("--d",type=str,default='d')
non_default = parse_non_default(parser) # non_default = {'a':'e'}
You could lookup the parser and compare which values differenciate:
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--a", type=str, default='a')
parser.add_argument("--b", type=str, default='b')
parser.add_argument("--c", type=str, default='c')
parser.add_argument("--d", type=str, default='d')
parser.add_argument("--n", type=int, default=999)
args = parser.parse_args(['--a', 'e']) # Test CLI arguments!
non_default = {
opt.dest: getattr(args, opt.dest)
for opt in parser._option_string_actions.values()
if hasattr(args, opt.dest) and opt.default != getattr(args, opt.dest)
}
print(non_default)
main()
Out:
{'a': 'e'}