Best Python code snippet using localstack_python
analysis_figure_histogram_logpx_bins.py
Source:analysis_figure_histogram_logpx_bins.py  
...157def tensor_to_image(obs: torch.Tensor) -> Image:158    return Image.fromarray(159        np.uint8(255 * obs.permute(1, 2, 0).squeeze().numpy())160    )161def export_image(sd: SplitDataset, idx: int, output_dir, relpath=False) -> str:162    obs = sd.get_item_by_index(idx)[0]163    img = tensor_to_image(obs)164    filename = f"{sd.name}_{idx}.png"165    pth = os.path.join(output_dir, filename)166    img.save(pth)167    return filename if relpath else pth168def random_satisfying(*args, size=1):169    first = args[0]170    assert isinstance(first, np.ndarray)171    init = np.ones_like(first, dtype=bool)172    cond = reduce(np.logical_and, args, init)173    indices = np.where(cond)[0]174    return np.random.choice(indices, size=size, replace=False)175def main():176    args = parse_args()177    np.random.seed(args.seed if args.seed else np.random.randint(10000))178    results = np.load(args.input, allow_pickle=True)179    metadata = results["metadata"][()]180    dataset = metadata["dataset"]181    root_seed = metadata["seed"]182    sd = SplitDataset(dataset, root_seed, params={"resize_64": True})183    outdir = args.outdir if args.outdir else os.path.dirname(args.output)184    relpath = args.outdir is None185    U = results["U"][:, -1]186    M = results["M"][:, -1]187    q = 0.95188    if dataset == "CelebA":189        binwidth = 500190        U_min = -17_000191        U_max = -11_000192        y_max = 60_000193        xticks = list(range(U_min, U_max + 1000, 1000))194        yticks = ["0", "20k", "40k", "60k"]195        legend_pos = "north east"196    elif dataset == "BinarizedMNIST" and metadata["learning_rate"] == 1e-3:197        binwidth = 10198        U_min = -150199        U_max = -30200        y_max = 16000201        xticks = list(range(U_min, U_max + 10, 20))202        yticks = ["0", "4k", "8k", "12k", "16k"]203        legend_pos = "north east"204    elif dataset == "BinarizedMNIST" and metadata["learning_rate"] == 1e-4:205        binwidth = 10206        U_min = -160207        U_max = -30208        y_max = 15000209        xticks = list(range(U_min, U_max + 10, 20))210        yticks = ["0", "5k", "10k", "15k"]211        legend_pos = "north east"212    else:213        raise NotImplementedError214    n_bins = (U_max - U_min) // binwidth + 1215    bins = (216        [-float("inf")]217        + [U_min + binwidth * i for i in range(n_bins)]218        + [float("inf")]219    )220    heights_reg = []221    heights_high = []222    midpoints = []223    for lb, ub in zip(bins[:-1], bins[1:]):224        midpoints.append(ub - binwidth / 2)225        in_bin = np.logical_and(U > lb, U <= ub)226        count_reg = np.sum(np.logical_and(in_bin, M <= np.quantile(M, q)))227        count_high = np.sum(np.logical_and(in_bin, M > np.quantile(M, q)))228        heights_reg.append(count_reg)229        heights_high.append(count_high)230    midpoints[-1] = midpoints[-2] + binwidth231    heights_reg = np.array(heights_reg)232    heights_high = np.array(heights_high)233    midpoints = np.array(midpoints)234    images = {}235    bottom_lines = []236    top_lines = []237    if dataset == "CelebA":238        xpos = U_min239        ypos = 19_000240        # Low logprob, low mem241        idx1, idx2 = random_satisfying(242            U < U_min, M < np.quantile(M, 0.5), size=2243        )244        pth1 = export_image(sd, idx1, outdir, relpath=relpath)245        pth2 = export_image(sd, idx2, outdir, relpath=relpath)246        images[pth1] = dict(247            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]248        )249        images[pth2] = dict(250            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]251        )252        # Low logprob, high mem253        idx1, idx2 = random_satisfying(254            U < U_min, M > np.quantile(M, 0.999), size=2255        )256        pth1 = export_image(sd, idx1, outdir, relpath=relpath)257        pth2 = export_image(sd, idx2, outdir, relpath=relpath)258        images[pth1] = dict(259            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]260        )261        images[pth2] = dict(262            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]263        )264        bottom_lines.append([(U_min - binwidth / 2, 4000), (U_min, 18_500)])265        ###266        ###267        xpos = -16_250268        # Med low log prob, low mem (1)269        idx1, idx2 = random_satisfying(270            U < -15_000, U > -15_500, M < np.quantile(M, 0.1), size=2271        )272        pth1 = export_image(sd, idx1, outdir, relpath=relpath)273        pth2 = export_image(sd, idx2, outdir, relpath=relpath)274        images[pth1] = dict(275            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]276        )277        images[pth2] = dict(278            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]279        )280        # Med low log prob, high mem (1)281        idx1, idx2 = random_satisfying(282            U < -15_000, U > -15_500, M > np.quantile(M, 0.999), size=2283        )284        pth1 = export_image(sd, idx1, outdir, relpath=relpath)285        pth2 = export_image(sd, idx2, outdir, relpath=relpath)286        images[pth1] = dict(287            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]288        )289        images[pth2] = dict(290            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]291        )292        bottom_lines.append([(-15_000 - binwidth / 2, 5000), (xpos, 18_500)])293        ###294        ###295        xpos = -15_500296        # Central log prob, low mem (1)297        idx1, idx2 = random_satisfying(298            U < -14_000, U > -14_500, M < np.quantile(M, 0.1), size=2299        )300        pth1 = export_image(sd, idx1, outdir, relpath=relpath)301        pth2 = export_image(sd, idx2, outdir, relpath=relpath)302        images[pth1] = dict(303            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]304        )305        images[pth2] = dict(306            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]307        )308        # Central log prob, high mem (1)309        idx1, idx2 = random_satisfying(310            U < -14_000, U > -14_500, M > np.quantile(M, 0.999), size=2311        )312        pth1 = export_image(sd, idx1, outdir, relpath=relpath)313        pth2 = export_image(sd, idx2, outdir, relpath=relpath)314        images[pth1] = dict(315            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]316        )317        images[pth2] = dict(318            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]319        )320        bottom_lines.append([(-14_250, 14_000), (xpos, 18_500)])321        ###322        ###323        xpos = -14_750324        # Central log prob, low mem (1)325        idx1, idx2 = random_satisfying(326            U < -13_000, U > -13_500, M < np.quantile(M, 0.1), size=2327        )328        pth1 = export_image(sd, idx1, outdir, relpath=relpath)329        pth2 = export_image(sd, idx2, outdir, relpath=relpath)330        images[pth1] = dict(331            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]332        )333        images[pth2] = dict(334            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]335        )336        # Central log prob, high mem (1)337        idx1, idx2 = random_satisfying(338            U < -13_000, U > -13_500, M > np.quantile(M, 0.995), size=2339        )340        pth1 = export_image(sd, idx1, outdir, relpath=relpath)341        pth2 = export_image(sd, idx2, outdir, relpath=relpath)342        images[pth1] = dict(343            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]344        )345        images[pth2] = dict(346            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]347        )348        top_lines.append([(-13250, 53_000), (-14_000, 59_000), (xpos, 58000)])349    elif dataset == "BinarizedMNIST" and metadata["learning_rate"] == 1e-3:350        xpos = -151351        ypos = 5_200352        # Low logprob, low mem353        idx1, idx2 = random_satisfying(354            U < U_min, M < np.quantile(M, 0.5), size=2355        )356        pth1 = export_image(sd, idx1, outdir, relpath=relpath)357        pth2 = export_image(sd, idx2, outdir, relpath=relpath)358        images[pth1] = dict(359            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]360        )361        images[pth2] = dict(362            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]363        )364        # Low logprob, high mem365        idx1, idx2 = random_satisfying(366            U < U_min, M > np.quantile(M, 0.999), size=2367        )368        pth1 = export_image(sd, idx1, outdir, relpath=relpath)369        pth2 = export_image(sd, idx2, outdir, relpath=relpath)370        images[pth1] = dict(371            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]372        )373        images[pth2] = dict(374            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]375        )376        bottom_lines.append([(U_min - binwidth / 2, 1200), (U_min, 4_900)])377        ####378        ####379        xpos = -138380        # Med low log prob, low mem (1)381        idx1, idx2 = random_satisfying(382            U < -110, U > -120, M < np.quantile(M, 0.1), size=2383        )384        pth1 = export_image(sd, idx1, outdir, relpath=relpath)385        pth2 = export_image(sd, idx2, outdir, relpath=relpath)386        images[pth1] = dict(387            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]388        )389        images[pth2] = dict(390            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]391        )392        # Med low log prob, high mem (1)393        idx1, idx2 = random_satisfying(394            U < -110, U > -120, M > np.quantile(M, 0.999), size=2395        )396        pth1 = export_image(sd, idx1, outdir, relpath=relpath)397        pth2 = export_image(sd, idx2, outdir, relpath=relpath)398        images[pth1] = dict(399            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]400        )401        images[pth2] = dict(402            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]403        )404        bottom_lines.append([(-110 - binwidth / 2, 3000), (xpos, 4_900)])405        ###406        ###407        xpos = -125408        # Central log prob, low mem (1)409        idx1, idx2 = random_satisfying(410            U < -80, U > -90, M < np.quantile(M, 0.1), size=2411        )412        pth1 = export_image(sd, idx1, outdir, relpath=relpath)413        pth2 = export_image(sd, idx2, outdir, relpath=relpath)414        images[pth1] = dict(415            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]416        )417        images[pth2] = dict(418            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]419        )420        # Central log prob, high mem (1)421        idx1, idx2 = random_satisfying(422            U < -80, U > -90, M > np.quantile(M, 0.995), size=2423        )424        pth1 = export_image(sd, idx1, outdir, relpath=relpath)425        pth2 = export_image(sd, idx2, outdir, relpath=relpath)426        images[pth1] = dict(427            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]428        )429        images[pth2] = dict(430            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]431        )432        top_lines.append([(-85, 14_000), (-100, 15_700), (xpos, 15_300)])433    elif dataset == "BinarizedMNIST" and metadata["learning_rate"] == 1e-4:434        xpos = -160435        ypos = 4_900436        # Low logprob, low mem437        idx1, idx2 = random_satisfying(438            U < U_min, M < np.quantile(M, 0.5), size=2439        )440        pth1 = export_image(sd, idx1, outdir, relpath=relpath)441        pth2 = export_image(sd, idx2, outdir, relpath=relpath)442        images[pth1] = dict(443            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]444        )445        images[pth2] = dict(446            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]447        )448        # Low logprob, high mem449        idx1, idx2 = random_satisfying(450            U < U_min, M > np.quantile(M, 0.999), size=2451        )452        pth1 = export_image(sd, idx1, outdir, relpath=relpath)453        pth2 = export_image(sd, idx2, outdir, relpath=relpath)454        images[pth1] = dict(455            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]456        )457        images[pth2] = dict(458            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]459        )460        bottom_lines.append([(U_min - binwidth / 2, 1200), (U_min, 4_800)])461        ####462        ####463        xpos = -146464        # Med low log prob, low mem (1)465        idx1, idx2 = random_satisfying(466            U < -110, U > -120, M < np.quantile(M, 0.1), size=2467        )468        pth1 = export_image(sd, idx1, outdir, relpath=relpath)469        pth2 = export_image(sd, idx2, outdir, relpath=relpath)470        images[pth1] = dict(471            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]472        )473        images[pth2] = dict(474            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]475        )476        # Med low log prob, high mem (1)477        idx1, idx2 = random_satisfying(478            U < -110, U > -120, M > np.quantile(M, 0.999), size=2479        )480        pth1 = export_image(sd, idx1, outdir, relpath=relpath)481        pth2 = export_image(sd, idx2, outdir, relpath=relpath)482        images[pth1] = dict(483            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]484        )485        images[pth2] = dict(486            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]487        )488        bottom_lines.append([(-110 - binwidth / 2, 3800), (xpos, 4_800)])489        ###490        ###491        xpos = -132492        # Central log prob, low mem (1)493        idx1, idx2 = random_satisfying(494            U < -80, U > -90, M < np.quantile(M, 0.1), size=2495        )496        pth1 = export_image(sd, idx1, outdir, relpath=relpath)497        pth2 = export_image(sd, idx2, outdir, relpath=relpath)498        images[pth1] = dict(499            mem=False, x=xpos, y=ypos, yshift=0, U=U[idx1], M=M[idx1]500        )501        images[pth2] = dict(502            mem=False, x=xpos, y=ypos, yshift=1, U=U[idx2], M=M[idx2]503        )504        # Central log prob, high mem (1)505        idx1, idx2 = random_satisfying(506            U < -80, U > -90, M > np.quantile(M, 0.995), size=2507        )508        pth1 = export_image(sd, idx1, outdir, relpath=relpath)509        pth2 = export_image(sd, idx2, outdir, relpath=relpath)510        images[pth1] = dict(511            mem=True, x=xpos, y=ypos, yshift=2, U=U[idx1], M=M[idx1]512        )513        images[pth2] = dict(514            mem=True, x=xpos, y=ypos, yshift=3, U=U[idx2], M=M[idx2]515        )516        top_lines.append([(-85, 13_800), (-110, 14_700), (xpos, 14_500)])517    else:518        raise NotImplementedError519    tex = make_tex(520        midpoints,521        heights_reg,522        heights_high,523        binwidth,...main-modified-for-bulk.py
Source:main-modified-for-bulk.py  
...7import image_effects.hsv_color_space as effects_hsv8import image_effects.luv_color_space as effects_luv9import image_effects.rgb_color_space as effects_rgb10count = 011def export_image(Image_Raw_Data) :12    print("[ ! ]  Exporting as an Image..")13    img_export_name = f"exported_image_files/mona_lisa-{count}.jpg"14    open_cv.imwrite(img_export_name, Image_Raw_Data)15# img_raw_data = open_cv.imread("./c.jpg")16    17count=count+118export_image(effects_rgb.bgr_2_rgb(open_cv.imread("./c.jpg")))19count=count+120export_image(effects_rgb.bgr_2_rgba(open_cv.imread("./c.jpg")))21count=count+122export_image(effects_rgb.grayscale_image(open_cv.imread("./c.jpg")))23count=count+124export_image(effects_rgb.red_channel_only(open_cv.imread("./c.jpg")))25count=count+126export_image(effects_rgb.green_channel_only(open_cv.imread("./c.jpg")))27count=count+128export_image(effects_rgb.blue_channel_only(open_cv.imread("./c.jpg")))29threshold = int(input("\n[ ? ]  Threshold Value : "))30count=count+131export_image(effects_rgb.custom_rgb_threshold(open_cv.imread("./c.jpg"),threshold))32r_threshold = int(input("\n[ ? ]  Red Threshold Value : "))33g_threshold = int(input("[ ? ]  Green Threshold Value : "))34b_threshold = int(input("[ ? ]  Blue Threshold Value : "))35count=count+136export_image(effects_rgb.custom_rgb_gain_or_loss(open_cv.imread("./c.jpg"),r_threshold,37                        g_threshold,b_threshold))38count=count+139export_image(effects_hsv.bgr_2_hsv(open_cv.imread("./c.jpg")))40count=count+141export_image(effects_hsv.hue_channel_only(open_cv.imread("./c.jpg")))42count=count+143export_image(effects_hsv.saturation_channel_only(open_cv.imread("./c.jpg")))44count=count+145export_image(effects_hsv.value_channel_only(open_cv.imread("./c.jpg")))46count=count+147export_image(effects_luv.bgr_2_luv(open_cv.imread("./c.jpg")))48count=count+149export_image(effects_luv.luminance_component_only(open_cv.imread("./c.jpg")))50count=count+151export_image(effects_luv.u_chroma_component_only(open_cv.imread("./c.jpg")))52count=count+153export_image(effects_luv.v_chroma_component_only(open_cv.imread("./c.jpg")))54count=count+155export_image(edge_detect.sobel_horizontal_edge_detect_algo(open_cv.imread("./c.jpg")))56count=count+157export_image(edge_detect.sobel_vertical_edge_detect_algo(open_cv.imread("./c.jpg")))58count=count+159export_image(edge_detect.sobel_bothaxis_bitws_or_edge_detect_algo(open_cv.imread("./c.jpg")))60count=count+161export_image(edge_detect.sobel_bothaxis_bitws_and_edge_detect_algo(open_cv.imread("./c.jpg")))62count=count+163export_image(edge_detect.sobel_bothaxis_bitws_xor_edge_detect_algo(open_cv.imread("./c.jpg")))64count=count+165export_image(edge_detect.sobel_bothaxis_bitws_not_edge_detect_algo(open_cv.imread("./c.jpg")))66count=count+167export_image(edge_detect.laplacian_edge_detect_algo(open_cv.imread("./c.jpg")))68threshold_a = int(input("\n[ ? ]  Threshold A Value : "))69threshold_b = int(input("[ ? ]  Threshold B Value : "))70count=count+1...export_to_image_editor.py
Source:export_to_image_editor.py  
1import bpy2import os3from bpy.types import Operator4from bpy_extras.io_utils import ImportHelper5class COATER_OT_image_editor_export(Operator):6    '''Exports the selected image paint canvas to the image editor defined in Blender's preferences'''7    bl_idname = "coater.image_editor_export"8    bl_label = "Export to External Image Editor"9    bl_description = "Exports the select image layer to the image editor defined in Blender's preferences"10    @ classmethod11    def poll(cls, context):12        return context.scene.coater_layers13        False14    def execute(self, context):15        export_image = context.scene.tool_settings.image_paint.canvas16        if export_image != None:17            if export_image.packed_file == None:18                if export_image.file_format == '' or export_image.filepath == '':19                    if export_image.is_dirty:20                        export_image.save()21                else:22                    self.report({'ERROR'}, "Export image has no defined filepath.")23            else:24                self.report({'ERROR'}, "Export image can't be packed to export to an external image editor.")25            bpy.ops.image.external_edit(filepath=export_image.filepath)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
