我希望在我的 Snakemake 工作流程中拥有各种动态“快捷方式”(规则名称),而不需要标记文件。我想到的方法与evalGNU Make 中的方法类似,但 Snakemake 似乎无法评估 Snakefile 语法中的变量扩展代码。有办法做到这一点吗?
这是一个简化的示例 Snakefile。我想要一个与每个输出“阶段”相对应的规则名称,现在我必须手动定义它们。想象一下,如果我有更多“阶段”和“步骤”,并且希望有一个规则,如果我添加这些阶段,可以生成所有“b”、“d”或“z”文件。动态定义规则名称比定义每次添加新阶段时更新的每个组合要简洁得多。
stages = ['a', 'b']
steps = [1, 2]
rule all:
input:
expand('{stage}{step}_file', stage=stages, step=steps)
rule:
output:
touch('{stage}{step}_file')
# Can these two be combined so that I don't have to add more
# rules for each new "stage" above while retaining the shorthand
# rule name corresponding to the stage?
rule a:
input: expand('a{step}_file', step=steps)
rule b:
input: expand('b{step}_file', step=steps)
Run Code Online (Sandbox Code Playgroud) 将包含“10%”和“0.10”类型字符串的 Pandas 系列转换为数值的最佳方法是什么?
我知道如果我有一个只有“0.10”类型字符串的系列,我可以这样做pd.to_numeric。
我也知道,如果我有一系列“10%”类型的字符串,我可以做str.replace("%","")然后做pd.to_numeric除以 100。
我遇到的问题是混合了“0.10”和“10%”类型字符串的系列。如何最好地将其转换为具有正确数字类型的系列。
我想我可以通过首先使用 True / False 制作一个临时系列来实现,具体取决于字符串中是否包含“%”,然后基于应用函数。但这似乎效率很低。
有没有更好的办法?
我尝试过的供参考:
mixed = pd.Series(["10%","0.10","5.5%","0.02563"])
mixed.str.replace("%","").astype("float")/100
0 0.100000
1 0.001000
2 0.055000
3 0.000256
dtype: float64
# This doesn't work, because even the 0.10 and 0.02563 are divided by 100.
Run Code Online (Sandbox Code Playgroud) fastai我试图了解使用's fastcore.basics.patch_toDecorator的附加值。方法如下fastcore:
from fastcore.basics import patch_to
class _T3(int):
pass
@patch_to(_T3)
def func1(self, a):
return self + a
Run Code Online (Sandbox Code Playgroud)
这是简单的猴子修补方法:
class simple_T3(int):
pass
def func1(self, a):
return self + a
simple_T3.func1 = func1
Run Code Online (Sandbox Code Playgroud)
检查这两个类没有发现任何差异。我知道简单的猴子修补可能会在更复杂的情况下导致问题,所以很高兴知道这些情况是什么?换句话说,它的附加值是多少fastcore.basics.patch_to?
我正在使用下面的代码,但在旋转 DataFrame 后出现错误:
数据框:
name day value time
0 MAC000002 2012-12-16 0.147 09:30:00
1 MAC000002 2012-12-16 0.110 10:00:00
2 MAC000002 2012-12-16 0.736 10:30:00
3 MAC000002 2012-12-16 0.404 11:00:00
4 MAC000003 2012-12-16 0.845 00:30:00
Run Code Online (Sandbox Code Playgroud)
读入数据并进行透视
name day value time
0 MAC000002 2012-12-16 0.147 09:30:00
1 MAC000002 2012-12-16 0.110 10:00:00
2 MAC000002 2012-12-16 0.736 10:30:00
3 MAC000002 2012-12-16 0.404 11:00:00
4 MAC000003 2012-12-16 0.845 00:30:00
Run Code Online (Sandbox Code Playgroud)
预期结果(带或不带索引)- 将行旋转到列而不进行任何值修改:
MAC000002 MAC000003 ...
0.147 0.845
0.110 ...
0.736 ...
0.404 ...
Run Code Online (Sandbox Code Playgroud)
知道为什么我会遇到这个问题KeyError 'index'以及如何克服这个问题吗?
想象一下我有一个Dask来自read_csv或以其他方式创建的 DataFrame。
如何为 dask 数据框创建唯一索引?
笔记:
reset_index在每个分区中构建单调升序索引。这意味着分区 1 为 (0,1,2,3,4,5,... ),分区 2 为 (0,1,2,3,4,5,... ),(0,1,2 ,3,4,5,... ) 对于分区 3 等等。
我想要数据帧中的每一行(跨所有分区)都有一个唯一的索引。
我想将该Client.map方法应用于使用多个参数的函数,Pool.starmap就像multiprocessing. 这是一个例子
from contextlib import contextmanager
from dask.distributed import Client
@contextmanager
def dask_client(**kwargs):
"""some docs"""
kwargs.setdefault("ip", "localhost:8786")
client = Client(**kwargs)
try:
yield client
except Exception:
raise
finally:
client.close()
def f(x,y,z):
return x+y+z
# Dummy function
if __name__ == "__main__":
with dask_client() as client:
client.map(f, (1,2,3), (1,2,3))
distributed.worker - WARNING - Compute Failed
Function: f
args: (1, 1)
kwargs: {}
Exception: TypeError("f() missing 1 required positional argument: 'z'")
distributed.worker - WARNING - Compute Failed
Function: …Run Code Online (Sandbox Code Playgroud) 当我py -3 -m venv venv在命令提示符下运行以创建虚拟环境时,它失败了。
"Error: Command '['C:\\Users\\Guest\\Desktop\\Python Web
App\\venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-
pip']' returned non-zero exit status 101.
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?我已经安装了最新的 Python 版本。我在为 Web 开发设置 Flask 的路上被困在这里。
目前,在具有约 15 万个节点和 200 万条边的无向图上进行计算nx.triangles(G)非常慢(大约需要 80 小时)。如果节点度分布高度倾斜,使用以下过程计算三角形是否有问题?
import networkx as nx
def largest_degree_node(G):
# this was improved using suggestion by Stef in the comments
return max(G.degree(), key=lambda x: x[1])[0]
def count_triangles(G):
G=G.copy()
triangle_counts = 0
while len(G.nodes()):
focal_node = largest_degree_node(G)
triangle_counts += nx.triangles(G, nodes=[focal_node])[focal_node]
G.remove_node(focal_node)
return triangle_counts
G = nx.erdos_renyi_graph(1000, 0.1)
# compute triangles with nx
triangles_nx = int(sum(v for k, v in nx.triangles(G).items()) / 3)
# compute triangles iteratively
triangles_iterative = count_triangles(G)
# assertion passes
assert int(triangles_nx) …Run Code Online (Sandbox Code Playgroud) 问题:将图形从networkxpytorch 几何图形转换为图形时如何保留节点顺序/标签?
代码:(在 Google Colab 中运行)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import torch
from torch.nn import Linear
import torch.nn.functional as F
torch.__version__
# install pytorch geometric
!pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
from torch_geometric.nn import GCNConv
from torch_geometric.utils.convert import to_networkx, from_networkx
# Make the networkx graph
G = nx.Graph()
# Add some cars
G.add_nodes_from([
('Ford', {'y': 0, 'Name': 'Ford'}),
('Lexus', {'y': 1, …Run Code Online (Sandbox Code Playgroud) 我正在一台具有 16GB RAM 的机器上运行下面粘贴的代码(故意)。
import dask.array as da
import dask.delayed
from sklearn.datasets import make_blobs
import numpy as np
from dask_ml.cluster import KMeans
from dask.distributed import Client
client = Client(n_workers=4, threads_per_worker=1, processes=False,
memory_limit='2GB', scheduler_port=0,
silence_logs=False, dashboard_address=8787)
n_centers = 12
n_features = 4
X_small, y_small = make_blobs(n_samples=1000, centers=n_centers, n_features=n_features, random_state=0)
centers = np.zeros((n_centers, n_features))
for i in range(n_centers):
centers[i] = X_small[y_small == i].mean(0)
print(centers)
n_samples_per_block = 450 * 650 * 900
n_blocks = 4
delayeds = [dask.delayed(make_blobs)(n_samples=n_samples_per_block,
centers=centers,
n_features=n_features,
random_state=i)[0]
for i …Run Code Online (Sandbox Code Playgroud) python ×10
dask ×4
pandas ×3
python-3.x ×3
dataframe ×2
networkx ×2
bash ×1
dask-ml ×1
eval ×1
fast-ai ×1
format ×1
gnu-make ×1
graph-theory ×1
k-means ×1
performance ×1
pivot-table ×1
pytorch ×1
scikit-learn ×1
snakemake ×1
string ×1
virtualenv ×1