Skip to main content

functions - How can I make assignments persist across sessions?


Yesterday, I imported a large set of data into a Mathematica notebook and stored each imported list of numbers in a function. For example, I would map a list like {10, 20, 30} to a function value as shown below



f[0] = {10, 20 30};
f[1] = {40, 50, 60};

With the lists stored in the functions I generated the below chart by writing


averageComparisonChart = 
BarChart[{fpAverages, fpiAverages},
ChartLabels -> {{"FP Quicksort", "FP Insertion Quicksort"},
Range[0, 160, 10]}, AxesLabel -> {HoldForm["Vector size"],
HoldForm["Execution time (ms)"]}, PlotLabel -> HoldForm["Quicksort vs.
Insertion sort"], LabelStyle -> {GrayLevel[0]}]


which output


bar chart


Before going to bed, I saved my notebook and shut down my computer. Today, all my functions have been reset. For example inputting f[0] outputs f[0] rather than the previously assigned list {10, 20, 30}.


Does anyone know what has caused this issue? How can a loss of data be avoided in the future? Is there a better way to store lists than in functions? Is there a way to restore the values from yesterday?


Related Question


The accepted answer to this question provides a method for creating persistence of data between sessions.



Answer



If you wrap your definitions in Once then their results will be remembered across sessions:


f[0] = Once[Print["a"]; {10, 20, 30}, "Local"]


Here the printing and the numbers {10, 20, 30} are used instead of a lengthy calculation that you only want to do once and whose result you want to remember in the next session.


On the first execution, the above code prints "a" and assigns the numbers {10, 20, 30} to f[0]. On subsequent executions (even after you've closed Mathematica and come back and are reevaluating the notebook), the execution of the first argument of Once does not take place any more, so there is no printing, and only the remembered result {10, 20, 30} is directly assigned to f[0]. This speeds up the reprocessing on subsequent executions dramatically if the list {10, 20, 30} is replaced with something hard to compute.


With Once you don't need to save/restore semi-manually as some comments suggest with Save, DumpSave, Get. Instead, persistent storage operates transparently to cache what has been calculated before.


If you place these Once calls within an initialization cell/group, then you have something resembling a persistent assignment.


Once has more options: you can specify in which cache the persistent storage should be (in the front end session, or locally so that even when you close and reopen Mathematica it's still there) and how long it should persist. See below for more details about storage management.


Another way to create persistent objects is with PersistentValue, which is a bit lower-level than Once but basically the same mechanism.


But Once is terribly slow!


It is true that retrieval from persistent storage is rather slow, taking several milliseconds even for the simplest lookups. Memoization, on the other hand, is very fast (nanoseconds) but impermanent. We can simply combine these two methods to achieve speed and permanence! For example,


g[n_] := g[n] = Once[Pause[1]; n^2, "Local"]


defines a function g[n] that, for every kernel session, only calls Once one time and then memoizes the result. We now have three timescales:




  • The very first call of g[4], for example, takes about one second (in this case) because it actually executes the body of the function definition:


    g[4] // AbsoluteTiming
    (* {1.0096, 16} *)


  • In each subsequent kernel session, the first call of g[4] takes a few milliseconds to retrieve the result from persistent storage:



    g[4] // AbsoluteTiming
    (* {0.009047, 16} *)


  • After this first call, every further call of g[4] only takes a few nanoseconds because of classical memoization:


    g[4] // RepeatedTiming
    (* {1.5*10^-7, 16} *)


How to categorize, inspect, and delete persistent objects



A certain wariness with persistent storage is in order. Note that persistent storage will never be consulted unless you explicitly wrap an expression in Once; there is no problem with these persistent objects contaminating unrelated calculations.


Nonetheless in practice I keep the persistent storage pool as clean as possible. The principal tool is to segregate persistent values from different calculations by storing them in different directories on the storage medium. For a given calculation, we can set up a storage location with, for example,


cacheloc = PersistenceLocation["Local", 
FileNameJoin[{$UserBaseDirectory, "caches", "mycalculation"}]]

If you don't do this (or set cacheloc = "Local" as in the f[0] and g[4] examples above), then all persistent values are stored in the $DefaultLocalBase directory. We can always simply delete such storage directories in order to clean up.


We use persistent storage to remember calculations in such a specific directory with


A = Once["hello", cacheloc]

As the documentation states, you can inspect the storage pool with



PersistentObjects["Hashes/Once/*", cacheloc]
(* {PersistentObject["Hashes/Once/Di20M1m4sLB", PersistenceLocation["Local", ...]]} *)

which gives you a list of persistent objects (identified by their hash strings) and where they are stored (in the kernel, locally, etc.). To see what each persistent object contains, run


PersistentObjects["Hashes/Once/*", cacheloc] /. 
PersistentObject[hash_, _[loc_, ___]] :>
{hash, loc, PersistentValue[hash, cacheloc]} // TableForm
(* Hashes/Once/Di20M1m4sLB Local Hold["hello"] *)

If we want to delete only the persistent element containing "hello" then we run



DeleteObject /@ PersistentObjects["Hashes/Once/Di20M1m4sLB", cacheloc];

and if we want to delete all persistent objects in this cache, we run


DeleteObject /@ PersistentObjects["Hashes/Once/*", cacheloc];



Usage examples: 199017


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...