Skip to content

Commit 076ccf7

Browse files
committed
A model class for conda environment parameters.
1 parent 767a3ad commit 076ccf7

7 files changed

Lines changed: 265 additions & 212 deletions

File tree

src/main/java/fiji/plugin/trackmate/helper/model/ParameterSweepModelIO.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* it under the terms of the GNU General Public License as
99
* published by the Free Software Foundation, either version 3 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/gpl-3.0.html>.
@@ -34,6 +34,8 @@
3434

3535
import javax.swing.JOptionPane;
3636

37+
import com.google.gson.ExclusionStrategy;
38+
import com.google.gson.FieldAttributes;
3739
import com.google.gson.Gson;
3840
import com.google.gson.GsonBuilder;
3941
import com.google.gson.JsonArray;
@@ -49,8 +51,9 @@
4951
import fiji.plugin.trackmate.detection.SpotDetectorFactoryBase;
5052
import fiji.plugin.trackmate.gui.Icons;
5153
import fiji.plugin.trackmate.helper.model.detector.DetectorSweepModel;
54+
import fiji.plugin.trackmate.helper.model.parameter.AbstractArrayParamSweepModel.RangeType;
5255
import fiji.plugin.trackmate.helper.model.parameter.AbstractParamSweepModel;
53-
import fiji.plugin.trackmate.helper.model.parameter.ArrayParamSweepModel.RangeType;
56+
import fiji.plugin.trackmate.helper.model.parameter.CondaEnvParamSweepModel;
5457
import fiji.plugin.trackmate.helper.model.parameter.EnumParamSweepModel;
5558
import fiji.plugin.trackmate.helper.model.tracker.TrackerSweepModel;
5659
import fiji.plugin.trackmate.providers.DetectorProvider;
@@ -67,7 +70,7 @@ public class ParameterSweepModelIO
6770
/**
6871
* Makes a file object that will save settings in the folder containing the
6972
* specified ground-truth folder.
70-
*
73+
*
7174
* @param groundTruthPath
7275
* the ground-truth folder.
7376
* @return a {@link File}.
@@ -155,6 +158,7 @@ public static ParameterSweepModel readFromDefault()
155158
private static Gson getGson()
156159
{
157160
final GsonBuilder builder = new GsonBuilder()
161+
.addDeserializationExclusionStrategy( new CustomExclusionStrategy() )
158162
.registerTypeAdapter( EnumParamSweepModel.class, new EnumParamSweepModelAdapter<>() )
159163
.registerTypeAdapter( SpotDetectorFactoryBase.class, new SpotDetectorFactoryBaseAdapter() )
160164
.registerTypeAdapter( SpotTrackerFactory.class, new SpotTrackerFactoryAdapter() )
@@ -177,6 +181,21 @@ public static ParameterSweepModel fromJson( final String str )
177181
return model;
178182
}
179183

184+
private static class CustomExclusionStrategy implements ExclusionStrategy
185+
{
186+
@Override
187+
public boolean shouldSkipField( final FieldAttributes f )
188+
{
189+
return f.getDeclaringClass() == CondaEnvParamSweepModel.class && f.getName().equals( "allValues" );
190+
}
191+
192+
@Override
193+
public boolean shouldSkipClass( final Class< ? > clazz )
194+
{
195+
return false;
196+
}
197+
}
198+
180199
private static class AbstractParamSweepModelAdapter implements JsonSerializer< AbstractParamSweepModel< ? > >, JsonDeserializer< AbstractParamSweepModel< ? > >
181200
{
182201

src/main/java/fiji/plugin/trackmate/helper/model/detector/YOLOOpt.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121
*/
2222
package fiji.plugin.trackmate.helper.model.detector;
2323

24-
import java.util.ArrayList;
2524
import java.util.LinkedHashMap;
26-
import java.util.List;
2725
import java.util.Map;
2826

2927
import fiji.plugin.trackmate.detection.SpotDetectorFactoryBase;
3028
import fiji.plugin.trackmate.helper.model.parameter.AbstractParamSweepModel;
31-
import fiji.plugin.trackmate.helper.model.parameter.ArrayParamSweepModel;
29+
import fiji.plugin.trackmate.helper.model.parameter.CondaEnvParamSweepModel;
3230
import fiji.plugin.trackmate.helper.model.parameter.DoubleParamSweepModel;
33-
import fiji.plugin.trackmate.helper.model.parameter.InfoParamSweepModel;
3431
import fiji.plugin.trackmate.helper.model.parameter.StringRangeParamSweepModel;
35-
import fiji.plugin.trackmate.util.cli.CLIUtils;
3632
import fiji.plugin.trackmate.yolo.YOLOCLI;
3733
import fiji.plugin.trackmate.yolo.YOLODetectorFactory;
3834

@@ -44,34 +40,11 @@ private YOLOOpt()
4440

4541
static Map< String, AbstractParamSweepModel< ? > > createModels()
4642
{
47-
final Map< String, AbstractParamSweepModel< ? > > models = new LinkedHashMap<>();
48-
final List< String > envList = new ArrayList<>();
49-
try
50-
{
51-
final List< String > l = CLIUtils.getEnvList();
52-
envList.addAll( l );
53-
}
54-
catch ( final Exception e )
55-
{
56-
e.printStackTrace();
57-
}
58-
if ( envList == null || envList.isEmpty() )
59-
{
60-
models.put( "", new InfoParamSweepModel()
61-
.info( "The conda executable seems not to be configured, <br>"
62-
+ "or no conda environment could be found. Please <br>"
63-
+ "follow the link below for installation instructions." )
64-
.url( "https://imagej.net/plugins/trackmate/detectors/trackmate-yolo" ) );
65-
return models;
66-
}
6743

6844
final YOLOCLI cli = new YOLOCLI();
6945

70-
final ArrayParamSweepModel< String > condaEnv = new ArrayParamSweepModel<>(
71-
envList.toArray( new String[] {} ) )
72-
.paramName( "YOLO conda environment" )
73-
.addValue( envList.get( 0 ) )
74-
.fixedValue( envList.get( 0 ) );
46+
final CondaEnvParamSweepModel condaEnv = new CondaEnvParamSweepModel()
47+
.paramName( "YOLO conda environment" );
7548

7649
final StringRangeParamSweepModel modelPath = new StringRangeParamSweepModel()
7750
.paramName( cli.modelPath().getName() )
@@ -90,6 +63,7 @@ private YOLOOpt()
9063
.max( 0.5 )
9164
.nSteps( 5 );
9265

66+
final Map< String, AbstractParamSweepModel< ? > > models = new LinkedHashMap<>();
9367
models.put( YOLOCLI.KEY_CONDA_ENV, condaEnv );
9468
models.put( YOLODetectorFactory.KEY_YOLO_MODEL_FILEPATH, modelPath );
9569
models.put( YOLODetectorFactory.KEY_YOLO_CONF, confidenceParam );
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*-
2+
* #%L
3+
* TrackMate: your buddy for everyday tracking.
4+
* %%
5+
* Copyright (C) 2021 - 2024 TrackMate developers.
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
20+
* #L%
21+
*/
22+
package fiji.plugin.trackmate.helper.model.parameter;
23+
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.Collections;
27+
import java.util.LinkedHashSet;
28+
import java.util.List;
29+
import java.util.Set;
30+
31+
public class AbstractArrayParamSweepModel< T, F extends AbstractArrayParamSweepModel< T, F > > extends AbstractParamSweepModel< T >
32+
{
33+
34+
protected final Set< T > set = new LinkedHashSet<>();
35+
36+
protected RangeType rangeType = RangeType.FIXED;
37+
38+
protected T fixedValue;
39+
40+
private T[] allValues;
41+
42+
private AbstractArrayParamSweepModel()
43+
{
44+
super();
45+
}
46+
47+
public AbstractArrayParamSweepModel( final T[] allValues )
48+
{
49+
this();
50+
this.allValues = allValues;
51+
}
52+
53+
@Override
54+
public List< T > getRange()
55+
{
56+
switch ( rangeType )
57+
{
58+
case FIXED:
59+
return Collections.singletonList( fixedValue );
60+
case LIST:
61+
return new ArrayList<>( set );
62+
case TEST_ALL:
63+
return getAllValues();
64+
default:
65+
throw new IllegalArgumentException( "Unknown range type: " + rangeType );
66+
}
67+
}
68+
69+
public RangeType getRangeType()
70+
{
71+
return rangeType;
72+
}
73+
74+
public T getFixedValue()
75+
{
76+
return fixedValue;
77+
}
78+
79+
public Set< T > getSelection()
80+
{
81+
return set;
82+
}
83+
84+
@SuppressWarnings( "unchecked" )
85+
@Override
86+
public F paramName( final String paramName )
87+
{
88+
return ( F ) super.paramName( paramName );
89+
}
90+
91+
@SuppressWarnings( "unchecked" )
92+
public F rangeType( final RangeType rangeType )
93+
{
94+
if ( this.rangeType != rangeType )
95+
{
96+
this.rangeType = rangeType;
97+
notifyListeners();
98+
}
99+
return ( F ) this;
100+
}
101+
102+
@SuppressWarnings( "unchecked" )
103+
public F fixedValue( final T fixedValue )
104+
{
105+
if ( this.fixedValue != fixedValue )
106+
{
107+
this.fixedValue = fixedValue;
108+
notifyListeners();
109+
}
110+
return ( F ) this;
111+
}
112+
113+
@SuppressWarnings( "unchecked" )
114+
public F addValue( final T value )
115+
{
116+
if ( set.add( value ) )
117+
notifyListeners();
118+
119+
return ( F ) this;
120+
}
121+
122+
@SuppressWarnings( "unchecked" )
123+
public F removeValue( final T value )
124+
{
125+
if ( set.remove( value ) )
126+
notifyListeners();
127+
128+
return ( F ) this;
129+
}
130+
131+
public List< T > getAllValues()
132+
{
133+
return Arrays.asList( allValues );
134+
}
135+
136+
@Override
137+
public String toString()
138+
{
139+
switch ( rangeType )
140+
{
141+
case FIXED:
142+
return String.format( "%s:\n"
143+
+ " - type: %s\n"
144+
+ " - value: %s",
145+
paramName,
146+
rangeType,
147+
fixedValue );
148+
case LIST:
149+
case TEST_ALL:
150+
return String.format( "%s:\n"
151+
+ " - type: %s\n"
152+
+ " - values: %s",
153+
paramName,
154+
rangeType,
155+
Arrays.toString( getRange().toArray() ) );
156+
default:
157+
throw new IllegalArgumentException( "Unknown range type: " + rangeType );
158+
}
159+
}
160+
161+
public enum RangeType
162+
{
163+
TEST_ALL( "test all" ), FIXED( "single value" ), LIST( "list" );
164+
165+
private final String name;
166+
167+
RangeType( final String name )
168+
{
169+
this.name = name;
170+
}
171+
172+
@Override
173+
public String toString()
174+
{
175+
return name;
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)