Java Code Examples for org.scijava.Context#getService()

The following examples show how to use org.scijava.Context#getService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestScriptEngine.java    From scijava-jupyter-kernel with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws ScriptException {
    // Only for testing purpose

    Context context = new Context();
    ScriptService scriptService = context.getService(ScriptService.class);
    ScriptLanguage scriptLanguage = scriptService.getLanguageByName("python");
    ScriptEngine engine = scriptLanguage.getScriptEngine();

    Object result = engine.eval("p=999\n555");
    System.out.println(result);

    scriptService = context.getService(ScriptService.class);
    scriptLanguage = scriptService.getLanguageByName("python");
    engine = scriptLanguage.getScriptEngine();
    
    result = engine.eval("555");
    System.out.println(result);

    context.dispose();
}
 
Example 2
Source File: SnippetCreator.java    From Scripts with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method is called when the plugin is loaded. See
 * {@link ij.plugin.PlugIn#run(java.lang.String)} Prompts user for a new
 * snippet that will be saved in {@code BAR/My_Routines/}
 *
 * @param arg
 *            ignored (Otherwise specified in plugins.config).
 */
@Override
public void run(final String arg) {
	Utils.shiftClickWarning();
	if (new GuiUtils().getFileCount(Utils.getLibDir()) == 0) {
		final YesNoCancelDialog query = new YesNoCancelDialog(null, "Install lib Files?",
				"Some of the code generated by this plugin assumes the adoption\n"
						+ "of centralized lib files, but none seem to exist in your local directory\n" + "("
						+ Utils.getLibDir() + ")\nWould you like to install them now?");
		if (query.cancelPressed()) {
			return;
		} else if (query.yesPressed()) {
			final Context context = (Context) IJ.runPlugIn("org.scijava.Context", "");
			final CommandService commandService = context.getService(CommandService.class);
			commandService.run(Installer.class, true);
			return;
		}
	}
	if (showDialog()) {
		if (sContents.length()>0)
			saveAndOpenSnippet();
		else
			IJ.showStatus(sFilename +" was empty. No file was saved...");
	}

}
 
Example 3
Source File: OpSearchResult.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public OpSearchResult(final Context context, final OpInfo info,
	final String baseDir)
{
	cacheService = context.getService(CacheService.class);
	convertService = context.getService(ConvertService.class);
	this.info = info;

	final Object shortSigKey = new ShortSigKey(info);
	final Object shortSigValue = cacheService.get(shortSigKey);
	if (shortSigValue == null) {
		shortSig = buildShortSig();
		cacheService.put(shortSigKey, shortSig);
	}
	else shortSig = (String) shortSigValue;

	props = new LinkedHashMap<>();

	props.put("Signature", info.toString());

	final String opType = info.getType().getName();
	props.put("Op type", opType.startsWith("net.imagej.ops.Ops$") ? //
		opType.substring(15).replace('$', '.') : opType);

	final String[] aliases = info.getAliases();
	if (aliases != null && aliases.length > 0) {
		props.put("Aliases", Arrays.toString(aliases));
	}

	props.put("Identifier", info.cInfo().getIdentifier());
	props.put("Location", ModuleSearcher.location(info.cInfo(), baseDir));
}
 
Example 4
Source File: ReadmeExampleTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testReadmesExample() throws Exception {
	// extract the example script
	final File readme = new File("README.md");
	final String contents = new String(FileUtils.readFile(readme), "UTF-8");
	final String telltale = String.format("```python%n");
	final int begin = contents.indexOf(telltale) + telltale.length();
	assertTrue(begin > telltale.length());
	assertTrue(contents.indexOf(telltale, begin) < 0);
	final int end = contents.indexOf(String.format("```%n"), begin);
	assertTrue(end > 0);
	final String snippet = contents.substring(begin, end);
	assertTrue(snippet.startsWith("# @ImageJ ij"));

	final Context context = new Context();
	final ScriptService script = context.getService(ScriptService.class);

	// create mock ImageJ gateway
	script.addAlias("ImageJ", Mock.class);
	final ScriptModule module =
		script.run("op-example.py", snippet, true).get();
	assertNotNull(module);
	module.run();

	final Mock ij = context.getService(Mock.class);
	assertEquals(3, ij.images.size());
	assertEquals(11.906, ij.getPixel("sinusoid", 50, 50), 1e-3);
	assertEquals(100, ij.getPixel("gradient", 50, 50), 1e-3);
	assertEquals(111.906, ij.getPixel("composite", 50, 50), 1e-3);
}
 
Example 5
Source File: IO.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Registers the given ImgPlus with the RefManagerService in the provided
 * component's Context.
 */
private static void register(
	@SuppressWarnings("rawtypes") final List<? extends SCIFIOImgPlus> imgPlus,
	final AbstractImgIOComponent component)
{
	final Context ctx = component.getContext();
	final RefManagerService refManagerService = ctx.getService(
		RefManagerService.class);
	for (final SCIFIOImgPlus<?> img : imgPlus) {
		refManagerService.manage(img, ctx);
	}
}
 
Example 6
Source File: IO.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param source the source to resolve
 * @param context
 * @return the resolved location or <code>null</code> if the resolving failed
 */
private static Location resolve(final String source, final Context context) {
	final LocationService loc = context.getService(
		LocationService.class);
	Location location = null;
	try {
		location = loc.resolve(source);
	}
	catch (final URISyntaxException e) {
		resolveError(source, e);
	}
	return location;
}
 
Example 7
Source File: Main.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void main(final String[] args) throws IOException {
	final Context context = new Context(OpService.class);
	final OpService ops = context.getService(OpService.class);
	new EvaluatorConsole(new OpEvaluator(ops)).showConsole();
}
 
Example 8
Source File: AbstractFormatTest.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@BeforeClass
public static void setup() {
	ctx = new Context();
	init = ctx.getService(
			InitializeService.class);
}
 
Example 9
Source File: FormatServiceTest.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Before
public void setUp() {
	final Context context = new Context();
	formatService = context.getService(FormatService.class);
}
 
Example 10
Source File: AxisGuesserTest.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Method for testing pattern guessing logic. */

	@Test
	public void testAxisguessing() throws FormatException, IOException {
		final Context context = new Context();
		final SCIFIO scifio = new SCIFIO(context);
		final LogService log = scifio.log();
		final DataHandleService dataHandleService = context.getService(
			DataHandleService.class);
		final URL resource = this.getClass().getResource(
			"img/axisguesser/test_stack/img_000000000_Cy3_000.tif");
		final FileLocation file = new FileLocation(resource.getFile());

		final String pat = scifio.filePattern().findPattern(file);
		if (pat == null) log.info("No pattern found.");
		else {
			assertEquals("Wrong pattern:", "img_00000000<0-1>_Cy3_00<0-4>.tif", pat);
			final FilePattern fp = new FilePattern(file, pat, dataHandleService);
			assertTrue(fp.isValid());
			final Location id = fp.getFiles()[0];

			// read dimensional information from first file
			final Reader reader = scifio.initializer().initializeReader(id);
			final AxisType[] dimOrder = reader.getMetadata().get(0).getAxes().stream()
				.map(CalibratedAxis::type).toArray(AxisType[]::new);
			final long sizeZ = reader.getMetadata().get(0).getAxisLength(Axes.Z);
			final long sizeT = reader.getMetadata().get(0).getAxisLength(Axes.TIME);
			final long sizeC = reader.getMetadata().get(0).getAxisLength(
				Axes.CHANNEL);
			final boolean certain = reader.getMetadata().get(0).isOrderCertain();

			assertArrayEquals("Dimension Order", new AxisType[] { Axes.X, Axes.Y },
				dimOrder);
			assertFalse(certain);

			assertEquals(1, sizeZ);
			assertEquals(1, sizeT);
			assertEquals(1, sizeC);

			// guess axes
			final AxisGuesser ag = new AxisGuesser(fp, dimOrder, sizeZ, sizeT, sizeC,
				certain);

			// output results
			final String[] blocks = fp.getBlocks();
			final String[] prefixes = fp.getPrefixes();
			final AxisType[] axes = ag.getAxisTypes();
			final AxisType[] newOrder = ag.getAdjustedOrder();
			final boolean isCertain = ag.isCertain();
			assertFalse(isCertain);

			assertEquals("<0-1>", blocks[0]);
			assertEquals("img_00000000", prefixes[0]);
			assertEquals(Axes.Z, axes[0]);

			assertEquals("<0-4>", blocks[1]);
			assertEquals("_Cy3_00", prefixes[1]);
			assertEquals(Axes.TIME, axes[1]);

			assertArrayEquals(dimOrder, newOrder);
		}
		context.dispose();
	}
 
Example 11
Source File: AxisGuesserTest.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testAxisguessing2() throws FormatException, IOException {
	final Context context = new Context();
	final SCIFIO scifio = new SCIFIO(context);
	final LogService log = scifio.log();
	final DataHandleService dataHandleService = context.getService(
		DataHandleService.class);
	final URL resource = this.getClass().getResource(
		"img/axisguesser/leica_stack/leica_stack_Series014_z000_ch00.tif");
	final FileLocation file = new FileLocation(resource.getFile());

	final String pat = scifio.filePattern().findPattern(file);
	if (pat == null) log.info("No pattern found.");
	else {
		assertEquals("Wrong pattern:", "leica_stack_Series014_z00<0-2>_ch00.tif",
			pat);
		final FilePattern fp = new FilePattern(file, pat, dataHandleService);
		assertTrue(fp.isValid());
		final Location id = fp.getFiles()[0];

		// read dimensional information from first file
		final Reader reader = scifio.initializer().initializeReader(id);
		final AxisType[] dimOrder = reader.getMetadata().get(0).getAxes().stream()
			.map(CalibratedAxis::type).toArray(AxisType[]::new);
		final long sizeZ = reader.getMetadata().get(0).getAxisLength(Axes.Z);
		final long sizeT = reader.getMetadata().get(0).getAxisLength(Axes.TIME);
		final long sizeC = reader.getMetadata().get(0).getAxisLength(
			Axes.CHANNEL);
		final boolean certain = reader.getMetadata().get(0).isOrderCertain();

		assertArrayEquals("Dimension Order", new AxisType[] { Axes.X, Axes.Y },
			dimOrder);
		assertFalse(certain);

		assertEquals(1, sizeZ);
		assertEquals(1, sizeT);
		assertEquals(1, sizeC);

		// guess axes
		final AxisGuesser ag = new AxisGuesser(fp, dimOrder, sizeZ, sizeT, sizeC,
			certain);

		// output results
		final String[] blocks = fp.getBlocks();
		final String[] prefixes = fp.getPrefixes();
		final AxisType[] axes = ag.getAxisTypes();
		final AxisType[] newOrder = ag.getAdjustedOrder();
		final boolean isCertain = ag.isCertain();
		assertFalse(isCertain);

		assertEquals("<0-2>", blocks[0]);
		assertEquals("leica_stack_Series014_z00", prefixes[0]);
		assertEquals(Axes.Z, axes[0]);

		assertArrayEquals(dimOrder, newOrder);
	}
	context.dispose();
}
 
Example 12
Source File: GuiUtils.java    From Scripts with GNU General Public License v3.0 4 votes vote down vote up
/** IJ1 constructor */
public GuiUtils() {
	final Context context = (Context) IJ.runPlugIn("org.scijava.Context", "");
	uiService = context.getService(UIService.class);
}