|
28 | 28 | */ |
29 | 29 | package org.mastodon.mamut.appose; |
30 | 30 |
|
31 | | -import java.io.BufferedWriter; |
32 | | -import java.io.File; |
33 | | -import java.io.FileWriter; |
34 | 31 | import java.io.IOException; |
35 | | -import java.nio.file.Files; |
| 32 | +import java.util.function.Consumer; |
36 | 33 |
|
37 | 34 | import org.apache.commons.lang3.time.StopWatch; |
38 | 35 | import org.apposed.appose.Appose; |
39 | 36 | import org.apposed.appose.Environment; |
40 | 37 | import org.apposed.appose.Service; |
| 38 | +import org.apposed.appose.TaskEvent; |
| 39 | +import org.mastodon.mamut.detection.cellpose.Cellpose3; |
41 | 40 |
|
42 | 41 | public class ApposeReuseEnvironmentExample |
43 | 42 | { |
44 | 43 | public static void main( String[] args ) throws IOException, InterruptedException |
45 | 44 | { |
46 | | - String content = "name: cellpose\n" |
47 | | - + "channels:\n" |
48 | | - + " - nvidia\n" |
49 | | - + " - pytorch\n" |
50 | | - + " - conda-forge\n" |
51 | | - + "dependencies:\n" |
52 | | - + " - python=3.10\n" |
53 | | - + " - pip\n" |
54 | | - + " - pip:\n" |
55 | | - + " - cellpose[gui]\n" |
56 | | - + " - appose\n" |
57 | | - + " - pytorch\n" |
58 | | - + " - pytorch-cuda=11.8\n" |
59 | | - + " - numpy=2.0.2\n"; |
60 | | - |
61 | | - Environment env = Appose.mamba().scheme( "environment.yml" ).content( content ).logDebug().build(); |
| 45 | + Environment env = Appose.mamba().scheme( "environment.yml" ).content( Cellpose3.ENV_FILE_CONTENT ).logDebug().build(); |
62 | 46 | System.out.println( "Created environment" ); |
63 | 47 | StopWatch stopWatch = StopWatch.createStarted(); |
64 | 48 |
|
65 | | - String script = "import numpy as np" + "\n" |
66 | | - + "from cellpose import models" + "\n" |
67 | | - + "import appose" + "\n\n" |
68 | | - + "5 + 6" + "\n"; |
| 49 | + String importScript = "import networkx as nx\n" |
| 50 | + + "import appose\n" |
| 51 | + + "from pandas import DataFrame\n" |
| 52 | + + "\n" |
| 53 | + + "task.update(message='Hello from import script')\n" |
| 54 | + + "task.export(nx=nx, appose=appose, DataFrame=DataFrame)\n"; |
| 55 | + |
| 56 | + String script = "task.update(message='Creating a graph...')\n" |
| 57 | + + "G = nx.Graph()\n" |
| 58 | + + "G.add_nodes_from(['A', 'B', 'C'])\n" |
| 59 | + + "G.add_edges_from([('A', 'B'), ('B', 'C')])\n" |
| 60 | + + "print('Nodes:', G.nodes())\n" |
| 61 | + + "print('Edges:', G.edges())\n" |
| 62 | + + "data = {'name': ['Alice', 'Bob'], 'age': [25, 30]}\n" |
| 63 | + + "df = DataFrame(data)\n" |
| 64 | + + "task.update(message='Graph created with ' + str(len(G.nodes())) + ' nodes and ' + str(len(G.edges())) + ' edges.')\n" |
| 65 | + + "task.update(message='DataFrame created: ' + df.to_string())\n" |
| 66 | + + "task.outputs['result']=len(G.nodes()) + len(G.edges())\n"; |
69 | 67 |
|
70 | | - try (Service python = env.python()) |
| 68 | + try (Service python = env.python().init( "import numpy" )) |
71 | 69 | { |
72 | 70 | stopWatch.split(); |
73 | 71 | System.out.println( "Python service started: " + stopWatch.formatSplitTime() ); |
74 | | - Service.Task task1 = python.task( script ); |
| 72 | + Service.Task task1 = python.task( importScript, "main" ); |
| 73 | + task1.listen( getTaskListener() ); |
75 | 74 | task1.waitFor(); |
76 | | - Object result1 = task1.outputs.get( "result" ); |
77 | | - System.out.println( "result1: " + result1 ); |
| 75 | + System.out.println( "Import script ran successfully." ); |
78 | 76 | stopWatch.split(); |
79 | | - System.out.println( "Python task1 finished: " + stopWatch.formatSplitTime() ); |
| 77 | + System.out.println( "Import task finished: " + stopWatch.formatSplitTime() ); |
80 | 78 |
|
| 79 | + // Now run the payload script that uses the imported modules |
81 | 80 | Service.Task task2 = python.task( script ); |
| 81 | + task2.listen( getTaskListener() ); |
82 | 82 | task2.waitFor(); |
83 | | - Object result2 = task2.outputs.get( "result" ); |
84 | | - System.out.println( "result2: " + result2 ); |
| 83 | + Object result = task2.outputs.get( "result" ); |
| 84 | + System.out.println( "Nodes + Edges (result): " + result ); |
85 | 85 |
|
86 | 86 | stopWatch.split(); |
87 | | - System.out.println( "Python task2 finished: " + stopWatch.formatSplitTime() ); |
| 87 | + System.out.println( "Payload task finished: " + stopWatch.formatSplitTime() ); |
88 | 88 | } |
89 | 89 | } |
| 90 | + |
| 91 | + private static Consumer< TaskEvent > getTaskListener() |
| 92 | + { |
| 93 | + return taskEvent -> { |
| 94 | + String message = taskEvent.responseType.toString(); |
| 95 | + message += taskEvent.message == null ? "" : ": " + taskEvent.message; |
| 96 | + System.out.println( message ); |
| 97 | + }; |
| 98 | + } |
90 | 99 | } |
0 commit comments