WeBWorK Problems

AskSage -- working example?

AskSage -- working example?

by Keir Lockridge -
Number of replies: 8

Being excited about the possibility of using Sage in WW, found this page:

https://webwork.maa.org/wiki/AskSage

I copied the code into a .pg file and tried to create it as a problem in WW. I've included the text of the problem below for convenience.

(A simplified version of what I want to accomplish is this. If I have $a = 1 and $b = 2 in my .pg file, I want Sage to compute $a + $b and return the answer as $c. Obviously I wouldn't use Sage to do this specifically, but if I could understand how to do this then it would help me better understand how AskSage works.)

But, the code doesn't work for me. I get this error:

1. ERROR caught by Translator while processing problem file:Linear_Algebra/HW2/b.pg
****************
ERRORS from evaluating PG file: 
'HASH' is not defined in this context; see position 1 of formula at line 23 of (eval 1805) Died within main::Matrix called at line 23 of (eval 1805)
line 23 is:

$M = Matrix($sageReply1)

WeBWorK Warnings

WeBWorK has encountered warnings while processing your request. If this occured when viewing a problem, it was likely caused by an error or ambiguity in that problem. Otherwise, it may indicate a problem with the WeBWorK system itself. If you are a student, report these warnings to your professor to have them corrected. If you are a professor, please consult the warning output below for more information.

Warning messages

Can't locate package Types::Serialiser::BooleanBase for @JSON::PP::Boolean::ISA at [PG]/lib/WeBWorK/PG/IO.pm line 414
Can't locate package Types::Serialiser::BooleanBase for @JSON::PP::Boolean::ISA at [PG]/lib/WeBWorK/PG/IO.pm line 414
Use of uninitialized value $sage_WEBWORK_data in substitution (s///) at [PG]/lib/WeBWorK/PG/IO.pm line 431
Use of uninitialized value $sage_WEBWORK_data in substitution (s///) at [PG]/lib/WeBWorK/PG/IO.pm line 432

IO.pm: ERROR trapped during JSON call to sage:
Error receiving JSON output from sage:
{"success": false, "execute_reply": {"status": "error", "traceback": ["\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m27\u001b[0m\n\u001b[0;31m print random_matrix(QQ,Integer(4),Integer(4),algorithm='echelonizable',rank=Integer(2)).rows()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"], "ename": "SyntaxError", "evalue": "invalid syntax (, line 27)", "engine_info": {"engine_uuid": "21685e14-fbe7-4bbd-b364-8c0de1f07688", "engine_id": -1, "method": "execute"}, "execution_count": 1, "user_expressions": {}, "payload": []}}
at [PG]/lib/WeBWorK/PG/IO.pm line 445
Died within WeBWorK::PG::IO::AskSage called at line 769 of [PG]/lib/PGcore.pm
from within PGcore::AskSage called at line 213 of [PG]/macros/PG.pl
from within main::AskSage called at line 22 of (eval 1357)
at [PG]/lib/WeBWorK/PG/IO.pm line 456
Processing of this PG problem was not completed. Probably because of a syntax error.
The translator died prematurely and no PG warning messages were transmitted. at /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm line 778.

########################################################

DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
);

###############################################
##
##  pg initializations and regular WeBWorK code


Context("Matrix");

my $rows = random(3,4);
my $columns = random(4,5);
my $rank = random(2,3);

$SageCode1=<<END;
print random_matrix(QQ,$rows,$columns,algorithm='echelonizable',rank=$rank).rows()
END

$sageReply1 = AskSage($SageCode1,{accepted_tos=>true,seed=>$problemSeed});
$M = Matrix($sageReply1);

if (sageReturnedFail($sageReply1) ) {
    $sageReply1 = "[1]"; # default value for $M;
}

$SageCode2 =<<END;
print matrix(QQ,$sageReply1).rref().rows()
END

$sageReply2 = AskSage($SageCode2,{accepted_tos=>true});

if (sageReturnedFail($sageReply2) ) {
    $sageReply2 = "[5]"; # default value for $ans;
}

$ans = Matrix($sageReply2);

Context()->texStrings;
BEGIN_TEXT

Row reduce \( $M \) $BR
\{ $ans->ans_array\}
END_TEXT
Context()->normalStrings;
ANS($ans->cmp());
ENDDOCUMENT();

In reply to Keir Lockridge

Re: AskSage -- working example?

by Michael Gage -

I'm just working on this problem now.  The root cause is that sageMath has moved to python3 and the syntax has changed. In addition however this might be a good time to see if we can get webwork to offer better error messages.

(Hint: If you add debug=>1 flag to the AskSage() requests you get back a lot more information (too much in fact) but it gives you something to work with.

In this case the problem is that print now requires parentheses in all cases.

print( ........................)

I think that solves the problem for most of the example questions.

Look at https://sagecell.sagemath.org/

for more info on changes that have to be made to sageMath code.

In addition you seem to be missing a CPAN package

Types::Serialiser::BooleanBase


In reply to Michael Gage

Re: AskSage -- working example?

by Keir Lockridge -
Thanks! I installed the CPAN pacakge.

What exactly does AskSage return? Is it a string? A list? does it come from the last executed line of sage code? I am having a hard time getting much out of the web page I cited, and my google skills are failing me. Is there a documentation page for AskSage I should look at? Sometimes when I print $sageReply I get "HASH...." and other times I seem to get a string.

Thank you so much for your help!

--Keir
In reply to Michael Gage

Re: AskSage -- working example?

by Keir Lockridge -
Actually ... no matter what I do I can't seem to install the CPAN Types::Serialiser::BooleanBase package. As root, I did this:

# cpan Types::Serialiser::BooleanBase

Loading internal logger. Log::Log4perl recommended for better logging
Reading '/root/.local/share/.cpan/Metadata'
Database was generated on Thu, 11 Jun 2020 19:41:03 GMT
Running install for module 'Types::Serialiser::BooleanBase'
CPAN: Digest::SHA loaded ok (v6.02)
CPAN: Compress::Zlib loaded ok (v2.093)
Checksum for /root/.local/share/.cpan/sources/authors/id/M/ML/MLEHMANN/Types-Serialiser-1.0.tar.gz ok
CPAN: YAML loaded ok (v1.30)
CPAN: CPAN::Meta::Requirements loaded ok (v2.140)
CPAN: Parse::CPAN::Meta loaded ok (v2.150010)
CPAN: CPAN::Meta loaded ok (v2.150010)
CPAN: Module::CoreList loaded ok (v5.20190522)
Configuring M/ML/MLEHMANN/Types-Serialiser-1.0.tar.gz with Makefile.PL
Checking if your kit is complete...
Looks good
Generating a Unix-style Makefile
Writing Makefile for Types::Serialiser
Writing MYMETA.yml and MYMETA.json
MLEHMANN/Types-Serialiser-1.0.tar.gz
/usr/bin/perl Makefile.PL INSTALLDIRS=site -- OK
Running make for M/ML/MLEHMANN/Types-Serialiser-1.0.tar.gz
cp Serialiser.pm blib/lib/Types/Serialiser.pm
cp Serialiser/Error.pm blib/lib/Types/Serialiser/Error.pm
Manifying 2 pod documents
MLEHMANN/Types-Serialiser-1.0.tar.gz
/usr/bin/make -- OK
Running make test for MLEHMANN/Types-Serialiser-1.0.tar.gz
PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/00_load.t ... ok
t/51_types.t .. ok
All tests successful.
Files=2, Tests=21, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.01 cusr 0.00 csys = 0.03 CPU)
Result: PASS
MLEHMANN/Types-Serialiser-1.0.tar.gz
/usr/bin/make test -- OK
Running make install for MLEHMANN/Types-Serialiser-1.0.tar.gz
Manifying 2 pod documents
Appending installation info to /usr/lib/x86_64-linux-gnu/perl/5.30/perllocal.pod
MLEHMANN/Types-Serialiser-1.0.tar.gz
/usr/bin/make install -- OK

But then:

# perl -MTypes::Serialiser::BooleanBase -e 'print "installed!\n"'
Can't locate Types/Serialiser/BooleanBase.pm in @INC (you may need to install the Types::Serialiser::BooleanBase module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base).
BEGIN failed--compilation aborted.

# perl -MTypes::Serialiser -e 'print "installed!\n"'
installed!

So I am lost.
In reply to Keir Lockridge

Re: AskSage -- working example?

by Michael Gage -

I notice that the install seems to have been made to /usr/lib.....

`info to /usr/lib/x86_64-linux-gnu/perl/5.30/perllocal.pod`

so  /usr/lib/x86_64-linux-gnu/perl/5.30/ is location of the installed module

the perl -M command in  @INC has /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 (note the local). The @INC doesn't have /usr/lib/x86_64-linux-gnu/perl/5.30/.

Not sure why -- possibly a difference in the set up for root?

In reply to Michael Gage

Re: AskSage -- working example?

by Keir Lockridge -
But doesn't this:

# perl -MTypes::Serialiser::BooleanBase -e 'print "installed!\n"'
Can't locate Types/Serialiser/BooleanBase.pm in @INC (you may need to install the Types::Serialiser::BooleanBase module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base).
BEGIN failed--compilation aborted.

Mean that @INC *does* contain /usr/lib/x86_64-linux-gnu/perl/5.30 (since it's in the "@INC contains" list above, 5th item)?

I also did `find / | grep BooleanBase` and got nothing (if there should be a file somewhere with that string in it).
In reply to Keir Lockridge

Re: AskSage -- working example?

by Michael Gage -

you are right.  I missed it.

since I run  several installations and the perl for my macbook desktop is different from the perl that runs the apache server I have often ended up with cpan modules installed in the wrong place

In reply to Michael Gage

Re: AskSage -- working example?

by Keir Lockridge -
I'm assuming you have BooleanBase installed -- can you tell me where it is on your machine, and what the names of the relevant files(s) are? Still trying to track this down ....
In reply to Keir Lockridge

Re: AskSage -- working example?

by Michael Gage -

Actually I don't seem to have it -- either on my old Macbook laptop installation (which is still using old versions of perl and  cpan files from a macport installation) or my dockerized installation -- which should be fairly up to date.

On the other hand I haven't gotten that error message so far either and those demo askSage problems work for me once I've fixed the print statements.