12/11/2022

pandas time series resample option

price.resample('M').count().plot(title='Monthly Counts') 

 

W : weekly frequency
M : month end frequency
SM : semi-month end frequency (15th and end of month)
Q : quarter end frequency

9/26/2022

python random function

 import random


mylist = [1, 2, 3]

random.shuffle(mylist) # randomly change the order of the list


print(mylist)

print(random.choice(mylist)) # randomly choose one element from the list

5/25/2022

Seismic ghost notch calculation

 

where  is the source or receiver depth in meters

print one column after another

 guofyuan @ htpvis-025a$ cat test.txt
AAA    111  123
BBB    222  234
CCC    333  345

guofyuan @ htpvis-025a$ awk '{if(maxc<NF)maxc=NF; for(i=1;i<=NF;i++){(a[i]!=""?a[i]=a[i]RS$i:a[i]=$i)} } END{for(i=1;i<=maxc;i++)print a[i]}' test.txt
AAA
BBB
CCC
111
222
333
123
234
345

 

 gawk '{for (i=1; i<=NF; i++) # loop over columns

           data[i][NR]=$i               # store in data[column][line]
      }
      END {for (i=1;i<=NR;i++)          # loop over lines
                for (j=1;j<=NF;j++)     # loop over columns
                     print data[i][j]   # print the given field
      }' file

https://stackoverflow.com/questions/39287224/how-to-print-columns-one-after-the-other-in-bash

5/20/2022

convert a horizontal file to vertical one using awk

 $ cat TBB11-str-ghost-far-tmp.txt | awk '{for(i=1;i<=NF;i++) {print $i}}'


Input

guofyuan @ htpvis-025a$ head TBB11-str-ghost-far-tmp.txt
     0.000     0.000     0.000     0.000     0.000     0.000    -0.001
     0.001    -0.001    -0.002     0.007    -0.013     0.016    -0.012
    -0.006     0.038    -0.076     0.100    -0.087     0.014     0.118
    -0.278     0.401    -0.403     0.213     0.193    -0.762     1.365
    -1.808     1.865    -1.235    -0.757    50.286    57.557     5.330
     5.619   -27.954  -110.350   -86.889   -19.798    -6.313    21.974
    52.920    32.938     6.974     1.216     2.681     6.191     4.807
     2.697     1.438    -0.089     0.905     1.381     0.864     0.263
     0.096     0.493     0.419     0.371     0.348     0.237     0.287
     0.396     0.447     0.389     0.383     0.459     0.519     0.553

output
guofyuan @ htpvis-025a$ head TBB11-str-ghost-far-vertical.txt
0.000
0.000
0.000
0.000
0.000
0.000
-0.001
0.001
-0.001
-0.002

5/02/2022

count the frequency of a list (uniq -c)

 guofyuan @ htpvis-025a$ ls Seq* | nl | sed 's/_Subline_/ /g' | sed 's/Seq//g' | awk '{print $2'} | uniq -c | awk '$1<4'

      3 125



4/15/2022

ImageIO find intensity

 

# Create left ventricle mask
labels, nlabels = ndi.label(mask)
lv_val = labels[128, 128]
lv_mask = np.where(labels == lv_val, 1, 0)

# Find bounding box of left ventricle
bboxes = ndi.find_objects(lv_mask)
print('Number of objects:', len(bboxes))
print('Indices for first box:', bboxes[0])

# Crop to the left ventricle (index 0)
im_lv = im[bboxes[0]]

# Plot the cropped image
plt.imshow(im_lv, cmap='rainbow')
format_and_render_plot()
 
 

4/06/2022

Biomedical Images_1

 Imageio

# Import ImageIO
import imageio
import matplotlib.pyplot as plt 
# Load "chest-220.dcm"
im = imageio.imread("chest-220.dcm")
# Print image attributes
print('Image type:', type(im))
print('Shape of image array:', im.shape)
# Print the available metadata fields
print(im.meta.keys())
print(im.meta['PatientSex']) 
# Draw the image with greater contrast
plt.imshow(im, cmap ='gray', vmin = -200, vmax = 200)
# Remove ticks and axises
plt.axis('off')
# Render the image
plt.show()

# Import ImageIO and NumPy
import imageio
import numpy as np
# Read in each 2D image
im1 = imageio.imread('chest-220.dcm')
im2 = imageio.imread('chest-221.dcm')
im3 = imageio.imread('chest-222.dcm')
# Stack images into a volume
vol = np.stack([im1, im2, im3])
print('Volume dimensions:', vol.shape)
 
# Load the "tcia-chest-ct" directory
vol = imageio.volread("tcia-chest-ct")
# Print image attributes
print('Available metadata:', vol.meta.keys())
print('Shape of image array:', vol.shape)
 
In [5]:
vol.meta['sampling']
Out[5]:
(3.270000000000001, 0.976562, 0.976562)
In [6]:
vol.shape
Out[6]:
(25, 512, 512)
 The field of view = (82, 500, 500)
 

To select a 2D frame, pick a frame for the first axis and select all data 

from the remaining two: vol[0, :, :]

For this exercise, use for loop to plot every 40th slice of vol on a 

separate subplot. matplotlib.pyplot (as plt) has been imported for you.

# Plot the images on a subplots array
fig, axes = plt.subplots(nrows=1, ncols=4)

# Loop through subplots and draw image
for ii in range(4):
im = vol[ii*40, :, :]
axes[ii].imshow(im, cmap='gray')
axes[ii].axis('off')
 
# Select frame from "vol"
im1 = vol[:, 256, :]
im2 = vol[:, :, 256]

# Compute aspect ratios
d0, d1, d2 = vol.meta['sampling']
asp1 = d0 / d2
asp2 = d0/d1

# Plot the images on a subplots array
fig, axes = plt.subplots(nrows=2, ncols=1)
axes[0].imshow(im1, cmap='gray',aspect=asp1)
axes[1].imshow(im2, cmap='gray', aspect=asp2)
plt.show()
 
Intensity: 
# Import SciPy's "ndimage" module
import scipy.ndimage as ndi
# Create a histogram, binned at each possible value
# Change data type could save spaces.
# im_int8 = im.astype(np.unit8)
hist = ndi.histogram(im, min=0, max=256, bins=256)
# Create a cumulative distribution function
cdf = hist.cumsum() / hist.sum()
# Plot the histogram and CDF
fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].plot(hist, label='Histogram')
axes[1].plot(cdf, label='CDF')
format_and_render_plot()








Mask:

# Create skin and bone masks
mask_bone = im>=145
mask_skin = (im>=45)&(im<145)
# Plot the skin (0) and bone (1) masks
fig, axes = plt.subplots(1,2)
axes[0].imshow(mask_skin, cmap='gray')
axes[1].imshow(mask_bone, cmap='gray')
format_and_render_plot()

 








# Import SciPy's "ndimage" module
import scipy.ndimage as ndi
# Screen out non-bone pixels from "im"
mask_bone = im >= 145
im_bone = np.where(mask_bone, im, 0)

# Get the histogram of bone intensities
hist = ndi.histogram(im_bone, min=1, max=255,bins=255)
# Plot masked image and histogram
fig, axes = plt.subplots(2,1)
axes[0].imshow(im_bone)
axes[1].plot(hist)
format_and_render_plot()

 
 
 
 
 
 
 
# Create and tune bone mask
mask_bone = im >=145
mask_dilate = ndi.binary_dilation(mask_bone, iterations=5)
mask_closed = ndi.binary_closing(mask_bone, iterations=5)

# Plot masked images
fig, axes = plt.subplots(1,3)
axes[0].imshow(mask_bone)
axes[1].imshow(mask_dilate)
axes[2].imshow(mask_closed)
format_and_render_plot()


Filters:

# Set filter weights
weights = [[0.11, 0.11, 0.11],
[0.11, 0.11, 0.11],
[0.11, 0.11, 0.11]]

# Convolve the image with the filter
im_filt = ndi.convolve(im, weights)

# Plot the images
fig, axes = plt.subplots(1,2)
axes[0].imshow(im)
axes[1].imshow(im_filt)
format_and_render_plot()
 

 
 
 
 
 
 
 
 
# Smooth "im" with Gaussian filters
im_s1 = ndi.gaussian_filter(im, sigma=1)
im_s3 = ndi.gaussian_filter(im, sigma=3)

# Draw bone masks of each image
fig, axes = plt.subplots(1,3)
axes[0].imshow(im >= 145)
axes[1].imshow(im_s1 >= 145)
axes[2].imshow(im_s3 >= 145)
format_and_render_plot()

 
 
 
 
Edges:
 
# Set weights to detect vertical edges
weights = [[1,0,-1], [1,0,-1], [1,0,-1]]
# Convolve "im" with filter weights
edges = ndi.convolve(im, weights)
# Draw the image in color
plt.imshow(edges, cmap = 'seismic', vmin=-150, vmax=150)
plt.colorbar()
format_and_render_plot()
 
 
 
 
 
 
 
 
 
 
# Apply Sobel filter along both axes
sobel_ax0 = ndi.sobel(im, axis=0)
sobel_ax1 = ndi.sobel(im, axis=1)
# Calculate edge magnitude
edges = np.sqrt(np.square(sobel_ax0) + np.square(sobel_ax1))

# Plot edge magnitude
plt.imshow(edges, cmap='gray', vmax=75)
format_and_render_plot()
 

 
 
 
 
 
 
 
 
 
 
 
 
 

2/07/2022

python testEqual function

 testEqual compares what is returned by the distance function and the 0 (the correct answer).


import test

def distance(x1, y1, x2, y2):

    dx = x2 - x1

    dy = y2 - y1

    dsquared = dx**2 + dy**2

    result = dsquared**0.5

    return result


test.testEqual(distance(1,2, 1,2), 0)

test.testEqual(distance(1,2, 4,6), 5)

test.testEqual(distance(0,0, 1,1), 1.41421)




Pass
Pass
Pass

2/02/2022

python list range not printing the last one, last one need to be larger than the one you want to print

 For even numbers we want to start at 0 and count by 2’s. So if we wanted the first 10 even numbers we would use range(0,19,2). The most general form of the range is range(start, beyondLast, step).


print(list(range(0, 20, 2)))

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Not printing 20 as you would thought

The randrange function generates an integer between its lower and upper
argument, using the same semantics as range — so the lower bound is included, but
the upper bound is excluded.

diceThrow = random.randrange(1, 7)       
# return an int, one of 1,2,3,4,5,6 But not 7

1/29/2022

python math operator

Division and Reminder

 

 total_secs=7350

hours=total_secs//3600 # hours=2

secs_still_remaining=total_secs%3600 # secs_still_remaining=150

minutes= secs_still_remaining // 60 # minutes=2

secs_finally_remaining=secs_still_remaining %60 # seconds=30










1/23/2022

vnc resolution

 

$ xrandr

 

 

$ xrandr --newmode "2560x1440_60.00"  312.25  2560 2752 3024 3488  1440 1443 1448 1493 -hsync +vsync

 

$ xrandr --addmode VNC-0 "2560x1440_60.00"

 

 

https://superuser.com/questions/223240/changing-screen-resolution-in-centos