Giter VIP home page Giter VIP logo

Comments (6)

DbxDev avatar DbxDev commented on September 4, 2024 1

This is the same WolframLanguage bug as witnessed in #22. It'll be fixed in 12.2 (I can reproduce on a 12.1 build, but not with a recent prototype build).
Messages that should be Quiet-ed, are incorrectly issued in some cases.

from wolframclientforpython.

DeepaMahm avatar DeepaMahm commented on September 4, 2024

@DbxDev Thanks, could you please explain how to pass the input arguments from python to .m file and load it back as MMA expression in python {edges,vl,ew,vd}=ImportString[#,"PythonExpression"]&/@ {"[(1,2),(1,3),(1,4),(2,5),(2,6),(5,6),(3,4),(3,7),(6,7),(7,8),(2,9)]", "[1,2,3,4,5,6,7,8,9]","{(1,2):49.6,(1,3):74.4,(1,4):49.6,(2,5):37.2,(2,6):74.4,(5,6):49.6,(3,4):37.2,(3,7):24.8,(6,7):62,(7,8):37.2,(2,9):24.8}", "{1:[75.,25.,0],2:[115.,45.,0],3:[10.,5.,0],4:[45.,0,0],5:[90.,60.,0],6:[45.,55.,0],7:[0,25.,0],8:[10.,50.,0],9:[115.,25.,0]}"}; ?

from wolframclientforpython.

DeepaMahm avatar DeepaMahm commented on September 4, 2024

@DbxDev

I tried

   from wolframclient.evaluation import WolframLanguageSession
   from wolframclient.language import wl, wlexpr, Global

    session = WolframLanguageSession()

    edges = [(1, 2), (1, 3), (1, 4), (2, 5), (2, 6), (5, 6), (3, 4), (3, 7), (6, 7), (7, 8), (2, 9)]
    vl = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ew = {(1, 2): 49.6, (1, 3): 74.4, (1, 4): 49.6, (2, 5): 37.2, (2, 6): 74.4, (5, 6): 49.6, (3, 4): 37.2, (3, 7): 24.8, (6, 7): 62, (7, 8): 37.2, (2, 9): 24.8}
    vd = {1: [75., 25., 0], 2: [115., 45., 0], 3: [10., 5., 0], 4: [45., 0, 0], 5: [90., 60., 0], 6: [45., 55., 0], 7: [0, 25., 0], 8: [10., 50., 0], 9: [115., 25., 0]}
    with WolframLanguageSession() as s:
        solution3d = wl.Get('test.m')
        s.evaluate(Global.solution3d(edges, vd, vl, ew))

test.m includes

solution3d[edges_, vd_, vl_, ew_] := (

    {edges,vl,ew,vd}=ImportString[#,"PythonExpression"]&/@{edges,vl,ew,vd};
    edges = UndirectedEdge @@@ edges; vcoords = List @@ vd;
    ew = Normal @ KeyMap[UndirectedEdge @@ # &,ew];

    g3d = Graph3D[vl, edges, VertexCoordinates -> vcoords, EdgeWeight->ew, VertexLabels->Placed["Name",Center],
    EdgeLabels->{e_:>Placed["EdgeWeight",Center]}, VertexSize->.5, BaseStyle->16];

    edgeLengths3d = # -> Norm[vars3d[[First@#]] - vars3d[[Last@#]]] /.solution3d & /@ EdgeList[g3d];

    vars3d = Array[Through[{x, y, z}@#] &, Length@vd];
    \[Lambda] = 1/100.; lbnd = 0; ubnd = 500;

    obj3d = Total[(Norm[vars3d[[First@#]] - vars3d[[Last@#]]] - # /. (Rule @@@ ew))^2 & /@ EdgeList[g3d]] + \[Lambda] * Total[Norm /@ (vars3d - Values@vd)];
    solution3d = Last @ Minimize[{obj3d, And @@ Thread[lbnd <= Join @@ vars3d <= ubnd]}, Join @@ vars3d];

    edgeLengths3d = # -> Norm[vars3d[[First@#]] - vars3d[[Last@#]]] /.solution3d & /@ EdgeList[g3d];
    ResourceFunction["PrettyGrid"][{#, # /. ew, # /. edgeLengths3d} & /@EdgeList[g3d],  "ColumnHeadings" -> {"edge", "EdgeWeight", "Edge Length"}];

    z1 = Values[solution3d] // Partition[#, 3] &;

    theFile = File["result.txt"];

    Export[theFile, z1, "Table"];
)

I'm not sure if

 solution3d = wl.Get('test.m')
 s.evaluate(Global.solution3d(edges, vd, vl, ew))

this right, nothing is written to the txt file unfortunately. Could you please have a look?

from wolframclientforpython.

DeepaMahm avatar DeepaMahm commented on September 4, 2024

@DbxDev This is a kind reminder

from wolframclientforpython.

DbxDev avatar DbxDev commented on September 4, 2024

This goes beyond the scope of this ticket and should rather be discussed on stackoverflow.

You're confusing Wolfram Language representation in Python and actual code evaluation. The python code solution3d = wl.Get('test.m') represents a WL expression that is never evaluated and should be replaced with s.evaluate(wl.Get('test.m')). It'll declare the WL function Global`solution3d for the given session that you're using afterward.

Remember that WL code declared in Python (using wl.xxx syntax) must be evaluated in a session using (session.evaluate(...)) otherwise it stays inert!

from wolframclientforpython.

DeepaMahm avatar DeepaMahm commented on September 4, 2024

@DbxDev Thank you. Unfortunately, I couldn't run it successfully after making the change that has been suggested. I have opened a new thread on stackoverflow as you suggested.

from wolframclientforpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.