Commit 272b1d31 authored by Flavia Sperati's avatar Flavia Sperati
Browse files

new folder

parent 21f1f528
import os
import zipfile
import json
import numpy as np
import matplotlib.pyplot as plt
from distutils.dir_util import copy_tree
import subprocess
config = {
"npz-file":"C:/Users/flavia.sperati/Local/Python/VisualStudio/Python_training/2022_11_09/10_Data/1652792066_streamed.npz",
"report_folder":"C:/Users/flavia.sperati/Local/Python/VisualStudio/Python_training/2022_11_09/20_Output/report1"
}
#print(config)
try:
os.makedirs(config['report_folder'])
except:
pass
npz_fn = config['npz-file']
zf = zipfile.ZipFile(npz_fn)
config.update(json.load(zf.open("config.json")))
zf.close()
#print(config['npz_header'])
#print(config)
npzfile = np.load(npz_fn)
print(npzfile.files)
waveform : np.array = npzfile['waveform']
#print(waveform.shape)
x = waveform[0]
V_In = waveform[1]
V_C = waveform[2]
V_R = V_In - V_C
plt.plot(x,V_In)
plt.savefig(os.path.join(config['report_folder'],"fig1.pdf"))
plt.close()
plt.plot(x,V_C)
plt.savefig(os.path.join(config['report_folder'],"fig2.pdf"))
plt.close()
####
#json for auto-generated
#yaml for user generated
copy_tree("tex_report", config['report_folder'])
os.chdir(config['report_folder'])
subprocess.run(["pdflatex", "report.tex"])
\ No newline at end of file
import random
import math
import time
import threading
import multiprocessing
import numpy as np
pi_steps = 1000000
N_pi_calc = 10
def calc_pi(n = 1000, pi_sum = None):
inside = 0
for i in range(int(n)):
x = random.random()
y = random.random()
r = math.sqrt(x**2 + y**2)
if r <=1.0:
inside += 1
pi = 4 * inside/n
if pi_sum != None:
pi_sum += pi
return pi
def calc_pi_sync(n = 1000, pi_sum = None):
inside = 0
for i in range(int(n)):
x = random.random()
y = random.random()
r = math.sqrt(x**2 + y**2)
if r <=1.0:
inside += 1
pi = 4 * inside/n
pi_sum.value += pi
return pi
def calc_pi_numpy(n = 1000, pi_sum = None):
xs = np.random.rand(n)
ys = np.random.rand(n)
rs = np.sqrt(xs**2 + ys**2)
inside = len(np.where(rs <= 1.0)[0])
pi = 4 * inside/n
return pi
if __name__ == '__main__':
print("\"Normal\" Python")
t0 = time.perf_counter_ns()
pi_sum = 0
for N in range(N_pi_calc):
pi_sum += calc_pi(pi_steps)
pi = pi_sum/N_pi_calc
t1 = time.perf_counter_ns()
elapsed_ms_norm = (t1-t0)/1e6
print(f"time elapsed python only {elapsed_ms_norm:.3f} ms")
print(pi)
print("")
# print("with threading (doesn't work)")
# t0 = time.perf_counter_ns()
# threads = []
# pi_sum = 0
# for N in range(N_pi_calc):
# threads.append(threading.Thread(target=calc_pi, args=(pi_steps, pi_sum)))
# threads[-1].start()
# for thread in threads:
# thread.join()
# pi = pi_sum/N_pi_calc
# t1 = time.perf_counter_ns()
# elapsed_ms = (t1-t0)/1e6
# print(f"time elapsed threading {elapsed_ms:.3f} ms")
# print(pi)
# print("")
print("with threading")
t0 = time.perf_counter_ns()
threads = []
pi_sum = multiprocessing.Value('f', 0)
for N in range(N_pi_calc):
threads.append(threading.Thread(target=calc_pi_sync, args=(pi_steps, pi_sum)))
threads[-1].start()
for thread in threads:
thread.join()
pi = pi_sum.value/N_pi_calc
t1 = time.perf_counter_ns()
elapsed_ms = (t1-t0)/1e6
ratio = int(elapsed_ms_norm / elapsed_ms) if elapsed_ms_norm >= elapsed_ms else round(elapsed_ms_norm / elapsed_ms, 2)
print(f"time elapsed threading {elapsed_ms:.3f} ms ({ratio}x)")
print(pi)
print("")
# print("with multiprocessing")
# t0 = time.perf_counter_ns()
# processes = []
# pi_sum = multiprocessing.Value('f', 0)
# for N in range(N_pi_calc):
# processes.append(multiprocessing.Process(target=calc_pi_sync, args=(pi_steps, pi_sum)))
# processes[-1].start()
# for process in processes:
# process.join()
# pi = pi_sum.value/N_pi_calc
# t1 = time.perf_counter_ns()
# elapsed_ms = (t1-t0)/1e6
# ratio = int(elapsed_ms_norm / elapsed_ms) if elapsed_ms >= elapsed_ms_norm else round(elapsed_ms_norm / elapsed_ms, 2)
# print(f"time elapsed multiprocessing {elapsed_ms:.3f} ms ({ratio}x)")
# print(pi)
# print("")
print("Numpy")
t0 = time.perf_counter_ns()
pi_sum = 0
for N in range(N_pi_calc):
pi_sum += calc_pi_numpy(pi_steps)
pi = pi_sum/N_pi_calc
t1 = time.perf_counter_ns()
elapsed_ms = (t1-t0)/1e6
ratio = int(elapsed_ms_norm / elapsed_ms) if elapsed_ms_norm >= elapsed_ms else round(elapsed_ms_norm / elapsed_ms, 2)
print(f"time elapsed Numpy {elapsed_ms:.3f} ms ({ratio}x)")
print(pi)
print("")
import requests
import time
import threading
import multiprocessing
def download_site(url, session):
with session.get(url) as response:
pass
def download_all_sites(sites):
with requests.Session() as session:
for url in sites:
download_site(url, session)
def download_all_sites_threaded(sites):
with requests.Session() as session:
threads = []
for url in sites:
threads.append(threading.Thread(target=download_site, args = (url, session)))
threads[-1].start()
for thread in threads:
thread.join()
def download_all_sites_processed(sites):
with requests.Session() as session:
processes = []
for url in sites:
processes.append(multiprocessing.Process(target=download_site, args = (url, session)))
processes[-1].start()
for process in processes:
process.join()
if __name__ == '__main__':
sites = [
"https://www.jython.org",
"http://olympus.realpython.org/dice",
] * 20
print("sequential")
t0 = time.perf_counter_ns()
download_all_sites(sites)
t1 = time.perf_counter_ns()
elapsed_ms = (t1-t0)/1e6
print(f"sequential: {elapsed_ms:.3f} ms\n")
print("threaded")
t0 = time.perf_counter_ns()
download_all_sites_threaded(sites)
t1 = time.perf_counter_ns()
elapsed_ms = (t1-t0)/1e6
print(f"threaded: {elapsed_ms:.3f} ms\n")
print("processed")
t0 = time.perf_counter_ns()
download_all_sites_processed(sites)
t1 = time.perf_counter_ns()
elapsed_ms = (t1-t0)/1e6
print(f"processed: {elapsed_ms:.3f} ms")
\ No newline at end of file
\documentclass[12pt]{article}
\usepackage{graphicx}
\begin{document}
\section{Intro}
\section{Waveforms}
\includegraphics[]{fig1.pdf}
\end{document}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment