def main(): from julia import Julia julia = Julia() julia.eval("@eval Main import Base.MainInclude: include") from julia import Main Main.include("test.jl") arr = np.array([1, 2, 3]) ret = Main.twice_array(arr) print("arr=", arr) print("ret=", ret) jlsin = Main.sin Main.rad45degree = np.pi / 4 print(jlsin(Main.rad45degree), np.sqrt(2) / 2)
class JuliaMagics(Magics): """A set of magics useful for interactive work with Julia. """ def __init__(self, shell): """ Parameters ---------- shell : IPython shell """ super(JuliaMagics, self).__init__(shell) print("Initializing Julia interpreter. This may take some time...", end='') # Flush, otherwise the Julia startup will keep stdout buffered sys.stdout.flush() self.julia = Julia(init_julia=True) print() @line_cell_magic def julia(self, line, cell=None): """ Execute code in Julia, and pull some of the results back into the Python namespace. """ src = compat.unicode_type(line if cell is None else cell) try: ans = self.julia.eval(src) except JuliaError as e: print(e.message, file=sys.stderr) ans = None return ans
class JuliaMagics(Magics): """A set of magics useful for interactive work with Julia. """ def __init__(self, shell): """ Parameters ---------- shell : IPython shell """ super(JuliaMagics, self).__init__(shell) print("Initializing Julia interpreter. This may take some time...", end='') # Flush, otherwise the Julia startup will keep stdout buffered sys.stdout.flush() self.julia = Julia(init_julia=True) print() @line_cell_magic def julia(self, line, cell=None): """ Execute code in Julia, and pull some of the results back into the Python namespace. """ src = str(line if cell is None else cell) return self.julia.eval(src)
def solve_it(input_data): """ Run appropriate jl script to generate an integer. """ jl = Julia() jl.include("any_integer.jl") result = jl.eval(f'generate_any_integer("{input_data}")') return result
def solve_it(input_data): """ Run appropriate jl script to solve the knapsack problem in Julia. """ jl = Julia() jl.include("knapsack.jl") output_data = jl.eval(f'optimise_knapsack("{input_data}", timeout=60)') return output_data
def setup(): jul = Julia() include(jul, 'setup.jl') # Make Julia functions and types exported from # DifferentialEquations accessible: jul.add_module_functions('DifferentialEquations') # pysolve has to be treated manually: # See: https://github.com/JuliaPy/pyjulia/issues/117#issuecomment-323498621 jul.pysolve = jul.eval('pysolve') return jul
class JuliaCompleter(Singleton): def __init__(self, julia=None): from julia import Julia self.julia = Julia() if julia is None else julia self.magic_re = re.compile(r".*(\s|^)%%?julia\s*") # With this regexp, "=%julia Cha<tab>" won't work. But maybe # it's better to be conservative here. @cached_property def jlcomplete(self): return self.julia.eval(""" import REPL (str, pos) -> begin ret, ran, should_complete = REPL.completions(str, pos) return ( map(REPL.completion_text, ret), (first(ran), last(ran)), should_complete, ) end """) def julia_completions(self, full_text: str, offset: int): self.last_text = full_text match = self.magic_re.match(full_text) if not match: return [] prefix_len = match.end() jl_pos = offset - prefix_len jl_code = full_text[prefix_len:] texts, (jl_start, jl_end), should_complete = \ self.jlcomplete(jl_code, jl_pos) start = jl_start - 1 + prefix_len end = jl_end + prefix_len completions = [Completion(start, end, txt) for txt in texts] self.last_completions = completions # if not should_complete: # return [] return completions
import numpy as np import os from julia import Julia jl = Julia(compiled_modules=False) script_path = os.path.abspath(__file__) jl.eval('push!(LOAD_PATH, "' + script_path[:script_path.rfind('/')] + '/")') from julia import MBD as MBD_jl class MBD: ''' Class encapsulating the Mass-based dissimilarity metric. Args: n_features_to_select (int): Number of i-tree space partitioning models to use. Attributes: n_features_to_select (int): Number of i-tree space partitioning models to use. ''' def __init__(self, num_itrees=10): self.num_itrees = num_itrees def get_dist_func(self, data): ''' Get computed metric function from data. Args: data (numpy.ndarray): Matrix of training samples. Returns:
Requires `pyjulia` package from https://github.com/JuliaPy/pyjulia """ import numpy as np from ase.calculators.calculator import Calculator from ase.optimize.optimize import Optimizer from julia import Julia julia = Julia() julia.using("JuLIP") # Workaround limitiation in PyCall that does not allow types to be called # https://github.com/JuliaPy/PyCall.jl/issues/319 ASEAtoms = julia.eval('ASEAtoms(a) = JuLIP.ASE.ASEAtoms(a)') ASECalculator = julia.eval('ASECalculator(c) = JuLIP.ASE.ASECalculator(c)') fixedcell = julia.eval('fixedcell(a) = JuLIP.Constraints.FixedCell(a)') variablecell = julia.eval( 'variablecell(a) = JuLIP.Constraints.VariableCell(a)') class JulipCalculator(Calculator): """ ASE-compatible Calculator that calls JuLIP.jl for forces and energy """ implemented_properties = ['forces', 'energy', 'stress'] default_parameters = {} name = 'JulipCalculator' def __init__(self, julip_calculator):
from julia import Julia jul = Julia(compiled_modules=False) # func = jul.include("speed_planner.jl") jul.eval('include("speed_planner.jl")') get_best_possible_action = jul.eval('get_best_possible_action') print(get_best_possible_action) cart_start_state_list = [8, 30, 0, 2] cart_goal_position = [7, 1] pedestrians_list = [4, 3, 14, 30, 3, 22, 14, 1, 11, 22, 1, 30, 12, 4, 1, 1] possible_goal_positions = [1, 1, 1, 30, 14, 30, 14, 1] initial_human_goal_distribution_list = [ 0.15, 0.3, 0.5, 0.05, 0.1, 0.3, 0.1, 0.5, 0.5, 0.25, 0.1, 0.15, 0.05, 0.5, 0.35, 0.1 ] given_astar_path = [ 8, 30, 7, 30, 7, 29, 7, 28, 7, 27, 7, 26, 7, 25, 7, 24, 7, 23, 7, 22, 6, 22, 6, 21, 5, 21, 5, 20, 5, 19, 5, 18, 5, 17, 5, 16, 5, 15, 5, 14, 5, 13, 5, 12, 5, 11, 5, 10, 5, 9, 5, 8, 5, 7, 5, 6, 5, 5, 5, 4, 5, 3, 5, 2, 5, 1, 6, 1, 7, 1 ]
import matplotlib import numpy as np import matplotlib.pyplot as plt import csv from julia import Julia julia = Julia() julia.eval("@eval Main import Base.MainInclude: include") from julia import Main Main.include("interpolate.jl") data = np.loadtxt('input.csv', delimiter=',') k = np.array(data[:, 0]) energy = np.array(data[:, 1]) knew = np.linspace(k[0], k[-1], num=k.size * 100) energynew = Main.interpolate(k, energy, knew) print(energynew) fig = plt.figure() plt.plot(k, energy, 'o') plt.plot(knew, energynew, '-') plt.ylim([min(energy), max(energy)]) plt.legend(['Raw data', '3D spline'], loc='best') plt.savefig("band_structure.png") new_band_structure = np.array(((k.size * 100) + 1, 2)) """ new_band_structure[:, 0] = knew new_band_structure[:, 1] = energynew with open("interpolated_band_structure.csv") as mycsv: