entrenar 0.7.8

Training & Optimization library with autograd, LoRA, quantization, and model merging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Notebook exporter for Jupyter format.

use serde::{Deserialize, Serialize};
use serde_json::json;

use super::cell::{Cell, CellType};
use super::kernel::KernelSpec;
use crate::research::literate::LiterateDocument;

/// Notebook exporter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotebookExporter {
    /// Notebook cells
    pub cells: Vec<Cell>,
    /// Kernel specification
    pub kernel: KernelSpec,
    /// Notebook metadata
    pub metadata: NotebookMetadata,
}

impl Default for NotebookExporter {
    fn default() -> Self {
        Self::new()
    }
}

impl NotebookExporter {
    /// Create a new notebook exporter
    pub fn new() -> Self {
        Self {
            cells: Vec::new(),
            kernel: KernelSpec::python3(),
            metadata: NotebookMetadata::default(),
        }
    }

    /// Create with a specific kernel
    pub fn with_kernel(kernel: KernelSpec) -> Self {
        Self { cells: Vec::new(), kernel, metadata: NotebookMetadata::default() }
    }

    /// Add a cell to the notebook
    pub fn add_cell(&mut self, cell: Cell) {
        self.cells.push(cell);
    }

    /// Add a code cell
    pub fn add_code(&mut self, source: impl Into<String>) {
        self.cells.push(Cell::code(source));
    }

    /// Add a markdown cell
    pub fn add_markdown(&mut self, source: impl Into<String>) {
        self.cells.push(Cell::markdown(source));
    }

    /// Create from a literate document
    pub fn from_literate(doc: &LiterateDocument) -> Self {
        let mut exporter = Self::new();

        // Determine kernel from document type
        if doc.is_typst() || doc.is_markdown() {
            // Extract code blocks and intersperse with markdown
            let content = doc.content();
            let blocks = doc.extract_code_blocks();

            // Detect primary language for kernel selection
            let primary_lang = blocks.iter().find_map(|b| b.language.as_ref()).map(String::as_str);

            exporter.kernel = match primary_lang {
                Some("rust") => KernelSpec::evcxr(),
                Some("julia") => KernelSpec::julia(),
                Some(other_lang) => {
                    eprintln!(
                        "Warning: unsupported kernel language '{other_lang}', defaulting to Python 3"
                    );
                    KernelSpec::python3()
                }
                None => KernelSpec::python3(),
            };

            // Simple parsing: add everything before first code block as markdown
            // then alternate between code and markdown
            let mut last_end = 0;

            for block in &blocks {
                // Find the start of this code block in the content
                let block_pattern = format!("```{}", block.language.as_deref().unwrap_or(""));
                if let Some(start_pos) = content[last_end..].find(&block_pattern) {
                    let absolute_start = last_end + start_pos;

                    // Add markdown before this block
                    let markdown_content = &content[last_end..absolute_start];
                    let trimmed = markdown_content.trim();
                    if !trimmed.is_empty() {
                        exporter.add_markdown(trimmed);
                    }

                    // Add the code block
                    exporter.add_code(&block.content);

                    // Find the end of this code block
                    let code_end = content[absolute_start..]
                        .find("```\n")
                        .or_else(|| content[absolute_start..].find("```"))
                        .map_or(content.len(), |p| {
                            absolute_start
                                + p
                                + content[absolute_start + p..].find('\n').unwrap_or(3)
                                + 1
                        });

                    last_end = code_end.min(content.len());
                }
            }

            // Add remaining markdown after last code block
            if last_end < content.len() {
                let remaining = content[last_end..].trim();
                if !remaining.is_empty() {
                    exporter.add_markdown(remaining);
                }
            }
        } else {
            // Raw text: add as single markdown cell
            exporter.add_markdown(doc.content());
        }

        exporter
    }

    /// Export to Jupyter notebook JSON format
    pub fn to_ipynb(&self) -> String {
        let notebook = json!({
            "nbformat": 4,
            "nbformat_minor": 5,
            "metadata": {
                "kernelspec": {
                    "display_name": self.kernel.display_name,
                    "language": self.kernel.language,
                    "name": self.kernel.name
                },
                "language_info": {
                    "name": self.kernel.language
                }
            },
            "cells": self.cells.iter().map(|cell| {
                let mut cell_json = json!({
                    "cell_type": cell.cell_type.to_string(),
                    "source": cell.source,
                    "metadata": cell.metadata
                });

                if cell.cell_type == CellType::Code {
                    cell_json["outputs"] = json!(cell.outputs.iter().map(|o| {
                        let mut out = json!({
                            "output_type": o.output_type
                        });
                        if let Some(data) = &o.data {
                            out["data"] = data.clone();
                        }
                        if let Some(text) = &o.text {
                            out["text"] = json!(text);
                            out["name"] = json!("stdout");
                        }
                        out
                    }).collect::<Vec<_>>());
                    cell_json["execution_count"] = json!(cell.execution_count);
                }

                cell_json
            }).collect::<Vec<_>>()
        });

        serde_json::to_string_pretty(&notebook).unwrap_or_else(|_err| "{}".to_string())
    }

    /// Get the number of cells
    pub fn cell_count(&self) -> usize {
        self.cells.len()
    }

    /// Get code cells only
    pub fn code_cells(&self) -> Vec<&Cell> {
        self.cells.iter().filter(|c| c.cell_type == CellType::Code).collect()
    }

    /// Get markdown cells only
    pub fn markdown_cells(&self) -> Vec<&Cell> {
        self.cells.iter().filter(|c| c.cell_type == CellType::Markdown).collect()
    }
}

/// Notebook metadata
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct NotebookMetadata {
    /// Notebook title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Authors
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub authors: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::super::cell::CellOutput;
    use super::*;

    #[test]
    fn test_kernel_selection_all_language_variants() {
        let languages: &[Option<&str>] = &[Some("rust"), Some("julia"), Some("javascript"), None];

        for lang in languages {
            // Syntactic match covering all arms from from_literate kernel selection
            let kernel = match *lang {
                Some("rust") => KernelSpec::evcxr(),
                Some("julia") => KernelSpec::julia(),
                Some(_other_lang) => KernelSpec::python3(),
                None => KernelSpec::python3(),
            };

            match lang {
                Some("rust") => assert_eq!(kernel.language, "rust"),
                Some("julia") => assert_eq!(kernel.language, "julia"),
                Some(_) => assert_eq!(kernel.language, "python"),
                None => assert_eq!(kernel.language, "python"),
            }
        }
    }

    #[test]
    fn test_notebook_exporter_new() {
        let exporter = NotebookExporter::new();
        assert_eq!(exporter.cells.len(), 0);
        assert_eq!(exporter.kernel.language, "python");
    }

    #[test]
    fn test_notebook_exporter_default() {
        let exporter = NotebookExporter::default();
        assert_eq!(exporter.cell_count(), 0);
    }

    #[test]
    fn test_add_code_and_markdown() {
        let mut exporter = NotebookExporter::new();
        exporter.add_code("print('hello')");
        exporter.add_markdown("# Title");
        assert_eq!(exporter.cell_count(), 2);
        assert_eq!(exporter.code_cells().len(), 1);
        assert_eq!(exporter.markdown_cells().len(), 1);
    }

    #[test]
    fn test_notebook_metadata_default() {
        let meta = NotebookMetadata::default();
        assert!(meta.title.is_none());
        assert!(meta.authors.is_empty());
    }

    // ── Additional coverage tests ─────────────────────────────────

    #[test]
    fn test_notebook_exporter_with_kernel() {
        let exporter = NotebookExporter::with_kernel(KernelSpec::evcxr());
        assert_eq!(exporter.kernel.language, "rust");
        assert_eq!(exporter.cell_count(), 0);
    }

    #[test]
    fn test_notebook_exporter_with_julia_kernel() {
        let exporter = NotebookExporter::with_kernel(KernelSpec::julia());
        assert_eq!(exporter.kernel.language, "julia");
        assert_eq!(exporter.kernel.display_name, "Julia 1.9");
    }

    #[test]
    fn test_add_cell_directly() {
        let mut exporter = NotebookExporter::new();
        exporter.add_cell(Cell::code("x = 1"));
        exporter.add_cell(Cell::markdown("# Hello"));
        exporter.add_cell(Cell::raw("raw text"));
        assert_eq!(exporter.cell_count(), 3);
        assert_eq!(exporter.code_cells().len(), 1);
        assert_eq!(exporter.markdown_cells().len(), 1);
    }

    #[test]
    fn test_to_ipynb_empty() {
        let exporter = NotebookExporter::new();
        let json = exporter.to_ipynb();
        assert!(json.contains("nbformat"));
        assert!(json.contains("\"cells\": []"));
    }

    #[test]
    fn test_to_ipynb_with_cells() {
        let mut exporter = NotebookExporter::new();
        exporter.add_code("print('hello')");
        exporter.add_markdown("# Title");
        let json = exporter.to_ipynb();
        assert!(json.contains("print('hello')"));
        assert!(json.contains("# Title"));
        assert!(json.contains("\"cell_type\": \"code\""));
        assert!(json.contains("\"cell_type\": \"markdown\""));
    }

    #[test]
    fn test_to_ipynb_kernelspec() {
        let exporter = NotebookExporter::with_kernel(KernelSpec::evcxr());
        let json = exporter.to_ipynb();
        assert!(json.contains("\"language\": \"rust\""));
        assert!(json.contains("Rust"));
    }

    #[test]
    fn test_to_ipynb_code_cell_has_outputs_and_execution_count() {
        let mut exporter = NotebookExporter::new();
        exporter.add_code("1 + 1");
        let json = exporter.to_ipynb();
        assert!(json.contains("\"outputs\""));
        assert!(json.contains("\"execution_count\""));
    }

    #[test]
    fn test_to_ipynb_with_output() {
        let mut exporter = NotebookExporter::new();
        let cell = Cell::code("print(42)")
            .with_output(CellOutput::stream("stdout", "42\n"))
            .with_execution_count(1);
        exporter.add_cell(cell);
        let json = exporter.to_ipynb();
        assert!(json.contains("stream"));
        assert!(json.contains("42"));
        assert!(json.contains("stdout"));
    }

    #[test]
    fn test_to_ipynb_with_execute_result() {
        let mut exporter = NotebookExporter::new();
        let data = serde_json::json!({"text/plain": ["result"]});
        let cell = Cell::code("1 + 1").with_output(CellOutput::execute_result(data));
        exporter.add_cell(cell);
        let json = exporter.to_ipynb();
        assert!(json.contains("execute_result"));
        assert!(json.contains("text/plain"));
    }

    #[test]
    fn test_from_literate_markdown() {
        let doc = LiterateDocument::parse_markdown(
            "# Hello\n\nSome text.\n\n```python\nprint('hi')\n```\n\nMore text.",
        );
        let exporter = NotebookExporter::from_literate(&doc);
        assert!(exporter.cell_count() > 0);
        // Should have at least one code cell and one markdown cell
        assert!(!exporter.code_cells().is_empty());
        assert!(!exporter.markdown_cells().is_empty());
        assert_eq!(exporter.kernel.language, "python");
    }

    #[test]
    fn test_from_literate_rust_kernel() {
        let doc =
            LiterateDocument::parse_markdown("# Rust Example\n\n```rust\nfn main() {}\n```\n");
        let exporter = NotebookExporter::from_literate(&doc);
        assert_eq!(exporter.kernel.language, "rust");
    }

    #[test]
    fn test_from_literate_julia_kernel() {
        let doc =
            LiterateDocument::parse_markdown("# Julia Example\n\n```julia\nprintln(\"hi\")\n```\n");
        let exporter = NotebookExporter::from_literate(&doc);
        assert_eq!(exporter.kernel.language, "julia");
    }

    #[test]
    fn test_from_literate_no_code_blocks() {
        let doc = LiterateDocument::parse_markdown("# Just Markdown\n\nNo code here.");
        let exporter = NotebookExporter::from_literate(&doc);
        // Should have at least some markdown content
        assert!(exporter.cell_count() >= 1);
        assert!(exporter.code_cells().is_empty());
    }

    #[test]
    fn test_from_literate_raw_text() {
        let doc = LiterateDocument::raw("Just plain text, no special parsing.");
        let exporter = NotebookExporter::from_literate(&doc);
        assert_eq!(exporter.cell_count(), 1);
        assert_eq!(exporter.markdown_cells().len(), 1);
    }

    #[test]
    fn test_from_literate_multiple_code_blocks() {
        let doc = LiterateDocument::parse_markdown(
            "Intro\n\n```python\nx = 1\n```\n\nMiddle text\n\n```python\ny = 2\n```\n\nEnd",
        );
        let exporter = NotebookExporter::from_literate(&doc);
        assert_eq!(exporter.code_cells().len(), 2);
    }

    #[test]
    fn test_notebook_metadata_with_values() {
        let meta = NotebookMetadata {
            title: Some("My Notebook".to_string()),
            authors: vec!["Author A".to_string(), "Author B".to_string()],
        };
        assert_eq!(meta.title.as_deref(), Some("My Notebook"));
        assert_eq!(meta.authors.len(), 2);
    }

    #[test]
    fn test_notebook_metadata_serialization() {
        let meta = NotebookMetadata {
            title: Some("Test".to_string()),
            authors: vec!["Alice".to_string()],
        };
        let json = serde_json::to_string(&meta).expect("serialize");
        let restored: NotebookMetadata = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored.title, meta.title);
        assert_eq!(restored.authors, meta.authors);
    }

    #[test]
    fn test_notebook_metadata_serialization_skip_empty() {
        let meta = NotebookMetadata::default();
        let json = serde_json::to_string(&meta).expect("serialize");
        // title should be omitted (skip_serializing_if = "Option::is_none")
        assert!(!json.contains("title"));
        // authors should be omitted (skip_serializing_if = "Vec::is_empty")
        assert!(!json.contains("authors"));
    }

    #[test]
    fn test_notebook_exporter_serialization() {
        let mut exporter = NotebookExporter::new();
        exporter.add_code("x = 1");
        let json = serde_json::to_string(&exporter).expect("serialize");
        let restored: NotebookExporter = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored.cell_count(), 1);
        assert_eq!(restored.kernel.language, "python");
    }

    #[test]
    fn test_code_cells_and_markdown_cells_filtering() {
        let mut exporter = NotebookExporter::new();
        exporter.add_code("a");
        exporter.add_markdown("b");
        exporter.add_code("c");
        exporter.add_markdown("d");
        exporter.add_code("e");

        assert_eq!(exporter.code_cells().len(), 3);
        assert_eq!(exporter.markdown_cells().len(), 2);
        assert_eq!(exporter.cell_count(), 5);
    }

    #[test]
    fn test_from_literate_typst() {
        let doc = LiterateDocument::Typst(
            "= Title\n\nSome text.\n\n```python\nprint('hi')\n```\n\nMore text.".to_string(),
        );
        let exporter = NotebookExporter::from_literate(&doc);
        assert!(exporter.cell_count() > 0);
    }
}