I love to share what I've learnt with others. Check out my blog posts and notes about my
academic research, as well as technical solutions on software engineering and data
science challenges. Opinions expressed in this blog are solely my own.
The python native library traceback can provide more details about an (unexpected) error compared to error catching with except Exception as ex: and then examine ex.
Let’s make a function that would result in error for demonstration:
importtracebackdefdo_something_wrong():cc=int("come on!")returncctry:# first catch
do_something_wrong()exceptExceptionasex:print(f"The Exception is here:\n{ex}")try:# second catch
do_something_wrong()except:print(f"Use traceback.format_exc() instead:\n{traceback.format_exc()}")
The first catch would only display
The Exception is here:
invalid literal for int() with base 10: 'come on!'
while the second catch includes not only the error but where it occurs
I wanted to create a GitHub workflow that did web API query and return the results as text files in the repo. Here are several problems I’ve solved during the development.
Passing keys and tokens via secrets to the web API
Several tokens and secrets are necessary to query the web API. I stored that as GitHub secrets and access them in the workflow file via:
After running run_script.py, there will be several .txt files produced in the directory data_dir/ inside the repository which I want to push to the GitHub repository. I tried committing and pushing the files with actions/checkout@v4 but it does not work:
...- name:add files to git# Below is a version that does not workuses:actions/checkout@v4with:token:${{ secrets.REPO_TOKEN }}-name:do the actual pushrun:|git add data_dir/*.txtgit commit -m "add files"git push
Running this, I receive an error: nothing to commit, working tree clean. Error: Process completed with exit code 1. .
The version that works eventually looks like this:
Note that it would commit all files produced to the repository, including some unwanted cached files. Therefore, I included a step before this to clean up the files:
A new release (v2.0.0) of the python package falwa has been published to cope with the deprecation of numpy.disutils in python 3.12 and involves some changes in installation procedures, which you can find in README section “Package Installation”.
Great thanks to Christopher Polster for figuring out a timely and clean solution for migration to python 3.12. 👏 For details and references related to this migration, users can refer to Christopher’s Pull request.
To train deep learning model written in PyTorch with Big Data in a distributed manner, we use BigDL-Orca at work. 🛠️
Compared to the Keras interface of BigDL, PyTorch (Orca) supports customization of various components for Deep Learning. For example, using bigdl-dllib keras API, you are constrained to use only available operations in Autograd module to customize loss functions, while you can do whatever you like in PyTorch (Orca) by creating customized subclass of torch.nn.modules.loss._Loss . 😁
One drawback of Orca, though, is the mysterious error logging, as what happened within the java class (i.e. what causes the error) is not logged at all. I got stuck in error during model training, but what I got from the Spark log was just socket timeout . There can be many possibilities, but the one I encountered was about the size of train_data.
Great thanks to my colleague Kevin Mueller who figured out the cause 🙏 - when the partitions contain different number of batches in Orca, some barriers can never be reached and that results in such error.
To get around this, I dropped some rows to make sure the total size of train_data is a multiple of batch size:
I wrote a blog post in 2021 about how to integrate pytest coverage check to GitHub Workflow.
To run coverage locally, execute coverage run --source=falwa -m pytest tests/ && coverage report -m would yield the report (this is from the PR for falwa release 1.3):
Our team lead shared with us some useful learning materials on advanced CS topics not covered in class: The Missing Semester of Your CS Education from MIT. I’ll spend some time to read this.
Below is the email I sent to the users of GitHub repo hn2016_falwa:
I am writing to inform you two recent releases of the GitHub repo v1.0.0 (major release) and v1.1.0 (a bug fix). You can refer to the release notes for the details. There are two important changes in v1.0.0:
The python package is renamed from hn2016_falwa to falwa since this package implements finite-amplitude wave activity and flux calculation beyond those published in Huang and Nakamura (2016). The GitHub repo URL remains the same: https://github.com/csyhuang/hn2016_falwa/ . The package can be installed via pip as well: https://pypi.org/project/falwa/
It happens that the bug fix release v0.7.2 has a bug in the code such that it over-corrects the nonlinear zonal advective flux term. v1.0.0 fix this bug. Thanks Christopher Polster for spotting the error. The fix requires re-compilation of fortran modules.
The rest of the details can be found in the release notes:
[Updated on 2023/12/11] After some research, it seems that scikit-build would be a continuously maintained solution: https://scikit-build.readthedocs.io/
We published an important bugfix release hn2016_falwa v0.7.2, which requires recompilation of fortran modules.
Two weeks ago, we discovered that there is a mistake in the derivation of expression of nonlinear zonal advective flux term, which leads to an underestimation of the nonlinear zonal advective flux component.
We will submit corrigendum for Neal et al. (2022, GRL) and Nakamura and Huang (2018, Science) to update the numerical results. The correct derivation of the flux expression can be found in the corrected supplementary materials of NH18 (to be submitted soon). There is no change in conclusions in any of the articles.
Please refer to Issue #83 for the numerical details and preliminary updated figures in NHN22 and NH18:
Thank you for your attention and let us know if we can help.
Thrilled that my open-source climate data analysis package gets sponsored by JetBrains Licenses for Open Source Development. 🥳
I’m really glad I started this project in 2016 when I was still in graduate school, with the hope that the climate data diagnostic proposed in my thesis can be applied by other more easily. Even though I have been working in industry after finishing my PhD, by maintaining this package, I’ve established valuable connections with many academic researchers. 😊 I’m grateful that JetBrains support open-source community and encourage the culture of sharing.
There are 2 parts in this post. Part I reviews the idea of Quantile Transformer. Part II shows the implementation of Quantile Transformer in Spark using Pandas UDF.
Part I: Quantile Transformer transforms data of arbitrary distribution to normal (or uniform) distribution
Problem Statement: I have some individuals (id) with 3 attributes of different distributions. I want to combine them linearly and also want to make sure the outcome follows a normal distribution.
In python, I create a toy dataset with column id, and 3 columns corresponding to random variables following different distributions:
importnumpyasnpimportpandasaspdimportscipyimportmathimportmatplotlib.pyplotaspltnum_of_items=10000# the size of my population
df=pd.DataFrame({'id':[str(i)foriinnp.arange(num_of_items)],'uniform':np.random.rand(num_of_items),'power_law':np.random.power(3,num_of_items),'exponential':np.random.exponential(1,num_of_items)})
Let’s say we want to map these values to a normal distribution with mean=0.5 and standard deviation=0.15. To look up the corresponding value in the CDF of normal distribution, we can use scipy.stats.norm.ppf:
I would get results of the same distribution. On the right, I show the results from linear combination of the original values for comparison:
Another combination strategy would be to get the max value among the 3 columns. The transformed variable follows similar distribution, dispute the mean shifts to larger values.
Part II: Implementation of Quantile Transformer in Spark
Given the introduction of Pandas UDF in Spark, the implementation is relatively simple. If ranking is too expensive, you can consider using approximate quantile instead.
(Note: Later I realized that the newest Spark version has pyspark.pandas.DataFrame.rank, see Spark documentation. That’s not available at my work station yet.)
You can append the transformed value to the original dataframe:
I have a dataframe df with columns id (integers) and document (string):
root
|-- id: integer
|-- document: string
Each id is associated with multiple documents. Each document would be transformed into a vector representation (embedding of dimension 100) using Spark NLP. Eventually, I want to get the average of vectors associated with each id.
When testing with small amount of data, i.e. 10k id with each associated with ~100 document, pyspark.ml.stat.Summarizer does the job quickly:
However, the real situation is that I have to deal with Big Data that consists of 100M distinct id and 200M distinct document. Each id can be associated with at most 30k document. The time taken to (1) attach embedding using Spark NLP and (2) aggregate the vectors per id took me 10 hours, which is too slow!
Eventually, I figured out a way to do the same thing while having the computing time shortened to ~2 hours.
Thanks to my colleague who spot out the bottleneck - step (1) is indeed not slow. It was step (2) that takes most of the time when there is a huge number of id to work with. In this scenario, the aggregation of values in 100 separate columns is actually faster than the aggregation of 100-dimension vectors.
Here is what I do to optimize the procedures:
1. Obtain vector representation as array for distinct document
One can specify in sparknlp.base.EmbeddingFinisher whether you want to output a vector or an array. To make the split easier, I set the output as array: