The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here
Using Macro value as Loop parameter iterations
Hello everybody,
today I needed the functionality of a simple FOR-loop in RapidMiner for the first time. So I took a look at the Loop operators and picked the simple "Loop". The number of iterations depends on some information extraction and preprocessing, so I prepared a nice attribute holding a number of type Integer for my loop. I just extract this cute litte Integer via "Extract Macro" and feed it into the iterations parameter of my "Loop" operator. But unfortunately the macro value seems to be of type Real and causes an error (extracted from value 5 of an integer attribute):
Message: A value for the parameter 'iterations' must be specified! Expected integer but found '5.0'.
I couldn't find a way to manipulate the data type of the macro, so I'm a bit perplexed. I can hardly believe that it's not possible to loop with a variable amount of iterations. I want to do something simple like for(i=1; i <= variableValue; i++). Is this currently not possible with "Loop" or am I completely off the track?
Kind regards,
Matthias
today I needed the functionality of a simple FOR-loop in RapidMiner for the first time. So I took a look at the Loop operators and picked the simple "Loop". The number of iterations depends on some information extraction and preprocessing, so I prepared a nice attribute holding a number of type Integer for my loop. I just extract this cute litte Integer via "Extract Macro" and feed it into the iterations parameter of my "Loop" operator. But unfortunately the macro value seems to be of type Real and causes an error (extracted from value 5 of an integer attribute):
Message: A value for the parameter 'iterations' must be specified! Expected integer but found '5.0'.
I couldn't find a way to manipulate the data type of the macro, so I'm a bit perplexed. I can hardly believe that it's not possible to loop with a variable amount of iterations. I want to do something simple like for(i=1; i <= variableValue; i++). Is this currently not possible with "Loop" or am I completely off the track?
Kind regards,
Matthias
Tagged:
0
Answers
this is of course ugly. But you can avoid this trap, if you use a generate macro operator before and put the iterations macro into an floor() function. Assign this to a new macro called for example intIterations and put this into the loop operator. Should work this way
Greetings,
Sebastian
thank you - this workaround helped out. After leaving this problem behind a new question arose.
The previously mentioned "Loop" operator runs inside a "Loop examples" and does some things according to a special attribute value from the current example. This is why I needed to extract this into a macro. My next problem is that I need to do some special things in the first run of the loop. I usually would check against my count variable or set some boolean flag. A simple if-statement would distinguish between the first and all following loop iterations. But I was a little confused that it's not possible to check a macro value inside the "Branch" operator. But checking a variable value with "if" is exactly what i would usually do. "Branch" allows only to check the existence of a macro, this would work well for a simple flag. But since there seems to be no way to unset a macro once it is defined, this would just work for my first example. I guess there is a (simple) way to differentiate the loop iterations or to have a special first run but I can't figure it out. Sometimes it's still hard to find the right things to do in RapidMiner analog to the things I would directly code in Java. But experience is slowly rising... not least due to your hints and help
Thanks again and best regards,
Matthias
unfortunately not. Please do me the favor of adding an macro unset operator as well as an condition on a macro's value to the feature request list on our bug tracker.
A not very nice way out: Use the remember / recall operators to store a dummy object in memory. This can be retrieved and deleted. If retrieved and not available an exception will be thrown. If wrapped inside a handle Exception operator, it can avoid the execution of the following operators inside the handle execution operator.
Wow. That was very much from the dark side.
Greetings,
Sebastian
by the way: We stumbled in one of our projects over the same issue. In the current SVN version it is already added.
Greetings,
Sebastian
a short workaround: Before each loop initialize a macro "run" to 1; in your loop set this macro to 2; now you can use a select subprocess operator to do something else in the first run. Best regards,
chero
It seems cherokee's workaround is not as dark as Sebastian's way I didn't even take the "Select Subprocess" operator into consideration because I never used or needed it before. But in this case it should be useful. I chose another way and simply added a flag attribute into my first example set from where the inner loop is started. Not so nice and clean but a quick way to get my process running ::)
Sebastian do you have any idea when the current SVN version will be released to public?
Best regards,
Matthias
By the way: macro handling could perhaps need some other revision. I needed an iteration counter variable for my loop (because %{a} doesn't begin with 1 for each start of the loop). Seemed easy, "Generate Macro" before my "Loop" operator and increase count by "Set Macro" inside the loop. But the latter didn't allow me something like %{counter}+1 because the value was interpreted as string. So I had to overwrite the existing macro with "Generate Macro" which allows some functions and calculations. If I didn't absolutely fail in using "Set Macro" this non-uniform value setting and handling is a bit confusing.
in fact a better support for macros is already planned for RapidMiner 6.0. Then macros will be added to the meta data transformation to allow checking and selecting the already defined macros in the gui...
Greetings,
Sebastian
this sounds good!
But for now I have to refer to the old story about Branch and Macros once again But this time it's just a short note in the margin.
I'm now using the latest version of RM with text and web extensions (by the way - great that extensions are available through subversion now) from subversion which already has "expression" as condition type implemented for the Branch operator. I just selected the condition type "macro_defined" unintentionally and discovered a change for this parameter. This selection does no longer activate a text parameter field where you can enter the respective macro name but shows an "io object" selection field instead. I guess this was not intended and you might want to fix it.
Regards,
Matthias
thanks a lot for this hint.
Greetings,
Sebastian
as I just heard: It's already fixed.
Greetings,
Sebastian
the results either stay 1 or end up with the following result: "eval (eval(1 + 1) + 1)" and so on.
I thought eval was supposed to .... evaluate as numeric?
what are you trying to do? Can you send us your process?
Are you using Set Macro or Generate Macro? Only Generate Macro evaluates expressions. You can see the difference in this process:
Regards,
Balázs