Thursday, December 12, 2024

A tool for remembering bash history

Sometimes we organize our codes in many repos and hence many directories. Remembering the directories is a nightmare. So, I created this simple tool.

Which you may refer here dirhistory tool .

Also, with fzf and fzf-tmux scripts available, We can have the wonderful commands such as fd, fe, pdir, ad etc.

fd -> cd to any remembered directory
fe -> find a file and open with vim
pdir --> cd to any parent dir
ad --> cd to any child dir

The bash functions are defined in the below script here:

ad() { maxdepth=5 if [ $# -gt 0 ] ; then maxdepth=$1 fi DIR=`find * -maxdepth $maxdepth -type d -print 2> /dev/null | fzf-tmux` \ && xcd "$DIR" } fd() { DIR=`cat ~/.dirs/dirs.txt 2> /dev/null | fzf-tmux` \ && xcd "$DIR" } fe() { IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0)) [[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}" } pdir() { local declare dirs=() get_parent_dirs() { if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi if [[ "${1}" == '/' ]]; then for _dir in "${dirs[@]}"; do echo $_dir; done else get_parent_dirs $(dirname "$1") fi } local DIR=$(get_parent_dirs $(realpath "${1:-$PWD}") | fzf-tmux --tac) xcd "$DIR" } # fd - cd to selected directory xd() { local dir dir=$(find ${1:-.} -path '*/\.*' -prune \ -o -type d -print 2> /dev/null | fzf +m) && cd "$dir" 
} 

 

Tuesday, September 10, 2024

Getting number of trees in a XGBoost model

 We can dump the number of trees in the xgboost model by below method:

Load the model and save it as a JSON file. Of course this step is not needed if the model was saved as a JSON already.

Then simply extract the num_trees information from the JSON file.

Below code snippet we may use for the same:


  1. import sys
  2. import json
  3. import xgboost as xgb
  4. if len(sys.argv) < 2:
  5. print(f'Usage: {sys.argv[0]} ')
  6. exit(1)
  7. loaded_model = xgb.Booster()
  8. loaded_model.load_model(sys.argv[1])
  9. loaded_model.save_model('/tmp/a_model.json')
  10. with open('/tmp/a_model.json', 'r') as fp:
  11. jsonrepr = json.load(fp)
  12. print(jsonrepr['learner']['gradient_booster']['model']['gbtree_model_param']['num_trees'])


We can run it as shown below (after saving the code snippet to a file xgb_tree_count.py):
python xgb_tree_count.py model-file-path

Sunday, March 17, 2024

Making unsearchable pdf a searchable one

Try this command: $ ocrmypdf unsearchable.pdf this_will_be_searchable.pdf Here is the link for this interesting tool https://github.com/ocrmypdf/OCRmyPDF