Can IPython cell magics get access to a unique cell identifier? - ipython

I'm trying to implement magics to support a programming language where entire modules must be compiled at once. My goal is that all cells in a notebook with a particular cell magic will be coalesced into a single module M.
To make this work, when a cell C changes I need to remove the old contents of C from M and replace them with the new contents of C. However when the cell magic is called for C, there is no way to tell that it is C and not any other old or new cell. What I need is an identifier that is constant between calls to the cell magic.
Is this kind of cell identifier available anywhere in the IPython API?

No: the kernel (where execution happens) knows nothing about the notebook interface. From the kernels point of view, it receives some code to execute, and sends back some results. So running the same cell twice is exactly the same as running two cells with the same content.
I don't know what language you're working on, but other magics that require compilation treat one cell as one module. For instance, have a look at the %%cython magic.

Related

MATLAB compiler says some functions in my app are using not licensed for compilation functions

I want to compile my app using matlab-compiler it does so, but with issues...
It says there are some functions that are not licensed for compilation.
The problem is that I haven't used those functions (one of them is fimath.m) in my app.
I think these functions are used inside some of my functions which I don't know.
My question is how to find out which one of my functions are using those functions in order to remove them or replace them with other functions.
There are more than 50 functions in my app and it's not possible to check them one by one.
For every returned "unlicensed" function you can execute the following command,
dbstop in <function name> % without the <>
and afterwards run your code normally for several typical inputs/cases. If it stops at one of these breakpoints, look at the call stack (using either dbstack or the Editor tab of the MATLAB GUI), and identify the entry point from your own code.
If none of the breakpoints is ever hit, it could mean that these functions are referred-to inside the code, but some logic is preventing their execution (turning them, practically, to "unreachable code"). In this case, you will likely need to remove these references manually. To know where from, using information from the link posted by VTodorov you can list the dependencies of each file using
[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
which can be called on the output of dir (after some minor conversion). It could be useful to use the toponly flag.

Call hierarchy from a certain function

Background:
Working in eclipse, I have two function: do_something and perform_task. I know that do_something calls a number of other functions which in turn call others (and so on and so on) and somewhere down the line perform_task gets called as well.
Since this is a big project, lots of flows and so on, I've already found two different sequences where do_something activates perform_task through some other sequence of functions.
Actuall question:
Is there a way in eclipse to get the call hierarchy of a certain function, but only sequences that will include also a certain other function in the sequence?
Thinking of this in terms of graph paths, we have a directed graph, and instead of asking what are paths to node x, I want to know what are the paths to node x that include node y.
It's not exactly what you're asking for, but might be useful enough:
In the Call Hierarchy view, there's an option to show the callees of the selected method instead of the callers. Look at the view toolbar of Call Hiearchy for the two buttons that depict green dots connected with lines; those button toggle between the two modes.
If you select do_something and open the Call Hierarchy view on it, then set the mode to Show Callees you might be able to explore the various paths out of do_something that lead to perform_task.

Ipython Notebook: programmatically read and execute cells

In the ipython notebook, I would like to programmatically read and execute code cells from within a code cell itself.
Something like
if condition:
# run input cell no. 3
I found a solution here, the function execute_notebook reads an ipynb file cell by cell and executes code cells using get_ipython().run_cell().
Is there a way to do the same, i.e. without reading the cells from the an external ipynb file first? Is there a way to write macros, to reference and access code cells from within an ipython notebook?
I'm not sure if I understand your question correctly. Would you like to be able to manipulate the exact same notebook that you are currently working in? While this may be possible, it honestly sounds like a recipe for disaster because it will most likely leave your currently running notebook in an undefined state (or at the very least it would make it hard to follow what's going on).
A cleaner solution may be to programmatically generate a separate notebook in which you include precisely the code cells you need for your use case, and then execute it. (Alternatively, you can have a "template" .ipynb file with dummy code cells which you then programmatically replace with the actual code that you want to run).
I needed to do a similar thing recently and have written up a detailed example which shows how to do this. It programmatically generates a new notebook, adds a few markdown and code cells (which in this case produce a sine wave plot where the frequency is injected on-the-fly), then executes this notebook and saves the result for further postprocessing.
I hope this helps. Let me know if I completely misunderstood what you are trying to do.
hi the exact answer to your question is :
import io
from IPython.nbformat import current
with io.open("Name_of_your_notebook.ipynb") as f:
nb = current.read(f, 'json')
ip = get_ipython()
for cell in nb.worksheets[0].cells:
if cell.cell_type != 'code':
continue
if cell.prompt_number==4186:
ip.run_cell(cell.input)
but keep in mid please, the cell will be run under this code, not like as function in py does
I think the best way in this case would be to write a procedure and run it as much as you want. But as a brain workout, I would suggest two options.
To execute the code from the cell when some condition is met (suppose we want to reuse the third one as in your example), you can use exec
if condition:
exec(In[3])
More reliable solution may be with marked cells and a magic command rerun:
if condition:
%rerun -g 'unique mark'
But again, I'm pretty sure the best way is to write a procedure and reuse it.

Matlab function signature changes

Let us say that I have a Matlab function and I change its signature (i.e. add parameter). As Matlab does not 'compile' is there an easy way to determine which other functions do not use the right signature (i.e. submits the additional parameter). I do not want to determine this at runtime (i.e. get an error message) or have to do text searches. Hope this makes sense. Any feedback would be very much appreciated. Many thanks.
If I understand you correctly, you want to change a function's signature and find all functions/scripts/classes that call it in the "old" way, and change it to the "new" way.
You also indicated you don't want to do it at runtime, or do text searches, but there is no way to detect "incorrect" calls at "parse-time", so I'm afraid these demands leave no option at all to detect old function calls...
What I would do in that case is temporarily add a few lines to the new function:
function myFunc(param1, param2, newParam) % <-- the NEW signature
if nargin == 2
clc, error('old call detected.'); end
and then run the main script/function/whatever in which this function resides. You'll get one error for each time something calls the function incorrectly, along with the error stack in the Matlab command window.
It is then a matter of clicking on the link in the bottom of the error stack, correct the function call, and repeat from the top until no more errors occur.
Don't forget to remove these lines when you're done, or better, replace the word error with warning just to capture anything that was missed.
Better yet: if you're on linux, a text search would be a matter of
$ grep -l 'myFunc(.*,.*); *.m'
which will list all the files having the "incorrect" call. That's not too difficult I'd say...You can probably do a similar thing with the standard windows search, but I can't test that right now.
This is more or less what the dependency report was invented for. Using that tool, you can find what functions/scripts call your altered function. Then it is just a question of manually inspecting every occurrence.
However, I'd advise to make your changes to the function signature such that backwards compatibility is maintained. You can do so by specifying default values for new parameters and/or issuing a warning in those scenarios. That way, your code will run, and you will get run-time hints of deprecated code (which is more or less a necessary evil in interpreted/dynamic languages).
For many dynamic languages (and MATLAB specifically) it is generally impossible to fully inspect the code without the interpreter executing the code. Just imagine the following piece of code:
x = magic(10);
In general, you'd say that the magic function is called. However, magic could map to a totally different function. This could be done in ways that are invisible to a static analysis tool (such as the dependency report): e.g. eval('magic = 1:100;');.
The only way is to go through your whole code base, either inspecting every occurrence manually (which can be found easily with a text search) or by running a test that fully covers your code base.
edit:
There is however a way to access intermediate outputs of the MATLAB parser. This can be accessed using the undocumented and unsupported mtree function (which can be called like this: t = mtree(file, '-file'); for every file in your code base). Using the resulting structure you might be able to find calls with a certain amount of parameters.

Eclipse plugin that allows customized view of variables/objects while debugging

While using C++ I have to deal with programs that have objects like matrices, linked lists etc
The default view of eclipse for variables while debugging is not very useful. It normally shows pointer values with value of only the first element in the array.
Below is how it is in eclipse:
As we can see the matrix object has rows and columns integers and a double 2D array. I cannot see the values of the array in user friendly manner.
My question is that, is there anyway in eclipse (using plugin etc) through which I can define custom user interfaces (popups) for each object/class of my interest.
For example I would like to have the following (Matlab's view) kind of popup when I hover over an initialized matrix object:
The JDT has a feature called detail formatters to make that possible. The corresponding implementation for the CDT seems to have stalled after a first prototype however, and did not make it into Eclipse.