Skip to content

Commit c03e0c2

Browse files
author
Stefan Hahmann
committed
Refactor Trackastra environment setup by consolidating preparation and build logic into methods. Add confirmEnvInstallation flag and OS check for improved flexibility.
1 parent b19f733 commit c03e0c2

1 file changed

Lines changed: 53 additions & 18 deletions

File tree

src/main/java/org/mastodon/mamut/linking/trackastra/TrackastraLinker.java

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class TrackastraLinker< V extends Vertex< E > & HasTimepoint & RealLocali
5050

5151
private long processingTime;
5252

53+
private boolean confirmEnvInstallation = true;
54+
5355
@Override
5456
public void mutate1( final ReadOnlyGraph< V, E > graph, final SpatioTemporalIndex< V > index )
5557
{
@@ -59,29 +61,22 @@ public void mutate1( final ReadOnlyGraph< V, E > graph, final SpatioTemporalInde
5961
Environment environment;
6062
try
6163
{
62-
Pair< Boolean, String > checkResult = ApposeUtils.confirmEnvInstallation( TrackastraUtils.ENV_NAME, logger );
63-
if ( Boolean.TRUE.equals( checkResult.getKey() ) )
64-
{
65-
environment = Appose.mamba().scheme( "environment.yml" ).content( TrackastraUtils.ENV_FILE_CONTENT ).logDebug()
66-
.subscribeProgress( ( title, cur, max ) -> log.info( "{}: {}/{}", title, cur, max ) )
67-
.subscribeOutput( log::info )
68-
.subscribeError( log::error ).build();
69-
}
70-
else
71-
{
72-
ok = false;
73-
errorMessage = checkResult.getValue();
74-
return;
75-
}
64+
environment = prepareEnvironment();
7665
}
7766
catch ( IOException e )
7867
{
79-
throw new PythonRuntimeException( e );
68+
ok = false;
69+
errorMessage = "Failed to prepare Trackastra environment: " + e.getMessage();
70+
log.error( errorMessage, e );
71+
return;
8072
}
81-
73+
if ( environment == null )
74+
return;
8275
try (Service python = environment.python())
8376
{
84-
python.init( "import numpy\n" );
77+
if ( isWindows() )
78+
python.init(
79+
"import numpy\nimport trackastra.data.wrfeat\nimport trackastra.utils\nimport trackastra.model.pretrained as pretrained\nfrom trackastra.model import Trackastra\nimport trackastra.model.predict as predict\n" );
8580
String importScripts = ResourceUtils
8681
.readResourceAsString( "org/mastodon/mamut/linking/trackastra/appose/region_props_imports.py", getClass() );
8782
Service.Task importTask = python.task( importScripts, "main" );
@@ -140,7 +135,7 @@ private List< SingleTimepointRegionProps > computeRegionProps( final SpatioTempo
140135

141136
try
142137
{
143-
RegionPropsComputation computation = new RegionPropsComputation( logger, model, this, this.statusService, python );
138+
RegionPropsComputation computation = new RegionPropsComputation( logger, model, this, statusService, python );
144139
return computation.computeRegionPropsForSource( source, level, Cast.unchecked( index ), minTimepoint, maxTimepoint );
145140
}
146141
catch ( Exception e )
@@ -166,6 +161,46 @@ private void runLinkPrediction( final SpatioTemporalIndex< V > index, final List
166161
}
167162
}
168163

164+
private static boolean isWindows()
165+
{
166+
String os = System.getProperty( "os.name" ).toLowerCase();
167+
return os.contains( "win" );
168+
}
169+
170+
/**
171+
* Prepares and returns the Appose environment required for Trackastra execution.
172+
* Handles optional environment existence checking and consolidates duplicated build code.
173+
*/
174+
private Environment prepareEnvironment() throws IOException
175+
{
176+
if ( confirmEnvInstallation )
177+
{
178+
Pair< Boolean, String > check = ApposeUtils.confirmEnvInstallation( TrackastraUtils.ENV_NAME, logger );
179+
if ( !Boolean.TRUE.equals( check.getKey() ) )
180+
{
181+
ok = false;
182+
errorMessage = check.getValue();
183+
return null;
184+
}
185+
}
186+
return buildEnvironment();
187+
}
188+
189+
/**
190+
* Builds an Appose environment using the Trackastra environment.yml descriptor.
191+
*/
192+
private Environment buildEnvironment() throws IOException
193+
{
194+
return Appose.mamba().scheme( "environment.yml" ).content( TrackastraUtils.ENV_FILE_CONTENT ).logDebug()
195+
.subscribeProgress( ( title, cur, max ) -> log.info( "{}: {}/{}", title, cur, max ) ).subscribeOutput( log::info )
196+
.subscribeError( log::error ).build();
197+
}
198+
199+
public void setConfirmEnvInstallation( final boolean confirmEnvInstallation )
200+
{
201+
this.confirmEnvInstallation = confirmEnvInstallation;
202+
}
203+
169204
@Override
170205
public long getProcessingTime()
171206
{

0 commit comments

Comments
 (0)