Band Math in snappy and in GUI gives different results

I am performing a simple band math operation in python using snappy:

BandDescriptor = jpy.get_type(‘org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor’)
targetBand1 = BandDescriptor() = ‘differential’
targetBand1.type = ‘float32’
targetBand1.expression = ‘(Sigma0_VV_db_mst_01Aug2020 - Sigma0_VH_db_slv1_17Jan2020)’

The band names are correct. However, the resulting band is not at all the expected difference pixel by pixel of the input bands but other numbers that seem random.

If instead I use the GUI:

by clicking on the Data sources on the left to select the bands it works properly. However, also the GUI will fail when I use the plain band name such as Sigma0_VV_db_mst_01Aug2020 (simply removing the “$2.” prefix) as in the pic below. Then I get again the same wrong result!

Any help on this? thanks in advance.

Do you have 2 products open in SNAP and do they contain bands with the same name?
The $2 refers to the product index which is shown in the Product Explorer by [#].
When no product index is used in the expression the product with index [1] is used. This might explain the different results.
But Why this happens also with snappy is not clear to me.
Can you show images of the different results and values of specific pixels?

Right ! thank you. That is explaining it. In the code there was a little error in the expression causing the problem.

However, now i still have slight difference when running on snappy or on the GUI:

The image on the left is from snappy. The extra white pixels comparing to the image on the right are given because of some NaN values for one band. The expression is:
(band_1 - 6) > band_2
When band_1 is NaN and band_2 has a value (e.g. - 24) it will results in 1 in Snappy and in NaN in the GUI. How to obtain the same in Snappy?
I am trying to set the parameter targetBand1.noDataValue but no success…

In SNAP Desktop the valid pixel expression of the target band is set based on the valid pixel expressions of the input bands. This is not done by the operator.
Check the valid expression of the target band and you can use it in your python script as parameter. Right-click on the band and select Properties.

fantastic! thank you very much.