Минималистичная библиотека для чтения подмножества бинарного формата хранения данных numpy "npz" с помощью чистого javascript
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

99 lines
3.5 KiB

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>Чтение .npz в браузере</title>
<script src="npz-reader.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
#input_file {
margin-bottom: 20px;
padding: 10px;
font-size: 16px;
}
#output {
background: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
.info {
margin-bottom: 10px;
color: #666;
}
</style>
</head>
<body>
<h1>Чтение NPZ файлов в браузере</h1>
<p class="info">Выберите .npz файл для чтения (без использования внешних библиотек)</p>
<input type="file" id="input_file" accept=".npz">
<pre id="output">Ожидание файла...</pre>
<script>
// Получаем ссылки на DOM-элементы
const input_file = document.getElementById('input_file');
const output_pre = document.getElementById('output');
// Обработчик выбора файла
input_file.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (!file) {
output_pre.textContent = 'Файл не выбран.';
return;
}
output_pre.textContent = 'Чтение файла...\nРазмер файла: ' + (file.size / 1024 / 1024).toFixed(2) + ' МБ';
try {
// Чтение файлового объекта как ArrayBuffer
console.log('Начало чтения файла...');
const array_buffer = await file.arrayBuffer();
console.log('Файл прочитан, размер:', array_buffer.byteLength, 'байт');
// Используем наш NPZReader для чтения файла
console.log('Начало парсинга NPZ...');
output_pre.textContent = 'Распаковка ZIP архива...';
const result = await NPZReader.readNPZ(array_buffer);
console.log('NPZ распарсен, количество массивов:', Object.keys(result).length);
// Форматируем вывод для лучшей читаемости
const formattedResult = {};
for (const [key, value] of Object.entries(result)) {
// Конвертируем данные в обычный массив, обрабатывая BigInt
let dataArray;
if (value.data instanceof BigInt64Array || value.data instanceof BigUint64Array) {
// Для BigInt конвертируем в строки
dataArray = Array.from(value.data).slice(0, 100).map(x => x.toString());
} else {
dataArray = Array.from(value.data).slice(0, 100);
}
formattedResult[key] = {
shape: value.shape,
dtype: value.dtype,
data: dataArray, // Показываем первые 100 элементов
totalElements: value.data.length,
fortran_order: value.fortran_order
};
}
// Выводим результаты (с обработкой BigInt)
output_pre.textContent = JSON.stringify(formattedResult, null, 2);
} catch (err) {
output_pre.textContent = 'Ошибка: ' + err.message + '\n\n' + err.stack;
console.error(err);
}
});
</script>
</body>
</html>