データタイプごとに重みをつけて Density Map 計算をする

データタイプごとに重みをつけて Density Map 計算をするスクリプトです

こちらからダウンロード (dose_weighted_densitymap.py)

スクリプト

import re
import csv
import sys
import os

# processing area, mesh size
area = (float(sys.argv[1]),float(sys.argv[2]),float(sys.argv[3]),float(sys.argv[4]))
mesh_size = (float(sys.argv[5]),float(sys.argv[6]))
result_file = ""
if len(sys.argv) is 8:
    result_file = sys.argv[7]
else:
    result_file = "result.csv"

# weight
min_weight = 0.8
max_weight = 1.2

def get_datatype_from_layername(layername):
    match = re.search(r":\s*(\d+)", layername)
    return int(match.group(1))

def get_weight_dict(datatypes, min_weight, max_weight):
    min_datatype = min(datatypes)
    max_datatype = max(datatypes)
    weight_dict = {}
    for v in datatypes:
        if max_datatype is min_datatype:
            weight_dict[v] = (min_weight + max_weight) / 2
        normalized = (v - min_datatype) / (max_datatype - min_datatype)
        weight = min_weight + normalized * (max_weight - min_weight)
        weight_dict[v] = weight
    return weight_dict

def output_result(origin, pitch, nx, ny, results, result_file):
    # Output result as VMAP CSV
    with open(result_file, 'w') as f:
        f.write(f"#vmapcsv {origin[0] + pitch[0] / 2},{origin[1] + pitch[1] / 2},{pitch[0]},{pitch[1]},{nx},{ny}\n")
        for result in results:
            x   = result[0][0]
            y   = result[0][1]
            val = result[1]
            f.write(f"{x},{y},{val:.10f}\n")

# Get datatypes and undisplay all datatypes.
datatypes = []
file_list_items = pynebv.file.items()
for item in file_list_items:
    if type(item) is not pynebv.ChipLayer:
        continue
    datatype = get_datatype_from_layername(item.name)
    datatypes.append(datatype)
    item.display = False
datatypes = list(set(datatypes))

# Calculate weight from datatypes in file
weight_dict = get_weight_dict(datatypes, min_weight, max_weight)

# round area by mesh
x_min, y_min, x_max, y_max  = area
mesh_w, mesh_h              = mesh_size

nx  = int((x_max - x_min) // mesh_w)
ny     = int((y_max - y_min) // mesh_h)
x_end   = x_min + nx * mesh_w
y_end   = y_min + ny * mesh_h

# for save results
results = []
y = y_min
while y < y_end:
    x = x_min
    while x < x_end:
        mesh = [(x + mesh_w / 2, y + mesh_h / 2), 0.0]
        results.append(mesh)
        x += mesh_w
    y += mesh_h

# Calculate density for each datatype
def next_datatype(layers):
    pynebv.calculation.clear()
    for layer in layers:
        layer.display = False
for datatype in datatypes:
    print(f"\"{datatype}\" proessing start")
    # display only target datatype
    layers = []
    for item in file_list_items:
        if type(item) is not pynebv.ChipLayer:
            continue
        if datatype is not get_datatype_from_layername(item.name):
            continue
        item.display = True
        # keep layer for undisplaying later
        layers.append(item)

    # Go to next datatypes if there are no figures.
    figure_count = pynebv.calculation.figure_count((x_min,y_min,x_end,y_end))
    if int(figure_count.props()['Count']) is 0:
        next_datatype(layers)
        continue

    weight = weight_dict[datatype]
    # Calculate Density map and get results from file
    pynebv.calculation.density_map((x_min,y_min,x_end,y_end), mesh={'size': [mesh_w, mesh_h], 'overlap': 0.0, 'originType': 'Selected Area Lower-Left', 'clipType': 'Overhang'})
    tmpfile = f"{datatype}.csv"
    pynebv.calculation.save(tmpfile, type='Density Map', format='Csv')
    with open(tmpfile, 'r', newline='') as f:
        reader = csv.reader(f)
        i = 0
        for row in reader:
            if not row or row[0].startswith('#'):
                continue
            val = float(row[4])
            results[i][1] += val * weight
            i += 1
    # Cleanup
    os.remove(tmpfile)
    next_datatype(layers)

# Output results
output_result((area[0], area[1]), (mesh_w, mesh_h), nx, ny, results, result_file)

使用方法

$ nebv --python-file "/path/to/script.py <sx> <sy> <ex> <ey> <mesh width> <mesh height> [/path/to/output_csv]"

実行結果

出力ファイル

VMAP CSV ファイル

コンソール

なし

最終更新 18.08.2026