Skip to content

Commit

Permalink
Feature/text chart (#337)
Browse files Browse the repository at this point in the history
* feat: create text tag

* feat: init

* feat: text data

* feat: zoom

* feat: text detail

* feat: step

* style

* fix: width

* fix: change

* fix: change

* feat: modal title

* fix: line clamp

* test: text log

* Squashed commit of the following:

commit 6ec7f6d
Author: KAAANG <79990647+SAKURA-CAT@users.noreply.github.com>
Date:   Thu Feb 22 16:13:17 2024 +0800

    record max step and min step (#341)

commit ce48cd8
Author: KAAANG <cunykang@gmail.com>
Date:   Thu Feb 22 15:51:42 2024 +0800

    delete Video

commit a11a7ed
Author: Bote Huang <106975133+KashiwaByte@users.noreply.github.com>
Date:   Thu Feb 22 15:36:32 2024 +0800

    Feat/video chart (#338)

    * feat:create class Video

    * feat:add __init__,add video chart

    * feat:add function for saving video

    * feat:add function for getting list of video

    * feat:add function get_data

    * feat:add function __save and __prepare_video

    * feat:add function load_from_str and load_from_BytesIO

    * bugfix: fix __save function

    * del test file

commit 6b5b9c3
Author: KAAANG <79990647+SAKURA-CAT@users.noreply.github.com>
Date:   Wed Feb 21 15:25:33 2024 +0800

    Feat/new line chart (#334)

    * add type——linear

    * label formatter

    * update tick

commit 788d8c1
Author: Ze-Yi LIN <58305964+xiaolin199912@users.noreply.github.com>
Date:   Tue Feb 20 21:07:39 2024 +0800

    Feat/image-add-parameters-boxes-masks (#329)

    * add BoundingBoxes class

    * update something

    * Update transfer_logfile_0.1.4.py

    * add ImageMask class

    * bugfix

commit 65a6378
Author: Ze-Yi LIN <58305964+xiaolin199912@users.noreply.github.com>
Date:   Mon Feb 19 18:42:11 2024 +0800

    Readme v0.2.0 (#326)

commit 7b964e9
Author: Zirui Cai <74649535+Feudalman@users.noreply.github.com>
Date:   Mon Feb 19 18:29:05 2024 +0800

    Fixbug/tag sort (#327)

* use hash name to filename

* feat: get text

* feat: get text

* feat: caption

* fix: log page layout

* ui: color

* feat: skeleton

* fix: detail caption

* fix: length

* feat: log height

* feat: page index

* fix: flash

* fix: skeleton

* fix: flash

* fix: some bug

* fix:wrap

* ui: color

* Squashed commit of the following:

commit b328478
Author: KAAANG <79990647+SAKURA-CAT@users.noreply.github.com>
Date:   Mon Feb 26 22:12:23 2024 +0800

    Update/line chart styles (#351)

    * hover active

    * hover top

    * optimize hover top

    * Update LineChart.vue

    * update hover

    * allow hover while running

    * Limit height when zooming in

    * fix slidebar bug

    * thickenByTag

    * Logic to distinguish between multiple data charts and single data charts

    * allow config crosshair

    * update crosshairsColor

    * update doc

    * update thickerLineWidth

    ---------

    Co-authored-by: ZeYi Lin <944270057@qq.com>

* refactor: click to turn page

* feat: enter and blur

* doc: comments

* fix: bg height

* Update launch.json

* refactor: get texts

* refactor: script to create experiment

---------

Co-authored-by: ZeYi Lin <944270057@qq.com>
Co-authored-by: KAAANG <cunykang@gmail.com>
  • Loading branch information
3 people authored Feb 28, 2024
1 parent 113d4bc commit bb6d595
Show file tree
Hide file tree
Showing 17 changed files with 675 additions and 40 deletions.
20 changes: 16 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
//sys.path 会加入顶层目录,影响模块导入查询路径
"env": { "PYTHONPATH": "${workspaceFolder}" }
},
// 打包命令
// 开发调试
{
"name": "构建项目",
"name": "开发调试",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/build_pypi.py",
"program": "${workspaceFolder}/test/test_create_experiment.py",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}"
"cwd": "${workspaceFolder}",
//sys.path 会加入顶层目录,影响模块导入查询路径
"env": { "PYTHONPATH": "${workspaceFolder}" }
},
// 模拟实验开启
{
Expand Down Expand Up @@ -69,6 +71,16 @@
"cwd": "${workspaceFolder}",
//sys.path 会加入顶层目录,影响模块导入查询路径
"env": { "PYTHONPATH": "${workspaceFolder}" }
},
// 打包命令
{
"name": "构建项目",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/build_pypi.py",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}"
}
]
}
1 change: 1 addition & 0 deletions swanlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
config,
Audio,
Image,
Text,
Run,
)

Expand Down
3 changes: 2 additions & 1 deletion swanlab/data/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


class FloatConvertible(Protocol):
def __float__(self) -> float: ...
def __float__(self) -> float:
...


DataType = Union[float, FloatConvertible, int, BaseType]
41 changes: 23 additions & 18 deletions swanlab/data/modules/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .base import BaseType
import os
from typing import Union, List
from ..utils.file import get_text_sha256_hash


class Text(BaseType):
Expand All @@ -25,25 +26,29 @@ def get_data(self):
# 如果传入的是Text类列表
if isinstance(self.value, list):
return self.get_data_list()
else:
# 预处理文本数据
self.__preprocess(self.value)

# 设置文本数据的保存路径
save_dir = os.path.join(self.settings.static_dir, self.tag)
save_name = (
f"{self.caption}-step{self.step}-{self.text_data[:16]}.txt"
if self.caption is not None
else f"text-step{self.step}-{self.text_data[:16]}.txt"
)
# 如果路径不存在,则创建路径
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_path = os.path.join(save_dir, save_name)

# 保存文本数据写入到指定目录的指定json文件
self.__save(save_path)
return save_name
# 预处理文本数据
self.__preprocess(self.value)

# 获取文本的hash值
hash_name = get_text_sha256_hash(self.text_data)[:16]

# 设置文本数据的保存路径
save_dir = os.path.join(self.settings.static_dir, self.tag)
save_name = (
f"{self.caption}-step{self.step}-{hash_name}.txt"
if self.caption is not None
else f"text-step{self.step}-{hash_name}.txt"
)

# 如果路径不存在,则创建路径
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_path = os.path.join(save_dir, save_name)

# 保存文本数据写入到指定目录的指定json文件
self.__save(save_path)
return save_name

def expect_types(self, *args, **kwargs) -> list:
return ["str", "int", "float"]
Expand Down
13 changes: 13 additions & 0 deletions swanlab/data/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ def get_file_hash_pil(image) -> str:
image.save(buffer, format="PNG") # 可以选择其他格式,如'JPEG'
hash_sha256.update(buffer.getvalue())
return hash_sha256.hexdigest()


def get_text_sha256_hash(text):
"""计算并返回给定文本的SHA-256哈希值。"""
import hashlib

# 使用hashlib库创建一个sha256哈希对象
hash_object = hashlib.sha256()
# 对输入的文本进行编码,因为hashlib需要的是字节对象
hash_object.update(text.encode())
# 获取十六进制格式的哈希值
hex_dig = hash_object.hexdigest()
return hex_dig
1 change: 1 addition & 0 deletions test/create_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
swanlab.log(
{
"test/image": swanlab.Image(test_image, caption="test"),
"test/text": swanlab.Text("hello swanlab!", caption="swanlab official"),
},
step=epoch,
)
Expand Down
9 changes: 8 additions & 1 deletion vue/src/charts/components/ChartContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import SLIcon from '@swanlab-vue/components/SLIcon.vue'
import { addTaskToBrowserMainThread } from '@swanlab-vue/utils/browser'
import LineChart from '../package/LineChart.vue'
import AudioChart from '../package/AudioChart.vue'
import TextChart from '../package/TextChart.vue'
import UnknownChart from '../package/UnknownChart.vue'
import PannelButton from './PannelButton.vue'
import { debounce } from '@swanlab-vue/utils/common'
Expand Down Expand Up @@ -98,6 +99,11 @@ const chartComponent = computed(() => {
type: ImageChart,
class: 'image-chart'
}
case 'text':
return {
type: TextChart,
class: 'text-chart'
}
default:
return {
type: UnknownChart
Expand Down Expand Up @@ -215,7 +221,8 @@ defineExpose({
// ---------------------------------- 图表样式 ----------------------------------
.audio-chart,
.image-chart {
.image-chart,
.text-chart {
@apply col-span-3;
}
</style>
24 changes: 14 additions & 10 deletions vue/src/charts/components/SlideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
</div>
<!-- <p>{{ max }}</p> -->
<!-- 输入框 -->
<div class="w-16 h-6 border rounded flex items-center p-1">
<input
class="w-full h-full border-none outline-none bg-transparent text-xs"
type="number"
v-model="_modelValue"
@change="handleChange"
/>
<div class="w-3 flex-shrink-0 flex-col flex">
<div class="flex items-center relative">
<input type="number" :value="_modelValue" ref="input" @keyup.enter="handleChange" @blur="handleChange" />
<div class="w-3 flex-shrink-0 flex-col flex absolute right-1">
<SLIcon icon="down" class="w-full h-3 -rotate-180 -mb-1" @click="handleClickUp" />
<SLIcon icon="down" class="w-full aspect-square" @click="handleClickDown" />
</div>
Expand All @@ -28,7 +23,7 @@
* @file: SlideBar.vue
* @since: 2024-01-30 16:18:31
**/
import { computed } from 'vue'
import { computed, ref } from 'vue'
const props = defineProps({
max: {
Expand All @@ -55,6 +50,8 @@ const props = defineProps({
const emits = defineEmits(['update:modelValue', 'change', 'turn'])
const input = ref(null)
const _modelValue = computed({
get() {
return props.modelValue
Expand All @@ -67,6 +64,7 @@ const _modelValue = computed({
}
emits('update:modelValue', value)
emits('change', value)
input.value.value = _modelValue.value
}
})
Expand All @@ -91,8 +89,10 @@ const handleClickUp = () => {
}
// ---------------------------------- 当input输入结束时,再次赋值 ----------------------------------
const handleChange = (e) => {
e.target.value = _modelValue.value
if (e.target.value == _modelValue.value) return
_modelValue.value = e.target.value
}
</script>

Expand All @@ -112,4 +112,8 @@ const handleChange = (e) => {
appearance: textfield;
}
}
input {
@apply w-16 h-6 pl-1 pr-5 rounded border outline-none bg-transparent text-xs focus:border-primary-default;
}
</style>
58 changes: 58 additions & 0 deletions vue/src/charts/modules/TextDetail.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<template>
<div class="px-6 py-10 overflow-hidden bg-higher rounded-lg">
<!-- infos -->
<div v-for="item in info_list" :key="item" class="sm:flex pb-3">
<span class="block w-48 font-semibold shrink-0">{{ $t(`common.chart.text-chart.titles.${item.key}`) }}:</span>
<span :title="item.value">{{ item.value }}</span>
</div>
<!-- text -->
<p class="font-semibold pb-2">{{ $t('common.chart.text-chart.titles.text') }}:</p>
<div class="p-4 min-h-[20vh] max-h-[55vh] overflow-y-auto border rounded bg-default">
<p v-for="text in data.text.split('\n')" :key="text">
<span v-if="text != ''">{{ text }}</span>
<br v-else />
</p>
</div>
</div>
</template>

<script setup>
/**
* @description: 展示 text 详细信息,与弹窗中调用
* @file: TextDetail.vue
* @since: 2024-02-21 13:46:40
**/
import { computed } from 'vue'
const props = defineProps({
data: {
type: Object,
required: true,
default: () => {}
}
})
const info_list = computed(() => {
const line = props.data.line
return [
{
key: 'tag',
value: props.data.tag
},
{
key: 'step',
value: line.index
},
{
key: 'caption',
value: props.data.caption || '-'
},
{
key: 'count',
value: props.data.text.replace(/[\r\n\s]/g, '').length
}
]
})
</script>

<style lang="scss" scoped></style>
Loading

0 comments on commit bb6d595

Please sign in to comment.