AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-3973175

con's questions

Martin Hope
con
Asked: 2024-07-02 09:07:41 +0800 CST

Perl 的捕获组在范围内消失

  • 5

我有一个非常简单的代码来解析文件名:

#!/usr/bin/env perl

use 5.040;
use warnings FATAL => 'all';
use autodie ':default';

my $string = '/home/con/bio.data/blastdb/phytophthora.infestans.KR_2_A2/GCA_012552325.1.protein.faa';

if ($string =~ m/blastdb\/(\w)\w+\.([\w\.]+)/) {
    my $rest = $2; # $1 would be valid here
    $rest =~ s/\./ /g;
    my $name = "$1.$rest"; # $1 disappears here
}

上述代码失败Use of uninitialized value $1 in concatenation (.) or string

但是,如果我将其保存$1到变量中,例如$g,信息不会丢失。

if ($string =~ m/blastdb\/(\w)\w+\.([\w\.]+)/) {
    my ($g, $rest) = ($1, $2);
    $rest =~ s/\./ /g;
    my $name = "$g.$rest";
}

所以我可以解决这个问题。

但是,$1它不应该就这样消失吗?$1在范围内不应该保持有效吗?这是 Perl 中的错误吗?还是我错过了https://perldoc.perl.org/perlretut中的某些规则?

regex
  • 1 个回答
  • 7 Views
Martin Hope
con
Asked: 2024-02-19 21:05:34 +0800 CST

hist2d 绘图时 vmin/vax 未知,直到使用组合颜色条绘图;以前的解决方案不起作用

  • 5

我正在尝试绘制行密度图。具有挑战性的部分是,每个图必须具有相同的色标最小值和最大值,并且我事先不知道最小值和最大值是多少。我必须首先绘制这些值,以便找出最小值和最大值,删除这些图,然后使用确定的最小值/最大值绘制新的图。

像这样的东西,但是使用 hist2d 并按行(来自另一个 SO 页面的图像): 在此输入图像描述

我提出了一个基于How to have one colorbar for all subplots , vmin and vmax in hist2d 的最小工作示例

以及来自“SpinUp __ A Davis”的解决方案,我不知道如何使用真实数据来实现:

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=1, ncols=3)
for ax in axes.flat: # I don't see how to implement this part with real data
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

fig.colorbar(im, ax=axes.ravel().tolist())

plt.show()

但我能想到的最好的办法是这个,这是行不通的:

import matplotlib.pyplot as plt
import numpy as np # only for MWE, not present in real file
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist2d.html

# getting min/max
fig, (ax0,ax1) = plt.subplots(nrows = 2, ncols = 1, sharex = True)
colormax = float("-inf")
colormin = float("inf")

h0 = ax0.hist2d(np.random.random(100), np.random.random(100))
colormax = max(h0[0].max(), colormax)
colormin = min(h0[0].min(), colormin)
h1 = ax1.hist2d(np.random.random(200), np.random.random(200))
colormax = max(h1[0].max(), colormax)
colormin = min(h1[0].min(), colormin)
# starting real plot, not just to get min/max
fig, axes = plt.subplots(nrows = 2, ncols = 1, sharex = True, figsize = (6.4,4.8))
i = 0
h = [h0, h1]
for ax in axes.flat: # I don't see how I can implement this
    im = ax.imshow(h[i], vmin = colormin, vmax = colormax)
    i += 1
fig.colorbar(im, ax= axes.ravel().tolist())
plt.savefig('imshow.debug.png')
python
  • 1 个回答
  • 47 Views
Martin Hope
con
Asked: 2024-01-27 02:34:26 +0800 CST

Perl 的日期时间未返回正确的时间差异

  • 7

受https://perlmaven.com/datetime的启发,我试图找到 2024-01-03T19:00:00 和 2024-01-07T16:00:00 (93 小时)之间的差异小时数。

#!/usr/bin/env perl

use 5.038;
use warnings FATAL => 'all';
use autodie ':default';
use DDP;
use Devel::Confess 'color';
use DateTime;

sub str_to_date ($date) {
    if ($date =~ m/^(\d+) # year
    \-(\d{1,2})           # month
    \-(\d{1,2})           # day
    T
    (\d+)                        # hour
    :
    (\d+)                        # minutes
    :
    (\d+)                        # seconds                  
    /x) {
        return DateTime -> new(
        year  => $1,
        month => $2,
        day   => $3,
        hour    => $4,
        minute=> $5,
        second=> $6,
    );
    } else {
        die "$date failed regex.";
    }
} # later dates are "greater"
my $wed_date = str_to_date('2024-01-03T19:00:00');
say $wed_date->day;
my $sun_date = str_to_date('2024-01-07T16:00:00');
my $diff = $sun_date - $wed_date; # returns a duration object

p $diff;
say $diff->clock_duration->in_units('hours'); # 21, but should be 93
say $diff->in_units('hours'); # 21 again
say $diff->hours; # 21 again
say $sun_date->delta_days($wed_date)->in_units('hours'); # 0
p $diff->deltas;

我从https://metacpan.org/pod/DateTime::Format::Duration找到的所有方法都给出了错误的答案。

或者,如果我删除解析,

#!/usr/bin/env perl
use 5.038;
use warnings FATAL => 'all';
use autodie ':default';
use DDP;
use Devel::Confess 'color';
use DateTime;

my $wed_date = DateTime->new(
    year => 2024,
    month=>1,
    day=>3,
    hour=>19
);#str_to_date('2024-01-03T19:00:00');
my $sun_date = DateTime->new(
    year=>2024,
    month=>1,
    day=>7,
    hour=>16#my $sun_date = str_to_date('2024-01-07T16:00:00');
);#
say $wed_date->day;

my $diff = $sun_date - $wed_date;

p $diff;
say $diff->clock_duration->in_units('hours'); # 21, but should be 93
say $diff->in_units('hours'); # 21 again
say $diff->hours; # 21 again
say $sun_date->delta_days($wed_date)->in_units('hours'); # 0
p $diff->deltas; # 10

答案是相同的。

DateTime::Duration 对象似乎忽略了中间的天数,而只关注小时之间的差异。

如何获得两个日期之间正确的小时数?

datetime
  • 2 个回答
  • 57 Views
Martin Hope
con
Asked: 2024-01-18 23:54:16 +0800 CST

Perl:减少字符串长度会增加字符串数组中的内存使用量

  • 8

我正在读取一个巨大的文件,以将数据存储在一个非常大的哈希中。我试图使 RAM 使用量尽可能小。

我有一个 MWE,它在 Perl 中表现出奇怪的行为:

#!/usr/bin/env perl

use 5.038;
use warnings FATAL => 'all';
use autodie ':default';
use DDP {output => 'STDOUT', array_max => 10, show_memsize => 1}; # pretty print with "p"

my @l = split /\s+/, 'OC   Pimascovirales; Iridoviridae; Betairidovirinae; Iridovirus.';
p @l;
$_ =~ s/[\.;]$// foreach @l; # single line keeps code shorter
p @l;

它有输出:

[
    [0] "OC",
    [1] "Pimascovirales;",
    [2] "Iridoviridae;",
    [3] "Betairidovirinae;",
    [4] "Iridovirus."
] (356B)
[
    [0] "OC",
    [1] "Pimascovirales",
    [2] "Iridoviridae",
    [3] "Betairidovirinae",
    [4] "Iridovirus"
] (400B)

虽然这个示例非常小,但我将多次执行此操作,因此 RAM 管理非常重要。

减少字符串长度如何将该数组的 RAM 大小从 356B增加到400B?

如果可能的话,我可以避免这样的增加吗?

perl
  • 3 个回答
  • 98 Views
Martin Hope
con
Asked: 2024-01-13 10:23:50 +0800 CST

python3 的 adjustment_text 移动文本效果很差

  • 6

我的脚本中有重叠的文本:

import matplotlib.pyplot as plt
from adjustText import adjust_text
x = [12,471,336,1300]
y = [2,5,4,11]
z = [0.1,0.2,0.3,0.4]
im = plt.scatter(x, y, c = z, cmap = "gist_rainbow", alpha = 0.5)
plt.colorbar(im)
texts = []
texts.append(plt.text(783, 7.62372448979592, 'TRL1'))
texts.append(plt.text(601, 6.05813953488372, 'CFT1'))
texts.append(plt.text(631, 4.28164556962025, 'PTR3'))
texts.append(plt.text(665, 7.68018018018018, 'STT4'))
texts.append(plt.text(607, 5.45888157894737, 'RSC9'))
texts.append(plt.text(914, 4.23497267759563, 'DOP1'))
texts.append(plt.text(612, 7.55138662316476, 'SEC8'))
texts.append(plt.text(766, 4.1264667535854, 'ATG1'))
texts.append(plt.text(681, 3.80205278592375, 'TFC3'))
plt.show()

显示重叠文本: 在此输入图像描述

但是,当我添加adjust_text:

import matplotlib.pyplot as plt
from adjustText import adjust_text
x = [12,471,336,1300]
y = [2,5,4,11]
z = [0.1,0.2,0.3,0.4]
im = plt.scatter(x, y, c = z, cmap = "gist_rainbow", alpha = 0.5)
plt.colorbar(im)
data = [
    (783, 7.62372448979592, 'TRL1'),
    (601, 6.05813953488372, 'CFT1'),
    (631, 4.28164556962025, 'PTR3'),
    (665, 7.68018018018018, 'STT4'),
    (607, 5.45888157894737, 'RSC9'),
    (914, 4.23497267759563, 'DOP1'),
    (612, 7.55138662316476, 'SEC8'),
    (766, 4.1264667535854, 'ATG1'),
    (681, 3.80205278592375, 'TFC3')
]

texts = [plt.text(x, y, l) for x, y, l in data]
adjust_text(texts)
plt.savefig('adjust.text.png', bbox_inches='tight', pad_inches = 0.1)

标签被移动到左下角,使它们变得毫无用处,而不是只是有点重叠。

我正在遵循adjust_text(texts)以下两个链接所建议的线索,

如何调整 Matplotlib 散点图中的文本以使散点不重叠?

和https://adjusttext.readthedocs.io/en/latest/Examples.html

我明白了:在此输入图像描述

我怎样才能adjust_text修复重叠的标签?

python
  • 1 个回答
  • 40 Views
Martin Hope
con
Asked: 2023-11-14 23:11:42 +0800 CST

在框外添加图形图例[重复]

  • 6
这个问题在这里已经有了答案:
matplotlib 中 plt.figure() 的必要性是什么? (2 个回答)
12 小时前关闭。

该帖子已于 11 小时前编辑并提交审核。

我正在尝试制作一个散点图,在框外有一个图形图例,正如如何将图例放在图之外所建议的那样,但我也在绘制多组数据一个用于多个散点图的颜色条。下面的最小工作示例删除了大部分集合,实际脚本中可以有 10 个集合:

import matplotlib.pyplot as plt
norm = plt.Normalize(55,1954)
plt.scatter([75,75,63,145,47],[0.979687,1.07782,2.83995,4.35468,4.44244], c = [75,75,70,70,70], norm = norm, label = 's1', cmap = 'gist_rainbow', marker = 'o')
plt.scatter([173,65],[0.263218,3.12112], c = [77,68], norm = norm, label = 's2', cmap = 'gist_rainbow', marker = 'v')
plt.colorbar().set_label('score')
plt.legend(loc = "outside center left")
plt.savefig('file.svg', bbox_inches='tight', pad_inches = 0.1)

但我收到错误:

/home/con/.local/lib/python3.10/site-packages/matplotlib/projections/__init__.py:63: UserWarning: Unable to import Axes3D. This may be due to multiple versions of Matplotlib being installed (e.g. as a system package and as a pip package). As a result, the 3D projection is not available.
  warnings.warn("Unable to import Axes3D. This may be due to multiple versions of "
Traceback (most recent call last):
  File "/tmp/2Le599Dl8S.py", line 6, in <module>
    plt.legend(loc = "outside center left")
  File "/home/con/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 3372, in legend
    return gca().legend(*args, **kwargs)
  File "/home/con/.local/lib/python3.10/site-packages/matplotlib/axes/_axes.py", line 323, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
  File "/home/con/.local/lib/python3.10/site-packages/matplotlib/legend.py", line 566, in __init__
    self.set_loc(loc)
  File "/home/con/.local/lib/python3.10/site-packages/matplotlib/legend.py", line 703, in set_loc
    raise ValueError(
ValueError: 'outside' option for loc='outside center left' keyword argument only works for figure legends

我也尝试过:

import matplotlib.pyplot as plt
norm = plt.Normalize(55,1954)
fig = plt.scatter([75,75,63,145,47],[0.979687,1.07782,2.83995,4.35468,4.44244], c = [75,75,70,70,70], norm = norm, label = 's1', cmap = 'gist_rainbow', marker = 'o')
plt.scatter([173,65],[0.263218,3.12112], c = [77,68], norm = norm, label = 's2', cmap = 'gist_rainbow', marker = 'v')
plt.colorbar().set_label('score')
fig.legend(loc = "outside center left")
plt.savefig('file.svg', bbox_inches='tight', pad_inches = 0.1)

但这又产生了另一个问题:

/home/con/.local/lib/python3.10/site-packages/matplotlib/projections/__init__.py:63: UserWarning: Unable to import Axes3D. This may be due to multiple versions of Matplotlib being installed (e.g. as a system package and as a pip package). As a result, the 3D projection is not available.
  warnings.warn("Unable to import Axes3D. This may be due to multiple versions of "
Traceback (most recent call last):
  File "/tmp/2Le599Dl8S.py", line 6, in <module>
    fig.legend(loc = "outside center left")
AttributeError: 'PathCollection' object has no attribute 'legend'

我还查看了matplotlib 中 plt.figure() 的必要性是什么?,但是里面没有关于图例的内容,所以那篇文章没有解决我的问题。它也没有出现在谷歌搜索中。

python
  • 1 个回答
  • 51 Views
Martin Hope
con
Asked: 2023-11-14 03:13:03 +0800 CST

我如何使用 Perl 的each 和列表?

  • 7

我试图:

#!/usr/bin/env perl

use 5.038;
use warnings FATAL => 'all';
use autodie ':all';
use Devel::Confess 'color';

while (my ($i, $t) = each('a','b','c')) {
    say "$i, $t";
}

但我收到错误:

Experimental each on scalar is now forbidden

('a','b','c')是一个标量?

我真的很喜欢 Perl 的each数组,因为我不必声明迭代器变量。

我也尝试过

while (my ($i, $t) = each(('a','b','c'))) {

while (my ($i, $t) = each(qw('a','b','c'))) {

但得到同样的错误。

while (my ($i, $t) = each(@{ ('a','b','c') }) {

但上面给出了一个错误:Useless use of a constant ("a") in void context这是我从如何解决 perl 中的“现在禁止标量上的实验值”问题得到的

我怎样才能让 Perl 相信each('a','b','c') 是一个数组?

perl
  • 2 个回答
  • 74 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve