latent-inspector 0.1.0

Fast CLI for inspecting and comparing learned representations across self-supervised vision models
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
//! Interactive terminal UI for the latent-inspector.
//!
//! Uses ratatui + crossterm for a rich, educational visualisation of
//! SSL model representations and analysis metrics.

pub mod app;
pub mod theme;
pub mod views;

use app::{App, Tab};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::prelude::*;
use ratatui::widgets::*;
use std::io;
use std::time::Duration;

/// Launch the TUI event loop.
pub fn run(mut app: App) -> io::Result<()> {
    crossterm::terminal::enable_raw_mode()?;
    let mut stdout = io::stdout();
    crossterm::execute!(stdout, crossterm::terminal::EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    terminal.clear()?;

    let result = event_loop(&mut terminal, &mut app);

    crossterm::terminal::disable_raw_mode()?;
    crossterm::execute!(
        terminal.backend_mut(),
        crossterm::terminal::LeaveAlternateScreen
    )?;
    terminal.show_cursor()?;

    result
}

fn event_loop(
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    app: &mut App,
) -> io::Result<()> {
    while app.running {
        terminal.draw(|frame| draw(frame, app))?;

        if event::poll(Duration::from_millis(50))? {
            if let Event::Key(key) = event::read()? {
                if key.kind != KeyEventKind::Press {
                    continue;
                }
                handle_key(app, key.code, key.modifiers);
            }
        }
    }
    Ok(())
}

fn handle_key(app: &mut App, code: KeyCode, modifiers: KeyModifiers) {
    // File browser takes priority when active
    if app.file_browser.active {
        handle_file_browser_key(app, code);
        return;
    }

    // Global keys
    match code {
        KeyCode::Char('q') | KeyCode::Esc => {
            app.running = false;
            return;
        }
        KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => {
            app.running = false;
            return;
        }
        KeyCode::Tab => {
            app.tab = app.tab.next();
            return;
        }
        KeyCode::BackTab => {
            app.tab = app.tab.prev();
            return;
        }
        KeyCode::Right => {
            app.tab = app.tab.next();
            return;
        }
        KeyCode::Left => {
            app.tab = app.tab.prev();
            return;
        }
        KeyCode::Char('1') => {
            app.tab = Tab::Dashboard;
            return;
        }
        KeyCode::Char('2') => {
            app.tab = Tab::Inspector;
            return;
        }
        KeyCode::Char('3') => {
            app.tab = Tab::Compare;
            return;
        }
        KeyCode::Char('4') => {
            app.tab = Tab::Spectrum;
            return;
        }
        KeyCode::Char('5') => {
            app.tab = Tab::Help;
            return;
        }
        KeyCode::Char('?') => {
            app.tab = Tab::Help;
            return;
        }
        KeyCode::Char('o') => {
            // Open file browser
            app.file_browser.active = true;
            app.file_browser.refresh();
            return;
        }
        _ => {}
    }

    // Per-tab keys
    match app.tab {
        Tab::Dashboard => match code {
            KeyCode::Down | KeyCode::Char('j') => app.select_next_model(),
            KeyCode::Up | KeyCode::Char('k') => app.select_prev_model(),
            KeyCode::Enter => app.tab = Tab::Inspector,
            _ => {}
        },
        Tab::Inspector => match code {
            KeyCode::Down | KeyCode::Char('j') => {
                app.inspector_scroll = app.inspector_scroll.saturating_add(1);
            }
            KeyCode::Up | KeyCode::Char('k') => {
                app.inspector_scroll = app.inspector_scroll.saturating_sub(1);
            }
            KeyCode::Char('m') => app.select_next_model(),
            KeyCode::Char('g') => app.inspector_scroll = 0,
            _ => {}
        },
        Tab::Compare => match code {
            KeyCode::Down | KeyCode::Char('j') => {
                app.compare_scroll = app.compare_scroll.saturating_add(1);
            }
            KeyCode::Up | KeyCode::Char('k') => {
                app.compare_scroll = app.compare_scroll.saturating_sub(1);
            }
            _ => {}
        },
        Tab::Spectrum => match code {
            KeyCode::Down | KeyCode::Char('j') => {
                app.spectrum_scroll = app.spectrum_scroll.saturating_add(1);
            }
            KeyCode::Up | KeyCode::Char('k') => {
                app.spectrum_scroll = app.spectrum_scroll.saturating_sub(1);
            }
            KeyCode::Char('m') => app.select_next_model(),
            KeyCode::Char('g') => app.spectrum_scroll = 0,
            _ => {}
        },
        Tab::Help => match code {
            KeyCode::Down | KeyCode::Char('j') => {
                app.help_scroll = app.help_scroll.saturating_add(1);
            }
            KeyCode::Up | KeyCode::Char('k') => {
                app.help_scroll = app.help_scroll.saturating_sub(1);
            }
            KeyCode::Char('g') => app.help_scroll = 0,
            KeyCode::Char('G') => app.help_scroll = 200,
            _ => {}
        },
    }
}

fn handle_file_browser_key(app: &mut App, code: KeyCode) {
    // Text input mode
    if app.file_browser.input_active {
        match code {
            KeyCode::Char(c) => app.file_browser.input_buffer.push(c),
            KeyCode::Backspace => {
                app.file_browser.input_buffer.pop();
            }
            KeyCode::Enter => {
                let path = std::path::PathBuf::from(&app.file_browser.input_buffer);
                if path.is_dir() {
                    app.file_browser.navigate_to(path);
                } else if path.is_file() {
                    app.load_image(&path);
                    app.file_browser.active = false;
                }
                app.file_browser.input_active = false;
            }
            KeyCode::Esc => {
                app.file_browser.input_active = false;
            }
            _ => {}
        }
        return;
    }

    // Navigation mode
    match code {
        KeyCode::Char('j') | KeyCode::Down => app.file_browser.select_down(),
        KeyCode::Char('k') | KeyCode::Up => app.file_browser.select_up(),
        KeyCode::Enter => {
            if let Some(entry) = app.file_browser.selected_entry() {
                let path = entry.path.clone();
                let is_dir = entry.is_dir;
                let is_image = entry.is_image;
                if is_dir {
                    app.file_browser.navigate_to(path);
                } else if is_image {
                    app.load_image(&path);
                    app.file_browser.active = false;
                }
            }
        }
        KeyCode::Backspace | KeyCode::Char('h') => {
            app.file_browser.go_up();
        }
        KeyCode::Esc | KeyCode::Char('q') => {
            app.file_browser.active = false;
        }
        KeyCode::Char('/') => {
            app.file_browser.toggle_input();
        }
        _ => {}
    }
}

// ── Rendering ───────────────────────────────────────────────────────────────

fn draw(frame: &mut Frame, app: &mut App) {
    let area = frame.area();

    // Background
    frame.render_widget(Block::new().style(Style::new().bg(theme::BG_DARK)), area);

    let layout = Layout::vertical([
        Constraint::Length(3), // Header + tabs
        Constraint::Fill(1),   // Content
        Constraint::Length(1), // Status bar
    ])
    .split(area);

    draw_header(frame, layout[0], app);

    match app.tab {
        Tab::Dashboard => views::dashboard::draw(frame, layout[1], app),
        Tab::Inspector => views::inspector::draw(frame, layout[1], app),
        Tab::Compare => views::compare::draw(frame, layout[1], app),
        Tab::Spectrum => views::spectrum::draw(frame, layout[1], app),
        Tab::Help => views::help::draw(frame, layout[1], app),
    }

    draw_status_bar(frame, layout[2], app);

    // File browser overlay (on top of everything)
    if app.file_browser.active {
        views::filebrowser::draw_overlay(frame, &app.file_browser);
    }
}

fn draw_header(frame: &mut Frame, area: Rect, app: &App) {
    let block = Block::bordered()
        .border_type(BorderType::Rounded)
        .border_style(Style::new().fg(theme::FG_DIM))
        .style(Style::new().bg(theme::BG_DARK));
    let inner = block.inner(area);
    frame.render_widget(block, area);

    let chunks = Layout::horizontal([
        Constraint::Length(22), // Title
        Constraint::Fill(1),    // Tabs
        Constraint::Length(8),  // Version
    ])
    .split(inner);

    // Title
    let title = Paragraph::new(Line::from(vec![
        Span::styled("", Style::new().fg(theme::BLUE)),
        Span::styled("LATENT", Style::new().fg(theme::FG_BRIGHT).bold()),
        Span::styled(" INSPECTOR", Style::new().fg(theme::BLUE).bold()),
    ]));
    frame.render_widget(title, chunks[0]);

    // Tabs
    let tab_titles: Vec<Line> = Tab::ALL
        .iter()
        .map(|tab| {
            let is_active = app.tab == *tab;
            if is_active {
                Line::from(vec![
                    Span::styled("", Style::new().fg(theme::BLUE)),
                    Span::styled(tab.label(), Style::new().fg(theme::FG_BRIGHT).bold()),
                ])
            } else {
                Line::from(vec![
                    Span::styled("", Style::new().fg(theme::FG_DIM)),
                    Span::styled(tab.label(), Style::new().fg(theme::FG_DIM)),
                ])
            }
        })
        .collect();

    let tabs = Tabs::new(tab_titles)
        .divider(Span::styled("", Style::new().fg(theme::FG_DIM)))
        .select(app.tab.index())
        .padding(" ", " ");
    frame.render_widget(tabs, chunks[1]);

    // Version
    let version = Paragraph::new(
        Line::from(Span::styled("v0.1.0", theme::dim_style())).alignment(Alignment::Right),
    );
    frame.render_widget(version, chunks[2]);
}

fn draw_status_bar(frame: &mut Frame, area: Rect, app: &App) {
    let tab_keys = match app.tab {
        Tab::Dashboard => "↑↓ select  enter inspect  ",
        Tab::Inspector => "↑↓ scroll  m model  ",
        Tab::Compare => "↑↓ scroll  ",
        Tab::Spectrum => "↑↓ scroll  m model  g top  ",
        Tab::Help => "↑↓ scroll  g top  G bottom  ",
    };

    let mode_indicator = if app.demo_mode { " [DEMO]" } else { "" };

    let image_indicator = app
        .image_path
        .as_ref()
        .and_then(|p| p.file_name())
        .and_then(|n| n.to_str())
        .map(|n| format!("  [{n}]"))
        .unwrap_or_default();

    let line = Line::from(vec![
        Span::styled(" ", Style::new().bg(theme::BG_PANEL)),
        Span::styled("q", theme::key_style()),
        Span::styled(
            " quit  ",
            Style::new().fg(theme::FG_DIM).bg(theme::BG_PANEL),
        ),
        Span::styled("←→", theme::key_style()),
        Span::styled(" tab  ", Style::new().fg(theme::FG_DIM).bg(theme::BG_PANEL)),
        Span::styled("o", theme::key_style()),
        Span::styled(
            " open  ",
            Style::new().fg(theme::FG_DIM).bg(theme::BG_PANEL),
        ),
        Span::styled(tab_keys, Style::new().fg(theme::FG_DIM).bg(theme::BG_PANEL)),
        Span::styled("?", theme::key_style()),
        Span::styled(" help", Style::new().fg(theme::FG_DIM).bg(theme::BG_PANEL)),
        Span::styled(
            image_indicator,
            Style::new().fg(theme::CYAN).bg(theme::BG_PANEL),
        ),
        Span::styled(
            mode_indicator.to_string(),
            Style::new().fg(theme::YELLOW).bg(theme::BG_PANEL).bold(),
        ),
    ]);

    let paragraph = Paragraph::new(line).style(Style::new().bg(theme::BG_PANEL));
    frame.render_widget(paragraph, area);
}

// ── Image preview rendering ─────────────────────────────────────────────────

/// Render an image thumbnail to styled half-block lines for terminal display.
/// Each terminal cell shows 2 vertical pixels using the `▀` character with
/// fg = top pixel, bg = bottom pixel.
pub fn render_image_preview(img: &image::RgbImage, width: u16, height: u16) -> Vec<Line<'static>> {
    let pixel_h = height as u32 * 2;
    let target_w = width as u32;

    // Calculate aspect-preserving size
    let img_w = img.width() as f32;
    let img_h = img.height() as f32;
    let scale = (target_w as f32 / img_w).min(pixel_h as f32 / img_h);
    let render_w = ((img_w * scale) as u32).max(1);
    let render_h = ((img_h * scale) as u32).max(1);

    let resized = image::imageops::resize(
        img,
        render_w,
        render_h,
        image::imageops::FilterType::Triangle,
    );

    let x_pad = target_w.saturating_sub(render_w) / 2;
    let y_pad = pixel_h.saturating_sub(render_h) / 2;

    let bg = Color::Rgb(26, 27, 38); // theme::BG_DARK

    let mut lines = Vec::new();
    for y_cell in 0..height as u32 {
        let mut spans = Vec::new();
        for x in 0..target_w {
            let img_x = x.wrapping_sub(x_pad);
            let top_y = (y_cell * 2).wrapping_sub(y_pad);
            let bot_y = (y_cell * 2 + 1).wrapping_sub(y_pad);

            let top_color = if img_x < render_w && top_y < render_h {
                let p = resized.get_pixel(img_x, top_y);
                Color::Rgb(p[0], p[1], p[2])
            } else {
                bg
            };

            let bot_color = if img_x < render_w && bot_y < render_h {
                let p = resized.get_pixel(img_x, bot_y);
                Color::Rgb(p[0], p[1], p[2])
            } else {
                bg
            };

            spans.push(Span::styled("", Style::new().fg(top_color).bg(bot_color)));
        }
        lines.push(Line::from(spans));
    }
    lines
}