-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path__init__.py
More file actions
218 lines (190 loc) · 6.61 KB
/
__init__.py
File metadata and controls
218 lines (190 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Supercharged Java access from Python, built on JPype and jgo.
Use Java classes from Python:
>>> from scyjava import jimport
>>> System = jimport('java.lang.System')
>>> System.getProperty('java.version')
'1.8.0_252'
Use Maven artifacts from remote repositories:
>>> from scyjava import config, jimport
>>> config.enable_headless_mode()
>>> config.add_repositories({
... 'scijava.public': 'https://maven.scijava.org/content/groups/public',
... })
>>> config.endpoints.append('net.imagej:imagej:2.1.0')
>>> ImageJ = jimport('net.imagej.ImageJ')
>>> ij = ImageJ()
>>> formula = "10 * (Math.cos(0.3*p[0]) + Math.sin(0.3*p[1]))"
>>> ArrayImgs = jimport('net.imglib2.img.array.ArrayImgs')
>>> blank = ArrayImgs.floats(64, 16)
>>> sinusoid = ij.op().image().equation(blank, formula)
>>> print(ij.op().image().ascii(sinusoid))
,,,--+oo******oo+--,,,,,--+oo******o++--,,,,,--+oo******o++--,,,
...,--+ooo**oo++--,....,,--+ooo**oo++-,,....,,--+ooo**oo++-,,...
...,--++oooo++--,... ...,--++oooo++--,... ...,--++oooo++-,,...
..,--++++++--,.. ..,--++o+++--,.. .,,--++o+++--,..
..,,-++++++-,,. ..,,-++++++-,,. ..,--++++++-,,.
.,,--++++--,,. .,,--++++--,,. .,,--++++--,..
.,,--++++--,,. .,,-+++++--,,. .,,-+++++--,,.
..,--++++++--,.. ..,--++++++--,.. ..,--++++++-,,..
..,,-++oooo++-,,.. ..,,-++oooo++-,,.. ..,,-++ooo+++-,,..
...,,-++oooooo++-,,.....,,-++oooooo++-,,.....,,-++oooooo+--,,...
.,,,-++oo****oo++-,,,.,,,-++oo****oo+--,,,.,,,-++oo****oo+--,,,.
,,--++o***OO**oo++-,,,,--++o***OO**oo+--,,,,--++o***OO**oo+--,,,
---++o**OOOOOO**o++-----++o**OOOOOO*oo++-----++o**OOOOOO*oo++---
--++oo*OO####OO*oo++---++oo*OO####OO*oo++---++o**OO####OO*oo++--
+++oo*OO######O**oo+++++oo*OO######O**oo+++++oo*OO######O**oo+++
+++oo*OO######OO*oo+++++oo*OO######OO*oo+++++oo*OO######OO*oo+++
Bootstrap a Java installation:
>>> from scyjava import config, jimport
>>> config.set_java_constraints(fetch=True, vendor='zulu', version='17')
>>> System = jimport('java.lang.System')
cjdk: Installing JDK zulu:17.0.15 to /home/chuckles/.cache/cjdk
Download 100% of 189.4 MiB |##########| Elapsed Time: 0:00:02 Time: 0:00:02
Extract | | # | 714 Elapsed Time: 0:00:01
cjdk: Installing Maven to /home/chuckles/.cache/cjdk
Download 100% of 8.7 MiB |##########| Elapsed Time: 0:00:00 Time: 0:00:00
Extract | |# | 102 Elapsed Time: 0:00:00
>>> System.getProperty('java.vendor')
'Azul Systems, Inc.'
>>> System.getProperty('java.version')
'17.0.15'
Convert Java collections to Python:
>>> from scyjava import jimport
>>> HashSet = jimport('java.util.HashSet')
>>> moves = {'jump', 'duck', 'dodge'}
>>> fish = {'walleye', 'pike', 'trout'}
>>> jbirds = HashSet()
>>> for bird in ('duck', 'goose', 'swan'): jbirds.add(bird)
>>> from scyjava import to_python as j2p
>>> j2p(jbirds).isdisjoint(moves)
False
>>> j2p(jbirds).isdisjoint(fish)
True
Convert Python collections to Java:
>>> from scyjava import jimport
>>> HashSet = jimport('java.util.HashSet')
>>> jset = HashSet()
>>> pset = {1, 2, 3}
>>> from scyjava import to_java as p2j
>>> jset.addAll(p2j(pset))
True
>>> jset.toString()
'[1, 2, 3]'
"""
import logging
from functools import lru_cache
from typing import Any, Callable, Dict
from . import config, inspect
from ._arrays import is_arraylike, is_memoryarraylike, is_xarraylike
from ._convert import (
Converter,
JavaCollection,
JavaIterable,
JavaIterator,
JavaList,
JavaMap,
JavaObject,
JavaSet,
Priority,
_stock_java_converters,
_stock_py_converters,
add_java_converter,
add_py_converter,
java_converters,
py_converters,
to_java,
to_python,
)
from ._introspect import (
jreflect,
jsource,
)
from ._jvm import ( # noqa: F401
available_processors,
gc,
is_awt_initialized,
is_jvm_headless,
jimport,
jvm_started,
jvm_version,
memory_max,
memory_total,
memory_used,
shutdown_jvm,
start_jvm,
when_jvm_starts,
when_jvm_stops,
)
from ._script import enable_python_scripting
from ._types import (
JavaClasses,
is_jarray,
is_jboolean,
is_jbyte,
is_jcharacter,
is_jdouble,
is_jfloat,
is_jinteger,
is_jlong,
is_jshort,
isjava,
jarray,
jclass,
jinstance,
jstacktrace,
numeric_bounds,
)
from ._versions import compare_version, get_version, is_version_at_least
__version__ = get_version("scyjava")
__all__ = [
k
for k, v in globals().items()
if not k.startswith("_")
and hasattr(v, "__module__")
and v.__module__.startswith("scyjava.")
]
_logger = logging.getLogger(__name__)
# Set of module properties
_CONSTANTS: Dict[str, Callable] = {}
def constant(func: Callable[[], Any], cache=True) -> Callable[[], Any]:
"""
Turns a function into a property of this module
Functions decorated with this property must have a
leading underscore!
:param func: The function to turn into a property
"""
if func.__name__[0] != "_":
raise ValueError(
f"""Function {func.__name__} must have
a leading underscore in its name
to become a module property!"""
)
name = func.__name__[1:]
if cache:
func = (lru_cache(maxsize=None))(func)
_CONSTANTS[name] = func
return func
def __getattr__(name):
"""
Runs as a fallback when this module does not have an attribute.
:param name: The name of the attribute being searched for.
"""
if name in _CONSTANTS:
return _CONSTANTS[name]()
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
# -- JVM startup callbacks --
# NB: These must be performed last, because if this class is imported after the
# JVM is already running -- for example, if we are running in Jep mode, where
# Python is started from inside the JVM -- then these functions execute the
# callbacks immediately, which means the involved functions must be defined and
# functional at this point.
def _initialize_converters():
_logger.debug("Initializing type converters")
for converter in _stock_java_converters():
add_java_converter(converter)
_logger.debug("Java converters:{'\n-'.join(java_converters)}")
for converter in _stock_py_converters():
add_py_converter(converter)
_logger.debug("Python converters:{'\n-'.join(py_converters)}")
when_jvm_starts(_initialize_converters)