Mathematica 笔记本的版本控制

发布于 2024-09-01 13:26:23 字数 259 浏览 7 评论 0原文

当然,Mathematica 笔记本是纯文本文件 - 似乎有理由期望它们应该与版本控制系统(在我的例子中是 git,尽管我怀疑特定系统是否重要)配合得很好。但事实是任何 .nb 文件都充满了缓存信息、时间戳和其他各种元数据。大量的它。

这意味着有限的版本控制是可能的——提交和回滚工作正常。然而,合并是一场灾难。 Mathematica 不会打开其中包含合并标记的文件,并且文本编辑器无法浏览 .nb 文件。

有没有人有幸将笔记本置于版本控制之下?如何?

Mathematica notebooks are, of course, plaintext files -- it seems reasonable to expect that they should play nice with a version-control system (git in my case, although I doubt the specific system matters). But the fact is that any .nb file is full of cache information, timestamps, and other assorted metadata. Scads of it.

Which means that limited version control is possible -- commits and rollbacks work fine. Merging, though, is a disaster. Mathematica won't open a file with merge markers in it, and a text editor is no way to go through a .nb file.

Has anyone had any luck putting a notebook under version control? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一杯敬自由 2024-09-08 13:26:24

您可以使用 .wls.wl 文件格式。
创建一个新的 wls 或 wl 脚本

它只保存代码、章节标记、注释等,但是没有额外的时间戳、计算结果、显示样式等,

例如raw.wl文件。
raw 中的 wl 格式

(* ::Package:: *)

(* ::Chapter:: *)
(*some title*)

(*som comment*)
NIntegrate[Erfc@x,{x,-1,1}]

(* ::Input:: *)
(*(*other comment*)*)
(*Plot[Beta[1/2,x],{x,-2,3}]*)

原始中的相同 nb 文件。
原始 nb 格式

(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
...
(* Internal cache information:
...
NotebookDataPosition[158, 7]
...
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["some title", "Chapter",...],

Cell[BoxData[
 RowBox[{
  RowBox[{"(*", ....], "Code",
 CellChangeTimes->{{3.921788175703182*^9, 3.921788181859129*^9}},
 CellLabel->"In[8]:=",...],

You can use the .wls or .wl file format.
create a new wls or wl script

It save only code, chapter mark, comments etc., but without extra timestamp, computed result, display style etc.,

for example, wl file in raw.
wl format in raw

(* ::Package:: *)

(* ::Chapter:: *)
(*some title*)

(*som comment*)
NIntegrate[Erfc@x,{x,-1,1}]

(* ::Input:: *)
(*(*other comment*)*)
(*Plot[Beta[1/2,x],{x,-2,3}]*)

the same nb file in raw.
nb format in raw

(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
...
(* Internal cache information:
...
NotebookDataPosition[158, 7]
...
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["some title", "Chapter",...],

Cell[BoxData[
 RowBox[{
  RowBox[{"(*", ....], "Code",
 CellChangeTimes->{{3.921788175703182*^9, 3.921788181859129*^9}},
 CellLabel->"In[8]:=",...],
萌化 2024-09-08 13:26:23

建议禁用文件大纲缓存,这是您使用文本编辑器查看笔记本时所引用的元数据。正如您所发现的,如果多方正在编辑同一个笔记本,则可能会导致合并冲突。

使用选项检查器可以轻松禁用此功能。在 Mathematica 菜单中,转到格式选项检查器...,在左上角将范围下拉列表设置为选定笔记本并在搜索字段中搜索FileOutlineCache。将选项设置为 False 并保存您的笔记本,您应该已准备就绪。

请注意,这可能会使打开笔记本的速度变慢,但除非笔记本相当大,否则您可能不会注意到差异。

It's recommended to disable the file outline cache, which is the metadata you're referring to when you look at the notebook with a text editor. As you discovered, it can cause merge conflicts if multiple parties are editing the same notebook.

This is easily disabled with the Option Inspector. In the Mathematica menu, go to FormatOption Inspector..., in the top-left set the scope dropdown to Selected Notebook and search for FileOutlineCache in the search field. Set the option to False and save your notebook, and you should be all set.

Note that this can make opening notebooks a little slower, but unless the notebook is rather large, you probably won't notice the difference.

携余温的黄昏 2024-09-08 13:26:23

关于如何使用 Git 对 Mathematica 进行版本控制,有一组很好的建议,位于 Mathematica Stack Exchange。简而言之,其理念是尽量减少使用 .nb 笔记本,并尝试使用 .m 包进行大部分版本控制(类似于上面 xuhdev 和 MMA 用户所说的)。考虑到笔记本的管理方式,这似乎相当合理。

There is a nice set of recommendations for how to use Git to do version control with Mathematica at Mathematica Stack Exchange. In short, the philosophy is to minimize use of .nb notebooks, and try to do most of the version control with .m packages (similar to what xuhdev and MMA user say above). This seems quite sensible given the way notebooks are managed.

埋葬我深情 2024-09-08 13:26:23

这并不是完全解决您的合并问题的方法,但这就是我们在团队中处理笔记本和源代码控制的方式。基本上,我们像对待二进制文件一样对待 Mathematica 笔记本。它们已签入,但是:

  • 我们总是在 .nb 旁边保留一份 pdf 副本(用于恢复信息的备份,以防我们由于某种原因失去读取 .nb 文件的能力。仍然是专有格式,但多一点广泛存在,并且 Adob​​e 和 Wolfram 可能不会同时消失)
  • 我们不允许合并,
  • 我们仅对最终产品(渲染的笔记本)而不是 .nb 文件进行代码审查。

我们主要使用 Mathematica 进行小型证明、探索和旁路,因此上述过程对我们来说效果很好(我们的主要文档是 LaTeX,它为非数学家/非程序员提供了更友好的文档)

Not a solution to your merging problem exactly, but this is how we handle notebooks and source control in my team. Basically, we treat Mathematica notebooks the way we'd treat binary files. They're checked-in, but:

  • we always keep a pdf copy alongside the .nb (backup for restoring the information in case we lose, for some reason, the capability of readings .nb files. Still proprietary format, but a bit more widespread, and chances are both Adobe and Wolfram won't simultaneously disappear)
  • we do not allow merges
  • we code-review only the final product (the rendered notebook) instead of the .nb file.

We mostly use Mathematica for small proofs, explorations and sidetracks, so the above procedure works fine for us (our main documentation is in LaTeX, which produces friendlier documentation for non-mathematicians/non-programmers)

人间☆小暴躁 2024-09-08 13:26:23

一种新的可能性是使用 mathematica-notebook-filter 解析 Mathematica 笔记本并剥离所有输出单元和元数据,以便它们不会提交到版本控制系统中。

在 git 的具体情况下,集成 mathematica-notebook- 非常容易过滤器,以便 git 在通过使用 gitattribute 过滤器。您需要有 mathematica-notebook-filter 过滤器已安装并添加到您的路径变量中(或调整下面的配置以指向二进制文件)并将以下行添加到您的 ~/.gitattributes 文件中:

*.nb    filter=dropoutput_nb

这指示 git 解析所有匹配 < code>*.nb 与 dropoutput_nb 过滤器在您的 ~/.gitconfig 中定义为:

[filter "dropoutput_nb"]
    clean = mathematica-notebook-filter
    smudge = cat

如果由于某种原因,您想要一个特定的Mathematica 笔记本提交了所有输出和元数据,您可以通过添加以下内容来禁用项目的 .gitattributes 文件中的过滤器:

notebook_file.nb    !filter

免责声明:我是此工具的作者。它是开源的,并且欢迎反馈(无论好坏)。
欢迎在 Github 上做出贡献。

A new possibility is to use mathematica-notebook-filter which parses Mathematica notebooks and strips all output cells and metadata so that these are not committed into the version control system.

In the specific case of git, it is quite easy to integrate mathematica-notebook-filter so that git automatically cleans the output and metadata when calculating diffs through the use of gitattribute filters. You will need to have mathematica-notebook-filter filter installed and added to your path variable (or adapt the configuration below to point to the binary) and add the following line to your ~/.gitattributes file:

*.nb    filter=dropoutput_nb

This instructs git to parse all files matching *.nb with the dropoutput_nb filter which is defined in your ~/.gitconfig as:

[filter "dropoutput_nb"]
    clean = mathematica-notebook-filter
    smudge = cat

If, for some reason, you want to have a specific Mathematica notebook committed with all output and metadata, you can disable the filter in the project's .gitattributes file by adding:

notebook_file.nb    !filter

Disclaimer: I am the author of this tool. It is open source and feedback (both good and bad) is appreciated.
Contributions are welcome on Github.

给不了的爱 2024-09-08 13:26:23

按照 Simon 和 Kena 所说的,当我将 Mathematica .nb 置于版本控制之下时,我经常仅创建输入代码的纯文本版本,并使用相同的名称但使用 .txt 扩展名保存它。虽然这并不能直接解决合并问题,但它确实使差异工作以合理的方式进行,并且当我稍后回去编辑 .nb 时,使手动合并更加明显。这种格式仍然有一些特性,但它比原始 .nb 格式更容易阅读。

要生成文本文件,我只需将笔记本复制到一个新的空白笔记本中(使用快捷键,Ctrl-A、C、N、V),选择菜单单元格->删除所有输出,复制结果(Ctrl-A, C),并将结果粘贴到纯文本编辑器中保存。一旦你掌握了它的窍门,只需要很少的时间就可以了。

Along the lines of what Simon and Kena were saying, when I have had Mathematica .nb's under version control, I often create a plain-text version of only the input code and save it with the same name but a .txt extension. While this doesn't directly solve the merging problem, it does make diff-ing work in a reasonable way and makes manual merging more obvious when I go back to edit the .nb's later. There are still some idiosyncrasies in this format, but it is MUCH easier to read than the raw .nb format.

To generate the text file, I just copy the notebook into a new blank notebook (with shortcuts, Ctrl-A,C,N,V), select the menu Cell->Delete All Output, copy the result (Ctrl-A,C), and paste the result into a plain text editor to save it. It takes surprisingly little time once you get the hang of it.

匿名。 2024-09-08 13:26:23

好吧,我的解决方案不是使用笔记本进行跟踪,而是使用纯文本文件(不是“笔记本”纯文本)。

只要您有笔记本,就可以使用“另存为...”菜单将当前文件另存为纯文本文件。当您需要加载它时,只需使用 Mahthematica 打开它即可。跟踪此文件比跟踪笔记本文件要好得多。我不确定使用纯文本格式而不是 Mathematica Notebook 可能会失去哪些功能,但到目前为止我还没有发现任何缺陷。

参考:http: //www.topbug.ne​​t/blog/2013/05/02/track-mathematica-source-files-with-version-control-systems/

Well, my solution is not using Notebook for tracking, but using plain text files (not the "Notebook" plain text).

Whenever you have a notebook, you can use the "save as..." menu to save the current file as a plain text file. When you need to load it, simply open it with Mahthematica. Tracking this file would be much nicer than tracking a Notebook file. I'm unsure about what features you may lose by using plain text format rather than the Mathematica Notebook, but I haven't found any defects so far.

Reference: http://www.topbug.net/blog/2013/05/02/track-mathematica-source-files-with-version-control-systems/

厌味 2024-09-08 13:26:23

仅当源代码控制系统检测到多个用户对单行进行更改时,您才应该获得合并标记。

源代码管理系统添加标记,以明确冲突的位置,并强制您手动删除它们(在解决每个冲突时)。源代码管理系统无法知道如何自动为您执行此操作。

如果文件是文本,但设计为仅由程序读取,则它可能根本没有行尾字符(或很长的行)。因此,如果多个人正在处理这样的文件,您将遇到许多合并冲突。

我不熟悉 nb 文件格式,但一般来说,解决此问题的方法是确保一次只有一个人正在处理一个文件(即对 nb 文件使用独占检出模式)。

You should only get merge markers if the source control system detects changes to a single line by multiple users.

The source control system adds markers to make if very clear where the conflicts are, and to force you to manually remove them (as you resolve each conflict). There is no way for a source control system to know how to do it automatically for you.

If the file is text, but is designed to be read by a program only, it may have no end of line characters at all (or very long lines). Therefore if multiple people are working on such a file you'll get many merge conflicts.

I'm not familiar with the nb file format, but in general the solution to this problem is to ensure only one person is working on a file at a time (ie use an exclusive check-out mode for nb files).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文