[GRASS-dev] r.regression.multi failing

Hi,

I am using r.regression.multi in a python script (using grass.run_command()) to compute the variance inflation factor (VIF). It runs in a loop, regressing variables one by one to all the other variables. It sometimes happen that the function fails with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This causes my script to stop. However, I would like to capture this warning / error, and continue. Do you know how I can accomplish this?

Paulo

On 23 March 2016 at 14:31, Paulo van Breugel <p.vanbreugel@gmail.com> wrote:

Hi,

Hi,

I am using r.regression.multi in a python script (using grass.run_command())
to compute the variance inflation factor (VIF). It runs in a loop,
regressing variables one by one to all the other variables. It sometimes
happen that the function fails with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This causes my script to stop. However, I would like to capture this warning
/ error, and continue. Do you know how I can accomplish this?

did you try with try except ?

Paulo

--
ciao
Luca

www.lucadelu.org

On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi <lucadeluge@gmail.com> wrote:

On 23 March 2016 at 14:31, Paulo van Breugel <p.vanbreugel@gmail.com> wrote:

Hi,

Hi,

I am using r.regression.multi in a python script (using grass.run_command())
to compute the variance inflation factor (VIF). It runs in a loop,
regressing variables one by one to all the other variables. It sometimes
happen that the function fails with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This causes my script to stop. However, I would like to capture this warning
/ error, and continue. Do you know how I can accomplish this?

did you try with try except ?

Yes, specifically:

from grass.exceptions import CalledModuleError

try:
    gscript.run_command(...)
except CalledModuleError:
   ...

Paulo

--
ciao
Luca

www.lucadelu.org
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

On 23-03-16 15:24, Anna Petrášová wrote:

On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi <lucadeluge@gmail.com> wrote:

On 23 March 2016 at 14:31, Paulo van Breugel <p.vanbreugel@gmail.com> wrote:

Hi,

Hi,

I am using r.regression.multi in a python script (using grass.run_command())
to compute the variance inflation factor (VIF). It runs in a loop,
regressing variables one by one to all the other variables. It sometimes
happen that the function fails with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This causes my script to stop. However, I would like to capture this warning
/ error, and continue. Do you know how I can accomplish this?

did you try with try except ?

Yes, specifically:

from grass.exceptions import CalledModuleError

try:
     gscript.run_command(...)
except CalledModuleError:
    ...

Thanks Luca and Anna, I'll have a look at except and give it a try

Paulo

--
ciao
Luca

www.lucadelu.org
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Le Wed, 23 Mar 2016 15:55:40 +0100,
Paulo van Breugel <p.vanbreugel@gmail.com> a écrit :

On 23-03-16 15:24, Anna Petrášová wrote:
> On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi
> <lucadeluge@gmail.com> wrote:
>> On 23 March 2016 at 14:31, Paulo van Breugel
>> <p.vanbreugel@gmail.com> wrote:
>>> Hi,
>>>
>> Hi,
>>
>>> I am using r.regression.multi in a python script (using
>>> grass.run_command()) to compute the variance inflation factor
>>> (VIF). It runs in a loop, regressing variables one by one to all
>>> the other variables. It sometimes happen that the function fails
>>> with:
>>>
>>> WARNING: Matrix is unsolvable
>>> ERROR: Multiple regression failed

This looks like a clear bug for me. So, even if I understand that you
need to work around it for now, could you also post a bug report with a
reproductible example ?

Moritz

On 23-03-16 16:21, Moritz Lennert wrote:

Le Wed, 23 Mar 2016 15:55:40 +0100,
Paulo van Breugel <p.vanbreugel@gmail.com> a écrit :

On 23-03-16 15:24, Anna Petrášová wrote:

On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi
<lucadeluge@gmail.com> wrote:

On 23 March 2016 at 14:31, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:

Hi,
  

Hi,
  

I am using r.regression.multi in a python script (using
grass.run_command()) to compute the variance inflation factor
(VIF). It runs in a loop, regressing variables one by one to all
the other variables. It sometimes happen that the function fails
with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This looks like a clear bug for me. So, even if I understand that you
need to work around it for now, could you also post a bug report with a
reproductible example ?

Not sure it is a bug? It happens when amongst the independent variables there are perfectly correlated variables. The message "WARNING: Matrix is unsolvable" therefore seems correct to me. Now this is treated as an error (ERROR: Multiple regression failed"), which I guess is the equivalent of grass.error() in a python script? Perhaps it would be possible to provide a warning instead, and give an output that can be captured, like NA for the coefficient estimates, instead of breaking of the function?

Moritz

On Wed, Mar 23, 2016 at 4:43 PM, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:

On 23-03-16 16:21, Moritz Lennert wrote:

Le Wed, 23 Mar 2016 15:55:40 +0100,
Paulo van Breugel <p.vanbreugel@gmail.com> a écrit :

On 23-03-16 15:24, Anna Petrášová wrote:

On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi
<lucadeluge@gmail.com> wrote:

On 23 March 2016 at 14:31, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:

Hi,

Hi,

I am using r.regression.multi in a python script (using
grass.run_command()) to compute the variance inflation factor
(VIF). It runs in a loop, regressing variables one by one to all
the other variables. It sometimes happen that the function fails
with:

WARNING: Matrix is unsolvable
ERROR: Multiple regression failed

This looks like a clear bug for me. So, even if I understand that you
need to work around it for now, could you also post a bug report with a
reproductible example ?

Not sure it is a bug? It happens when amongst the independent variables
there are perfectly correlated variables. The message "WARNING: Matrix is
unsolvable" therefore seems correct to me.

Yes, this warning should only occur in case of colinearity

Now this is treated as an error
(ERROR: Multiple regression failed"), which I guess is the equivalent of
grass.error() in a python script? Perhaps it would be possible to provide a
warning instead, and give an output that can be captured, like NA for the
coefficient estimates, instead of breaking of the function?

Instead of grass.run_command(), you can use

ps = grass.start_command()
returncode = ps.wait()

returncode is zero if the module finished successfully, non-zero if an
error occurred, easy to capture in a script.

Markus M

On Wed, Mar 23, 2016 at 4:57 PM, Markus Metz <markus.metz.giswork@gmail.com>
wrote:

On Wed, Mar 23, 2016 at 4:43 PM, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:
>
>
> On 23-03-16 16:21, Moritz Lennert wrote:
>>
>> Le Wed, 23 Mar 2016 15:55:40 +0100,
>> Paulo van Breugel <p.vanbreugel@gmail.com> a écrit :
>>
>>> On 23-03-16 15:24, Anna Petrášová wrote:
>>>>
>>>> On Wed, Mar 23, 2016 at 10:01 AM, Luca Delucchi
>>>> <lucadeluge@gmail.com> wrote:
>>>>>
>>>>> On 23 March 2016 at 14:31, Paulo van Breugel
>>>>> <p.vanbreugel@gmail.com> wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>
>>>>> Hi,
>>>>>
>>>>>>
>>>>>> I am using r.regression.multi in a python script (using
>>>>>> grass.run_command()) to compute the variance inflation factor
>>>>>> (VIF). It runs in a loop, regressing variables one by one to all
>>>>>> the other variables. It sometimes happen that the function fails
>>>>>> with:
>>>>>>
>>>>>> WARNING: Matrix is unsolvable
>>>>>> ERROR: Multiple regression failed
>>
>> This looks like a clear bug for me. So, even if I understand that you
>> need to work around it for now, could you also post a bug report with a
>> reproductible example ?
>
> Not sure it is a bug? It happens when amongst the independent variables
> there are perfectly correlated variables. The message "WARNING: Matrix is
> unsolvable" therefore seems correct to me.

Yes, this warning should only occur in case of colinearity

> Now this is treated as an error
> (ERROR: Multiple regression failed"), which I guess is the equivalent of
> grass.error() in a python script? Perhaps it would be possible to
provide a
> warning instead, and give an output that can be captured, like NA for the
> coefficient estimates, instead of breaking of the function?

Instead of grass.run_command(), you can use

ps = grass.start_command()
returncode = ps.wait()

returncode is zero if the module finished successfully, non-zero if an
error occurred, easy to capture in a script.

OK, thanks

Markus M

On Wed, Mar 23, 2016 at 11:57 AM, Markus Metz <markus.metz.giswork@gmail.com

wrote:

> Now this is treated as an error
> (ERROR: Multiple regression failed"), which I guess is the equivalent of
> grass.error() in a python script? Perhaps it would be possible to
provide a
> warning instead, and give an output that can be captured, like NA for the
> coefficient estimates, instead of breaking of the function?

Instead of grass.run_command(), you can use

ps = grass.start_command()
returncode = ps.wait()

returncode is zero if the module finished successfully, non-zero if an
error occurred, easy to capture in a script.

I don't know if the called module behavior is but or if it should be
improved somehow. Anyway, the way to deal with subprocess failure when
using GRASS Python API is to use try-except. You can also get run_command
to behave the same as in 6.4 version of the API using errors parameter:

returncode = run_command('g.region', ..., errors='status')

Other possible values are raise, ignore and exit. raise is the default and
it raises a CalledModuleError exception. Note that the exception is the
default because that's how the errors are handled in Python and it works
well also for read_command() which returns the module stdout by value.
There are different *_command() functions to conveniently cover different
cases. The most general is start_command() which is used similarly to the
underlying Popen.

Vaclav