Skip to main content

Posts

Absolute vs. Relative File Path (Python)

Is it better to use a relative or absolute file path in Python, in both a situational setting and for best practice? I know what both of them do, and I'm wondering if, for example, always using an absolute file path is better practice than using a relative file path, or if it depends. And if it is a situational thing, when should you use one or the other? source https://stackoverflow.com/questions/77695077/absolute-vs-relative-file-path-python

Batch update entity property in pyautocad

I am making a pyqt widget tool which should operate with data in AutoCAD. Basically it allow user to select color and fill it in selected hatch objects in AutoCAD. It actually can be done directly in AutoCAD but sometimes it's time consuming task to open custom palette and manually select color. My current tool is based on pyautocad and pyqt modules. This is how it looks like: While widget is opened, user can select hatches and then select type in widget and then press Fill type button and finally we got repainted hatches. This is how it looks like in Python code: red, green, blue = 146, 32, 6 acad = Autocad() # initializing AutoCAD connection list_to_upd = [] curr_sset = acad.doc.PickfirstSelectionSet # get a list of selected entities new_color = None for obj in curr_sset: if obj.ObjectName == 'AcDbHatch': # check if entity is hatch type if not new_color: tcolor = obj.TrueColor tcolor.SetRGB(red, green, blue) new_colo

How to solve Tabula error reading pdf to pandas?

Help. I am having the following error in reading pdf files using tabula: Error importing jpype dependencies. Fallback to subprocess. No module named 'jpype' Error from tabula-java: The operation couldn’t be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java. i'm using a macbook with the following code: from tabula import read_pdf for file in glob.glob(os.path.join(link_scrape['pdfs'],'*.pdf')): try: df = read_pdf(file,pages='all') except Exception as e: print(e) break how do i resolve this? source https://stackoverflow.com/questions/77691801/how-to-solve-tabula-error-reading-pdf-to-pandas

Python interface for openEMS software not installing

I am trying to install an open source Maxwell's equations solver known as OpenEMS. To run that software the python interface for two things within it are required to be manually installed through terminal-CSXCAD and openEMS. To do that I have to go into the python directory within both CSXCAD and the openems directory in terminal and run the command pip install --user . . However it is not happening every time I try. When I try to run it within the CSXCAD python directory it says the following, python setup.py bdist_wheel did not run succesfully And when I try to install the python interface for openems I get the following errors, python setup.py egg_info did not run succesfully I searched up the problems that were coming such as python setup.py egg info did not run successfully for the CSXCAD python interface and python setup.py bdist_wheel did not run successfully. I even tried upgrading pip and setuptools but nothing is working source https://stackoverflow.com/questions

FID and custom feature extractor

I would like to use a custom feature extractor to calculate FID according to https://lightning.ai/docs/torchmetrics/stable/image/frechet_inception_distance.html I can use nn.Module for feature What is wrong with the following code? import torch _ = torch.manual_seed(123) from torchmetrics.image.fid import FrechetInceptionDistance from torchvision.models import inception_v3 net = inception_v3() checkpoint = torch.load('checkpoint.pt') net.load_state_dict(checkpoint['state_dict']) net.eval() fid = FrechetInceptionDistance(feature=net) # generate two slightly overlapping image intensity distributions imgs_dist1 = torch.randint(0, 200, (100, 3, 299, 299), dtype=torch.uint8) imgs_dist2 = torch.randint(100, 255, (100, 3, 299, 299), dtype=torch.uint8) fid.update(imgs_dist1, real=True) fid.update(imgs_dist2, real=False) result = fid.compute() print(result) Traceback (most recent call last): File "foo.py", line 12, in <module> fid = FrechetInce

Unable to resolve KeyError: "Index 'slice(None, None, None)' is not valid for indexed component 'MindtPy_utils.objective_value'"

import pandas as pd import random as r import numpy as np import glpk from pyomo.environ import * from amplpy import AMPL def pyblock(pyp, pytau, pyr, pys): M = ConcreteModel() M.m = Set(initialize = list(range(int(len(pyp))))) M.e = Set(initialize = list(range(int(len(pyr))))) M.s = Set(initialize = list(range(int(pys)))) M.r = Param(M.e, initialize = pyr) M.tau = Param(M.m, initialize = pytau) M.p = Param(M.m, M.e, M.s, initialize = 0) M.n = Var(M.m, M.e, M.s, domain=NonNegativeIntegers, initialize=0) def obj(M): return sum(-log(1-prod((1-pyp[i,j,k])**(M.n[i,j,k]) for j in M.e for k in M.s)) for i in M.m) M.obj=Objective(rule=obj, sense=minimize) def fire_rate(M, j, k): return sum(M.n[i,j,k] for i in M.m) <= M.r[j] M.fire_rate=Constraint(M.e, M.s, rule = fire_rate) opt = SolverFactory('mindtpy') results = opt.solve( M, mip_solver = 'cplex', nlp_solver = 'ipopt', tee=True ) #

session.execute with multiple databases

I want to execute raw sql on separate databases through sqlalchemy sessions. My session/engine is configured as follows: db1Base = declarative_base() db2Base = declarative_base() DB_ENGINES['db1'] = create_engine(db1_postgres_url, **connection_args) DB_ENGINES['db2'] = create_engine(db2_postgres_url, **connection_args) session_factory = sessionmaker(autocommit=False, autoflush=False, twophase=True) session_factory.configure(binds={db1Base: DB_ENGINES['db1'], db2Base: DB_ENGINES['db2']}) Session = scoped_session(session_factory) When I do session.query(Model) , sqlalchemy is able to figure out which engine to use depending on which base the model inherits from. But if I instead use session.execute(text(query), params) i'll get the error UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session. Because sqlalchemy cannot figure out which engine to use. Is there a way to explicitly or implicitly specify which engine