Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for imports #11

Closed
qo4on opened this issue Feb 9, 2020 · 3 comments
Closed

Improve support for imports #11

qo4on opened this issue Feb 9, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@qo4on
Copy link

qo4on commented Feb 9, 2020

This code works:
!python3 -m manim example_scenes.py WarpSquare -pl

This code works also in jupyter:

%%manim Shapes --base64 --low_quality
import numpy as np
class WarpSquare(Scene):
    def construct(self):
        square = Square()
        self.play(ApplyPointwiseFunction(
            lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
            square
        ))
        self.wait()

But when
import numpy as np
was written in the cell before and already executed the code without this line can't find numpy:

%%manim Shapes --base64 --low_quality
class WarpSquare(Scene):
    def construct(self):
        square = Square()
        self.play(ApplyPointwiseFunction(
            lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
            square
        ))
        self.wait()

Media will be written to ./media/. You can change this behavior with the --media_dir flag.

/usr/local/lib/python3.6/dist-packages/jupyter_manim/init.py:259: UserWarning: Could not find path in the manim output
warn('Could not find path in the manim output')
/tmp/tmp0wa7hz1o.py:15: UserWarning: Import from notebook: name already in the globals(), skipping
warn('Import from notebook: ' + name + ' already in the globals(), skipping')

Shapes is not in the script

Traceback (most recent call last):
File "/content/manim/manimlib/extract_scene.py", line 155, in main
scene = SceneClass(**scene_kwargs)
File "/content/manim/manimlib/scene/scene.py", line 53, in init
self.construct()
File "/tmp/tmp0wa7hz1o.py", line 25, in construct
square
File "/content/manim/manimlib/scene/scene.py", line 406, in wrapper
func(self, *args, **kwargs)
File "/content/manim/manimlib/scene/scene.py", line 462, in play
self.begin_animations(animations)
File "/content/manim/manimlib/scene/scene.py", line 415, in begin_animations
animation.begin()
File "/content/manim/manimlib/animation/transform.py", line 46, in begin
self.target_mobject = self.create_target()
File "/content/manim/manimlib/animation/transform.py", line 182, in create_target
method.func(target, *args, **method_kwargs)
File "/content/manim/manimlib/mobject/types/vectorized_mobject.py", line 564, in apply_function
Mobject.apply_function(self, function)
File "/content/manim/manimlib/mobject/mobject.py", line 280, in apply_function
**kwargs
File "/content/manim/manimlib/mobject/mobject.py", line 360, in apply_points_function_about_point
mob.points = func(mob.points)
File "/content/manim/manimlib/mobject/mobject.py", line 279, in
lambda points: np.apply_along_axis(function, 1, points),
File "<array_function internals>", line 6, in apply_along_axis
File "/usr/local/lib/python3.6/dist-packages/numpy/lib/shape_base.py", line 379, in apply_along_axis
for ind in inds:
File "/tmp/tmp0wa7hz1o.py", line 24, in
lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
NameError: name 'np' is not defined

Why doesn't the cell with magic %%manim find already imported libraries?

@krassowski krassowski added the bug Something isn't working label Feb 12, 2020
@qo4on
Copy link
Author

qo4on commented Feb 15, 2020

import sys and import random do the same.

@krassowski krassowski added the help wanted Extra attention is needed label Apr 5, 2020
@krassowski krassowski changed the title Strange behaviour Improve support for imports Apr 5, 2020
@krassowski
Copy link
Owner

One solution would be to search through the global objects, collect modules, assemble import code and then add it to the code passed to manim. The first two steps are easy:

from types import ModuleType

@magics_class
class ManimMagics(Magics):

    # ....

    def extract_imports(self):
        frame = find_ipython_frame(inspect.stack())
        if not frame:
            raise Exception('Could not find IPython frame')

        globals_dict = frame[0].f_globals
        
        modules = {
            name: obj
            for name, obj in globals_dict.items()
            if (not name.startswith('_')) and isinstance(obj, ModuleType)
        }
        return '\n'.join(
            assemble_import(module) for module in modules
        )

but writing the assemble_import function is difficult. It should work like this:

import statistics
from os import path
import os
sys = os.sys
import abc as xyz
assert assemble_import(statistics) = 'import statistics'
assert assemble_import(path) = 'from os import path'
assert assemble_import(sys) = 'from os import sys'
assert assemble_import(xyz) = 'import abc as xyz'

All I had time for this weekend, maybe someone else will have more ideas/time to send pick up from there and send a PR - help wanted!

@krassowski
Copy link
Owner

@krassowski krassowski removed the help wanted Extra attention is needed label Apr 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants